162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
362306a36Sopenharmony_ci */
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include <linux/uaccess.h>
662306a36Sopenharmony_ci#include <linux/string.h>
762306a36Sopenharmony_ci#include <linux/time.h>
862306a36Sopenharmony_ci#include "reiserfs.h"
962306a36Sopenharmony_ci#include <linux/buffer_head.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci/*
1262306a36Sopenharmony_ci * copy copy_count entries from source directory item to dest buffer
1362306a36Sopenharmony_ci * (creating new item if needed)
1462306a36Sopenharmony_ci */
1562306a36Sopenharmony_cistatic void leaf_copy_dir_entries(struct buffer_info *dest_bi,
1662306a36Sopenharmony_ci				  struct buffer_head *source, int last_first,
1762306a36Sopenharmony_ci				  int item_num, int from, int copy_count)
1862306a36Sopenharmony_ci{
1962306a36Sopenharmony_ci	struct buffer_head *dest = dest_bi->bi_bh;
2062306a36Sopenharmony_ci	/*
2162306a36Sopenharmony_ci	 * either the number of target item, or if we must create a
2262306a36Sopenharmony_ci	 * new item, the number of the item we will create it next to
2362306a36Sopenharmony_ci	 */
2462306a36Sopenharmony_ci	int item_num_in_dest;
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci	struct item_head *ih;
2762306a36Sopenharmony_ci	struct reiserfs_de_head *deh;
2862306a36Sopenharmony_ci	int copy_records_len;	/* length of all records in item to be copied */
2962306a36Sopenharmony_ci	char *records;
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci	ih = item_head(source, item_num);
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci	RFALSE(!is_direntry_le_ih(ih), "vs-10000: item must be directory item");
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	/*
3662306a36Sopenharmony_ci	 * length of all record to be copied and first byte of
3762306a36Sopenharmony_ci	 * the last of them
3862306a36Sopenharmony_ci	 */
3962306a36Sopenharmony_ci	deh = B_I_DEH(source, ih);
4062306a36Sopenharmony_ci	if (copy_count) {
4162306a36Sopenharmony_ci		copy_records_len = (from ? deh_location(&deh[from - 1]) :
4262306a36Sopenharmony_ci				    ih_item_len(ih)) -
4362306a36Sopenharmony_ci		    deh_location(&deh[from + copy_count - 1]);
4462306a36Sopenharmony_ci		records =
4562306a36Sopenharmony_ci		    source->b_data + ih_location(ih) +
4662306a36Sopenharmony_ci		    deh_location(&deh[from + copy_count - 1]);
4762306a36Sopenharmony_ci	} else {
4862306a36Sopenharmony_ci		copy_records_len = 0;
4962306a36Sopenharmony_ci		records = NULL;
5062306a36Sopenharmony_ci	}
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	/* when copy last to first, dest buffer can contain 0 items */
5362306a36Sopenharmony_ci	item_num_in_dest =
5462306a36Sopenharmony_ci	    (last_first ==
5562306a36Sopenharmony_ci	     LAST_TO_FIRST) ? ((B_NR_ITEMS(dest)) ? 0 : -1) : (B_NR_ITEMS(dest)
5662306a36Sopenharmony_ci							       - 1);
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	/*
5962306a36Sopenharmony_ci	 * if there are no items in dest or the first/last item in
6062306a36Sopenharmony_ci	 * dest is not item of the same directory
6162306a36Sopenharmony_ci	 */
6262306a36Sopenharmony_ci	if ((item_num_in_dest == -1) ||
6362306a36Sopenharmony_ci	    (last_first == FIRST_TO_LAST && le_ih_k_offset(ih) == DOT_OFFSET) ||
6462306a36Sopenharmony_ci	    (last_first == LAST_TO_FIRST
6562306a36Sopenharmony_ci	     && comp_short_le_keys /*COMP_SHORT_KEYS */ (&ih->ih_key,
6662306a36Sopenharmony_ci							 leaf_key(dest,
6762306a36Sopenharmony_ci								  item_num_in_dest))))
6862306a36Sopenharmony_ci	{
6962306a36Sopenharmony_ci		/* create new item in dest */
7062306a36Sopenharmony_ci		struct item_head new_ih;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci		/* form item header */
7362306a36Sopenharmony_ci		memcpy(&new_ih.ih_key, &ih->ih_key, KEY_SIZE);
7462306a36Sopenharmony_ci		put_ih_version(&new_ih, KEY_FORMAT_3_5);
7562306a36Sopenharmony_ci		/* calculate item len */
7662306a36Sopenharmony_ci		put_ih_item_len(&new_ih,
7762306a36Sopenharmony_ci				DEH_SIZE * copy_count + copy_records_len);
7862306a36Sopenharmony_ci		put_ih_entry_count(&new_ih, 0);
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci		if (last_first == LAST_TO_FIRST) {
8162306a36Sopenharmony_ci			/* form key by the following way */
8262306a36Sopenharmony_ci			if (from < ih_entry_count(ih)) {
8362306a36Sopenharmony_ci				set_le_ih_k_offset(&new_ih,
8462306a36Sopenharmony_ci						   deh_offset(&deh[from]));
8562306a36Sopenharmony_ci			} else {
8662306a36Sopenharmony_ci				/*
8762306a36Sopenharmony_ci				 * no entries will be copied to this
8862306a36Sopenharmony_ci				 * item in this function
8962306a36Sopenharmony_ci				 */
9062306a36Sopenharmony_ci				set_le_ih_k_offset(&new_ih, U32_MAX);
9162306a36Sopenharmony_ci				/*
9262306a36Sopenharmony_ci				 * this item is not yet valid, but we
9362306a36Sopenharmony_ci				 * want I_IS_DIRECTORY_ITEM to return 1
9462306a36Sopenharmony_ci				 * for it, so we -1
9562306a36Sopenharmony_ci				 */
9662306a36Sopenharmony_ci			}
9762306a36Sopenharmony_ci			set_le_key_k_type(KEY_FORMAT_3_5, &new_ih.ih_key,
9862306a36Sopenharmony_ci					  TYPE_DIRENTRY);
9962306a36Sopenharmony_ci		}
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci		/* insert item into dest buffer */
10262306a36Sopenharmony_ci		leaf_insert_into_buf(dest_bi,
10362306a36Sopenharmony_ci				     (last_first ==
10462306a36Sopenharmony_ci				      LAST_TO_FIRST) ? 0 : B_NR_ITEMS(dest),
10562306a36Sopenharmony_ci				     &new_ih, NULL, 0);
10662306a36Sopenharmony_ci	} else {
10762306a36Sopenharmony_ci		/* prepare space for entries */
10862306a36Sopenharmony_ci		leaf_paste_in_buffer(dest_bi,
10962306a36Sopenharmony_ci				     (last_first ==
11062306a36Sopenharmony_ci				      FIRST_TO_LAST) ? (B_NR_ITEMS(dest) -
11162306a36Sopenharmony_ci							1) : 0, MAX_US_INT,
11262306a36Sopenharmony_ci				     DEH_SIZE * copy_count + copy_records_len,
11362306a36Sopenharmony_ci				     records, 0);
11462306a36Sopenharmony_ci	}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	item_num_in_dest =
11762306a36Sopenharmony_ci	    (last_first == FIRST_TO_LAST) ? (B_NR_ITEMS(dest) - 1) : 0;
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	leaf_paste_entries(dest_bi, item_num_in_dest,
12062306a36Sopenharmony_ci			   (last_first ==
12162306a36Sopenharmony_ci			    FIRST_TO_LAST) ? ih_entry_count(item_head(dest,
12262306a36Sopenharmony_ci									  item_num_in_dest))
12362306a36Sopenharmony_ci			   : 0, copy_count, deh + from, records,
12462306a36Sopenharmony_ci			   DEH_SIZE * copy_count + copy_records_len);
12562306a36Sopenharmony_ci}
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci/*
12862306a36Sopenharmony_ci * Copy the first (if last_first == FIRST_TO_LAST) or last
12962306a36Sopenharmony_ci * (last_first == LAST_TO_FIRST) item or part of it or nothing
13062306a36Sopenharmony_ci * (see the return 0 below) from SOURCE to the end (if last_first)
13162306a36Sopenharmony_ci * or beginning (!last_first) of the DEST
13262306a36Sopenharmony_ci */
13362306a36Sopenharmony_ci/* returns 1 if anything was copied, else 0 */
13462306a36Sopenharmony_cistatic int leaf_copy_boundary_item(struct buffer_info *dest_bi,
13562306a36Sopenharmony_ci				   struct buffer_head *src, int last_first,
13662306a36Sopenharmony_ci				   int bytes_or_entries)
13762306a36Sopenharmony_ci{
13862306a36Sopenharmony_ci	struct buffer_head *dest = dest_bi->bi_bh;
13962306a36Sopenharmony_ci	/* number of items in the source and destination buffers */
14062306a36Sopenharmony_ci	int dest_nr_item, src_nr_item;
14162306a36Sopenharmony_ci	struct item_head *ih;
14262306a36Sopenharmony_ci	struct item_head *dih;
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	dest_nr_item = B_NR_ITEMS(dest);
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	/*
14762306a36Sopenharmony_ci	 * if ( DEST is empty or first item of SOURCE and last item of
14862306a36Sopenharmony_ci	 * DEST are the items of different objects or of different types )
14962306a36Sopenharmony_ci	 * then there is no need to treat this item differently from the
15062306a36Sopenharmony_ci	 * other items that we copy, so we return
15162306a36Sopenharmony_ci	 */
15262306a36Sopenharmony_ci	if (last_first == FIRST_TO_LAST) {
15362306a36Sopenharmony_ci		ih = item_head(src, 0);
15462306a36Sopenharmony_ci		dih = item_head(dest, dest_nr_item - 1);
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci		/* there is nothing to merge */
15762306a36Sopenharmony_ci		if (!dest_nr_item
15862306a36Sopenharmony_ci		    || (!op_is_left_mergeable(&ih->ih_key, src->b_size)))
15962306a36Sopenharmony_ci			return 0;
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci		RFALSE(!ih_item_len(ih),
16262306a36Sopenharmony_ci		       "vs-10010: item can not have empty length");
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci		if (is_direntry_le_ih(ih)) {
16562306a36Sopenharmony_ci			if (bytes_or_entries == -1)
16662306a36Sopenharmony_ci				/* copy all entries to dest */
16762306a36Sopenharmony_ci				bytes_or_entries = ih_entry_count(ih);
16862306a36Sopenharmony_ci			leaf_copy_dir_entries(dest_bi, src, FIRST_TO_LAST, 0, 0,
16962306a36Sopenharmony_ci					      bytes_or_entries);
17062306a36Sopenharmony_ci			return 1;
17162306a36Sopenharmony_ci		}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci		/*
17462306a36Sopenharmony_ci		 * copy part of the body of the first item of SOURCE
17562306a36Sopenharmony_ci		 * to the end of the body of the last item of the DEST
17662306a36Sopenharmony_ci		 * part defined by 'bytes_or_entries'; if bytes_or_entries
17762306a36Sopenharmony_ci		 * == -1 copy whole body; don't create new item header
17862306a36Sopenharmony_ci		 */
17962306a36Sopenharmony_ci		if (bytes_or_entries == -1)
18062306a36Sopenharmony_ci			bytes_or_entries = ih_item_len(ih);
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci#ifdef CONFIG_REISERFS_CHECK
18362306a36Sopenharmony_ci		else {
18462306a36Sopenharmony_ci			if (bytes_or_entries == ih_item_len(ih)
18562306a36Sopenharmony_ci			    && is_indirect_le_ih(ih))
18662306a36Sopenharmony_ci				if (get_ih_free_space(ih))
18762306a36Sopenharmony_ci					reiserfs_panic(sb_from_bi(dest_bi),
18862306a36Sopenharmony_ci						       "vs-10020",
18962306a36Sopenharmony_ci						       "last unformatted node "
19062306a36Sopenharmony_ci						       "must be filled "
19162306a36Sopenharmony_ci						       "entirely (%h)", ih);
19262306a36Sopenharmony_ci		}
19362306a36Sopenharmony_ci#endif
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci		/*
19662306a36Sopenharmony_ci		 * merge first item (or its part) of src buffer with the last
19762306a36Sopenharmony_ci		 * item of dest buffer. Both are of the same file
19862306a36Sopenharmony_ci		 */
19962306a36Sopenharmony_ci		leaf_paste_in_buffer(dest_bi,
20062306a36Sopenharmony_ci				     dest_nr_item - 1, ih_item_len(dih),
20162306a36Sopenharmony_ci				     bytes_or_entries, ih_item_body(src, ih), 0);
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci		if (is_indirect_le_ih(dih)) {
20462306a36Sopenharmony_ci			RFALSE(get_ih_free_space(dih),
20562306a36Sopenharmony_ci			       "vs-10030: merge to left: last unformatted node of non-last indirect item %h must have zerto free space",
20662306a36Sopenharmony_ci			       ih);
20762306a36Sopenharmony_ci			if (bytes_or_entries == ih_item_len(ih))
20862306a36Sopenharmony_ci				set_ih_free_space(dih, get_ih_free_space(ih));
20962306a36Sopenharmony_ci		}
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci		return 1;
21262306a36Sopenharmony_ci	}
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci	/* copy boundary item to right (last_first == LAST_TO_FIRST) */
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	/*
21762306a36Sopenharmony_ci	 * (DEST is empty or last item of SOURCE and first item of DEST
21862306a36Sopenharmony_ci	 * are the items of different object or of different types)
21962306a36Sopenharmony_ci	 */
22062306a36Sopenharmony_ci	src_nr_item = B_NR_ITEMS(src);
22162306a36Sopenharmony_ci	ih = item_head(src, src_nr_item - 1);
22262306a36Sopenharmony_ci	dih = item_head(dest, 0);
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	if (!dest_nr_item || !op_is_left_mergeable(&dih->ih_key, src->b_size))
22562306a36Sopenharmony_ci		return 0;
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	if (is_direntry_le_ih(ih)) {
22862306a36Sopenharmony_ci		/*
22962306a36Sopenharmony_ci		 * bytes_or_entries = entries number in last
23062306a36Sopenharmony_ci		 * item body of SOURCE
23162306a36Sopenharmony_ci		 */
23262306a36Sopenharmony_ci		if (bytes_or_entries == -1)
23362306a36Sopenharmony_ci			bytes_or_entries = ih_entry_count(ih);
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci		leaf_copy_dir_entries(dest_bi, src, LAST_TO_FIRST,
23662306a36Sopenharmony_ci				      src_nr_item - 1,
23762306a36Sopenharmony_ci				      ih_entry_count(ih) - bytes_or_entries,
23862306a36Sopenharmony_ci				      bytes_or_entries);
23962306a36Sopenharmony_ci		return 1;
24062306a36Sopenharmony_ci	}
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci	/*
24362306a36Sopenharmony_ci	 * copy part of the body of the last item of SOURCE to the
24462306a36Sopenharmony_ci	 * begin of the body of the first item of the DEST; part defined
24562306a36Sopenharmony_ci	 * by 'bytes_or_entries'; if byte_or_entriess == -1 copy whole body;
24662306a36Sopenharmony_ci	 * change first item key of the DEST; don't create new item header
24762306a36Sopenharmony_ci	 */
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci	RFALSE(is_indirect_le_ih(ih) && get_ih_free_space(ih),
25062306a36Sopenharmony_ci	       "vs-10040: merge to right: last unformatted node of non-last indirect item must be filled entirely (%h)",
25162306a36Sopenharmony_ci	       ih);
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	if (bytes_or_entries == -1) {
25462306a36Sopenharmony_ci		/* bytes_or_entries = length of last item body of SOURCE */
25562306a36Sopenharmony_ci		bytes_or_entries = ih_item_len(ih);
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci		RFALSE(le_ih_k_offset(dih) !=
25862306a36Sopenharmony_ci		       le_ih_k_offset(ih) + op_bytes_number(ih, src->b_size),
25962306a36Sopenharmony_ci		       "vs-10050: items %h and %h do not match", ih, dih);
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci		/* change first item key of the DEST */
26262306a36Sopenharmony_ci		set_le_ih_k_offset(dih, le_ih_k_offset(ih));
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci		/* item becomes non-mergeable */
26562306a36Sopenharmony_ci		/* or mergeable if left item was */
26662306a36Sopenharmony_ci		set_le_ih_k_type(dih, le_ih_k_type(ih));
26762306a36Sopenharmony_ci	} else {
26862306a36Sopenharmony_ci		/* merge to right only part of item */
26962306a36Sopenharmony_ci		RFALSE(ih_item_len(ih) <= bytes_or_entries,
27062306a36Sopenharmony_ci		       "vs-10060: no so much bytes %lu (needed %lu)",
27162306a36Sopenharmony_ci		       (unsigned long)ih_item_len(ih),
27262306a36Sopenharmony_ci		       (unsigned long)bytes_or_entries);
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci		/* change first item key of the DEST */
27562306a36Sopenharmony_ci		if (is_direct_le_ih(dih)) {
27662306a36Sopenharmony_ci			RFALSE(le_ih_k_offset(dih) <=
27762306a36Sopenharmony_ci			       (unsigned long)bytes_or_entries,
27862306a36Sopenharmony_ci			       "vs-10070: dih %h, bytes_or_entries(%d)", dih,
27962306a36Sopenharmony_ci			       bytes_or_entries);
28062306a36Sopenharmony_ci			set_le_ih_k_offset(dih,
28162306a36Sopenharmony_ci					   le_ih_k_offset(dih) -
28262306a36Sopenharmony_ci					   bytes_or_entries);
28362306a36Sopenharmony_ci		} else {
28462306a36Sopenharmony_ci			RFALSE(le_ih_k_offset(dih) <=
28562306a36Sopenharmony_ci			       (bytes_or_entries / UNFM_P_SIZE) * dest->b_size,
28662306a36Sopenharmony_ci			       "vs-10080: dih %h, bytes_or_entries(%d)",
28762306a36Sopenharmony_ci			       dih,
28862306a36Sopenharmony_ci			       (bytes_or_entries / UNFM_P_SIZE) * dest->b_size);
28962306a36Sopenharmony_ci			set_le_ih_k_offset(dih,
29062306a36Sopenharmony_ci					   le_ih_k_offset(dih) -
29162306a36Sopenharmony_ci					   ((bytes_or_entries / UNFM_P_SIZE) *
29262306a36Sopenharmony_ci					    dest->b_size));
29362306a36Sopenharmony_ci		}
29462306a36Sopenharmony_ci	}
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci	leaf_paste_in_buffer(dest_bi, 0, 0, bytes_or_entries,
29762306a36Sopenharmony_ci			     ih_item_body(src,
29862306a36Sopenharmony_ci				       ih) + ih_item_len(ih) - bytes_or_entries,
29962306a36Sopenharmony_ci			     0);
30062306a36Sopenharmony_ci	return 1;
30162306a36Sopenharmony_ci}
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci/*
30462306a36Sopenharmony_ci * copy cpy_mun items from buffer src to buffer dest
30562306a36Sopenharmony_ci * last_first == FIRST_TO_LAST means, that we copy cpy_num items beginning
30662306a36Sopenharmony_ci *                             from first-th item in src to tail of dest
30762306a36Sopenharmony_ci * last_first == LAST_TO_FIRST means, that we copy cpy_num items beginning
30862306a36Sopenharmony_ci *                             from first-th item in src to head of dest
30962306a36Sopenharmony_ci */
31062306a36Sopenharmony_cistatic void leaf_copy_items_entirely(struct buffer_info *dest_bi,
31162306a36Sopenharmony_ci				     struct buffer_head *src, int last_first,
31262306a36Sopenharmony_ci				     int first, int cpy_num)
31362306a36Sopenharmony_ci{
31462306a36Sopenharmony_ci	struct buffer_head *dest;
31562306a36Sopenharmony_ci	int nr, free_space;
31662306a36Sopenharmony_ci	int dest_before;
31762306a36Sopenharmony_ci	int last_loc, last_inserted_loc, location;
31862306a36Sopenharmony_ci	int i, j;
31962306a36Sopenharmony_ci	struct block_head *blkh;
32062306a36Sopenharmony_ci	struct item_head *ih;
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	RFALSE(last_first != LAST_TO_FIRST && last_first != FIRST_TO_LAST,
32362306a36Sopenharmony_ci	       "vs-10090: bad last_first parameter %d", last_first);
32462306a36Sopenharmony_ci	RFALSE(B_NR_ITEMS(src) - first < cpy_num,
32562306a36Sopenharmony_ci	       "vs-10100: too few items in source %d, required %d from %d",
32662306a36Sopenharmony_ci	       B_NR_ITEMS(src), cpy_num, first);
32762306a36Sopenharmony_ci	RFALSE(cpy_num < 0, "vs-10110: can not copy negative amount of items");
32862306a36Sopenharmony_ci	RFALSE(!dest_bi, "vs-10120: can not copy negative amount of items");
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci	dest = dest_bi->bi_bh;
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	RFALSE(!dest, "vs-10130: can not copy negative amount of items");
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci	if (cpy_num == 0)
33562306a36Sopenharmony_ci		return;
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci	blkh = B_BLK_HEAD(dest);
33862306a36Sopenharmony_ci	nr = blkh_nr_item(blkh);
33962306a36Sopenharmony_ci	free_space = blkh_free_space(blkh);
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci	/*
34262306a36Sopenharmony_ci	 * we will insert items before 0-th or nr-th item in dest buffer.
34362306a36Sopenharmony_ci	 * It depends of last_first parameter
34462306a36Sopenharmony_ci	 */
34562306a36Sopenharmony_ci	dest_before = (last_first == LAST_TO_FIRST) ? 0 : nr;
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci	/* location of head of first new item */
34862306a36Sopenharmony_ci	ih = item_head(dest, dest_before);
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci	RFALSE(blkh_free_space(blkh) < cpy_num * IH_SIZE,
35162306a36Sopenharmony_ci	       "vs-10140: not enough free space for headers %d (needed %d)",
35262306a36Sopenharmony_ci	       B_FREE_SPACE(dest), cpy_num * IH_SIZE);
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci	/* prepare space for headers */
35562306a36Sopenharmony_ci	memmove(ih + cpy_num, ih, (nr - dest_before) * IH_SIZE);
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci	/* copy item headers */
35862306a36Sopenharmony_ci	memcpy(ih, item_head(src, first), cpy_num * IH_SIZE);
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci	free_space -= (IH_SIZE * cpy_num);
36162306a36Sopenharmony_ci	set_blkh_free_space(blkh, free_space);
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci	/* location of unmovable item */
36462306a36Sopenharmony_ci	j = location = (dest_before == 0) ? dest->b_size : ih_location(ih - 1);
36562306a36Sopenharmony_ci	for (i = dest_before; i < nr + cpy_num; i++) {
36662306a36Sopenharmony_ci		location -= ih_item_len(ih + i - dest_before);
36762306a36Sopenharmony_ci		put_ih_location(ih + i - dest_before, location);
36862306a36Sopenharmony_ci	}
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci	/* prepare space for items */
37162306a36Sopenharmony_ci	last_loc = ih_location(&ih[nr + cpy_num - 1 - dest_before]);
37262306a36Sopenharmony_ci	last_inserted_loc = ih_location(&ih[cpy_num - 1]);
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci	/* check free space */
37562306a36Sopenharmony_ci	RFALSE(free_space < j - last_inserted_loc,
37662306a36Sopenharmony_ci	       "vs-10150: not enough free space for items %d (needed %d)",
37762306a36Sopenharmony_ci	       free_space, j - last_inserted_loc);
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci	memmove(dest->b_data + last_loc,
38062306a36Sopenharmony_ci		dest->b_data + last_loc + j - last_inserted_loc,
38162306a36Sopenharmony_ci		last_inserted_loc - last_loc);
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ci	/* copy items */
38462306a36Sopenharmony_ci	memcpy(dest->b_data + last_inserted_loc,
38562306a36Sopenharmony_ci	       item_body(src, (first + cpy_num - 1)),
38662306a36Sopenharmony_ci	       j - last_inserted_loc);
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci	/* sizes, item number */
38962306a36Sopenharmony_ci	set_blkh_nr_item(blkh, nr + cpy_num);
39062306a36Sopenharmony_ci	set_blkh_free_space(blkh, free_space - (j - last_inserted_loc));
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci	do_balance_mark_leaf_dirty(dest_bi->tb, dest, 0);
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	if (dest_bi->bi_parent) {
39562306a36Sopenharmony_ci		struct disk_child *t_dc;
39662306a36Sopenharmony_ci		t_dc = B_N_CHILD(dest_bi->bi_parent, dest_bi->bi_position);
39762306a36Sopenharmony_ci		RFALSE(dc_block_number(t_dc) != dest->b_blocknr,
39862306a36Sopenharmony_ci		       "vs-10160: block number in bh does not match to field in disk_child structure %lu and %lu",
39962306a36Sopenharmony_ci		       (long unsigned)dest->b_blocknr,
40062306a36Sopenharmony_ci		       (long unsigned)dc_block_number(t_dc));
40162306a36Sopenharmony_ci		put_dc_size(t_dc,
40262306a36Sopenharmony_ci			    dc_size(t_dc) + (j - last_inserted_loc +
40362306a36Sopenharmony_ci					     IH_SIZE * cpy_num));
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci		do_balance_mark_internal_dirty(dest_bi->tb, dest_bi->bi_parent,
40662306a36Sopenharmony_ci					       0);
40762306a36Sopenharmony_ci	}
40862306a36Sopenharmony_ci}
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci/*
41162306a36Sopenharmony_ci * This function splits the (liquid) item into two items (useful when
41262306a36Sopenharmony_ci * shifting part of an item into another node.)
41362306a36Sopenharmony_ci */
41462306a36Sopenharmony_cistatic void leaf_item_bottle(struct buffer_info *dest_bi,
41562306a36Sopenharmony_ci			     struct buffer_head *src, int last_first,
41662306a36Sopenharmony_ci			     int item_num, int cpy_bytes)
41762306a36Sopenharmony_ci{
41862306a36Sopenharmony_ci	struct buffer_head *dest = dest_bi->bi_bh;
41962306a36Sopenharmony_ci	struct item_head *ih;
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci	RFALSE(cpy_bytes == -1,
42262306a36Sopenharmony_ci	       "vs-10170: bytes == - 1 means: do not split item");
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ci	if (last_first == FIRST_TO_LAST) {
42562306a36Sopenharmony_ci		/*
42662306a36Sopenharmony_ci		 * if ( if item in position item_num in buffer SOURCE
42762306a36Sopenharmony_ci		 * is directory item )
42862306a36Sopenharmony_ci		 */
42962306a36Sopenharmony_ci		ih = item_head(src, item_num);
43062306a36Sopenharmony_ci		if (is_direntry_le_ih(ih))
43162306a36Sopenharmony_ci			leaf_copy_dir_entries(dest_bi, src, FIRST_TO_LAST,
43262306a36Sopenharmony_ci					      item_num, 0, cpy_bytes);
43362306a36Sopenharmony_ci		else {
43462306a36Sopenharmony_ci			struct item_head n_ih;
43562306a36Sopenharmony_ci
43662306a36Sopenharmony_ci			/*
43762306a36Sopenharmony_ci			 * copy part of the body of the item number 'item_num'
43862306a36Sopenharmony_ci			 * of SOURCE to the end of the DEST part defined by
43962306a36Sopenharmony_ci			 * 'cpy_bytes'; create new item header; change old
44062306a36Sopenharmony_ci			 * item_header (????); n_ih = new item_header;
44162306a36Sopenharmony_ci			 */
44262306a36Sopenharmony_ci			memcpy(&n_ih, ih, IH_SIZE);
44362306a36Sopenharmony_ci			put_ih_item_len(&n_ih, cpy_bytes);
44462306a36Sopenharmony_ci			if (is_indirect_le_ih(ih)) {
44562306a36Sopenharmony_ci				RFALSE(cpy_bytes == ih_item_len(ih)
44662306a36Sopenharmony_ci				       && get_ih_free_space(ih),
44762306a36Sopenharmony_ci				       "vs-10180: when whole indirect item is bottle to left neighbor, it must have free_space==0 (not %lu)",
44862306a36Sopenharmony_ci				       (long unsigned)get_ih_free_space(ih));
44962306a36Sopenharmony_ci				set_ih_free_space(&n_ih, 0);
45062306a36Sopenharmony_ci			}
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ci			RFALSE(op_is_left_mergeable(&ih->ih_key, src->b_size),
45362306a36Sopenharmony_ci			       "vs-10190: bad mergeability of item %h", ih);
45462306a36Sopenharmony_ci			n_ih.ih_version = ih->ih_version;	/* JDM Endian safe, both le */
45562306a36Sopenharmony_ci			leaf_insert_into_buf(dest_bi, B_NR_ITEMS(dest), &n_ih,
45662306a36Sopenharmony_ci					     item_body(src, item_num), 0);
45762306a36Sopenharmony_ci		}
45862306a36Sopenharmony_ci	} else {
45962306a36Sopenharmony_ci		/*
46062306a36Sopenharmony_ci		 * if ( if item in position item_num in buffer
46162306a36Sopenharmony_ci		 * SOURCE is directory item )
46262306a36Sopenharmony_ci		 */
46362306a36Sopenharmony_ci		ih = item_head(src, item_num);
46462306a36Sopenharmony_ci		if (is_direntry_le_ih(ih))
46562306a36Sopenharmony_ci			leaf_copy_dir_entries(dest_bi, src, LAST_TO_FIRST,
46662306a36Sopenharmony_ci					      item_num,
46762306a36Sopenharmony_ci					      ih_entry_count(ih) - cpy_bytes,
46862306a36Sopenharmony_ci					      cpy_bytes);
46962306a36Sopenharmony_ci		else {
47062306a36Sopenharmony_ci			struct item_head n_ih;
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_ci			/*
47362306a36Sopenharmony_ci			 * copy part of the body of the item number 'item_num'
47462306a36Sopenharmony_ci			 * of SOURCE to the begin of the DEST part defined by
47562306a36Sopenharmony_ci			 * 'cpy_bytes'; create new item header;
47662306a36Sopenharmony_ci			 * n_ih = new item_header;
47762306a36Sopenharmony_ci			 */
47862306a36Sopenharmony_ci			memcpy(&n_ih.ih_key, &ih->ih_key, KEY_SIZE);
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci			/* Endian safe, both le */
48162306a36Sopenharmony_ci			n_ih.ih_version = ih->ih_version;
48262306a36Sopenharmony_ci
48362306a36Sopenharmony_ci			if (is_direct_le_ih(ih)) {
48462306a36Sopenharmony_ci				set_le_ih_k_offset(&n_ih,
48562306a36Sopenharmony_ci						   le_ih_k_offset(ih) +
48662306a36Sopenharmony_ci						   ih_item_len(ih) - cpy_bytes);
48762306a36Sopenharmony_ci				set_le_ih_k_type(&n_ih, TYPE_DIRECT);
48862306a36Sopenharmony_ci				set_ih_free_space(&n_ih, MAX_US_INT);
48962306a36Sopenharmony_ci			} else {
49062306a36Sopenharmony_ci				/* indirect item */
49162306a36Sopenharmony_ci				RFALSE(!cpy_bytes && get_ih_free_space(ih),
49262306a36Sopenharmony_ci				       "vs-10200: ih->ih_free_space must be 0 when indirect item will be appended");
49362306a36Sopenharmony_ci				set_le_ih_k_offset(&n_ih,
49462306a36Sopenharmony_ci						   le_ih_k_offset(ih) +
49562306a36Sopenharmony_ci						   (ih_item_len(ih) -
49662306a36Sopenharmony_ci						    cpy_bytes) / UNFM_P_SIZE *
49762306a36Sopenharmony_ci						   dest->b_size);
49862306a36Sopenharmony_ci				set_le_ih_k_type(&n_ih, TYPE_INDIRECT);
49962306a36Sopenharmony_ci				set_ih_free_space(&n_ih, get_ih_free_space(ih));
50062306a36Sopenharmony_ci			}
50162306a36Sopenharmony_ci
50262306a36Sopenharmony_ci			/* set item length */
50362306a36Sopenharmony_ci			put_ih_item_len(&n_ih, cpy_bytes);
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_ci			/* Endian safe, both le */
50662306a36Sopenharmony_ci			n_ih.ih_version = ih->ih_version;
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci			leaf_insert_into_buf(dest_bi, 0, &n_ih,
50962306a36Sopenharmony_ci					     item_body(src, item_num) +
51062306a36Sopenharmony_ci						ih_item_len(ih) - cpy_bytes, 0);
51162306a36Sopenharmony_ci		}
51262306a36Sopenharmony_ci	}
51362306a36Sopenharmony_ci}
51462306a36Sopenharmony_ci
51562306a36Sopenharmony_ci/*
51662306a36Sopenharmony_ci * If cpy_bytes equals minus one than copy cpy_num whole items from SOURCE
51762306a36Sopenharmony_ci * to DEST.  If cpy_bytes not equal to minus one than copy cpy_num-1 whole
51862306a36Sopenharmony_ci * items from SOURCE to DEST.  From last item copy cpy_num bytes for regular
51962306a36Sopenharmony_ci * item and cpy_num directory entries for directory item.
52062306a36Sopenharmony_ci */
52162306a36Sopenharmony_cistatic int leaf_copy_items(struct buffer_info *dest_bi, struct buffer_head *src,
52262306a36Sopenharmony_ci			   int last_first, int cpy_num, int cpy_bytes)
52362306a36Sopenharmony_ci{
52462306a36Sopenharmony_ci	struct buffer_head *dest;
52562306a36Sopenharmony_ci	int pos, i, src_nr_item, bytes;
52662306a36Sopenharmony_ci
52762306a36Sopenharmony_ci	dest = dest_bi->bi_bh;
52862306a36Sopenharmony_ci	RFALSE(!dest || !src, "vs-10210: !dest || !src");
52962306a36Sopenharmony_ci	RFALSE(last_first != FIRST_TO_LAST && last_first != LAST_TO_FIRST,
53062306a36Sopenharmony_ci	       "vs-10220:last_first != FIRST_TO_LAST && last_first != LAST_TO_FIRST");
53162306a36Sopenharmony_ci	RFALSE(B_NR_ITEMS(src) < cpy_num,
53262306a36Sopenharmony_ci	       "vs-10230: No enough items: %d, req. %d", B_NR_ITEMS(src),
53362306a36Sopenharmony_ci	       cpy_num);
53462306a36Sopenharmony_ci	RFALSE(cpy_num < 0, "vs-10240: cpy_num < 0 (%d)", cpy_num);
53562306a36Sopenharmony_ci
53662306a36Sopenharmony_ci	if (cpy_num == 0)
53762306a36Sopenharmony_ci		return 0;
53862306a36Sopenharmony_ci
53962306a36Sopenharmony_ci	if (last_first == FIRST_TO_LAST) {
54062306a36Sopenharmony_ci		/* copy items to left */
54162306a36Sopenharmony_ci		pos = 0;
54262306a36Sopenharmony_ci		if (cpy_num == 1)
54362306a36Sopenharmony_ci			bytes = cpy_bytes;
54462306a36Sopenharmony_ci		else
54562306a36Sopenharmony_ci			bytes = -1;
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci		/*
54862306a36Sopenharmony_ci		 * copy the first item or it part or nothing to the end of
54962306a36Sopenharmony_ci		 * the DEST (i = leaf_copy_boundary_item(DEST,SOURCE,0,bytes))
55062306a36Sopenharmony_ci		 */
55162306a36Sopenharmony_ci		i = leaf_copy_boundary_item(dest_bi, src, FIRST_TO_LAST, bytes);
55262306a36Sopenharmony_ci		cpy_num -= i;
55362306a36Sopenharmony_ci		if (cpy_num == 0)
55462306a36Sopenharmony_ci			return i;
55562306a36Sopenharmony_ci		pos += i;
55662306a36Sopenharmony_ci		if (cpy_bytes == -1)
55762306a36Sopenharmony_ci			/*
55862306a36Sopenharmony_ci			 * copy first cpy_num items starting from position
55962306a36Sopenharmony_ci			 * 'pos' of SOURCE to end of DEST
56062306a36Sopenharmony_ci			 */
56162306a36Sopenharmony_ci			leaf_copy_items_entirely(dest_bi, src, FIRST_TO_LAST,
56262306a36Sopenharmony_ci						 pos, cpy_num);
56362306a36Sopenharmony_ci		else {
56462306a36Sopenharmony_ci			/*
56562306a36Sopenharmony_ci			 * copy first cpy_num-1 items starting from position
56662306a36Sopenharmony_ci			 * 'pos-1' of the SOURCE to the end of the DEST
56762306a36Sopenharmony_ci			 */
56862306a36Sopenharmony_ci			leaf_copy_items_entirely(dest_bi, src, FIRST_TO_LAST,
56962306a36Sopenharmony_ci						 pos, cpy_num - 1);
57062306a36Sopenharmony_ci
57162306a36Sopenharmony_ci			/*
57262306a36Sopenharmony_ci			 * copy part of the item which number is
57362306a36Sopenharmony_ci			 * cpy_num+pos-1 to the end of the DEST
57462306a36Sopenharmony_ci			 */
57562306a36Sopenharmony_ci			leaf_item_bottle(dest_bi, src, FIRST_TO_LAST,
57662306a36Sopenharmony_ci					 cpy_num + pos - 1, cpy_bytes);
57762306a36Sopenharmony_ci		}
57862306a36Sopenharmony_ci	} else {
57962306a36Sopenharmony_ci		/* copy items to right */
58062306a36Sopenharmony_ci		src_nr_item = B_NR_ITEMS(src);
58162306a36Sopenharmony_ci		if (cpy_num == 1)
58262306a36Sopenharmony_ci			bytes = cpy_bytes;
58362306a36Sopenharmony_ci		else
58462306a36Sopenharmony_ci			bytes = -1;
58562306a36Sopenharmony_ci
58662306a36Sopenharmony_ci		/*
58762306a36Sopenharmony_ci		 * copy the last item or it part or nothing to the
58862306a36Sopenharmony_ci		 * begin of the DEST
58962306a36Sopenharmony_ci		 * (i = leaf_copy_boundary_item(DEST,SOURCE,1,bytes));
59062306a36Sopenharmony_ci		 */
59162306a36Sopenharmony_ci		i = leaf_copy_boundary_item(dest_bi, src, LAST_TO_FIRST, bytes);
59262306a36Sopenharmony_ci
59362306a36Sopenharmony_ci		cpy_num -= i;
59462306a36Sopenharmony_ci		if (cpy_num == 0)
59562306a36Sopenharmony_ci			return i;
59662306a36Sopenharmony_ci
59762306a36Sopenharmony_ci		pos = src_nr_item - cpy_num - i;
59862306a36Sopenharmony_ci		if (cpy_bytes == -1) {
59962306a36Sopenharmony_ci			/*
60062306a36Sopenharmony_ci			 * starting from position 'pos' copy last cpy_num
60162306a36Sopenharmony_ci			 * items of SOURCE to begin of DEST
60262306a36Sopenharmony_ci			 */
60362306a36Sopenharmony_ci			leaf_copy_items_entirely(dest_bi, src, LAST_TO_FIRST,
60462306a36Sopenharmony_ci						 pos, cpy_num);
60562306a36Sopenharmony_ci		} else {
60662306a36Sopenharmony_ci			/*
60762306a36Sopenharmony_ci			 * copy last cpy_num-1 items starting from position
60862306a36Sopenharmony_ci			 * 'pos+1' of the SOURCE to the begin of the DEST;
60962306a36Sopenharmony_ci			 */
61062306a36Sopenharmony_ci			leaf_copy_items_entirely(dest_bi, src, LAST_TO_FIRST,
61162306a36Sopenharmony_ci						 pos + 1, cpy_num - 1);
61262306a36Sopenharmony_ci
61362306a36Sopenharmony_ci			/*
61462306a36Sopenharmony_ci			 * copy part of the item which number is pos to
61562306a36Sopenharmony_ci			 * the begin of the DEST
61662306a36Sopenharmony_ci			 */
61762306a36Sopenharmony_ci			leaf_item_bottle(dest_bi, src, LAST_TO_FIRST, pos,
61862306a36Sopenharmony_ci					 cpy_bytes);
61962306a36Sopenharmony_ci		}
62062306a36Sopenharmony_ci	}
62162306a36Sopenharmony_ci	return i;
62262306a36Sopenharmony_ci}
62362306a36Sopenharmony_ci
62462306a36Sopenharmony_ci/*
62562306a36Sopenharmony_ci * there are types of coping: from S[0] to L[0], from S[0] to R[0],
62662306a36Sopenharmony_ci * from R[0] to L[0]. for each of these we have to define parent and
62762306a36Sopenharmony_ci * positions of destination and source buffers
62862306a36Sopenharmony_ci */
62962306a36Sopenharmony_cistatic void leaf_define_dest_src_infos(int shift_mode, struct tree_balance *tb,
63062306a36Sopenharmony_ci				       struct buffer_info *dest_bi,
63162306a36Sopenharmony_ci				       struct buffer_info *src_bi,
63262306a36Sopenharmony_ci				       int *first_last,
63362306a36Sopenharmony_ci				       struct buffer_head *Snew)
63462306a36Sopenharmony_ci{
63562306a36Sopenharmony_ci	memset(dest_bi, 0, sizeof(struct buffer_info));
63662306a36Sopenharmony_ci	memset(src_bi, 0, sizeof(struct buffer_info));
63762306a36Sopenharmony_ci
63862306a36Sopenharmony_ci	/* define dest, src, dest parent, dest position */
63962306a36Sopenharmony_ci	switch (shift_mode) {
64062306a36Sopenharmony_ci	case LEAF_FROM_S_TO_L:	/* it is used in leaf_shift_left */
64162306a36Sopenharmony_ci		src_bi->tb = tb;
64262306a36Sopenharmony_ci		src_bi->bi_bh = PATH_PLAST_BUFFER(tb->tb_path);
64362306a36Sopenharmony_ci		src_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, 0);
64462306a36Sopenharmony_ci
64562306a36Sopenharmony_ci		/* src->b_item_order */
64662306a36Sopenharmony_ci		src_bi->bi_position = PATH_H_B_ITEM_ORDER(tb->tb_path, 0);
64762306a36Sopenharmony_ci		dest_bi->tb = tb;
64862306a36Sopenharmony_ci		dest_bi->bi_bh = tb->L[0];
64962306a36Sopenharmony_ci		dest_bi->bi_parent = tb->FL[0];
65062306a36Sopenharmony_ci		dest_bi->bi_position = get_left_neighbor_position(tb, 0);
65162306a36Sopenharmony_ci		*first_last = FIRST_TO_LAST;
65262306a36Sopenharmony_ci		break;
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci	case LEAF_FROM_S_TO_R:	/* it is used in leaf_shift_right */
65562306a36Sopenharmony_ci		src_bi->tb = tb;
65662306a36Sopenharmony_ci		src_bi->bi_bh = PATH_PLAST_BUFFER(tb->tb_path);
65762306a36Sopenharmony_ci		src_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, 0);
65862306a36Sopenharmony_ci		src_bi->bi_position = PATH_H_B_ITEM_ORDER(tb->tb_path, 0);
65962306a36Sopenharmony_ci		dest_bi->tb = tb;
66062306a36Sopenharmony_ci		dest_bi->bi_bh = tb->R[0];
66162306a36Sopenharmony_ci		dest_bi->bi_parent = tb->FR[0];
66262306a36Sopenharmony_ci		dest_bi->bi_position = get_right_neighbor_position(tb, 0);
66362306a36Sopenharmony_ci		*first_last = LAST_TO_FIRST;
66462306a36Sopenharmony_ci		break;
66562306a36Sopenharmony_ci
66662306a36Sopenharmony_ci	case LEAF_FROM_R_TO_L:	/* it is used in balance_leaf_when_delete */
66762306a36Sopenharmony_ci		src_bi->tb = tb;
66862306a36Sopenharmony_ci		src_bi->bi_bh = tb->R[0];
66962306a36Sopenharmony_ci		src_bi->bi_parent = tb->FR[0];
67062306a36Sopenharmony_ci		src_bi->bi_position = get_right_neighbor_position(tb, 0);
67162306a36Sopenharmony_ci		dest_bi->tb = tb;
67262306a36Sopenharmony_ci		dest_bi->bi_bh = tb->L[0];
67362306a36Sopenharmony_ci		dest_bi->bi_parent = tb->FL[0];
67462306a36Sopenharmony_ci		dest_bi->bi_position = get_left_neighbor_position(tb, 0);
67562306a36Sopenharmony_ci		*first_last = FIRST_TO_LAST;
67662306a36Sopenharmony_ci		break;
67762306a36Sopenharmony_ci
67862306a36Sopenharmony_ci	case LEAF_FROM_L_TO_R:	/* it is used in balance_leaf_when_delete */
67962306a36Sopenharmony_ci		src_bi->tb = tb;
68062306a36Sopenharmony_ci		src_bi->bi_bh = tb->L[0];
68162306a36Sopenharmony_ci		src_bi->bi_parent = tb->FL[0];
68262306a36Sopenharmony_ci		src_bi->bi_position = get_left_neighbor_position(tb, 0);
68362306a36Sopenharmony_ci		dest_bi->tb = tb;
68462306a36Sopenharmony_ci		dest_bi->bi_bh = tb->R[0];
68562306a36Sopenharmony_ci		dest_bi->bi_parent = tb->FR[0];
68662306a36Sopenharmony_ci		dest_bi->bi_position = get_right_neighbor_position(tb, 0);
68762306a36Sopenharmony_ci		*first_last = LAST_TO_FIRST;
68862306a36Sopenharmony_ci		break;
68962306a36Sopenharmony_ci
69062306a36Sopenharmony_ci	case LEAF_FROM_S_TO_SNEW:
69162306a36Sopenharmony_ci		src_bi->tb = tb;
69262306a36Sopenharmony_ci		src_bi->bi_bh = PATH_PLAST_BUFFER(tb->tb_path);
69362306a36Sopenharmony_ci		src_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, 0);
69462306a36Sopenharmony_ci		src_bi->bi_position = PATH_H_B_ITEM_ORDER(tb->tb_path, 0);
69562306a36Sopenharmony_ci		dest_bi->tb = tb;
69662306a36Sopenharmony_ci		dest_bi->bi_bh = Snew;
69762306a36Sopenharmony_ci		dest_bi->bi_parent = NULL;
69862306a36Sopenharmony_ci		dest_bi->bi_position = 0;
69962306a36Sopenharmony_ci		*first_last = LAST_TO_FIRST;
70062306a36Sopenharmony_ci		break;
70162306a36Sopenharmony_ci
70262306a36Sopenharmony_ci	default:
70362306a36Sopenharmony_ci		reiserfs_panic(sb_from_bi(src_bi), "vs-10250",
70462306a36Sopenharmony_ci			       "shift type is unknown (%d)", shift_mode);
70562306a36Sopenharmony_ci	}
70662306a36Sopenharmony_ci	RFALSE(!src_bi->bi_bh || !dest_bi->bi_bh,
70762306a36Sopenharmony_ci	       "vs-10260: mode==%d, source (%p) or dest (%p) buffer is initialized incorrectly",
70862306a36Sopenharmony_ci	       shift_mode, src_bi->bi_bh, dest_bi->bi_bh);
70962306a36Sopenharmony_ci}
71062306a36Sopenharmony_ci
71162306a36Sopenharmony_ci/*
71262306a36Sopenharmony_ci * copy mov_num items and mov_bytes of the (mov_num-1)th item to
71362306a36Sopenharmony_ci * neighbor. Delete them from source
71462306a36Sopenharmony_ci */
71562306a36Sopenharmony_ciint leaf_move_items(int shift_mode, struct tree_balance *tb, int mov_num,
71662306a36Sopenharmony_ci		    int mov_bytes, struct buffer_head *Snew)
71762306a36Sopenharmony_ci{
71862306a36Sopenharmony_ci	int ret_value;
71962306a36Sopenharmony_ci	struct buffer_info dest_bi, src_bi;
72062306a36Sopenharmony_ci	int first_last;
72162306a36Sopenharmony_ci
72262306a36Sopenharmony_ci	leaf_define_dest_src_infos(shift_mode, tb, &dest_bi, &src_bi,
72362306a36Sopenharmony_ci				   &first_last, Snew);
72462306a36Sopenharmony_ci
72562306a36Sopenharmony_ci	ret_value =
72662306a36Sopenharmony_ci	    leaf_copy_items(&dest_bi, src_bi.bi_bh, first_last, mov_num,
72762306a36Sopenharmony_ci			    mov_bytes);
72862306a36Sopenharmony_ci
72962306a36Sopenharmony_ci	leaf_delete_items(&src_bi, first_last,
73062306a36Sopenharmony_ci			  (first_last ==
73162306a36Sopenharmony_ci			   FIRST_TO_LAST) ? 0 : (B_NR_ITEMS(src_bi.bi_bh) -
73262306a36Sopenharmony_ci						 mov_num), mov_num, mov_bytes);
73362306a36Sopenharmony_ci
73462306a36Sopenharmony_ci	return ret_value;
73562306a36Sopenharmony_ci}
73662306a36Sopenharmony_ci
73762306a36Sopenharmony_ci/*
73862306a36Sopenharmony_ci * Shift shift_num items (and shift_bytes of last shifted item if
73962306a36Sopenharmony_ci * shift_bytes != -1) from S[0] to L[0] and replace the delimiting key
74062306a36Sopenharmony_ci */
74162306a36Sopenharmony_ciint leaf_shift_left(struct tree_balance *tb, int shift_num, int shift_bytes)
74262306a36Sopenharmony_ci{
74362306a36Sopenharmony_ci	struct buffer_head *S0 = PATH_PLAST_BUFFER(tb->tb_path);
74462306a36Sopenharmony_ci	int i;
74562306a36Sopenharmony_ci
74662306a36Sopenharmony_ci	/*
74762306a36Sopenharmony_ci	 * move shift_num (and shift_bytes bytes) items from S[0]
74862306a36Sopenharmony_ci	 * to left neighbor L[0]
74962306a36Sopenharmony_ci	 */
75062306a36Sopenharmony_ci	i = leaf_move_items(LEAF_FROM_S_TO_L, tb, shift_num, shift_bytes, NULL);
75162306a36Sopenharmony_ci
75262306a36Sopenharmony_ci	if (shift_num) {
75362306a36Sopenharmony_ci		/* number of items in S[0] == 0 */
75462306a36Sopenharmony_ci		if (B_NR_ITEMS(S0) == 0) {
75562306a36Sopenharmony_ci
75662306a36Sopenharmony_ci			RFALSE(shift_bytes != -1,
75762306a36Sopenharmony_ci			       "vs-10270: S0 is empty now, but shift_bytes != -1 (%d)",
75862306a36Sopenharmony_ci			       shift_bytes);
75962306a36Sopenharmony_ci#ifdef CONFIG_REISERFS_CHECK
76062306a36Sopenharmony_ci			if (tb->tb_mode == M_PASTE || tb->tb_mode == M_INSERT) {
76162306a36Sopenharmony_ci				print_cur_tb("vs-10275");
76262306a36Sopenharmony_ci				reiserfs_panic(tb->tb_sb, "vs-10275",
76362306a36Sopenharmony_ci					       "balance condition corrupted "
76462306a36Sopenharmony_ci					       "(%c)", tb->tb_mode);
76562306a36Sopenharmony_ci			}
76662306a36Sopenharmony_ci#endif
76762306a36Sopenharmony_ci
76862306a36Sopenharmony_ci			if (PATH_H_POSITION(tb->tb_path, 1) == 0)
76962306a36Sopenharmony_ci				replace_key(tb, tb->CFL[0], tb->lkey[0],
77062306a36Sopenharmony_ci					    PATH_H_PPARENT(tb->tb_path, 0), 0);
77162306a36Sopenharmony_ci
77262306a36Sopenharmony_ci		} else {
77362306a36Sopenharmony_ci			/* replace lkey in CFL[0] by 0-th key from S[0]; */
77462306a36Sopenharmony_ci			replace_key(tb, tb->CFL[0], tb->lkey[0], S0, 0);
77562306a36Sopenharmony_ci
77662306a36Sopenharmony_ci			RFALSE((shift_bytes != -1 &&
77762306a36Sopenharmony_ci				!(is_direntry_le_ih(item_head(S0, 0))
77862306a36Sopenharmony_ci				  && !ih_entry_count(item_head(S0, 0)))) &&
77962306a36Sopenharmony_ci			       (!op_is_left_mergeable
78062306a36Sopenharmony_ci				(leaf_key(S0, 0), S0->b_size)),
78162306a36Sopenharmony_ci			       "vs-10280: item must be mergeable");
78262306a36Sopenharmony_ci		}
78362306a36Sopenharmony_ci	}
78462306a36Sopenharmony_ci
78562306a36Sopenharmony_ci	return i;
78662306a36Sopenharmony_ci}
78762306a36Sopenharmony_ci
78862306a36Sopenharmony_ci/* CLEANING STOPPED HERE */
78962306a36Sopenharmony_ci
79062306a36Sopenharmony_ci/*
79162306a36Sopenharmony_ci * Shift shift_num (shift_bytes) items from S[0] to the right neighbor,
79262306a36Sopenharmony_ci * and replace the delimiting key
79362306a36Sopenharmony_ci */
79462306a36Sopenharmony_ciint leaf_shift_right(struct tree_balance *tb, int shift_num, int shift_bytes)
79562306a36Sopenharmony_ci{
79662306a36Sopenharmony_ci	int ret_value;
79762306a36Sopenharmony_ci
79862306a36Sopenharmony_ci	/*
79962306a36Sopenharmony_ci	 * move shift_num (and shift_bytes) items from S[0] to
80062306a36Sopenharmony_ci	 * right neighbor R[0]
80162306a36Sopenharmony_ci	 */
80262306a36Sopenharmony_ci	ret_value =
80362306a36Sopenharmony_ci	    leaf_move_items(LEAF_FROM_S_TO_R, tb, shift_num, shift_bytes, NULL);
80462306a36Sopenharmony_ci
80562306a36Sopenharmony_ci	/* replace rkey in CFR[0] by the 0-th key from R[0] */
80662306a36Sopenharmony_ci	if (shift_num) {
80762306a36Sopenharmony_ci		replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
80862306a36Sopenharmony_ci
80962306a36Sopenharmony_ci	}
81062306a36Sopenharmony_ci
81162306a36Sopenharmony_ci	return ret_value;
81262306a36Sopenharmony_ci}
81362306a36Sopenharmony_ci
81462306a36Sopenharmony_cistatic void leaf_delete_items_entirely(struct buffer_info *bi,
81562306a36Sopenharmony_ci				       int first, int del_num);
81662306a36Sopenharmony_ci/*
81762306a36Sopenharmony_ci * If del_bytes == -1, starting from position 'first' delete del_num
81862306a36Sopenharmony_ci * items in whole in buffer CUR.
81962306a36Sopenharmony_ci *   If not.
82062306a36Sopenharmony_ci *   If last_first == 0. Starting from position 'first' delete del_num-1
82162306a36Sopenharmony_ci *   items in whole. Delete part of body of the first item. Part defined by
82262306a36Sopenharmony_ci *   del_bytes. Don't delete first item header
82362306a36Sopenharmony_ci *   If last_first == 1. Starting from position 'first+1' delete del_num-1
82462306a36Sopenharmony_ci *   items in whole. Delete part of body of the last item . Part defined by
82562306a36Sopenharmony_ci *   del_bytes. Don't delete last item header.
82662306a36Sopenharmony_ci*/
82762306a36Sopenharmony_civoid leaf_delete_items(struct buffer_info *cur_bi, int last_first,
82862306a36Sopenharmony_ci		       int first, int del_num, int del_bytes)
82962306a36Sopenharmony_ci{
83062306a36Sopenharmony_ci	struct buffer_head *bh;
83162306a36Sopenharmony_ci	int item_amount = B_NR_ITEMS(bh = cur_bi->bi_bh);
83262306a36Sopenharmony_ci
83362306a36Sopenharmony_ci	RFALSE(!bh, "10155: bh is not defined");
83462306a36Sopenharmony_ci	RFALSE(del_num < 0, "10160: del_num can not be < 0. del_num==%d",
83562306a36Sopenharmony_ci	       del_num);
83662306a36Sopenharmony_ci	RFALSE(first < 0
83762306a36Sopenharmony_ci	       || first + del_num > item_amount,
83862306a36Sopenharmony_ci	       "10165: invalid number of first item to be deleted (%d) or "
83962306a36Sopenharmony_ci	       "no so much items (%d) to delete (only %d)", first,
84062306a36Sopenharmony_ci	       first + del_num, item_amount);
84162306a36Sopenharmony_ci
84262306a36Sopenharmony_ci	if (del_num == 0)
84362306a36Sopenharmony_ci		return;
84462306a36Sopenharmony_ci
84562306a36Sopenharmony_ci	if (first == 0 && del_num == item_amount && del_bytes == -1) {
84662306a36Sopenharmony_ci		make_empty_node(cur_bi);
84762306a36Sopenharmony_ci		do_balance_mark_leaf_dirty(cur_bi->tb, bh, 0);
84862306a36Sopenharmony_ci		return;
84962306a36Sopenharmony_ci	}
85062306a36Sopenharmony_ci
85162306a36Sopenharmony_ci	if (del_bytes == -1)
85262306a36Sopenharmony_ci		/* delete del_num items beginning from item in position first */
85362306a36Sopenharmony_ci		leaf_delete_items_entirely(cur_bi, first, del_num);
85462306a36Sopenharmony_ci	else {
85562306a36Sopenharmony_ci		if (last_first == FIRST_TO_LAST) {
85662306a36Sopenharmony_ci			/*
85762306a36Sopenharmony_ci			 * delete del_num-1 items beginning from
85862306a36Sopenharmony_ci			 * item in position first
85962306a36Sopenharmony_ci			 */
86062306a36Sopenharmony_ci			leaf_delete_items_entirely(cur_bi, first, del_num - 1);
86162306a36Sopenharmony_ci
86262306a36Sopenharmony_ci			/*
86362306a36Sopenharmony_ci			 * delete the part of the first item of the bh
86462306a36Sopenharmony_ci			 * do not delete item header
86562306a36Sopenharmony_ci			 */
86662306a36Sopenharmony_ci			leaf_cut_from_buffer(cur_bi, 0, 0, del_bytes);
86762306a36Sopenharmony_ci		} else {
86862306a36Sopenharmony_ci			struct item_head *ih;
86962306a36Sopenharmony_ci			int len;
87062306a36Sopenharmony_ci
87162306a36Sopenharmony_ci			/*
87262306a36Sopenharmony_ci			 * delete del_num-1 items beginning from
87362306a36Sopenharmony_ci			 * item in position first+1
87462306a36Sopenharmony_ci			 */
87562306a36Sopenharmony_ci			leaf_delete_items_entirely(cur_bi, first + 1,
87662306a36Sopenharmony_ci						   del_num - 1);
87762306a36Sopenharmony_ci
87862306a36Sopenharmony_ci			ih = item_head(bh, B_NR_ITEMS(bh) - 1);
87962306a36Sopenharmony_ci			if (is_direntry_le_ih(ih))
88062306a36Sopenharmony_ci				/* the last item is directory  */
88162306a36Sopenharmony_ci				/*
88262306a36Sopenharmony_ci				 * len = numbers of directory entries
88362306a36Sopenharmony_ci				 * in this item
88462306a36Sopenharmony_ci				 */
88562306a36Sopenharmony_ci				len = ih_entry_count(ih);
88662306a36Sopenharmony_ci			else
88762306a36Sopenharmony_ci				/* len = body len of item */
88862306a36Sopenharmony_ci				len = ih_item_len(ih);
88962306a36Sopenharmony_ci
89062306a36Sopenharmony_ci			/*
89162306a36Sopenharmony_ci			 * delete the part of the last item of the bh
89262306a36Sopenharmony_ci			 * do not delete item header
89362306a36Sopenharmony_ci			 */
89462306a36Sopenharmony_ci			leaf_cut_from_buffer(cur_bi, B_NR_ITEMS(bh) - 1,
89562306a36Sopenharmony_ci					     len - del_bytes, del_bytes);
89662306a36Sopenharmony_ci		}
89762306a36Sopenharmony_ci	}
89862306a36Sopenharmony_ci}
89962306a36Sopenharmony_ci
90062306a36Sopenharmony_ci/* insert item into the leaf node in position before */
90162306a36Sopenharmony_civoid leaf_insert_into_buf(struct buffer_info *bi, int before,
90262306a36Sopenharmony_ci			  struct item_head * const inserted_item_ih,
90362306a36Sopenharmony_ci			  const char * const inserted_item_body,
90462306a36Sopenharmony_ci			  int zeros_number)
90562306a36Sopenharmony_ci{
90662306a36Sopenharmony_ci	struct buffer_head *bh = bi->bi_bh;
90762306a36Sopenharmony_ci	int nr, free_space;
90862306a36Sopenharmony_ci	struct block_head *blkh;
90962306a36Sopenharmony_ci	struct item_head *ih;
91062306a36Sopenharmony_ci	int i;
91162306a36Sopenharmony_ci	int last_loc, unmoved_loc;
91262306a36Sopenharmony_ci	char *to;
91362306a36Sopenharmony_ci
91462306a36Sopenharmony_ci	blkh = B_BLK_HEAD(bh);
91562306a36Sopenharmony_ci	nr = blkh_nr_item(blkh);
91662306a36Sopenharmony_ci	free_space = blkh_free_space(blkh);
91762306a36Sopenharmony_ci
91862306a36Sopenharmony_ci	/* check free space */
91962306a36Sopenharmony_ci	RFALSE(free_space < ih_item_len(inserted_item_ih) + IH_SIZE,
92062306a36Sopenharmony_ci	       "vs-10170: not enough free space in block %z, new item %h",
92162306a36Sopenharmony_ci	       bh, inserted_item_ih);
92262306a36Sopenharmony_ci	RFALSE(zeros_number > ih_item_len(inserted_item_ih),
92362306a36Sopenharmony_ci	       "vs-10172: zero number == %d, item length == %d",
92462306a36Sopenharmony_ci	       zeros_number, ih_item_len(inserted_item_ih));
92562306a36Sopenharmony_ci
92662306a36Sopenharmony_ci	/* get item new item must be inserted before */
92762306a36Sopenharmony_ci	ih = item_head(bh, before);
92862306a36Sopenharmony_ci
92962306a36Sopenharmony_ci	/* prepare space for the body of new item */
93062306a36Sopenharmony_ci	last_loc = nr ? ih_location(&ih[nr - before - 1]) : bh->b_size;
93162306a36Sopenharmony_ci	unmoved_loc = before ? ih_location(ih - 1) : bh->b_size;
93262306a36Sopenharmony_ci
93362306a36Sopenharmony_ci	memmove(bh->b_data + last_loc - ih_item_len(inserted_item_ih),
93462306a36Sopenharmony_ci		bh->b_data + last_loc, unmoved_loc - last_loc);
93562306a36Sopenharmony_ci
93662306a36Sopenharmony_ci	to = bh->b_data + unmoved_loc - ih_item_len(inserted_item_ih);
93762306a36Sopenharmony_ci	memset(to, 0, zeros_number);
93862306a36Sopenharmony_ci	to += zeros_number;
93962306a36Sopenharmony_ci
94062306a36Sopenharmony_ci	/* copy body to prepared space */
94162306a36Sopenharmony_ci	if (inserted_item_body)
94262306a36Sopenharmony_ci		memmove(to, inserted_item_body,
94362306a36Sopenharmony_ci			ih_item_len(inserted_item_ih) - zeros_number);
94462306a36Sopenharmony_ci	else
94562306a36Sopenharmony_ci		memset(to, '\0', ih_item_len(inserted_item_ih) - zeros_number);
94662306a36Sopenharmony_ci
94762306a36Sopenharmony_ci	/* insert item header */
94862306a36Sopenharmony_ci	memmove(ih + 1, ih, IH_SIZE * (nr - before));
94962306a36Sopenharmony_ci	memmove(ih, inserted_item_ih, IH_SIZE);
95062306a36Sopenharmony_ci
95162306a36Sopenharmony_ci	/* change locations */
95262306a36Sopenharmony_ci	for (i = before; i < nr + 1; i++) {
95362306a36Sopenharmony_ci		unmoved_loc -= ih_item_len(&ih[i - before]);
95462306a36Sopenharmony_ci		put_ih_location(&ih[i - before], unmoved_loc);
95562306a36Sopenharmony_ci	}
95662306a36Sopenharmony_ci
95762306a36Sopenharmony_ci	/* sizes, free space, item number */
95862306a36Sopenharmony_ci	set_blkh_nr_item(blkh, blkh_nr_item(blkh) + 1);
95962306a36Sopenharmony_ci	set_blkh_free_space(blkh,
96062306a36Sopenharmony_ci			    free_space - (IH_SIZE +
96162306a36Sopenharmony_ci					  ih_item_len(inserted_item_ih)));
96262306a36Sopenharmony_ci	do_balance_mark_leaf_dirty(bi->tb, bh, 1);
96362306a36Sopenharmony_ci
96462306a36Sopenharmony_ci	if (bi->bi_parent) {
96562306a36Sopenharmony_ci		struct disk_child *t_dc;
96662306a36Sopenharmony_ci		t_dc = B_N_CHILD(bi->bi_parent, bi->bi_position);
96762306a36Sopenharmony_ci		put_dc_size(t_dc,
96862306a36Sopenharmony_ci			    dc_size(t_dc) + (IH_SIZE +
96962306a36Sopenharmony_ci					     ih_item_len(inserted_item_ih)));
97062306a36Sopenharmony_ci		do_balance_mark_internal_dirty(bi->tb, bi->bi_parent, 0);
97162306a36Sopenharmony_ci	}
97262306a36Sopenharmony_ci}
97362306a36Sopenharmony_ci
97462306a36Sopenharmony_ci/*
97562306a36Sopenharmony_ci * paste paste_size bytes to affected_item_num-th item.
97662306a36Sopenharmony_ci * When item is a directory, this only prepare space for new entries
97762306a36Sopenharmony_ci */
97862306a36Sopenharmony_civoid leaf_paste_in_buffer(struct buffer_info *bi, int affected_item_num,
97962306a36Sopenharmony_ci			  int pos_in_item, int paste_size,
98062306a36Sopenharmony_ci			  const char *body, int zeros_number)
98162306a36Sopenharmony_ci{
98262306a36Sopenharmony_ci	struct buffer_head *bh = bi->bi_bh;
98362306a36Sopenharmony_ci	int nr, free_space;
98462306a36Sopenharmony_ci	struct block_head *blkh;
98562306a36Sopenharmony_ci	struct item_head *ih;
98662306a36Sopenharmony_ci	int i;
98762306a36Sopenharmony_ci	int last_loc, unmoved_loc;
98862306a36Sopenharmony_ci
98962306a36Sopenharmony_ci	blkh = B_BLK_HEAD(bh);
99062306a36Sopenharmony_ci	nr = blkh_nr_item(blkh);
99162306a36Sopenharmony_ci	free_space = blkh_free_space(blkh);
99262306a36Sopenharmony_ci
99362306a36Sopenharmony_ci	/* check free space */
99462306a36Sopenharmony_ci	RFALSE(free_space < paste_size,
99562306a36Sopenharmony_ci	       "vs-10175: not enough free space: needed %d, available %d",
99662306a36Sopenharmony_ci	       paste_size, free_space);
99762306a36Sopenharmony_ci
99862306a36Sopenharmony_ci#ifdef CONFIG_REISERFS_CHECK
99962306a36Sopenharmony_ci	if (zeros_number > paste_size) {
100062306a36Sopenharmony_ci		struct super_block *sb = NULL;
100162306a36Sopenharmony_ci		if (bi && bi->tb)
100262306a36Sopenharmony_ci			sb = bi->tb->tb_sb;
100362306a36Sopenharmony_ci		print_cur_tb("10177");
100462306a36Sopenharmony_ci		reiserfs_panic(sb, "vs-10177",
100562306a36Sopenharmony_ci			       "zeros_number == %d, paste_size == %d",
100662306a36Sopenharmony_ci			       zeros_number, paste_size);
100762306a36Sopenharmony_ci	}
100862306a36Sopenharmony_ci#endif				/* CONFIG_REISERFS_CHECK */
100962306a36Sopenharmony_ci
101062306a36Sopenharmony_ci	/* item to be appended */
101162306a36Sopenharmony_ci	ih = item_head(bh, affected_item_num);
101262306a36Sopenharmony_ci
101362306a36Sopenharmony_ci	last_loc = ih_location(&ih[nr - affected_item_num - 1]);
101462306a36Sopenharmony_ci	unmoved_loc = affected_item_num ? ih_location(ih - 1) : bh->b_size;
101562306a36Sopenharmony_ci
101662306a36Sopenharmony_ci	/* prepare space */
101762306a36Sopenharmony_ci	memmove(bh->b_data + last_loc - paste_size, bh->b_data + last_loc,
101862306a36Sopenharmony_ci		unmoved_loc - last_loc);
101962306a36Sopenharmony_ci
102062306a36Sopenharmony_ci	/* change locations */
102162306a36Sopenharmony_ci	for (i = affected_item_num; i < nr; i++)
102262306a36Sopenharmony_ci		put_ih_location(&ih[i - affected_item_num],
102362306a36Sopenharmony_ci				ih_location(&ih[i - affected_item_num]) -
102462306a36Sopenharmony_ci				paste_size);
102562306a36Sopenharmony_ci
102662306a36Sopenharmony_ci	if (body) {
102762306a36Sopenharmony_ci		if (!is_direntry_le_ih(ih)) {
102862306a36Sopenharmony_ci			if (!pos_in_item) {
102962306a36Sopenharmony_ci				/* shift data to right */
103062306a36Sopenharmony_ci				memmove(bh->b_data + ih_location(ih) +
103162306a36Sopenharmony_ci					paste_size,
103262306a36Sopenharmony_ci					bh->b_data + ih_location(ih),
103362306a36Sopenharmony_ci					ih_item_len(ih));
103462306a36Sopenharmony_ci				/* paste data in the head of item */
103562306a36Sopenharmony_ci				memset(bh->b_data + ih_location(ih), 0,
103662306a36Sopenharmony_ci				       zeros_number);
103762306a36Sopenharmony_ci				memcpy(bh->b_data + ih_location(ih) +
103862306a36Sopenharmony_ci				       zeros_number, body,
103962306a36Sopenharmony_ci				       paste_size - zeros_number);
104062306a36Sopenharmony_ci			} else {
104162306a36Sopenharmony_ci				memset(bh->b_data + unmoved_loc - paste_size, 0,
104262306a36Sopenharmony_ci				       zeros_number);
104362306a36Sopenharmony_ci				memcpy(bh->b_data + unmoved_loc - paste_size +
104462306a36Sopenharmony_ci				       zeros_number, body,
104562306a36Sopenharmony_ci				       paste_size - zeros_number);
104662306a36Sopenharmony_ci			}
104762306a36Sopenharmony_ci		}
104862306a36Sopenharmony_ci	} else
104962306a36Sopenharmony_ci		memset(bh->b_data + unmoved_loc - paste_size, '\0', paste_size);
105062306a36Sopenharmony_ci
105162306a36Sopenharmony_ci	put_ih_item_len(ih, ih_item_len(ih) + paste_size);
105262306a36Sopenharmony_ci
105362306a36Sopenharmony_ci	/* change free space */
105462306a36Sopenharmony_ci	set_blkh_free_space(blkh, free_space - paste_size);
105562306a36Sopenharmony_ci
105662306a36Sopenharmony_ci	do_balance_mark_leaf_dirty(bi->tb, bh, 0);
105762306a36Sopenharmony_ci
105862306a36Sopenharmony_ci	if (bi->bi_parent) {
105962306a36Sopenharmony_ci		struct disk_child *t_dc =
106062306a36Sopenharmony_ci		    B_N_CHILD(bi->bi_parent, bi->bi_position);
106162306a36Sopenharmony_ci		put_dc_size(t_dc, dc_size(t_dc) + paste_size);
106262306a36Sopenharmony_ci		do_balance_mark_internal_dirty(bi->tb, bi->bi_parent, 0);
106362306a36Sopenharmony_ci	}
106462306a36Sopenharmony_ci}
106562306a36Sopenharmony_ci
106662306a36Sopenharmony_ci/*
106762306a36Sopenharmony_ci * cuts DEL_COUNT entries beginning from FROM-th entry. Directory item
106862306a36Sopenharmony_ci * does not have free space, so it moves DEHs and remaining records as
106962306a36Sopenharmony_ci * necessary. Return value is size of removed part of directory item
107062306a36Sopenharmony_ci * in bytes.
107162306a36Sopenharmony_ci */
107262306a36Sopenharmony_cistatic int leaf_cut_entries(struct buffer_head *bh,
107362306a36Sopenharmony_ci			    struct item_head *ih, int from, int del_count)
107462306a36Sopenharmony_ci{
107562306a36Sopenharmony_ci	char *item;
107662306a36Sopenharmony_ci	struct reiserfs_de_head *deh;
107762306a36Sopenharmony_ci	int prev_record_offset;	/* offset of record, that is (from-1)th */
107862306a36Sopenharmony_ci	char *prev_record;	/* */
107962306a36Sopenharmony_ci	int cut_records_len;	/* length of all removed records */
108062306a36Sopenharmony_ci	int i;
108162306a36Sopenharmony_ci
108262306a36Sopenharmony_ci	/*
108362306a36Sopenharmony_ci	 * make sure that item is directory and there are enough entries to
108462306a36Sopenharmony_ci	 * remove
108562306a36Sopenharmony_ci	 */
108662306a36Sopenharmony_ci	RFALSE(!is_direntry_le_ih(ih), "10180: item is not directory item");
108762306a36Sopenharmony_ci	RFALSE(ih_entry_count(ih) < from + del_count,
108862306a36Sopenharmony_ci	       "10185: item contains not enough entries: entry_count = %d, from = %d, to delete = %d",
108962306a36Sopenharmony_ci	       ih_entry_count(ih), from, del_count);
109062306a36Sopenharmony_ci
109162306a36Sopenharmony_ci	if (del_count == 0)
109262306a36Sopenharmony_ci		return 0;
109362306a36Sopenharmony_ci
109462306a36Sopenharmony_ci	/* first byte of item */
109562306a36Sopenharmony_ci	item = bh->b_data + ih_location(ih);
109662306a36Sopenharmony_ci
109762306a36Sopenharmony_ci	/* entry head array */
109862306a36Sopenharmony_ci	deh = B_I_DEH(bh, ih);
109962306a36Sopenharmony_ci
110062306a36Sopenharmony_ci	/*
110162306a36Sopenharmony_ci	 * first byte of remaining entries, those are BEFORE cut entries
110262306a36Sopenharmony_ci	 * (prev_record) and length of all removed records (cut_records_len)
110362306a36Sopenharmony_ci	 */
110462306a36Sopenharmony_ci	prev_record_offset =
110562306a36Sopenharmony_ci	    (from ? deh_location(&deh[from - 1]) : ih_item_len(ih));
110662306a36Sopenharmony_ci	cut_records_len = prev_record_offset /*from_record */  -
110762306a36Sopenharmony_ci	    deh_location(&deh[from + del_count - 1]);
110862306a36Sopenharmony_ci	prev_record = item + prev_record_offset;
110962306a36Sopenharmony_ci
111062306a36Sopenharmony_ci	/* adjust locations of remaining entries */
111162306a36Sopenharmony_ci	for (i = ih_entry_count(ih) - 1; i > from + del_count - 1; i--)
111262306a36Sopenharmony_ci		put_deh_location(&deh[i],
111362306a36Sopenharmony_ci				 deh_location(&deh[i]) -
111462306a36Sopenharmony_ci				 (DEH_SIZE * del_count));
111562306a36Sopenharmony_ci
111662306a36Sopenharmony_ci	for (i = 0; i < from; i++)
111762306a36Sopenharmony_ci		put_deh_location(&deh[i],
111862306a36Sopenharmony_ci				 deh_location(&deh[i]) - (DEH_SIZE * del_count +
111962306a36Sopenharmony_ci							  cut_records_len));
112062306a36Sopenharmony_ci
112162306a36Sopenharmony_ci	put_ih_entry_count(ih, ih_entry_count(ih) - del_count);
112262306a36Sopenharmony_ci
112362306a36Sopenharmony_ci	/* shift entry head array and entries those are AFTER removed entries */
112462306a36Sopenharmony_ci	memmove((char *)(deh + from),
112562306a36Sopenharmony_ci		deh + from + del_count,
112662306a36Sopenharmony_ci		prev_record - cut_records_len - (char *)(deh + from +
112762306a36Sopenharmony_ci							 del_count));
112862306a36Sopenharmony_ci
112962306a36Sopenharmony_ci	/* shift records, those are BEFORE removed entries */
113062306a36Sopenharmony_ci	memmove(prev_record - cut_records_len - DEH_SIZE * del_count,
113162306a36Sopenharmony_ci		prev_record, item + ih_item_len(ih) - prev_record);
113262306a36Sopenharmony_ci
113362306a36Sopenharmony_ci	return DEH_SIZE * del_count + cut_records_len;
113462306a36Sopenharmony_ci}
113562306a36Sopenharmony_ci
113662306a36Sopenharmony_ci/*
113762306a36Sopenharmony_ci * when cut item is part of regular file
113862306a36Sopenharmony_ci *      pos_in_item - first byte that must be cut
113962306a36Sopenharmony_ci *      cut_size - number of bytes to be cut beginning from pos_in_item
114062306a36Sopenharmony_ci *
114162306a36Sopenharmony_ci * when cut item is part of directory
114262306a36Sopenharmony_ci *      pos_in_item - number of first deleted entry
114362306a36Sopenharmony_ci *      cut_size - count of deleted entries
114462306a36Sopenharmony_ci */
114562306a36Sopenharmony_civoid leaf_cut_from_buffer(struct buffer_info *bi, int cut_item_num,
114662306a36Sopenharmony_ci			  int pos_in_item, int cut_size)
114762306a36Sopenharmony_ci{
114862306a36Sopenharmony_ci	int nr;
114962306a36Sopenharmony_ci	struct buffer_head *bh = bi->bi_bh;
115062306a36Sopenharmony_ci	struct block_head *blkh;
115162306a36Sopenharmony_ci	struct item_head *ih;
115262306a36Sopenharmony_ci	int last_loc, unmoved_loc;
115362306a36Sopenharmony_ci	int i;
115462306a36Sopenharmony_ci
115562306a36Sopenharmony_ci	blkh = B_BLK_HEAD(bh);
115662306a36Sopenharmony_ci	nr = blkh_nr_item(blkh);
115762306a36Sopenharmony_ci
115862306a36Sopenharmony_ci	/* item head of truncated item */
115962306a36Sopenharmony_ci	ih = item_head(bh, cut_item_num);
116062306a36Sopenharmony_ci
116162306a36Sopenharmony_ci	if (is_direntry_le_ih(ih)) {
116262306a36Sopenharmony_ci		/* first cut entry () */
116362306a36Sopenharmony_ci		cut_size = leaf_cut_entries(bh, ih, pos_in_item, cut_size);
116462306a36Sopenharmony_ci		if (pos_in_item == 0) {
116562306a36Sopenharmony_ci			/* change key */
116662306a36Sopenharmony_ci			RFALSE(cut_item_num,
116762306a36Sopenharmony_ci			       "when 0-th enrty of item is cut, that item must be first in the node, not %d-th",
116862306a36Sopenharmony_ci			       cut_item_num);
116962306a36Sopenharmony_ci			/* change item key by key of first entry in the item */
117062306a36Sopenharmony_ci			set_le_ih_k_offset(ih, deh_offset(B_I_DEH(bh, ih)));
117162306a36Sopenharmony_ci		}
117262306a36Sopenharmony_ci	} else {
117362306a36Sopenharmony_ci		/* item is direct or indirect */
117462306a36Sopenharmony_ci		RFALSE(is_statdata_le_ih(ih), "10195: item is stat data");
117562306a36Sopenharmony_ci		RFALSE(pos_in_item && pos_in_item + cut_size != ih_item_len(ih),
117662306a36Sopenharmony_ci		       "10200: invalid offset (%lu) or trunc_size (%lu) or ih_item_len (%lu)",
117762306a36Sopenharmony_ci		       (long unsigned)pos_in_item, (long unsigned)cut_size,
117862306a36Sopenharmony_ci		       (long unsigned)ih_item_len(ih));
117962306a36Sopenharmony_ci
118062306a36Sopenharmony_ci		/* shift item body to left if cut is from the head of item */
118162306a36Sopenharmony_ci		if (pos_in_item == 0) {
118262306a36Sopenharmony_ci			memmove(bh->b_data + ih_location(ih),
118362306a36Sopenharmony_ci				bh->b_data + ih_location(ih) + cut_size,
118462306a36Sopenharmony_ci				ih_item_len(ih) - cut_size);
118562306a36Sopenharmony_ci
118662306a36Sopenharmony_ci			/* change key of item */
118762306a36Sopenharmony_ci			if (is_direct_le_ih(ih))
118862306a36Sopenharmony_ci				set_le_ih_k_offset(ih,
118962306a36Sopenharmony_ci						   le_ih_k_offset(ih) +
119062306a36Sopenharmony_ci						   cut_size);
119162306a36Sopenharmony_ci			else {
119262306a36Sopenharmony_ci				set_le_ih_k_offset(ih,
119362306a36Sopenharmony_ci						   le_ih_k_offset(ih) +
119462306a36Sopenharmony_ci						   (cut_size / UNFM_P_SIZE) *
119562306a36Sopenharmony_ci						   bh->b_size);
119662306a36Sopenharmony_ci				RFALSE(ih_item_len(ih) == cut_size
119762306a36Sopenharmony_ci				       && get_ih_free_space(ih),
119862306a36Sopenharmony_ci				       "10205: invalid ih_free_space (%h)", ih);
119962306a36Sopenharmony_ci			}
120062306a36Sopenharmony_ci		}
120162306a36Sopenharmony_ci	}
120262306a36Sopenharmony_ci
120362306a36Sopenharmony_ci	/* location of the last item */
120462306a36Sopenharmony_ci	last_loc = ih_location(&ih[nr - cut_item_num - 1]);
120562306a36Sopenharmony_ci
120662306a36Sopenharmony_ci	/* location of the item, which is remaining at the same place */
120762306a36Sopenharmony_ci	unmoved_loc = cut_item_num ? ih_location(ih - 1) : bh->b_size;
120862306a36Sopenharmony_ci
120962306a36Sopenharmony_ci	/* shift */
121062306a36Sopenharmony_ci	memmove(bh->b_data + last_loc + cut_size, bh->b_data + last_loc,
121162306a36Sopenharmony_ci		unmoved_loc - last_loc - cut_size);
121262306a36Sopenharmony_ci
121362306a36Sopenharmony_ci	/* change item length */
121462306a36Sopenharmony_ci	put_ih_item_len(ih, ih_item_len(ih) - cut_size);
121562306a36Sopenharmony_ci
121662306a36Sopenharmony_ci	if (is_indirect_le_ih(ih)) {
121762306a36Sopenharmony_ci		if (pos_in_item)
121862306a36Sopenharmony_ci			set_ih_free_space(ih, 0);
121962306a36Sopenharmony_ci	}
122062306a36Sopenharmony_ci
122162306a36Sopenharmony_ci	/* change locations */
122262306a36Sopenharmony_ci	for (i = cut_item_num; i < nr; i++)
122362306a36Sopenharmony_ci		put_ih_location(&ih[i - cut_item_num],
122462306a36Sopenharmony_ci				ih_location(&ih[i - cut_item_num]) + cut_size);
122562306a36Sopenharmony_ci
122662306a36Sopenharmony_ci	/* size, free space */
122762306a36Sopenharmony_ci	set_blkh_free_space(blkh, blkh_free_space(blkh) + cut_size);
122862306a36Sopenharmony_ci
122962306a36Sopenharmony_ci	do_balance_mark_leaf_dirty(bi->tb, bh, 0);
123062306a36Sopenharmony_ci
123162306a36Sopenharmony_ci	if (bi->bi_parent) {
123262306a36Sopenharmony_ci		struct disk_child *t_dc;
123362306a36Sopenharmony_ci		t_dc = B_N_CHILD(bi->bi_parent, bi->bi_position);
123462306a36Sopenharmony_ci		put_dc_size(t_dc, dc_size(t_dc) - cut_size);
123562306a36Sopenharmony_ci		do_balance_mark_internal_dirty(bi->tb, bi->bi_parent, 0);
123662306a36Sopenharmony_ci	}
123762306a36Sopenharmony_ci}
123862306a36Sopenharmony_ci
123962306a36Sopenharmony_ci/* delete del_num items from buffer starting from the first'th item */
124062306a36Sopenharmony_cistatic void leaf_delete_items_entirely(struct buffer_info *bi,
124162306a36Sopenharmony_ci				       int first, int del_num)
124262306a36Sopenharmony_ci{
124362306a36Sopenharmony_ci	struct buffer_head *bh = bi->bi_bh;
124462306a36Sopenharmony_ci	int nr;
124562306a36Sopenharmony_ci	int i, j;
124662306a36Sopenharmony_ci	int last_loc, last_removed_loc;
124762306a36Sopenharmony_ci	struct block_head *blkh;
124862306a36Sopenharmony_ci	struct item_head *ih;
124962306a36Sopenharmony_ci
125062306a36Sopenharmony_ci	RFALSE(bh == NULL, "10210: buffer is 0");
125162306a36Sopenharmony_ci	RFALSE(del_num < 0, "10215: del_num less than 0 (%d)", del_num);
125262306a36Sopenharmony_ci
125362306a36Sopenharmony_ci	if (del_num == 0)
125462306a36Sopenharmony_ci		return;
125562306a36Sopenharmony_ci
125662306a36Sopenharmony_ci	blkh = B_BLK_HEAD(bh);
125762306a36Sopenharmony_ci	nr = blkh_nr_item(blkh);
125862306a36Sopenharmony_ci
125962306a36Sopenharmony_ci	RFALSE(first < 0 || first + del_num > nr,
126062306a36Sopenharmony_ci	       "10220: first=%d, number=%d, there is %d items", first, del_num,
126162306a36Sopenharmony_ci	       nr);
126262306a36Sopenharmony_ci
126362306a36Sopenharmony_ci	if (first == 0 && del_num == nr) {
126462306a36Sopenharmony_ci		/* this does not work */
126562306a36Sopenharmony_ci		make_empty_node(bi);
126662306a36Sopenharmony_ci
126762306a36Sopenharmony_ci		do_balance_mark_leaf_dirty(bi->tb, bh, 0);
126862306a36Sopenharmony_ci		return;
126962306a36Sopenharmony_ci	}
127062306a36Sopenharmony_ci
127162306a36Sopenharmony_ci	ih = item_head(bh, first);
127262306a36Sopenharmony_ci
127362306a36Sopenharmony_ci	/* location of unmovable item */
127462306a36Sopenharmony_ci	j = (first == 0) ? bh->b_size : ih_location(ih - 1);
127562306a36Sopenharmony_ci
127662306a36Sopenharmony_ci	/* delete items */
127762306a36Sopenharmony_ci	last_loc = ih_location(&ih[nr - 1 - first]);
127862306a36Sopenharmony_ci	last_removed_loc = ih_location(&ih[del_num - 1]);
127962306a36Sopenharmony_ci
128062306a36Sopenharmony_ci	memmove(bh->b_data + last_loc + j - last_removed_loc,
128162306a36Sopenharmony_ci		bh->b_data + last_loc, last_removed_loc - last_loc);
128262306a36Sopenharmony_ci
128362306a36Sopenharmony_ci	/* delete item headers */
128462306a36Sopenharmony_ci	memmove(ih, ih + del_num, (nr - first - del_num) * IH_SIZE);
128562306a36Sopenharmony_ci
128662306a36Sopenharmony_ci	/* change item location */
128762306a36Sopenharmony_ci	for (i = first; i < nr - del_num; i++)
128862306a36Sopenharmony_ci		put_ih_location(&ih[i - first],
128962306a36Sopenharmony_ci				ih_location(&ih[i - first]) + (j -
129062306a36Sopenharmony_ci								 last_removed_loc));
129162306a36Sopenharmony_ci
129262306a36Sopenharmony_ci	/* sizes, item number */
129362306a36Sopenharmony_ci	set_blkh_nr_item(blkh, blkh_nr_item(blkh) - del_num);
129462306a36Sopenharmony_ci	set_blkh_free_space(blkh,
129562306a36Sopenharmony_ci			    blkh_free_space(blkh) + (j - last_removed_loc +
129662306a36Sopenharmony_ci						     IH_SIZE * del_num));
129762306a36Sopenharmony_ci
129862306a36Sopenharmony_ci	do_balance_mark_leaf_dirty(bi->tb, bh, 0);
129962306a36Sopenharmony_ci
130062306a36Sopenharmony_ci	if (bi->bi_parent) {
130162306a36Sopenharmony_ci		struct disk_child *t_dc =
130262306a36Sopenharmony_ci		    B_N_CHILD(bi->bi_parent, bi->bi_position);
130362306a36Sopenharmony_ci		put_dc_size(t_dc,
130462306a36Sopenharmony_ci			    dc_size(t_dc) - (j - last_removed_loc +
130562306a36Sopenharmony_ci					     IH_SIZE * del_num));
130662306a36Sopenharmony_ci		do_balance_mark_internal_dirty(bi->tb, bi->bi_parent, 0);
130762306a36Sopenharmony_ci	}
130862306a36Sopenharmony_ci}
130962306a36Sopenharmony_ci
131062306a36Sopenharmony_ci/*
131162306a36Sopenharmony_ci * paste new_entry_count entries (new_dehs, records) into position
131262306a36Sopenharmony_ci * before to item_num-th item
131362306a36Sopenharmony_ci */
131462306a36Sopenharmony_civoid leaf_paste_entries(struct buffer_info *bi,
131562306a36Sopenharmony_ci			int item_num,
131662306a36Sopenharmony_ci			int before,
131762306a36Sopenharmony_ci			int new_entry_count,
131862306a36Sopenharmony_ci			struct reiserfs_de_head *new_dehs,
131962306a36Sopenharmony_ci			const char *records, int paste_size)
132062306a36Sopenharmony_ci{
132162306a36Sopenharmony_ci	struct item_head *ih;
132262306a36Sopenharmony_ci	char *item;
132362306a36Sopenharmony_ci	struct reiserfs_de_head *deh;
132462306a36Sopenharmony_ci	char *insert_point;
132562306a36Sopenharmony_ci	int i;
132662306a36Sopenharmony_ci	struct buffer_head *bh = bi->bi_bh;
132762306a36Sopenharmony_ci
132862306a36Sopenharmony_ci	if (new_entry_count == 0)
132962306a36Sopenharmony_ci		return;
133062306a36Sopenharmony_ci
133162306a36Sopenharmony_ci	ih = item_head(bh, item_num);
133262306a36Sopenharmony_ci
133362306a36Sopenharmony_ci	/*
133462306a36Sopenharmony_ci	 * make sure, that item is directory, and there are enough
133562306a36Sopenharmony_ci	 * records in it
133662306a36Sopenharmony_ci	 */
133762306a36Sopenharmony_ci	RFALSE(!is_direntry_le_ih(ih), "10225: item is not directory item");
133862306a36Sopenharmony_ci	RFALSE(ih_entry_count(ih) < before,
133962306a36Sopenharmony_ci	       "10230: there are no entry we paste entries before. entry_count = %d, before = %d",
134062306a36Sopenharmony_ci	       ih_entry_count(ih), before);
134162306a36Sopenharmony_ci
134262306a36Sopenharmony_ci	/* first byte of dest item */
134362306a36Sopenharmony_ci	item = bh->b_data + ih_location(ih);
134462306a36Sopenharmony_ci
134562306a36Sopenharmony_ci	/* entry head array */
134662306a36Sopenharmony_ci	deh = B_I_DEH(bh, ih);
134762306a36Sopenharmony_ci
134862306a36Sopenharmony_ci	/* new records will be pasted at this point */
134962306a36Sopenharmony_ci	insert_point =
135062306a36Sopenharmony_ci	    item +
135162306a36Sopenharmony_ci	    (before ? deh_location(&deh[before - 1])
135262306a36Sopenharmony_ci	     : (ih_item_len(ih) - paste_size));
135362306a36Sopenharmony_ci
135462306a36Sopenharmony_ci	/* adjust locations of records that will be AFTER new records */
135562306a36Sopenharmony_ci	for (i = ih_entry_count(ih) - 1; i >= before; i--)
135662306a36Sopenharmony_ci		put_deh_location(&deh[i],
135762306a36Sopenharmony_ci				 deh_location(&deh[i]) +
135862306a36Sopenharmony_ci				 (DEH_SIZE * new_entry_count));
135962306a36Sopenharmony_ci
136062306a36Sopenharmony_ci	/* adjust locations of records that will be BEFORE new records */
136162306a36Sopenharmony_ci	for (i = 0; i < before; i++)
136262306a36Sopenharmony_ci		put_deh_location(&deh[i],
136362306a36Sopenharmony_ci				 deh_location(&deh[i]) + paste_size);
136462306a36Sopenharmony_ci
136562306a36Sopenharmony_ci	put_ih_entry_count(ih, ih_entry_count(ih) + new_entry_count);
136662306a36Sopenharmony_ci
136762306a36Sopenharmony_ci	/* prepare space for pasted records */
136862306a36Sopenharmony_ci	memmove(insert_point + paste_size, insert_point,
136962306a36Sopenharmony_ci		item + (ih_item_len(ih) - paste_size) - insert_point);
137062306a36Sopenharmony_ci
137162306a36Sopenharmony_ci	/* copy new records */
137262306a36Sopenharmony_ci	memcpy(insert_point + DEH_SIZE * new_entry_count, records,
137362306a36Sopenharmony_ci	       paste_size - DEH_SIZE * new_entry_count);
137462306a36Sopenharmony_ci
137562306a36Sopenharmony_ci	/* prepare space for new entry heads */
137662306a36Sopenharmony_ci	deh += before;
137762306a36Sopenharmony_ci	memmove((char *)(deh + new_entry_count), deh,
137862306a36Sopenharmony_ci		insert_point - (char *)deh);
137962306a36Sopenharmony_ci
138062306a36Sopenharmony_ci	/* copy new entry heads */
138162306a36Sopenharmony_ci	deh = (struct reiserfs_de_head *)((char *)deh);
138262306a36Sopenharmony_ci	memcpy(deh, new_dehs, DEH_SIZE * new_entry_count);
138362306a36Sopenharmony_ci
138462306a36Sopenharmony_ci	/* set locations of new records */
138562306a36Sopenharmony_ci	for (i = 0; i < new_entry_count; i++) {
138662306a36Sopenharmony_ci		put_deh_location(&deh[i],
138762306a36Sopenharmony_ci				 deh_location(&deh[i]) +
138862306a36Sopenharmony_ci				 (-deh_location
138962306a36Sopenharmony_ci				  (&new_dehs[new_entry_count - 1]) +
139062306a36Sopenharmony_ci				  insert_point + DEH_SIZE * new_entry_count -
139162306a36Sopenharmony_ci				  item));
139262306a36Sopenharmony_ci	}
139362306a36Sopenharmony_ci
139462306a36Sopenharmony_ci	/* change item key if necessary (when we paste before 0-th entry */
139562306a36Sopenharmony_ci	if (!before) {
139662306a36Sopenharmony_ci		set_le_ih_k_offset(ih, deh_offset(new_dehs));
139762306a36Sopenharmony_ci	}
139862306a36Sopenharmony_ci#ifdef CONFIG_REISERFS_CHECK
139962306a36Sopenharmony_ci	{
140062306a36Sopenharmony_ci		int prev, next;
140162306a36Sopenharmony_ci		/* check record locations */
140262306a36Sopenharmony_ci		deh = B_I_DEH(bh, ih);
140362306a36Sopenharmony_ci		for (i = 0; i < ih_entry_count(ih); i++) {
140462306a36Sopenharmony_ci			next =
140562306a36Sopenharmony_ci			    (i <
140662306a36Sopenharmony_ci			     ih_entry_count(ih) -
140762306a36Sopenharmony_ci			     1) ? deh_location(&deh[i + 1]) : 0;
140862306a36Sopenharmony_ci			prev = (i != 0) ? deh_location(&deh[i - 1]) : 0;
140962306a36Sopenharmony_ci
141062306a36Sopenharmony_ci			if (prev && prev <= deh_location(&deh[i]))
141162306a36Sopenharmony_ci				reiserfs_error(sb_from_bi(bi), "vs-10240",
141262306a36Sopenharmony_ci					       "directory item (%h) "
141362306a36Sopenharmony_ci					       "corrupted (prev %a, "
141462306a36Sopenharmony_ci					       "cur(%d) %a)",
141562306a36Sopenharmony_ci					       ih, deh + i - 1, i, deh + i);
141662306a36Sopenharmony_ci			if (next && next >= deh_location(&deh[i]))
141762306a36Sopenharmony_ci				reiserfs_error(sb_from_bi(bi), "vs-10250",
141862306a36Sopenharmony_ci					       "directory item (%h) "
141962306a36Sopenharmony_ci					       "corrupted (cur(%d) %a, "
142062306a36Sopenharmony_ci					       "next %a)",
142162306a36Sopenharmony_ci					       ih, i, deh + i, deh + i + 1);
142262306a36Sopenharmony_ci		}
142362306a36Sopenharmony_ci	}
142462306a36Sopenharmony_ci#endif
142562306a36Sopenharmony_ci
142662306a36Sopenharmony_ci}
1427