Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  *  Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *  Written by Anatoly P. Pinchuk pap@namesys.botik.ru
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *  Programm System Institute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *  Pereslavl-Zalessky Russia
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include "reiserfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) /* Does the buffer contain a disk block which is in the tree. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) inline int B_IS_IN_TREE(const struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 	RFALSE(B_LEVEL(bh) > MAX_HEIGHT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 	       "PAP-1010: block (%b) has too big level (%z)", bh, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 	return (B_LEVEL(bh) != FREE_LEVEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) /* to get item head in le form */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) inline void copy_item_head(struct item_head *to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 			   const struct item_head *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	memcpy(to, from, IH_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  * k1 is pointer to on-disk structure which is stored in little-endian
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  * form. k2 is pointer to cpu variable. For key of items of the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  * object this returns 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  * Returns: -1 if key1 < key2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * 0 if key1 == key2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * 1 if key1 > key2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) inline int comp_short_keys(const struct reiserfs_key *le_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 			   const struct cpu_key *cpu_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	__u32 n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	n = le32_to_cpu(le_key->k_dir_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	if (n < cpu_key->on_disk_key.k_dir_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	if (n > cpu_key->on_disk_key.k_dir_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	n = le32_to_cpu(le_key->k_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	if (n < cpu_key->on_disk_key.k_objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	if (n > cpu_key->on_disk_key.k_objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  * k1 is pointer to on-disk structure which is stored in little-endian
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  * form. k2 is pointer to cpu variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  * Compare keys using all 4 key fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  * Returns: -1 if key1 < key2 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66)  * if key1 = key2 1 if key1 > key2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) static inline int comp_keys(const struct reiserfs_key *le_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 			    const struct cpu_key *cpu_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	retval = comp_short_keys(le_key, cpu_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	if (le_key_k_offset(le_key_version(le_key), le_key) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	    cpu_key_k_offset(cpu_key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	if (le_key_k_offset(le_key_version(le_key), le_key) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	    cpu_key_k_offset(cpu_key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	if (cpu_key->key_length == 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	/* this part is needed only when tail conversion is in progress */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	if (le_key_k_type(le_key_version(le_key), le_key) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	    cpu_key_k_type(cpu_key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	if (le_key_k_type(le_key_version(le_key), le_key) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	    cpu_key_k_type(cpu_key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) inline int comp_short_le_keys(const struct reiserfs_key *key1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 			      const struct reiserfs_key *key2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	__u32 *k1_u32, *k2_u32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	int key_length = REISERFS_SHORT_KEY_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	k1_u32 = (__u32 *) key1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	k2_u32 = (__u32 *) key2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	for (; key_length--; ++k1_u32, ++k2_u32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 		if (le32_to_cpu(*k1_u32) < le32_to_cpu(*k2_u32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 		if (le32_to_cpu(*k1_u32) > le32_to_cpu(*k2_u32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) inline void le_key2cpu_key(struct cpu_key *to, const struct reiserfs_key *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	int version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	to->on_disk_key.k_dir_id = le32_to_cpu(from->k_dir_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	to->on_disk_key.k_objectid = le32_to_cpu(from->k_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	/* find out version of the key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	version = le_key_version(from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	to->version = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	to->on_disk_key.k_offset = le_key_k_offset(version, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	to->on_disk_key.k_type = le_key_k_type(version, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129)  * this does not say which one is bigger, it only returns 1 if keys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130)  * are not equal, 0 otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) inline int comp_le_keys(const struct reiserfs_key *k1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 			const struct reiserfs_key *k2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	return memcmp(k1, k2, sizeof(struct reiserfs_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) /**************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139)  *  Binary search toolkit function                                        *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140)  *  Search for an item in the array by the item key                       *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141)  *  Returns:    1 if found,  0 if not found;                              *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142)  *        *pos = number of the searched element if found, else the        *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143)  *        number of the first element that is larger than key.            *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144)  **************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146)  * For those not familiar with binary search: lbound is the leftmost item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147)  * that it could be, rbound the rightmost item that it could be.  We examine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148)  * the item halfway between lbound and rbound, and that tells us either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149)  * that we can increase lbound, or decrease rbound, or that we have found it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150)  * or if lbound <= rbound that there are no possible items, and we have not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151)  * found it. With each examination we cut the number of possible items it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152)  * could be by one more than half rounded down, or we find it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) static inline int bin_search(const void *key,	/* Key to search for. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 			     const void *base,	/* First item in the array. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 			     int num,	/* Number of items in the array. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 			     /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 			      * Item size in the array.  searched. Lest the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 			      * reader be confused, note that this is crafted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 			      * as a general function, and when it is applied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 			      * specifically to the array of item headers in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 			      * node, width is actually the item header size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 			      * not the item size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 			      */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 			     int width,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 			     int *pos /* Number of the searched for element. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)     )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	int rbound, lbound, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	for (j = ((rbound = num - 1) + (lbound = 0)) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	     lbound <= rbound; j = (rbound + lbound) / 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		switch (comp_keys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 			((struct reiserfs_key *)((char *)base + j * width),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 			 (struct cpu_key *)key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 		case -1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 			lbound = j + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 			rbound = j - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 			*pos = j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 			return ITEM_FOUND;	/* Key found in the array.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	 * bin_search did not find given key, it returns position of key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	 * that is minimal and greater than the given one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	*pos = lbound;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	return ITEM_NOT_FOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) /* Minimal possible key. It is never in the tree. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) const struct reiserfs_key MIN_KEY = { 0, 0, {{0, 0},} };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) /* Maximal possible key. It is never in the tree. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) static const struct reiserfs_key MAX_KEY = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	cpu_to_le32(0xffffffff),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	cpu_to_le32(0xffffffff),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	{{cpu_to_le32(0xffffffff),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	  cpu_to_le32(0xffffffff)},}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208)  * Get delimiting key of the buffer by looking for it in the buffers in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209)  * path, starting from the bottom of the path, and going upwards.  We must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210)  * check the path's validity at each step.  If the key is not in the path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211)  * there is no delimiting key in the tree (buffer is first or last buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212)  * in tree), and in this case we return a special key, either MIN_KEY or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  * MAX_KEY.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) static inline const struct reiserfs_key *get_lkey(const struct treepath *chk_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 						  const struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	int position, path_offset = chk_path->path_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	struct buffer_head *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	       "PAP-5010: invalid offset in the path");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	/* While not higher in path than first element. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	while (path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		RFALSE(!buffer_uptodate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		       (PATH_OFFSET_PBUFFER(chk_path, path_offset)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		       "PAP-5020: parent is not uptodate");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		/* Parent at the path is not in the tree now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		if (!B_IS_IN_TREE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		    (parent =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		     PATH_OFFSET_PBUFFER(chk_path, path_offset)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 			return &MAX_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		/* Check whether position in the parent is correct. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		if ((position =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		     PATH_OFFSET_POSITION(chk_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 					  path_offset)) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		    B_NR_ITEMS(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 			return &MAX_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		/* Check whether parent at the path really points to the child. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		if (B_N_CHILD_NUM(parent, position) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 		    PATH_OFFSET_PBUFFER(chk_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 					path_offset + 1)->b_blocknr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 			return &MAX_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		 * Return delimiting key if position in the parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		 * is not equal to zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		if (position)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 			return internal_key(parent, position - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	/* Return MIN_KEY if we are in the root of the buffer tree. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	if (PATH_OFFSET_PBUFFER(chk_path, FIRST_PATH_ELEMENT_OFFSET)->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	    b_blocknr == SB_ROOT_BLOCK(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		return &MIN_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	return &MAX_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) /* Get delimiting key of the buffer at the path and its right neighbor. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) inline const struct reiserfs_key *get_rkey(const struct treepath *chk_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 					   const struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	int position, path_offset = chk_path->path_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	struct buffer_head *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	       "PAP-5030: invalid offset in the path");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	while (path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		RFALSE(!buffer_uptodate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		       (PATH_OFFSET_PBUFFER(chk_path, path_offset)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		       "PAP-5040: parent is not uptodate");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		/* Parent at the path is not in the tree now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		if (!B_IS_IN_TREE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		    (parent =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		     PATH_OFFSET_PBUFFER(chk_path, path_offset)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 			return &MIN_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		/* Check whether position in the parent is correct. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		if ((position =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		     PATH_OFFSET_POSITION(chk_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 					  path_offset)) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		    B_NR_ITEMS(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 			return &MIN_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		 * Check whether parent at the path really points
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		 * to the child.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		if (B_N_CHILD_NUM(parent, position) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		    PATH_OFFSET_PBUFFER(chk_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 					path_offset + 1)->b_blocknr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 			return &MIN_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		 * Return delimiting key if position in the parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		 * is not the last one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 		if (position != B_NR_ITEMS(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 			return internal_key(parent, position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	/* Return MAX_KEY if we are in the root of the buffer tree. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	if (PATH_OFFSET_PBUFFER(chk_path, FIRST_PATH_ELEMENT_OFFSET)->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	    b_blocknr == SB_ROOT_BLOCK(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		return &MAX_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	return &MIN_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313)  * Check whether a key is contained in the tree rooted from a buffer at a path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314)  * This works by looking at the left and right delimiting keys for the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315)  * in the last path_element in the path.  These delimiting keys are stored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316)  * at least one level above that buffer in the tree. If the buffer is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317)  * first or last node in the tree order then one of the delimiting keys may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318)  * be absent, and in this case get_lkey and get_rkey return a special key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319)  * which is MIN_KEY or MAX_KEY.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) static inline int key_in_buffer(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 				/* Path which should be checked. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 				struct treepath *chk_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 				/* Key which should be checked. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 				const struct cpu_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 				struct super_block *sb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327)     )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	RFALSE(!key || chk_path->path_length < FIRST_PATH_ELEMENT_OFFSET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	       || chk_path->path_length > MAX_HEIGHT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	       "PAP-5050: pointer to the key(%p) is NULL or invalid path length(%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	       key, chk_path->path_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	RFALSE(!PATH_PLAST_BUFFER(chk_path)->b_bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	       "PAP-5060: device must not be NODEV");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	if (comp_keys(get_lkey(chk_path, sb), key) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		/* left delimiting key is bigger, that the key we look for */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	/*  if ( comp_keys(key, get_rkey(chk_path, sb)) != -1 ) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	if (comp_keys(get_rkey(chk_path, sb), key) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		/* key must be less than right delimitiing key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) int reiserfs_check_path(struct treepath *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	RFALSE(p->path_length != ILLEGAL_PATH_ELEMENT_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	       "path not properly relsed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355)  * Drop the reference to each buffer in a path and restore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356)  * dirty bits clean when preparing the buffer for the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357)  * This version should only be called from fix_nodes()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) void pathrelse_and_restore(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 			   struct treepath *search_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	int path_offset = search_path->path_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	RFALSE(path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	       "clm-4000: invalid path offset");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	while (path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		bh = PATH_OFFSET_PBUFFER(search_path, path_offset--);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		reiserfs_restore_prepared_buffer(sb, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) /* Drop the reference to each buffer in a path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) void pathrelse(struct treepath *search_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	int path_offset = search_path->path_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	RFALSE(path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	       "PAP-5090: invalid path offset");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	while (path_offset > ILLEGAL_PATH_ELEMENT_OFFSET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		brelse(PATH_OFFSET_PBUFFER(search_path, path_offset--));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) static int has_valid_deh_location(struct buffer_head *bh, struct item_head *ih)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	struct reiserfs_de_head *deh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	deh = B_I_DEH(bh, ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	for (i = 0; i < ih_entry_count(ih); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		if (deh_location(&deh[i]) > ih_item_len(ih)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 			reiserfs_warning(NULL, "reiserfs-5094",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 					 "directory entry location seems wrong %h",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 					 &deh[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) static int is_leaf(char *buf, int blocksize, struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	struct block_head *blkh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	struct item_head *ih;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	int used_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	int prev_location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	blkh = (struct block_head *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	if (blkh_level(blkh) != DISK_LEAF_NODE_LEVEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		reiserfs_warning(NULL, "reiserfs-5080",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 				 "this should be caught earlier");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	nr = blkh_nr_item(blkh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	if (nr < 1 || nr > ((blocksize - BLKH_SIZE) / (IH_SIZE + MIN_ITEM_LEN))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		/* item number is too big or too small */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		reiserfs_warning(NULL, "reiserfs-5081",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 				 "nr_item seems wrong: %z", bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	ih = (struct item_head *)(buf + BLKH_SIZE) + nr - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	used_space = BLKH_SIZE + IH_SIZE * nr + (blocksize - ih_location(ih));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	/* free space does not match to calculated amount of use space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	if (used_space != blocksize - blkh_free_space(blkh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		reiserfs_warning(NULL, "reiserfs-5082",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 				 "free space seems wrong: %z", bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	 * FIXME: it is_leaf will hit performance too much - we may have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	 * return 1 here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	/* check tables of item heads */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	ih = (struct item_head *)(buf + BLKH_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	prev_location = blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	for (i = 0; i < nr; i++, ih++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		if (le_ih_k_type(ih) == TYPE_ANY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 			reiserfs_warning(NULL, "reiserfs-5083",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 					 "wrong item type for item %h",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 					 ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		if (ih_location(ih) >= blocksize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		    || ih_location(ih) < IH_SIZE * nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 			reiserfs_warning(NULL, "reiserfs-5084",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 					 "item location seems wrong: %h",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 					 ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		if (ih_item_len(ih) < 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		    || ih_item_len(ih) > MAX_ITEM_LEN(blocksize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 			reiserfs_warning(NULL, "reiserfs-5085",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 					 "item length seems wrong: %h",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 					 ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		if (prev_location - ih_location(ih) != ih_item_len(ih)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 			reiserfs_warning(NULL, "reiserfs-5086",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 					 "item location seems wrong "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 					 "(second one): %h", ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		if (is_direntry_le_ih(ih)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 			if (ih_item_len(ih) < (ih_entry_count(ih) * IH_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 				reiserfs_warning(NULL, "reiserfs-5093",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 						 "item entry count seems wrong %h",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 						 ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 			return has_valid_deh_location(bh, ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		prev_location = ih_location(ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	/* one may imagine many more checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) /* returns 1 if buf looks like an internal node, 0 otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) static int is_internal(char *buf, int blocksize, struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	struct block_head *blkh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	int used_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	blkh = (struct block_head *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	nr = blkh_level(blkh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	if (nr <= DISK_LEAF_NODE_LEVEL || nr > MAX_HEIGHT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		/* this level is not possible for internal nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		reiserfs_warning(NULL, "reiserfs-5087",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 				 "this should be caught earlier");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	nr = blkh_nr_item(blkh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	/* for internal which is not root we might check min number of keys */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	if (nr > (blocksize - BLKH_SIZE - DC_SIZE) / (KEY_SIZE + DC_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		reiserfs_warning(NULL, "reiserfs-5088",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 				 "number of key seems wrong: %z", bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	used_space = BLKH_SIZE + KEY_SIZE * nr + DC_SIZE * (nr + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	if (used_space != blocksize - blkh_free_space(blkh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		reiserfs_warning(NULL, "reiserfs-5089",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 				 "free space seems wrong: %z", bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	/* one may imagine many more checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527)  * make sure that bh contains formatted node of reiserfs tree of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528)  * 'level'-th level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) static int is_tree_node(struct buffer_head *bh, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	if (B_LEVEL(bh) != level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		reiserfs_warning(NULL, "reiserfs-5090", "node level %d does "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 				 "not match to the expected one %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 				 B_LEVEL(bh), level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	if (level == DISK_LEAF_NODE_LEVEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		return is_leaf(bh->b_data, bh->b_size, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	return is_internal(bh->b_data, bh->b_size, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) #define SEARCH_BY_KEY_READA 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547)  * The function is NOT SCHEDULE-SAFE!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548)  * It might unlock the write lock if we needed to wait for a block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549)  * to be read. Note that in this case it won't recover the lock to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550)  * high contention resulting from too much lock requests, especially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551)  * the caller (search_by_key) will perform other schedule-unsafe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552)  * operations just after calling this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554)  * @return depth of lock to be restored after read completes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) static int search_by_key_reada(struct super_block *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 				struct buffer_head **bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 				b_blocknr_t *b, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	int depth = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	for (i = 0; i < num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		bh[i] = sb_getblk(s, b[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	 * We are going to read some blocks on which we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	 * have a reference. It's safe, though we might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	 * reading blocks concurrently changed if we release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	 * the lock. But it's still fine because we check later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	 * if the tree changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	for (j = 0; j < i; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		 * note, this needs attention if we are getting rid of the BKL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		 * you have to make sure the prepared bit isn't set on this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		 * buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		if (!buffer_uptodate(bh[j])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 			if (depth == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 				depth = reiserfs_write_unlock_nested(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 			ll_rw_block(REQ_OP_READ, REQ_RAHEAD, 1, bh + j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		brelse(bh[j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	return depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590)  * This function fills up the path from the root to the leaf as it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591)  * descends the tree looking for the key.  It uses reiserfs_bread to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592)  * try to find buffers in the cache given their block number.  If it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593)  * does not find them in the cache it reads them from disk.  For each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594)  * node search_by_key finds using reiserfs_bread it then uses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595)  * bin_search to look through that node.  bin_search will find the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596)  * position of the block_number of the next node if it is looking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597)  * through an internal node.  If it is looking through a leaf node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598)  * bin_search will find the position of the item which has key either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599)  * equal to given key, or which is the maximal key less than the given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600)  * key.  search_by_key returns a path that must be checked for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601)  * correctness of the top of the path but need not be checked for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602)  * correctness of the bottom of the path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605)  * search_by_key - search for key (and item) in stree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606)  * @sb: superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607)  * @key: pointer to key to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608)  * @search_path: Allocated and initialized struct treepath; Returned filled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609)  *		 on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610)  * @stop_level: How far down the tree to search, Use DISK_LEAF_NODE_LEVEL to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611)  *		stop at leaf level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613)  * The function is NOT SCHEDULE-SAFE!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) int search_by_key(struct super_block *sb, const struct cpu_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		  struct treepath *search_path, int stop_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	b_blocknr_t block_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	int expected_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	struct path_element *last_element;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	int node_level, retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	int fs_gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	struct buffer_head *reada_bh[SEARCH_BY_KEY_READA];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	b_blocknr_t reada_blocks[SEARCH_BY_KEY_READA];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	int reada_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) #ifdef CONFIG_REISERFS_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	int repeat_counter = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	PROC_INFO_INC(sb, search_by_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	 * As we add each node to a path we increase its count.  This means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	 * that we must be careful to release all nodes in a path before we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	 * either discard the path struct or re-use the path struct, as we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	 * do here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	pathrelse(search_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	 * With each iteration of this loop we search through the items in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	 * current node, and calculate the next current node(next path element)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	 * for the next iteration of this loop..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	block_number = SB_ROOT_BLOCK(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	expected_level = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) #ifdef CONFIG_REISERFS_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		if (!(++repeat_counter % 50000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 			reiserfs_warning(sb, "PAP-5100",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 					 "%s: there were %d iterations of "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 					 "while loop looking for key %K",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 					 current->comm, repeat_counter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 					 key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		/* prep path to have another element added to it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		last_element =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		    PATH_OFFSET_PELEMENT(search_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 					 ++search_path->path_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		fs_gen = get_generation(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		 * Read the next tree node, and set the last element
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		 * in the path to have a pointer to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 		if ((bh = last_element->pe_buffer =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		     sb_getblk(sb, block_number))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 			 * We'll need to drop the lock if we encounter any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 			 * buffers that need to be read. If all of them are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 			 * already up to date, we don't need to drop the lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 			int depth = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 			if (!buffer_uptodate(bh) && reada_count > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 				depth = search_by_key_reada(sb, reada_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 						    reada_blocks, reada_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 			if (!buffer_uptodate(bh) && depth == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 				depth = reiserfs_write_unlock_nested(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 			ll_rw_block(REQ_OP_READ, 0, 1, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 			wait_on_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 			if (depth != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 				reiserfs_write_lock_nested(sb, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 			if (!buffer_uptodate(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 				goto io_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) io_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 			search_path->path_length--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 			pathrelse(search_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 			return IO_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		reada_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 		if (expected_level == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 			expected_level = SB_TREE_HEIGHT(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		expected_level--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		 * It is possible that schedule occurred. We must check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		 * whether the key to search is still in the tree rooted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		 * from the current buffer. If not then repeat search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		 * from the root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 		if (fs_changed(fs_gen, sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		    (!B_IS_IN_TREE(bh) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		     B_LEVEL(bh) != expected_level ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		     !key_in_buffer(search_path, key, sb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 			PROC_INFO_INC(sb, search_by_key_fs_changed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 			PROC_INFO_INC(sb, search_by_key_restarted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 			PROC_INFO_INC(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 				      sbk_restarted[expected_level - 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 			pathrelse(search_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 			 * Get the root block number so that we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 			 * repeat the search starting from the root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 			block_number = SB_ROOT_BLOCK(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 			expected_level = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 			/* repeat search from the root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		 * only check that the key is in the buffer if key is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		 * equal to the MAX_KEY. Latter case is only possible in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		 * "finish_unfinished()" processing during mount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		RFALSE(comp_keys(&MAX_KEY, key) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		       !key_in_buffer(search_path, key, sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		       "PAP-5130: key is not in the buffer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) #ifdef CONFIG_REISERFS_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		if (REISERFS_SB(sb)->cur_tb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 			print_cur_tb("5140");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 			reiserfs_panic(sb, "PAP-5140",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 				       "schedule occurred in do_balance!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		 * make sure, that the node contents look like a node of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		 * certain level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		if (!is_tree_node(bh, expected_level)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 			reiserfs_error(sb, "vs-5150",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 				       "invalid format found in block %ld. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 				       "Fsck?", bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 			pathrelse(search_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 			return IO_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		/* ok, we have acquired next formatted node in the tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		node_level = B_LEVEL(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		PROC_INFO_BH_STAT(sb, bh, node_level - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		RFALSE(node_level < stop_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		       "vs-5152: tree level (%d) is less than stop level (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		       node_level, stop_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		retval = bin_search(key, item_head(bh, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 				      B_NR_ITEMS(bh),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 				      (node_level ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 				       DISK_LEAF_NODE_LEVEL) ? IH_SIZE :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 				      KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 				      &last_element->pe_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		if (node_level == stop_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		/* we are not in the stop level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		 * item has been found, so we choose the pointer which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		 * is to the right of the found one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		if (retval == ITEM_FOUND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 			last_element->pe_position++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		 * if item was not found we choose the position which is to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		 * the left of the found item. This requires no code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		 * bin_search did it already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		 * So we have chosen a position in the current node which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		 * an internal node.  Now we calculate child block number by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		 * position in the node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		block_number =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		    B_N_CHILD_NUM(bh, last_element->pe_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		 * if we are going to read leaf nodes, try for read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		 * ahead as well
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		if ((search_path->reada & PATH_READA) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		    node_level == DISK_LEAF_NODE_LEVEL + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 			int pos = last_element->pe_position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 			int limit = B_NR_ITEMS(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 			struct reiserfs_key *le_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 			if (search_path->reada & PATH_READA_BACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 				limit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 			while (reada_count < SEARCH_BY_KEY_READA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 				if (pos == limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 				reada_blocks[reada_count++] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 				    B_N_CHILD_NUM(bh, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 				if (search_path->reada & PATH_READA_BACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 					pos--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 					pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 				 * check to make sure we're in the same object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 				le_key = internal_key(bh, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 				if (le32_to_cpu(le_key->k_objectid) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 				    key->on_disk_key.k_objectid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838)  * Form the path to an item and position in this item which contains
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839)  * file byte defined by key. If there is no such item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840)  * corresponding to the key, we point the path to the item with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841)  * maximal key less than key, and *pos_in_item is set to one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842)  * past the last entry/byte in the item.  If searching for entry in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843)  * directory item, and it is not found, *pos_in_item is set to one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844)  * entry more than the entry with maximal key which is less than the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845)  * sought key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847)  * Note that if there is no entry in this same node which is one more,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848)  * then we point to an imaginary entry.  for direct items, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849)  * position is in units of bytes, for indirect items the position is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850)  * in units of blocknr entries, for directory items the position is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851)  * units of directory entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) /* The function is NOT SCHEDULE-SAFE! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) int search_for_position_by_key(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 			       /* Key to search (cpu variable) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 			       const struct cpu_key *p_cpu_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 			       /* Filled up by this function. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 			       struct treepath *search_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	struct item_head *p_le_ih;	/* pointer to on-disk structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	int blk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	loff_t item_offset, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	struct reiserfs_dir_entry de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	/* If searching for directory entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	if (is_direntry_cpu_key(p_cpu_key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		return search_by_entry_key(sb, p_cpu_key, search_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 					   &de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	/* If not searching for directory entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	/* If item is found. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	retval = search_item(sb, p_cpu_key, search_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	if (retval == IO_ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	if (retval == ITEM_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		RFALSE(!ih_item_len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		       (item_head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 			(PATH_PLAST_BUFFER(search_path),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 			 PATH_LAST_POSITION(search_path))),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		       "PAP-5165: item length equals zero");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		pos_in_item(search_path) = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		return POSITION_FOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	RFALSE(!PATH_LAST_POSITION(search_path),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	       "PAP-5170: position equals zero");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	/* Item is not found. Set path to the previous item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	p_le_ih =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	    item_head(PATH_PLAST_BUFFER(search_path),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 			   --PATH_LAST_POSITION(search_path));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	blk_size = sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	if (comp_short_keys(&p_le_ih->ih_key, p_cpu_key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		return FILE_NOT_FOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	/* FIXME: quite ugly this far */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	item_offset = le_ih_k_offset(p_le_ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	offset = cpu_key_k_offset(p_cpu_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	/* Needed byte is contained in the item pointed to by the path. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	if (item_offset <= offset &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	    item_offset + op_bytes_number(p_le_ih, blk_size) > offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		pos_in_item(search_path) = offset - item_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		if (is_indirect_le_ih(p_le_ih)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 			pos_in_item(search_path) /= blk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		return POSITION_FOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	 * Needed byte is not contained in the item pointed to by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	 * path. Set pos_in_item out of the item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	if (is_indirect_le_ih(p_le_ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		pos_in_item(search_path) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		    ih_item_len(p_le_ih) / UNFM_P_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		pos_in_item(search_path) = ih_item_len(p_le_ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	return POSITION_NOT_FOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) /* Compare given item and item pointed to by the path. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) int comp_items(const struct item_head *stored_ih, const struct treepath *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	struct buffer_head *bh = PATH_PLAST_BUFFER(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	struct item_head *ih;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	/* Last buffer at the path is not in the tree. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	if (!B_IS_IN_TREE(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	/* Last path position is invalid. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	if (PATH_LAST_POSITION(path) >= B_NR_ITEMS(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	/* we need only to know, whether it is the same item */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	ih = tp_item_head(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	return memcmp(stored_ih, ih, IH_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) /* prepare for delete or cut of direct item */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) static inline int prepare_for_direct_item(struct treepath *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 					  struct item_head *le_ih,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 					  struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 					  loff_t new_file_length, int *cut_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	loff_t round_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	if (new_file_length == max_reiserfs_offset(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		/* item has to be deleted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		*cut_size = -(IH_SIZE + ih_item_len(le_ih));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		return M_DELETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	/* new file gets truncated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	if (get_inode_item_key_version(inode) == KEY_FORMAT_3_6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		round_len = ROUND_UP(new_file_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		/* this was new_file_length < le_ih ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		if (round_len < le_ih_k_offset(le_ih)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 			*cut_size = -(IH_SIZE + ih_item_len(le_ih));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 			return M_DELETE;	/* Delete this item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		/* Calculate first position and size for cutting from item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		pos_in_item(path) = round_len - (le_ih_k_offset(le_ih) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		*cut_size = -(ih_item_len(le_ih) - pos_in_item(path));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		return M_CUT;	/* Cut from this item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	/* old file: items may have any length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	if (new_file_length < le_ih_k_offset(le_ih)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		*cut_size = -(IH_SIZE + ih_item_len(le_ih));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		return M_DELETE;	/* Delete this item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	/* Calculate first position and size for cutting from item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	*cut_size = -(ih_item_len(le_ih) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		      (pos_in_item(path) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		       new_file_length + 1 - le_ih_k_offset(le_ih)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	return M_CUT;		/* Cut from this item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) static inline int prepare_for_direntry_item(struct treepath *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 					    struct item_head *le_ih,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 					    struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 					    loff_t new_file_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 					    int *cut_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	if (le_ih_k_offset(le_ih) == DOT_OFFSET &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	    new_file_length == max_reiserfs_offset(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		RFALSE(ih_entry_count(le_ih) != 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		       "PAP-5220: incorrect empty directory item (%h)", le_ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		*cut_size = -(IH_SIZE + ih_item_len(le_ih));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		/* Delete the directory item containing "." and ".." entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		return M_DELETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	if (ih_entry_count(le_ih) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		 * Delete the directory item such as there is one record only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		 * in this item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		*cut_size = -(IH_SIZE + ih_item_len(le_ih));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		return M_DELETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	/* Cut one record from the directory item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	*cut_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	    -(DEH_SIZE +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	      entry_length(get_last_bh(path), le_ih, pos_in_item(path)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	return M_CUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) #define JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD (2 * JOURNAL_PER_BALANCE_CNT + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)  * If the path points to a directory or direct item, calculate mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)  * and the size cut, for balance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)  * If the path points to an indirect item, remove some number of its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)  * unformatted nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)  * In case of file truncate calculate whether this item must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)  * deleted/truncated or last unformatted node of this item will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)  * converted to a direct item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)  * This function returns a determination of what balance mode the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)  * calling function should employ.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) static char prepare_for_delete_or_cut(struct reiserfs_transaction_handle *th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 				      struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 				      struct treepath *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 				      const struct cpu_key *item_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 				      /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 				       * Number of unformatted nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 				       * which were removed from end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 				       * of the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 				       */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 				      int *removed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 				      int *cut_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 				      /* MAX_KEY_OFFSET in case of delete. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 				      unsigned long long new_file_length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)     )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	struct item_head *p_le_ih = tp_item_head(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	struct buffer_head *bh = PATH_PLAST_BUFFER(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	BUG_ON(!th->t_trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	/* Stat_data item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	if (is_statdata_le_ih(p_le_ih)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		RFALSE(new_file_length != max_reiserfs_offset(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		       "PAP-5210: mode must be M_DELETE");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		*cut_size = -(IH_SIZE + ih_item_len(p_le_ih));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		return M_DELETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	/* Directory item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	if (is_direntry_le_ih(p_le_ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		return prepare_for_direntry_item(path, p_le_ih, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 						 new_file_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 						 cut_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	/* Direct item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	if (is_direct_le_ih(p_le_ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 		return prepare_for_direct_item(path, p_le_ih, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 					       new_file_length, cut_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	/* Case of an indirect item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	    int blk_size = sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	    struct item_head s_ih;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	    int need_re_search;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	    int delete = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	    int result = M_CUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	    int pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	    if ( new_file_length == max_reiserfs_offset (inode) ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		 * prepare_for_delete_or_cut() is called by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		 * reiserfs_delete_item()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		new_file_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		delete = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	    }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	    do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		need_re_search = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		*cut_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		bh = PATH_PLAST_BUFFER(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		copy_item_head(&s_ih, tp_item_head(path));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		pos = I_UNFM_NUM(&s_ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		while (le_ih_k_offset (&s_ih) + (pos - 1) * blk_size > new_file_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		    __le32 *unfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		    __u32 block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		    /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		     * Each unformatted block deletion may involve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		     * one additional bitmap block into the transaction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		     * thereby the initial journal space reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		     * might not be enough.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		     */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		    if (!delete && (*cut_size) != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 			reiserfs_transaction_free_space(th) < JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		    unfm = (__le32 *)ih_item_body(bh, &s_ih) + pos - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		    block = get_block_num(unfm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		    if (block != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 			reiserfs_prepare_for_journal(sb, bh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 			put_block_num(unfm, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 			journal_mark_dirty(th, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 			reiserfs_free_block(th, inode, block, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		    }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		    reiserfs_cond_resched(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		    if (item_moved (&s_ih, path))  {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 			need_re_search = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		    }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		    pos --;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		    (*removed)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		    (*cut_size) -= UNFM_P_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		    if (pos == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 			(*cut_size) -= IH_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 			result = M_DELETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		    }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		 * a trick.  If the buffer has been logged, this will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		 * do nothing.  If we've broken the loop without logging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		 * it, it will restore the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		reiserfs_restore_prepared_buffer(sb, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	    } while (need_re_search &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		     search_for_position_by_key(sb, item_key, path) == POSITION_FOUND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	    pos_in_item(path) = pos * UNFM_P_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	    if (*cut_size == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		 * Nothing was cut. maybe convert last unformatted node to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		 * direct item?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		result = M_CONVERT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	    }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	    return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) /* Calculate number of bytes which will be deleted or cut during balance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) static int calc_deleted_bytes_number(struct tree_balance *tb, char mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	int del_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	struct item_head *p_le_ih = tp_item_head(tb->tb_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	if (is_statdata_le_ih(p_le_ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	del_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	    (mode ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	     M_DELETE) ? ih_item_len(p_le_ih) : -tb->insert_size[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	if (is_direntry_le_ih(p_le_ih)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		 * return EMPTY_DIR_SIZE; We delete emty directories only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		 * we can't use EMPTY_DIR_SIZE, as old format dirs have a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		 * different empty size.  ick. FIXME, is this right?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		return del_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	if (is_indirect_le_ih(p_le_ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		del_size = (del_size / UNFM_P_SIZE) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 				(PATH_PLAST_BUFFER(tb->tb_path)->b_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	return del_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) static void init_tb_struct(struct reiserfs_transaction_handle *th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 			   struct tree_balance *tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 			   struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 			   struct treepath *path, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	BUG_ON(!th->t_trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	memset(tb, '\0', sizeof(struct tree_balance));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	tb->transaction_handle = th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	tb->tb_sb = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	tb->tb_path = path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	PATH_OFFSET_PBUFFER(path, ILLEGAL_PATH_ELEMENT_OFFSET) = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	PATH_OFFSET_POSITION(path, ILLEGAL_PATH_ELEMENT_OFFSET) = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	tb->insert_size[0] = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) void padd_item(char *item, int total_length, int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	for (i = total_length; i > length;)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		item[--i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) #ifdef REISERQUOTA_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) char key2type(struct reiserfs_key *ih)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	if (is_direntry_le_key(2, ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		return 'd';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	if (is_direct_le_key(2, ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		return 'D';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	if (is_indirect_le_key(2, ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		return 'i';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	if (is_statdata_le_key(2, ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 		return 's';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	return 'u';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) char head2type(struct item_head *ih)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	if (is_direntry_le_ih(ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		return 'd';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	if (is_direct_le_ih(ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		return 'D';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	if (is_indirect_le_ih(ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		return 'i';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	if (is_statdata_le_ih(ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		return 's';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	return 'u';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)  * Delete object item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)  * th       - active transaction handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)  * path     - path to the deleted item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247)  * item_key - key to search for the deleted item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)  * indode   - used for updating i_blocks and quotas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249)  * un_bh    - NULL or unformatted node pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) int reiserfs_delete_item(struct reiserfs_transaction_handle *th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 			 struct treepath *path, const struct cpu_key *item_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 			 struct inode *inode, struct buffer_head *un_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	struct tree_balance s_del_balance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	struct item_head s_ih;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	struct item_head *q_ih;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	int quota_cut_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	int ret_value, del_size, removed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) #ifdef CONFIG_REISERFS_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	char mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	int iter = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	BUG_ON(!th->t_trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	init_tb_struct(th, &s_del_balance, sb, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		       0 /*size is unknown */ );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		removed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) #ifdef CONFIG_REISERFS_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		iter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		mode =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		    prepare_for_delete_or_cut(th, inode, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 					      item_key, &removed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 					      &del_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 					      max_reiserfs_offset(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		RFALSE(mode != M_DELETE, "PAP-5320: mode must be M_DELETE");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		copy_item_head(&s_ih, tp_item_head(path));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		s_del_balance.insert_size[0] = del_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 		ret_value = fix_nodes(M_DELETE, &s_del_balance, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		if (ret_value != REPEAT_SEARCH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		PROC_INFO_INC(sb, delete_item_restarted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		/* file system changed, repeat search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		ret_value =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		    search_for_position_by_key(sb, item_key, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		if (ret_value == IO_ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		if (ret_value == FILE_NOT_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 			reiserfs_warning(sb, "vs-5340",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 					 "no items of the file %K found",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 					 item_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	}			/* while (1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	if (ret_value != CARRY_ON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		unfix_nodes(&s_del_balance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	/* reiserfs_delete_item returns item length when success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	ret_value = calc_deleted_bytes_number(&s_del_balance, M_DELETE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	q_ih = tp_item_head(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	quota_cut_bytes = ih_item_len(q_ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	 * hack so the quota code doesn't have to guess if the file has a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	 * tail.  On tail insert, we allocate quota for 1 unformatted node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	 * We test the offset because the tail might have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	 * split into multiple items, and we only want to decrement for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	 * the unfm node once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(q_ih)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		if ((le_ih_k_offset(q_ih) & (sb->s_blocksize - 1)) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 			quota_cut_bytes = sb->s_blocksize + UNFM_P_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 			quota_cut_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	if (un_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 		int off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		char *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		 * We are in direct2indirect conversion, so move tail contents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		 * to the unformatted node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		 * note, we do the copy before preparing the buffer because we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		 * don't care about the contents of the unformatted node yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		 * the only thing we really care about is the direct item's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		 * data is in the unformatted node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		 * Otherwise, we would have to call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		 * reiserfs_prepare_for_journal on the unformatted node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		 * which might schedule, meaning we'd have to loop all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		 * way back up to the start of the while loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		 * The unformatted node must be dirtied later on.  We can't be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		 * sure here if the entire tail has been deleted yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		 * un_bh is from the page cache (all unformatted nodes are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		 * from the page cache) and might be a highmem page.  So, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		 * can't use un_bh->b_data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 		 * -clm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		data = kmap_atomic(un_bh->b_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		off = ((le_ih_k_offset(&s_ih) - 1) & (PAGE_SIZE - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		memcpy(data + off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		       ih_item_body(PATH_PLAST_BUFFER(path), &s_ih),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 		       ret_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		kunmap_atomic(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	/* Perform balancing after all resources have been collected at once. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	do_balance(&s_del_balance, NULL, NULL, M_DELETE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) #ifdef REISERQUOTA_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	reiserfs_debug(sb, REISERFS_DEBUG_CODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		       "reiserquota delete_item(): freeing %u, id=%u type=%c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		       quota_cut_bytes, inode->i_uid, head2type(&s_ih));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	depth = reiserfs_write_unlock_nested(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	dquot_free_space_nodirty(inode, quota_cut_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	reiserfs_write_lock_nested(inode->i_sb, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	/* Return deleted body length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	return ret_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387)  * Summary Of Mechanisms For Handling Collisions Between Processes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389)  *  deletion of the body of the object is performed by iput(), with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390)  *  result that if multiple processes are operating on a file, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391)  *  deletion of the body of the file is deferred until the last process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392)  *  that has an open inode performs its iput().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394)  *  writes and truncates are protected from collisions by use of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395)  *  semaphores.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)  *  creates, linking, and mknod are protected from collisions with other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398)  *  processes by making the reiserfs_add_entry() the last step in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399)  *  creation, and then rolling back all changes if there was a collision.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400)  *  - Hans
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) /* this deletes item which never gets split */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) void reiserfs_delete_solid_item(struct reiserfs_transaction_handle *th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 				struct inode *inode, struct reiserfs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	struct super_block *sb = th->t_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	struct tree_balance tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	INITIALIZE_PATH(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	int item_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	int tb_init = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	struct cpu_key cpu_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	int quota_cut_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	BUG_ON(!th->t_trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	le_key2cpu_key(&cpu_key, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		retval = search_item(th->t_super, &cpu_key, &path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		if (retval == IO_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 			reiserfs_error(th->t_super, "vs-5350",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 				       "i/o failure occurred trying "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 				       "to delete %K", &cpu_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		if (retval != ITEM_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 			pathrelse(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 			 * No need for a warning, if there is just no free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 			 * space to insert '..' item into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 			 * newly-created subdir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 			if (!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 			    ((unsigned long long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 			     GET_HASH_VALUE(le_key_k_offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 					    (le_key_version(key), key)) == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 			     && (unsigned long long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 			     GET_GENERATION_NUMBER(le_key_k_offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 						   (le_key_version(key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 						    key)) == 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 				reiserfs_warning(th->t_super, "vs-5355",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 						 "%k not found", key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		if (!tb_init) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 			tb_init = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 			item_len = ih_item_len(tp_item_head(&path));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 			init_tb_struct(th, &tb, th->t_super, &path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 				       -(IH_SIZE + item_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 		quota_cut_bytes = ih_item_len(tp_item_head(&path));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 		retval = fix_nodes(M_DELETE, &tb, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		if (retval == REPEAT_SEARCH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 			PROC_INFO_INC(th->t_super, delete_solid_item_restarted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		if (retval == CARRY_ON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 			do_balance(&tb, NULL, NULL, M_DELETE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 			 * Should we count quota for item? (we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 			 * count quotas for save-links)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 			if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 				int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) #ifdef REISERQUOTA_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 				reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 					       "reiserquota delete_solid_item(): freeing %u id=%u type=%c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 					       quota_cut_bytes, inode->i_uid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 					       key2type(key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 				depth = reiserfs_write_unlock_nested(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 				dquot_free_space_nodirty(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 							 quota_cut_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 				reiserfs_write_lock_nested(sb, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		/* IO_ERROR, NO_DISK_SPACE, etc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 		reiserfs_warning(th->t_super, "vs-5360",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 				 "could not delete %K due to fix_nodes failure",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 				 &cpu_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 		unfix_nodes(&tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	reiserfs_check_path(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) int reiserfs_delete_object(struct reiserfs_transaction_handle *th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 			   struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	inode->i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	BUG_ON(!th->t_trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	/* for directory this deletes item containing "." and ".." */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	err =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	    reiserfs_do_truncate(th, inode, NULL, 0 /*no timestamp updates */ );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) #if defined( USE_INODE_GENERATION_COUNTER )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	if (!old_format_only(th->t_super)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		__le32 *inode_generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 		inode_generation =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		    &REISERFS_SB(th->t_super)->s_rs->s_inode_generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 		le32_add_cpu(inode_generation, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) /* USE_INODE_GENERATION_COUNTER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) static void unmap_buffers(struct page *page, loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	struct buffer_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	struct buffer_head *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	unsigned long tail_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	unsigned long cur_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	if (page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		if (page_has_buffers(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 			tail_index = pos & (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 			cur_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 			head = page_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 			bh = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 			do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 				next = bh->b_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 				 * we want to unmap the buffers that contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 				 * the tail, and all the buffers after it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 				 * (since the tail must be at the end of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 				 * file).  We don't want to unmap file data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 				 * before the tail, since it might be dirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 				 * and waiting to reach disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 				cur_index += bh->b_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 				if (cur_index > tail_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 					reiserfs_unmap_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 				bh = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 			} while (bh != head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) static int maybe_indirect_to_direct(struct reiserfs_transaction_handle *th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 				    struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 				    struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 				    struct treepath *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 				    const struct cpu_key *item_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 				    loff_t new_file_size, char *mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	int block_size = sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	int cut_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	BUG_ON(!th->t_trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	BUG_ON(new_file_size != inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	 * the page being sent in could be NULL if there was an i/o error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	 * reading in the last block.  The user will hit problems trying to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	 * read the file, but for now we just skip the indirect2direct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	if (atomic_read(&inode->i_count) > 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	    !tail_has_to_be_packed(inode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	    !page || (REISERFS_I(inode)->i_flags & i_nopack_mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 		/* leave tail in an unformatted node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 		*mode = M_SKIP_BALANCING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		cut_bytes =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		    block_size - (new_file_size & (block_size - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 		pathrelse(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 		return cut_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	/* Perform the conversion to a direct_item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	return indirect2direct(th, inode, page, path, item_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 			       new_file_size, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592)  * we did indirect_to_direct conversion. And we have inserted direct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593)  * item successesfully, but there were no disk space to cut unfm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594)  * pointer being converted. Therefore we have to delete inserted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595)  * direct item(s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) static void indirect_to_direct_roll_back(struct reiserfs_transaction_handle *th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 					 struct inode *inode, struct treepath *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	struct cpu_key tail_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	int tail_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	int removed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	BUG_ON(!th->t_trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	make_cpu_key(&tail_key, inode, inode->i_size + 1, TYPE_DIRECT, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	tail_key.key_length = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	tail_len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	    (cpu_key_k_offset(&tail_key) & (inode->i_sb->s_blocksize - 1)) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	while (tail_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 		/* look for the last byte of the tail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 		if (search_for_position_by_key(inode->i_sb, &tail_key, path) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 		    POSITION_NOT_FOUND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 			reiserfs_panic(inode->i_sb, "vs-5615",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 				       "found invalid item");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		RFALSE(path->pos_in_item !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 		       ih_item_len(tp_item_head(path)) - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 		       "vs-5616: appended bytes found");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 		PATH_LAST_POSITION(path)--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		removed =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 		    reiserfs_delete_item(th, path, &tail_key, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 					 NULL /*unbh not needed */ );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 		RFALSE(removed <= 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		       || removed > tail_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		       "vs-5617: there was tail %d bytes, removed item length %d bytes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		       tail_len, removed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		tail_len -= removed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		set_cpu_key_k_offset(&tail_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 				     cpu_key_k_offset(&tail_key) - removed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	reiserfs_warning(inode->i_sb, "reiserfs-5091", "indirect_to_direct "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 			 "conversion has been rolled back due to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 			 "lack of disk space");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) /* (Truncate or cut entry) or delete object item. Returns < 0 on failure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) int reiserfs_cut_from_item(struct reiserfs_transaction_handle *th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 			   struct treepath *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 			   struct cpu_key *item_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 			   struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 			   struct page *page, loff_t new_file_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	 * Every function which is going to call do_balance must first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	 * create a tree_balance structure.  Then it must fill up this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	 * structure by using the init_tb_struct and fix_nodes functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	 * After that we can make tree balancing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	struct tree_balance s_cut_balance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	struct item_head *p_le_ih;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	int cut_size = 0;	/* Amount to be cut. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	int ret_value = CARRY_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	int removed = 0;	/* Number of the removed unformatted nodes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	int is_inode_locked = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	char mode;		/* Mode of the balance. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	int retval2 = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	int quota_cut_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	loff_t tail_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	BUG_ON(!th->t_trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	init_tb_struct(th, &s_cut_balance, inode->i_sb, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 		       cut_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	 * Repeat this loop until we either cut the item without needing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	 * to balance, or we fix_nodes without schedule occurring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		 * Determine the balance mode, position of the first byte to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		 * be cut, and size to be cut.  In case of the indirect item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		 * free unformatted nodes which are pointed to by the cut
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 		 * pointers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 		mode =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 		    prepare_for_delete_or_cut(th, inode, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 					      item_key, &removed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 					      &cut_size, new_file_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 		if (mode == M_CONVERT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 			 * convert last unformatted node to direct item or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 			 * leave tail in the unformatted node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 			RFALSE(ret_value != CARRY_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 			       "PAP-5570: can not convert twice");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 			ret_value =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 			    maybe_indirect_to_direct(th, inode, page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 						     path, item_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 						     new_file_size, &mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 			if (mode == M_SKIP_BALANCING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 				/* tail has been left in the unformatted node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 				return ret_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 			is_inode_locked = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 			 * removing of last unformatted node will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 			 * change value we have to return to truncate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 			 * Save it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 			retval2 = ret_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 			 * So, we have performed the first part of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 			 * conversion:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 			 * inserting the new direct item.  Now we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 			 * removing the last unformatted node pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 			 * Set key to search for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 			set_cpu_key_k_type(item_key, TYPE_INDIRECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 			item_key->key_length = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 			new_file_size -=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 			    (new_file_size & (sb->s_blocksize - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 			tail_pos = new_file_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 			set_cpu_key_k_offset(item_key, new_file_size + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 			if (search_for_position_by_key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 			    (sb, item_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 			     path) == POSITION_NOT_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 				print_block(PATH_PLAST_BUFFER(path), 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 					    PATH_LAST_POSITION(path) - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 					    PATH_LAST_POSITION(path) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 				reiserfs_panic(sb, "PAP-5580", "item to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 					       "convert does not exist (%K)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 					       item_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		if (cut_size == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 			pathrelse(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 		s_cut_balance.insert_size[0] = cut_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 		ret_value = fix_nodes(mode, &s_cut_balance, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		if (ret_value != REPEAT_SEARCH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 		PROC_INFO_INC(sb, cut_from_item_restarted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 		ret_value =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 		    search_for_position_by_key(sb, item_key, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 		if (ret_value == POSITION_FOUND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 		reiserfs_warning(sb, "PAP-5610", "item %K not found",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 				 item_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 		unfix_nodes(&s_cut_balance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 		return (ret_value == IO_ERROR) ? -EIO : -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	}			/* while */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	/* check fix_nodes results (IO_ERROR or NO_DISK_SPACE) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	if (ret_value != CARRY_ON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 		if (is_inode_locked) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 			 * FIXME: this seems to be not needed: we are always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 			 * able to cut item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 			indirect_to_direct_roll_back(th, inode, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 		if (ret_value == NO_DISK_SPACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 			reiserfs_warning(sb, "reiserfs-5092",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 					 "NO_DISK_SPACE");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 		unfix_nodes(&s_cut_balance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	/* go ahead and perform balancing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	RFALSE(mode == M_PASTE || mode == M_INSERT, "invalid mode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	/* Calculate number of bytes that need to be cut from the item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	quota_cut_bytes =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	    (mode ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	     M_DELETE) ? ih_item_len(tp_item_head(path)) : -s_cut_balance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	    insert_size[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	if (retval2 == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 		ret_value = calc_deleted_bytes_number(&s_cut_balance, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 		ret_value = retval2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	 * For direct items, we only change the quota when deleting the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	 * item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	p_le_ih = tp_item_head(s_cut_balance.tb_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(p_le_ih)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 		if (mode == M_DELETE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 		    (le_ih_k_offset(p_le_ih) & (sb->s_blocksize - 1)) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		    1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 			/* FIXME: this is to keep 3.5 happy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 			REISERFS_I(inode)->i_first_direct_byte = U32_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 			quota_cut_bytes = sb->s_blocksize + UNFM_P_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 			quota_cut_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) #ifdef CONFIG_REISERFS_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	if (is_inode_locked) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 		struct item_head *le_ih =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 		    tp_item_head(s_cut_balance.tb_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 		 * we are going to complete indirect2direct conversion. Make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 		 * sure, that we exactly remove last unformatted node pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 		 * of the item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 		if (!is_indirect_le_ih(le_ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 			reiserfs_panic(sb, "vs-5652",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 				       "item must be indirect %h", le_ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 		if (mode == M_DELETE && ih_item_len(le_ih) != UNFM_P_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 			reiserfs_panic(sb, "vs-5653", "completing "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 				       "indirect2direct conversion indirect "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 				       "item %h being deleted must be of "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 				       "4 byte long", le_ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 		if (mode == M_CUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 		    && s_cut_balance.insert_size[0] != -UNFM_P_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 			reiserfs_panic(sb, "vs-5654", "can not complete "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 				       "indirect2direct conversion of %h "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 				       "(CUT, insert_size==%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 				       le_ih, s_cut_balance.insert_size[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 		 * it would be useful to make sure, that right neighboring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 		 * item is direct item of this file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	do_balance(&s_cut_balance, NULL, NULL, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	if (is_inode_locked) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 		 * we've done an indirect->direct conversion.  when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 		 * data block was freed, it was removed from the list of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 		 * blocks that must be flushed before the transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 		 * commits, make sure to unmap and invalidate it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 		unmap_buffers(page, tail_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 		REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) #ifdef REISERQUOTA_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 		       "reiserquota cut_from_item(): freeing %u id=%u type=%c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 		       quota_cut_bytes, inode->i_uid, '?');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	depth = reiserfs_write_unlock_nested(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	dquot_free_space_nodirty(inode, quota_cut_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	reiserfs_write_lock_nested(sb, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	return ret_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) static void truncate_directory(struct reiserfs_transaction_handle *th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 			       struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	BUG_ON(!th->t_trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	if (inode->i_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 		reiserfs_error(inode->i_sb, "vs-5655", "link count != 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), DOT_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_DIRENTRY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	reiserfs_update_sd(th, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), SD_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 	set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_STAT_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876)  * Truncate file to the new size. Note, this must be called with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877)  * transaction already started
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) int reiserfs_do_truncate(struct reiserfs_transaction_handle *th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 			 struct inode *inode,	/* ->i_size contains new size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 			 struct page *page,	/* up to date for last block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 			 /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 			  * when it is called by file_release to convert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 			  * the tail - no timestamps should be updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 			  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 			 int update_timestamps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887)     )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	INITIALIZE_PATH(s_search_path);	/* Path to the current object item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	struct item_head *p_le_ih;	/* Pointer to an item header. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	/* Key to search for a previous file item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	struct cpu_key s_item_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	loff_t file_size,	/* Old file size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	 new_file_size;	/* New file size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	int deleted;		/* Number of deleted or truncated bytes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	BUG_ON(!th->t_trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	if (!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	    (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	     || S_ISLNK(inode->i_mode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	/* deletion of directory - no need to update timestamps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 	if (S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 		truncate_directory(th, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	/* Get new file size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	new_file_size = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	/* FIXME: note, that key type is unimportant here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	make_cpu_key(&s_item_key, inode, max_reiserfs_offset(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 		     TYPE_DIRECT, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	    search_for_position_by_key(inode->i_sb, &s_item_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 				       &s_search_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 	if (retval == IO_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 		reiserfs_error(inode->i_sb, "vs-5657",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 			       "i/o failure occurred trying to truncate %K",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 			       &s_item_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	if (retval == POSITION_FOUND || retval == FILE_NOT_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 		reiserfs_error(inode->i_sb, "PAP-5660",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 			       "wrong result %d of search for %K", retval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 			       &s_item_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	s_search_path.pos_in_item--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	/* Get real file size (total length of all file items) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	p_le_ih = tp_item_head(&s_search_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 	if (is_statdata_le_ih(p_le_ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 		file_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 		loff_t offset = le_ih_k_offset(p_le_ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 		int bytes =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 		    op_bytes_number(p_le_ih, inode->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 		 * this may mismatch with real file size: if last direct item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 		 * had no padding zeros and last unformatted node had no free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 		 * space, this file would have this file size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 		file_size = offset + bytes - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	 * are we doing a full truncate or delete, if so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	 * kick in the reada code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	if (new_file_size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 		s_search_path.reada = PATH_READA | PATH_READA_BACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 	if (file_size == 0 || file_size < new_file_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 		goto update_and_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	/* Update key to search for the last file item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	set_cpu_key_k_offset(&s_item_key, file_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 		/* Cut or delete file item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 		deleted =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 		    reiserfs_cut_from_item(th, &s_search_path, &s_item_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 					   inode, page, new_file_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 		if (deleted < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 			reiserfs_warning(inode->i_sb, "vs-5665",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 					 "reiserfs_cut_from_item failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 			reiserfs_check_path(&s_search_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 		RFALSE(deleted > file_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 		       "PAP-5670: reiserfs_cut_from_item: too many bytes deleted: deleted %d, file_size %lu, item_key %K",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 		       deleted, file_size, &s_item_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 		/* Change key to search the last file item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 		file_size -= deleted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 		set_cpu_key_k_offset(&s_item_key, file_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 		 * While there are bytes to truncate and previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 		 * file item is presented in the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 		 * This loop could take a really long time, and could log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 		 * many more blocks than a transaction can hold.  So, we do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 		 * a polite journal end here, and if the transaction needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 		 * ending, we make sure the file is consistent before ending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 		 * the current trans and starting a new one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 		if (journal_transaction_should_end(th, 0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 		    reiserfs_transaction_free_space(th) <= JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 			pathrelse(&s_search_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 			if (update_timestamps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 				inode->i_mtime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 				inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 			reiserfs_update_sd(th, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 			err = journal_end(th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 			err = journal_begin(th, inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 					    JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD + JOURNAL_PER_BALANCE_CNT * 4) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 			reiserfs_update_inode_transaction(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	} while (file_size > ROUND_UP(new_file_size) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 		 search_for_position_by_key(inode->i_sb, &s_item_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 					    &s_search_path) == POSITION_FOUND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 	RFALSE(file_size > ROUND_UP(new_file_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	       "PAP-5680: truncate did not finish: new_file_size %lld, current %lld, oid %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	       new_file_size, file_size, s_item_key.on_disk_key.k_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) update_and_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	if (update_timestamps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 		/* this is truncate, not file closing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		inode->i_mtime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 		inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	reiserfs_update_sd(th, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 	pathrelse(&s_search_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) #ifdef CONFIG_REISERFS_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) /* this makes sure, that we __append__, not overwrite or add holes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) static void check_research_for_paste(struct treepath *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 				     const struct cpu_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	struct item_head *found_ih = tp_item_head(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	if (is_direct_le_ih(found_ih)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 		if (le_ih_k_offset(found_ih) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 		    op_bytes_number(found_ih,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 				    get_last_bh(path)->b_size) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 		    cpu_key_k_offset(key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 		    || op_bytes_number(found_ih,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 				       get_last_bh(path)->b_size) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 		    pos_in_item(path))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 			reiserfs_panic(NULL, "PAP-5720", "found direct item "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 				       "%h or position (%d) does not match "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 				       "to key %K", found_ih,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 				       pos_in_item(path), key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	if (is_indirect_le_ih(found_ih)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 		if (le_ih_k_offset(found_ih) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 		    op_bytes_number(found_ih,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 				    get_last_bh(path)->b_size) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 		    cpu_key_k_offset(key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 		    || I_UNFM_NUM(found_ih) != pos_in_item(path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 		    || get_ih_free_space(found_ih) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 			reiserfs_panic(NULL, "PAP-5730", "found indirect "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 				       "item (%h) or position (%d) does not "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 				       "match to key (%K)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 				       found_ih, pos_in_item(path), key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) #endif				/* config reiserfs check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079)  * Paste bytes to the existing item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080)  * Returns bytes number pasted into the item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) int reiserfs_paste_into_item(struct reiserfs_transaction_handle *th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 			     /* Path to the pasted item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 			     struct treepath *search_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 			     /* Key to search for the needed item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 			     const struct cpu_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 			     /* Inode item belongs to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 			     struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 			     /* Pointer to the bytes to paste. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 			     const char *body,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 			     /* Size of pasted bytes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 			     int pasted_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	struct tree_balance s_paste_balance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	int fs_gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	BUG_ON(!th->t_trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	fs_gen = get_generation(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) #ifdef REISERQUOTA_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 	reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 		       "reiserquota paste_into_item(): allocating %u id=%u type=%c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 		       pasted_size, inode->i_uid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 		       key2type(&key->on_disk_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	depth = reiserfs_write_unlock_nested(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	retval = dquot_alloc_space_nodirty(inode, pasted_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	reiserfs_write_lock_nested(sb, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 		pathrelse(search_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	init_tb_struct(th, &s_paste_balance, th->t_super, search_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 		       pasted_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) #ifdef DISPLACE_NEW_PACKING_LOCALITIES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	s_paste_balance.key = key->on_disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	/* DQUOT_* can schedule, must check before the fix_nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	if (fs_changed(fs_gen, inode->i_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 		goto search_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	while ((retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 		fix_nodes(M_PASTE, &s_paste_balance, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 			  body)) == REPEAT_SEARCH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) search_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 		/* file system changed while we were in the fix_nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 		PROC_INFO_INC(th->t_super, paste_into_item_restarted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 		retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 		    search_for_position_by_key(th->t_super, key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 					       search_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 		if (retval == IO_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 			retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 			goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 		if (retval == POSITION_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 			reiserfs_warning(inode->i_sb, "PAP-5710",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 					 "entry or pasted byte (%K) exists",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 					 key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 			retval = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 			goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) #ifdef CONFIG_REISERFS_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 		check_research_for_paste(search_path, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	 * Perform balancing after all resources are collected by fix_nodes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	 * and accessing them will not risk triggering schedule.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 	if (retval == CARRY_ON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 		do_balance(&s_paste_balance, NULL /*ih */ , body, M_PASTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) error_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 	/* this also releases the path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 	unfix_nodes(&s_paste_balance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) #ifdef REISERQUOTA_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 		       "reiserquota paste_into_item(): freeing %u id=%u type=%c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 		       pasted_size, inode->i_uid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 		       key2type(&key->on_disk_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 	depth = reiserfs_write_unlock_nested(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 	dquot_free_space_nodirty(inode, pasted_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 	reiserfs_write_lock_nested(sb, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179)  * Insert new item into the buffer at the path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180)  * th   - active transaction handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181)  * path - path to the inserted item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182)  * ih   - pointer to the item header to insert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183)  * body - pointer to the bytes to insert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) int reiserfs_insert_item(struct reiserfs_transaction_handle *th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 			 struct treepath *path, const struct cpu_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 			 struct item_head *ih, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 			 const char *body)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 	struct tree_balance s_ins_balance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 	int fs_gen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 	int quota_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 	BUG_ON(!th->t_trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	if (inode) {		/* Do we count quotas for item? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 		int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 		fs_gen = get_generation(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 		quota_bytes = ih_item_len(ih);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 		 * hack so the quota code doesn't have to guess
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 		 * if the file has a tail, links are always tails,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 		 * so there's no guessing needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 		if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(ih))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 			quota_bytes = inode->i_sb->s_blocksize + UNFM_P_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) #ifdef REISERQUOTA_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 		reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 			       "reiserquota insert_item(): allocating %u id=%u type=%c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 			       quota_bytes, inode->i_uid, head2type(ih));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 		 * We can't dirty inode here. It would be immediately
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 		 * written but appropriate stat item isn't inserted yet...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 		depth = reiserfs_write_unlock_nested(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 		retval = dquot_alloc_space_nodirty(inode, quota_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 		reiserfs_write_lock_nested(inode->i_sb, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 		if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 			pathrelse(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	init_tb_struct(th, &s_ins_balance, th->t_super, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 		       IH_SIZE + ih_item_len(ih));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) #ifdef DISPLACE_NEW_PACKING_LOCALITIES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	s_ins_balance.key = key->on_disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 	 * DQUOT_* can schedule, must check to be sure calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	 * fix_nodes is safe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 	if (inode && fs_changed(fs_gen, inode->i_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 		goto search_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 	while ((retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 		fix_nodes(M_INSERT, &s_ins_balance, ih,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 			  body)) == REPEAT_SEARCH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) search_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 		/* file system changed while we were in the fix_nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 		PROC_INFO_INC(th->t_super, insert_item_restarted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 		retval = search_item(th->t_super, key, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 		if (retval == IO_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 			retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 			goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 		if (retval == ITEM_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 			reiserfs_warning(th->t_super, "PAP-5760",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 					 "key %K already exists in the tree",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 					 key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 			retval = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 			goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 	/* make balancing after all resources will be collected at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 	if (retval == CARRY_ON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 		do_balance(&s_ins_balance, ih, body, M_INSERT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 	retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) error_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 	/* also releases the path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 	unfix_nodes(&s_ins_balance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) #ifdef REISERQUOTA_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 	if (inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 		reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 		       "reiserquota insert_item(): freeing %u id=%u type=%c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 		       quota_bytes, inode->i_uid, head2type(ih));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 	if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 		int depth = reiserfs_write_unlock_nested(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 		dquot_free_space_nodirty(inode, quota_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 		reiserfs_write_lock_nested(inode->i_sb, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) }