Blame view

fs/reiserfs/do_balan.c 57 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
  /*
   * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
   */
  
  /* Now we have all buffers that must be used in balancing of the tree 	*/
  /* Further calculations can not cause schedule(), and thus the buffer 	*/
  /* tree will be stable until the balancing will be finished 		*/
  /* balance the tree according to the analysis made before,		*/
  /* and using buffers obtained after all above.				*/
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
13
14
15
  /**
   ** balance_leaf_when_delete
   ** balance_leaf
   ** do_balance
   **
   **/
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16
17
18
19
  #include <asm/uaccess.h>
  #include <linux/time.h>
  #include <linux/reiserfs_fs.h>
  #include <linux/buffer_head.h>
79a81aef7   Ahmed S. Darwish   [PATCH] reiserfs:...
20
  #include <linux/kernel.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21

fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  static inline void buffer_info_init_left(struct tree_balance *tb,
                                           struct buffer_info *bi)
  {
  	bi->tb          = tb;
  	bi->bi_bh       = tb->L[0];
  	bi->bi_parent   = tb->FL[0];
  	bi->bi_position = get_left_neighbor_position(tb, 0);
  }
  
  static inline void buffer_info_init_right(struct tree_balance *tb,
                                            struct buffer_info *bi)
  {
  	bi->tb          = tb;
  	bi->bi_bh       = tb->R[0];
  	bi->bi_parent   = tb->FR[0];
  	bi->bi_position = get_right_neighbor_position(tb, 0);
  }
  
  static inline void buffer_info_init_tbS0(struct tree_balance *tb,
                                           struct buffer_info *bi)
  {
  	bi->tb          = tb;
  	bi->bi_bh        = PATH_PLAST_BUFFER(tb->tb_path);
  	bi->bi_parent   = PATH_H_PPARENT(tb->tb_path, 0);
  	bi->bi_position = PATH_H_POSITION(tb->tb_path, 1);
  }
  
  static inline void buffer_info_init_bh(struct tree_balance *tb,
                                         struct buffer_info *bi,
                                         struct buffer_head *bh)
  {
  	bi->tb          = tb;
  	bi->bi_bh       = bh;
  	bi->bi_parent   = NULL;
  	bi->bi_position = 0;
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
58
59
  inline void do_balance_mark_leaf_dirty(struct tree_balance *tb,
  				       struct buffer_head *bh, int flag)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
61
62
  	journal_mark_dirty(tb->transaction_handle,
  			   tb->transaction_handle->t_super, bh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
64
65
66
  }
  
  #define do_balance_mark_internal_dirty do_balance_mark_leaf_dirty
  #define do_balance_mark_sb_dirty do_balance_mark_leaf_dirty
0222e6571   Jeff Mahoney   reiserfs: strip t...
67
  /* summary:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
68
69
70
71
72
73
   if deleting something ( tb->insert_size[0] < 0 )
     return(balance_leaf_when_delete()); (flag d handled here)
   else
     if lnum is larger than 0 we put items into the left node
     if rnum is larger than 0 we put items into the right node
     if snum1 is larger than 0 we put items into the new node s1
0222e6571   Jeff Mahoney   reiserfs: strip t...
74
     if snum2 is larger than 0 we put items into the new node s2
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
76
77
78
79
80
  Note that all *num* count new items being created.
  
  It would be easier to read balance_leaf() if each of these summary
  lines was a separate procedure rather than being inlined.  I think
  that there are many passages here and in balance_leaf_when_delete() in
  which two calls to one procedure can replace two passages, and it
0222e6571   Jeff Mahoney   reiserfs: strip t...
81
  might save cache space and improve software maintenance costs to do so.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
84
85
86
87
88
  
  Vladimir made the perceptive comment that we should offload most of
  the decision making in this function into fix_nodes/check_balance, and
  then create some sort of structure in tb that says what actions should
  be performed by do_balance.
  
  -Hans */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
90
91
92
93
94
95
  /* Balance leaf node in case of delete or cut: insert_size[0] < 0
   *
   * lnum, rnum can have values >= -1
   *	-1 means that the neighbor must be joined with S
   *	 0 means that nothing should be done with the neighbor
   *	>0 means to shift entirely or partly the specified number of items to the neighbor
   */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
96
  static int balance_leaf_when_delete(struct tree_balance *tb, int flag)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
98
99
100
101
102
103
  	struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
  	int item_pos = PATH_LAST_POSITION(tb->tb_path);
  	int pos_in_item = tb->tb_path->pos_in_item;
  	struct buffer_info bi;
  	int n;
  	struct item_head *ih;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104

bd4c625c0   Linus Torvalds   reiserfs: run scr...
105
106
107
108
109
110
  	RFALSE(tb->FR[0] && B_LEVEL(tb->FR[0]) != DISK_LEAF_NODE_LEVEL + 1,
  	       "vs- 12000: level: wrong FR %z", tb->FR[0]);
  	RFALSE(tb->blknum[0] > 1,
  	       "PAP-12005: tb->blknum == %d, can not be > 1", tb->blknum[0]);
  	RFALSE(!tb->blknum[0] && !PATH_H_PPARENT(tb->tb_path, 0),
  	       "PAP-12010: tree can not be empty");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111

bd4c625c0   Linus Torvalds   reiserfs: run scr...
112
  	ih = B_N_PITEM_HEAD(tbS0, item_pos);
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
113
  	buffer_info_init_tbS0(tb, &bi);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114

bd4c625c0   Linus Torvalds   reiserfs: run scr...
115
  	/* Delete or truncate the item */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116

bd4c625c0   Linus Torvalds   reiserfs: run scr...
117
118
119
120
121
122
  	switch (flag) {
  	case M_DELETE:		/* delete item in S[0] */
  
  		RFALSE(ih_item_len(ih) + IH_SIZE != -tb->insert_size[0],
  		       "vs-12013: mode Delete, insert size %d, ih to be deleted %h",
  		       -tb->insert_size[0], ih);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
123
124
125
126
127
128
129
130
131
132
133
134
135
  		leaf_delete_items(&bi, 0, item_pos, 1, -1);
  
  		if (!item_pos && tb->CFL[0]) {
  			if (B_NR_ITEMS(tbS0)) {
  				replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0,
  					    0);
  			} else {
  				if (!PATH_H_POSITION(tb->tb_path, 1))
  					replace_key(tb, tb->CFL[0], tb->lkey[0],
  						    PATH_H_PPARENT(tb->tb_path,
  								   0), 0);
  			}
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136

bd4c625c0   Linus Torvalds   reiserfs: run scr...
137
138
139
  		RFALSE(!item_pos && !tb->CFL[0],
  		       "PAP-12020: tb->CFL[0]==%p, tb->L[0]==%p", tb->CFL[0],
  		       tb->L[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140

bd4c625c0   Linus Torvalds   reiserfs: run scr...
141
142
143
  		break;
  
  	case M_CUT:{		/* cut item in S[0] */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  			if (is_direntry_le_ih(ih)) {
  
  				/* UFS unlink semantics are such that you can only delete one directory entry at a time. */
  				/* when we cut a directory tb->insert_size[0] means number of entries to be cut (always 1) */
  				tb->insert_size[0] = -1;
  				leaf_cut_from_buffer(&bi, item_pos, pos_in_item,
  						     -tb->insert_size[0]);
  
  				RFALSE(!item_pos && !pos_in_item && !tb->CFL[0],
  				       "PAP-12030: can not change delimiting key. CFL[0]=%p",
  				       tb->CFL[0]);
  
  				if (!item_pos && !pos_in_item && tb->CFL[0]) {
  					replace_key(tb, tb->CFL[0], tb->lkey[0],
  						    tbS0, 0);
  				}
  			} else {
  				leaf_cut_from_buffer(&bi, item_pos, pos_in_item,
  						     -tb->insert_size[0]);
  
  				RFALSE(!ih_item_len(ih),
  				       "PAP-12035: cut must leave non-zero dynamic length of item");
  			}
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169

bd4c625c0   Linus Torvalds   reiserfs: run scr...
170
171
  	default:
  		print_cur_tb("12040");
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
172
173
  		reiserfs_panic(tb->tb_sb, "PAP-12040",
  			       "unexpected mode: %s(%d)",
bd4c625c0   Linus Torvalds   reiserfs: run scr...
174
175
176
177
178
  			       (flag ==
  				M_PASTE) ? "PASTE" : ((flag ==
  						       M_INSERT) ? "INSERT" :
  						      "UNKNOWN"), flag);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179

bd4c625c0   Linus Torvalds   reiserfs: run scr...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
  	/* the rule is that no shifting occurs unless by shifting a node can be freed */
  	n = B_NR_ITEMS(tbS0);
  	if (tb->lnum[0]) {	/* L[0] takes part in balancing */
  		if (tb->lnum[0] == -1) {	/* L[0] must be joined with S[0] */
  			if (tb->rnum[0] == -1) {	/* R[0] must be also joined with S[0] */
  				if (tb->FR[0] == PATH_H_PPARENT(tb->tb_path, 0)) {
  					/* all contents of all the 3 buffers will be in L[0] */
  					if (PATH_H_POSITION(tb->tb_path, 1) == 0
  					    && 1 < B_NR_ITEMS(tb->FR[0]))
  						replace_key(tb, tb->CFL[0],
  							    tb->lkey[0],
  							    tb->FR[0], 1);
  
  					leaf_move_items(LEAF_FROM_S_TO_L, tb, n,
  							-1, NULL);
  					leaf_move_items(LEAF_FROM_R_TO_L, tb,
  							B_NR_ITEMS(tb->R[0]),
  							-1, NULL);
  
  					reiserfs_invalidate_buffer(tb, tbS0);
  					reiserfs_invalidate_buffer(tb,
  								   tb->R[0]);
  
  					return 0;
  				}
  				/* all contents of all the 3 buffers will be in R[0] */
  				leaf_move_items(LEAF_FROM_S_TO_R, tb, n, -1,
  						NULL);
  				leaf_move_items(LEAF_FROM_L_TO_R, tb,
  						B_NR_ITEMS(tb->L[0]), -1, NULL);
  
  				/* right_delimiting_key is correct in R[0] */
  				replace_key(tb, tb->CFR[0], tb->rkey[0],
  					    tb->R[0], 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214

bd4c625c0   Linus Torvalds   reiserfs: run scr...
215
216
  				reiserfs_invalidate_buffer(tb, tbS0);
  				reiserfs_invalidate_buffer(tb, tb->L[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
217

bd4c625c0   Linus Torvalds   reiserfs: run scr...
218
219
  				return -1;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220

bd4c625c0   Linus Torvalds   reiserfs: run scr...
221
222
223
224
  			RFALSE(tb->rnum[0] != 0,
  			       "PAP-12045: rnum must be 0 (%d)", tb->rnum[0]);
  			/* all contents of L[0] and S[0] will be in L[0] */
  			leaf_shift_left(tb, n, -1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225

bd4c625c0   Linus Torvalds   reiserfs: run scr...
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  			reiserfs_invalidate_buffer(tb, tbS0);
  
  			return 0;
  		}
  		/* a part of contents of S[0] will be in L[0] and the rest part of S[0] will be in R[0] */
  
  		RFALSE((tb->lnum[0] + tb->rnum[0] < n) ||
  		       (tb->lnum[0] + tb->rnum[0] > n + 1),
  		       "PAP-12050: rnum(%d) and lnum(%d) and item number(%d) in S[0] are not consistent",
  		       tb->rnum[0], tb->lnum[0], n);
  		RFALSE((tb->lnum[0] + tb->rnum[0] == n) &&
  		       (tb->lbytes != -1 || tb->rbytes != -1),
  		       "PAP-12055: bad rbytes (%d)/lbytes (%d) parameters when items are not split",
  		       tb->rbytes, tb->lbytes);
  		RFALSE((tb->lnum[0] + tb->rnum[0] == n + 1) &&
  		       (tb->lbytes < 1 || tb->rbytes != -1),
  		       "PAP-12060: bad rbytes (%d)/lbytes (%d) parameters when items are split",
  		       tb->rbytes, tb->lbytes);
  
  		leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
  		leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
  
  		reiserfs_invalidate_buffer(tb, tbS0);
  
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
252

bd4c625c0   Linus Torvalds   reiserfs: run scr...
253
254
255
256
257
258
  	if (tb->rnum[0] == -1) {
  		/* all contents of R[0] and S[0] will be in R[0] */
  		leaf_shift_right(tb, n, -1);
  		reiserfs_invalidate_buffer(tb, tbS0);
  		return 0;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259

bd4c625c0   Linus Torvalds   reiserfs: run scr...
260
261
  	RFALSE(tb->rnum[0],
  	       "PAP-12065: bad rnum parameter must be 0 (%d)", tb->rnum[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
264
265
266
267
268
269
270
271
272
  static int balance_leaf(struct tree_balance *tb, struct item_head *ih,	/* item header of inserted item (this is on little endian) */
  			const char *body,	/* body  of inserted item or bytes to paste */
  			int flag,	/* i - insert, d - delete, c - cut, p - paste
  					   (see comment to do_balance) */
  			struct item_head *insert_key,	/* in our processing of one level we sometimes determine what
  							   must be inserted into the next higher level.  This insertion
  							   consists of a key or two keys and their corresponding
  							   pointers */
  			struct buffer_head **insert_ptr	/* inserted node-ptrs for the next level */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
274
      )
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
275
  	struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
0222e6571   Jeff Mahoney   reiserfs: strip t...
276
  	int item_pos = PATH_LAST_POSITION(tb->tb_path);	/*  index into the array of item headers in S[0]
bd4c625c0   Linus Torvalds   reiserfs: run scr...
277
278
279
280
281
282
  							   of the affected item */
  	struct buffer_info bi;
  	struct buffer_head *S_new[2];	/* new nodes allocated to hold what could not fit into S */
  	int snum[2];		/* number of items that will be placed
  				   into S_new (includes partially shifted
  				   items) */
0222e6571   Jeff Mahoney   reiserfs: strip t...
283
284
  	int sbytes[2];		/* if an item is partially shifted into S_new then
  				   if it is a directory item
bd4c625c0   Linus Torvalds   reiserfs: run scr...
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
  				   it is the number of entries from the item that are shifted into S_new
  				   else
  				   it is the number of bytes from the item that are shifted into S_new
  				 */
  	int n, i;
  	int ret_val;
  	int pos_in_item;
  	int zeros_num;
  
  	PROC_INFO_INC(tb->tb_sb, balance_at[0]);
  
  	/* Make balance in case insert_size[0] < 0 */
  	if (tb->insert_size[0] < 0)
  		return balance_leaf_when_delete(tb, flag);
  
  	zeros_num = 0;
9dce07f1a   Al Viro   NULL noise: fs/*,...
301
  	if (flag == M_INSERT && !body)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
  		zeros_num = ih_item_len(ih);
  
  	pos_in_item = tb->tb_path->pos_in_item;
  	/* for indirect item pos_in_item is measured in unformatted node
  	   pointers. Recalculate to bytes */
  	if (flag != M_INSERT
  	    && is_indirect_le_ih(B_N_PITEM_HEAD(tbS0, item_pos)))
  		pos_in_item *= UNFM_P_SIZE;
  
  	if (tb->lnum[0] > 0) {
  		/* Shift lnum[0] items from S[0] to the left neighbor L[0] */
  		if (item_pos < tb->lnum[0]) {
  			/* new item or it part falls to L[0], shift it too */
  			n = B_NR_ITEMS(tb->L[0]);
  
  			switch (flag) {
  			case M_INSERT:	/* insert item into L[0] */
  
  				if (item_pos == tb->lnum[0] - 1
  				    && tb->lbytes != -1) {
  					/* part of new item falls into L[0] */
  					int new_item_len;
  					int version;
  
  					ret_val =
  					    leaf_shift_left(tb, tb->lnum[0] - 1,
  							    -1);
  
  					/* Calculate item length to insert to S[0] */
  					new_item_len =
  					    ih_item_len(ih) - tb->lbytes;
  					/* Calculate and check item length to insert to L[0] */
  					put_ih_item_len(ih,
  							ih_item_len(ih) -
  							new_item_len);
  
  					RFALSE(ih_item_len(ih) <= 0,
  					       "PAP-12080: there is nothing to insert into L[0]: ih_item_len=%d",
  					       ih_item_len(ih));
  
  					/* Insert new item into L[0] */
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
343
  					buffer_info_init_left(tb, &bi);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
344
345
346
347
348
349
350
351
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
378
379
380
381
382
  					leaf_insert_into_buf(&bi,
  							     n + item_pos -
  							     ret_val, ih, body,
  							     zeros_num >
  							     ih_item_len(ih) ?
  							     ih_item_len(ih) :
  							     zeros_num);
  
  					version = ih_version(ih);
  
  					/* Calculate key component, item length and body to insert into S[0] */
  					set_le_ih_k_offset(ih,
  							   le_ih_k_offset(ih) +
  							   (tb->
  							    lbytes <<
  							    (is_indirect_le_ih
  							     (ih) ? tb->tb_sb->
  							     s_blocksize_bits -
  							     UNFM_P_SHIFT :
  							     0)));
  
  					put_ih_item_len(ih, new_item_len);
  					if (tb->lbytes > zeros_num) {
  						body +=
  						    (tb->lbytes - zeros_num);
  						zeros_num = 0;
  					} else
  						zeros_num -= tb->lbytes;
  
  					RFALSE(ih_item_len(ih) <= 0,
  					       "PAP-12085: there is nothing to insert into S[0]: ih_item_len=%d",
  					       ih_item_len(ih));
  				} else {
  					/* new item in whole falls into L[0] */
  					/* Shift lnum[0]-1 items to L[0] */
  					ret_val =
  					    leaf_shift_left(tb, tb->lnum[0] - 1,
  							    tb->lbytes);
  					/* Insert new item into L[0] */
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
383
  					buffer_info_init_left(tb, &bi);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
384
385
386
387
388
389
  					leaf_insert_into_buf(&bi,
  							     n + item_pos -
  							     ret_val, ih, body,
  							     zeros_num);
  					tb->insert_size[0] = 0;
  					zeros_num = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
390
  				}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
  				break;
  
  			case M_PASTE:	/* append item in L[0] */
  
  				if (item_pos == tb->lnum[0] - 1
  				    && tb->lbytes != -1) {
  					/* we must shift the part of the appended item */
  					if (is_direntry_le_ih
  					    (B_N_PITEM_HEAD(tbS0, item_pos))) {
  
  						RFALSE(zeros_num,
  						       "PAP-12090: invalid parameter in case of a directory");
  						/* directory item */
  						if (tb->lbytes > pos_in_item) {
  							/* new directory entry falls into L[0] */
  							struct item_head
  							    *pasted;
  							int l_pos_in_item =
  							    pos_in_item;
  
  							/* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 entries from given directory item */
  							ret_val =
  							    leaf_shift_left(tb,
  									    tb->
  									    lnum
  									    [0],
  									    tb->
  									    lbytes
  									    -
  									    1);
  							if (ret_val
  							    && !item_pos) {
  								pasted =
  								    B_N_PITEM_HEAD
  								    (tb->L[0],
  								     B_NR_ITEMS
  								     (tb->
  								      L[0]) -
  								     1);
  								l_pos_in_item +=
  								    I_ENTRY_COUNT
  								    (pasted) -
  								    (tb->
  								     lbytes -
  								     1);
  							}
  
  							/* Append given directory entry to directory item */
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
439
  							buffer_info_init_left(tb, &bi);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
440
441
442
443
444
445
446
447
448
449
450
451
452
  							leaf_paste_in_buffer
  							    (&bi,
  							     n + item_pos -
  							     ret_val,
  							     l_pos_in_item,
  							     tb->insert_size[0],
  							     body, zeros_num);
  
  							/* previous string prepared space for pasting new entry, following string pastes this entry */
  
  							/* when we have merge directory item, pos_in_item has been changed too */
  
  							/* paste new directory entry. 1 is entry number */
eba003055   Jeff Mahoney   reiserfs: use buf...
453
  							leaf_paste_entries(&bi,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
  									   n +
  									   item_pos
  									   -
  									   ret_val,
  									   l_pos_in_item,
  									   1,
  									   (struct
  									    reiserfs_de_head
  									    *)
  									   body,
  									   body
  									   +
  									   DEH_SIZE,
  									   tb->
  									   insert_size
  									   [0]
  							    );
  							tb->insert_size[0] = 0;
  						} else {
  							/* new directory item doesn't fall into L[0] */
  							/* Shift lnum[0]-1 items in whole. Shift lbytes directory entries from directory item number lnum[0] */
  							leaf_shift_left(tb,
  									tb->
  									lnum[0],
  									tb->
  									lbytes);
  						}
  						/* Calculate new position to append in item body */
  						pos_in_item -= tb->lbytes;
  					} else {
  						/* regular object */
  						RFALSE(tb->lbytes <= 0,
  						       "PAP-12095: there is nothing to shift to L[0]. lbytes=%d",
  						       tb->lbytes);
  						RFALSE(pos_in_item !=
  						       ih_item_len
  						       (B_N_PITEM_HEAD
  							(tbS0, item_pos)),
  						       "PAP-12100: incorrect position to paste: item_len=%d, pos_in_item=%d",
  						       ih_item_len
  						       (B_N_PITEM_HEAD
  							(tbS0, item_pos)),
  						       pos_in_item);
  
  						if (tb->lbytes >= pos_in_item) {
  							/* appended item will be in L[0] in whole */
  							int l_n;
  
  							/* this bytes number must be appended to the last item of L[h] */
  							l_n =
  							    tb->lbytes -
  							    pos_in_item;
  
  							/* Calculate new insert_size[0] */
  							tb->insert_size[0] -=
  							    l_n;
  
  							RFALSE(tb->
  							       insert_size[0] <=
  							       0,
  							       "PAP-12105: there is nothing to paste into L[0]. insert_size=%d",
  							       tb->
  							       insert_size[0]);
  							ret_val =
  							    leaf_shift_left(tb,
  									    tb->
  									    lnum
  									    [0],
  									    ih_item_len
  									    (B_N_PITEM_HEAD
  									     (tbS0,
  									      item_pos)));
  							/* Append to body of item in L[0] */
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
527
  							buffer_info_init_left(tb, &bi);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
  							leaf_paste_in_buffer
  							    (&bi,
  							     n + item_pos -
  							     ret_val,
  							     ih_item_len
  							     (B_N_PITEM_HEAD
  							      (tb->L[0],
  							       n + item_pos -
  							       ret_val)), l_n,
  							     body,
  							     zeros_num >
  							     l_n ? l_n :
  							     zeros_num);
  							/* 0-th item in S0 can be only of DIRECT type when l_n != 0 */
  							{
  								int version;
  								int temp_l =
  								    l_n;
  
  								RFALSE
  								    (ih_item_len
  								     (B_N_PITEM_HEAD
  								      (tbS0,
  								       0)),
  								     "PAP-12106: item length must be 0");
  								RFALSE
  								    (comp_short_le_keys
  								     (B_N_PKEY
  								      (tbS0, 0),
  								      B_N_PKEY
  								      (tb->L[0],
  								       n +
  								       item_pos
  								       -
  								       ret_val)),
  								     "PAP-12107: items must be of the same file");
  								if (is_indirect_le_ih(B_N_PITEM_HEAD(tb->L[0], n + item_pos - ret_val))) {
  									temp_l =
  									    l_n
  									    <<
  									    (tb->
  									     tb_sb->
  									     s_blocksize_bits
  									     -
  									     UNFM_P_SHIFT);
  								}
  								/* update key of first item in S0 */
  								version =
  								    ih_version
  								    (B_N_PITEM_HEAD
  								     (tbS0, 0));
  								set_le_key_k_offset
  								    (version,
  								     B_N_PKEY
  								     (tbS0, 0),
  								     le_key_k_offset
  								     (version,
  								      B_N_PKEY
  								      (tbS0,
  								       0)) +
  								     temp_l);
  								/* update left delimiting key */
  								set_le_key_k_offset
  								    (version,
  								     B_N_PDELIM_KEY
  								     (tb->
  								      CFL[0],
  								      tb->
  								      lkey[0]),
  								     le_key_k_offset
  								     (version,
  								      B_N_PDELIM_KEY
  								      (tb->
  								       CFL[0],
  								       tb->
  								       lkey[0]))
  								     + temp_l);
  							}
  
  							/* Calculate new body, position in item and insert_size[0] */
  							if (l_n > zeros_num) {
  								body +=
  								    (l_n -
  								     zeros_num);
  								zeros_num = 0;
  							} else
  								zeros_num -=
  								    l_n;
  							pos_in_item = 0;
  
  							RFALSE
  							    (comp_short_le_keys
  							     (B_N_PKEY(tbS0, 0),
  							      B_N_PKEY(tb->L[0],
  								       B_NR_ITEMS
  								       (tb->
  									L[0]) -
  								       1))
  							     ||
  							     !op_is_left_mergeable
  							     (B_N_PKEY(tbS0, 0),
  							      tbS0->b_size)
  							     ||
  							     !op_is_left_mergeable
  							     (B_N_PDELIM_KEY
  							      (tb->CFL[0],
  							       tb->lkey[0]),
  							      tbS0->b_size),
  							     "PAP-12120: item must be merge-able with left neighboring item");
  						} else {	/* only part of the appended item will be in L[0] */
  
  							/* Calculate position in item for append in S[0] */
  							pos_in_item -=
  							    tb->lbytes;
  
  							RFALSE(pos_in_item <= 0,
  							       "PAP-12125: no place for paste. pos_in_item=%d",
  							       pos_in_item);
  
  							/* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
  							leaf_shift_left(tb,
  									tb->
  									lnum[0],
  									tb->
  									lbytes);
  						}
  					}
  				} else {	/* appended item will be in L[0] in whole */
  
  					struct item_head *pasted;
  
  					if (!item_pos && op_is_left_mergeable(B_N_PKEY(tbS0, 0), tbS0->b_size)) {	/* if we paste into first item of S[0] and it is left mergable */
  						/* then increment pos_in_item by the size of the last item in L[0] */
  						pasted =
  						    B_N_PITEM_HEAD(tb->L[0],
  								   n - 1);
  						if (is_direntry_le_ih(pasted))
  							pos_in_item +=
  							    ih_entry_count
  							    (pasted);
  						else
  							pos_in_item +=
  							    ih_item_len(pasted);
  					}
  
  					/* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
  					ret_val =
  					    leaf_shift_left(tb, tb->lnum[0],
  							    tb->lbytes);
  					/* Append to body of item in L[0] */
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
678
  					buffer_info_init_left(tb, &bi);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
679
680
681
682
683
684
685
686
687
688
689
690
691
  					leaf_paste_in_buffer(&bi,
  							     n + item_pos -
  							     ret_val,
  							     pos_in_item,
  							     tb->insert_size[0],
  							     body, zeros_num);
  
  					/* if appended item is directory, paste entry */
  					pasted =
  					    B_N_PITEM_HEAD(tb->L[0],
  							   n + item_pos -
  							   ret_val);
  					if (is_direntry_le_ih(pasted))
eba003055   Jeff Mahoney   reiserfs: use buf...
692
  						leaf_paste_entries(&bi,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
  								   n +
  								   item_pos -
  								   ret_val,
  								   pos_in_item,
  								   1,
  								   (struct
  								    reiserfs_de_head
  								    *)body,
  								   body +
  								   DEH_SIZE,
  								   tb->
  								   insert_size
  								   [0]
  						    );
  					/* if appended item is indirect item, put unformatted node into un list */
  					if (is_indirect_le_ih(pasted))
  						set_ih_free_space(pasted, 0);
  					tb->insert_size[0] = 0;
  					zeros_num = 0;
  				}
  				break;
  			default:	/* cases d and t */
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
715
716
717
  				reiserfs_panic(tb->tb_sb, "PAP-12130",
  					       "lnum > 0: unexpected mode: "
  					       " %s(%d)",
bd4c625c0   Linus Torvalds   reiserfs: run scr...
718
719
720
721
722
723
724
  					       (flag ==
  						M_DELETE) ? "DELETE" : ((flag ==
  									 M_CUT)
  									? "CUT"
  									:
  									"UNKNOWN"),
  					       flag);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
725
  			}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
726
727
728
  		} else {
  			/* new item doesn't fall into L[0] */
  			leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
729
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
730
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
731

bd4c625c0   Linus Torvalds   reiserfs: run scr...
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
  	/* tb->lnum[0] > 0 */
  	/* Calculate new item position */
  	item_pos -= (tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0));
  
  	if (tb->rnum[0] > 0) {
  		/* shift rnum[0] items from S[0] to the right neighbor R[0] */
  		n = B_NR_ITEMS(tbS0);
  		switch (flag) {
  
  		case M_INSERT:	/* insert item */
  			if (n - tb->rnum[0] < item_pos) {	/* new item or its part falls to R[0] */
  				if (item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) {	/* part of new item falls into R[0] */
  					loff_t old_key_comp, old_len,
  					    r_zeros_number;
  					const char *r_body;
  					int version;
  					loff_t offset;
  
  					leaf_shift_right(tb, tb->rnum[0] - 1,
  							 -1);
  
  					version = ih_version(ih);
  					/* Remember key component and item length */
  					old_key_comp = le_ih_k_offset(ih);
  					old_len = ih_item_len(ih);
  
  					/* Calculate key component and item length to insert into R[0] */
  					offset =
  					    le_ih_k_offset(ih) +
  					    ((old_len -
  					      tb->
  					      rbytes) << (is_indirect_le_ih(ih)
  							  ? tb->tb_sb->
  							  s_blocksize_bits -
  							  UNFM_P_SHIFT : 0));
  					set_le_ih_k_offset(ih, offset);
  					put_ih_item_len(ih, tb->rbytes);
  					/* Insert part of the item into R[0] */
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
770
  					buffer_info_init_right(tb, &bi);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
  					if ((old_len - tb->rbytes) > zeros_num) {
  						r_zeros_number = 0;
  						r_body =
  						    body + (old_len -
  							    tb->rbytes) -
  						    zeros_num;
  					} else {
  						r_body = body;
  						r_zeros_number =
  						    zeros_num - (old_len -
  								 tb->rbytes);
  						zeros_num -= r_zeros_number;
  					}
  
  					leaf_insert_into_buf(&bi, 0, ih, r_body,
  							     r_zeros_number);
  
  					/* Replace right delimiting key by first key in R[0] */
  					replace_key(tb, tb->CFR[0], tb->rkey[0],
  						    tb->R[0], 0);
  
  					/* Calculate key component and item length to insert into S[0] */
  					set_le_ih_k_offset(ih, old_key_comp);
  					put_ih_item_len(ih,
  							old_len - tb->rbytes);
  
  					tb->insert_size[0] -= tb->rbytes;
  
  				} else {	/* whole new item falls into R[0] */
  
  					/* Shift rnum[0]-1 items to R[0] */
  					ret_val =
  					    leaf_shift_right(tb,
  							     tb->rnum[0] - 1,
  							     tb->rbytes);
  					/* Insert new item into R[0] */
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
807
  					buffer_info_init_right(tb, &bi);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
  					leaf_insert_into_buf(&bi,
  							     item_pos - n +
  							     tb->rnum[0] - 1,
  							     ih, body,
  							     zeros_num);
  
  					if (item_pos - n + tb->rnum[0] - 1 == 0) {
  						replace_key(tb, tb->CFR[0],
  							    tb->rkey[0],
  							    tb->R[0], 0);
  
  					}
  					zeros_num = tb->insert_size[0] = 0;
  				}
  			} else {	/* new item or part of it doesn't fall into R[0] */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823

bd4c625c0   Linus Torvalds   reiserfs: run scr...
824
  				leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
825
  			}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
  			break;
  
  		case M_PASTE:	/* append item */
  
  			if (n - tb->rnum[0] <= item_pos) {	/* pasted item or part of it falls to R[0] */
  				if (item_pos == n - tb->rnum[0] && tb->rbytes != -1) {	/* we must shift the part of the appended item */
  					if (is_direntry_le_ih(B_N_PITEM_HEAD(tbS0, item_pos))) {	/* we append to directory item */
  						int entry_count;
  
  						RFALSE(zeros_num,
  						       "PAP-12145: invalid parameter in case of a directory");
  						entry_count =
  						    I_ENTRY_COUNT(B_N_PITEM_HEAD
  								  (tbS0,
  								   item_pos));
  						if (entry_count - tb->rbytes <
  						    pos_in_item)
  							/* new directory entry falls into R[0] */
  						{
  							int paste_entry_position;
  
  							RFALSE(tb->rbytes - 1 >=
  							       entry_count
  							       || !tb->
  							       insert_size[0],
  							       "PAP-12150: no enough of entries to shift to R[0]: rbytes=%d, entry_count=%d",
  							       tb->rbytes,
  							       entry_count);
  							/* Shift rnum[0]-1 items in whole. Shift rbytes-1 directory entries from directory item number rnum[0] */
  							leaf_shift_right(tb,
  									 tb->
  									 rnum
  									 [0],
  									 tb->
  									 rbytes
  									 - 1);
  							/* Paste given directory entry to directory item */
  							paste_entry_position =
  							    pos_in_item -
  							    entry_count +
  							    tb->rbytes - 1;
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
867
  							buffer_info_init_right(tb, &bi);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
868
869
870
871
872
873
  							leaf_paste_in_buffer
  							    (&bi, 0,
  							     paste_entry_position,
  							     tb->insert_size[0],
  							     body, zeros_num);
  							/* paste entry */
eba003055   Jeff Mahoney   reiserfs: use buf...
874
  							leaf_paste_entries(&bi,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
  									   0,
  									   paste_entry_position,
  									   1,
  									   (struct
  									    reiserfs_de_head
  									    *)
  									   body,
  									   body
  									   +
  									   DEH_SIZE,
  									   tb->
  									   insert_size
  									   [0]
  							    );
  
  							if (paste_entry_position
  							    == 0) {
  								/* change delimiting keys */
  								replace_key(tb,
  									    tb->
  									    CFR
  									    [0],
  									    tb->
  									    rkey
  									    [0],
  									    tb->
  									    R
  									    [0],
  									    0);
  							}
  
  							tb->insert_size[0] = 0;
  							pos_in_item++;
  						} else {	/* new directory entry doesn't fall into R[0] */
  
  							leaf_shift_right(tb,
  									 tb->
  									 rnum
  									 [0],
  									 tb->
  									 rbytes);
  						}
  					} else {	/* regular object */
  
  						int n_shift, n_rem,
  						    r_zeros_number;
  						const char *r_body;
  
  						/* Calculate number of bytes which must be shifted from appended item */
  						if ((n_shift =
  						     tb->rbytes -
  						     tb->insert_size[0]) < 0)
  							n_shift = 0;
  
  						RFALSE(pos_in_item !=
  						       ih_item_len
  						       (B_N_PITEM_HEAD
  							(tbS0, item_pos)),
  						       "PAP-12155: invalid position to paste. ih_item_len=%d, pos_in_item=%d",
  						       pos_in_item,
  						       ih_item_len
  						       (B_N_PITEM_HEAD
  							(tbS0, item_pos)));
  
  						leaf_shift_right(tb,
  								 tb->rnum[0],
  								 n_shift);
  						/* Calculate number of bytes which must remain in body after appending to R[0] */
  						if ((n_rem =
  						     tb->insert_size[0] -
  						     tb->rbytes) < 0)
  							n_rem = 0;
  
  						{
  							int version;
  							unsigned long temp_rem =
  							    n_rem;
  
  							version =
  							    ih_version
  							    (B_N_PITEM_HEAD
  							     (tb->R[0], 0));
  							if (is_indirect_le_key
  							    (version,
  							     B_N_PKEY(tb->R[0],
  								      0))) {
  								temp_rem =
  								    n_rem <<
  								    (tb->tb_sb->
  								     s_blocksize_bits
  								     -
  								     UNFM_P_SHIFT);
  							}
  							set_le_key_k_offset
  							    (version,
  							     B_N_PKEY(tb->R[0],
  								      0),
  							     le_key_k_offset
  							     (version,
  							      B_N_PKEY(tb->R[0],
  								       0)) +
  							     temp_rem);
  							set_le_key_k_offset
  							    (version,
  							     B_N_PDELIM_KEY(tb->
  									    CFR
  									    [0],
  									    tb->
  									    rkey
  									    [0]),
  							     le_key_k_offset
  							     (version,
  							      B_N_PDELIM_KEY
  							      (tb->CFR[0],
  							       tb->rkey[0])) +
  							     temp_rem);
  						}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
992
993
  /*		  k_offset (B_N_PKEY(tb->R[0],0)) += n_rem;
  		  k_offset (B_N_PDELIM_KEY(tb->CFR[0],tb->rkey[0])) += n_rem;*/
bd4c625c0   Linus Torvalds   reiserfs: run scr...
994
995
996
997
  						do_balance_mark_internal_dirty
  						    (tb, tb->CFR[0], 0);
  
  						/* Append part of body into R[0] */
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
998
  						buffer_info_init_right(tb, &bi);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
  						if (n_rem > zeros_num) {
  							r_zeros_number = 0;
  							r_body =
  							    body + n_rem -
  							    zeros_num;
  						} else {
  							r_body = body;
  							r_zeros_number =
  							    zeros_num - n_rem;
  							zeros_num -=
  							    r_zeros_number;
  						}
  
  						leaf_paste_in_buffer(&bi, 0,
  								     n_shift,
  								     tb->
  								     insert_size
  								     [0] -
  								     n_rem,
  								     r_body,
  								     r_zeros_number);
  
  						if (is_indirect_le_ih
  						    (B_N_PITEM_HEAD
  						     (tb->R[0], 0))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1024
  #if 0
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1025
1026
  							RFALSE(n_rem,
  							       "PAP-12160: paste more than one unformatted node pointer");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1027
  #endif
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
  							set_ih_free_space
  							    (B_N_PITEM_HEAD
  							     (tb->R[0], 0), 0);
  						}
  						tb->insert_size[0] = n_rem;
  						if (!n_rem)
  							pos_in_item++;
  					}
  				} else {	/* pasted item in whole falls into R[0] */
  
  					struct item_head *pasted;
  
  					ret_val =
  					    leaf_shift_right(tb, tb->rnum[0],
  							     tb->rbytes);
  					/* append item in R[0] */
  					if (pos_in_item >= 0) {
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
1045
  						buffer_info_init_right(tb, &bi);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
  						leaf_paste_in_buffer(&bi,
  								     item_pos -
  								     n +
  								     tb->
  								     rnum[0],
  								     pos_in_item,
  								     tb->
  								     insert_size
  								     [0], body,
  								     zeros_num);
  					}
  
  					/* paste new entry, if item is directory item */
  					pasted =
  					    B_N_PITEM_HEAD(tb->R[0],
  							   item_pos - n +
  							   tb->rnum[0]);
  					if (is_direntry_le_ih(pasted)
  					    && pos_in_item >= 0) {
eba003055   Jeff Mahoney   reiserfs: use buf...
1065
  						leaf_paste_entries(&bi,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
  								   item_pos -
  								   n +
  								   tb->rnum[0],
  								   pos_in_item,
  								   1,
  								   (struct
  								    reiserfs_de_head
  								    *)body,
  								   body +
  								   DEH_SIZE,
  								   tb->
  								   insert_size
  								   [0]
  						    );
  						if (!pos_in_item) {
  
  							RFALSE(item_pos - n +
  							       tb->rnum[0],
  							       "PAP-12165: directory item must be first item of node when pasting is in 0th position");
  
  							/* update delimiting keys */
  							replace_key(tb,
  								    tb->CFR[0],
  								    tb->rkey[0],
  								    tb->R[0],
  								    0);
  						}
  					}
  
  					if (is_indirect_le_ih(pasted))
  						set_ih_free_space(pasted, 0);
  					zeros_num = tb->insert_size[0] = 0;
  				}
  			} else {	/* new item doesn't fall into R[0] */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1100

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1101
  				leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1102
  			}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1103
1104
  			break;
  		default:	/* cases d and t */
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
1105
1106
  			reiserfs_panic(tb->tb_sb, "PAP-12175",
  				       "rnum > 0: unexpected mode: %s(%d)",
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1107
1108
1109
1110
1111
  				       (flag ==
  					M_DELETE) ? "DELETE" : ((flag ==
  								 M_CUT) ? "CUT"
  								: "UNKNOWN"),
  				       flag);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1112
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1113

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1114
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1115

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
  	/* tb->rnum[0] > 0 */
  	RFALSE(tb->blknum[0] > 3,
  	       "PAP-12180: blknum can not be %d. It must be <= 3",
  	       tb->blknum[0]);
  	RFALSE(tb->blknum[0] < 0,
  	       "PAP-12185: blknum can not be %d. It must be >= 0",
  	       tb->blknum[0]);
  
  	/* if while adding to a node we discover that it is possible to split
  	   it in two, and merge the left part into the left neighbor and the
  	   right part into the right neighbor, eliminating the node */
  	if (tb->blknum[0] == 0) {	/* node S[0] is empty now */
  
  		RFALSE(!tb->lnum[0] || !tb->rnum[0],
  		       "PAP-12190: lnum and rnum must not be zero");
  		/* if insertion was done before 0-th position in R[0], right
  		   delimiting key of the tb->L[0]'s and left delimiting key are
  		   not set correctly */
  		if (tb->CFL[0]) {
  			if (!tb->CFR[0])
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
1136
1137
  				reiserfs_panic(tb->tb_sb, "vs-12195",
  					       "CFR not initialized");
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1138
1139
1140
1141
  			copy_key(B_N_PDELIM_KEY(tb->CFL[0], tb->lkey[0]),
  				 B_N_PDELIM_KEY(tb->CFR[0], tb->rkey[0]));
  			do_balance_mark_internal_dirty(tb, tb->CFL[0], 0);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1142

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1143
1144
1145
  		reiserfs_invalidate_buffer(tb, tbS0);
  		return 0;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1146

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
  	/* Fill new nodes that appear in place of S[0] */
  
  	/* I am told that this copying is because we need an array to enable
  	   the looping code. -Hans */
  	snum[0] = tb->s1num, snum[1] = tb->s2num;
  	sbytes[0] = tb->s1bytes;
  	sbytes[1] = tb->s2bytes;
  	for (i = tb->blknum[0] - 2; i >= 0; i--) {
  
  		RFALSE(!snum[i], "PAP-12200: snum[%d] == %d. Must be > 0", i,
  		       snum[i]);
  
  		/* here we shift from S to S_new nodes */
  
  		S_new[i] = get_FEB(tb);
  
  		/* initialized block type and tree level */
  		set_blkh_level(B_BLK_HEAD(S_new[i]), DISK_LEAF_NODE_LEVEL);
  
  		n = B_NR_ITEMS(tbS0);
  
  		switch (flag) {
  		case M_INSERT:	/* insert item */
  
  			if (n - snum[i] < item_pos) {	/* new item or it's part falls to first new node S_new[i] */
  				if (item_pos == n - snum[i] + 1 && sbytes[i] != -1) {	/* part of new item falls into S_new[i] */
  					int old_key_comp, old_len,
  					    r_zeros_number;
  					const char *r_body;
  					int version;
  
  					/* Move snum[i]-1 items from S[0] to S_new[i] */
  					leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
  							snum[i] - 1, -1,
  							S_new[i]);
  					/* Remember key component and item length */
  					version = ih_version(ih);
  					old_key_comp = le_ih_k_offset(ih);
  					old_len = ih_item_len(ih);
  
  					/* Calculate key component and item length to insert into S_new[i] */
  					set_le_ih_k_offset(ih,
  							   le_ih_k_offset(ih) +
  							   ((old_len -
  							     sbytes[i]) <<
  							    (is_indirect_le_ih
  							     (ih) ? tb->tb_sb->
  							     s_blocksize_bits -
  							     UNFM_P_SHIFT :
  							     0)));
  
  					put_ih_item_len(ih, sbytes[i]);
  
  					/* Insert part of the item into S_new[i] before 0-th item */
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
1201
  					buffer_info_init_bh(tb, &bi, S_new[i]);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
  
  					if ((old_len - sbytes[i]) > zeros_num) {
  						r_zeros_number = 0;
  						r_body =
  						    body + (old_len -
  							    sbytes[i]) -
  						    zeros_num;
  					} else {
  						r_body = body;
  						r_zeros_number =
  						    zeros_num - (old_len -
  								 sbytes[i]);
  						zeros_num -= r_zeros_number;
  					}
  
  					leaf_insert_into_buf(&bi, 0, ih, r_body,
  							     r_zeros_number);
  
  					/* Calculate key component and item length to insert into S[i] */
  					set_le_ih_k_offset(ih, old_key_comp);
  					put_ih_item_len(ih,
  							old_len - sbytes[i]);
  					tb->insert_size[0] -= sbytes[i];
  				} else {	/* whole new item falls into S_new[i] */
  
  					/* Shift snum[0] - 1 items to S_new[i] (sbytes[i] of split item) */
  					leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
  							snum[i] - 1, sbytes[i],
  							S_new[i]);
  
  					/* Insert new item into S_new[i] */
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
1233
  					buffer_info_init_bh(tb, &bi, S_new[i]);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1234
1235
1236
1237
1238
1239
1240
1241
  					leaf_insert_into_buf(&bi,
  							     item_pos - n +
  							     snum[i] - 1, ih,
  							     body, zeros_num);
  
  					zeros_num = tb->insert_size[0] = 0;
  				}
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1242

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1243
  			else {	/* new item or it part don't falls into S_new[i] */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1244

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1245
1246
  				leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
  						snum[i], sbytes[i], S_new[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1247
  			}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1248
1249
1250
1251
1252
1253
1254
1255
1256
  			break;
  
  		case M_PASTE:	/* append item */
  
  			if (n - snum[i] <= item_pos) {	/* pasted item or part if it falls to S_new[i] */
  				if (item_pos == n - snum[i] && sbytes[i] != -1) {	/* we must shift part of the appended item */
  					struct item_head *aux_ih;
  
  					RFALSE(ih, "PAP-12210: ih must be 0");
1d965fe0e   Jeff Mahoney   reiserfs: fix war...
1257
1258
  					aux_ih = B_N_PITEM_HEAD(tbS0, item_pos);
  					if (is_direntry_le_ih(aux_ih)) {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
  						/* we append to directory item */
  
  						int entry_count;
  
  						entry_count =
  						    ih_entry_count(aux_ih);
  
  						if (entry_count - sbytes[i] <
  						    pos_in_item
  						    && pos_in_item <=
  						    entry_count) {
  							/* new directory entry falls into S_new[i] */
  
  							RFALSE(!tb->
  							       insert_size[0],
  							       "PAP-12215: insert_size is already 0");
  							RFALSE(sbytes[i] - 1 >=
  							       entry_count,
  							       "PAP-12220: there are no so much entries (%d), only %d",
  							       sbytes[i] - 1,
  							       entry_count);
  
  							/* Shift snum[i]-1 items in whole. Shift sbytes[i] directory entries from directory item number snum[i] */
  							leaf_move_items
  							    (LEAF_FROM_S_TO_SNEW,
  							     tb, snum[i],
  							     sbytes[i] - 1,
  							     S_new[i]);
  							/* Paste given directory entry to directory item */
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
1288
  							buffer_info_init_bh(tb, &bi, S_new[i]);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1289
1290
1291
1292
1293
1294
1295
1296
  							leaf_paste_in_buffer
  							    (&bi, 0,
  							     pos_in_item -
  							     entry_count +
  							     sbytes[i] - 1,
  							     tb->insert_size[0],
  							     body, zeros_num);
  							/* paste new directory entry */
eba003055   Jeff Mahoney   reiserfs: use buf...
1297
  							leaf_paste_entries(&bi,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
  									   0,
  									   pos_in_item
  									   -
  									   entry_count
  									   +
  									   sbytes
  									   [i] -
  									   1, 1,
  									   (struct
  									    reiserfs_de_head
  									    *)
  									   body,
  									   body
  									   +
  									   DEH_SIZE,
  									   tb->
  									   insert_size
  									   [0]
  							    );
  							tb->insert_size[0] = 0;
  							pos_in_item++;
  						} else {	/* new directory entry doesn't fall into S_new[i] */
  							leaf_move_items
  							    (LEAF_FROM_S_TO_SNEW,
  							     tb, snum[i],
  							     sbytes[i],
  							     S_new[i]);
  						}
  					} else {	/* regular object */
  
  						int n_shift, n_rem,
  						    r_zeros_number;
  						const char *r_body;
  
  						RFALSE(pos_in_item !=
  						       ih_item_len
  						       (B_N_PITEM_HEAD
  							(tbS0, item_pos))
  						       || tb->insert_size[0] <=
  						       0,
  						       "PAP-12225: item too short or insert_size <= 0");
  
  						/* Calculate number of bytes which must be shifted from appended item */
  						n_shift =
  						    sbytes[i] -
  						    tb->insert_size[0];
  						if (n_shift < 0)
  							n_shift = 0;
  						leaf_move_items
  						    (LEAF_FROM_S_TO_SNEW, tb,
  						     snum[i], n_shift,
  						     S_new[i]);
  
  						/* Calculate number of bytes which must remain in body after append to S_new[i] */
  						n_rem =
  						    tb->insert_size[0] -
  						    sbytes[i];
  						if (n_rem < 0)
  							n_rem = 0;
  						/* Append part of body into S_new[0] */
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
1358
  						buffer_info_init_bh(tb, &bi, S_new[i]);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
  						if (n_rem > zeros_num) {
  							r_zeros_number = 0;
  							r_body =
  							    body + n_rem -
  							    zeros_num;
  						} else {
  							r_body = body;
  							r_zeros_number =
  							    zeros_num - n_rem;
  							zeros_num -=
  							    r_zeros_number;
  						}
  
  						leaf_paste_in_buffer(&bi, 0,
  								     n_shift,
  								     tb->
  								     insert_size
  								     [0] -
  								     n_rem,
  								     r_body,
  								     r_zeros_number);
  						{
  							struct item_head *tmp;
  
  							tmp =
  							    B_N_PITEM_HEAD(S_new
  									   [i],
  									   0);
  							if (is_indirect_le_ih
  							    (tmp)) {
  								set_ih_free_space
  								    (tmp, 0);
  								set_le_ih_k_offset
  								    (tmp,
  								     le_ih_k_offset
  								     (tmp) +
  								     (n_rem <<
  								      (tb->
  								       tb_sb->
  								       s_blocksize_bits
  								       -
  								       UNFM_P_SHIFT)));
  							} else {
  								set_le_ih_k_offset
  								    (tmp,
  								     le_ih_k_offset
  								     (tmp) +
  								     n_rem);
  							}
  						}
  
  						tb->insert_size[0] = n_rem;
  						if (!n_rem)
  							pos_in_item++;
  					}
  				} else
  					/* item falls wholly into S_new[i] */
  				{
8acc570fa   Harvey Harrison   reiserfs: fix mor...
1417
  					int leaf_mi;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1418
  					struct item_head *pasted;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1419

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1420
  #ifdef CONFIG_REISERFS_CHECK
8acc570fa   Harvey Harrison   reiserfs: fix mor...
1421
  					struct item_head *ih_check =
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1422
  					    B_N_PITEM_HEAD(tbS0, item_pos);
8acc570fa   Harvey Harrison   reiserfs: fix mor...
1423
1424
  					if (!is_direntry_le_ih(ih_check)
  					    && (pos_in_item != ih_item_len(ih_check)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1425
1426
  						|| tb->insert_size[0] <= 0))
  						reiserfs_panic(tb->tb_sb,
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
1427
1428
1429
1430
  							     "PAP-12235",
  							     "pos_in_item "
  							     "must be equal "
  							     "to ih_item_len");
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1431
  #endif				/* CONFIG_REISERFS_CHECK */
8acc570fa   Harvey Harrison   reiserfs: fix mor...
1432
  					leaf_mi =
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1433
1434
1435
1436
  					    leaf_move_items(LEAF_FROM_S_TO_SNEW,
  							    tb, snum[i],
  							    sbytes[i],
  							    S_new[i]);
8acc570fa   Harvey Harrison   reiserfs: fix mor...
1437
  					RFALSE(leaf_mi,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1438
  					       "PAP-12240: unexpected value returned by leaf_move_items (%d)",
8acc570fa   Harvey Harrison   reiserfs: fix mor...
1439
  					       leaf_mi);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1440
1441
  
  					/* paste into item */
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
1442
  					buffer_info_init_bh(tb, &bi, S_new[i]);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
  					leaf_paste_in_buffer(&bi,
  							     item_pos - n +
  							     snum[i],
  							     pos_in_item,
  							     tb->insert_size[0],
  							     body, zeros_num);
  
  					pasted =
  					    B_N_PITEM_HEAD(S_new[i],
  							   item_pos - n +
  							   snum[i]);
  					if (is_direntry_le_ih(pasted)) {
eba003055   Jeff Mahoney   reiserfs: use buf...
1455
  						leaf_paste_entries(&bi,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
  								   item_pos -
  								   n + snum[i],
  								   pos_in_item,
  								   1,
  								   (struct
  								    reiserfs_de_head
  								    *)body,
  								   body +
  								   DEH_SIZE,
  								   tb->
  								   insert_size
  								   [0]
  						    );
  					}
  
  					/* if we paste to indirect item update ih_free_space */
  					if (is_indirect_le_ih(pasted))
  						set_ih_free_space(pasted, 0);
  					zeros_num = tb->insert_size[0] = 0;
  				}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1476
  			}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1477
  			else {	/* pasted item doesn't fall into S_new[i] */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1478

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1479
1480
1481
1482
1483
  				leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
  						snum[i], sbytes[i], S_new[i]);
  			}
  			break;
  		default:	/* cases d and t */
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
1484
1485
  			reiserfs_panic(tb->tb_sb, "PAP-12245",
  				       "blknum > 2: unexpected mode: %s(%d)",
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1486
1487
1488
1489
1490
  				       (flag ==
  					M_DELETE) ? "DELETE" : ((flag ==
  								 M_CUT) ? "CUT"
  								: "UNKNOWN"),
  				       flag);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1491
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1492

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1493
1494
1495
1496
1497
1498
1499
  		memcpy(insert_key + i, B_N_PKEY(S_new[i], 0), KEY_SIZE);
  		insert_ptr[i] = S_new[i];
  
  		RFALSE(!buffer_journaled(S_new[i])
  		       || buffer_journal_dirty(S_new[i])
  		       || buffer_dirty(S_new[i]), "PAP-12247: S_new[%d] : (%b)",
  		       i, S_new[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1500
  	}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1501
1502
1503
1504
1505
1506
  	/* if the affected item was not wholly shifted then we perform all necessary operations on that part or whole of the
  	   affected item which remains in S */
  	if (0 <= item_pos && item_pos < tb->s0num) {	/* if we must insert or append into buffer S[0] */
  
  		switch (flag) {
  		case M_INSERT:	/* insert item into S[0] */
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
1507
  			buffer_info_init_tbS0(tb, &bi);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1508
1509
1510
1511
1512
1513
1514
1515
  			leaf_insert_into_buf(&bi, item_pos, ih, body,
  					     zeros_num);
  
  			/* If we insert the first key change the delimiting key */
  			if (item_pos == 0) {
  				if (tb->CFL[0])	/* can be 0 in reiserfsck */
  					replace_key(tb, tb->CFL[0], tb->lkey[0],
  						    tbS0, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1516
1517
  
  			}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
  			break;
  
  		case M_PASTE:{	/* append item in S[0] */
  				struct item_head *pasted;
  
  				pasted = B_N_PITEM_HEAD(tbS0, item_pos);
  				/* when directory, may be new entry already pasted */
  				if (is_direntry_le_ih(pasted)) {
  					if (pos_in_item >= 0 &&
  					    pos_in_item <=
  					    ih_entry_count(pasted)) {
  
  						RFALSE(!tb->insert_size[0],
  						       "PAP-12260: insert_size is 0 already");
  
  						/* prepare space */
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
1534
  						buffer_info_init_tbS0(tb, &bi);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1535
1536
1537
1538
1539
1540
1541
1542
1543
  						leaf_paste_in_buffer(&bi,
  								     item_pos,
  								     pos_in_item,
  								     tb->
  								     insert_size
  								     [0], body,
  								     zeros_num);
  
  						/* paste entry */
eba003055   Jeff Mahoney   reiserfs: use buf...
1544
  						leaf_paste_entries(&bi,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
  								   item_pos,
  								   pos_in_item,
  								   1,
  								   (struct
  								    reiserfs_de_head
  								    *)body,
  								   body +
  								   DEH_SIZE,
  								   tb->
  								   insert_size
  								   [0]
  						    );
  						if (!item_pos && !pos_in_item) {
  							RFALSE(!tb->CFL[0]
  							       || !tb->L[0],
  							       "PAP-12270: CFL[0]/L[0] must be specified");
  							if (tb->CFL[0]) {
  								replace_key(tb,
  									    tb->
  									    CFL
  									    [0],
  									    tb->
  									    lkey
  									    [0],
  									    tbS0,
  									    0);
  
  							}
  						}
  						tb->insert_size[0] = 0;
  					}
  				} else {	/* regular object */
  					if (pos_in_item == ih_item_len(pasted)) {
  
  						RFALSE(tb->insert_size[0] <= 0,
  						       "PAP-12275: insert size must not be %d",
  						       tb->insert_size[0]);
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
1582
  						buffer_info_init_tbS0(tb, &bi);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1583
1584
1585
1586
1587
1588
1589
1590
1591
  						leaf_paste_in_buffer(&bi,
  								     item_pos,
  								     pos_in_item,
  								     tb->
  								     insert_size
  								     [0], body,
  								     zeros_num);
  
  						if (is_indirect_le_ih(pasted)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1592
  #if 0
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1593
1594
1595
1596
1597
1598
1599
  							RFALSE(tb->
  							       insert_size[0] !=
  							       UNFM_P_SIZE,
  							       "PAP-12280: insert_size for indirect item must be %d, not %d",
  							       UNFM_P_SIZE,
  							       tb->
  							       insert_size[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1600
  #endif
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1601
1602
1603
1604
1605
  							set_ih_free_space
  							    (pasted, 0);
  						}
  						tb->insert_size[0] = 0;
  					}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1606
  #ifdef CONFIG_REISERFS_CHECK
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1607
1608
1609
1610
1611
  					else {
  						if (tb->insert_size[0]) {
  							print_cur_tb("12285");
  							reiserfs_panic(tb->
  								       tb_sb,
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
1612
1613
1614
1615
1616
  							    "PAP-12285",
  							    "insert_size "
  							    "must be 0 "
  							    "(%d)",
  							    tb->insert_size[0]);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1617
1618
1619
1620
1621
1622
  						}
  					}
  #endif				/* CONFIG_REISERFS_CHECK */
  
  				}
  			}	/* case M_PASTE: */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1623
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1624
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1625
  #ifdef CONFIG_REISERFS_CHECK
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1626
1627
1628
  	if (flag == M_PASTE && tb->insert_size[0]) {
  		print_cur_tb("12290");
  		reiserfs_panic(tb->tb_sb,
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
1629
  			       "PAP-12290", "insert_size is still not 0 (%d)",
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1630
1631
1632
  			       tb->insert_size[0]);
  	}
  #endif				/* CONFIG_REISERFS_CHECK */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1633
1634
  	return 0;
  }				/* Leaf level of the tree is balanced (end of balance_leaf) */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1635
1636
  
  /* Make empty node */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1637
  void make_empty_node(struct buffer_info *bi)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1638
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1639
  	struct block_head *blkh;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1640

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1641
  	RFALSE(bi->bi_bh == NULL, "PAP-12295: pointer to the buffer is NULL");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1642

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1643
1644
1645
  	blkh = B_BLK_HEAD(bi->bi_bh);
  	set_blkh_nr_item(blkh, 0);
  	set_blkh_free_space(blkh, MAX_CHILD_SIZE(bi->bi_bh));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1646

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1647
1648
  	if (bi->bi_parent)
  		B_N_CHILD(bi->bi_parent, bi->bi_position)->dc_size = 0;	/* Endian safe if 0 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1649
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1650
  /* Get first empty buffer */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1651
  struct buffer_head *get_FEB(struct tree_balance *tb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1652
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1653
  	int i;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1654
  	struct buffer_info bi;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1655

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1656
  	for (i = 0; i < MAX_FEB_SIZE; i++)
9dce07f1a   Al Viro   NULL noise: fs/*,...
1657
  		if (tb->FEB[i] != NULL)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1658
1659
1660
  			break;
  
  	if (i == MAX_FEB_SIZE)
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
1661
  		reiserfs_panic(tb->tb_sb, "vs-12300", "FEB list is empty");
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1662

fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
1663
  	buffer_info_init_bh(tb, &bi, tb->FEB[i]);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1664
  	make_empty_node(&bi);
fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
1665
1666
  	set_buffer_uptodate(tb->FEB[i]);
  	tb->used[i] = tb->FEB[i];
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1667
  	tb->FEB[i] = NULL;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1668

fba4ebb5f   Jeff Mahoney   reiserfs: factor ...
1669
  	return tb->used[i];
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1670
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1671
1672
1673
1674
  
  /* This is now used because reiserfs_free_block has to be able to
  ** schedule.
  */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1675
  static void store_thrown(struct tree_balance *tb, struct buffer_head *bh)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1676
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1677
1678
1679
  	int i;
  
  	if (buffer_dirty(bh))
45b03d5e8   Jeff Mahoney   reiserfs: rework ...
1680
1681
  		reiserfs_warning(tb->tb_sb, "reiserfs-12320",
  				 "called with dirty buffer");
79a81aef7   Ahmed S. Darwish   [PATCH] reiserfs:...
1682
  	for (i = 0; i < ARRAY_SIZE(tb->thrown); i++)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1683
1684
1685
1686
1687
  		if (!tb->thrown[i]) {
  			tb->thrown[i] = bh;
  			get_bh(bh);	/* free_thrown puts this */
  			return;
  		}
45b03d5e8   Jeff Mahoney   reiserfs: rework ...
1688
1689
  	reiserfs_warning(tb->tb_sb, "reiserfs-12321",
  			 "too many thrown buffers");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1690
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1691
1692
1693
1694
  static void free_thrown(struct tree_balance *tb)
  {
  	int i;
  	b_blocknr_t blocknr;
79a81aef7   Ahmed S. Darwish   [PATCH] reiserfs:...
1695
  	for (i = 0; i < ARRAY_SIZE(tb->thrown); i++) {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1696
1697
1698
  		if (tb->thrown[i]) {
  			blocknr = tb->thrown[i]->b_blocknr;
  			if (buffer_dirty(tb->thrown[i]))
45b03d5e8   Jeff Mahoney   reiserfs: rework ...
1699
1700
  				reiserfs_warning(tb->tb_sb, "reiserfs-12322",
  						 "called with dirty buffer %d",
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1701
1702
1703
1704
1705
  						 blocknr);
  			brelse(tb->thrown[i]);	/* incremented in store_thrown */
  			reiserfs_free_block(tb->transaction_handle, NULL,
  					    blocknr, 0);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1706
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1707
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1708
  void reiserfs_invalidate_buffer(struct tree_balance *tb, struct buffer_head *bh)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1709
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1710
1711
1712
1713
1714
1715
1716
  	struct block_head *blkh;
  	blkh = B_BLK_HEAD(bh);
  	set_blkh_level(blkh, FREE_LEVEL);
  	set_blkh_nr_item(blkh, 0);
  
  	clear_buffer_dirty(bh);
  	store_thrown(tb, bh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1717
1718
1719
  }
  
  /* Replace n_dest'th key in buffer dest by n_src'th key of buffer src.*/
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1720
1721
  void replace_key(struct tree_balance *tb, struct buffer_head *dest, int n_dest,
  		 struct buffer_head *src, int n_src)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1722
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
  	RFALSE(dest == NULL || src == NULL,
  	       "vs-12305: source or destination buffer is 0 (src=%p, dest=%p)",
  	       src, dest);
  	RFALSE(!B_IS_KEYS_LEVEL(dest),
  	       "vs-12310: invalid level (%z) for destination buffer. dest must be leaf",
  	       dest);
  	RFALSE(n_dest < 0 || n_src < 0,
  	       "vs-12315: src(%d) or dest(%d) key number < 0", n_src, n_dest);
  	RFALSE(n_dest >= B_NR_ITEMS(dest) || n_src >= B_NR_ITEMS(src),
  	       "vs-12320: src(%d(%d)) or dest(%d(%d)) key number is too big",
  	       n_src, B_NR_ITEMS(src), n_dest, B_NR_ITEMS(dest));
  
  	if (B_IS_ITEMS_LEVEL(src))
  		/* source buffer contains leaf node */
  		memcpy(B_N_PDELIM_KEY(dest, n_dest), B_N_PITEM_HEAD(src, n_src),
  		       KEY_SIZE);
  	else
  		memcpy(B_N_PDELIM_KEY(dest, n_dest), B_N_PDELIM_KEY(src, n_src),
  		       KEY_SIZE);
  
  	do_balance_mark_internal_dirty(tb, dest, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1744
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1745
  int get_left_neighbor_position(struct tree_balance *tb, int h)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1746
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1747
  	int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1748

9dce07f1a   Al Viro   NULL noise: fs/*,...
1749
  	RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FL[h] == NULL,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1750
1751
  	       "vs-12325: FL[%d](%p) or F[%d](%p) does not exist",
  	       h, tb->FL[h], h, PATH_H_PPARENT(tb->tb_path, h));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1752

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1753
1754
1755
1756
  	if (Sh_position == 0)
  		return B_NR_ITEMS(tb->FL[h]);
  	else
  		return Sh_position - 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1757
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1758
  int get_right_neighbor_position(struct tree_balance *tb, int h)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1759
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1760
  	int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1761

9dce07f1a   Al Viro   NULL noise: fs/*,...
1762
  	RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FR[h] == NULL,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1763
1764
  	       "vs-12330: F[%d](%p) or FR[%d](%p) does not exist",
  	       h, PATH_H_PPARENT(tb->tb_path, h), h, tb->FR[h]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1765

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1766
1767
1768
1769
  	if (Sh_position == B_NR_ITEMS(PATH_H_PPARENT(tb->tb_path, h)))
  		return 0;
  	else
  		return Sh_position + 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1770
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1771
  #ifdef CONFIG_REISERFS_CHECK
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1772
1773
1774
  int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value);
  static void check_internal_node(struct super_block *s, struct buffer_head *bh,
  				char *mes)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1775
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1776
1777
  	struct disk_child *dc;
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1778

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1779
  	RFALSE(!bh, "PAP-12336: bh == 0");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1780

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1781
1782
  	if (!bh || !B_IS_IN_TREE(bh))
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1783

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1784
1785
1786
1787
  	RFALSE(!buffer_dirty(bh) &&
  	       !(buffer_journaled(bh) || buffer_journal_dirty(bh)),
  	       "PAP-12337: buffer (%b) must be dirty", bh);
  	dc = B_N_CHILD(bh, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1788

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1789
1790
1791
  	for (i = 0; i <= B_NR_ITEMS(bh); i++, dc++) {
  		if (!is_reusable(s, dc_block_number(dc), 1)) {
  			print_cur_tb(mes);
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
1792
1793
  			reiserfs_panic(s, "PAP-12338",
  				       "invalid child pointer %y in %b",
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1794
1795
1796
  				       dc, bh);
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1797
  }
45b03d5e8   Jeff Mahoney   reiserfs: rework ...
1798
1799
  static int locked_or_not_in_tree(struct tree_balance *tb,
  				  struct buffer_head *bh, char *which)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1800
1801
1802
  {
  	if ((!buffer_journal_prepared(bh) && buffer_locked(bh)) ||
  	    !B_IS_IN_TREE(bh)) {
45b03d5e8   Jeff Mahoney   reiserfs: rework ...
1803
  		reiserfs_warning(tb->tb_sb, "vs-12339", "%s (%b)", which, bh);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1804
1805
1806
1807
  		return 1;
  	}
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1808

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1809
  static int check_before_balancing(struct tree_balance *tb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1810
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1811
  	int retval = 0;
08f14fc89   Frederic Weisbecker   kill-the-bkl/reis...
1812
  	if (REISERFS_SB(tb->tb_sb)->cur_tb) {
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
1813
1814
1815
  		reiserfs_panic(tb->tb_sb, "vs-12335", "suspect that schedule "
  			       "occurred based on cur_tb not being null at "
  			       "this point in code. do_balance cannot properly "
08f14fc89   Frederic Weisbecker   kill-the-bkl/reis...
1816
1817
  			       "handle concurrent tree accesses on a same "
  			       "mount point.");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1818
  	}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1819
1820
1821
1822
  
  	/* double check that buffers that we will modify are unlocked. (fix_nodes should already have
  	   prepped all of these for us). */
  	if (tb->lnum[0]) {
45b03d5e8   Jeff Mahoney   reiserfs: rework ...
1823
1824
1825
  		retval |= locked_or_not_in_tree(tb, tb->L[0], "L[0]");
  		retval |= locked_or_not_in_tree(tb, tb->FL[0], "FL[0]");
  		retval |= locked_or_not_in_tree(tb, tb->CFL[0], "CFL[0]");
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1826
  		check_leaf(tb->L[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1827
  	}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1828
  	if (tb->rnum[0]) {
45b03d5e8   Jeff Mahoney   reiserfs: rework ...
1829
1830
1831
  		retval |= locked_or_not_in_tree(tb, tb->R[0], "R[0]");
  		retval |= locked_or_not_in_tree(tb, tb->FR[0], "FR[0]");
  		retval |= locked_or_not_in_tree(tb, tb->CFR[0], "CFR[0]");
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1832
1833
  		check_leaf(tb->R[0]);
  	}
45b03d5e8   Jeff Mahoney   reiserfs: rework ...
1834
1835
  	retval |= locked_or_not_in_tree(tb, PATH_PLAST_BUFFER(tb->tb_path),
  					"S[0]");
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1836
  	check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1837

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1838
1839
  	return retval;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1840

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1841
  static void check_after_balance_leaf(struct tree_balance *tb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1842
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1843
1844
1845
1846
1847
1848
  	if (tb->lnum[0]) {
  		if (B_FREE_SPACE(tb->L[0]) !=
  		    MAX_CHILD_SIZE(tb->L[0]) -
  		    dc_size(B_N_CHILD
  			    (tb->FL[0], get_left_neighbor_position(tb, 0)))) {
  			print_cur_tb("12221");
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
1849
1850
  			reiserfs_panic(tb->tb_sb, "PAP-12355",
  				       "shift to left was incorrect");
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1851
1852
1853
1854
1855
1856
1857
1858
  		}
  	}
  	if (tb->rnum[0]) {
  		if (B_FREE_SPACE(tb->R[0]) !=
  		    MAX_CHILD_SIZE(tb->R[0]) -
  		    dc_size(B_N_CHILD
  			    (tb->FR[0], get_right_neighbor_position(tb, 0)))) {
  			print_cur_tb("12222");
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
1859
1860
  			reiserfs_panic(tb->tb_sb, "PAP-12360",
  				       "shift to right was incorrect");
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
  		}
  	}
  	if (PATH_H_PBUFFER(tb->tb_path, 1) &&
  	    (B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0)) !=
  	     (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
  	      dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
  				PATH_H_POSITION(tb->tb_path, 1)))))) {
  		int left = B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0));
  		int right = (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
  			     dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
  					       PATH_H_POSITION(tb->tb_path,
  							       1))));
  		print_cur_tb("12223");
45b03d5e8   Jeff Mahoney   reiserfs: rework ...
1874
  		reiserfs_warning(tb->tb_sb, "reiserfs-12363",
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
  				 "B_FREE_SPACE (PATH_H_PBUFFER(tb->tb_path,0)) = %d; "
  				 "MAX_CHILD_SIZE (%d) - dc_size( %y, %d ) [%d] = %d",
  				 left,
  				 MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)),
  				 PATH_H_PBUFFER(tb->tb_path, 1),
  				 PATH_H_POSITION(tb->tb_path, 1),
  				 dc_size(B_N_CHILD
  					 (PATH_H_PBUFFER(tb->tb_path, 1),
  					  PATH_H_POSITION(tb->tb_path, 1))),
  				 right);
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
1885
  		reiserfs_panic(tb->tb_sb, "PAP-12365", "S is incorrect");
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1886
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1887
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1888
  static void check_leaf_level(struct tree_balance *tb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1889
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1890
1891
1892
1893
  	check_leaf(tb->L[0]);
  	check_leaf(tb->R[0]);
  	check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1894

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
  static void check_internal_levels(struct tree_balance *tb)
  {
  	int h;
  
  	/* check all internal nodes */
  	for (h = 1; tb->insert_size[h]; h++) {
  		check_internal_node(tb->tb_sb, PATH_H_PBUFFER(tb->tb_path, h),
  				    "BAD BUFFER ON PATH");
  		if (tb->lnum[h])
  			check_internal_node(tb->tb_sb, tb->L[h], "BAD L");
  		if (tb->rnum[h])
  			check_internal_node(tb->tb_sb, tb->R[h], "BAD R");
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1908
1909
1910
1911
  
  }
  
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
  /* Now we have all of the buffers that must be used in balancing of
     the tree.  We rely on the assumption that schedule() will not occur
     while do_balance works. ( Only interrupt handlers are acceptable.)
     We balance the tree according to the analysis made before this,
     using buffers already obtained.  For SMP support it will someday be
     necessary to add ordered locking of tb. */
  
  /* Some interesting rules of balancing:
  
     we delete a maximum of two nodes per level per balancing: we never
     delete R, when we delete two of three nodes L, S, R then we move
     them into R.
  
     we only delete L if we are deleting two nodes, if we delete only
     one node we delete S
  
     if we shift leaves then we shift as much as we can: this is a
     deliberate policy of extremism in node packing which results in
     higher average utilization after repeated random balance operations
     at the cost of more memory copies and more balancing as a result of
     small insertions to full nodes.
  
     if we shift internal nodes we try to evenly balance the node
     utilization, with consequent less balancing at the cost of lower
     utilization.
  
     one could argue that the policy for directories in leaves should be
     that of internal nodes, but we will wait until another day to
     evaluate this....  It would be nice to someday measure and prove
     these assumptions as to what is optimal....
  
  */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1944
  static inline void do_balance_starts(struct tree_balance *tb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1945
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1946
1947
  	/* use print_cur_tb() to see initial state of struct
  	   tree_balance */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1948

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1949
  	/* store_print_tb (tb); */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1950

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1951
  	/* do not delete, just comment it out */
0222e6571   Jeff Mahoney   reiserfs: strip t...
1952
  /*    print_tb(flag, PATH_LAST_POSITION(tb->tb_path), tb->tb_path->pos_in_item, tb,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1953
  	     "check");*/
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1954
  	RFALSE(check_before_balancing(tb), "PAP-12340: locked buffers in TB");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1955
  #ifdef CONFIG_REISERFS_CHECK
08f14fc89   Frederic Weisbecker   kill-the-bkl/reis...
1956
  	REISERFS_SB(tb->tb_sb)->cur_tb = tb;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1957
1958
  #endif
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1959
  static inline void do_balance_completed(struct tree_balance *tb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1960
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1961

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1962
  #ifdef CONFIG_REISERFS_CHECK
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1963
1964
  	check_leaf_level(tb);
  	check_internal_levels(tb);
08f14fc89   Frederic Weisbecker   kill-the-bkl/reis...
1965
  	REISERFS_SB(tb->tb_sb)->cur_tb = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1966
  #endif
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1967
1968
1969
1970
  	/* reiserfs_free_block is no longer schedule safe.  So, we need to
  	 ** put the buffers we want freed on the thrown list during do_balance,
  	 ** and then free them now
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1971

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1972
  	REISERFS_SB(tb->tb_sb)->s_do_balance++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1973

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1974
1975
  	/* release all nodes hold to perform the balancing */
  	unfix_nodes(tb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1976

bd4c625c0   Linus Torvalds   reiserfs: run scr...
1977
  	free_thrown(tb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1978
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
  void do_balance(struct tree_balance *tb,	/* tree_balance structure */
  		struct item_head *ih,	/* item header of inserted item */
  		const char *body,	/* body  of inserted item or bytes to paste */
  		int flag)
  {				/* i - insert, d - delete
  				   c - cut, p - paste
  
  				   Cut means delete part of an item
  				   (includes removing an entry from a
  				   directory).
  
  				   Delete means delete whole item.
  
  				   Insert means add a new item into the
  				   tree.
  
  				   Paste means to append to the end of an
  				   existing file or to insert a directory
  				   entry.  */
  	int child_pos,		/* position of a child node in its parent */
  	 h;			/* level of the tree being processed */
  	struct item_head insert_key[2];	/* in our processing of one level
  					   we sometimes determine what
  					   must be inserted into the next
  					   higher level.  This insertion
  					   consists of a key or two keys
  					   and their corresponding
  					   pointers */
  	struct buffer_head *insert_ptr[2];	/* inserted node-ptrs for the next
  						   level */
  
  	tb->tb_mode = flag;
  	tb->need_balance_dirty = 0;
  
  	if (FILESYSTEM_CHANGED_TB(tb)) {
c3a9c2109   Jeff Mahoney   reiserfs: rework ...
2014
2015
  		reiserfs_panic(tb->tb_sb, "clm-6000", "fs generation has "
  			       "changed");
bd4c625c0   Linus Torvalds   reiserfs: run scr...
2016
2017
2018
  	}
  	/* if we have no real work to do  */
  	if (!tb->insert_size[0]) {
45b03d5e8   Jeff Mahoney   reiserfs: rework ...
2019
2020
  		reiserfs_warning(tb->tb_sb, "PAP-12350",
  				 "insert_size == 0, mode == %c", flag);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
2021
2022
2023
  		unfix_nodes(tb);
  		return;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2024

bd4c625c0   Linus Torvalds   reiserfs: run scr...
2025
2026
  	atomic_inc(&(fs_generation(tb->tb_sb)));
  	do_balance_starts(tb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2027

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2028
2029
  	/* balance leaf returns 0 except if combining L R and S into
  	   one node.  see balance_internal() for explanation of this
bd4c625c0   Linus Torvalds   reiserfs: run scr...
2030
2031
2032
  	   line of code. */
  	child_pos = PATH_H_B_ITEM_ORDER(tb->tb_path, 0) +
  	    balance_leaf(tb, ih, body, flag, insert_key, insert_ptr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2033
2034
  
  #ifdef CONFIG_REISERFS_CHECK
bd4c625c0   Linus Torvalds   reiserfs: run scr...
2035
  	check_after_balance_leaf(tb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2036
  #endif
bd4c625c0   Linus Torvalds   reiserfs: run scr...
2037
2038
2039
2040
  	/* Balance internal level of the tree. */
  	for (h = 1; h < MAX_HEIGHT && tb->insert_size[h]; h++)
  		child_pos =
  		    balance_internal(tb, h, child_pos, insert_key, insert_ptr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2041

bd4c625c0   Linus Torvalds   reiserfs: run scr...
2042
  	do_balance_completed(tb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2043
2044
  
  }