Blame view

fs/reiserfs/ibalance.c 34.3 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /*
   * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
   */
17093991a   Fabian Frederick   fs/reiserfs: use ...
4
  #include <linux/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
6
  #include <linux/string.h>
  #include <linux/time.h>
f466c6fdb   Al Viro   move private bits...
7
  #include "reiserfs.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
10
  #include <linux/buffer_head.h>
  
  /* this is one and only function that is used outside (do_balance.c) */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
11
12
  int balance_internal(struct tree_balance *,
  		     int, int, struct item_head *, struct buffer_head **);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13

098297b27   Jeff Mahoney   reiserfs: cleanup...
14
15
16
17
  /*
   * modes of internal_shift_left, internal_shift_right and
   * internal_insert_childs
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
21
22
23
24
  #define INTERNAL_SHIFT_FROM_S_TO_L 0
  #define INTERNAL_SHIFT_FROM_R_TO_S 1
  #define INTERNAL_SHIFT_FROM_L_TO_S 2
  #define INTERNAL_SHIFT_FROM_S_TO_R 3
  #define INTERNAL_INSERT_TO_S 4
  #define INTERNAL_INSERT_TO_L 5
  #define INTERNAL_INSERT_TO_R 6
bd4c625c0   Linus Torvalds   reiserfs: run scr...
25
26
27
28
29
30
  static void internal_define_dest_src_infos(int shift_mode,
  					   struct tree_balance *tb,
  					   int h,
  					   struct buffer_info *dest_bi,
  					   struct buffer_info *src_bi,
  					   int *d_key, struct buffer_head **cf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
32
33
34
35
  	memset(dest_bi, 0, sizeof(struct buffer_info));
  	memset(src_bi, 0, sizeof(struct buffer_info));
  	/* define dest, src, dest parent, dest position */
  	switch (shift_mode) {
098297b27   Jeff Mahoney   reiserfs: cleanup...
36
37
38
  
  	/* used in internal_shift_left */
  	case INTERNAL_SHIFT_FROM_S_TO_L:
bd4c625c0   Linus Torvalds   reiserfs: run scr...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  		src_bi->tb = tb;
  		src_bi->bi_bh = PATH_H_PBUFFER(tb->tb_path, h);
  		src_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, h);
  		src_bi->bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
  		dest_bi->tb = tb;
  		dest_bi->bi_bh = tb->L[h];
  		dest_bi->bi_parent = tb->FL[h];
  		dest_bi->bi_position = get_left_neighbor_position(tb, h);
  		*d_key = tb->lkey[h];
  		*cf = tb->CFL[h];
  		break;
  	case INTERNAL_SHIFT_FROM_L_TO_S:
  		src_bi->tb = tb;
  		src_bi->bi_bh = tb->L[h];
  		src_bi->bi_parent = tb->FL[h];
  		src_bi->bi_position = get_left_neighbor_position(tb, h);
  		dest_bi->tb = tb;
  		dest_bi->bi_bh = PATH_H_PBUFFER(tb->tb_path, h);
  		dest_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, h);
098297b27   Jeff Mahoney   reiserfs: cleanup...
58
59
  		/* dest position is analog of dest->b_item_order */
  		dest_bi->bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
60
61
62
  		*d_key = tb->lkey[h];
  		*cf = tb->CFL[h];
  		break;
098297b27   Jeff Mahoney   reiserfs: cleanup...
63
64
  	/* used in internal_shift_left */
  	case INTERNAL_SHIFT_FROM_R_TO_S:
bd4c625c0   Linus Torvalds   reiserfs: run scr...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  		src_bi->tb = tb;
  		src_bi->bi_bh = tb->R[h];
  		src_bi->bi_parent = tb->FR[h];
  		src_bi->bi_position = get_right_neighbor_position(tb, h);
  		dest_bi->tb = tb;
  		dest_bi->bi_bh = PATH_H_PBUFFER(tb->tb_path, h);
  		dest_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, h);
  		dest_bi->bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
  		*d_key = tb->rkey[h];
  		*cf = tb->CFR[h];
  		break;
  
  	case INTERNAL_SHIFT_FROM_S_TO_R:
  		src_bi->tb = tb;
  		src_bi->bi_bh = PATH_H_PBUFFER(tb->tb_path, h);
  		src_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, h);
  		src_bi->bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
  		dest_bi->tb = tb;
  		dest_bi->bi_bh = tb->R[h];
  		dest_bi->bi_parent = tb->FR[h];
  		dest_bi->bi_position = get_right_neighbor_position(tb, h);
  		*d_key = tb->rkey[h];
  		*cf = tb->CFR[h];
  		break;
  
  	case INTERNAL_INSERT_TO_L:
  		dest_bi->tb = tb;
  		dest_bi->bi_bh = tb->L[h];
  		dest_bi->bi_parent = tb->FL[h];
  		dest_bi->bi_position = get_left_neighbor_position(tb, h);
  		break;
  
  	case INTERNAL_INSERT_TO_S:
  		dest_bi->tb = tb;
  		dest_bi->bi_bh = PATH_H_PBUFFER(tb->tb_path, h);
  		dest_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, h);
  		dest_bi->bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
  		break;
  
  	case INTERNAL_INSERT_TO_R:
  		dest_bi->tb = tb;
  		dest_bi->bi_bh = tb->R[h];
  		dest_bi->bi_parent = tb->FR[h];
  		dest_bi->bi_position = get_right_neighbor_position(tb, h);
  		break;
  
  	default:
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
112
113
  		reiserfs_panic(tb->tb_sb, "ibalance-1",
  			       "shift type is unknown (%d)",
bd4c625c0   Linus Torvalds   reiserfs: run scr...
114
115
  			       shift_mode);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
  }
098297b27   Jeff Mahoney   reiserfs: cleanup...
117
118
  /*
   * Insert count node pointers into buffer cur before position to + 1.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
120
   * Insert count items into buffer cur before position to.
   * Items and node pointers are specified by inserted and bh respectively.
bd4c625c0   Linus Torvalds   reiserfs: run scr...
121
122
123
124
125
   */
  static void internal_insert_childs(struct buffer_info *cur_bi,
  				   int to, int count,
  				   struct item_head *inserted,
  				   struct buffer_head **bh)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  	struct buffer_head *cur = cur_bi->bi_bh;
  	struct block_head *blkh;
  	int nr;
  	struct reiserfs_key *ih;
  	struct disk_child new_dc[2];
  	struct disk_child *dc;
  	int i;
  
  	if (count <= 0)
  		return;
  
  	blkh = B_BLK_HEAD(cur);
  	nr = blkh_nr_item(blkh);
  
  	RFALSE(count > 2, "too many children (%d) are to be inserted", count);
  	RFALSE(B_FREE_SPACE(cur) < count * (KEY_SIZE + DC_SIZE),
  	       "no enough free space (%d), needed %d bytes",
  	       B_FREE_SPACE(cur), count * (KEY_SIZE + DC_SIZE));
  
  	/* prepare space for count disk_child */
  	dc = B_N_CHILD(cur, to + 1);
  
  	memmove(dc + count, dc, (nr + 1 - (to + 1)) * DC_SIZE);
  
  	/* copy to_be_insert disk children */
  	for (i = 0; i < count; i++) {
a228bf8f0   Jeff Mahoney   reiserfs: cleanup...
153
  		put_dc_size(&new_dc[i],
bd4c625c0   Linus Torvalds   reiserfs: run scr...
154
  			    MAX_CHILD_SIZE(bh[i]) - B_FREE_SPACE(bh[i]));
a228bf8f0   Jeff Mahoney   reiserfs: cleanup...
155
  		put_dc_block_number(&new_dc[i], bh[i]->b_blocknr);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
156
157
158
159
  	}
  	memcpy(dc, new_dc, DC_SIZE * count);
  
  	/* prepare space for count items  */
