18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
38c2ecf20Sopenharmony_ci */
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
68c2ecf20Sopenharmony_ci#include <linux/string.h>
78c2ecf20Sopenharmony_ci#include <linux/time.h>
88c2ecf20Sopenharmony_ci#include "reiserfs.h"
98c2ecf20Sopenharmony_ci#include <linux/buffer_head.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci * copy copy_count entries from source directory item to dest buffer
138c2ecf20Sopenharmony_ci * (creating new item if needed)
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_cistatic void leaf_copy_dir_entries(struct buffer_info *dest_bi,
168c2ecf20Sopenharmony_ci				  struct buffer_head *source, int last_first,
178c2ecf20Sopenharmony_ci				  int item_num, int from, int copy_count)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	struct buffer_head *dest = dest_bi->bi_bh;
208c2ecf20Sopenharmony_ci	/*
218c2ecf20Sopenharmony_ci	 * either the number of target item, or if we must create a
228c2ecf20Sopenharmony_ci	 * new item, the number of the item we will create it next to
238c2ecf20Sopenharmony_ci	 */
248c2ecf20Sopenharmony_ci	int item_num_in_dest;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	struct item_head *ih;
278c2ecf20Sopenharmony_ci	struct reiserfs_de_head *deh;
288c2ecf20Sopenharmony_ci	int copy_records_len;	/* length of all records in item to be copied */
298c2ecf20Sopenharmony_ci	char *records;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	ih = item_head(source, item_num);
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	RFALSE(!is_direntry_le_ih(ih), "vs-10000: item must be directory item");
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	/*
368c2ecf20Sopenharmony_ci	 * length of all record to be copied and first byte of
378c2ecf20Sopenharmony_ci	 * the last of them
388c2ecf20Sopenharmony_ci	 */
398c2ecf20Sopenharmony_ci	deh = B_I_DEH(source, ih);
408c2ecf20Sopenharmony_ci	if (copy_count) {
418c2ecf20Sopenharmony_ci		copy_records_len = (from ? deh_location(&deh[from - 1]) :
428c2ecf20Sopenharmony_ci				    ih_item_len(ih)) -
438c2ecf20Sopenharmony_ci		    deh_location(&deh[from + copy_count - 1]);
448c2ecf20Sopenharmony_ci		records =
458c2ecf20Sopenharmony_ci		    source->b_data + ih_location(ih) +
468c2ecf20Sopenharmony_ci		    deh_location(&deh[from + copy_count - 1]);
478c2ecf20Sopenharmony_ci	} else {
488c2ecf20Sopenharmony_ci		copy_records_len = 0;
498c2ecf20Sopenharmony_ci		records = NULL;
508c2ecf20Sopenharmony_ci	}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	/* when copy last to first, dest buffer can contain 0 items */
538c2ecf20Sopenharmony_ci	item_num_in_dest =
548c2ecf20Sopenharmony_ci	    (last_first ==
558c2ecf20Sopenharmony_ci	     LAST_TO_FIRST) ? ((B_NR_ITEMS(dest)) ? 0 : -1) : (B_NR_ITEMS(dest)
568c2ecf20Sopenharmony_ci							       - 1);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	/*
598c2ecf20Sopenharmony_ci	 * if there are no items in dest or the first/last item in
608c2ecf20Sopenharmony_ci	 * dest is not item of the same directory
618c2ecf20Sopenharmony_ci	 */
628c2ecf20Sopenharmony_ci	if ((item_num_in_dest == -1) ||
638c2ecf20Sopenharmony_ci	    (last_first == FIRST_TO_LAST && le_ih_k_offset(ih) == DOT_OFFSET) ||
648c2ecf20Sopenharmony_ci	    (last_first == LAST_TO_FIRST
658c2ecf20Sopenharmony_ci	     && comp_short_le_keys /*COMP_SHORT_KEYS */ (&ih->ih_key,
668c2ecf20Sopenharmony_ci							 leaf_key(dest,
678c2ecf20Sopenharmony_ci								  item_num_in_dest))))
688c2ecf20Sopenharmony_ci	{
698c2ecf20Sopenharmony_ci		/* create new item in dest */
708c2ecf20Sopenharmony_ci		struct item_head new_ih;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci		/* form item header */
738c2ecf20Sopenharmony_ci		memcpy(&new_ih.ih_key, &ih->ih_key, KEY_SIZE);
748c2ecf20Sopenharmony_ci		put_ih_version(&new_ih, KEY_FORMAT_3_5);
758c2ecf20Sopenharmony_ci		/* calculate item len */
768c2ecf20Sopenharmony_ci		put_ih_item_len(&new_ih,
778c2ecf20Sopenharmony_ci				DEH_SIZE * copy_count + copy_records_len);
788c2ecf20Sopenharmony_ci		put_ih_entry_count(&new_ih, 0);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci		if (last_first == LAST_TO_FIRST) {
818c2ecf20Sopenharmony_ci			/* form key by the following way */
828c2ecf20Sopenharmony_ci			if (from < ih_entry_count(ih)) {
838c2ecf20Sopenharmony_ci				set_le_ih_k_offset(&new_ih,
848c2ecf20Sopenharmony_ci						   deh_offset(&deh[from]));
858c2ecf20Sopenharmony_ci			} else {
868c2ecf20Sopenharmony_ci				/*
878c2ecf20Sopenharmony_ci				 * no entries will be copied to this
888c2ecf20Sopenharmony_ci				 * item in this function
898c2ecf20Sopenharmony_ci				 */
908c2ecf20Sopenharmony_ci				set_le_ih_k_offset(&new_ih, U32_MAX);
918c2ecf20Sopenharmony_ci				/*
928c2ecf20Sopenharmony_ci				 * this item is not yet valid, but we
938c2ecf20Sopenharmony_ci				 * want I_IS_DIRECTORY_ITEM to return 1
948c2ecf20Sopenharmony_ci				 * for it, so we -1
958c2ecf20Sopenharmony_ci				 */
968c2ecf20Sopenharmony_ci			}
978c2ecf20Sopenharmony_ci			set_le_key_k_type(KEY_FORMAT_3_5, &new_ih.ih_key,
988c2ecf20Sopenharmony_ci					  TYPE_DIRENTRY);
998c2ecf20Sopenharmony_ci		}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci		/* insert item into dest buffer */
1028c2ecf20Sopenharmony_ci		leaf_insert_into_buf(dest_bi,
1038c2ecf20Sopenharmony_ci				     (last_first ==
1048c2ecf20Sopenharmony_ci				      LAST_TO_FIRST) ? 0 : B_NR_ITEMS(dest),
1058c2ecf20Sopenharmony_ci				     &new_ih, NULL, 0);
1068c2ecf20Sopenharmony_ci	} else {
1078c2ecf20Sopenharmony_ci		/* prepare space for entries */
1088c2ecf20Sopenharmony_ci		leaf_paste_in_buffer(dest_bi,
1098c2ecf20Sopenharmony_ci				     (last_first ==
1108c2ecf20Sopenharmony_ci				      FIRST_TO_LAST) ? (B_NR_ITEMS(dest) -
1118c2ecf20Sopenharmony_ci							1) : 0, MAX_US_INT,
1128c2ecf20Sopenharmony_ci				     DEH_SIZE * copy_count + copy_records_len,
1138c2ecf20Sopenharmony_ci				     records, 0);
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	item_num_in_dest =
1178c2ecf20Sopenharmony_ci	    (last_first == FIRST_TO_LAST) ? (B_NR_ITEMS(dest) - 1) : 0;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	leaf_paste_entries(dest_bi, item_num_in_dest,
1208c2ecf20Sopenharmony_ci			   (last_first ==
1218c2ecf20Sopenharmony_ci			    FIRST_TO_LAST) ? ih_entry_count(item_head(dest,
1228c2ecf20Sopenharmony_ci									  item_num_in_dest))
1238c2ecf20Sopenharmony_ci			   : 0, copy_count, deh + from, records,
1248c2ecf20Sopenharmony_ci			   DEH_SIZE * copy_count + copy_records_len);
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/*
1288c2ecf20Sopenharmony_ci * Copy the first (if last_first == FIRST_TO_LAST) or last
1298c2ecf20Sopenharmony_ci * (last_first == LAST_TO_FIRST) item or part of it or nothing
1308c2ecf20Sopenharmony_ci * (see the return 0 below) from SOURCE to the end (if last_first)
1318c2ecf20Sopenharmony_ci * or beginning (!last_first) of the DEST
1328c2ecf20Sopenharmony_ci */
1338c2ecf20Sopenharmony_ci/* returns 1 if anything was copied, else 0 */
1348c2ecf20Sopenharmony_cistatic int leaf_copy_boundary_item(struct buffer_info *dest_bi,
1358c2ecf20Sopenharmony_ci				   struct buffer_head *src, int last_first,
1368c2ecf20Sopenharmony_ci				   int bytes_or_entries)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	struct buffer_head *dest = dest_bi->bi_bh;
1398c2ecf20Sopenharmony_ci	/* number of items in the source and destination buffers */
1408c2ecf20Sopenharmony_ci	int dest_nr_item, src_nr_item;
1418c2ecf20Sopenharmony_ci	struct item_head *ih;
1428c2ecf20Sopenharmony_ci	struct item_head *dih;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	dest_nr_item = B_NR_ITEMS(dest);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	/*
1478c2ecf20Sopenharmony_ci	 * if ( DEST is empty or first item of SOURCE and last item of
1488c2ecf20Sopenharmony_ci	 * DEST are the items of different objects or of different types )
1498c2ecf20Sopenharmony_ci	 * then there is no need to treat this item differently from the
1508c2ecf20Sopenharmony_ci	 * other items that we copy, so we return
1518c2ecf20Sopenharmony_ci	 */
1528c2ecf20Sopenharmony_ci	if (last_first == FIRST_TO_LAST) {
1538c2ecf20Sopenharmony_ci		ih = item_head(src, 0);
1548c2ecf20Sopenharmony_ci		dih = item_head(dest, dest_nr_item - 1);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci		/* there is nothing to merge */
1578c2ecf20Sopenharmony_ci		if (!dest_nr_item
1588c2ecf20Sopenharmony_ci		    || (!op_is_left_mergeable(&ih->ih_key, src->b_size)))
1598c2ecf20Sopenharmony_ci			return 0;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci		RFALSE(!ih_item_len(ih),
1628c2ecf20Sopenharmony_ci		       "vs-10010: item can not have empty length");
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci		if (is_direntry_le_ih(ih)) {
1658c2ecf20Sopenharmony_ci			if (bytes_or_entries == -1)
1668c2ecf20Sopenharmony_ci				/* copy all entries to dest */
1678c2ecf20Sopenharmony_ci				bytes_or_entries = ih_entry_count(ih);
1688c2ecf20Sopenharmony_ci			leaf_copy_dir_entries(dest_bi, src, FIRST_TO_LAST, 0, 0,
1698c2ecf20Sopenharmony_ci					      bytes_or_entries);
1708c2ecf20Sopenharmony_ci			return 1;
1718c2ecf20Sopenharmony_ci		}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci		/*
1748c2ecf20Sopenharmony_ci		 * copy part of the body of the first item of SOURCE
1758c2ecf20Sopenharmony_ci		 * to the end of the body of the last item of the DEST
1768c2ecf20Sopenharmony_ci		 * part defined by 'bytes_or_entries'; if bytes_or_entries
1778c2ecf20Sopenharmony_ci		 * == -1 copy whole body; don't create new item header
1788c2ecf20Sopenharmony_ci		 */
1798c2ecf20Sopenharmony_ci		if (bytes_or_entries == -1)
1808c2ecf20Sopenharmony_ci			bytes_or_entries = ih_item_len(ih);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci#ifdef CONFIG_REISERFS_CHECK
1838c2ecf20Sopenharmony_ci		else {
1848c2ecf20Sopenharmony_ci			if (bytes_or_entries == ih_item_len(ih)
1858c2ecf20Sopenharmony_ci			    && is_indirect_le_ih(ih))
1868c2ecf20Sopenharmony_ci				if (get_ih_free_space(ih))
1878c2ecf20Sopenharmony_ci					reiserfs_panic(sb_from_bi(dest_bi),
1888c2ecf20Sopenharmony_ci						       "vs-10020",
1898c2ecf20Sopenharmony_ci						       "last unformatted node "
1908c2ecf20Sopenharmony_ci						       "must be filled "
1918c2ecf20Sopenharmony_ci						       "entirely (%h)", ih);
1928c2ecf20Sopenharmony_ci		}
1938c2ecf20Sopenharmony_ci#endif
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci		/*
1968c2ecf20Sopenharmony_ci		 * merge first item (or its part) of src buffer with the last
1978c2ecf20Sopenharmony_ci		 * item of dest buffer. Both are of the same file
1988c2ecf20Sopenharmony_ci		 */
1998c2ecf20Sopenharmony_ci		leaf_paste_in_buffer(dest_bi,
2008c2ecf20Sopenharmony_ci				     dest_nr_item - 1, ih_item_len(dih),
2018c2ecf20Sopenharmony_ci				     bytes_or_entries, ih_item_body(src, ih), 0);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci		if (is_indirect_le_ih(dih)) {
2048c2ecf20Sopenharmony_ci			RFALSE(get_ih_free_space(dih),
2058c2ecf20Sopenharmony_ci			       "vs-10030: merge to left: last unformatted node of non-last indirect item %h must have zerto free space",
2068c2ecf20Sopenharmony_ci			       ih);
2078c2ecf20Sopenharmony_ci			if (bytes_or_entries == ih_item_len(ih))
2088c2ecf20Sopenharmony_ci				set_ih_free_space(dih, get_ih_free_space(ih));
2098c2ecf20Sopenharmony_ci		}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci		return 1;
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	/* copy boundary item to right (last_first == LAST_TO_FIRST) */
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	/*
2178c2ecf20Sopenharmony_ci	 * (DEST is empty or last item of SOURCE and first item of DEST
2188c2ecf20Sopenharmony_ci	 * are the items of different object or of different types)
2198c2ecf20Sopenharmony_ci	 */
2208c2ecf20Sopenharmony_ci	src_nr_item = B_NR_ITEMS(src);
2218c2ecf20Sopenharmony_ci	ih = item_head(src, src_nr_item - 1);
2228c2ecf20Sopenharmony_ci	dih = item_head(dest, 0);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	if (!dest_nr_item || !op_is_left_mergeable(&dih->ih_key, src->b_size))
2258c2ecf20Sopenharmony_ci		return 0;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	if (is_direntry_le_ih(ih)) {
2288c2ecf20Sopenharmony_ci		/*
2298c2ecf20Sopenharmony_ci		 * bytes_or_entries = entries number in last
2308c2ecf20Sopenharmony_ci		 * item body of SOURCE
2318c2ecf20Sopenharmony_ci		 */
2328c2ecf20Sopenharmony_ci		if (bytes_or_entries == -1)
2338c2ecf20Sopenharmony_ci			bytes_or_entries = ih_entry_count(ih);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci		leaf_copy_dir_entries(dest_bi, src, LAST_TO_FIRST,
2368c2ecf20Sopenharmony_ci				      src_nr_item - 1,
2378c2ecf20Sopenharmony_ci				      ih_entry_count(ih) - bytes_or_entries,
2388c2ecf20Sopenharmony_ci				      bytes_or_entries);
2398c2ecf20Sopenharmony_ci		return 1;
2408c2ecf20Sopenharmony_ci	}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	/*
2438c2ecf20Sopenharmony_ci	 * copy part of the body of the last item of SOURCE to the
2448c2ecf20Sopenharmony_ci	 * begin of the body of the first item of the DEST; part defined
2458c2ecf20Sopenharmony_ci	 * by 'bytes_or_entries'; if byte_or_entriess == -1 copy whole body;
2468c2ecf20Sopenharmony_ci	 * change first item key of the DEST; don't create new item header
2478c2ecf20Sopenharmony_ci	 */
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	RFALSE(is_indirect_le_ih(ih) && get_ih_free_space(ih),
2508c2ecf20Sopenharmony_ci	       "vs-10040: merge to right: last unformatted node of non-last indirect item must be filled entirely (%h)",
2518c2ecf20Sopenharmony_ci	       ih);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	if (bytes_or_entries == -1) {
2548c2ecf20Sopenharmony_ci		/* bytes_or_entries = length of last item body of SOURCE */
2558c2ecf20Sopenharmony_ci		bytes_or_entries = ih_item_len(ih);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci		RFALSE(le_ih_k_offset(dih) !=
2588c2ecf20Sopenharmony_ci		       le_ih_k_offset(ih) + op_bytes_number(ih, src->b_size),
2598c2ecf20Sopenharmony_ci		       "vs-10050: items %h and %h do not match", ih, dih);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci		/* change first item key of the DEST */
2628c2ecf20Sopenharmony_ci		set_le_ih_k_offset(dih, le_ih_k_offset(ih));
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci		/* item becomes non-mergeable */
2658c2ecf20Sopenharmony_ci		/* or mergeable if left item was */
2668c2ecf20Sopenharmony_ci		set_le_ih_k_type(dih, le_ih_k_type(ih));
2678c2ecf20Sopenharmony_ci	} else {
2688c2ecf20Sopenharmony_ci		/* merge to right only part of item */
2698c2ecf20Sopenharmony_ci		RFALSE(ih_item_len(ih) <= bytes_or_entries,
2708c2ecf20Sopenharmony_ci		       "vs-10060: no so much bytes %lu (needed %lu)",
2718c2ecf20Sopenharmony_ci		       (unsigned long)ih_item_len(ih),
2728c2ecf20Sopenharmony_ci		       (unsigned long)bytes_or_entries);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci		/* change first item key of the DEST */
2758c2ecf20Sopenharmony_ci		if (is_direct_le_ih(dih)) {
2768c2ecf20Sopenharmony_ci			RFALSE(le_ih_k_offset(dih) <=
2778c2ecf20Sopenharmony_ci			       (unsigned long)bytes_or_entries,
2788c2ecf20Sopenharmony_ci			       "vs-10070: dih %h, bytes_or_entries(%d)", dih,
2798c2ecf20Sopenharmony_ci			       bytes_or_entries);
2808c2ecf20Sopenharmony_ci			set_le_ih_k_offset(dih,
2818c2ecf20Sopenharmony_ci					   le_ih_k_offset(dih) -
2828c2ecf20Sopenharmony_ci					   bytes_or_entries);
2838c2ecf20Sopenharmony_ci		} else {
2848c2ecf20Sopenharmony_ci			RFALSE(le_ih_k_offset(dih) <=
2858c2ecf20Sopenharmony_ci			       (bytes_or_entries / UNFM_P_SIZE) * dest->b_size,
2868c2ecf20Sopenharmony_ci			       "vs-10080: dih %h, bytes_or_entries(%d)",
2878c2ecf20Sopenharmony_ci			       dih,
2888c2ecf20Sopenharmony_ci			       (bytes_or_entries / UNFM_P_SIZE) * dest->b_size);
2898c2ecf20Sopenharmony_ci			set_le_ih_k_offset(dih,
2908c2ecf20Sopenharmony_ci					   le_ih_k_offset(dih) -
2918c2ecf20Sopenharmony_ci					   ((bytes_or_entries / UNFM_P_SIZE) *
2928c2ecf20Sopenharmony_ci					    dest->b_size));
2938c2ecf20Sopenharmony_ci		}
2948c2ecf20Sopenharmony_ci	}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	leaf_paste_in_buffer(dest_bi, 0, 0, bytes_or_entries,
2978c2ecf20Sopenharmony_ci			     ih_item_body(src,
2988c2ecf20Sopenharmony_ci				       ih) + ih_item_len(ih) - bytes_or_entries,
2998c2ecf20Sopenharmony_ci			     0);
3008c2ecf20Sopenharmony_ci	return 1;
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci/*
3048c2ecf20Sopenharmony_ci * copy cpy_mun items from buffer src to buffer dest
3058c2ecf20Sopenharmony_ci * last_first == FIRST_TO_LAST means, that we copy cpy_num items beginning
3068c2ecf20Sopenharmony_ci *                             from first-th item in src to tail of dest
3078c2ecf20Sopenharmony_ci * last_first == LAST_TO_FIRST means, that we copy cpy_num items beginning
3088c2ecf20Sopenharmony_ci *                             from first-th item in src to head of dest
3098c2ecf20Sopenharmony_ci */
3108c2ecf20Sopenharmony_cistatic void leaf_copy_items_entirely(struct buffer_info *dest_bi,
3118c2ecf20Sopenharmony_ci				     struct buffer_head *src, int last_first,
3128c2ecf20Sopenharmony_ci				     int first, int cpy_num)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	struct buffer_head *dest;
3158c2ecf20Sopenharmony_ci	int nr, free_space;
3168c2ecf20Sopenharmony_ci	int dest_before;
3178c2ecf20Sopenharmony_ci	int last_loc, last_inserted_loc, location;
3188c2ecf20Sopenharmony_ci	int i, j;
3198c2ecf20Sopenharmony_ci	struct block_head *blkh;
3208c2ecf20Sopenharmony_ci	struct item_head *ih;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	RFALSE(last_first != LAST_TO_FIRST && last_first != FIRST_TO_LAST,
3238c2ecf20Sopenharmony_ci	       "vs-10090: bad last_first parameter %d", last_first);
3248c2ecf20Sopenharmony_ci	RFALSE(B_NR_ITEMS(src) - first < cpy_num,
3258c2ecf20Sopenharmony_ci	       "vs-10100: too few items in source %d, required %d from %d",
3268c2ecf20Sopenharmony_ci	       B_NR_ITEMS(src), cpy_num, first);
3278c2ecf20Sopenharmony_ci	RFALSE(cpy_num < 0, "vs-10110: can not copy negative amount of items");
3288c2ecf20Sopenharmony_ci	RFALSE(!dest_bi, "vs-10120: can not copy negative amount of items");
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	dest = dest_bi->bi_bh;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	RFALSE(!dest, "vs-10130: can not copy negative amount of items");
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	if (cpy_num == 0)
3358c2ecf20Sopenharmony_ci		return;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	blkh = B_BLK_HEAD(dest);
3388c2ecf20Sopenharmony_ci	nr = blkh_nr_item(blkh);
3398c2ecf20Sopenharmony_ci	free_space = blkh_free_space(blkh);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	/*
3428c2ecf20Sopenharmony_ci	 * we will insert items before 0-th or nr-th item in dest buffer.
3438c2ecf20Sopenharmony_ci	 * It depends of last_first parameter
3448c2ecf20Sopenharmony_ci	 */
3458c2ecf20Sopenharmony_ci	dest_before = (last_first == LAST_TO_FIRST) ? 0 : nr;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	/* location of head of first new item */
3488c2ecf20Sopenharmony_ci	ih = item_head(dest, dest_before);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	RFALSE(blkh_free_space(blkh) < cpy_num * IH_SIZE,
3518c2ecf20Sopenharmony_ci	       "vs-10140: not enough free space for headers %d (needed %d)",
3528c2ecf20Sopenharmony_ci	       B_FREE_SPACE(dest), cpy_num * IH_SIZE);
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	/* prepare space for headers */
3558c2ecf20Sopenharmony_ci	memmove(ih + cpy_num, ih, (nr - dest_before) * IH_SIZE);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	/* copy item headers */
3588c2ecf20Sopenharmony_ci	memcpy(ih, item_head(src, first), cpy_num * IH_SIZE);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	free_space -= (IH_SIZE * cpy_num);
3618c2ecf20Sopenharmony_ci	set_blkh_free_space(blkh, free_space);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	/* location of unmovable item */
3648c2ecf20Sopenharmony_ci	j = location = (dest_before == 0) ? dest->b_size : ih_location(ih - 1);
3658c2ecf20Sopenharmony_ci	for (i = dest_before; i < nr + cpy_num; i++) {
3668c2ecf20Sopenharmony_ci		location -= ih_item_len(ih + i - dest_before);
3678c2ecf20Sopenharmony_ci		put_ih_location(ih + i - dest_before, location);
3688c2ecf20Sopenharmony_ci	}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	/* prepare space for items */
3718c2ecf20Sopenharmony_ci	last_loc = ih_location(&ih[nr + cpy_num - 1 - dest_before]);
3728c2ecf20Sopenharmony_ci	last_inserted_loc = ih_location(&ih[cpy_num - 1]);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	/* check free space */
3758c2ecf20Sopenharmony_ci	RFALSE(free_space < j - last_inserted_loc,
3768c2ecf20Sopenharmony_ci	       "vs-10150: not enough free space for items %d (needed %d)",
3778c2ecf20Sopenharmony_ci	       free_space, j - last_inserted_loc);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	memmove(dest->b_data + last_loc,
3808c2ecf20Sopenharmony_ci		dest->b_data + last_loc + j - last_inserted_loc,
3818c2ecf20Sopenharmony_ci		last_inserted_loc - last_loc);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	/* copy items */
3848c2ecf20Sopenharmony_ci	memcpy(dest->b_data + last_inserted_loc,
3858c2ecf20Sopenharmony_ci	       item_body(src, (first + cpy_num - 1)),
3868c2ecf20Sopenharmony_ci	       j - last_inserted_loc);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	/* sizes, item number */
3898c2ecf20Sopenharmony_ci	set_blkh_nr_item(blkh, nr + cpy_num);
3908c2ecf20Sopenharmony_ci	set_blkh_free_space(blkh, free_space - (j - last_inserted_loc));
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	do_balance_mark_leaf_dirty(dest_bi->tb, dest, 0);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	if (dest_bi->bi_parent) {
3958c2ecf20Sopenharmony_ci		struct disk_child *t_dc;
3968c2ecf20Sopenharmony_ci		t_dc = B_N_CHILD(dest_bi->bi_parent, dest_bi->bi_position);
3978c2ecf20Sopenharmony_ci		RFALSE(dc_block_number(t_dc) != dest->b_blocknr,
3988c2ecf20Sopenharmony_ci		       "vs-10160: block number in bh does not match to field in disk_child structure %lu and %lu",
3998c2ecf20Sopenharmony_ci		       (long unsigned)dest->b_blocknr,
4008c2ecf20Sopenharmony_ci		       (long unsigned)dc_block_number(t_dc));
4018c2ecf20Sopenharmony_ci		put_dc_size(t_dc,
4028c2ecf20Sopenharmony_ci			    dc_size(t_dc) + (j - last_inserted_loc +
4038c2ecf20Sopenharmony_ci					     IH_SIZE * cpy_num));
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci		do_balance_mark_internal_dirty(dest_bi->tb, dest_bi->bi_parent,
4068c2ecf20Sopenharmony_ci					       0);
4078c2ecf20Sopenharmony_ci	}
4088c2ecf20Sopenharmony_ci}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci/*
4118c2ecf20Sopenharmony_ci * This function splits the (liquid) item into two items (useful when
4128c2ecf20Sopenharmony_ci * shifting part of an item into another node.)
4138c2ecf20Sopenharmony_ci */
4148c2ecf20Sopenharmony_cistatic void leaf_item_bottle(struct buffer_info *dest_bi,
4158c2ecf20Sopenharmony_ci			     struct buffer_head *src, int last_first,
4168c2ecf20Sopenharmony_ci			     int item_num, int cpy_bytes)
4178c2ecf20Sopenharmony_ci{
4188c2ecf20Sopenharmony_ci	struct buffer_head *dest = dest_bi->bi_bh;
4198c2ecf20Sopenharmony_ci	struct item_head *ih;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	RFALSE(cpy_bytes == -1,
4228c2ecf20Sopenharmony_ci	       "vs-10170: bytes == - 1 means: do not split item");
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	if (last_first == FIRST_TO_LAST) {
4258c2ecf20Sopenharmony_ci		/*
4268c2ecf20Sopenharmony_ci		 * if ( if item in position item_num in buffer SOURCE
4278c2ecf20Sopenharmony_ci		 * is directory item )
4288c2ecf20Sopenharmony_ci		 */
4298c2ecf20Sopenharmony_ci		ih = item_head(src, item_num);
4308c2ecf20Sopenharmony_ci		if (is_direntry_le_ih(ih))
4318c2ecf20Sopenharmony_ci			leaf_copy_dir_entries(dest_bi, src, FIRST_TO_LAST,
4328c2ecf20Sopenharmony_ci					      item_num, 0, cpy_bytes);
4338c2ecf20Sopenharmony_ci		else {
4348c2ecf20Sopenharmony_ci			struct item_head n_ih;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci			/*
4378c2ecf20Sopenharmony_ci			 * copy part of the body of the item number 'item_num'
4388c2ecf20Sopenharmony_ci			 * of SOURCE to the end of the DEST part defined by
4398c2ecf20Sopenharmony_ci			 * 'cpy_bytes'; create new item header; change old
4408c2ecf20Sopenharmony_ci			 * item_header (????); n_ih = new item_header;
4418c2ecf20Sopenharmony_ci			 */
4428c2ecf20Sopenharmony_ci			memcpy(&n_ih, ih, IH_SIZE);
4438c2ecf20Sopenharmony_ci			put_ih_item_len(&n_ih, cpy_bytes);
4448c2ecf20Sopenharmony_ci			if (is_indirect_le_ih(ih)) {
4458c2ecf20Sopenharmony_ci				RFALSE(cpy_bytes == ih_item_len(ih)
4468c2ecf20Sopenharmony_ci				       && get_ih_free_space(ih),
4478c2ecf20Sopenharmony_ci				       "vs-10180: when whole indirect item is bottle to left neighbor, it must have free_space==0 (not %lu)",
4488c2ecf20Sopenharmony_ci				       (long unsigned)get_ih_free_space(ih));
4498c2ecf20Sopenharmony_ci				set_ih_free_space(&n_ih, 0);
4508c2ecf20Sopenharmony_ci			}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci			RFALSE(op_is_left_mergeable(&ih->ih_key, src->b_size),
4538c2ecf20Sopenharmony_ci			       "vs-10190: bad mergeability of item %h", ih);
4548c2ecf20Sopenharmony_ci			n_ih.ih_version = ih->ih_version;	/* JDM Endian safe, both le */
4558c2ecf20Sopenharmony_ci			leaf_insert_into_buf(dest_bi, B_NR_ITEMS(dest), &n_ih,
4568c2ecf20Sopenharmony_ci					     item_body(src, item_num), 0);
4578c2ecf20Sopenharmony_ci		}
4588c2ecf20Sopenharmony_ci	} else {
4598c2ecf20Sopenharmony_ci		/*
4608c2ecf20Sopenharmony_ci		 * if ( if item in position item_num in buffer
4618c2ecf20Sopenharmony_ci		 * SOURCE is directory item )
4628c2ecf20Sopenharmony_ci		 */
4638c2ecf20Sopenharmony_ci		ih = item_head(src, item_num);
4648c2ecf20Sopenharmony_ci		if (is_direntry_le_ih(ih))
4658c2ecf20Sopenharmony_ci			leaf_copy_dir_entries(dest_bi, src, LAST_TO_FIRST,
4668c2ecf20Sopenharmony_ci					      item_num,
4678c2ecf20Sopenharmony_ci					      ih_entry_count(ih) - cpy_bytes,
4688c2ecf20Sopenharmony_ci					      cpy_bytes);
4698c2ecf20Sopenharmony_ci		else {
4708c2ecf20Sopenharmony_ci			struct item_head n_ih;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci			/*
4738c2ecf20Sopenharmony_ci			 * copy part of the body of the item number 'item_num'
4748c2ecf20Sopenharmony_ci			 * of SOURCE to the begin of the DEST part defined by
4758c2ecf20Sopenharmony_ci			 * 'cpy_bytes'; create new item header;
4768c2ecf20Sopenharmony_ci			 * n_ih = new item_header;
4778c2ecf20Sopenharmony_ci			 */
4788c2ecf20Sopenharmony_ci			memcpy(&n_ih.ih_key, &ih->ih_key, KEY_SIZE);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci			/* Endian safe, both le */
4818c2ecf20Sopenharmony_ci			n_ih.ih_version = ih->ih_version;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci			if (is_direct_le_ih(ih)) {
4848c2ecf20Sopenharmony_ci				set_le_ih_k_offset(&n_ih,
4858c2ecf20Sopenharmony_ci						   le_ih_k_offset(ih) +
4868c2ecf20Sopenharmony_ci						   ih_item_len(ih) - cpy_bytes);
4878c2ecf20Sopenharmony_ci				set_le_ih_k_type(&n_ih, TYPE_DIRECT);
4888c2ecf20Sopenharmony_ci				set_ih_free_space(&n_ih, MAX_US_INT);
4898c2ecf20Sopenharmony_ci			} else {
4908c2ecf20Sopenharmony_ci				/* indirect item */
4918c2ecf20Sopenharmony_ci				RFALSE(!cpy_bytes && get_ih_free_space(ih),
4928c2ecf20Sopenharmony_ci				       "vs-10200: ih->ih_free_space must be 0 when indirect item will be appended");
4938c2ecf20Sopenharmony_ci				set_le_ih_k_offset(&n_ih,
4948c2ecf20Sopenharmony_ci						   le_ih_k_offset(ih) +
4958c2ecf20Sopenharmony_ci						   (ih_item_len(ih) -
4968c2ecf20Sopenharmony_ci						    cpy_bytes) / UNFM_P_SIZE *
4978c2ecf20Sopenharmony_ci						   dest->b_size);
4988c2ecf20Sopenharmony_ci				set_le_ih_k_type(&n_ih, TYPE_INDIRECT);
4998c2ecf20Sopenharmony_ci				set_ih_free_space(&n_ih, get_ih_free_space(ih));
5008c2ecf20Sopenharmony_ci			}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci			/* set item length */
5038c2ecf20Sopenharmony_ci			put_ih_item_len(&n_ih, cpy_bytes);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci			/* Endian safe, both le */
5068c2ecf20Sopenharmony_ci			n_ih.ih_version = ih->ih_version;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci			leaf_insert_into_buf(dest_bi, 0, &n_ih,
5098c2ecf20Sopenharmony_ci					     item_body(src, item_num) +
5108c2ecf20Sopenharmony_ci						ih_item_len(ih) - cpy_bytes, 0);
5118c2ecf20Sopenharmony_ci		}
5128c2ecf20Sopenharmony_ci	}
5138c2ecf20Sopenharmony_ci}
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci/*
5168c2ecf20Sopenharmony_ci * If cpy_bytes equals minus one than copy cpy_num whole items from SOURCE
5178c2ecf20Sopenharmony_ci * to DEST.  If cpy_bytes not equal to minus one than copy cpy_num-1 whole
5188c2ecf20Sopenharmony_ci * items from SOURCE to DEST.  From last item copy cpy_num bytes for regular
5198c2ecf20Sopenharmony_ci * item and cpy_num directory entries for directory item.
5208c2ecf20Sopenharmony_ci */
5218c2ecf20Sopenharmony_cistatic int leaf_copy_items(struct buffer_info *dest_bi, struct buffer_head *src,
5228c2ecf20Sopenharmony_ci			   int last_first, int cpy_num, int cpy_bytes)
5238c2ecf20Sopenharmony_ci{
5248c2ecf20Sopenharmony_ci	struct buffer_head *dest;
5258c2ecf20Sopenharmony_ci	int pos, i, src_nr_item, bytes;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	dest = dest_bi->bi_bh;
5288c2ecf20Sopenharmony_ci	RFALSE(!dest || !src, "vs-10210: !dest || !src");
5298c2ecf20Sopenharmony_ci	RFALSE(last_first != FIRST_TO_LAST && last_first != LAST_TO_FIRST,
5308c2ecf20Sopenharmony_ci	       "vs-10220:last_first != FIRST_TO_LAST && last_first != LAST_TO_FIRST");
5318c2ecf20Sopenharmony_ci	RFALSE(B_NR_ITEMS(src) < cpy_num,
5328c2ecf20Sopenharmony_ci	       "vs-10230: No enough items: %d, req. %d", B_NR_ITEMS(src),
5338c2ecf20Sopenharmony_ci	       cpy_num);
5348c2ecf20Sopenharmony_ci	RFALSE(cpy_num < 0, "vs-10240: cpy_num < 0 (%d)", cpy_num);
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	if (cpy_num == 0)
5378c2ecf20Sopenharmony_ci		return 0;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	if (last_first == FIRST_TO_LAST) {
5408c2ecf20Sopenharmony_ci		/* copy items to left */
5418c2ecf20Sopenharmony_ci		pos = 0;
5428c2ecf20Sopenharmony_ci		if (cpy_num == 1)
5438c2ecf20Sopenharmony_ci			bytes = cpy_bytes;
5448c2ecf20Sopenharmony_ci		else
5458c2ecf20Sopenharmony_ci			bytes = -1;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci		/*
5488c2ecf20Sopenharmony_ci		 * copy the first item or it part or nothing to the end of
5498c2ecf20Sopenharmony_ci		 * the DEST (i = leaf_copy_boundary_item(DEST,SOURCE,0,bytes))
5508c2ecf20Sopenharmony_ci		 */
5518c2ecf20Sopenharmony_ci		i = leaf_copy_boundary_item(dest_bi, src, FIRST_TO_LAST, bytes);
5528c2ecf20Sopenharmony_ci		cpy_num -= i;
5538c2ecf20Sopenharmony_ci		if (cpy_num == 0)
5548c2ecf20Sopenharmony_ci			return i;
5558c2ecf20Sopenharmony_ci		pos += i;
5568c2ecf20Sopenharmony_ci		if (cpy_bytes == -1)
5578c2ecf20Sopenharmony_ci			/*
5588c2ecf20Sopenharmony_ci			 * copy first cpy_num items starting from position
5598c2ecf20Sopenharmony_ci			 * 'pos' of SOURCE to end of DEST
5608c2ecf20Sopenharmony_ci			 */
5618c2ecf20Sopenharmony_ci			leaf_copy_items_entirely(dest_bi, src, FIRST_TO_LAST,
5628c2ecf20Sopenharmony_ci						 pos, cpy_num);
5638c2ecf20Sopenharmony_ci		else {
5648c2ecf20Sopenharmony_ci			/*
5658c2ecf20Sopenharmony_ci			 * copy first cpy_num-1 items starting from position
5668c2ecf20Sopenharmony_ci			 * 'pos-1' of the SOURCE to the end of the DEST
5678c2ecf20Sopenharmony_ci			 */
5688c2ecf20Sopenharmony_ci			leaf_copy_items_entirely(dest_bi, src, FIRST_TO_LAST,
5698c2ecf20Sopenharmony_ci						 pos, cpy_num - 1);
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci			/*
5728c2ecf20Sopenharmony_ci			 * copy part of the item which number is
5738c2ecf20Sopenharmony_ci			 * cpy_num+pos-1 to the end of the DEST
5748c2ecf20Sopenharmony_ci			 */
5758c2ecf20Sopenharmony_ci			leaf_item_bottle(dest_bi, src, FIRST_TO_LAST,
5768c2ecf20Sopenharmony_ci					 cpy_num + pos - 1, cpy_bytes);
5778c2ecf20Sopenharmony_ci		}
5788c2ecf20Sopenharmony_ci	} else {
5798c2ecf20Sopenharmony_ci		/* copy items to right */
5808c2ecf20Sopenharmony_ci		src_nr_item = B_NR_ITEMS(src);
5818c2ecf20Sopenharmony_ci		if (cpy_num == 1)
5828c2ecf20Sopenharmony_ci			bytes = cpy_bytes;
5838c2ecf20Sopenharmony_ci		else
5848c2ecf20Sopenharmony_ci			bytes = -1;
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci		/*
5878c2ecf20Sopenharmony_ci		 * copy the last item or it part or nothing to the
5888c2ecf20Sopenharmony_ci		 * begin of the DEST
5898c2ecf20Sopenharmony_ci		 * (i = leaf_copy_boundary_item(DEST,SOURCE,1,bytes));
5908c2ecf20Sopenharmony_ci		 */
5918c2ecf20Sopenharmony_ci		i = leaf_copy_boundary_item(dest_bi, src, LAST_TO_FIRST, bytes);
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci		cpy_num -= i;
5948c2ecf20Sopenharmony_ci		if (cpy_num == 0)
5958c2ecf20Sopenharmony_ci			return i;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci		pos = src_nr_item - cpy_num - i;
5988c2ecf20Sopenharmony_ci		if (cpy_bytes == -1) {
5998c2ecf20Sopenharmony_ci			/*
6008c2ecf20Sopenharmony_ci			 * starting from position 'pos' copy last cpy_num
6018c2ecf20Sopenharmony_ci			 * items of SOURCE to begin of DEST
6028c2ecf20Sopenharmony_ci			 */
6038c2ecf20Sopenharmony_ci			leaf_copy_items_entirely(dest_bi, src, LAST_TO_FIRST,
6048c2ecf20Sopenharmony_ci						 pos, cpy_num);
6058c2ecf20Sopenharmony_ci		} else {
6068c2ecf20Sopenharmony_ci			/*
6078c2ecf20Sopenharmony_ci			 * copy last cpy_num-1 items starting from position
6088c2ecf20Sopenharmony_ci			 * 'pos+1' of the SOURCE to the begin of the DEST;
6098c2ecf20Sopenharmony_ci			 */
6108c2ecf20Sopenharmony_ci			leaf_copy_items_entirely(dest_bi, src, LAST_TO_FIRST,
6118c2ecf20Sopenharmony_ci						 pos + 1, cpy_num - 1);
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci			/*
6148c2ecf20Sopenharmony_ci			 * copy part of the item which number is pos to
6158c2ecf20Sopenharmony_ci			 * the begin of the DEST
6168c2ecf20Sopenharmony_ci			 */
6178c2ecf20Sopenharmony_ci			leaf_item_bottle(dest_bi, src, LAST_TO_FIRST, pos,
6188c2ecf20Sopenharmony_ci					 cpy_bytes);
6198c2ecf20Sopenharmony_ci		}
6208c2ecf20Sopenharmony_ci	}
6218c2ecf20Sopenharmony_ci	return i;
6228c2ecf20Sopenharmony_ci}
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci/*
6258c2ecf20Sopenharmony_ci * there are types of coping: from S[0] to L[0], from S[0] to R[0],
6268c2ecf20Sopenharmony_ci * from R[0] to L[0]. for each of these we have to define parent and
6278c2ecf20Sopenharmony_ci * positions of destination and source buffers
6288c2ecf20Sopenharmony_ci */
6298c2ecf20Sopenharmony_cistatic void leaf_define_dest_src_infos(int shift_mode, struct tree_balance *tb,
6308c2ecf20Sopenharmony_ci				       struct buffer_info *dest_bi,
6318c2ecf20Sopenharmony_ci				       struct buffer_info *src_bi,
6328c2ecf20Sopenharmony_ci				       int *first_last,
6338c2ecf20Sopenharmony_ci				       struct buffer_head *Snew)
6348c2ecf20Sopenharmony_ci{
6358c2ecf20Sopenharmony_ci	memset(dest_bi, 0, sizeof(struct buffer_info));
6368c2ecf20Sopenharmony_ci	memset(src_bi, 0, sizeof(struct buffer_info));
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	/* define dest, src, dest parent, dest position */
6398c2ecf20Sopenharmony_ci	switch (shift_mode) {
6408c2ecf20Sopenharmony_ci	case LEAF_FROM_S_TO_L:	/* it is used in leaf_shift_left */
6418c2ecf20Sopenharmony_ci		src_bi->tb = tb;
6428c2ecf20Sopenharmony_ci		src_bi->bi_bh = PATH_PLAST_BUFFER(tb->tb_path);
6438c2ecf20Sopenharmony_ci		src_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, 0);
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci		/* src->b_item_order */
6468c2ecf20Sopenharmony_ci		src_bi->bi_position = PATH_H_B_ITEM_ORDER(tb->tb_path, 0);
6478c2ecf20Sopenharmony_ci		dest_bi->tb = tb;
6488c2ecf20Sopenharmony_ci		dest_bi->bi_bh = tb->L[0];
6498c2ecf20Sopenharmony_ci		dest_bi->bi_parent = tb->FL[0];
6508c2ecf20Sopenharmony_ci		dest_bi->bi_position = get_left_neighbor_position(tb, 0);
6518c2ecf20Sopenharmony_ci		*first_last = FIRST_TO_LAST;
6528c2ecf20Sopenharmony_ci		break;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	case LEAF_FROM_S_TO_R:	/* it is used in leaf_shift_right */
6558c2ecf20Sopenharmony_ci		src_bi->tb = tb;
6568c2ecf20Sopenharmony_ci		src_bi->bi_bh = PATH_PLAST_BUFFER(tb->tb_path);
6578c2ecf20Sopenharmony_ci		src_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, 0);
6588c2ecf20Sopenharmony_ci		src_bi->bi_position = PATH_H_B_ITEM_ORDER(tb->tb_path, 0);
6598c2ecf20Sopenharmony_ci		dest_bi->tb = tb;
6608c2ecf20Sopenharmony_ci		dest_bi->bi_bh = tb->R[0];
6618c2ecf20Sopenharmony_ci		dest_bi->bi_parent = tb->FR[0];
6628c2ecf20Sopenharmony_ci		dest_bi->bi_position = get_right_neighbor_position(tb, 0);
6638c2ecf20Sopenharmony_ci		*first_last = LAST_TO_FIRST;
6648c2ecf20Sopenharmony_ci		break;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	case LEAF_FROM_R_TO_L:	/* it is used in balance_leaf_when_delete */
6678c2ecf20Sopenharmony_ci		src_bi->tb = tb;
6688c2ecf20Sopenharmony_ci		src_bi->bi_bh = tb->R[0];
6698c2ecf20Sopenharmony_ci		src_bi->bi_parent = tb->FR[0];
6708c2ecf20Sopenharmony_ci		src_bi->bi_position = get_right_neighbor_position(tb, 0);
6718c2ecf20Sopenharmony_ci		dest_bi->tb = tb;
6728c2ecf20Sopenharmony_ci		dest_bi->bi_bh = tb->L[0];
6738c2ecf20Sopenharmony_ci		dest_bi->bi_parent = tb->FL[0];
6748c2ecf20Sopenharmony_ci		dest_bi->bi_position = get_left_neighbor_position(tb, 0);
6758c2ecf20Sopenharmony_ci		*first_last = FIRST_TO_LAST;
6768c2ecf20Sopenharmony_ci		break;
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	case LEAF_FROM_L_TO_R:	/* it is used in balance_leaf_when_delete */
6798c2ecf20Sopenharmony_ci		src_bi->tb = tb;
6808c2ecf20Sopenharmony_ci		src_bi->bi_bh = tb->L[0];
6818c2ecf20Sopenharmony_ci		src_bi->bi_parent = tb->FL[0];
6828c2ecf20Sopenharmony_ci		src_bi->bi_position = get_left_neighbor_position(tb, 0);
6838c2ecf20Sopenharmony_ci		dest_bi->tb = tb;
6848c2ecf20Sopenharmony_ci		dest_bi->bi_bh = tb->R[0];
6858c2ecf20Sopenharmony_ci		dest_bi->bi_parent = tb->FR[0];
6868c2ecf20Sopenharmony_ci		dest_bi->bi_position = get_right_neighbor_position(tb, 0);
6878c2ecf20Sopenharmony_ci		*first_last = LAST_TO_FIRST;
6888c2ecf20Sopenharmony_ci		break;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	case LEAF_FROM_S_TO_SNEW:
6918c2ecf20Sopenharmony_ci		src_bi->tb = tb;
6928c2ecf20Sopenharmony_ci		src_bi->bi_bh = PATH_PLAST_BUFFER(tb->tb_path);
6938c2ecf20Sopenharmony_ci		src_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, 0);
6948c2ecf20Sopenharmony_ci		src_bi->bi_position = PATH_H_B_ITEM_ORDER(tb->tb_path, 0);
6958c2ecf20Sopenharmony_ci		dest_bi->tb = tb;
6968c2ecf20Sopenharmony_ci		dest_bi->bi_bh = Snew;
6978c2ecf20Sopenharmony_ci		dest_bi->bi_parent = NULL;
6988c2ecf20Sopenharmony_ci		dest_bi->bi_position = 0;
6998c2ecf20Sopenharmony_ci		*first_last = LAST_TO_FIRST;
7008c2ecf20Sopenharmony_ci		break;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	default:
7038c2ecf20Sopenharmony_ci		reiserfs_panic(sb_from_bi(src_bi), "vs-10250",
7048c2ecf20Sopenharmony_ci			       "shift type is unknown (%d)", shift_mode);
7058c2ecf20Sopenharmony_ci	}
7068c2ecf20Sopenharmony_ci	RFALSE(!src_bi->bi_bh || !dest_bi->bi_bh,
7078c2ecf20Sopenharmony_ci	       "vs-10260: mode==%d, source (%p) or dest (%p) buffer is initialized incorrectly",
7088c2ecf20Sopenharmony_ci	       shift_mode, src_bi->bi_bh, dest_bi->bi_bh);
7098c2ecf20Sopenharmony_ci}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci/*
7128c2ecf20Sopenharmony_ci * copy mov_num items and mov_bytes of the (mov_num-1)th item to
7138c2ecf20Sopenharmony_ci * neighbor. Delete them from source
7148c2ecf20Sopenharmony_ci */
7158c2ecf20Sopenharmony_ciint leaf_move_items(int shift_mode, struct tree_balance *tb, int mov_num,
7168c2ecf20Sopenharmony_ci		    int mov_bytes, struct buffer_head *Snew)
7178c2ecf20Sopenharmony_ci{
7188c2ecf20Sopenharmony_ci	int ret_value;
7198c2ecf20Sopenharmony_ci	struct buffer_info dest_bi, src_bi;
7208c2ecf20Sopenharmony_ci	int first_last;
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	leaf_define_dest_src_infos(shift_mode, tb, &dest_bi, &src_bi,
7238c2ecf20Sopenharmony_ci				   &first_last, Snew);
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	ret_value =
7268c2ecf20Sopenharmony_ci	    leaf_copy_items(&dest_bi, src_bi.bi_bh, first_last, mov_num,
7278c2ecf20Sopenharmony_ci			    mov_bytes);
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	leaf_delete_items(&src_bi, first_last,
7308c2ecf20Sopenharmony_ci			  (first_last ==
7318c2ecf20Sopenharmony_ci			   FIRST_TO_LAST) ? 0 : (B_NR_ITEMS(src_bi.bi_bh) -
7328c2ecf20Sopenharmony_ci						 mov_num), mov_num, mov_bytes);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	return ret_value;
7358c2ecf20Sopenharmony_ci}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci/*
7388c2ecf20Sopenharmony_ci * Shift shift_num items (and shift_bytes of last shifted item if
7398c2ecf20Sopenharmony_ci * shift_bytes != -1) from S[0] to L[0] and replace the delimiting key
7408c2ecf20Sopenharmony_ci */
7418c2ecf20Sopenharmony_ciint leaf_shift_left(struct tree_balance *tb, int shift_num, int shift_bytes)
7428c2ecf20Sopenharmony_ci{
7438c2ecf20Sopenharmony_ci	struct buffer_head *S0 = PATH_PLAST_BUFFER(tb->tb_path);
7448c2ecf20Sopenharmony_ci	int i;
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	/*
7478c2ecf20Sopenharmony_ci	 * move shift_num (and shift_bytes bytes) items from S[0]
7488c2ecf20Sopenharmony_ci	 * to left neighbor L[0]
7498c2ecf20Sopenharmony_ci	 */
7508c2ecf20Sopenharmony_ci	i = leaf_move_items(LEAF_FROM_S_TO_L, tb, shift_num, shift_bytes, NULL);
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	if (shift_num) {
7538c2ecf20Sopenharmony_ci		/* number of items in S[0] == 0 */
7548c2ecf20Sopenharmony_ci		if (B_NR_ITEMS(S0) == 0) {
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci			RFALSE(shift_bytes != -1,
7578c2ecf20Sopenharmony_ci			       "vs-10270: S0 is empty now, but shift_bytes != -1 (%d)",
7588c2ecf20Sopenharmony_ci			       shift_bytes);
7598c2ecf20Sopenharmony_ci#ifdef CONFIG_REISERFS_CHECK
7608c2ecf20Sopenharmony_ci			if (tb->tb_mode == M_PASTE || tb->tb_mode == M_INSERT) {
7618c2ecf20Sopenharmony_ci				print_cur_tb("vs-10275");
7628c2ecf20Sopenharmony_ci				reiserfs_panic(tb->tb_sb, "vs-10275",
7638c2ecf20Sopenharmony_ci					       "balance condition corrupted "
7648c2ecf20Sopenharmony_ci					       "(%c)", tb->tb_mode);
7658c2ecf20Sopenharmony_ci			}
7668c2ecf20Sopenharmony_ci#endif
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci			if (PATH_H_POSITION(tb->tb_path, 1) == 0)
7698c2ecf20Sopenharmony_ci				replace_key(tb, tb->CFL[0], tb->lkey[0],
7708c2ecf20Sopenharmony_ci					    PATH_H_PPARENT(tb->tb_path, 0), 0);
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci		} else {
7738c2ecf20Sopenharmony_ci			/* replace lkey in CFL[0] by 0-th key from S[0]; */
7748c2ecf20Sopenharmony_ci			replace_key(tb, tb->CFL[0], tb->lkey[0], S0, 0);
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci			RFALSE((shift_bytes != -1 &&
7778c2ecf20Sopenharmony_ci				!(is_direntry_le_ih(item_head(S0, 0))
7788c2ecf20Sopenharmony_ci				  && !ih_entry_count(item_head(S0, 0)))) &&
7798c2ecf20Sopenharmony_ci			       (!op_is_left_mergeable
7808c2ecf20Sopenharmony_ci				(leaf_key(S0, 0), S0->b_size)),
7818c2ecf20Sopenharmony_ci			       "vs-10280: item must be mergeable");
7828c2ecf20Sopenharmony_ci		}
7838c2ecf20Sopenharmony_ci	}
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	return i;
7868c2ecf20Sopenharmony_ci}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci/* CLEANING STOPPED HERE */
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci/*
7918c2ecf20Sopenharmony_ci * Shift shift_num (shift_bytes) items from S[0] to the right neighbor,
7928c2ecf20Sopenharmony_ci * and replace the delimiting key
7938c2ecf20Sopenharmony_ci */
7948c2ecf20Sopenharmony_ciint leaf_shift_right(struct tree_balance *tb, int shift_num, int shift_bytes)
7958c2ecf20Sopenharmony_ci{
7968c2ecf20Sopenharmony_ci	int ret_value;
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	/*
7998c2ecf20Sopenharmony_ci	 * move shift_num (and shift_bytes) items from S[0] to
8008c2ecf20Sopenharmony_ci	 * right neighbor R[0]
8018c2ecf20Sopenharmony_ci	 */
8028c2ecf20Sopenharmony_ci	ret_value =
8038c2ecf20Sopenharmony_ci	    leaf_move_items(LEAF_FROM_S_TO_R, tb, shift_num, shift_bytes, NULL);
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	/* replace rkey in CFR[0] by the 0-th key from R[0] */
8068c2ecf20Sopenharmony_ci	if (shift_num) {
8078c2ecf20Sopenharmony_ci		replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	}
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	return ret_value;
8128c2ecf20Sopenharmony_ci}
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_cistatic void leaf_delete_items_entirely(struct buffer_info *bi,
8158c2ecf20Sopenharmony_ci				       int first, int del_num);
8168c2ecf20Sopenharmony_ci/*
8178c2ecf20Sopenharmony_ci * If del_bytes == -1, starting from position 'first' delete del_num
8188c2ecf20Sopenharmony_ci * items in whole in buffer CUR.
8198c2ecf20Sopenharmony_ci *   If not.
8208c2ecf20Sopenharmony_ci *   If last_first == 0. Starting from position 'first' delete del_num-1
8218c2ecf20Sopenharmony_ci *   items in whole. Delete part of body of the first item. Part defined by
8228c2ecf20Sopenharmony_ci *   del_bytes. Don't delete first item header
8238c2ecf20Sopenharmony_ci *   If last_first == 1. Starting from position 'first+1' delete del_num-1
8248c2ecf20Sopenharmony_ci *   items in whole. Delete part of body of the last item . Part defined by
8258c2ecf20Sopenharmony_ci *   del_bytes. Don't delete last item header.
8268c2ecf20Sopenharmony_ci*/
8278c2ecf20Sopenharmony_civoid leaf_delete_items(struct buffer_info *cur_bi, int last_first,
8288c2ecf20Sopenharmony_ci		       int first, int del_num, int del_bytes)
8298c2ecf20Sopenharmony_ci{
8308c2ecf20Sopenharmony_ci	struct buffer_head *bh;
8318c2ecf20Sopenharmony_ci	int item_amount = B_NR_ITEMS(bh = cur_bi->bi_bh);
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	RFALSE(!bh, "10155: bh is not defined");
8348c2ecf20Sopenharmony_ci	RFALSE(del_num < 0, "10160: del_num can not be < 0. del_num==%d",
8358c2ecf20Sopenharmony_ci	       del_num);
8368c2ecf20Sopenharmony_ci	RFALSE(first < 0
8378c2ecf20Sopenharmony_ci	       || first + del_num > item_amount,
8388c2ecf20Sopenharmony_ci	       "10165: invalid number of first item to be deleted (%d) or "
8398c2ecf20Sopenharmony_ci	       "no so much items (%d) to delete (only %d)", first,
8408c2ecf20Sopenharmony_ci	       first + del_num, item_amount);
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	if (del_num == 0)
8438c2ecf20Sopenharmony_ci		return;
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	if (first == 0 && del_num == item_amount && del_bytes == -1) {
8468c2ecf20Sopenharmony_ci		make_empty_node(cur_bi);
8478c2ecf20Sopenharmony_ci		do_balance_mark_leaf_dirty(cur_bi->tb, bh, 0);
8488c2ecf20Sopenharmony_ci		return;
8498c2ecf20Sopenharmony_ci	}
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	if (del_bytes == -1)
8528c2ecf20Sopenharmony_ci		/* delete del_num items beginning from item in position first */
8538c2ecf20Sopenharmony_ci		leaf_delete_items_entirely(cur_bi, first, del_num);
8548c2ecf20Sopenharmony_ci	else {
8558c2ecf20Sopenharmony_ci		if (last_first == FIRST_TO_LAST) {
8568c2ecf20Sopenharmony_ci			/*
8578c2ecf20Sopenharmony_ci			 * delete del_num-1 items beginning from
8588c2ecf20Sopenharmony_ci			 * item in position first
8598c2ecf20Sopenharmony_ci			 */
8608c2ecf20Sopenharmony_ci			leaf_delete_items_entirely(cur_bi, first, del_num - 1);
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci			/*
8638c2ecf20Sopenharmony_ci			 * delete the part of the first item of the bh
8648c2ecf20Sopenharmony_ci			 * do not delete item header
8658c2ecf20Sopenharmony_ci			 */
8668c2ecf20Sopenharmony_ci			leaf_cut_from_buffer(cur_bi, 0, 0, del_bytes);
8678c2ecf20Sopenharmony_ci		} else {
8688c2ecf20Sopenharmony_ci			struct item_head *ih;
8698c2ecf20Sopenharmony_ci			int len;
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci			/*
8728c2ecf20Sopenharmony_ci			 * delete del_num-1 items beginning from
8738c2ecf20Sopenharmony_ci			 * item in position first+1
8748c2ecf20Sopenharmony_ci			 */
8758c2ecf20Sopenharmony_ci			leaf_delete_items_entirely(cur_bi, first + 1,
8768c2ecf20Sopenharmony_ci						   del_num - 1);
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci			ih = item_head(bh, B_NR_ITEMS(bh) - 1);
8798c2ecf20Sopenharmony_ci			if (is_direntry_le_ih(ih))
8808c2ecf20Sopenharmony_ci				/* the last item is directory  */
8818c2ecf20Sopenharmony_ci				/*
8828c2ecf20Sopenharmony_ci				 * len = numbers of directory entries
8838c2ecf20Sopenharmony_ci				 * in this item
8848c2ecf20Sopenharmony_ci				 */
8858c2ecf20Sopenharmony_ci				len = ih_entry_count(ih);
8868c2ecf20Sopenharmony_ci			else
8878c2ecf20Sopenharmony_ci				/* len = body len of item */
8888c2ecf20Sopenharmony_ci				len = ih_item_len(ih);
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci			/*
8918c2ecf20Sopenharmony_ci			 * delete the part of the last item of the bh
8928c2ecf20Sopenharmony_ci			 * do not delete item header
8938c2ecf20Sopenharmony_ci			 */
8948c2ecf20Sopenharmony_ci			leaf_cut_from_buffer(cur_bi, B_NR_ITEMS(bh) - 1,
8958c2ecf20Sopenharmony_ci					     len - del_bytes, del_bytes);
8968c2ecf20Sopenharmony_ci		}
8978c2ecf20Sopenharmony_ci	}
8988c2ecf20Sopenharmony_ci}
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci/* insert item into the leaf node in position before */
9018c2ecf20Sopenharmony_civoid leaf_insert_into_buf(struct buffer_info *bi, int before,
9028c2ecf20Sopenharmony_ci			  struct item_head * const inserted_item_ih,
9038c2ecf20Sopenharmony_ci			  const char * const inserted_item_body,
9048c2ecf20Sopenharmony_ci			  int zeros_number)
9058c2ecf20Sopenharmony_ci{
9068c2ecf20Sopenharmony_ci	struct buffer_head *bh = bi->bi_bh;
9078c2ecf20Sopenharmony_ci	int nr, free_space;
9088c2ecf20Sopenharmony_ci	struct block_head *blkh;
9098c2ecf20Sopenharmony_ci	struct item_head *ih;
9108c2ecf20Sopenharmony_ci	int i;
9118c2ecf20Sopenharmony_ci	int last_loc, unmoved_loc;
9128c2ecf20Sopenharmony_ci	char *to;
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	blkh = B_BLK_HEAD(bh);
9158c2ecf20Sopenharmony_ci	nr = blkh_nr_item(blkh);
9168c2ecf20Sopenharmony_ci	free_space = blkh_free_space(blkh);
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci	/* check free space */
9198c2ecf20Sopenharmony_ci	RFALSE(free_space < ih_item_len(inserted_item_ih) + IH_SIZE,
9208c2ecf20Sopenharmony_ci	       "vs-10170: not enough free space in block %z, new item %h",
9218c2ecf20Sopenharmony_ci	       bh, inserted_item_ih);
9228c2ecf20Sopenharmony_ci	RFALSE(zeros_number > ih_item_len(inserted_item_ih),
9238c2ecf20Sopenharmony_ci	       "vs-10172: zero number == %d, item length == %d",
9248c2ecf20Sopenharmony_ci	       zeros_number, ih_item_len(inserted_item_ih));
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	/* get item new item must be inserted before */
9278c2ecf20Sopenharmony_ci	ih = item_head(bh, before);
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	/* prepare space for the body of new item */
9308c2ecf20Sopenharmony_ci	last_loc = nr ? ih_location(&ih[nr - before - 1]) : bh->b_size;
9318c2ecf20Sopenharmony_ci	unmoved_loc = before ? ih_location(ih - 1) : bh->b_size;
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci	memmove(bh->b_data + last_loc - ih_item_len(inserted_item_ih),
9348c2ecf20Sopenharmony_ci		bh->b_data + last_loc, unmoved_loc - last_loc);
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	to = bh->b_data + unmoved_loc - ih_item_len(inserted_item_ih);
9378c2ecf20Sopenharmony_ci	memset(to, 0, zeros_number);
9388c2ecf20Sopenharmony_ci	to += zeros_number;
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	/* copy body to prepared space */
9418c2ecf20Sopenharmony_ci	if (inserted_item_body)
9428c2ecf20Sopenharmony_ci		memmove(to, inserted_item_body,
9438c2ecf20Sopenharmony_ci			ih_item_len(inserted_item_ih) - zeros_number);
9448c2ecf20Sopenharmony_ci	else
9458c2ecf20Sopenharmony_ci		memset(to, '\0', ih_item_len(inserted_item_ih) - zeros_number);
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci	/* insert item header */
9488c2ecf20Sopenharmony_ci	memmove(ih + 1, ih, IH_SIZE * (nr - before));
9498c2ecf20Sopenharmony_ci	memmove(ih, inserted_item_ih, IH_SIZE);
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	/* change locations */
9528c2ecf20Sopenharmony_ci	for (i = before; i < nr + 1; i++) {
9538c2ecf20Sopenharmony_ci		unmoved_loc -= ih_item_len(&ih[i - before]);
9548c2ecf20Sopenharmony_ci		put_ih_location(&ih[i - before], unmoved_loc);
9558c2ecf20Sopenharmony_ci	}
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	/* sizes, free space, item number */
9588c2ecf20Sopenharmony_ci	set_blkh_nr_item(blkh, blkh_nr_item(blkh) + 1);
9598c2ecf20Sopenharmony_ci	set_blkh_free_space(blkh,
9608c2ecf20Sopenharmony_ci			    free_space - (IH_SIZE +
9618c2ecf20Sopenharmony_ci					  ih_item_len(inserted_item_ih)));
9628c2ecf20Sopenharmony_ci	do_balance_mark_leaf_dirty(bi->tb, bh, 1);
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	if (bi->bi_parent) {
9658c2ecf20Sopenharmony_ci		struct disk_child *t_dc;
9668c2ecf20Sopenharmony_ci		t_dc = B_N_CHILD(bi->bi_parent, bi->bi_position);
9678c2ecf20Sopenharmony_ci		put_dc_size(t_dc,
9688c2ecf20Sopenharmony_ci			    dc_size(t_dc) + (IH_SIZE +
9698c2ecf20Sopenharmony_ci					     ih_item_len(inserted_item_ih)));
9708c2ecf20Sopenharmony_ci		do_balance_mark_internal_dirty(bi->tb, bi->bi_parent, 0);
9718c2ecf20Sopenharmony_ci	}
9728c2ecf20Sopenharmony_ci}
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci/*
9758c2ecf20Sopenharmony_ci * paste paste_size bytes to affected_item_num-th item.
9768c2ecf20Sopenharmony_ci * When item is a directory, this only prepare space for new entries
9778c2ecf20Sopenharmony_ci */
9788c2ecf20Sopenharmony_civoid leaf_paste_in_buffer(struct buffer_info *bi, int affected_item_num,
9798c2ecf20Sopenharmony_ci			  int pos_in_item, int paste_size,
9808c2ecf20Sopenharmony_ci			  const char *body, int zeros_number)
9818c2ecf20Sopenharmony_ci{
9828c2ecf20Sopenharmony_ci	struct buffer_head *bh = bi->bi_bh;
9838c2ecf20Sopenharmony_ci	int nr, free_space;
9848c2ecf20Sopenharmony_ci	struct block_head *blkh;
9858c2ecf20Sopenharmony_ci	struct item_head *ih;
9868c2ecf20Sopenharmony_ci	int i;
9878c2ecf20Sopenharmony_ci	int last_loc, unmoved_loc;
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci	blkh = B_BLK_HEAD(bh);
9908c2ecf20Sopenharmony_ci	nr = blkh_nr_item(blkh);
9918c2ecf20Sopenharmony_ci	free_space = blkh_free_space(blkh);
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci	/* check free space */
9948c2ecf20Sopenharmony_ci	RFALSE(free_space < paste_size,
9958c2ecf20Sopenharmony_ci	       "vs-10175: not enough free space: needed %d, available %d",
9968c2ecf20Sopenharmony_ci	       paste_size, free_space);
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci#ifdef CONFIG_REISERFS_CHECK
9998c2ecf20Sopenharmony_ci	if (zeros_number > paste_size) {
10008c2ecf20Sopenharmony_ci		struct super_block *sb = NULL;
10018c2ecf20Sopenharmony_ci		if (bi && bi->tb)
10028c2ecf20Sopenharmony_ci			sb = bi->tb->tb_sb;
10038c2ecf20Sopenharmony_ci		print_cur_tb("10177");
10048c2ecf20Sopenharmony_ci		reiserfs_panic(sb, "vs-10177",
10058c2ecf20Sopenharmony_ci			       "zeros_number == %d, paste_size == %d",
10068c2ecf20Sopenharmony_ci			       zeros_number, paste_size);
10078c2ecf20Sopenharmony_ci	}
10088c2ecf20Sopenharmony_ci#endif				/* CONFIG_REISERFS_CHECK */
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci	/* item to be appended */
10118c2ecf20Sopenharmony_ci	ih = item_head(bh, affected_item_num);
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci	last_loc = ih_location(&ih[nr - affected_item_num - 1]);
10148c2ecf20Sopenharmony_ci	unmoved_loc = affected_item_num ? ih_location(ih - 1) : bh->b_size;
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci	/* prepare space */
10178c2ecf20Sopenharmony_ci	memmove(bh->b_data + last_loc - paste_size, bh->b_data + last_loc,
10188c2ecf20Sopenharmony_ci		unmoved_loc - last_loc);
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_ci	/* change locations */
10218c2ecf20Sopenharmony_ci	for (i = affected_item_num; i < nr; i++)
10228c2ecf20Sopenharmony_ci		put_ih_location(&ih[i - affected_item_num],
10238c2ecf20Sopenharmony_ci				ih_location(&ih[i - affected_item_num]) -
10248c2ecf20Sopenharmony_ci				paste_size);
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci	if (body) {
10278c2ecf20Sopenharmony_ci		if (!is_direntry_le_ih(ih)) {
10288c2ecf20Sopenharmony_ci			if (!pos_in_item) {
10298c2ecf20Sopenharmony_ci				/* shift data to right */
10308c2ecf20Sopenharmony_ci				memmove(bh->b_data + ih_location(ih) +
10318c2ecf20Sopenharmony_ci					paste_size,
10328c2ecf20Sopenharmony_ci					bh->b_data + ih_location(ih),
10338c2ecf20Sopenharmony_ci					ih_item_len(ih));
10348c2ecf20Sopenharmony_ci				/* paste data in the head of item */
10358c2ecf20Sopenharmony_ci				memset(bh->b_data + ih_location(ih), 0,
10368c2ecf20Sopenharmony_ci				       zeros_number);
10378c2ecf20Sopenharmony_ci				memcpy(bh->b_data + ih_location(ih) +
10388c2ecf20Sopenharmony_ci				       zeros_number, body,
10398c2ecf20Sopenharmony_ci				       paste_size - zeros_number);
10408c2ecf20Sopenharmony_ci			} else {
10418c2ecf20Sopenharmony_ci				memset(bh->b_data + unmoved_loc - paste_size, 0,
10428c2ecf20Sopenharmony_ci				       zeros_number);
10438c2ecf20Sopenharmony_ci				memcpy(bh->b_data + unmoved_loc - paste_size +
10448c2ecf20Sopenharmony_ci				       zeros_number, body,
10458c2ecf20Sopenharmony_ci				       paste_size - zeros_number);
10468c2ecf20Sopenharmony_ci			}
10478c2ecf20Sopenharmony_ci		}
10488c2ecf20Sopenharmony_ci	} else
10498c2ecf20Sopenharmony_ci		memset(bh->b_data + unmoved_loc - paste_size, '\0', paste_size);
10508c2ecf20Sopenharmony_ci
10518c2ecf20Sopenharmony_ci	put_ih_item_len(ih, ih_item_len(ih) + paste_size);
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	/* change free space */
10548c2ecf20Sopenharmony_ci	set_blkh_free_space(blkh, free_space - paste_size);
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci	do_balance_mark_leaf_dirty(bi->tb, bh, 0);
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci	if (bi->bi_parent) {
10598c2ecf20Sopenharmony_ci		struct disk_child *t_dc =
10608c2ecf20Sopenharmony_ci		    B_N_CHILD(bi->bi_parent, bi->bi_position);
10618c2ecf20Sopenharmony_ci		put_dc_size(t_dc, dc_size(t_dc) + paste_size);
10628c2ecf20Sopenharmony_ci		do_balance_mark_internal_dirty(bi->tb, bi->bi_parent, 0);
10638c2ecf20Sopenharmony_ci	}
10648c2ecf20Sopenharmony_ci}
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci/*
10678c2ecf20Sopenharmony_ci * cuts DEL_COUNT entries beginning from FROM-th entry. Directory item
10688c2ecf20Sopenharmony_ci * does not have free space, so it moves DEHs and remaining records as
10698c2ecf20Sopenharmony_ci * necessary. Return value is size of removed part of directory item
10708c2ecf20Sopenharmony_ci * in bytes.
10718c2ecf20Sopenharmony_ci */
10728c2ecf20Sopenharmony_cistatic int leaf_cut_entries(struct buffer_head *bh,
10738c2ecf20Sopenharmony_ci			    struct item_head *ih, int from, int del_count)
10748c2ecf20Sopenharmony_ci{
10758c2ecf20Sopenharmony_ci	char *item;
10768c2ecf20Sopenharmony_ci	struct reiserfs_de_head *deh;
10778c2ecf20Sopenharmony_ci	int prev_record_offset;	/* offset of record, that is (from-1)th */
10788c2ecf20Sopenharmony_ci	char *prev_record;	/* */
10798c2ecf20Sopenharmony_ci	int cut_records_len;	/* length of all removed records */
10808c2ecf20Sopenharmony_ci	int i;
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	/*
10838c2ecf20Sopenharmony_ci	 * make sure that item is directory and there are enough entries to
10848c2ecf20Sopenharmony_ci	 * remove
10858c2ecf20Sopenharmony_ci	 */
10868c2ecf20Sopenharmony_ci	RFALSE(!is_direntry_le_ih(ih), "10180: item is not directory item");
10878c2ecf20Sopenharmony_ci	RFALSE(ih_entry_count(ih) < from + del_count,
10888c2ecf20Sopenharmony_ci	       "10185: item contains not enough entries: entry_count = %d, from = %d, to delete = %d",
10898c2ecf20Sopenharmony_ci	       ih_entry_count(ih), from, del_count);
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci	if (del_count == 0)
10928c2ecf20Sopenharmony_ci		return 0;
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci	/* first byte of item */
10958c2ecf20Sopenharmony_ci	item = bh->b_data + ih_location(ih);
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_ci	/* entry head array */
10988c2ecf20Sopenharmony_ci	deh = B_I_DEH(bh, ih);
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	/*
11018c2ecf20Sopenharmony_ci	 * first byte of remaining entries, those are BEFORE cut entries
11028c2ecf20Sopenharmony_ci	 * (prev_record) and length of all removed records (cut_records_len)
11038c2ecf20Sopenharmony_ci	 */
11048c2ecf20Sopenharmony_ci	prev_record_offset =
11058c2ecf20Sopenharmony_ci	    (from ? deh_location(&deh[from - 1]) : ih_item_len(ih));
11068c2ecf20Sopenharmony_ci	cut_records_len = prev_record_offset /*from_record */  -
11078c2ecf20Sopenharmony_ci	    deh_location(&deh[from + del_count - 1]);
11088c2ecf20Sopenharmony_ci	prev_record = item + prev_record_offset;
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	/* adjust locations of remaining entries */
11118c2ecf20Sopenharmony_ci	for (i = ih_entry_count(ih) - 1; i > from + del_count - 1; i--)
11128c2ecf20Sopenharmony_ci		put_deh_location(&deh[i],
11138c2ecf20Sopenharmony_ci				 deh_location(&deh[i]) -
11148c2ecf20Sopenharmony_ci				 (DEH_SIZE * del_count));
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ci	for (i = 0; i < from; i++)
11178c2ecf20Sopenharmony_ci		put_deh_location(&deh[i],
11188c2ecf20Sopenharmony_ci				 deh_location(&deh[i]) - (DEH_SIZE * del_count +
11198c2ecf20Sopenharmony_ci							  cut_records_len));
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci	put_ih_entry_count(ih, ih_entry_count(ih) - del_count);
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci	/* shift entry head array and entries those are AFTER removed entries */
11248c2ecf20Sopenharmony_ci	memmove((char *)(deh + from),
11258c2ecf20Sopenharmony_ci		deh + from + del_count,
11268c2ecf20Sopenharmony_ci		prev_record - cut_records_len - (char *)(deh + from +
11278c2ecf20Sopenharmony_ci							 del_count));
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	/* shift records, those are BEFORE removed entries */
11308c2ecf20Sopenharmony_ci	memmove(prev_record - cut_records_len - DEH_SIZE * del_count,
11318c2ecf20Sopenharmony_ci		prev_record, item + ih_item_len(ih) - prev_record);
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	return DEH_SIZE * del_count + cut_records_len;
11348c2ecf20Sopenharmony_ci}
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_ci/*
11378c2ecf20Sopenharmony_ci * when cut item is part of regular file
11388c2ecf20Sopenharmony_ci *      pos_in_item - first byte that must be cut
11398c2ecf20Sopenharmony_ci *      cut_size - number of bytes to be cut beginning from pos_in_item
11408c2ecf20Sopenharmony_ci *
11418c2ecf20Sopenharmony_ci * when cut item is part of directory
11428c2ecf20Sopenharmony_ci *      pos_in_item - number of first deleted entry
11438c2ecf20Sopenharmony_ci *      cut_size - count of deleted entries
11448c2ecf20Sopenharmony_ci */
11458c2ecf20Sopenharmony_civoid leaf_cut_from_buffer(struct buffer_info *bi, int cut_item_num,
11468c2ecf20Sopenharmony_ci			  int pos_in_item, int cut_size)
11478c2ecf20Sopenharmony_ci{
11488c2ecf20Sopenharmony_ci	int nr;
11498c2ecf20Sopenharmony_ci	struct buffer_head *bh = bi->bi_bh;
11508c2ecf20Sopenharmony_ci	struct block_head *blkh;
11518c2ecf20Sopenharmony_ci	struct item_head *ih;
11528c2ecf20Sopenharmony_ci	int last_loc, unmoved_loc;
11538c2ecf20Sopenharmony_ci	int i;
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	blkh = B_BLK_HEAD(bh);
11568c2ecf20Sopenharmony_ci	nr = blkh_nr_item(blkh);
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	/* item head of truncated item */
11598c2ecf20Sopenharmony_ci	ih = item_head(bh, cut_item_num);
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	if (is_direntry_le_ih(ih)) {
11628c2ecf20Sopenharmony_ci		/* first cut entry () */
11638c2ecf20Sopenharmony_ci		cut_size = leaf_cut_entries(bh, ih, pos_in_item, cut_size);
11648c2ecf20Sopenharmony_ci		if (pos_in_item == 0) {
11658c2ecf20Sopenharmony_ci			/* change key */
11668c2ecf20Sopenharmony_ci			RFALSE(cut_item_num,
11678c2ecf20Sopenharmony_ci			       "when 0-th enrty of item is cut, that item must be first in the node, not %d-th",
11688c2ecf20Sopenharmony_ci			       cut_item_num);
11698c2ecf20Sopenharmony_ci			/* change item key by key of first entry in the item */
11708c2ecf20Sopenharmony_ci			set_le_ih_k_offset(ih, deh_offset(B_I_DEH(bh, ih)));
11718c2ecf20Sopenharmony_ci		}
11728c2ecf20Sopenharmony_ci	} else {
11738c2ecf20Sopenharmony_ci		/* item is direct or indirect */
11748c2ecf20Sopenharmony_ci		RFALSE(is_statdata_le_ih(ih), "10195: item is stat data");
11758c2ecf20Sopenharmony_ci		RFALSE(pos_in_item && pos_in_item + cut_size != ih_item_len(ih),
11768c2ecf20Sopenharmony_ci		       "10200: invalid offset (%lu) or trunc_size (%lu) or ih_item_len (%lu)",
11778c2ecf20Sopenharmony_ci		       (long unsigned)pos_in_item, (long unsigned)cut_size,
11788c2ecf20Sopenharmony_ci		       (long unsigned)ih_item_len(ih));
11798c2ecf20Sopenharmony_ci
11808c2ecf20Sopenharmony_ci		/* shift item body to left if cut is from the head of item */
11818c2ecf20Sopenharmony_ci		if (pos_in_item == 0) {
11828c2ecf20Sopenharmony_ci			memmove(bh->b_data + ih_location(ih),
11838c2ecf20Sopenharmony_ci				bh->b_data + ih_location(ih) + cut_size,
11848c2ecf20Sopenharmony_ci				ih_item_len(ih) - cut_size);
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ci			/* change key of item */
11878c2ecf20Sopenharmony_ci			if (is_direct_le_ih(ih))
11888c2ecf20Sopenharmony_ci				set_le_ih_k_offset(ih,
11898c2ecf20Sopenharmony_ci						   le_ih_k_offset(ih) +
11908c2ecf20Sopenharmony_ci						   cut_size);
11918c2ecf20Sopenharmony_ci			else {
11928c2ecf20Sopenharmony_ci				set_le_ih_k_offset(ih,
11938c2ecf20Sopenharmony_ci						   le_ih_k_offset(ih) +
11948c2ecf20Sopenharmony_ci						   (cut_size / UNFM_P_SIZE) *
11958c2ecf20Sopenharmony_ci						   bh->b_size);
11968c2ecf20Sopenharmony_ci				RFALSE(ih_item_len(ih) == cut_size
11978c2ecf20Sopenharmony_ci				       && get_ih_free_space(ih),
11988c2ecf20Sopenharmony_ci				       "10205: invalid ih_free_space (%h)", ih);
11998c2ecf20Sopenharmony_ci			}
12008c2ecf20Sopenharmony_ci		}
12018c2ecf20Sopenharmony_ci	}
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	/* location of the last item */
12048c2ecf20Sopenharmony_ci	last_loc = ih_location(&ih[nr - cut_item_num - 1]);
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_ci	/* location of the item, which is remaining at the same place */
12078c2ecf20Sopenharmony_ci	unmoved_loc = cut_item_num ? ih_location(ih - 1) : bh->b_size;
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_ci	/* shift */
12108c2ecf20Sopenharmony_ci	memmove(bh->b_data + last_loc + cut_size, bh->b_data + last_loc,
12118c2ecf20Sopenharmony_ci		unmoved_loc - last_loc - cut_size);
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci	/* change item length */
12148c2ecf20Sopenharmony_ci	put_ih_item_len(ih, ih_item_len(ih) - cut_size);
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_ci	if (is_indirect_le_ih(ih)) {
12178c2ecf20Sopenharmony_ci		if (pos_in_item)
12188c2ecf20Sopenharmony_ci			set_ih_free_space(ih, 0);
12198c2ecf20Sopenharmony_ci	}
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_ci	/* change locations */
12228c2ecf20Sopenharmony_ci	for (i = cut_item_num; i < nr; i++)
12238c2ecf20Sopenharmony_ci		put_ih_location(&ih[i - cut_item_num],
12248c2ecf20Sopenharmony_ci				ih_location(&ih[i - cut_item_num]) + cut_size);
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	/* size, free space */
12278c2ecf20Sopenharmony_ci	set_blkh_free_space(blkh, blkh_free_space(blkh) + cut_size);
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_ci	do_balance_mark_leaf_dirty(bi->tb, bh, 0);
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_ci	if (bi->bi_parent) {
12328c2ecf20Sopenharmony_ci		struct disk_child *t_dc;
12338c2ecf20Sopenharmony_ci		t_dc = B_N_CHILD(bi->bi_parent, bi->bi_position);
12348c2ecf20Sopenharmony_ci		put_dc_size(t_dc, dc_size(t_dc) - cut_size);
12358c2ecf20Sopenharmony_ci		do_balance_mark_internal_dirty(bi->tb, bi->bi_parent, 0);
12368c2ecf20Sopenharmony_ci	}
12378c2ecf20Sopenharmony_ci}
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci/* delete del_num items from buffer starting from the first'th item */
12408c2ecf20Sopenharmony_cistatic void leaf_delete_items_entirely(struct buffer_info *bi,
12418c2ecf20Sopenharmony_ci				       int first, int del_num)
12428c2ecf20Sopenharmony_ci{
12438c2ecf20Sopenharmony_ci	struct buffer_head *bh = bi->bi_bh;
12448c2ecf20Sopenharmony_ci	int nr;
12458c2ecf20Sopenharmony_ci	int i, j;
12468c2ecf20Sopenharmony_ci	int last_loc, last_removed_loc;
12478c2ecf20Sopenharmony_ci	struct block_head *blkh;
12488c2ecf20Sopenharmony_ci	struct item_head *ih;
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci	RFALSE(bh == NULL, "10210: buffer is 0");
12518c2ecf20Sopenharmony_ci	RFALSE(del_num < 0, "10215: del_num less than 0 (%d)", del_num);
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	if (del_num == 0)
12548c2ecf20Sopenharmony_ci		return;
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci	blkh = B_BLK_HEAD(bh);
12578c2ecf20Sopenharmony_ci	nr = blkh_nr_item(blkh);
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	RFALSE(first < 0 || first + del_num > nr,
12608c2ecf20Sopenharmony_ci	       "10220: first=%d, number=%d, there is %d items", first, del_num,
12618c2ecf20Sopenharmony_ci	       nr);
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci	if (first == 0 && del_num == nr) {
12648c2ecf20Sopenharmony_ci		/* this does not work */
12658c2ecf20Sopenharmony_ci		make_empty_node(bi);
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci		do_balance_mark_leaf_dirty(bi->tb, bh, 0);
12688c2ecf20Sopenharmony_ci		return;
12698c2ecf20Sopenharmony_ci	}
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_ci	ih = item_head(bh, first);
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci	/* location of unmovable item */
12748c2ecf20Sopenharmony_ci	j = (first == 0) ? bh->b_size : ih_location(ih - 1);
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci	/* delete items */
12778c2ecf20Sopenharmony_ci	last_loc = ih_location(&ih[nr - 1 - first]);
12788c2ecf20Sopenharmony_ci	last_removed_loc = ih_location(&ih[del_num - 1]);
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci	memmove(bh->b_data + last_loc + j - last_removed_loc,
12818c2ecf20Sopenharmony_ci		bh->b_data + last_loc, last_removed_loc - last_loc);
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_ci	/* delete item headers */
12848c2ecf20Sopenharmony_ci	memmove(ih, ih + del_num, (nr - first - del_num) * IH_SIZE);
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci	/* change item location */
12878c2ecf20Sopenharmony_ci	for (i = first; i < nr - del_num; i++)
12888c2ecf20Sopenharmony_ci		put_ih_location(&ih[i - first],
12898c2ecf20Sopenharmony_ci				ih_location(&ih[i - first]) + (j -
12908c2ecf20Sopenharmony_ci								 last_removed_loc));
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	/* sizes, item number */
12938c2ecf20Sopenharmony_ci	set_blkh_nr_item(blkh, blkh_nr_item(blkh) - del_num);
12948c2ecf20Sopenharmony_ci	set_blkh_free_space(blkh,
12958c2ecf20Sopenharmony_ci			    blkh_free_space(blkh) + (j - last_removed_loc +
12968c2ecf20Sopenharmony_ci						     IH_SIZE * del_num));
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_ci	do_balance_mark_leaf_dirty(bi->tb, bh, 0);
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci	if (bi->bi_parent) {
13018c2ecf20Sopenharmony_ci		struct disk_child *t_dc =
13028c2ecf20Sopenharmony_ci		    B_N_CHILD(bi->bi_parent, bi->bi_position);
13038c2ecf20Sopenharmony_ci		put_dc_size(t_dc,
13048c2ecf20Sopenharmony_ci			    dc_size(t_dc) - (j - last_removed_loc +
13058c2ecf20Sopenharmony_ci					     IH_SIZE * del_num));
13068c2ecf20Sopenharmony_ci		do_balance_mark_internal_dirty(bi->tb, bi->bi_parent, 0);
13078c2ecf20Sopenharmony_ci	}
13088c2ecf20Sopenharmony_ci}
13098c2ecf20Sopenharmony_ci
13108c2ecf20Sopenharmony_ci/*
13118c2ecf20Sopenharmony_ci * paste new_entry_count entries (new_dehs, records) into position
13128c2ecf20Sopenharmony_ci * before to item_num-th item
13138c2ecf20Sopenharmony_ci */
13148c2ecf20Sopenharmony_civoid leaf_paste_entries(struct buffer_info *bi,
13158c2ecf20Sopenharmony_ci			int item_num,
13168c2ecf20Sopenharmony_ci			int before,
13178c2ecf20Sopenharmony_ci			int new_entry_count,
13188c2ecf20Sopenharmony_ci			struct reiserfs_de_head *new_dehs,
13198c2ecf20Sopenharmony_ci			const char *records, int paste_size)
13208c2ecf20Sopenharmony_ci{
13218c2ecf20Sopenharmony_ci	struct item_head *ih;
13228c2ecf20Sopenharmony_ci	char *item;
13238c2ecf20Sopenharmony_ci	struct reiserfs_de_head *deh;
13248c2ecf20Sopenharmony_ci	char *insert_point;
13258c2ecf20Sopenharmony_ci	int i;
13268c2ecf20Sopenharmony_ci	struct buffer_head *bh = bi->bi_bh;
13278c2ecf20Sopenharmony_ci
13288c2ecf20Sopenharmony_ci	if (new_entry_count == 0)
13298c2ecf20Sopenharmony_ci		return;
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_ci	ih = item_head(bh, item_num);
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_ci	/*
13348c2ecf20Sopenharmony_ci	 * make sure, that item is directory, and there are enough
13358c2ecf20Sopenharmony_ci	 * records in it
13368c2ecf20Sopenharmony_ci	 */
13378c2ecf20Sopenharmony_ci	RFALSE(!is_direntry_le_ih(ih), "10225: item is not directory item");
13388c2ecf20Sopenharmony_ci	RFALSE(ih_entry_count(ih) < before,
13398c2ecf20Sopenharmony_ci	       "10230: there are no entry we paste entries before. entry_count = %d, before = %d",
13408c2ecf20Sopenharmony_ci	       ih_entry_count(ih), before);
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	/* first byte of dest item */
13438c2ecf20Sopenharmony_ci	item = bh->b_data + ih_location(ih);
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_ci	/* entry head array */
13468c2ecf20Sopenharmony_ci	deh = B_I_DEH(bh, ih);
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci	/* new records will be pasted at this point */
13498c2ecf20Sopenharmony_ci	insert_point =
13508c2ecf20Sopenharmony_ci	    item +
13518c2ecf20Sopenharmony_ci	    (before ? deh_location(&deh[before - 1])
13528c2ecf20Sopenharmony_ci	     : (ih_item_len(ih) - paste_size));
13538c2ecf20Sopenharmony_ci
13548c2ecf20Sopenharmony_ci	/* adjust locations of records that will be AFTER new records */
13558c2ecf20Sopenharmony_ci	for (i = ih_entry_count(ih) - 1; i >= before; i--)
13568c2ecf20Sopenharmony_ci		put_deh_location(&deh[i],
13578c2ecf20Sopenharmony_ci				 deh_location(&deh[i]) +
13588c2ecf20Sopenharmony_ci				 (DEH_SIZE * new_entry_count));
13598c2ecf20Sopenharmony_ci
13608c2ecf20Sopenharmony_ci	/* adjust locations of records that will be BEFORE new records */
13618c2ecf20Sopenharmony_ci	for (i = 0; i < before; i++)
13628c2ecf20Sopenharmony_ci		put_deh_location(&deh[i],
13638c2ecf20Sopenharmony_ci				 deh_location(&deh[i]) + paste_size);
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	put_ih_entry_count(ih, ih_entry_count(ih) + new_entry_count);
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_ci	/* prepare space for pasted records */
13688c2ecf20Sopenharmony_ci	memmove(insert_point + paste_size, insert_point,
13698c2ecf20Sopenharmony_ci		item + (ih_item_len(ih) - paste_size) - insert_point);
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci	/* copy new records */
13728c2ecf20Sopenharmony_ci	memcpy(insert_point + DEH_SIZE * new_entry_count, records,
13738c2ecf20Sopenharmony_ci	       paste_size - DEH_SIZE * new_entry_count);
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci	/* prepare space for new entry heads */
13768c2ecf20Sopenharmony_ci	deh += before;
13778c2ecf20Sopenharmony_ci	memmove((char *)(deh + new_entry_count), deh,
13788c2ecf20Sopenharmony_ci		insert_point - (char *)deh);
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_ci	/* copy new entry heads */
13818c2ecf20Sopenharmony_ci	deh = (struct reiserfs_de_head *)((char *)deh);
13828c2ecf20Sopenharmony_ci	memcpy(deh, new_dehs, DEH_SIZE * new_entry_count);
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci	/* set locations of new records */
13858c2ecf20Sopenharmony_ci	for (i = 0; i < new_entry_count; i++) {
13868c2ecf20Sopenharmony_ci		put_deh_location(&deh[i],
13878c2ecf20Sopenharmony_ci				 deh_location(&deh[i]) +
13888c2ecf20Sopenharmony_ci				 (-deh_location
13898c2ecf20Sopenharmony_ci				  (&new_dehs[new_entry_count - 1]) +
13908c2ecf20Sopenharmony_ci				  insert_point + DEH_SIZE * new_entry_count -
13918c2ecf20Sopenharmony_ci				  item));
13928c2ecf20Sopenharmony_ci	}
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_ci	/* change item key if necessary (when we paste before 0-th entry */
13958c2ecf20Sopenharmony_ci	if (!before) {
13968c2ecf20Sopenharmony_ci		set_le_ih_k_offset(ih, deh_offset(new_dehs));
13978c2ecf20Sopenharmony_ci	}
13988c2ecf20Sopenharmony_ci#ifdef CONFIG_REISERFS_CHECK
13998c2ecf20Sopenharmony_ci	{
14008c2ecf20Sopenharmony_ci		int prev, next;
14018c2ecf20Sopenharmony_ci		/* check record locations */
14028c2ecf20Sopenharmony_ci		deh = B_I_DEH(bh, ih);
14038c2ecf20Sopenharmony_ci		for (i = 0; i < ih_entry_count(ih); i++) {
14048c2ecf20Sopenharmony_ci			next =
14058c2ecf20Sopenharmony_ci			    (i <
14068c2ecf20Sopenharmony_ci			     ih_entry_count(ih) -
14078c2ecf20Sopenharmony_ci			     1) ? deh_location(&deh[i + 1]) : 0;
14088c2ecf20Sopenharmony_ci			prev = (i != 0) ? deh_location(&deh[i - 1]) : 0;
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_ci			if (prev && prev <= deh_location(&deh[i]))
14118c2ecf20Sopenharmony_ci				reiserfs_error(sb_from_bi(bi), "vs-10240",
14128c2ecf20Sopenharmony_ci					       "directory item (%h) "
14138c2ecf20Sopenharmony_ci					       "corrupted (prev %a, "
14148c2ecf20Sopenharmony_ci					       "cur(%d) %a)",
14158c2ecf20Sopenharmony_ci					       ih, deh + i - 1, i, deh + i);
14168c2ecf20Sopenharmony_ci			if (next && next >= deh_location(&deh[i]))
14178c2ecf20Sopenharmony_ci				reiserfs_error(sb_from_bi(bi), "vs-10250",
14188c2ecf20Sopenharmony_ci					       "directory item (%h) "
14198c2ecf20Sopenharmony_ci					       "corrupted (cur(%d) %a, "
14208c2ecf20Sopenharmony_ci					       "next %a)",
14218c2ecf20Sopenharmony_ci					       ih, i, deh + i, deh + i + 1);
14228c2ecf20Sopenharmony_ci		}
14238c2ecf20Sopenharmony_ci	}
14248c2ecf20Sopenharmony_ci#endif
14258c2ecf20Sopenharmony_ci
14268c2ecf20Sopenharmony_ci}
1427