4cf5f7add   Jeff Mahoney   reiserfs: cleanup...
160
  	ih = internal_key(cur, ((to == -1) ? 0 : to));
bd4c625c0   Linus Torvalds   reiserfs: run scr...
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  
  	memmove(ih + count, ih,
  		(nr - to) * KEY_SIZE + (nr + 1 + count) * DC_SIZE);
  
  	/* copy item headers (keys) */
  	memcpy(ih, inserted, KEY_SIZE);
  	if (count > 1)
  		memcpy(ih + 1, inserted + 1, KEY_SIZE);
  
  	/* sizes, item number */
  	set_blkh_nr_item(blkh, blkh_nr_item(blkh) + count);
  	set_blkh_free_space(blkh,
  			    blkh_free_space(blkh) - count * (DC_SIZE +
  							     KEY_SIZE));
  
  	do_balance_mark_internal_dirty(cur_bi->tb, cur, 0);
  
  	/*&&&&&&&&&&&&&&&&&&&&&&&& */
  	check_internal(cur);
  	/*&&&&&&&&&&&&&&&&&&&&&&&& */
  
  	if (cur_bi->bi_parent) {
  		struct disk_child *t_dc =
  		    B_N_CHILD(cur_bi->bi_parent, cur_bi->bi_position);
  		put_dc_size(t_dc,
  			    dc_size(t_dc) + (count * (DC_SIZE + KEY_SIZE)));
  		do_balance_mark_internal_dirty(cur_bi->tb, cur_bi->bi_parent,
  					       0);
  
  		/*&&&&&&&&&&&&&&&&&&&&&&&& */
  		check_internal(cur_bi->bi_parent);
  		/*&&&&&&&&&&&&&&&&&&&&&&&& */
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
195
  
  }
098297b27   Jeff Mahoney   reiserfs: cleanup...
196
197
198
199
  /*
   * Delete del_num items and node pointers from buffer cur starting from
   * the first_i'th item and first_p'th pointers respectively.
   */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
200
201
202
  static void internal_delete_pointers_items(struct buffer_info *cur_bi,
  					   int first_p,
  					   int first_i, int del_num)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  	struct buffer_head *cur = cur_bi->bi_bh;
  	int nr;
  	struct block_head *blkh;
  	struct reiserfs_key *key;
  	struct disk_child *dc;
  
  	RFALSE(cur == NULL, "buffer is 0");
  	RFALSE(del_num < 0,
  	       "negative number of items (%d) can not be deleted", del_num);
  	RFALSE(first_p < 0 || first_p + del_num > B_NR_ITEMS(cur) + 1
  	       || first_i < 0,
  	       "first pointer order (%d) < 0 or "
  	       "no so many pointers (%d), only (%d) or "
  	       "first key order %d < 0", first_p, first_p + del_num,
  	       B_NR_ITEMS(cur) + 1, first_i);
  	if (del_num == 0)
  		return;
  
  	blkh = B_BLK_HEAD(cur);
  	nr = blkh_nr_item(blkh);
  
  	if (first_p == 0 && del_num == nr + 1) {
  		RFALSE(first_i != 0,
  		       "1st deleted key must have order 0, not %d", first_i);
  		make_empty_node(cur_bi);
  		return;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
231

bd4c625c0   Linus Torvalds   reiserfs: run scr...
232
233
234
235
236
237
238
239
240
  	RFALSE(first_i + del_num > B_NR_ITEMS(cur),
  	       "first_i = %d del_num = %d "
  	       "no so many keys (%d) in the node (%b)(%z)",
  	       first_i, del_num, first_i + del_num, cur, cur);
  
  	/* deleting */
  	dc = B_N_CHILD(cur, first_p);
  
  	memmove(dc, dc + del_num, (nr + 1 - first_p - del_num) * DC_SIZE);
4cf5f7add   Jeff Mahoney   reiserfs: cleanup...
241
  	key = internal_key(cur, first_i);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
  	memmove(key, key + del_num,
  		(nr - first_i - del_num) * KEY_SIZE + (nr + 1 -
  						       del_num) * DC_SIZE);
  
  	/* sizes, item number */
  	set_blkh_nr_item(blkh, blkh_nr_item(blkh) - del_num);
  	set_blkh_free_space(blkh,
  			    blkh_free_space(blkh) +
  			    (del_num * (KEY_SIZE + DC_SIZE)));
  
  	do_balance_mark_internal_dirty(cur_bi->tb, cur, 0);
  	/*&&&&&&&&&&&&&&&&&&&&&&& */
  	check_internal(cur);
  	/*&&&&&&&&&&&&&&&&&&&&&&& */
  
  	if (cur_bi->bi_parent) {
  		struct disk_child *t_dc;
  		t_dc = B_N_CHILD(cur_bi->bi_parent, cur_bi->bi_position);
  		put_dc_size(t_dc,
  			    dc_size(t_dc) - (del_num * (KEY_SIZE + DC_SIZE)));
  
  		do_balance_mark_internal_dirty(cur_bi->tb, cur_bi->bi_parent,
  					       0);
  		/*&&&&&&&&&&&&&&&&&&&&&&&& */
  		check_internal(cur_bi->bi_parent);
  		/*&&&&&&&&&&&&&&&&&&&&&&&& */
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
270
271
  
  /* delete n node pointers and items starting from given position */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
272
  static void internal_delete_childs(struct buffer_info *cur_bi, int from, int n)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
274
  	int i_from;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
275

bd4c625c0   Linus Torvalds   reiserfs: run scr...
276
  	i_from = (from == 0) ? from : from - 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
277

098297b27   Jeff Mahoney   reiserfs: cleanup...
278
279
280
  	/*
  	 * delete n pointers starting from `from' position in CUR;
  	 * delete n keys starting from 'i_from' position in CUR;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
281
282
  	 */
  	internal_delete_pointers_items(cur_bi, from, i_from, n);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
  }
098297b27   Jeff Mahoney   reiserfs: cleanup...
284
285
286
287
288
289
290
  /*
   * copy cpy_num node pointers and cpy_num - 1 items from buffer src to buffer
   * dest
   * last_first == FIRST_TO_LAST means that we copy first items
   *                             from src to tail of dest
   * last_first == LAST_TO_FIRST means that we copy last items
   *                             from src to head of dest
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
291
   */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
292
293
294
  static void internal_copy_pointers_items(struct buffer_info *dest_bi,
  					 struct buffer_head *src,
  					 int last_first, int cpy_num)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
295
  {
098297b27   Jeff Mahoney   reiserfs: cleanup...
296
297
298
299
300
  	/*
  	 * ATTENTION! Number of node pointers in DEST is equal to number
  	 * of items in DEST  as delimiting key have already inserted to
  	 * buffer dest.
  	 */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  	struct buffer_head *dest = dest_bi->bi_bh;
  	int nr_dest, nr_src;
  	int dest_order, src_order;
  	struct block_head *blkh;
  	struct reiserfs_key *key;
  	struct disk_child *dc;
  
  	nr_src = B_NR_ITEMS(src);
  
  	RFALSE(dest == NULL || src == NULL,
  	       "src (%p) or dest (%p) buffer is 0", src, dest);
  	RFALSE(last_first != FIRST_TO_LAST && last_first != LAST_TO_FIRST,
  	       "invalid last_first parameter (%d)", last_first);
  	RFALSE(nr_src < cpy_num - 1,
  	       "no so many items (%d) in src (%d)", cpy_num, nr_src);
  	RFALSE(cpy_num < 0, "cpy_num less than 0 (%d)", cpy_num);
  	RFALSE(cpy_num - 1 + B_NR_ITEMS(dest) > (int)MAX_NR_KEY(dest),
  	       "cpy_num (%d) + item number in dest (%d) can not be > MAX_NR_KEY(%d)",
  	       cpy_num, B_NR_ITEMS(dest), MAX_NR_KEY(dest));
  
  	if (cpy_num == 0)
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
323
324
  
  	/* coping */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
325
326
  	blkh = B_BLK_HEAD(dest);
  	nr_dest = blkh_nr_item(blkh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
327

bd4c625c0   Linus Torvalds   reiserfs: run scr...
328
329
330
331
332
333
334
  	/*dest_order = (last_first == LAST_TO_FIRST) ? 0 : nr_dest; */
  	/*src_order = (last_first == LAST_TO_FIRST) ? (nr_src - cpy_num + 1) : 0; */
  	(last_first == LAST_TO_FIRST) ? (dest_order = 0, src_order =
  					 nr_src - cpy_num + 1) : (dest_order =
  								  nr_dest,
  								  src_order =
  								  0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335

bd4c625c0   Linus Torvalds   reiserfs: run scr...
336
337
  	/* prepare space for cpy_num pointers */
  	dc = B_N_CHILD(dest, dest_order);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
338

bd4c625c0   Linus Torvalds   reiserfs: run scr...
339
  	memmove(dc + cpy_num, dc, (nr_dest - dest_order) * DC_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
340
341
  
  	/* insert pointers */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
342
343
344
  	memcpy(dc, B_N_CHILD(src, src_order), DC_SIZE * cpy_num);
  
  	/* prepare space for cpy_num - 1 item headers */
4cf5f7add   Jeff Mahoney   reiserfs: cleanup...
345
  	key = internal_key(dest, dest_order);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
346
347
348
349
350
  	memmove(key + cpy_num - 1, key,
  		KEY_SIZE * (nr_dest - dest_order) + DC_SIZE * (nr_dest +
  							       cpy_num));
  
  	/* insert headers */
4cf5f7add   Jeff Mahoney   reiserfs: cleanup...
351
  	memcpy(key, internal_key(src, src_order), KEY_SIZE * (cpy_num - 1));
bd4c625c0   Linus Torvalds   reiserfs: run scr...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
  
  	/* sizes, item number */
  	set_blkh_nr_item(blkh, blkh_nr_item(blkh) + (cpy_num - 1));
  	set_blkh_free_space(blkh,
  			    blkh_free_space(blkh) - (KEY_SIZE * (cpy_num - 1) +
  						     DC_SIZE * cpy_num));
  
  	do_balance_mark_internal_dirty(dest_bi->tb, dest, 0);
  
  	/*&&&&&&&&&&&&&&&&&&&&&&&& */
  	check_internal(dest);
  	/*&&&&&&&&&&&&&&&&&&&&&&&& */
  
  	if (dest_bi->bi_parent) {
  		struct disk_child *t_dc;
  		t_dc = B_N_CHILD(dest_bi->bi_parent, dest_bi->bi_position);
  		put_dc_size(t_dc,
  			    dc_size(t_dc) + (KEY_SIZE * (cpy_num - 1) +
  					     DC_SIZE * cpy_num));
  
  		do_balance_mark_internal_dirty(dest_bi->tb, dest_bi->bi_parent,
  					       0);
  		/*&&&&&&&&&&&&&&&&&&&&&&&& */
  		check_internal(dest_bi->bi_parent);
  		/*&&&&&&&&&&&&&&&&&&&&&&&& */
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
378
379
  
  }
098297b27   Jeff Mahoney   reiserfs: cleanup...
380
381
382
  /*
   * Copy cpy_num node pointers and cpy_num - 1 items from buffer src to
   * buffer dest.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
383
384
385
386
   * Delete cpy_num - del_par items and node pointers from buffer src.
   * last_first == FIRST_TO_LAST means, that we copy/delete first items from src.
   * last_first == LAST_TO_FIRST means, that we copy/delete last items from src.
   */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
387
388
389
390
  static void internal_move_pointers_items(struct buffer_info *dest_bi,
  					 struct buffer_info *src_bi,
  					 int last_first, int cpy_num,
  					 int del_par)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
392
393
394
395
396
397
398
399
400
  	int first_pointer;
  	int first_item;
  
  	internal_copy_pointers_items(dest_bi, src_bi->bi_bh, last_first,
  				     cpy_num);
  
  	if (last_first == FIRST_TO_LAST) {	/* shift_left occurs */
  		first_pointer = 0;
  		first_item = 0;
098297b27   Jeff Mahoney   reiserfs: cleanup...
401
402
403
404
  		/*
  		 * delete cpy_num - del_par pointers and keys starting for
  		 * pointers with first_pointer, for key - with first_item
  		 */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  		internal_delete_pointers_items(src_bi, first_pointer,
  					       first_item, cpy_num - del_par);
  	} else {		/* shift_right occurs */
  		int i, j;
  
  		i = (cpy_num - del_par ==
  		     (j =
  		      B_NR_ITEMS(src_bi->bi_bh)) + 1) ? 0 : j - cpy_num +
  		    del_par;
  
  		internal_delete_pointers_items(src_bi,
  					       j + 1 - cpy_num + del_par, i,
  					       cpy_num - del_par);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
419
420
421
  }
  
  /* Insert n_src'th key of buffer src before n_dest'th key of buffer dest. */
098297b27   Jeff Mahoney   reiserfs: cleanup...
422
423
424
  static void internal_insert_key(struct buffer_info *dest_bi,
  				/* insert key before key with n_dest number */
  				int dest_position_before,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
425
  				struct buffer_head *src, int src_position)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
426
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
  	struct buffer_head *dest = dest_bi->bi_bh;
  	int nr;
  	struct block_head *blkh;
  	struct reiserfs_key *key;
  
  	RFALSE(dest == NULL || src == NULL,
  	       "source(%p) or dest(%p) buffer is 0", src, dest);
  	RFALSE(dest_position_before < 0 || src_position < 0,
  	       "source(%d) or dest(%d) key number less than 0",
  	       src_position, dest_position_before);
  	RFALSE(dest_position_before > B_NR_ITEMS(dest) ||
  	       src_position >= B_NR_ITEMS(src),
  	       "invalid position in dest (%d (key number %d)) or in src (%d (key number %d))",
  	       dest_position_before, B_NR_ITEMS(dest),
  	       src_position, B_NR_ITEMS(src));
  	RFALSE(B_FREE_SPACE(dest) < KEY_SIZE,
  	       "no enough free space (%d) in dest buffer", B_FREE_SPACE(dest));
  
  	blkh = B_BLK_HEAD(dest);
  	nr = blkh_nr_item(blkh);
  
  	/* prepare space for inserting key */
4cf5f7add   Jeff Mahoney   reiserfs: cleanup...
449
  	key = internal_key(dest, dest_position_before);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
450
451
452
453
  	memmove(key + 1, key,
  		(nr - dest_position_before) * KEY_SIZE + (nr + 1) * DC_SIZE);
  
  	/* insert key */
4cf5f7add   Jeff Mahoney   reiserfs: cleanup...
454
  	memcpy(key, internal_key(src, src_position), KEY_SIZE);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
  
  	/* Change dirt, free space, item number fields. */
  
  	set_blkh_nr_item(blkh, blkh_nr_item(blkh) + 1);
  	set_blkh_free_space(blkh, blkh_free_space(blkh) - KEY_SIZE);
  
  	do_balance_mark_internal_dirty(dest_bi->tb, dest, 0);
  
  	if (dest_bi->bi_parent) {
  		struct disk_child *t_dc;
  		t_dc = B_N_CHILD(dest_bi->bi_parent, dest_bi->bi_position);
  		put_dc_size(t_dc, dc_size(t_dc) + KEY_SIZE);
  
  		do_balance_mark_internal_dirty(dest_bi->tb, dest_bi->bi_parent,
  					       0);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471
  }
098297b27   Jeff Mahoney   reiserfs: cleanup...
472
473
474
475
  /*
   * Insert d_key'th (delimiting) key from buffer cfl to tail of dest.
   * Copy pointer_amount node pointers and pointer_amount - 1 items from
   * buffer src to buffer dest.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476
477
478
479
   * Replace  d_key'th key in buffer cfl.
   * Delete pointer_amount items and node pointers from buffer src.
   */
  /* this can be invoked both to shift from S to L and from R to S */
098297b27   Jeff Mahoney   reiserfs: cleanup...
480
481
482
483
484
  static void internal_shift_left(
  				/*
  				 * INTERNAL_FROM_S_TO_L | INTERNAL_FROM_R_TO_S
  				 */
  				int mode,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
485
486
  				struct tree_balance *tb,
  				int h, int pointer_amount)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
487
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
488
489
490
491
492
493
494
495
496
497
498
  	struct buffer_info dest_bi, src_bi;
  	struct buffer_head *cf;
  	int d_key_position;
  
  	internal_define_dest_src_infos(mode, tb, h, &dest_bi, &src_bi,
  				       &d_key_position, &cf);
  
  	/*printk("pointer_amount = %d
  ",pointer_amount); */
  
  	if (pointer_amount) {
098297b27   Jeff Mahoney   reiserfs: cleanup...
499
500
501
502
  		/*
  		 * insert delimiting key from common father of dest and
  		 * src to node dest into position B_NR_ITEM(dest)
  		 */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
  		internal_insert_key(&dest_bi, B_NR_ITEMS(dest_bi.bi_bh), cf,
  				    d_key_position);
  
  		if (B_NR_ITEMS(src_bi.bi_bh) == pointer_amount - 1) {
  			if (src_bi.bi_position /*src->b_item_order */  == 0)
  				replace_key(tb, cf, d_key_position,
  					    src_bi.
  					    bi_parent /*src->b_parent */ , 0);
  		} else
  			replace_key(tb, cf, d_key_position, src_bi.bi_bh,
  				    pointer_amount - 1);
  	}
  	/* last parameter is del_parameter */
  	internal_move_pointers_items(&dest_bi, &src_bi, FIRST_TO_LAST,
  				     pointer_amount, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
518
519
  
  }
098297b27   Jeff Mahoney   reiserfs: cleanup...
520
521
  /*
   * Insert delimiting key to L[h].
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
522
523
524
525
   * Copy n node pointers and n - 1 items from buffer S[h] to L[h].
   * Delete n - 1 items and node pointers from buffer S[h].
   */
  /* it always shifts from S[h] to L[h] */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
526
527
  static void internal_shift1_left(struct tree_balance *tb,
  				 int h, int pointer_amount)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
528
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
529
530
531
  	struct buffer_info dest_bi, src_bi;
  	struct buffer_head *cf;
  	int d_key_position;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
532

bd4c625c0   Linus Torvalds   reiserfs: run scr...
533
534
  	internal_define_dest_src_infos(INTERNAL_SHIFT_FROM_S_TO_L, tb, h,
  				       &dest_bi, &src_bi, &d_key_position, &cf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
535

098297b27   Jeff Mahoney   reiserfs: cleanup...
536
537
  	/* insert lkey[h]-th key  from CFL[h] to left neighbor L[h] */
  	if (pointer_amount > 0)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
538
539
  		internal_insert_key(&dest_bi, B_NR_ITEMS(dest_bi.bi_bh), cf,
  				    d_key_position);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
540

bd4c625c0   Linus Torvalds   reiserfs: run scr...
541
542
543
  	/* last parameter is del_parameter */
  	internal_move_pointers_items(&dest_bi, &src_bi, FIRST_TO_LAST,
  				     pointer_amount, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
544
  }
098297b27   Jeff Mahoney   reiserfs: cleanup...
545
546
  /*
   * Insert d_key'th (delimiting) key from buffer cfr to head of dest.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
547
548
549
550
   * Copy n node pointers and n - 1 items from buffer src to buffer dest.
   * Replace  d_key'th key in buffer cfr.
   * Delete n items and node pointers from buffer src.
   */
098297b27   Jeff Mahoney   reiserfs: cleanup...
551
552
553
554
555
  static void internal_shift_right(
  				 /*
  				  * INTERNAL_FROM_S_TO_R | INTERNAL_FROM_L_TO_S
  				  */
  				 int mode,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
556
557
  				 struct tree_balance *tb,
  				 int h, int pointer_amount)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
558
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
559
560
561
562
563
564
565
566
567
568
569
  	struct buffer_info dest_bi, src_bi;
  	struct buffer_head *cf;
  	int d_key_position;
  	int nr;
  
  	internal_define_dest_src_infos(mode, tb, h, &dest_bi, &src_bi,
  				       &d_key_position, &cf);
  
  	nr = B_NR_ITEMS(src_bi.bi_bh);
  
  	if (pointer_amount > 0) {
098297b27   Jeff Mahoney   reiserfs: cleanup...
570
571
572
573
  		/*
  		 * insert delimiting key from common father of dest
  		 * and src to dest node into position 0
  		 */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
  		internal_insert_key(&dest_bi, 0, cf, d_key_position);
  		if (nr == pointer_amount - 1) {
  			RFALSE(src_bi.bi_bh != PATH_H_PBUFFER(tb->tb_path, h) /*tb->S[h] */ ||
  			       dest_bi.bi_bh != tb->R[h],
  			       "src (%p) must be == tb->S[h](%p) when it disappears",
  			       src_bi.bi_bh, PATH_H_PBUFFER(tb->tb_path, h));
  			/* when S[h] disappers replace left delemiting key as well */
  			if (tb->CFL[h])
  				replace_key(tb, cf, d_key_position, tb->CFL[h],
  					    tb->lkey[h]);
  		} else
  			replace_key(tb, cf, d_key_position, src_bi.bi_bh,
  				    nr - pointer_amount);
  	}
  
  	/* last parameter is del_parameter */
  	internal_move_pointers_items(&dest_bi, &src_bi, LAST_TO_FIRST,
  				     pointer_amount, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
592
  }
098297b27   Jeff Mahoney   reiserfs: cleanup...
593
594
  /*
   * Insert delimiting key to R[h].
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
595
596
597
598
   * Copy n node pointers and n - 1 items from buffer S[h] to R[h].
   * Delete n - 1 items and node pointers from buffer S[h].
   */
  /* it always shift from S[h] to R[h] */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
599
600
  static void internal_shift1_right(struct tree_balance *tb,
  				  int h, int pointer_amount)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
601
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
602
603
604
605
606
607
  	struct buffer_info dest_bi, src_bi;
  	struct buffer_head *cf;
  	int d_key_position;
  
  	internal_define_dest_src_infos(INTERNAL_SHIFT_FROM_S_TO_R, tb, h,
  				       &dest_bi, &src_bi, &d_key_position, &cf);
098297b27   Jeff Mahoney   reiserfs: cleanup...
608
609
  	/* insert rkey from CFR[h] to right neighbor R[h] */
  	if (pointer_amount > 0)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
610
  		internal_insert_key(&dest_bi, 0, cf, d_key_position);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
611

bd4c625c0   Linus Torvalds   reiserfs: run scr...
612
613
614
  	/* last parameter is del_parameter */
  	internal_move_pointers_items(&dest_bi, &src_bi, LAST_TO_FIRST,
  				     pointer_amount, 1);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
615
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
616

098297b27   Jeff Mahoney   reiserfs: cleanup...
617
618
619
620
  /*
   * Delete insert_num node pointers together with their left items
   * and balance current node.
   */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
621
622
  static void balance_internal_when_delete(struct tree_balance *tb,
  					 int h, int child_pos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
623
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
  	int insert_num;
  	int n;
  	struct buffer_head *tbSh = PATH_H_PBUFFER(tb->tb_path, h);
  	struct buffer_info bi;
  
  	insert_num = tb->insert_size[h] / ((int)(DC_SIZE + KEY_SIZE));
  
  	/* delete child-node-pointer(s) together with their left item(s) */
  	bi.tb = tb;
  	bi.bi_bh = tbSh;
  	bi.bi_parent = PATH_H_PPARENT(tb->tb_path, h);
  	bi.bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
  
  	internal_delete_childs(&bi, child_pos, -insert_num);
  
  	RFALSE(tb->blknum[h] > 1,
  	       "tb->blknum[%d]=%d when insert_size < 0", h, tb->blknum[h]);
  
  	n = B_NR_ITEMS(tbSh);
  
  	if (tb->lnum[h] == 0 && tb->rnum[h] == 0) {
  		if (tb->blknum[h] == 0) {
  			/* node S[h] (root of the tree) is empty now */
  			struct buffer_head *new_root;
  
  			RFALSE(n
  			       || B_FREE_SPACE(tbSh) !=
  			       MAX_CHILD_SIZE(tbSh) - DC_SIZE,
  			       "buffer must have only 0 keys (%d)", n);
  			RFALSE(bi.bi_parent, "root has parent (%p)",
  			       bi.bi_parent);
  
  			/* choose a new root */
  			if (!tb->L[h - 1] || !B_NR_ITEMS(tb->L[h - 1]))
  				new_root = tb->R[h - 1];
  			else
  				new_root = tb->L[h - 1];
098297b27   Jeff Mahoney   reiserfs: cleanup...
661
662
663
  			/*
  			 * switch super block's tree root block
  			 * number to the new value */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
664
  			PUT_SB_ROOT_BLOCK(tb->tb_sb, new_root->b_blocknr);
098297b27   Jeff Mahoney   reiserfs: cleanup...
665
  			/*REISERFS_SB(tb->tb_sb)->s_rs->s_tree_height --; */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
666
667
668
669
670
671
672
  			PUT_SB_TREE_HEIGHT(tb->tb_sb,
  					   SB_TREE_HEIGHT(tb->tb_sb) - 1);
  
  			do_balance_mark_sb_dirty(tb,
  						 REISERFS_SB(tb->tb_sb)->s_sbh,
  						 1);
  			/*&&&&&&&&&&&&&&&&&&&&&& */
098297b27   Jeff Mahoney   reiserfs: cleanup...
673
  			/* use check_internal if new root is an internal node */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
674
  			if (h > 1)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
675
676
677
678
679
680
681
682
683
  				check_internal(new_root);
  			/*&&&&&&&&&&&&&&&&&&&&&& */
  
  			/* do what is needed for buffer thrown from tree */
  			reiserfs_invalidate_buffer(tb, tbSh);
  			return;
  		}
  		return;
  	}
098297b27   Jeff Mahoney   reiserfs: cleanup...
684
685
  	/* join S[h] with L[h] */
  	if (tb->L[h] && tb->lnum[h] == -B_NR_ITEMS(tb->L[h]) - 1) {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
686
687
688
689
690
691
692
693
694
695
  
  		RFALSE(tb->rnum[h] != 0,
  		       "invalid tb->rnum[%d]==%d when joining S[h] with L[h]",
  		       h, tb->rnum[h]);
  
  		internal_shift_left(INTERNAL_SHIFT_FROM_S_TO_L, tb, h, n + 1);
  		reiserfs_invalidate_buffer(tb, tbSh);
  
  		return;
  	}
098297b27   Jeff Mahoney   reiserfs: cleanup...
696
697
  	/* join S[h] with R[h] */
  	if (tb->R[h] && tb->rnum[h] == -B_NR_ITEMS(tb->R[h]) - 1) {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
698
699
700
701
702
703
704
705
  		RFALSE(tb->lnum[h] != 0,
  		       "invalid tb->lnum[%d]==%d when joining S[h] with R[h]",
  		       h, tb->lnum[h]);
  
  		internal_shift_right(INTERNAL_SHIFT_FROM_S_TO_R, tb, h, n + 1);
  
  		reiserfs_invalidate_buffer(tb, tbSh);
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
706
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
707

098297b27   Jeff Mahoney   reiserfs: cleanup...
708
709
  	/* borrow from left neighbor L[h] */
  	if (tb->lnum[h] < 0) {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
710
711
712
  		RFALSE(tb->rnum[h] != 0,
  		       "wrong tb->rnum[%d]==%d when borrow from L[h]", h,
  		       tb->rnum[h]);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
713
714
715
716
  		internal_shift_right(INTERNAL_SHIFT_FROM_L_TO_S, tb, h,
  				     -tb->lnum[h]);
  		return;
  	}
098297b27   Jeff Mahoney   reiserfs: cleanup...
717
718
  	/* borrow from right neighbor R[h] */
  	if (tb->rnum[h] < 0) {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
719
720
721
722
723
724
  		RFALSE(tb->lnum[h] != 0,
  		       "invalid tb->lnum[%d]==%d when borrow from R[h]",
  		       h, tb->lnum[h]);
  		internal_shift_left(INTERNAL_SHIFT_FROM_R_TO_S, tb, h, -tb->rnum[h]);	/*tb->S[h], tb->CFR[h], tb->rkey[h], tb->R[h], -tb->rnum[h]); */
  		return;
  	}
098297b27   Jeff Mahoney   reiserfs: cleanup...
725
726
  	/* split S[h] into two parts and put them into neighbors */
  	if (tb->lnum[h] > 0) {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
727
728
729
730
731
732
733
734
735
736
737
738
  		RFALSE(tb->rnum[h] == 0 || tb->lnum[h] + tb->rnum[h] != n + 1,
  		       "invalid tb->lnum[%d]==%d or tb->rnum[%d]==%d when S[h](item number == %d) is split between them",
  		       h, tb->lnum[h], h, tb->rnum[h], n);
  
  		internal_shift_left(INTERNAL_SHIFT_FROM_S_TO_L, tb, h, tb->lnum[h]);	/*tb->L[h], tb->CFL[h], tb->lkey[h], tb->S[h], tb->lnum[h]); */
  		internal_shift_right(INTERNAL_SHIFT_FROM_S_TO_R, tb, h,
  				     tb->rnum[h]);
  
  		reiserfs_invalidate_buffer(tb, tbSh);
  
  		return;
  	}
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
739
740
  	reiserfs_panic(tb->tb_sb, "ibalance-2",
  		       "unexpected tb->lnum[%d]==%d or tb->rnum[%d]==%d",
bd4c625c0   Linus Torvalds   reiserfs: run scr...
741
742
  		       h, tb->lnum[h], h, tb->rnum[h]);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
743
744
  
  /* Replace delimiting key of buffers L[h] and S[h] by the given key.*/
bd4c625c0   Linus Torvalds   reiserfs: run scr...
745
  static void replace_lkey(struct tree_balance *tb, int h, struct item_head *key)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
746
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
747
748
749
  	RFALSE(tb->L[h] == NULL || tb->CFL[h] == NULL,
  	       "L[h](%p) and CFL[h](%p) must exist in replace_lkey",
  	       tb->L[h], tb->CFL[h]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
750

bd4c625c0   Linus Torvalds   reiserfs: run scr...
751
752
  	if (B_NR_ITEMS(PATH_H_PBUFFER(tb->tb_path, h)) == 0)
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753

4cf5f7add   Jeff Mahoney   reiserfs: cleanup...
754
  	memcpy(internal_key(tb->CFL[h], tb->lkey[h]), key, KEY_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
755

bd4c625c0   Linus Torvalds   reiserfs: run scr...
756
  	do_balance_mark_internal_dirty(tb, tb->CFL[h], 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
757
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
758
  /* Replace delimiting key of buffers S[h] and R[h] by the given key.*/
bd4c625c0   Linus Torvalds   reiserfs: run scr...
759
  static void replace_rkey(struct tree_balance *tb, int h, struct item_head *key)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
760
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
761
762
763
764
765
766
  	RFALSE(tb->R[h] == NULL || tb->CFR[h] == NULL,
  	       "R[h](%p) and CFR[h](%p) must exist in replace_rkey",
  	       tb->R[h], tb->CFR[h]);
  	RFALSE(B_NR_ITEMS(tb->R[h]) == 0,
  	       "R[h] can not be empty if it exists (item number=%d)",
  	       B_NR_ITEMS(tb->R[h]));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
767

4cf5f7add   Jeff Mahoney   reiserfs: cleanup...
768
  	memcpy(internal_key(tb->CFR[h], tb->rkey[h]), key, KEY_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
769

bd4c625c0   Linus Torvalds   reiserfs: run scr...
770
  	do_balance_mark_internal_dirty(tb, tb->CFR[h], 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
771
  }
098297b27   Jeff Mahoney   reiserfs: cleanup...
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
  
  /*
   * if inserting/pasting {
   *   child_pos is the position of the node-pointer in S[h] that
   *   pointed to S[h-1] before balancing of the h-1 level;
   *   this means that new pointers and items must be inserted AFTER
   *   child_pos
   * } else {
   *   it is the position of the leftmost pointer that must be deleted
   *   (together with its corresponding key to the left of the pointer)
   *   as a result of the previous level's balancing.
   * }
   */
  
  int balance_internal(struct tree_balance *tb,
  		     int h,	/* level of the tree */
  		     int child_pos,
  		     /* key for insertion on higher level    */
  		     struct item_head *insert_key,
  		     /* node for insertion on higher level */
  		     struct buffer_head **insert_ptr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
793
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
794
795
  	struct buffer_head *tbSh = PATH_H_PBUFFER(tb->tb_path, h);
  	struct buffer_info bi;
098297b27   Jeff Mahoney   reiserfs: cleanup...
796
797
798
799
800
801
  
  	/*
  	 * we return this: it is 0 if there is no S[h],
  	 * else it is tb->S[h]->b_item_order
  	 */
  	int order;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
802
803
804
805
806
807
808
809
810
811
812
813
814
  	int insert_num, n, k;
  	struct buffer_head *S_new;
  	struct item_head new_insert_key;
  	struct buffer_head *new_insert_ptr = NULL;
  	struct item_head *new_insert_key_addr = insert_key;
  
  	RFALSE(h < 1, "h (%d) can not be < 1 on internal level", h);
  
  	PROC_INFO_INC(tb->tb_sb, balance_at[h]);
  
  	order =
  	    (tbSh) ? PATH_H_POSITION(tb->tb_path,
  				     h + 1) /*tb->S[h]->b_item_order */ : 0;
098297b27   Jeff Mahoney   reiserfs: cleanup...
815
816
817
818
  	/*
  	 * Using insert_size[h] calculate the number insert_num of items
  	 * that must be inserted to or deleted from S[h].
  	 */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
819
820
821
822
823
824
825
826
827
828
829
830
831
832
  	insert_num = tb->insert_size[h] / ((int)(KEY_SIZE + DC_SIZE));
  
  	/* Check whether insert_num is proper * */
  	RFALSE(insert_num < -2 || insert_num > 2,
  	       "incorrect number of items inserted to the internal node (%d)",
  	       insert_num);
  	RFALSE(h > 1 && (insert_num > 1 || insert_num < -1),
  	       "incorrect number of items (%d) inserted to the internal node on a level (h=%d) higher than last internal level",
  	       insert_num, h);
  
  	/* Make balance in case insert_num < 0 */
  	if (insert_num < 0) {
  		balance_internal_when_delete(tb, h, child_pos);
  		return order;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
833
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
834

bd4c625c0   Linus Torvalds   reiserfs: run scr...
835
836
  	k = 0;
  	if (tb->lnum[h] > 0) {
098297b27   Jeff Mahoney   reiserfs: cleanup...
837
838
839
840
841
  		/*
  		 * shift lnum[h] items from S[h] to the left neighbor L[h].
  		 * check how many of new items fall into L[h] or CFL[h] after
  		 * shifting
  		 */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
842
843
844
845
846
  		n = B_NR_ITEMS(tb->L[h]);	/* number of items in L[h] */
  		if (tb->lnum[h] <= child_pos) {
  			/* new items don't fall into L[h] or CFL[h] */
  			internal_shift_left(INTERNAL_SHIFT_FROM_S_TO_L, tb, h,
  					    tb->lnum[h]);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
847
848
849
850
851
  			child_pos -= tb->lnum[h];
  		} else if (tb->lnum[h] > child_pos + insert_num) {
  			/* all new items fall into L[h] */
  			internal_shift_left(INTERNAL_SHIFT_FROM_S_TO_L, tb, h,
  					    tb->lnum[h] - insert_num);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
852
853
854
855
856
857
858
859
860
861
862
863
864
865
  			/* insert insert_num keys and node-pointers into L[h] */
  			bi.tb = tb;
  			bi.bi_bh = tb->L[h];
  			bi.bi_parent = tb->FL[h];
  			bi.bi_position = get_left_neighbor_position(tb, h);
  			internal_insert_childs(&bi,
  					       /*tb->L[h], tb->S[h-1]->b_next */
  					       n + child_pos + 1,
  					       insert_num, insert_key,
  					       insert_ptr);
  
  			insert_num = 0;
  		} else {
  			struct disk_child *dc;
098297b27   Jeff Mahoney   reiserfs: cleanup...
866
867
868
869
  			/*
  			 * some items fall into L[h] or CFL[h],
  			 * but some don't fall
  			 */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
870
871
872
873
874
875
876
877
878
879
880
881
882
  			internal_shift1_left(tb, h, child_pos + 1);
  			/* calculate number of new items that fall into L[h] */
  			k = tb->lnum[h] - child_pos - 1;
  			bi.tb = tb;
  			bi.bi_bh = tb->L[h];
  			bi.bi_parent = tb->FL[h];
  			bi.bi_position = get_left_neighbor_position(tb, h);
  			internal_insert_childs(&bi,
  					       /*tb->L[h], tb->S[h-1]->b_next, */
  					       n + child_pos + 1, k,
  					       insert_key, insert_ptr);
  
  			replace_lkey(tb, h, insert_key + k);
098297b27   Jeff Mahoney   reiserfs: cleanup...
883
884
885
886
  			/*
  			 * replace the first node-ptr in S[h] by
  			 * node-ptr to insert_ptr[k]
  			 */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
  			dc = B_N_CHILD(tbSh, 0);
  			put_dc_size(dc,
  				    MAX_CHILD_SIZE(insert_ptr[k]) -
  				    B_FREE_SPACE(insert_ptr[k]));
  			put_dc_block_number(dc, insert_ptr[k]->b_blocknr);
  
  			do_balance_mark_internal_dirty(tb, tbSh, 0);
  
  			k++;
  			insert_key += k;
  			insert_ptr += k;
  			insert_num -= k;
  			child_pos = 0;
  		}
  	}
  	/* tb->lnum[h] > 0 */
  	if (tb->rnum[h] > 0) {
  		/*shift rnum[h] items from S[h] to the right neighbor R[h] */
098297b27   Jeff Mahoney   reiserfs: cleanup...
905
906
907
908
  		/*
  		 * check how many of new items fall into R or CFR
  		 * after shifting
  		 */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
909
910
911
  		n = B_NR_ITEMS(tbSh);	/* number of items in S[h] */
  		if (n - tb->rnum[h] >= child_pos)
  			/* new items fall into S[h] */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
912
913
914
915
  			internal_shift_right(INTERNAL_SHIFT_FROM_S_TO_R, tb, h,
  					     tb->rnum[h]);
  		else if (n + insert_num - tb->rnum[h] < child_pos) {
  			/* all new items fall into R[h] */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
  			internal_shift_right(INTERNAL_SHIFT_FROM_S_TO_R, tb, h,
  					     tb->rnum[h] - insert_num);
  
  			/* insert insert_num keys and node-pointers into R[h] */
  			bi.tb = tb;
  			bi.bi_bh = tb->R[h];
  			bi.bi_parent = tb->FR[h];
  			bi.bi_position = get_right_neighbor_position(tb, h);
  			internal_insert_childs(&bi,
  					       /*tb->R[h],tb->S[h-1]->b_next */
  					       child_pos - n - insert_num +
  					       tb->rnum[h] - 1,
  					       insert_num, insert_key,
  					       insert_ptr);
  			insert_num = 0;
  		} else {
  			struct disk_child *dc;
  
  			/* one of the items falls into CFR[h] */
  			internal_shift1_right(tb, h, n - child_pos + 1);
  			/* calculate number of new items that fall into R[h] */
  			k = tb->rnum[h] - n + child_pos - 1;
  			bi.tb = tb;
  			bi.bi_bh = tb->R[h];
  			bi.bi_parent = tb->FR[h];
  			bi.bi_position = get_right_neighbor_position(tb, h);
  			internal_insert_childs(&bi,
  					       /*tb->R[h], tb->R[h]->b_child, */
  					       0, k, insert_key + 1,
  					       insert_ptr + 1);
  
  			replace_rkey(tb, h, insert_key + insert_num - k - 1);
098297b27   Jeff Mahoney   reiserfs: cleanup...
948
949
950
951
  			/*
  			 * replace the first node-ptr in R[h] by
  			 * node-ptr insert_ptr[insert_num-k-1]
  			 */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
  			dc = B_N_CHILD(tb->R[h], 0);
  			put_dc_size(dc,
  				    MAX_CHILD_SIZE(insert_ptr
  						   [insert_num - k - 1]) -
  				    B_FREE_SPACE(insert_ptr
  						 [insert_num - k - 1]));
  			put_dc_block_number(dc,
  					    insert_ptr[insert_num - k -
  						       1]->b_blocknr);
  
  			do_balance_mark_internal_dirty(tb, tb->R[h], 0);
  
  			insert_num -= (k + 1);
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
967

098297b27   Jeff Mahoney   reiserfs: cleanup...
968
  	/** Fill new node that appears instead of S[h] **/
bd4c625c0   Linus Torvalds   reiserfs: run scr...
969
970
  	RFALSE(tb->blknum[h] > 2, "blknum can not be > 2 for internal level");
  	RFALSE(tb->blknum[h] < 0, "blknum can not be < 0");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
971

bd4c625c0   Linus Torvalds   reiserfs: run scr...
972
973
  	if (!tb->blknum[h]) {	/* node S[h] is empty now */
  		RFALSE(!tbSh, "S[h] is equal NULL");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
974

bd4c625c0   Linus Torvalds   reiserfs: run scr...
975
976
977
978
  		/* do what is needed for buffer thrown from tree */
  		reiserfs_invalidate_buffer(tb, tbSh);
  		return order;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
979

bd4c625c0   Linus Torvalds   reiserfs: run scr...
980
981
982
983
984
  	if (!tbSh) {
  		/* create new root */
  		struct disk_child *dc;
  		struct buffer_head *tbSh_1 = PATH_H_PBUFFER(tb->tb_path, h - 1);
  		struct block_head *blkh;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
985

bd4c625c0   Linus Torvalds   reiserfs: run scr...
986
  		if (tb->blknum[h] != 1)
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
987
988
  			reiserfs_panic(NULL, "ibalance-3", "One new node "
  				       "required for creating the new root");
bd4c625c0   Linus Torvalds   reiserfs: run scr...
989
990
991
992
  		/* S[h] = empty buffer from the list FEB. */
  		tbSh = get_FEB(tb);
  		blkh = B_BLK_HEAD(tbSh);
  		set_blkh_level(blkh, h + 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
993

bd4c625c0   Linus Torvalds   reiserfs: run scr...
994
995
996
997
998
999
1000
1001
1002
  		/* Put the unique node-pointer to S[h] that points to S[h-1]. */
  
  		dc = B_N_CHILD(tbSh, 0);
  		put_dc_block_number(dc, tbSh_1->b_blocknr);
  		put_dc_size(dc,
  			    (MAX_CHILD_SIZE(tbSh_1) - B_FREE_SPACE(tbSh_1)));
  
  		tb->insert_size[h] -= DC_SIZE;
  		set_blkh_free_space(blkh, blkh_free_space(blkh) - DC_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1003

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1004
  		do_balance_mark_internal_dirty(tb, tbSh, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1005

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
  		/*&&&&&&&&&&&&&&&&&&&&&&&& */
  		check_internal(tbSh);
  		/*&&&&&&&&&&&&&&&&&&&&&&&& */
  
  		/* put new root into path structure */
  		PATH_OFFSET_PBUFFER(tb->tb_path, ILLEGAL_PATH_ELEMENT_OFFSET) =
  		    tbSh;
  
  		/* Change root in structure super block. */
  		PUT_SB_ROOT_BLOCK(tb->tb_sb, tbSh->b_blocknr);
  		PUT_SB_TREE_HEIGHT(tb->tb_sb, SB_TREE_HEIGHT(tb->tb_sb) + 1);
  		do_balance_mark_sb_dirty(tb, REISERFS_SB(tb->tb_sb)->s_sbh, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1018
  	}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
  
  	if (tb->blknum[h] == 2) {
  		int snum;
  		struct buffer_info dest_bi, src_bi;
  
  		/* S_new = free buffer from list FEB */
  		S_new = get_FEB(tb);
  
  		set_blkh_level(B_BLK_HEAD(S_new), h + 1);
  
  		dest_bi.tb = tb;
  		dest_bi.bi_bh = S_new;
  		dest_bi.bi_parent = NULL;
  		dest_bi.bi_position = 0;
  		src_bi.tb = tb;
  		src_bi.bi_bh = tbSh;
  		src_bi.bi_parent = PATH_H_PPARENT(tb->tb_path, h);
  		src_bi.bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
  
  		n = B_NR_ITEMS(tbSh);	/* number of items in S[h] */
  		snum = (insert_num + n + 1) / 2;
  		if (n - snum >= child_pos) {
  			/* new items don't fall into S_new */
  			/*  store the delimiting key for the next level */
  			/* new_insert_key = (n - snum)'th key in S[h] */
4cf5f7add   Jeff Mahoney   reiserfs: cleanup...
1044
  			memcpy(&new_insert_key, internal_key(tbSh, n - snum),
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1045
1046
1047
1048
  			       KEY_SIZE);
  			/* last parameter is del_par */
  			internal_move_pointers_items(&dest_bi, &src_bi,
  						     LAST_TO_FIRST, snum, 0);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1049
1050
1051
  		} else if (n + insert_num - snum < child_pos) {
  			/* all new items fall into S_new */
  			/*  store the delimiting key for the next level */
098297b27   Jeff Mahoney   reiserfs: cleanup...
1052
1053
1054
1055
  			/*
  			 * new_insert_key = (n + insert_item - snum)'th
  			 * key in S[h]
  			 */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1056
  			memcpy(&new_insert_key,
4cf5f7add   Jeff Mahoney   reiserfs: cleanup...
1057
  			       internal_key(tbSh, n + insert_num - snum),
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1058
1059
1060
1061
1062
  			       KEY_SIZE);
  			/* last parameter is del_par */
  			internal_move_pointers_items(&dest_bi, &src_bi,
  						     LAST_TO_FIRST,
  						     snum - insert_num, 0);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1063

098297b27   Jeff Mahoney   reiserfs: cleanup...
1064
1065
1066
1067
  			/*
  			 * insert insert_num keys and node-pointers
  			 * into S_new
  			 */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
  			internal_insert_childs(&dest_bi,
  					       /*S_new,tb->S[h-1]->b_next, */
  					       child_pos - n - insert_num +
  					       snum - 1,
  					       insert_num, insert_key,
  					       insert_ptr);
  
  			insert_num = 0;
  		} else {
  			struct disk_child *dc;
  
  			/* some items fall into S_new, but some don't fall */
  			/* last parameter is del_par */
  			internal_move_pointers_items(&dest_bi, &src_bi,
  						     LAST_TO_FIRST,
  						     n - child_pos + 1, 1);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1084
1085
1086
1087
1088
1089
1090
1091
1092
  			/* calculate number of new items that fall into S_new */
  			k = snum - n + child_pos - 1;
  
  			internal_insert_childs(&dest_bi, /*S_new, */ 0, k,
  					       insert_key + 1, insert_ptr + 1);
  
  			/* new_insert_key = insert_key[insert_num - k - 1] */
  			memcpy(&new_insert_key, insert_key + insert_num - k - 1,
  			       KEY_SIZE);
098297b27   Jeff Mahoney   reiserfs: cleanup...
1093
1094
1095
1096
  			/*
  			 * replace first node-ptr in S_new by node-ptr
  			 * to insert_ptr[insert_num-k-1]
  			 */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
  
  			dc = B_N_CHILD(S_new, 0);
  			put_dc_size(dc,
  				    (MAX_CHILD_SIZE
  				     (insert_ptr[insert_num - k - 1]) -
  				     B_FREE_SPACE(insert_ptr
  						  [insert_num - k - 1])));
  			put_dc_block_number(dc,
  					    insert_ptr[insert_num - k -
  						       1]->b_blocknr);
  
  			do_balance_mark_internal_dirty(tb, S_new, 0);
  
  			insert_num -= (k + 1);
  		}
  		/* new_insert_ptr = node_pointer to S_new */
  		new_insert_ptr = S_new;
  
  		RFALSE(!buffer_journaled(S_new) || buffer_journal_dirty(S_new)
  		       || buffer_dirty(S_new), "cm-00001: bad S_new (%b)",
  		       S_new);
098297b27   Jeff Mahoney   reiserfs: cleanup...
1118
  		/* S_new is released in unfix_nodes */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1119
  	}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1120
  	n = B_NR_ITEMS(tbSh);	/*number of items in S[h] */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1121

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
  	if (0 <= child_pos && child_pos <= n && insert_num > 0) {
  		bi.tb = tb;
  		bi.bi_bh = tbSh;
  		bi.bi_parent = PATH_H_PPARENT(tb->tb_path, h);
  		bi.bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
  		internal_insert_childs(&bi,	/*tbSh, */
  				       /*          ( tb->S[h-1]->b_parent == tb->S[h] ) ? tb->S[h-1]->b_next :  tb->S[h]->b_child->b_next, */
  				       child_pos, insert_num, insert_key,
  				       insert_ptr);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1132
  	insert_ptr[0] = new_insert_ptr;
0a11b9aae   Jeff Mahoney   reiserfs: fix "ne...
1133
1134
  	if (new_insert_ptr)
  		memcpy(new_insert_key_addr, &new_insert_key, KEY_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1135
1136
  
  	return order;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1137
  }