xref: /linux/fs/reiserfs/stree.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
31da177e4SLinus Torvalds  */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds /*
61da177e4SLinus Torvalds  *  Written by Anatoly P. Pinchuk pap@namesys.botik.ru
71da177e4SLinus Torvalds  *  Programm System Institute
81da177e4SLinus Torvalds  *  Pereslavl-Zalessky Russia
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/time.h>
121da177e4SLinus Torvalds #include <linux/string.h>
131da177e4SLinus Torvalds #include <linux/pagemap.h>
142f8b5444SChristoph Hellwig #include <linux/bio.h>
15f466c6fdSAl Viro #include "reiserfs.h"
161da177e4SLinus Torvalds #include <linux/buffer_head.h>
171da177e4SLinus Torvalds #include <linux/quotaops.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds /* Does the buffer contain a disk block which is in the tree. */
B_IS_IN_TREE(const struct buffer_head * bh)20ad31a4fcSJeff Mahoney inline int B_IS_IN_TREE(const struct buffer_head *bh)
211da177e4SLinus Torvalds {
221da177e4SLinus Torvalds 
23ad31a4fcSJeff Mahoney 	RFALSE(B_LEVEL(bh) > MAX_HEIGHT,
24ad31a4fcSJeff Mahoney 	       "PAP-1010: block (%b) has too big level (%z)", bh, bh);
251da177e4SLinus Torvalds 
26ad31a4fcSJeff Mahoney 	return (B_LEVEL(bh) != FREE_LEVEL);
271da177e4SLinus Torvalds }
281da177e4SLinus Torvalds 
29098297b2SJeff Mahoney /* to get item head in le form */
copy_item_head(struct item_head * to,const struct item_head * from)30d68caa95SJeff Mahoney inline void copy_item_head(struct item_head *to,
31d68caa95SJeff Mahoney 			   const struct item_head *from)
321da177e4SLinus Torvalds {
33d68caa95SJeff Mahoney 	memcpy(to, from, IH_SIZE);
341da177e4SLinus Torvalds }
351da177e4SLinus Torvalds 
36098297b2SJeff Mahoney /*
37098297b2SJeff Mahoney  * k1 is pointer to on-disk structure which is stored in little-endian
38098297b2SJeff Mahoney  * form. k2 is pointer to cpu variable. For key of items of the same
39098297b2SJeff Mahoney  * object this returns 0.
40098297b2SJeff Mahoney  * Returns: -1 if key1 < key2
41098297b2SJeff Mahoney  * 0 if key1 == key2
42098297b2SJeff Mahoney  * 1 if key1 > key2
43098297b2SJeff Mahoney  */
comp_short_keys(const struct reiserfs_key * le_key,const struct cpu_key * cpu_key)441da177e4SLinus Torvalds inline int comp_short_keys(const struct reiserfs_key *le_key,
451da177e4SLinus Torvalds 			   const struct cpu_key *cpu_key)
461da177e4SLinus Torvalds {
476b9f5829SAl Viro 	__u32 n;
486b9f5829SAl Viro 	n = le32_to_cpu(le_key->k_dir_id);
496b9f5829SAl Viro 	if (n < cpu_key->on_disk_key.k_dir_id)
501da177e4SLinus Torvalds 		return -1;
516b9f5829SAl Viro 	if (n > cpu_key->on_disk_key.k_dir_id)
521da177e4SLinus Torvalds 		return 1;
536b9f5829SAl Viro 	n = le32_to_cpu(le_key->k_objectid);
546b9f5829SAl Viro 	if (n < cpu_key->on_disk_key.k_objectid)
556b9f5829SAl Viro 		return -1;
566b9f5829SAl Viro 	if (n > cpu_key->on_disk_key.k_objectid)
576b9f5829SAl Viro 		return 1;
581da177e4SLinus Torvalds 	return 0;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
61098297b2SJeff Mahoney /*
62098297b2SJeff Mahoney  * k1 is pointer to on-disk structure which is stored in little-endian
63098297b2SJeff Mahoney  * form. k2 is pointer to cpu variable.
64098297b2SJeff Mahoney  * Compare keys using all 4 key fields.
65098297b2SJeff Mahoney  * Returns: -1 if key1 < key2 0
66098297b2SJeff Mahoney  * if key1 = key2 1 if key1 > key2
67098297b2SJeff Mahoney  */
comp_keys(const struct reiserfs_key * le_key,const struct cpu_key * cpu_key)68bd4c625cSLinus Torvalds static inline int comp_keys(const struct reiserfs_key *le_key,
69bd4c625cSLinus Torvalds 			    const struct cpu_key *cpu_key)
701da177e4SLinus Torvalds {
711da177e4SLinus Torvalds 	int retval;
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	retval = comp_short_keys(le_key, cpu_key);
741da177e4SLinus Torvalds 	if (retval)
751da177e4SLinus Torvalds 		return retval;
76bd4c625cSLinus Torvalds 	if (le_key_k_offset(le_key_version(le_key), le_key) <
77bd4c625cSLinus Torvalds 	    cpu_key_k_offset(cpu_key))
781da177e4SLinus Torvalds 		return -1;
79bd4c625cSLinus Torvalds 	if (le_key_k_offset(le_key_version(le_key), le_key) >
80bd4c625cSLinus Torvalds 	    cpu_key_k_offset(cpu_key))
811da177e4SLinus Torvalds 		return 1;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	if (cpu_key->key_length == 3)
841da177e4SLinus Torvalds 		return 0;
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	/* this part is needed only when tail conversion is in progress */
87bd4c625cSLinus Torvalds 	if (le_key_k_type(le_key_version(le_key), le_key) <
88bd4c625cSLinus Torvalds 	    cpu_key_k_type(cpu_key))
891da177e4SLinus Torvalds 		return -1;
901da177e4SLinus Torvalds 
91bd4c625cSLinus Torvalds 	if (le_key_k_type(le_key_version(le_key), le_key) >
92bd4c625cSLinus Torvalds 	    cpu_key_k_type(cpu_key))
931da177e4SLinus Torvalds 		return 1;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	return 0;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
comp_short_le_keys(const struct reiserfs_key * key1,const struct reiserfs_key * key2)98bd4c625cSLinus Torvalds inline int comp_short_le_keys(const struct reiserfs_key *key1,
99bd4c625cSLinus Torvalds 			      const struct reiserfs_key *key2)
1001da177e4SLinus Torvalds {
101d68caa95SJeff Mahoney 	__u32 *k1_u32, *k2_u32;
102ee93961bSJeff Mahoney 	int key_length = REISERFS_SHORT_KEY_LEN;
1031da177e4SLinus Torvalds 
104d68caa95SJeff Mahoney 	k1_u32 = (__u32 *) key1;
105d68caa95SJeff Mahoney 	k2_u32 = (__u32 *) key2;
106ee93961bSJeff Mahoney 	for (; key_length--; ++k1_u32, ++k2_u32) {
107d68caa95SJeff Mahoney 		if (le32_to_cpu(*k1_u32) < le32_to_cpu(*k2_u32))
1081da177e4SLinus Torvalds 			return -1;
109d68caa95SJeff Mahoney 		if (le32_to_cpu(*k1_u32) > le32_to_cpu(*k2_u32))
1101da177e4SLinus Torvalds 			return 1;
1111da177e4SLinus Torvalds 	}
1121da177e4SLinus Torvalds 	return 0;
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
le_key2cpu_key(struct cpu_key * to,const struct reiserfs_key * from)1151da177e4SLinus Torvalds inline void le_key2cpu_key(struct cpu_key *to, const struct reiserfs_key *from)
1161da177e4SLinus Torvalds {
1176b9f5829SAl Viro 	int version;
1181da177e4SLinus Torvalds 	to->on_disk_key.k_dir_id = le32_to_cpu(from->k_dir_id);
1191da177e4SLinus Torvalds 	to->on_disk_key.k_objectid = le32_to_cpu(from->k_objectid);
1201da177e4SLinus Torvalds 
121098297b2SJeff Mahoney 	/* find out version of the key */
1226b9f5829SAl Viro 	version = le_key_version(from);
1236b9f5829SAl Viro 	to->version = version;
1246b9f5829SAl Viro 	to->on_disk_key.k_offset = le_key_k_offset(version, from);
1256b9f5829SAl Viro 	to->on_disk_key.k_type = le_key_k_type(version, from);
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
128098297b2SJeff Mahoney /*
129098297b2SJeff Mahoney  * this does not say which one is bigger, it only returns 1 if keys
130098297b2SJeff Mahoney  * are not equal, 0 otherwise
131098297b2SJeff Mahoney  */
comp_le_keys(const struct reiserfs_key * k1,const struct reiserfs_key * k2)132bd4c625cSLinus Torvalds inline int comp_le_keys(const struct reiserfs_key *k1,
133bd4c625cSLinus Torvalds 			const struct reiserfs_key *k2)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	return memcmp(k1, k2, sizeof(struct reiserfs_key));
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /**************************************************************************
1391da177e4SLinus Torvalds  *  Binary search toolkit function                                        *
1401da177e4SLinus Torvalds  *  Search for an item in the array by the item key                       *
1411da177e4SLinus Torvalds  *  Returns:    1 if found,  0 if not found;                              *
142d68caa95SJeff Mahoney  *        *pos = number of the searched element if found, else the        *
143d68caa95SJeff Mahoney  *        number of the first element that is larger than key.            *
1441da177e4SLinus Torvalds  **************************************************************************/
145098297b2SJeff Mahoney /*
146098297b2SJeff Mahoney  * For those not familiar with binary search: lbound is the leftmost item
147098297b2SJeff Mahoney  * that it could be, rbound the rightmost item that it could be.  We examine
148098297b2SJeff Mahoney  * the item halfway between lbound and rbound, and that tells us either
149098297b2SJeff Mahoney  * that we can increase lbound, or decrease rbound, or that we have found it,
150098297b2SJeff Mahoney  * or if lbound <= rbound that there are no possible items, and we have not
151098297b2SJeff Mahoney  * found it. With each examination we cut the number of possible items it
152098297b2SJeff Mahoney  * could be by one more than half rounded down, or we find it.
153098297b2SJeff Mahoney  */
bin_search(const void * key,const void * base,int num,int width,int * pos)154d68caa95SJeff Mahoney static inline int bin_search(const void *key,	/* Key to search for. */
155d68caa95SJeff Mahoney 			     const void *base,	/* First item in the array. */
156d68caa95SJeff Mahoney 			     int num,	/* Number of items in the array. */
157098297b2SJeff Mahoney 			     /*
158098297b2SJeff Mahoney 			      * Item size in the array.  searched. Lest the
159098297b2SJeff Mahoney 			      * reader be confused, note that this is crafted
160098297b2SJeff Mahoney 			      * as a general function, and when it is applied
161098297b2SJeff Mahoney 			      * specifically to the array of item headers in a
162098297b2SJeff Mahoney 			      * node, width is actually the item header size
163098297b2SJeff Mahoney 			      * not the item size.
164098297b2SJeff Mahoney 			      */
165098297b2SJeff Mahoney 			     int width,
166d68caa95SJeff Mahoney 			     int *pos /* Number of the searched for element. */
167bd4c625cSLinus Torvalds     )
168bd4c625cSLinus Torvalds {
169ee93961bSJeff Mahoney 	int rbound, lbound, j;
1701da177e4SLinus Torvalds 
171ee93961bSJeff Mahoney 	for (j = ((rbound = num - 1) + (lbound = 0)) / 2;
172ee93961bSJeff Mahoney 	     lbound <= rbound; j = (rbound + lbound) / 2)
173bd4c625cSLinus Torvalds 		switch (comp_keys
174ee93961bSJeff Mahoney 			((struct reiserfs_key *)((char *)base + j * width),
175d68caa95SJeff Mahoney 			 (struct cpu_key *)key)) {
176bd4c625cSLinus Torvalds 		case -1:
177ee93961bSJeff Mahoney 			lbound = j + 1;
178bd4c625cSLinus Torvalds 			continue;
179bd4c625cSLinus Torvalds 		case 1:
180ee93961bSJeff Mahoney 			rbound = j - 1;
181bd4c625cSLinus Torvalds 			continue;
182bd4c625cSLinus Torvalds 		case 0:
183ee93961bSJeff Mahoney 			*pos = j;
184bd4c625cSLinus Torvalds 			return ITEM_FOUND;	/* Key found in the array.  */
1851da177e4SLinus Torvalds 		}
1861da177e4SLinus Torvalds 
187098297b2SJeff Mahoney 	/*
188098297b2SJeff Mahoney 	 * bin_search did not find given key, it returns position of key,
189098297b2SJeff Mahoney 	 * that is minimal and greater than the given one.
190098297b2SJeff Mahoney 	 */
191ee93961bSJeff Mahoney 	*pos = lbound;
1921da177e4SLinus Torvalds 	return ITEM_NOT_FOUND;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds /* Minimal possible key. It is never in the tree. */
1971da177e4SLinus Torvalds const struct reiserfs_key MIN_KEY = { 0, 0, {{0, 0},} };
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds /* Maximal possible key. It is never in the tree. */
20052c1da39SAdrian Bunk static const struct reiserfs_key MAX_KEY = {
201afd54c68SFabian Frederick 	cpu_to_le32(0xffffffff),
202afd54c68SFabian Frederick 	cpu_to_le32(0xffffffff),
203afd54c68SFabian Frederick 	{{cpu_to_le32(0xffffffff),
204afd54c68SFabian Frederick 	  cpu_to_le32(0xffffffff)},}
2053e8962beSAl Viro };
2061da177e4SLinus Torvalds 
207098297b2SJeff Mahoney /*
208098297b2SJeff Mahoney  * Get delimiting key of the buffer by looking for it in the buffers in the
209098297b2SJeff Mahoney  * path, starting from the bottom of the path, and going upwards.  We must
210098297b2SJeff Mahoney  * check the path's validity at each step.  If the key is not in the path,
211098297b2SJeff Mahoney  * there is no delimiting key in the tree (buffer is first or last buffer
212098297b2SJeff Mahoney  * in tree), and in this case we return a special key, either MIN_KEY or
213098297b2SJeff Mahoney  * MAX_KEY.
214098297b2SJeff Mahoney  */
get_lkey(const struct treepath * chk_path,const struct super_block * sb)215ee93961bSJeff Mahoney static inline const struct reiserfs_key *get_lkey(const struct treepath *chk_path,
216ee93961bSJeff Mahoney 						  const struct super_block *sb)
217bd4c625cSLinus Torvalds {
218ee93961bSJeff Mahoney 	int position, path_offset = chk_path->path_length;
219d68caa95SJeff Mahoney 	struct buffer_head *parent;
2201da177e4SLinus Torvalds 
221ee93961bSJeff Mahoney 	RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET,
2221da177e4SLinus Torvalds 	       "PAP-5010: invalid offset in the path");
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	/* While not higher in path than first element. */
225ee93961bSJeff Mahoney 	while (path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
2261da177e4SLinus Torvalds 
227bd4c625cSLinus Torvalds 		RFALSE(!buffer_uptodate
228ee93961bSJeff Mahoney 		       (PATH_OFFSET_PBUFFER(chk_path, path_offset)),
2291da177e4SLinus Torvalds 		       "PAP-5020: parent is not uptodate");
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 		/* Parent at the path is not in the tree now. */
232bd4c625cSLinus Torvalds 		if (!B_IS_IN_TREE
233d68caa95SJeff Mahoney 		    (parent =
234ee93961bSJeff Mahoney 		     PATH_OFFSET_PBUFFER(chk_path, path_offset)))
2351da177e4SLinus Torvalds 			return &MAX_KEY;
2361da177e4SLinus Torvalds 		/* Check whether position in the parent is correct. */
237ee93961bSJeff Mahoney 		if ((position =
238d68caa95SJeff Mahoney 		     PATH_OFFSET_POSITION(chk_path,
239ee93961bSJeff Mahoney 					  path_offset)) >
240d68caa95SJeff Mahoney 		    B_NR_ITEMS(parent))
2411da177e4SLinus Torvalds 			return &MAX_KEY;
2421da177e4SLinus Torvalds 		/* Check whether parent at the path really points to the child. */
243ee93961bSJeff Mahoney 		if (B_N_CHILD_NUM(parent, position) !=
244d68caa95SJeff Mahoney 		    PATH_OFFSET_PBUFFER(chk_path,
245ee93961bSJeff Mahoney 					path_offset + 1)->b_blocknr)
2461da177e4SLinus Torvalds 			return &MAX_KEY;
247098297b2SJeff Mahoney 		/*
248098297b2SJeff Mahoney 		 * Return delimiting key if position in the parent
249098297b2SJeff Mahoney 		 * is not equal to zero.
250098297b2SJeff Mahoney 		 */
251ee93961bSJeff Mahoney 		if (position)
2524cf5f7adSJeff Mahoney 			return internal_key(parent, position - 1);
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 	/* Return MIN_KEY if we are in the root of the buffer tree. */
255d68caa95SJeff Mahoney 	if (PATH_OFFSET_PBUFFER(chk_path, FIRST_PATH_ELEMENT_OFFSET)->
256a9dd3643SJeff Mahoney 	    b_blocknr == SB_ROOT_BLOCK(sb))
2571da177e4SLinus Torvalds 		return &MIN_KEY;
2581da177e4SLinus Torvalds 	return &MAX_KEY;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds /* Get delimiting key of the buffer at the path and its right neighbor. */
get_rkey(const struct treepath * chk_path,const struct super_block * sb)262d68caa95SJeff Mahoney inline const struct reiserfs_key *get_rkey(const struct treepath *chk_path,
263a9dd3643SJeff Mahoney 					   const struct super_block *sb)
264bd4c625cSLinus Torvalds {
265ee93961bSJeff Mahoney 	int position, path_offset = chk_path->path_length;
266d68caa95SJeff Mahoney 	struct buffer_head *parent;
2671da177e4SLinus Torvalds 
268ee93961bSJeff Mahoney 	RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET,
2691da177e4SLinus Torvalds 	       "PAP-5030: invalid offset in the path");
2701da177e4SLinus Torvalds 
271ee93961bSJeff Mahoney 	while (path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
2721da177e4SLinus Torvalds 
273bd4c625cSLinus Torvalds 		RFALSE(!buffer_uptodate
274ee93961bSJeff Mahoney 		       (PATH_OFFSET_PBUFFER(chk_path, path_offset)),
2751da177e4SLinus Torvalds 		       "PAP-5040: parent is not uptodate");
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 		/* Parent at the path is not in the tree now. */
278bd4c625cSLinus Torvalds 		if (!B_IS_IN_TREE
279d68caa95SJeff Mahoney 		    (parent =
280ee93961bSJeff Mahoney 		     PATH_OFFSET_PBUFFER(chk_path, path_offset)))
2811da177e4SLinus Torvalds 			return &MIN_KEY;
2821da177e4SLinus Torvalds 		/* Check whether position in the parent is correct. */
283ee93961bSJeff Mahoney 		if ((position =
284d68caa95SJeff Mahoney 		     PATH_OFFSET_POSITION(chk_path,
285ee93961bSJeff Mahoney 					  path_offset)) >
286d68caa95SJeff Mahoney 		    B_NR_ITEMS(parent))
2871da177e4SLinus Torvalds 			return &MIN_KEY;
288098297b2SJeff Mahoney 		/*
289098297b2SJeff Mahoney 		 * Check whether parent at the path really points
290098297b2SJeff Mahoney 		 * to the child.
291098297b2SJeff Mahoney 		 */
292ee93961bSJeff Mahoney 		if (B_N_CHILD_NUM(parent, position) !=
293d68caa95SJeff Mahoney 		    PATH_OFFSET_PBUFFER(chk_path,
294ee93961bSJeff Mahoney 					path_offset + 1)->b_blocknr)
2951da177e4SLinus Torvalds 			return &MIN_KEY;
296098297b2SJeff Mahoney 
297098297b2SJeff Mahoney 		/*
298098297b2SJeff Mahoney 		 * Return delimiting key if position in the parent
299098297b2SJeff Mahoney 		 * is not the last one.
300098297b2SJeff Mahoney 		 */
301ee93961bSJeff Mahoney 		if (position != B_NR_ITEMS(parent))
3024cf5f7adSJeff Mahoney 			return internal_key(parent, position);
3031da177e4SLinus Torvalds 	}
304098297b2SJeff Mahoney 
3051da177e4SLinus Torvalds 	/* Return MAX_KEY if we are in the root of the buffer tree. */
306d68caa95SJeff Mahoney 	if (PATH_OFFSET_PBUFFER(chk_path, FIRST_PATH_ELEMENT_OFFSET)->
307a9dd3643SJeff Mahoney 	    b_blocknr == SB_ROOT_BLOCK(sb))
3081da177e4SLinus Torvalds 		return &MAX_KEY;
3091da177e4SLinus Torvalds 	return &MIN_KEY;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
312098297b2SJeff Mahoney /*
313098297b2SJeff Mahoney  * Check whether a key is contained in the tree rooted from a buffer at a path.
314098297b2SJeff Mahoney  * This works by looking at the left and right delimiting keys for the buffer
315098297b2SJeff Mahoney  * in the last path_element in the path.  These delimiting keys are stored
316098297b2SJeff Mahoney  * at least one level above that buffer in the tree. If the buffer is the
317098297b2SJeff Mahoney  * first or last node in the tree order then one of the delimiting keys may
318098297b2SJeff Mahoney  * be absent, and in this case get_lkey and get_rkey return a special key
319098297b2SJeff Mahoney  * which is MIN_KEY or MAX_KEY.
320098297b2SJeff Mahoney  */
key_in_buffer(struct treepath * chk_path,const struct cpu_key * key,struct super_block * sb)321098297b2SJeff Mahoney static inline int key_in_buffer(
322098297b2SJeff Mahoney 				/* Path which should be checked. */
323098297b2SJeff Mahoney 				struct treepath *chk_path,
324098297b2SJeff Mahoney 				/* Key which should be checked. */
325098297b2SJeff Mahoney 				const struct cpu_key *key,
326d68caa95SJeff Mahoney 				struct super_block *sb
327bd4c625cSLinus Torvalds     )
328bd4c625cSLinus Torvalds {
3291da177e4SLinus Torvalds 
330d68caa95SJeff Mahoney 	RFALSE(!key || chk_path->path_length < FIRST_PATH_ELEMENT_OFFSET
331d68caa95SJeff Mahoney 	       || chk_path->path_length > MAX_HEIGHT,
3321da177e4SLinus Torvalds 	       "PAP-5050: pointer to the key(%p) is NULL or invalid path length(%d)",
333d68caa95SJeff Mahoney 	       key, chk_path->path_length);
334d68caa95SJeff Mahoney 	RFALSE(!PATH_PLAST_BUFFER(chk_path)->b_bdev,
3351da177e4SLinus Torvalds 	       "PAP-5060: device must not be NODEV");
3361da177e4SLinus Torvalds 
337d68caa95SJeff Mahoney 	if (comp_keys(get_lkey(chk_path, sb), key) == 1)
3381da177e4SLinus Torvalds 		/* left delimiting key is bigger, that the key we look for */
3391da177e4SLinus Torvalds 		return 0;
340d68caa95SJeff Mahoney 	/*  if ( comp_keys(key, get_rkey(chk_path, sb)) != -1 ) */
341d68caa95SJeff Mahoney 	if (comp_keys(get_rkey(chk_path, sb), key) != 1)
342d68caa95SJeff Mahoney 		/* key must be less than right delimitiing key */
3431da177e4SLinus Torvalds 		return 0;
3441da177e4SLinus Torvalds 	return 1;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
reiserfs_check_path(struct treepath * p)347fec6d055SJosef "Jeff" Sipek int reiserfs_check_path(struct treepath *p)
348bd4c625cSLinus Torvalds {
3491da177e4SLinus Torvalds 	RFALSE(p->path_length != ILLEGAL_PATH_ELEMENT_OFFSET,
3501da177e4SLinus Torvalds 	       "path not properly relsed");
3511da177e4SLinus Torvalds 	return 0;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
354098297b2SJeff Mahoney /*
355098297b2SJeff Mahoney  * Drop the reference to each buffer in a path and restore
3563cd6dbe6SJeff Mahoney  * dirty bits clean when preparing the buffer for the log.
357098297b2SJeff Mahoney  * This version should only be called from fix_nodes()
358098297b2SJeff Mahoney  */
pathrelse_and_restore(struct super_block * sb,struct treepath * search_path)3593cd6dbe6SJeff Mahoney void pathrelse_and_restore(struct super_block *sb,
360d68caa95SJeff Mahoney 			   struct treepath *search_path)
361bd4c625cSLinus Torvalds {
362ee93961bSJeff Mahoney 	int path_offset = search_path->path_length;
3631da177e4SLinus Torvalds 
364ee93961bSJeff Mahoney 	RFALSE(path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
3651da177e4SLinus Torvalds 	       "clm-4000: invalid path offset");
3661da177e4SLinus Torvalds 
367ee93961bSJeff Mahoney 	while (path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) {
3683cd6dbe6SJeff Mahoney 		struct buffer_head *bh;
369ee93961bSJeff Mahoney 		bh = PATH_OFFSET_PBUFFER(search_path, path_offset--);
3703cd6dbe6SJeff Mahoney 		reiserfs_restore_prepared_buffer(sb, bh);
3713cd6dbe6SJeff Mahoney 		brelse(bh);
3721da177e4SLinus Torvalds 	}
373d68caa95SJeff Mahoney 	search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
3763cd6dbe6SJeff Mahoney /* Drop the reference to each buffer in a path */
pathrelse(struct treepath * search_path)377d68caa95SJeff Mahoney void pathrelse(struct treepath *search_path)
378bd4c625cSLinus Torvalds {
379ee93961bSJeff Mahoney 	int path_offset = search_path->path_length;
3801da177e4SLinus Torvalds 
381ee93961bSJeff Mahoney 	RFALSE(path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
3821da177e4SLinus Torvalds 	       "PAP-5090: invalid path offset");
3831da177e4SLinus Torvalds 
384ee93961bSJeff Mahoney 	while (path_offset > ILLEGAL_PATH_ELEMENT_OFFSET)
385ee93961bSJeff Mahoney 		brelse(PATH_OFFSET_PBUFFER(search_path, path_offset--));
3861da177e4SLinus Torvalds 
387d68caa95SJeff Mahoney 	search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
has_valid_deh_location(struct buffer_head * bh,struct item_head * ih)39013d25750SShreyansh Chouhan static int has_valid_deh_location(struct buffer_head *bh, struct item_head *ih)
39113d25750SShreyansh Chouhan {
39213d25750SShreyansh Chouhan 	struct reiserfs_de_head *deh;
39313d25750SShreyansh Chouhan 	int i;
39413d25750SShreyansh Chouhan 
39513d25750SShreyansh Chouhan 	deh = B_I_DEH(bh, ih);
39613d25750SShreyansh Chouhan 	for (i = 0; i < ih_entry_count(ih); i++) {
39713d25750SShreyansh Chouhan 		if (deh_location(&deh[i]) > ih_item_len(ih)) {
39813d25750SShreyansh Chouhan 			reiserfs_warning(NULL, "reiserfs-5094",
39913d25750SShreyansh Chouhan 					 "directory entry location seems wrong %h",
40013d25750SShreyansh Chouhan 					 &deh[i]);
40113d25750SShreyansh Chouhan 			return 0;
40213d25750SShreyansh Chouhan 		}
40313d25750SShreyansh Chouhan 	}
40413d25750SShreyansh Chouhan 
40513d25750SShreyansh Chouhan 	return 1;
40613d25750SShreyansh Chouhan }
40713d25750SShreyansh Chouhan 
is_leaf(char * buf,int blocksize,struct buffer_head * bh)4081da177e4SLinus Torvalds static int is_leaf(char *buf, int blocksize, struct buffer_head *bh)
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	struct block_head *blkh;
4111da177e4SLinus Torvalds 	struct item_head *ih;
4121da177e4SLinus Torvalds 	int used_space;
4131da177e4SLinus Torvalds 	int prev_location;
4141da177e4SLinus Torvalds 	int i;
4151da177e4SLinus Torvalds 	int nr;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	blkh = (struct block_head *)buf;
4181da177e4SLinus Torvalds 	if (blkh_level(blkh) != DISK_LEAF_NODE_LEVEL) {
41945b03d5eSJeff Mahoney 		reiserfs_warning(NULL, "reiserfs-5080",
42045b03d5eSJeff Mahoney 				 "this should be caught earlier");
4211da177e4SLinus Torvalds 		return 0;
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	nr = blkh_nr_item(blkh);
4251da177e4SLinus Torvalds 	if (nr < 1 || nr > ((blocksize - BLKH_SIZE) / (IH_SIZE + MIN_ITEM_LEN))) {
4261da177e4SLinus Torvalds 		/* item number is too big or too small */
42745b03d5eSJeff Mahoney 		reiserfs_warning(NULL, "reiserfs-5081",
42845b03d5eSJeff Mahoney 				 "nr_item seems wrong: %z", bh);
4291da177e4SLinus Torvalds 		return 0;
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 	ih = (struct item_head *)(buf + BLKH_SIZE) + nr - 1;
4321da177e4SLinus Torvalds 	used_space = BLKH_SIZE + IH_SIZE * nr + (blocksize - ih_location(ih));
433098297b2SJeff Mahoney 
4341da177e4SLinus Torvalds 	/* free space does not match to calculated amount of use space */
435098297b2SJeff Mahoney 	if (used_space != blocksize - blkh_free_space(blkh)) {
43645b03d5eSJeff Mahoney 		reiserfs_warning(NULL, "reiserfs-5082",
43745b03d5eSJeff Mahoney 				 "free space seems wrong: %z", bh);
4381da177e4SLinus Torvalds 		return 0;
4391da177e4SLinus Torvalds 	}
440098297b2SJeff Mahoney 	/*
441098297b2SJeff Mahoney 	 * FIXME: it is_leaf will hit performance too much - we may have
442098297b2SJeff Mahoney 	 * return 1 here
443098297b2SJeff Mahoney 	 */
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	/* check tables of item heads */
4461da177e4SLinus Torvalds 	ih = (struct item_head *)(buf + BLKH_SIZE);
4471da177e4SLinus Torvalds 	prev_location = blocksize;
4481da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ih++) {
4491da177e4SLinus Torvalds 		if (le_ih_k_type(ih) == TYPE_ANY) {
45045b03d5eSJeff Mahoney 			reiserfs_warning(NULL, "reiserfs-5083",
45145b03d5eSJeff Mahoney 					 "wrong item type for item %h",
452bd4c625cSLinus Torvalds 					 ih);
4531da177e4SLinus Torvalds 			return 0;
4541da177e4SLinus Torvalds 		}
455bd4c625cSLinus Torvalds 		if (ih_location(ih) >= blocksize
456bd4c625cSLinus Torvalds 		    || ih_location(ih) < IH_SIZE * nr) {
45745b03d5eSJeff Mahoney 			reiserfs_warning(NULL, "reiserfs-5084",
45845b03d5eSJeff Mahoney 					 "item location seems wrong: %h",
459bd4c625cSLinus Torvalds 					 ih);
4601da177e4SLinus Torvalds 			return 0;
4611da177e4SLinus Torvalds 		}
462bd4c625cSLinus Torvalds 		if (ih_item_len(ih) < 1
463bd4c625cSLinus Torvalds 		    || ih_item_len(ih) > MAX_ITEM_LEN(blocksize)) {
46445b03d5eSJeff Mahoney 			reiserfs_warning(NULL, "reiserfs-5085",
46545b03d5eSJeff Mahoney 					 "item length seems wrong: %h",
466bd4c625cSLinus Torvalds 					 ih);
4671da177e4SLinus Torvalds 			return 0;
4681da177e4SLinus Torvalds 		}
4691da177e4SLinus Torvalds 		if (prev_location - ih_location(ih) != ih_item_len(ih)) {
47045b03d5eSJeff Mahoney 			reiserfs_warning(NULL, "reiserfs-5086",
47145b03d5eSJeff Mahoney 					 "item location seems wrong "
47245b03d5eSJeff Mahoney 					 "(second one): %h", ih);
4731da177e4SLinus Torvalds 			return 0;
4741da177e4SLinus Torvalds 		}
47513d25750SShreyansh Chouhan 		if (is_direntry_le_ih(ih)) {
47613d25750SShreyansh Chouhan 			if (ih_item_len(ih) < (ih_entry_count(ih) * IH_SIZE)) {
477d24396c5SRustam Kovhaev 				reiserfs_warning(NULL, "reiserfs-5093",
478d24396c5SRustam Kovhaev 						 "item entry count seems wrong %h",
479d24396c5SRustam Kovhaev 						 ih);
480d24396c5SRustam Kovhaev 				return 0;
481d24396c5SRustam Kovhaev 			}
48213d25750SShreyansh Chouhan 			return has_valid_deh_location(bh, ih);
48313d25750SShreyansh Chouhan 		}
4841da177e4SLinus Torvalds 		prev_location = ih_location(ih);
4851da177e4SLinus Torvalds 	}
4861da177e4SLinus Torvalds 
487098297b2SJeff Mahoney 	/* one may imagine many more checks */
4881da177e4SLinus Torvalds 	return 1;
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds /* returns 1 if buf looks like an internal node, 0 otherwise */
is_internal(char * buf,int blocksize,struct buffer_head * bh)4921da177e4SLinus Torvalds static int is_internal(char *buf, int blocksize, struct buffer_head *bh)
4931da177e4SLinus Torvalds {
4941da177e4SLinus Torvalds 	struct block_head *blkh;
4951da177e4SLinus Torvalds 	int nr;
4961da177e4SLinus Torvalds 	int used_space;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	blkh = (struct block_head *)buf;
4991da177e4SLinus Torvalds 	nr = blkh_level(blkh);
5001da177e4SLinus Torvalds 	if (nr <= DISK_LEAF_NODE_LEVEL || nr > MAX_HEIGHT) {
5011da177e4SLinus Torvalds 		/* this level is not possible for internal nodes */
50245b03d5eSJeff Mahoney 		reiserfs_warning(NULL, "reiserfs-5087",
50345b03d5eSJeff Mahoney 				 "this should be caught earlier");
5041da177e4SLinus Torvalds 		return 0;
5051da177e4SLinus Torvalds 	}
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	nr = blkh_nr_item(blkh);
5081da177e4SLinus Torvalds 	/* for internal which is not root we might check min number of keys */
509098297b2SJeff Mahoney 	if (nr > (blocksize - BLKH_SIZE - DC_SIZE) / (KEY_SIZE + DC_SIZE)) {
51045b03d5eSJeff Mahoney 		reiserfs_warning(NULL, "reiserfs-5088",
51145b03d5eSJeff Mahoney 				 "number of key seems wrong: %z", bh);
5121da177e4SLinus Torvalds 		return 0;
5131da177e4SLinus Torvalds 	}
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	used_space = BLKH_SIZE + KEY_SIZE * nr + DC_SIZE * (nr + 1);
5161da177e4SLinus Torvalds 	if (used_space != blocksize - blkh_free_space(blkh)) {
51745b03d5eSJeff Mahoney 		reiserfs_warning(NULL, "reiserfs-5089",
51845b03d5eSJeff Mahoney 				 "free space seems wrong: %z", bh);
5191da177e4SLinus Torvalds 		return 0;
5201da177e4SLinus Torvalds 	}
521098297b2SJeff Mahoney 
522098297b2SJeff Mahoney 	/* one may imagine many more checks */
5231da177e4SLinus Torvalds 	return 1;
5241da177e4SLinus Torvalds }
5251da177e4SLinus Torvalds 
526098297b2SJeff Mahoney /*
527098297b2SJeff Mahoney  * make sure that bh contains formatted node of reiserfs tree of
528098297b2SJeff Mahoney  * 'level'-th level
529098297b2SJeff Mahoney  */
is_tree_node(struct buffer_head * bh,int level)5301da177e4SLinus Torvalds static int is_tree_node(struct buffer_head *bh, int level)
5311da177e4SLinus Torvalds {
5321da177e4SLinus Torvalds 	if (B_LEVEL(bh) != level) {
53345b03d5eSJeff Mahoney 		reiserfs_warning(NULL, "reiserfs-5090", "node level %d does "
53445b03d5eSJeff Mahoney 				 "not match to the expected one %d",
5351da177e4SLinus Torvalds 				 B_LEVEL(bh), level);
5361da177e4SLinus Torvalds 		return 0;
5371da177e4SLinus Torvalds 	}
5381da177e4SLinus Torvalds 	if (level == DISK_LEAF_NODE_LEVEL)
5391da177e4SLinus Torvalds 		return is_leaf(bh->b_data, bh->b_size, bh);
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	return is_internal(bh->b_data, bh->b_size, bh);
5421da177e4SLinus Torvalds }
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds #define SEARCH_BY_KEY_READA 16
5451da177e4SLinus Torvalds 
5462ac62695SFrederic Weisbecker /*
5472ac62695SFrederic Weisbecker  * The function is NOT SCHEDULE-SAFE!
5482ac62695SFrederic Weisbecker  * It might unlock the write lock if we needed to wait for a block
5492ac62695SFrederic Weisbecker  * to be read. Note that in this case it won't recover the lock to avoid
5502ac62695SFrederic Weisbecker  * high contention resulting from too much lock requests, especially
5512ac62695SFrederic Weisbecker  * the caller (search_by_key) will perform other schedule-unsafe
5522ac62695SFrederic Weisbecker  * operations just after calling this function.
5532ac62695SFrederic Weisbecker  *
554278f6679SJeff Mahoney  * @return depth of lock to be restored after read completes
5552ac62695SFrederic Weisbecker  */
search_by_key_reada(struct super_block * s,struct buffer_head ** bh,b_blocknr_t * b,int num)556278f6679SJeff Mahoney static int search_by_key_reada(struct super_block *s,
5571da177e4SLinus Torvalds 				struct buffer_head **bh,
5583ee16670SJeff Mahoney 				b_blocknr_t *b, int num)
5591da177e4SLinus Torvalds {
5601da177e4SLinus Torvalds 	int i, j;
561278f6679SJeff Mahoney 	int depth = -1;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	for (i = 0; i < num; i++) {
5641da177e4SLinus Torvalds 		bh[i] = sb_getblk(s, b[i]);
5651da177e4SLinus Torvalds 	}
56609eb47a7SFrederic Weisbecker 	/*
56709eb47a7SFrederic Weisbecker 	 * We are going to read some blocks on which we
56809eb47a7SFrederic Weisbecker 	 * have a reference. It's safe, though we might be
56909eb47a7SFrederic Weisbecker 	 * reading blocks concurrently changed if we release
57009eb47a7SFrederic Weisbecker 	 * the lock. But it's still fine because we check later
57109eb47a7SFrederic Weisbecker 	 * if the tree changed
57209eb47a7SFrederic Weisbecker 	 */
5731da177e4SLinus Torvalds 	for (j = 0; j < i; j++) {
5741da177e4SLinus Torvalds 		/*
5751da177e4SLinus Torvalds 		 * note, this needs attention if we are getting rid of the BKL
576098297b2SJeff Mahoney 		 * you have to make sure the prepared bit isn't set on this
577098297b2SJeff Mahoney 		 * buffer
5781da177e4SLinus Torvalds 		 */
5792ac62695SFrederic Weisbecker 		if (!buffer_uptodate(bh[j])) {
580278f6679SJeff Mahoney 			if (depth == -1)
581278f6679SJeff Mahoney 				depth = reiserfs_write_unlock_nested(s);
582d554822eSZhang Yi 			bh_readahead(bh[j], REQ_RAHEAD);
5832ac62695SFrederic Weisbecker 		}
5841da177e4SLinus Torvalds 		brelse(bh[j]);
5851da177e4SLinus Torvalds 	}
586278f6679SJeff Mahoney 	return depth;
5871da177e4SLinus Torvalds }
5881da177e4SLinus Torvalds 
589098297b2SJeff Mahoney /*
590098297b2SJeff Mahoney  * This function fills up the path from the root to the leaf as it
591098297b2SJeff Mahoney  * descends the tree looking for the key.  It uses reiserfs_bread to
592098297b2SJeff Mahoney  * try to find buffers in the cache given their block number.  If it
593098297b2SJeff Mahoney  * does not find them in the cache it reads them from disk.  For each
594098297b2SJeff Mahoney  * node search_by_key finds using reiserfs_bread it then uses
595098297b2SJeff Mahoney  * bin_search to look through that node.  bin_search will find the
596098297b2SJeff Mahoney  * position of the block_number of the next node if it is looking
597098297b2SJeff Mahoney  * through an internal node.  If it is looking through a leaf node
598098297b2SJeff Mahoney  * bin_search will find the position of the item which has key either
599098297b2SJeff Mahoney  * equal to given key, or which is the maximal key less than the given
600098297b2SJeff Mahoney  * key.  search_by_key returns a path that must be checked for the
601098297b2SJeff Mahoney  * correctness of the top of the path but need not be checked for the
602098297b2SJeff Mahoney  * correctness of the bottom of the path
603098297b2SJeff Mahoney  */
604098297b2SJeff Mahoney /*
605098297b2SJeff Mahoney  * search_by_key - search for key (and item) in stree
606098297b2SJeff Mahoney  * @sb: superblock
607098297b2SJeff Mahoney  * @key: pointer to key to search for
608098297b2SJeff Mahoney  * @search_path: Allocated and initialized struct treepath; Returned filled
609098297b2SJeff Mahoney  *		 on success.
610098297b2SJeff Mahoney  * @stop_level: How far down the tree to search, Use DISK_LEAF_NODE_LEVEL to
611098297b2SJeff Mahoney  *		stop at leaf level.
612098297b2SJeff Mahoney  *
613098297b2SJeff Mahoney  * The function is NOT SCHEDULE-SAFE!
614098297b2SJeff Mahoney  */
search_by_key(struct super_block * sb,const struct cpu_key * key,struct treepath * search_path,int stop_level)615098297b2SJeff Mahoney int search_by_key(struct super_block *sb, const struct cpu_key *key,
616098297b2SJeff Mahoney 		  struct treepath *search_path, int stop_level)
617bd4c625cSLinus Torvalds {
618ee93961bSJeff Mahoney 	b_blocknr_t block_number;
6191da177e4SLinus Torvalds 	int expected_level;
620ad31a4fcSJeff Mahoney 	struct buffer_head *bh;
621d68caa95SJeff Mahoney 	struct path_element *last_element;
622ee93961bSJeff Mahoney 	int node_level, retval;
6231da177e4SLinus Torvalds 	int fs_gen;
6241da177e4SLinus Torvalds 	struct buffer_head *reada_bh[SEARCH_BY_KEY_READA];
6253ee16670SJeff Mahoney 	b_blocknr_t reada_blocks[SEARCH_BY_KEY_READA];
6261da177e4SLinus Torvalds 	int reada_count = 0;
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds #ifdef CONFIG_REISERFS_CHECK
629ee93961bSJeff Mahoney 	int repeat_counter = 0;
6301da177e4SLinus Torvalds #endif
6311da177e4SLinus Torvalds 
632a9dd3643SJeff Mahoney 	PROC_INFO_INC(sb, search_by_key);
6331da177e4SLinus Torvalds 
634098297b2SJeff Mahoney 	/*
635098297b2SJeff Mahoney 	 * As we add each node to a path we increase its count.  This means
636098297b2SJeff Mahoney 	 * that we must be careful to release all nodes in a path before we
637098297b2SJeff Mahoney 	 * either discard the path struct or re-use the path struct, as we
638098297b2SJeff Mahoney 	 * do here.
639098297b2SJeff Mahoney 	 */
6401da177e4SLinus Torvalds 
641d68caa95SJeff Mahoney 	pathrelse(search_path);
6421da177e4SLinus Torvalds 
643098297b2SJeff Mahoney 	/*
644098297b2SJeff Mahoney 	 * With each iteration of this loop we search through the items in the
645098297b2SJeff Mahoney 	 * current node, and calculate the next current node(next path element)
646098297b2SJeff Mahoney 	 * for the next iteration of this loop..
647098297b2SJeff Mahoney 	 */
648ee93961bSJeff Mahoney 	block_number = SB_ROOT_BLOCK(sb);
6491da177e4SLinus Torvalds 	expected_level = -1;
6501da177e4SLinus Torvalds 	while (1) {
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds #ifdef CONFIG_REISERFS_CHECK
653ee93961bSJeff Mahoney 		if (!(++repeat_counter % 50000))
654a9dd3643SJeff Mahoney 			reiserfs_warning(sb, "PAP-5100",
65545b03d5eSJeff Mahoney 					 "%s: there were %d iterations of "
65645b03d5eSJeff Mahoney 					 "while loop looking for key %K",
657ee93961bSJeff Mahoney 					 current->comm, repeat_counter,
658d68caa95SJeff Mahoney 					 key);
6591da177e4SLinus Torvalds #endif
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 		/* prep path to have another element added to it. */
662d68caa95SJeff Mahoney 		last_element =
663d68caa95SJeff Mahoney 		    PATH_OFFSET_PELEMENT(search_path,
664d68caa95SJeff Mahoney 					 ++search_path->path_length);
665a9dd3643SJeff Mahoney 		fs_gen = get_generation(sb);
6661da177e4SLinus Torvalds 
667098297b2SJeff Mahoney 		/*
668098297b2SJeff Mahoney 		 * Read the next tree node, and set the last element
669098297b2SJeff Mahoney 		 * in the path to have a pointer to it.
670098297b2SJeff Mahoney 		 */
671d68caa95SJeff Mahoney 		if ((bh = last_element->pe_buffer =
672ee93961bSJeff Mahoney 		     sb_getblk(sb, block_number))) {
673278f6679SJeff Mahoney 
674278f6679SJeff Mahoney 			/*
675278f6679SJeff Mahoney 			 * We'll need to drop the lock if we encounter any
676278f6679SJeff Mahoney 			 * buffers that need to be read. If all of them are
677278f6679SJeff Mahoney 			 * already up to date, we don't need to drop the lock.
678278f6679SJeff Mahoney 			 */
679278f6679SJeff Mahoney 			int depth = -1;
6802ac62695SFrederic Weisbecker 
681ad31a4fcSJeff Mahoney 			if (!buffer_uptodate(bh) && reada_count > 1)
682278f6679SJeff Mahoney 				depth = search_by_key_reada(sb, reada_bh,
6831da177e4SLinus Torvalds 						    reada_blocks, reada_count);
684278f6679SJeff Mahoney 
685278f6679SJeff Mahoney 			if (!buffer_uptodate(bh) && depth == -1)
686278f6679SJeff Mahoney 				depth = reiserfs_write_unlock_nested(sb);
687278f6679SJeff Mahoney 
688d554822eSZhang Yi 			bh_read_nowait(bh, 0);
689ad31a4fcSJeff Mahoney 			wait_on_buffer(bh);
6902ac62695SFrederic Weisbecker 
691278f6679SJeff Mahoney 			if (depth != -1)
692278f6679SJeff Mahoney 				reiserfs_write_lock_nested(sb, depth);
693ad31a4fcSJeff Mahoney 			if (!buffer_uptodate(bh))
6941da177e4SLinus Torvalds 				goto io_error;
6951da177e4SLinus Torvalds 		} else {
6961da177e4SLinus Torvalds io_error:
697d68caa95SJeff Mahoney 			search_path->path_length--;
698d68caa95SJeff Mahoney 			pathrelse(search_path);
6991da177e4SLinus Torvalds 			return IO_ERROR;
7001da177e4SLinus Torvalds 		}
7011da177e4SLinus Torvalds 		reada_count = 0;
7021da177e4SLinus Torvalds 		if (expected_level == -1)
703a9dd3643SJeff Mahoney 			expected_level = SB_TREE_HEIGHT(sb);
7041da177e4SLinus Torvalds 		expected_level--;
7051da177e4SLinus Torvalds 
706098297b2SJeff Mahoney 		/*
707098297b2SJeff Mahoney 		 * It is possible that schedule occurred. We must check
708098297b2SJeff Mahoney 		 * whether the key to search is still in the tree rooted
709098297b2SJeff Mahoney 		 * from the current buffer. If not then repeat search
710098297b2SJeff Mahoney 		 * from the root.
711098297b2SJeff Mahoney 		 */
712a9dd3643SJeff Mahoney 		if (fs_changed(fs_gen, sb) &&
713ad31a4fcSJeff Mahoney 		    (!B_IS_IN_TREE(bh) ||
714ad31a4fcSJeff Mahoney 		     B_LEVEL(bh) != expected_level ||
715d68caa95SJeff Mahoney 		     !key_in_buffer(search_path, key, sb))) {
716a9dd3643SJeff Mahoney 			PROC_INFO_INC(sb, search_by_key_fs_changed);
717a9dd3643SJeff Mahoney 			PROC_INFO_INC(sb, search_by_key_restarted);
718a9dd3643SJeff Mahoney 			PROC_INFO_INC(sb,
719bd4c625cSLinus Torvalds 				      sbk_restarted[expected_level - 1]);
720d68caa95SJeff Mahoney 			pathrelse(search_path);
7211da177e4SLinus Torvalds 
722098297b2SJeff Mahoney 			/*
723098297b2SJeff Mahoney 			 * Get the root block number so that we can
724098297b2SJeff Mahoney 			 * repeat the search starting from the root.
725098297b2SJeff Mahoney 			 */
726ee93961bSJeff Mahoney 			block_number = SB_ROOT_BLOCK(sb);
7271da177e4SLinus Torvalds 			expected_level = -1;
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 			/* repeat search from the root */
7301da177e4SLinus Torvalds 			continue;
7311da177e4SLinus Torvalds 		}
7321da177e4SLinus Torvalds 
733098297b2SJeff Mahoney 		/*
734098297b2SJeff Mahoney 		 * only check that the key is in the buffer if key is not
735098297b2SJeff Mahoney 		 * equal to the MAX_KEY. Latter case is only possible in
736098297b2SJeff Mahoney 		 * "finish_unfinished()" processing during mount.
737098297b2SJeff Mahoney 		 */
738d68caa95SJeff Mahoney 		RFALSE(comp_keys(&MAX_KEY, key) &&
739d68caa95SJeff Mahoney 		       !key_in_buffer(search_path, key, sb),
7401da177e4SLinus Torvalds 		       "PAP-5130: key is not in the buffer");
7411da177e4SLinus Torvalds #ifdef CONFIG_REISERFS_CHECK
74208f14fc8SFrederic Weisbecker 		if (REISERFS_SB(sb)->cur_tb) {
7431da177e4SLinus Torvalds 			print_cur_tb("5140");
744a9dd3643SJeff Mahoney 			reiserfs_panic(sb, "PAP-5140",
745c3a9c210SJeff Mahoney 				       "schedule occurred in do_balance!");
7461da177e4SLinus Torvalds 		}
7471da177e4SLinus Torvalds #endif
7481da177e4SLinus Torvalds 
749098297b2SJeff Mahoney 		/*
750098297b2SJeff Mahoney 		 * make sure, that the node contents look like a node of
751098297b2SJeff Mahoney 		 * certain level
752098297b2SJeff Mahoney 		 */
753ad31a4fcSJeff Mahoney 		if (!is_tree_node(bh, expected_level)) {
754a9dd3643SJeff Mahoney 			reiserfs_error(sb, "vs-5150",
75545b03d5eSJeff Mahoney 				       "invalid format found in block %ld. "
756ad31a4fcSJeff Mahoney 				       "Fsck?", bh->b_blocknr);
757d68caa95SJeff Mahoney 			pathrelse(search_path);
7581da177e4SLinus Torvalds 			return IO_ERROR;
7591da177e4SLinus Torvalds 		}
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 		/* ok, we have acquired next formatted node in the tree */
762ee93961bSJeff Mahoney 		node_level = B_LEVEL(bh);
7631da177e4SLinus Torvalds 
764ee93961bSJeff Mahoney 		PROC_INFO_BH_STAT(sb, bh, node_level - 1);
7651da177e4SLinus Torvalds 
766ee93961bSJeff Mahoney 		RFALSE(node_level < stop_level,
7671da177e4SLinus Torvalds 		       "vs-5152: tree level (%d) is less than stop level (%d)",
768ee93961bSJeff Mahoney 		       node_level, stop_level);
7691da177e4SLinus Torvalds 
7704cf5f7adSJeff Mahoney 		retval = bin_search(key, item_head(bh, 0),
771ad31a4fcSJeff Mahoney 				      B_NR_ITEMS(bh),
772ee93961bSJeff Mahoney 				      (node_level ==
773bd4c625cSLinus Torvalds 				       DISK_LEAF_NODE_LEVEL) ? IH_SIZE :
774bd4c625cSLinus Torvalds 				      KEY_SIZE,
775a228bf8fSJeff Mahoney 				      &last_element->pe_position);
776ee93961bSJeff Mahoney 		if (node_level == stop_level) {
777ee93961bSJeff Mahoney 			return retval;
7781da177e4SLinus Torvalds 		}
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 		/* we are not in the stop level */
781098297b2SJeff Mahoney 		/*
782098297b2SJeff Mahoney 		 * item has been found, so we choose the pointer which
783098297b2SJeff Mahoney 		 * is to the right of the found one
784098297b2SJeff Mahoney 		 */
785ee93961bSJeff Mahoney 		if (retval == ITEM_FOUND)
786d68caa95SJeff Mahoney 			last_element->pe_position++;
7871da177e4SLinus Torvalds 
788098297b2SJeff Mahoney 		/*
789098297b2SJeff Mahoney 		 * if item was not found we choose the position which is to
790098297b2SJeff Mahoney 		 * the left of the found item. This requires no code,
791098297b2SJeff Mahoney 		 * bin_search did it already.
792098297b2SJeff Mahoney 		 */
7931da177e4SLinus Torvalds 
794098297b2SJeff Mahoney 		/*
795098297b2SJeff Mahoney 		 * So we have chosen a position in the current node which is
796098297b2SJeff Mahoney 		 * an internal node.  Now we calculate child block number by
797098297b2SJeff Mahoney 		 * position in the node.
798098297b2SJeff Mahoney 		 */
799ee93961bSJeff Mahoney 		block_number =
800d68caa95SJeff Mahoney 		    B_N_CHILD_NUM(bh, last_element->pe_position);
8011da177e4SLinus Torvalds 
802098297b2SJeff Mahoney 		/*
803098297b2SJeff Mahoney 		 * if we are going to read leaf nodes, try for read
804098297b2SJeff Mahoney 		 * ahead as well
805098297b2SJeff Mahoney 		 */
806d68caa95SJeff Mahoney 		if ((search_path->reada & PATH_READA) &&
807ee93961bSJeff Mahoney 		    node_level == DISK_LEAF_NODE_LEVEL + 1) {
808d68caa95SJeff Mahoney 			int pos = last_element->pe_position;
809ad31a4fcSJeff Mahoney 			int limit = B_NR_ITEMS(bh);
8101da177e4SLinus Torvalds 			struct reiserfs_key *le_key;
8111da177e4SLinus Torvalds 
812d68caa95SJeff Mahoney 			if (search_path->reada & PATH_READA_BACK)
8131da177e4SLinus Torvalds 				limit = 0;
8141da177e4SLinus Torvalds 			while (reada_count < SEARCH_BY_KEY_READA) {
8151da177e4SLinus Torvalds 				if (pos == limit)
8161da177e4SLinus Torvalds 					break;
817bd4c625cSLinus Torvalds 				reada_blocks[reada_count++] =
818ad31a4fcSJeff Mahoney 				    B_N_CHILD_NUM(bh, pos);
819d68caa95SJeff Mahoney 				if (search_path->reada & PATH_READA_BACK)
8201da177e4SLinus Torvalds 					pos--;
8211da177e4SLinus Torvalds 				else
8221da177e4SLinus Torvalds 					pos++;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 				/*
8251da177e4SLinus Torvalds 				 * check to make sure we're in the same object
8261da177e4SLinus Torvalds 				 */
8274cf5f7adSJeff Mahoney 				le_key = internal_key(bh, pos);
8281da177e4SLinus Torvalds 				if (le32_to_cpu(le_key->k_objectid) !=
829d68caa95SJeff Mahoney 				    key->on_disk_key.k_objectid) {
8301da177e4SLinus Torvalds 					break;
8311da177e4SLinus Torvalds 				}
8321da177e4SLinus Torvalds 			}
8331da177e4SLinus Torvalds 		}
8341da177e4SLinus Torvalds 	}
8351da177e4SLinus Torvalds }
8361da177e4SLinus Torvalds 
837098297b2SJeff Mahoney /*
838098297b2SJeff Mahoney  * Form the path to an item and position in this item which contains
839098297b2SJeff Mahoney  * file byte defined by key. If there is no such item
840098297b2SJeff Mahoney  * corresponding to the key, we point the path to the item with
841098297b2SJeff Mahoney  * maximal key less than key, and *pos_in_item is set to one
842098297b2SJeff Mahoney  * past the last entry/byte in the item.  If searching for entry in a
843098297b2SJeff Mahoney  * directory item, and it is not found, *pos_in_item is set to one
844098297b2SJeff Mahoney  * entry more than the entry with maximal key which is less than the
845098297b2SJeff Mahoney  * sought key.
846098297b2SJeff Mahoney  *
847098297b2SJeff Mahoney  * Note that if there is no entry in this same node which is one more,
848098297b2SJeff Mahoney  * then we point to an imaginary entry.  for direct items, the
849098297b2SJeff Mahoney  * position is in units of bytes, for indirect items the position is
850098297b2SJeff Mahoney  * in units of blocknr entries, for directory items the position is in
851098297b2SJeff Mahoney  * units of directory entries.
852098297b2SJeff Mahoney  */
8531da177e4SLinus Torvalds /* The function is NOT SCHEDULE-SAFE! */
search_for_position_by_key(struct super_block * sb,const struct cpu_key * p_cpu_key,struct treepath * search_path)854098297b2SJeff Mahoney int search_for_position_by_key(struct super_block *sb,
855098297b2SJeff Mahoney 			       /* Key to search (cpu variable) */
856098297b2SJeff Mahoney 			       const struct cpu_key *p_cpu_key,
857098297b2SJeff Mahoney 			       /* Filled up by this function. */
858098297b2SJeff Mahoney 			       struct treepath *search_path)
859bd4c625cSLinus Torvalds {
8601da177e4SLinus Torvalds 	struct item_head *p_le_ih;	/* pointer to on-disk structure */
861ee93961bSJeff Mahoney 	int blk_size;
8621da177e4SLinus Torvalds 	loff_t item_offset, offset;
8631da177e4SLinus Torvalds 	struct reiserfs_dir_entry de;
8641da177e4SLinus Torvalds 	int retval;
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	/* If searching for directory entry. */
8671da177e4SLinus Torvalds 	if (is_direntry_cpu_key(p_cpu_key))
868d68caa95SJeff Mahoney 		return search_by_entry_key(sb, p_cpu_key, search_path,
869bd4c625cSLinus Torvalds 					   &de);
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	/* If not searching for directory entry. */
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	/* If item is found. */
874d68caa95SJeff Mahoney 	retval = search_item(sb, p_cpu_key, search_path);
8751da177e4SLinus Torvalds 	if (retval == IO_ERROR)
8761da177e4SLinus Torvalds 		return retval;
8771da177e4SLinus Torvalds 	if (retval == ITEM_FOUND) {
8781da177e4SLinus Torvalds 
879bd4c625cSLinus Torvalds 		RFALSE(!ih_item_len
8804cf5f7adSJeff Mahoney 		       (item_head
881d68caa95SJeff Mahoney 			(PATH_PLAST_BUFFER(search_path),
882d68caa95SJeff Mahoney 			 PATH_LAST_POSITION(search_path))),
8831da177e4SLinus Torvalds 		       "PAP-5165: item length equals zero");
8841da177e4SLinus Torvalds 
885d68caa95SJeff Mahoney 		pos_in_item(search_path) = 0;
8861da177e4SLinus Torvalds 		return POSITION_FOUND;
8871da177e4SLinus Torvalds 	}
8881da177e4SLinus Torvalds 
889d68caa95SJeff Mahoney 	RFALSE(!PATH_LAST_POSITION(search_path),
8901da177e4SLinus Torvalds 	       "PAP-5170: position equals zero");
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	/* Item is not found. Set path to the previous item. */
893bd4c625cSLinus Torvalds 	p_le_ih =
8944cf5f7adSJeff Mahoney 	    item_head(PATH_PLAST_BUFFER(search_path),
895d68caa95SJeff Mahoney 			   --PATH_LAST_POSITION(search_path));
896ee93961bSJeff Mahoney 	blk_size = sb->s_blocksize;
8971da177e4SLinus Torvalds 
898a228bf8fSJeff Mahoney 	if (comp_short_keys(&p_le_ih->ih_key, p_cpu_key))
8991da177e4SLinus Torvalds 		return FILE_NOT_FOUND;
900098297b2SJeff Mahoney 
901098297b2SJeff Mahoney 	/* FIXME: quite ugly this far */
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	item_offset = le_ih_k_offset(p_le_ih);
9041da177e4SLinus Torvalds 	offset = cpu_key_k_offset(p_cpu_key);
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	/* Needed byte is contained in the item pointed to by the path. */
9071da177e4SLinus Torvalds 	if (item_offset <= offset &&
908ee93961bSJeff Mahoney 	    item_offset + op_bytes_number(p_le_ih, blk_size) > offset) {
909d68caa95SJeff Mahoney 		pos_in_item(search_path) = offset - item_offset;
9101da177e4SLinus Torvalds 		if (is_indirect_le_ih(p_le_ih)) {
911ee93961bSJeff Mahoney 			pos_in_item(search_path) /= blk_size;
9121da177e4SLinus Torvalds 		}
9131da177e4SLinus Torvalds 		return POSITION_FOUND;
9141da177e4SLinus Torvalds 	}
9151da177e4SLinus Torvalds 
916098297b2SJeff Mahoney 	/*
917098297b2SJeff Mahoney 	 * Needed byte is not contained in the item pointed to by the
918098297b2SJeff Mahoney 	 * path. Set pos_in_item out of the item.
919098297b2SJeff Mahoney 	 */
9201da177e4SLinus Torvalds 	if (is_indirect_le_ih(p_le_ih))
921d68caa95SJeff Mahoney 		pos_in_item(search_path) =
922bd4c625cSLinus Torvalds 		    ih_item_len(p_le_ih) / UNFM_P_SIZE;
9231da177e4SLinus Torvalds 	else
924d68caa95SJeff Mahoney 		pos_in_item(search_path) = ih_item_len(p_le_ih);
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	return POSITION_NOT_FOUND;
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds /* Compare given item and item pointed to by the path. */
comp_items(const struct item_head * stored_ih,const struct treepath * path)930d68caa95SJeff Mahoney int comp_items(const struct item_head *stored_ih, const struct treepath *path)
9311da177e4SLinus Torvalds {
932d68caa95SJeff Mahoney 	struct buffer_head *bh = PATH_PLAST_BUFFER(path);
9331da177e4SLinus Torvalds 	struct item_head *ih;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	/* Last buffer at the path is not in the tree. */
936ad31a4fcSJeff Mahoney 	if (!B_IS_IN_TREE(bh))
9371da177e4SLinus Torvalds 		return 1;
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	/* Last path position is invalid. */
940d68caa95SJeff Mahoney 	if (PATH_LAST_POSITION(path) >= B_NR_ITEMS(bh))
9411da177e4SLinus Torvalds 		return 1;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	/* we need only to know, whether it is the same item */
9444cf5f7adSJeff Mahoney 	ih = tp_item_head(path);
9451da177e4SLinus Torvalds 	return memcmp(stored_ih, ih, IH_SIZE);
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
948098297b2SJeff Mahoney /* prepare for delete or cut of direct item */
prepare_for_direct_item(struct treepath * path,struct item_head * le_ih,struct inode * inode,loff_t new_file_length,int * cut_size)949fec6d055SJosef "Jeff" Sipek static inline int prepare_for_direct_item(struct treepath *path,
9501da177e4SLinus Torvalds 					  struct item_head *le_ih,
9511da177e4SLinus Torvalds 					  struct inode *inode,
952bd4c625cSLinus Torvalds 					  loff_t new_file_length, int *cut_size)
9531da177e4SLinus Torvalds {
9541da177e4SLinus Torvalds 	loff_t round_len;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	if (new_file_length == max_reiserfs_offset(inode)) {
9571da177e4SLinus Torvalds 		/* item has to be deleted */
9581da177e4SLinus Torvalds 		*cut_size = -(IH_SIZE + ih_item_len(le_ih));
9591da177e4SLinus Torvalds 		return M_DELETE;
9601da177e4SLinus Torvalds 	}
961098297b2SJeff Mahoney 	/* new file gets truncated */
9621da177e4SLinus Torvalds 	if (get_inode_item_key_version(inode) == KEY_FORMAT_3_6) {
9631da177e4SLinus Torvalds 		round_len = ROUND_UP(new_file_length);
964ee93961bSJeff Mahoney 		/* this was new_file_length < le_ih ... */
9651da177e4SLinus Torvalds 		if (round_len < le_ih_k_offset(le_ih)) {
9661da177e4SLinus Torvalds 			*cut_size = -(IH_SIZE + ih_item_len(le_ih));
9671da177e4SLinus Torvalds 			return M_DELETE;	/* Delete this item. */
9681da177e4SLinus Torvalds 		}
9691da177e4SLinus Torvalds 		/* Calculate first position and size for cutting from item. */
9701da177e4SLinus Torvalds 		pos_in_item(path) = round_len - (le_ih_k_offset(le_ih) - 1);
9711da177e4SLinus Torvalds 		*cut_size = -(ih_item_len(le_ih) - pos_in_item(path));
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 		return M_CUT;	/* Cut from this item. */
9741da177e4SLinus Torvalds 	}
9751da177e4SLinus Torvalds 
976098297b2SJeff Mahoney 	/* old file: items may have any length */
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	if (new_file_length < le_ih_k_offset(le_ih)) {
9791da177e4SLinus Torvalds 		*cut_size = -(IH_SIZE + ih_item_len(le_ih));
9801da177e4SLinus Torvalds 		return M_DELETE;	/* Delete this item. */
9811da177e4SLinus Torvalds 	}
982098297b2SJeff Mahoney 
9831da177e4SLinus Torvalds 	/* Calculate first position and size for cutting from item. */
9841da177e4SLinus Torvalds 	*cut_size = -(ih_item_len(le_ih) -
985bd4c625cSLinus Torvalds 		      (pos_in_item(path) =
986bd4c625cSLinus Torvalds 		       new_file_length + 1 - le_ih_k_offset(le_ih)));
9871da177e4SLinus Torvalds 	return M_CUT;		/* Cut from this item. */
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
prepare_for_direntry_item(struct treepath * path,struct item_head * le_ih,struct inode * inode,loff_t new_file_length,int * cut_size)990fec6d055SJosef "Jeff" Sipek static inline int prepare_for_direntry_item(struct treepath *path,
9911da177e4SLinus Torvalds 					    struct item_head *le_ih,
9921da177e4SLinus Torvalds 					    struct inode *inode,
9931da177e4SLinus Torvalds 					    loff_t new_file_length,
9941da177e4SLinus Torvalds 					    int *cut_size)
9951da177e4SLinus Torvalds {
9961da177e4SLinus Torvalds 	if (le_ih_k_offset(le_ih) == DOT_OFFSET &&
9971da177e4SLinus Torvalds 	    new_file_length == max_reiserfs_offset(inode)) {
9981da177e4SLinus Torvalds 		RFALSE(ih_entry_count(le_ih) != 2,
9991da177e4SLinus Torvalds 		       "PAP-5220: incorrect empty directory item (%h)", le_ih);
10001da177e4SLinus Torvalds 		*cut_size = -(IH_SIZE + ih_item_len(le_ih));
1001098297b2SJeff Mahoney 		/* Delete the directory item containing "." and ".." entry. */
1002098297b2SJeff Mahoney 		return M_DELETE;
10031da177e4SLinus Torvalds 	}
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	if (ih_entry_count(le_ih) == 1) {
1006098297b2SJeff Mahoney 		/*
1007098297b2SJeff Mahoney 		 * Delete the directory item such as there is one record only
1008098297b2SJeff Mahoney 		 * in this item
1009098297b2SJeff Mahoney 		 */
10101da177e4SLinus Torvalds 		*cut_size = -(IH_SIZE + ih_item_len(le_ih));
10111da177e4SLinus Torvalds 		return M_DELETE;
10121da177e4SLinus Torvalds 	}
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	/* Cut one record from the directory item. */
1015bd4c625cSLinus Torvalds 	*cut_size =
1016bd4c625cSLinus Torvalds 	    -(DEH_SIZE +
1017bd4c625cSLinus Torvalds 	      entry_length(get_last_bh(path), le_ih, pos_in_item(path)));
10181da177e4SLinus Torvalds 	return M_CUT;
10191da177e4SLinus Torvalds }
10201da177e4SLinus Torvalds 
102123f9e0f8SAlexander Zarochentzev #define JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD (2 * JOURNAL_PER_BALANCE_CNT + 1)
102223f9e0f8SAlexander Zarochentzev 
1023098297b2SJeff Mahoney /*
1024098297b2SJeff Mahoney  * If the path points to a directory or direct item, calculate mode
1025098297b2SJeff Mahoney  * and the size cut, for balance.
1026098297b2SJeff Mahoney  * If the path points to an indirect item, remove some number of its
1027098297b2SJeff Mahoney  * unformatted nodes.
1028098297b2SJeff Mahoney  * In case of file truncate calculate whether this item must be
1029098297b2SJeff Mahoney  * deleted/truncated or last unformatted node of this item will be
1030098297b2SJeff Mahoney  * converted to a direct item.
1031098297b2SJeff Mahoney  * This function returns a determination of what balance mode the
1032098297b2SJeff Mahoney  * calling function should employ.
1033098297b2SJeff Mahoney  */
prepare_for_delete_or_cut(struct reiserfs_transaction_handle * th,struct inode * inode,struct treepath * path,const struct cpu_key * item_key,int * removed,int * cut_size,unsigned long long new_file_length)1034098297b2SJeff Mahoney static char prepare_for_delete_or_cut(struct reiserfs_transaction_handle *th,
1035098297b2SJeff Mahoney 				      struct inode *inode,
1036098297b2SJeff Mahoney 				      struct treepath *path,
1037098297b2SJeff Mahoney 				      const struct cpu_key *item_key,
1038098297b2SJeff Mahoney 				      /*
1039098297b2SJeff Mahoney 				       * Number of unformatted nodes
1040098297b2SJeff Mahoney 				       * which were removed from end
1041098297b2SJeff Mahoney 				       * of the file.
1042098297b2SJeff Mahoney 				       */
1043098297b2SJeff Mahoney 				      int *removed,
1044098297b2SJeff Mahoney 				      int *cut_size,
1045098297b2SJeff Mahoney 				      /* MAX_KEY_OFFSET in case of delete. */
1046098297b2SJeff Mahoney 				      unsigned long long new_file_length
1047bd4c625cSLinus Torvalds     )
1048bd4c625cSLinus Torvalds {
1049a9dd3643SJeff Mahoney 	struct super_block *sb = inode->i_sb;
10504cf5f7adSJeff Mahoney 	struct item_head *p_le_ih = tp_item_head(path);
1051d68caa95SJeff Mahoney 	struct buffer_head *bh = PATH_PLAST_BUFFER(path);
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 	BUG_ON(!th->t_trans_id);
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	/* Stat_data item. */
10561da177e4SLinus Torvalds 	if (is_statdata_le_ih(p_le_ih)) {
10571da177e4SLinus Torvalds 
1058ee93961bSJeff Mahoney 		RFALSE(new_file_length != max_reiserfs_offset(inode),
10591da177e4SLinus Torvalds 		       "PAP-5210: mode must be M_DELETE");
10601da177e4SLinus Torvalds 
1061d68caa95SJeff Mahoney 		*cut_size = -(IH_SIZE + ih_item_len(p_le_ih));
10621da177e4SLinus Torvalds 		return M_DELETE;
10631da177e4SLinus Torvalds 	}
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	/* Directory item. */
10661da177e4SLinus Torvalds 	if (is_direntry_le_ih(p_le_ih))
1067d68caa95SJeff Mahoney 		return prepare_for_direntry_item(path, p_le_ih, inode,
1068ee93961bSJeff Mahoney 						 new_file_length,
1069d68caa95SJeff Mahoney 						 cut_size);
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	/* Direct item. */
10721da177e4SLinus Torvalds 	if (is_direct_le_ih(p_le_ih))
1073d68caa95SJeff Mahoney 		return prepare_for_direct_item(path, p_le_ih, inode,
1074ee93961bSJeff Mahoney 					       new_file_length, cut_size);
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds 	/* Case of an indirect item. */
10771da177e4SLinus Torvalds 	{
1078a9dd3643SJeff Mahoney 	    int blk_size = sb->s_blocksize;
107923f9e0f8SAlexander Zarochentzev 	    struct item_head s_ih;
108023f9e0f8SAlexander Zarochentzev 	    int need_re_search;
108123f9e0f8SAlexander Zarochentzev 	    int delete = 0;
108223f9e0f8SAlexander Zarochentzev 	    int result = M_CUT;
108323f9e0f8SAlexander Zarochentzev 	    int pos = 0;
10841da177e4SLinus Torvalds 
1085ee93961bSJeff Mahoney 	    if ( new_file_length == max_reiserfs_offset (inode) ) {
1086098297b2SJeff Mahoney 		/*
1087098297b2SJeff Mahoney 		 * prepare_for_delete_or_cut() is called by
1088098297b2SJeff Mahoney 		 * reiserfs_delete_item()
1089098297b2SJeff Mahoney 		 */
1090ee93961bSJeff Mahoney 		new_file_length = 0;
109123f9e0f8SAlexander Zarochentzev 		delete = 1;
109223f9e0f8SAlexander Zarochentzev 	    }
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	    do {
109523f9e0f8SAlexander Zarochentzev 		need_re_search = 0;
1096d68caa95SJeff Mahoney 		*cut_size = 0;
1097d68caa95SJeff Mahoney 		bh = PATH_PLAST_BUFFER(path);
10984cf5f7adSJeff Mahoney 		copy_item_head(&s_ih, tp_item_head(path));
109923f9e0f8SAlexander Zarochentzev 		pos = I_UNFM_NUM(&s_ih);
11001da177e4SLinus Torvalds 
1101ee93961bSJeff Mahoney 		while (le_ih_k_offset (&s_ih) + (pos - 1) * blk_size > new_file_length) {
110287588dd6SAl Viro 		    __le32 *unfm;
110387588dd6SAl Viro 		    __u32 block;
11041da177e4SLinus Torvalds 
1105098297b2SJeff Mahoney 		    /*
1106098297b2SJeff Mahoney 		     * Each unformatted block deletion may involve
1107098297b2SJeff Mahoney 		     * one additional bitmap block into the transaction,
1108098297b2SJeff Mahoney 		     * thereby the initial journal space reservation
1109098297b2SJeff Mahoney 		     * might not be enough.
1110098297b2SJeff Mahoney 		     */
1111d68caa95SJeff Mahoney 		    if (!delete && (*cut_size) != 0 &&
1112d68caa95SJeff Mahoney 			reiserfs_transaction_free_space(th) < JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD)
111323f9e0f8SAlexander Zarochentzev 			break;
11141da177e4SLinus Torvalds 
11154cf5f7adSJeff Mahoney 		    unfm = (__le32 *)ih_item_body(bh, &s_ih) + pos - 1;
111623f9e0f8SAlexander Zarochentzev 		    block = get_block_num(unfm, 0);
11171da177e4SLinus Torvalds 
111823f9e0f8SAlexander Zarochentzev 		    if (block != 0) {
1119ad31a4fcSJeff Mahoney 			reiserfs_prepare_for_journal(sb, bh, 1);
112023f9e0f8SAlexander Zarochentzev 			put_block_num(unfm, 0, 0);
112109f1b80bSJeff Mahoney 			journal_mark_dirty(th, bh);
112223f9e0f8SAlexander Zarochentzev 			reiserfs_free_block(th, inode, block, 1);
112323f9e0f8SAlexander Zarochentzev 		    }
11241da177e4SLinus Torvalds 
1125278f6679SJeff Mahoney 		    reiserfs_cond_resched(sb);
112623f9e0f8SAlexander Zarochentzev 
1127d68caa95SJeff Mahoney 		    if (item_moved (&s_ih, path))  {
112823f9e0f8SAlexander Zarochentzev 			need_re_search = 1;
11291da177e4SLinus Torvalds 			break;
11301da177e4SLinus Torvalds 		    }
11311da177e4SLinus Torvalds 
113223f9e0f8SAlexander Zarochentzev 		    pos --;
1133d68caa95SJeff Mahoney 		    (*removed)++;
1134d68caa95SJeff Mahoney 		    (*cut_size) -= UNFM_P_SIZE;
11351da177e4SLinus Torvalds 
113623f9e0f8SAlexander Zarochentzev 		    if (pos == 0) {
1137d68caa95SJeff Mahoney 			(*cut_size) -= IH_SIZE;
113823f9e0f8SAlexander Zarochentzev 			result = M_DELETE;
11391da177e4SLinus Torvalds 			break;
11401da177e4SLinus Torvalds 		    }
11411da177e4SLinus Torvalds 		}
1142098297b2SJeff Mahoney 		/*
1143098297b2SJeff Mahoney 		 * a trick.  If the buffer has been logged, this will
1144098297b2SJeff Mahoney 		 * do nothing.  If we've broken the loop without logging
1145098297b2SJeff Mahoney 		 * it, it will restore the buffer
1146098297b2SJeff Mahoney 		 */
1147ad31a4fcSJeff Mahoney 		reiserfs_restore_prepared_buffer(sb, bh);
114823f9e0f8SAlexander Zarochentzev 	    } while (need_re_search &&
1149d68caa95SJeff Mahoney 		     search_for_position_by_key(sb, item_key, path) == POSITION_FOUND);
1150d68caa95SJeff Mahoney 	    pos_in_item(path) = pos * UNFM_P_SIZE;
11511da177e4SLinus Torvalds 
1152d68caa95SJeff Mahoney 	    if (*cut_size == 0) {
1153098297b2SJeff Mahoney 		/*
1154098297b2SJeff Mahoney 		 * Nothing was cut. maybe convert last unformatted node to the
1155098297b2SJeff Mahoney 		 * direct item?
1156098297b2SJeff Mahoney 		 */
115723f9e0f8SAlexander Zarochentzev 		result = M_CONVERT;
115823f9e0f8SAlexander Zarochentzev 	    }
115923f9e0f8SAlexander Zarochentzev 	    return result;
11601da177e4SLinus Torvalds 	}
11611da177e4SLinus Torvalds }
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds /* Calculate number of bytes which will be deleted or cut during balance */
calc_deleted_bytes_number(struct tree_balance * tb,char mode)1164ee93961bSJeff Mahoney static int calc_deleted_bytes_number(struct tree_balance *tb, char mode)
1165bd4c625cSLinus Torvalds {
1166ee93961bSJeff Mahoney 	int del_size;
11674cf5f7adSJeff Mahoney 	struct item_head *p_le_ih = tp_item_head(tb->tb_path);
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	if (is_statdata_le_ih(p_le_ih))
11701da177e4SLinus Torvalds 		return 0;
11711da177e4SLinus Torvalds 
1172ee93961bSJeff Mahoney 	del_size =
1173ee93961bSJeff Mahoney 	    (mode ==
1174a063ae17SJeff Mahoney 	     M_DELETE) ? ih_item_len(p_le_ih) : -tb->insert_size[0];
11751da177e4SLinus Torvalds 	if (is_direntry_le_ih(p_le_ih)) {
1176098297b2SJeff Mahoney 		/*
1177098297b2SJeff Mahoney 		 * return EMPTY_DIR_SIZE; We delete emty directories only.
1178098297b2SJeff Mahoney 		 * we can't use EMPTY_DIR_SIZE, as old format dirs have a
1179098297b2SJeff Mahoney 		 * different empty size.  ick. FIXME, is this right?
1180098297b2SJeff Mahoney 		 */
1181ee93961bSJeff Mahoney 		return del_size;
11821da177e4SLinus Torvalds 	}
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 	if (is_indirect_le_ih(p_le_ih))
1185ee93961bSJeff Mahoney 		del_size = (del_size / UNFM_P_SIZE) *
1186a063ae17SJeff Mahoney 				(PATH_PLAST_BUFFER(tb->tb_path)->b_size);
1187ee93961bSJeff Mahoney 	return del_size;
11881da177e4SLinus Torvalds }
11891da177e4SLinus Torvalds 
init_tb_struct(struct reiserfs_transaction_handle * th,struct tree_balance * tb,struct super_block * sb,struct treepath * path,int size)1190bd4c625cSLinus Torvalds static void init_tb_struct(struct reiserfs_transaction_handle *th,
1191a063ae17SJeff Mahoney 			   struct tree_balance *tb,
1192a9dd3643SJeff Mahoney 			   struct super_block *sb,
1193ee93961bSJeff Mahoney 			   struct treepath *path, int size)
1194bd4c625cSLinus Torvalds {
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	BUG_ON(!th->t_trans_id);
11971da177e4SLinus Torvalds 
1198a063ae17SJeff Mahoney 	memset(tb, '\0', sizeof(struct tree_balance));
1199a063ae17SJeff Mahoney 	tb->transaction_handle = th;
1200a063ae17SJeff Mahoney 	tb->tb_sb = sb;
1201d68caa95SJeff Mahoney 	tb->tb_path = path;
1202d68caa95SJeff Mahoney 	PATH_OFFSET_PBUFFER(path, ILLEGAL_PATH_ELEMENT_OFFSET) = NULL;
1203d68caa95SJeff Mahoney 	PATH_OFFSET_POSITION(path, ILLEGAL_PATH_ELEMENT_OFFSET) = 0;
1204ee93961bSJeff Mahoney 	tb->insert_size[0] = size;
12051da177e4SLinus Torvalds }
12061da177e4SLinus Torvalds 
padd_item(char * item,int total_length,int length)12071da177e4SLinus Torvalds void padd_item(char *item, int total_length, int length)
12081da177e4SLinus Torvalds {
12091da177e4SLinus Torvalds 	int i;
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	for (i = total_length; i > length;)
12121da177e4SLinus Torvalds 		item[--i] = 0;
12131da177e4SLinus Torvalds }
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds #ifdef REISERQUOTA_DEBUG
key2type(struct reiserfs_key * ih)12161da177e4SLinus Torvalds char key2type(struct reiserfs_key *ih)
12171da177e4SLinus Torvalds {
12181da177e4SLinus Torvalds 	if (is_direntry_le_key(2, ih))
12191da177e4SLinus Torvalds 		return 'd';
12201da177e4SLinus Torvalds 	if (is_direct_le_key(2, ih))
12211da177e4SLinus Torvalds 		return 'D';
12221da177e4SLinus Torvalds 	if (is_indirect_le_key(2, ih))
12231da177e4SLinus Torvalds 		return 'i';
12241da177e4SLinus Torvalds 	if (is_statdata_le_key(2, ih))
12251da177e4SLinus Torvalds 		return 's';
12261da177e4SLinus Torvalds 	return 'u';
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
head2type(struct item_head * ih)12291da177e4SLinus Torvalds char head2type(struct item_head *ih)
12301da177e4SLinus Torvalds {
12311da177e4SLinus Torvalds 	if (is_direntry_le_ih(ih))
12321da177e4SLinus Torvalds 		return 'd';
12331da177e4SLinus Torvalds 	if (is_direct_le_ih(ih))
12341da177e4SLinus Torvalds 		return 'D';
12351da177e4SLinus Torvalds 	if (is_indirect_le_ih(ih))
12361da177e4SLinus Torvalds 		return 'i';
12371da177e4SLinus Torvalds 	if (is_statdata_le_ih(ih))
12381da177e4SLinus Torvalds 		return 's';
12391da177e4SLinus Torvalds 	return 'u';
12401da177e4SLinus Torvalds }
12411da177e4SLinus Torvalds #endif
12421da177e4SLinus Torvalds 
1243098297b2SJeff Mahoney /*
1244098297b2SJeff Mahoney  * Delete object item.
1245d68caa95SJeff Mahoney  * th       - active transaction handle
1246d68caa95SJeff Mahoney  * path     - path to the deleted item
1247d68caa95SJeff Mahoney  * item_key - key to search for the deleted item
1248d68caa95SJeff Mahoney  * indode   - used for updating i_blocks and quotas
1249d68caa95SJeff Mahoney  * un_bh    - NULL or unformatted node pointer
1250d68caa95SJeff Mahoney  */
reiserfs_delete_item(struct reiserfs_transaction_handle * th,struct treepath * path,const struct cpu_key * item_key,struct inode * inode,struct buffer_head * un_bh)1251d68caa95SJeff Mahoney int reiserfs_delete_item(struct reiserfs_transaction_handle *th,
1252d68caa95SJeff Mahoney 			 struct treepath *path, const struct cpu_key *item_key,
1253d68caa95SJeff Mahoney 			 struct inode *inode, struct buffer_head *un_bh)
1254d68caa95SJeff Mahoney {
1255995c762eSJeff Mahoney 	struct super_block *sb = inode->i_sb;
12561da177e4SLinus Torvalds 	struct tree_balance s_del_balance;
12571da177e4SLinus Torvalds 	struct item_head s_ih;
12581da177e4SLinus Torvalds 	struct item_head *q_ih;
12591da177e4SLinus Torvalds 	int quota_cut_bytes;
1260ee93961bSJeff Mahoney 	int ret_value, del_size, removed;
1261d2d0395fSJeff Mahoney 	int depth;
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds #ifdef CONFIG_REISERFS_CHECK
1264ee93961bSJeff Mahoney 	char mode;
12651da177e4SLinus Torvalds #endif
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	BUG_ON(!th->t_trans_id);
12681da177e4SLinus Torvalds 
1269d68caa95SJeff Mahoney 	init_tb_struct(th, &s_del_balance, sb, path,
1270bd4c625cSLinus Torvalds 		       0 /*size is unknown */ );
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	while (1) {
1273ee93961bSJeff Mahoney 		removed = 0;
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds #ifdef CONFIG_REISERFS_CHECK
1276ee93961bSJeff Mahoney 		mode =
12771da177e4SLinus Torvalds #endif
1278d68caa95SJeff Mahoney 		    prepare_for_delete_or_cut(th, inode, path,
1279ee93961bSJeff Mahoney 					      item_key, &removed,
1280ee93961bSJeff Mahoney 					      &del_size,
1281995c762eSJeff Mahoney 					      max_reiserfs_offset(inode));
12821da177e4SLinus Torvalds 
1283ee93961bSJeff Mahoney 		RFALSE(mode != M_DELETE, "PAP-5320: mode must be M_DELETE");
12841da177e4SLinus Torvalds 
12854cf5f7adSJeff Mahoney 		copy_item_head(&s_ih, tp_item_head(path));
1286ee93961bSJeff Mahoney 		s_del_balance.insert_size[0] = del_size;
12871da177e4SLinus Torvalds 
1288ee93961bSJeff Mahoney 		ret_value = fix_nodes(M_DELETE, &s_del_balance, NULL, NULL);
1289ee93961bSJeff Mahoney 		if (ret_value != REPEAT_SEARCH)
12901da177e4SLinus Torvalds 			break;
12911da177e4SLinus Torvalds 
1292a9dd3643SJeff Mahoney 		PROC_INFO_INC(sb, delete_item_restarted);
12931da177e4SLinus Torvalds 
1294098297b2SJeff Mahoney 		/* file system changed, repeat search */
1295ee93961bSJeff Mahoney 		ret_value =
1296d68caa95SJeff Mahoney 		    search_for_position_by_key(sb, item_key, path);
1297ee93961bSJeff Mahoney 		if (ret_value == IO_ERROR)
12981da177e4SLinus Torvalds 			break;
1299ee93961bSJeff Mahoney 		if (ret_value == FILE_NOT_FOUND) {
1300a9dd3643SJeff Mahoney 			reiserfs_warning(sb, "vs-5340",
1301bd4c625cSLinus Torvalds 					 "no items of the file %K found",
1302d68caa95SJeff Mahoney 					 item_key);
13031da177e4SLinus Torvalds 			break;
13041da177e4SLinus Torvalds 		}
13051da177e4SLinus Torvalds 	}			/* while (1) */
13061da177e4SLinus Torvalds 
1307ee93961bSJeff Mahoney 	if (ret_value != CARRY_ON) {
13081da177e4SLinus Torvalds 		unfix_nodes(&s_del_balance);
13091da177e4SLinus Torvalds 		return 0;
13101da177e4SLinus Torvalds 	}
1311098297b2SJeff Mahoney 
1312098297b2SJeff Mahoney 	/* reiserfs_delete_item returns item length when success */
1313ee93961bSJeff Mahoney 	ret_value = calc_deleted_bytes_number(&s_del_balance, M_DELETE);
13144cf5f7adSJeff Mahoney 	q_ih = tp_item_head(path);
13151da177e4SLinus Torvalds 	quota_cut_bytes = ih_item_len(q_ih);
13161da177e4SLinus Torvalds 
1317098297b2SJeff Mahoney 	/*
1318098297b2SJeff Mahoney 	 * hack so the quota code doesn't have to guess if the file has a
1319098297b2SJeff Mahoney 	 * tail.  On tail insert, we allocate quota for 1 unformatted node.
1320098297b2SJeff Mahoney 	 * We test the offset because the tail might have been
1321098297b2SJeff Mahoney 	 * split into multiple items, and we only want to decrement for
1322098297b2SJeff Mahoney 	 * the unfm node once
13231da177e4SLinus Torvalds 	 */
1324995c762eSJeff Mahoney 	if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(q_ih)) {
1325a9dd3643SJeff Mahoney 		if ((le_ih_k_offset(q_ih) & (sb->s_blocksize - 1)) == 1) {
1326a9dd3643SJeff Mahoney 			quota_cut_bytes = sb->s_blocksize + UNFM_P_SIZE;
13271da177e4SLinus Torvalds 		} else {
13281da177e4SLinus Torvalds 			quota_cut_bytes = 0;
13291da177e4SLinus Torvalds 		}
13301da177e4SLinus Torvalds 	}
13311da177e4SLinus Torvalds 
1332d68caa95SJeff Mahoney 	if (un_bh) {
13331da177e4SLinus Torvalds 		int off;
13341da177e4SLinus Torvalds 		char *data;
13351da177e4SLinus Torvalds 
1336098297b2SJeff Mahoney 		/*
1337098297b2SJeff Mahoney 		 * We are in direct2indirect conversion, so move tail contents
1338098297b2SJeff Mahoney 		 * to the unformatted node
1339098297b2SJeff Mahoney 		 */
1340098297b2SJeff Mahoney 		/*
1341098297b2SJeff Mahoney 		 * note, we do the copy before preparing the buffer because we
1342098297b2SJeff Mahoney 		 * don't care about the contents of the unformatted node yet.
1343098297b2SJeff Mahoney 		 * the only thing we really care about is the direct item's
1344098297b2SJeff Mahoney 		 * data is in the unformatted node.
1345098297b2SJeff Mahoney 		 *
1346098297b2SJeff Mahoney 		 * Otherwise, we would have to call
1347098297b2SJeff Mahoney 		 * reiserfs_prepare_for_journal on the unformatted node,
1348098297b2SJeff Mahoney 		 * which might schedule, meaning we'd have to loop all the
1349098297b2SJeff Mahoney 		 * way back up to the start of the while loop.
1350098297b2SJeff Mahoney 		 *
1351098297b2SJeff Mahoney 		 * The unformatted node must be dirtied later on.  We can't be
1352098297b2SJeff Mahoney 		 * sure here if the entire tail has been deleted yet.
1353098297b2SJeff Mahoney 		 *
1354098297b2SJeff Mahoney 		 * un_bh is from the page cache (all unformatted nodes are
1355098297b2SJeff Mahoney 		 * from the page cache) and might be a highmem page.  So, we
1356098297b2SJeff Mahoney 		 * can't use un_bh->b_data.
1357098297b2SJeff Mahoney 		 * -clm
13581da177e4SLinus Torvalds 		 */
13591da177e4SLinus Torvalds 
1360883da600SCong Wang 		data = kmap_atomic(un_bh->b_page);
136109cbfeafSKirill A. Shutemov 		off = ((le_ih_k_offset(&s_ih) - 1) & (PAGE_SIZE - 1));
13621da177e4SLinus Torvalds 		memcpy(data + off,
13634cf5f7adSJeff Mahoney 		       ih_item_body(PATH_PLAST_BUFFER(path), &s_ih),
1364ee93961bSJeff Mahoney 		       ret_value);
1365883da600SCong Wang 		kunmap_atomic(data);
13661da177e4SLinus Torvalds 	}
1367098297b2SJeff Mahoney 
13681da177e4SLinus Torvalds 	/* Perform balancing after all resources have been collected at once. */
13691da177e4SLinus Torvalds 	do_balance(&s_del_balance, NULL, NULL, M_DELETE);
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds #ifdef REISERQUOTA_DEBUG
1372a9dd3643SJeff Mahoney 	reiserfs_debug(sb, REISERFS_DEBUG_CODE,
1373bd4c625cSLinus Torvalds 		       "reiserquota delete_item(): freeing %u, id=%u type=%c",
1374995c762eSJeff Mahoney 		       quota_cut_bytes, inode->i_uid, head2type(&s_ih));
13751da177e4SLinus Torvalds #endif
1376d2d0395fSJeff Mahoney 	depth = reiserfs_write_unlock_nested(inode->i_sb);
13775dd4056dSChristoph Hellwig 	dquot_free_space_nodirty(inode, quota_cut_bytes);
1378d2d0395fSJeff Mahoney 	reiserfs_write_lock_nested(inode->i_sb, depth);
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	/* Return deleted body length */
1381ee93961bSJeff Mahoney 	return ret_value;
13821da177e4SLinus Torvalds }
13831da177e4SLinus Torvalds 
1384098297b2SJeff Mahoney /*
1385098297b2SJeff Mahoney  * Summary Of Mechanisms For Handling Collisions Between Processes:
1386098297b2SJeff Mahoney  *
1387098297b2SJeff Mahoney  *  deletion of the body of the object is performed by iput(), with the
1388098297b2SJeff Mahoney  *  result that if multiple processes are operating on a file, the
1389098297b2SJeff Mahoney  *  deletion of the body of the file is deferred until the last process
1390098297b2SJeff Mahoney  *  that has an open inode performs its iput().
1391098297b2SJeff Mahoney  *
1392098297b2SJeff Mahoney  *  writes and truncates are protected from collisions by use of
1393098297b2SJeff Mahoney  *  semaphores.
1394098297b2SJeff Mahoney  *
1395098297b2SJeff Mahoney  *  creates, linking, and mknod are protected from collisions with other
1396098297b2SJeff Mahoney  *  processes by making the reiserfs_add_entry() the last step in the
1397098297b2SJeff Mahoney  *  creation, and then rolling back all changes if there was a collision.
1398098297b2SJeff Mahoney  *  - Hans
13991da177e4SLinus Torvalds */
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds /* this deletes item which never gets split */
reiserfs_delete_solid_item(struct reiserfs_transaction_handle * th,struct inode * inode,struct reiserfs_key * key)14021da177e4SLinus Torvalds void reiserfs_delete_solid_item(struct reiserfs_transaction_handle *th,
1403bd4c625cSLinus Torvalds 				struct inode *inode, struct reiserfs_key *key)
14041da177e4SLinus Torvalds {
1405d2d0395fSJeff Mahoney 	struct super_block *sb = th->t_super;
14061da177e4SLinus Torvalds 	struct tree_balance tb;
14071da177e4SLinus Torvalds 	INITIALIZE_PATH(path);
14081da177e4SLinus Torvalds 	int item_len = 0;
14091da177e4SLinus Torvalds 	int tb_init = 0;
1410*dd8f87f2SEdward Adam Davis 	struct cpu_key cpu_key = {};
14111da177e4SLinus Torvalds 	int retval;
14121da177e4SLinus Torvalds 	int quota_cut_bytes = 0;
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	BUG_ON(!th->t_trans_id);
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 	le_key2cpu_key(&cpu_key, key);
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 	while (1) {
14191da177e4SLinus Torvalds 		retval = search_item(th->t_super, &cpu_key, &path);
14201da177e4SLinus Torvalds 		if (retval == IO_ERROR) {
14210030b645SJeff Mahoney 			reiserfs_error(th->t_super, "vs-5350",
142245b03d5eSJeff Mahoney 				       "i/o failure occurred trying "
142345b03d5eSJeff Mahoney 				       "to delete %K", &cpu_key);
14241da177e4SLinus Torvalds 			break;
14251da177e4SLinus Torvalds 		}
14261da177e4SLinus Torvalds 		if (retval != ITEM_FOUND) {
14271da177e4SLinus Torvalds 			pathrelse(&path);
1428098297b2SJeff Mahoney 			/*
1429098297b2SJeff Mahoney 			 * No need for a warning, if there is just no free
1430098297b2SJeff Mahoney 			 * space to insert '..' item into the
1431098297b2SJeff Mahoney 			 * newly-created subdir
1432098297b2SJeff Mahoney 			 */
1433bd4c625cSLinus Torvalds 			if (!
1434bd4c625cSLinus Torvalds 			    ((unsigned long long)
1435bd4c625cSLinus Torvalds 			     GET_HASH_VALUE(le_key_k_offset
1436bd4c625cSLinus Torvalds 					    (le_key_version(key), key)) == 0
1437bd4c625cSLinus Torvalds 			     && (unsigned long long)
1438bd4c625cSLinus Torvalds 			     GET_GENERATION_NUMBER(le_key_k_offset
1439bd4c625cSLinus Torvalds 						   (le_key_version(key),
1440bd4c625cSLinus Torvalds 						    key)) == 1))
144145b03d5eSJeff Mahoney 				reiserfs_warning(th->t_super, "vs-5355",
144245b03d5eSJeff Mahoney 						 "%k not found", key);
14431da177e4SLinus Torvalds 			break;
14441da177e4SLinus Torvalds 		}
14451da177e4SLinus Torvalds 		if (!tb_init) {
14461da177e4SLinus Torvalds 			tb_init = 1;
14474cf5f7adSJeff Mahoney 			item_len = ih_item_len(tp_item_head(&path));
1448bd4c625cSLinus Torvalds 			init_tb_struct(th, &tb, th->t_super, &path,
1449bd4c625cSLinus Torvalds 				       -(IH_SIZE + item_len));
14501da177e4SLinus Torvalds 		}
14514cf5f7adSJeff Mahoney 		quota_cut_bytes = ih_item_len(tp_item_head(&path));
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds 		retval = fix_nodes(M_DELETE, &tb, NULL, NULL);
14541da177e4SLinus Torvalds 		if (retval == REPEAT_SEARCH) {
14551da177e4SLinus Torvalds 			PROC_INFO_INC(th->t_super, delete_solid_item_restarted);
14561da177e4SLinus Torvalds 			continue;
14571da177e4SLinus Torvalds 		}
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 		if (retval == CARRY_ON) {
14601da177e4SLinus Torvalds 			do_balance(&tb, NULL, NULL, M_DELETE);
1461098297b2SJeff Mahoney 			/*
1462098297b2SJeff Mahoney 			 * Should we count quota for item? (we don't
1463098297b2SJeff Mahoney 			 * count quotas for save-links)
1464098297b2SJeff Mahoney 			 */
1465098297b2SJeff Mahoney 			if (inode) {
1466d2d0395fSJeff Mahoney 				int depth;
14671da177e4SLinus Torvalds #ifdef REISERQUOTA_DEBUG
1468bd4c625cSLinus Torvalds 				reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
1469bd4c625cSLinus Torvalds 					       "reiserquota delete_solid_item(): freeing %u id=%u type=%c",
1470bd4c625cSLinus Torvalds 					       quota_cut_bytes, inode->i_uid,
1471bd4c625cSLinus Torvalds 					       key2type(key));
14721da177e4SLinus Torvalds #endif
1473d2d0395fSJeff Mahoney 				depth = reiserfs_write_unlock_nested(sb);
14745dd4056dSChristoph Hellwig 				dquot_free_space_nodirty(inode,
1475bd4c625cSLinus Torvalds 							 quota_cut_bytes);
1476d2d0395fSJeff Mahoney 				reiserfs_write_lock_nested(sb, depth);
14771da177e4SLinus Torvalds 			}
14781da177e4SLinus Torvalds 			break;
14791da177e4SLinus Torvalds 		}
1480098297b2SJeff Mahoney 
1481098297b2SJeff Mahoney 		/* IO_ERROR, NO_DISK_SPACE, etc */
148245b03d5eSJeff Mahoney 		reiserfs_warning(th->t_super, "vs-5360",
1483bd4c625cSLinus Torvalds 				 "could not delete %K due to fix_nodes failure",
1484bd4c625cSLinus Torvalds 				 &cpu_key);
14851da177e4SLinus Torvalds 		unfix_nodes(&tb);
14861da177e4SLinus Torvalds 		break;
14871da177e4SLinus Torvalds 	}
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 	reiserfs_check_path(&path);
14901da177e4SLinus Torvalds }
14911da177e4SLinus Torvalds 
reiserfs_delete_object(struct reiserfs_transaction_handle * th,struct inode * inode)1492bd4c625cSLinus Torvalds int reiserfs_delete_object(struct reiserfs_transaction_handle *th,
1493bd4c625cSLinus Torvalds 			   struct inode *inode)
14941da177e4SLinus Torvalds {
14951da177e4SLinus Torvalds 	int err;
14961da177e4SLinus Torvalds 	inode->i_size = 0;
14971da177e4SLinus Torvalds 	BUG_ON(!th->t_trans_id);
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 	/* for directory this deletes item containing "." and ".." */
1500bd4c625cSLinus Torvalds 	err =
1501bd4c625cSLinus Torvalds 	    reiserfs_do_truncate(th, inode, NULL, 0 /*no timestamp updates */ );
15021da177e4SLinus Torvalds 	if (err)
15031da177e4SLinus Torvalds 		return err;
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds #if defined( USE_INODE_GENERATION_COUNTER )
1506bd4c625cSLinus Torvalds 	if (!old_format_only(th->t_super)) {
15073e8962beSAl Viro 		__le32 *inode_generation;
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 		inode_generation =
15101da177e4SLinus Torvalds 		    &REISERFS_SB(th->t_super)->s_rs->s_inode_generation;
15119e902df6SMarcin Slusarz 		le32_add_cpu(inode_generation, 1);
15121da177e4SLinus Torvalds 	}
15131da177e4SLinus Torvalds /* USE_INODE_GENERATION_COUNTER */
15141da177e4SLinus Torvalds #endif
15151da177e4SLinus Torvalds 	reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	return err;
15181da177e4SLinus Torvalds }
15191da177e4SLinus Torvalds 
unmap_buffers(struct page * page,loff_t pos)1520bd4c625cSLinus Torvalds static void unmap_buffers(struct page *page, loff_t pos)
1521bd4c625cSLinus Torvalds {
15221da177e4SLinus Torvalds 	struct buffer_head *bh;
15231da177e4SLinus Torvalds 	struct buffer_head *head;
15241da177e4SLinus Torvalds 	struct buffer_head *next;
15251da177e4SLinus Torvalds 	unsigned long tail_index;
15261da177e4SLinus Torvalds 	unsigned long cur_index;
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds 	if (page) {
15291da177e4SLinus Torvalds 		if (page_has_buffers(page)) {
153009cbfeafSKirill A. Shutemov 			tail_index = pos & (PAGE_SIZE - 1);
15311da177e4SLinus Torvalds 			cur_index = 0;
15321da177e4SLinus Torvalds 			head = page_buffers(page);
15331da177e4SLinus Torvalds 			bh = head;
15341da177e4SLinus Torvalds 			do {
15351da177e4SLinus Torvalds 				next = bh->b_this_page;
15361da177e4SLinus Torvalds 
1537098297b2SJeff Mahoney 				/*
1538098297b2SJeff Mahoney 				 * we want to unmap the buffers that contain
1539098297b2SJeff Mahoney 				 * the tail, and all the buffers after it
1540098297b2SJeff Mahoney 				 * (since the tail must be at the end of the
1541098297b2SJeff Mahoney 				 * file).  We don't want to unmap file data
1542098297b2SJeff Mahoney 				 * before the tail, since it might be dirty
1543098297b2SJeff Mahoney 				 * and waiting to reach disk
15441da177e4SLinus Torvalds 				 */
15451da177e4SLinus Torvalds 				cur_index += bh->b_size;
15461da177e4SLinus Torvalds 				if (cur_index > tail_index) {
15471da177e4SLinus Torvalds 					reiserfs_unmap_buffer(bh);
15481da177e4SLinus Torvalds 				}
15491da177e4SLinus Torvalds 				bh = next;
15501da177e4SLinus Torvalds 			} while (bh != head);
15511da177e4SLinus Torvalds 		}
15521da177e4SLinus Torvalds 	}
15531da177e4SLinus Torvalds }
15541da177e4SLinus Torvalds 
maybe_indirect_to_direct(struct reiserfs_transaction_handle * th,struct inode * inode,struct page * page,struct treepath * path,const struct cpu_key * item_key,loff_t new_file_size,char * mode)15551da177e4SLinus Torvalds static int maybe_indirect_to_direct(struct reiserfs_transaction_handle *th,
1556995c762eSJeff Mahoney 				    struct inode *inode,
15571da177e4SLinus Torvalds 				    struct page *page,
1558d68caa95SJeff Mahoney 				    struct treepath *path,
1559d68caa95SJeff Mahoney 				    const struct cpu_key *item_key,
1560ee93961bSJeff Mahoney 				    loff_t new_file_size, char *mode)
1561bd4c625cSLinus Torvalds {
1562995c762eSJeff Mahoney 	struct super_block *sb = inode->i_sb;
1563ee93961bSJeff Mahoney 	int block_size = sb->s_blocksize;
15641da177e4SLinus Torvalds 	int cut_bytes;
15651da177e4SLinus Torvalds 	BUG_ON(!th->t_trans_id);
1566ee93961bSJeff Mahoney 	BUG_ON(new_file_size != inode->i_size);
15671da177e4SLinus Torvalds 
1568098297b2SJeff Mahoney 	/*
1569098297b2SJeff Mahoney 	 * the page being sent in could be NULL if there was an i/o error
1570098297b2SJeff Mahoney 	 * reading in the last block.  The user will hit problems trying to
1571098297b2SJeff Mahoney 	 * read the file, but for now we just skip the indirect2direct
15721da177e4SLinus Torvalds 	 */
1573995c762eSJeff Mahoney 	if (atomic_read(&inode->i_count) > 1 ||
1574995c762eSJeff Mahoney 	    !tail_has_to_be_packed(inode) ||
1575995c762eSJeff Mahoney 	    !page || (REISERFS_I(inode)->i_flags & i_nopack_mask)) {
15760222e657SJeff Mahoney 		/* leave tail in an unformatted node */
1577d68caa95SJeff Mahoney 		*mode = M_SKIP_BALANCING;
1578bd4c625cSLinus Torvalds 		cut_bytes =
1579ee93961bSJeff Mahoney 		    block_size - (new_file_size & (block_size - 1));
1580d68caa95SJeff Mahoney 		pathrelse(path);
15811da177e4SLinus Torvalds 		return cut_bytes;
15821da177e4SLinus Torvalds 	}
1583098297b2SJeff Mahoney 
1584d68caa95SJeff Mahoney 	/* Perform the conversion to a direct_item. */
1585d68caa95SJeff Mahoney 	return indirect2direct(th, inode, page, path, item_key,
1586ee93961bSJeff Mahoney 			       new_file_size, mode);
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds 
1589098297b2SJeff Mahoney /*
1590098297b2SJeff Mahoney  * we did indirect_to_direct conversion. And we have inserted direct
1591098297b2SJeff Mahoney  * item successesfully, but there were no disk space to cut unfm
1592098297b2SJeff Mahoney  * pointer being converted. Therefore we have to delete inserted
1593098297b2SJeff Mahoney  * direct item(s)
1594098297b2SJeff Mahoney  */
indirect_to_direct_roll_back(struct reiserfs_transaction_handle * th,struct inode * inode,struct treepath * path)1595bd4c625cSLinus Torvalds static void indirect_to_direct_roll_back(struct reiserfs_transaction_handle *th,
1596fec6d055SJosef "Jeff" Sipek 					 struct inode *inode, struct treepath *path)
15971da177e4SLinus Torvalds {
15981da177e4SLinus Torvalds 	struct cpu_key tail_key;
15991da177e4SLinus Torvalds 	int tail_len;
16001da177e4SLinus Torvalds 	int removed;
16011da177e4SLinus Torvalds 	BUG_ON(!th->t_trans_id);
16021da177e4SLinus Torvalds 
1603098297b2SJeff Mahoney 	make_cpu_key(&tail_key, inode, inode->i_size + 1, TYPE_DIRECT, 4);
16041da177e4SLinus Torvalds 	tail_key.key_length = 4;
16051da177e4SLinus Torvalds 
1606bd4c625cSLinus Torvalds 	tail_len =
1607bd4c625cSLinus Torvalds 	    (cpu_key_k_offset(&tail_key) & (inode->i_sb->s_blocksize - 1)) - 1;
16081da177e4SLinus Torvalds 	while (tail_len) {
16091da177e4SLinus Torvalds 		/* look for the last byte of the tail */
1610bd4c625cSLinus Torvalds 		if (search_for_position_by_key(inode->i_sb, &tail_key, path) ==
1611bd4c625cSLinus Torvalds 		    POSITION_NOT_FOUND)
1612c3a9c210SJeff Mahoney 			reiserfs_panic(inode->i_sb, "vs-5615",
1613c3a9c210SJeff Mahoney 				       "found invalid item");
1614bd4c625cSLinus Torvalds 		RFALSE(path->pos_in_item !=
16154cf5f7adSJeff Mahoney 		       ih_item_len(tp_item_head(path)) - 1,
16161da177e4SLinus Torvalds 		       "vs-5616: appended bytes found");
16171da177e4SLinus Torvalds 		PATH_LAST_POSITION(path)--;
16181da177e4SLinus Torvalds 
1619bd4c625cSLinus Torvalds 		removed =
1620bd4c625cSLinus Torvalds 		    reiserfs_delete_item(th, path, &tail_key, inode,
1621bd4c625cSLinus Torvalds 					 NULL /*unbh not needed */ );
1622bd4c625cSLinus Torvalds 		RFALSE(removed <= 0
1623bd4c625cSLinus Torvalds 		       || removed > tail_len,
16241da177e4SLinus Torvalds 		       "vs-5617: there was tail %d bytes, removed item length %d bytes",
16251da177e4SLinus Torvalds 		       tail_len, removed);
16261da177e4SLinus Torvalds 		tail_len -= removed;
1627bd4c625cSLinus Torvalds 		set_cpu_key_k_offset(&tail_key,
1628bd4c625cSLinus Torvalds 				     cpu_key_k_offset(&tail_key) - removed);
16291da177e4SLinus Torvalds 	}
163045b03d5eSJeff Mahoney 	reiserfs_warning(inode->i_sb, "reiserfs-5091", "indirect_to_direct "
163145b03d5eSJeff Mahoney 			 "conversion has been rolled back due to "
163245b03d5eSJeff Mahoney 			 "lack of disk space");
16331da177e4SLinus Torvalds 	mark_inode_dirty(inode);
16341da177e4SLinus Torvalds }
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds /* (Truncate or cut entry) or delete object item. Returns < 0 on failure */
reiserfs_cut_from_item(struct reiserfs_transaction_handle * th,struct treepath * path,struct cpu_key * item_key,struct inode * inode,struct page * page,loff_t new_file_size)16371da177e4SLinus Torvalds int reiserfs_cut_from_item(struct reiserfs_transaction_handle *th,
1638d68caa95SJeff Mahoney 			   struct treepath *path,
1639d68caa95SJeff Mahoney 			   struct cpu_key *item_key,
1640995c762eSJeff Mahoney 			   struct inode *inode,
1641ee93961bSJeff Mahoney 			   struct page *page, loff_t new_file_size)
16421da177e4SLinus Torvalds {
1643995c762eSJeff Mahoney 	struct super_block *sb = inode->i_sb;
1644098297b2SJeff Mahoney 	/*
1645098297b2SJeff Mahoney 	 * Every function which is going to call do_balance must first
1646098297b2SJeff Mahoney 	 * create a tree_balance structure.  Then it must fill up this
1647098297b2SJeff Mahoney 	 * structure by using the init_tb_struct and fix_nodes functions.
1648098297b2SJeff Mahoney 	 * After that we can make tree balancing.
1649098297b2SJeff Mahoney 	 */
16501da177e4SLinus Torvalds 	struct tree_balance s_cut_balance;
16511da177e4SLinus Torvalds 	struct item_head *p_le_ih;
1652098297b2SJeff Mahoney 	int cut_size = 0;	/* Amount to be cut. */
1653098297b2SJeff Mahoney 	int ret_value = CARRY_ON;
1654098297b2SJeff Mahoney 	int removed = 0;	/* Number of the removed unformatted nodes. */
1655098297b2SJeff Mahoney 	int is_inode_locked = 0;
1656ee93961bSJeff Mahoney 	char mode;		/* Mode of the balance. */
16571da177e4SLinus Torvalds 	int retval2 = -1;
16581da177e4SLinus Torvalds 	int quota_cut_bytes;
16591da177e4SLinus Torvalds 	loff_t tail_pos = 0;
1660d2d0395fSJeff Mahoney 	int depth;
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds 	BUG_ON(!th->t_trans_id);
16631da177e4SLinus Torvalds 
1664d68caa95SJeff Mahoney 	init_tb_struct(th, &s_cut_balance, inode->i_sb, path,
1665ee93961bSJeff Mahoney 		       cut_size);
16661da177e4SLinus Torvalds 
1667098297b2SJeff Mahoney 	/*
1668098297b2SJeff Mahoney 	 * Repeat this loop until we either cut the item without needing
1669098297b2SJeff Mahoney 	 * to balance, or we fix_nodes without schedule occurring
1670098297b2SJeff Mahoney 	 */
16711da177e4SLinus Torvalds 	while (1) {
1672098297b2SJeff Mahoney 		/*
1673098297b2SJeff Mahoney 		 * Determine the balance mode, position of the first byte to
1674098297b2SJeff Mahoney 		 * be cut, and size to be cut.  In case of the indirect item
1675098297b2SJeff Mahoney 		 * free unformatted nodes which are pointed to by the cut
1676098297b2SJeff Mahoney 		 * pointers.
1677098297b2SJeff Mahoney 		 */
16781da177e4SLinus Torvalds 
1679ee93961bSJeff Mahoney 		mode =
1680d68caa95SJeff Mahoney 		    prepare_for_delete_or_cut(th, inode, path,
1681ee93961bSJeff Mahoney 					      item_key, &removed,
1682ee93961bSJeff Mahoney 					      &cut_size, new_file_size);
1683ee93961bSJeff Mahoney 		if (mode == M_CONVERT) {
1684098297b2SJeff Mahoney 			/*
1685098297b2SJeff Mahoney 			 * convert last unformatted node to direct item or
1686098297b2SJeff Mahoney 			 * leave tail in the unformatted node
1687098297b2SJeff Mahoney 			 */
1688ee93961bSJeff Mahoney 			RFALSE(ret_value != CARRY_ON,
1689bd4c625cSLinus Torvalds 			       "PAP-5570: can not convert twice");
16901da177e4SLinus Torvalds 
1691ee93961bSJeff Mahoney 			ret_value =
1692995c762eSJeff Mahoney 			    maybe_indirect_to_direct(th, inode, page,
1693d68caa95SJeff Mahoney 						     path, item_key,
1694ee93961bSJeff Mahoney 						     new_file_size, &mode);
1695ee93961bSJeff Mahoney 			if (mode == M_SKIP_BALANCING)
16961da177e4SLinus Torvalds 				/* tail has been left in the unformatted node */
1697ee93961bSJeff Mahoney 				return ret_value;
16981da177e4SLinus Torvalds 
1699ee93961bSJeff Mahoney 			is_inode_locked = 1;
17001da177e4SLinus Torvalds 
1701098297b2SJeff Mahoney 			/*
1702098297b2SJeff Mahoney 			 * removing of last unformatted node will
1703098297b2SJeff Mahoney 			 * change value we have to return to truncate.
1704098297b2SJeff Mahoney 			 * Save it
1705098297b2SJeff Mahoney 			 */
1706ee93961bSJeff Mahoney 			retval2 = ret_value;
17071da177e4SLinus Torvalds 
1708098297b2SJeff Mahoney 			/*
1709098297b2SJeff Mahoney 			 * So, we have performed the first part of the
1710098297b2SJeff Mahoney 			 * conversion:
1711098297b2SJeff Mahoney 			 * inserting the new direct item.  Now we are
1712098297b2SJeff Mahoney 			 * removing the last unformatted node pointer.
1713098297b2SJeff Mahoney 			 * Set key to search for it.
1714098297b2SJeff Mahoney 			 */
1715d68caa95SJeff Mahoney 			set_cpu_key_k_type(item_key, TYPE_INDIRECT);
1716d68caa95SJeff Mahoney 			item_key->key_length = 4;
1717ee93961bSJeff Mahoney 			new_file_size -=
1718ee93961bSJeff Mahoney 			    (new_file_size & (sb->s_blocksize - 1));
1719ee93961bSJeff Mahoney 			tail_pos = new_file_size;
1720ee93961bSJeff Mahoney 			set_cpu_key_k_offset(item_key, new_file_size + 1);
1721bd4c625cSLinus Torvalds 			if (search_for_position_by_key
1722d68caa95SJeff Mahoney 			    (sb, item_key,
1723d68caa95SJeff Mahoney 			     path) == POSITION_NOT_FOUND) {
1724d68caa95SJeff Mahoney 				print_block(PATH_PLAST_BUFFER(path), 3,
1725d68caa95SJeff Mahoney 					    PATH_LAST_POSITION(path) - 1,
1726d68caa95SJeff Mahoney 					    PATH_LAST_POSITION(path) + 1);
1727a9dd3643SJeff Mahoney 				reiserfs_panic(sb, "PAP-5580", "item to "
1728c3a9c210SJeff Mahoney 					       "convert does not exist (%K)",
1729d68caa95SJeff Mahoney 					       item_key);
17301da177e4SLinus Torvalds 			}
17311da177e4SLinus Torvalds 			continue;
17321da177e4SLinus Torvalds 		}
1733ee93961bSJeff Mahoney 		if (cut_size == 0) {
1734d68caa95SJeff Mahoney 			pathrelse(path);
17351da177e4SLinus Torvalds 			return 0;
17361da177e4SLinus Torvalds 		}
17371da177e4SLinus Torvalds 
1738ee93961bSJeff Mahoney 		s_cut_balance.insert_size[0] = cut_size;
17391da177e4SLinus Torvalds 
1740ee93961bSJeff Mahoney 		ret_value = fix_nodes(mode, &s_cut_balance, NULL, NULL);
1741ee93961bSJeff Mahoney 		if (ret_value != REPEAT_SEARCH)
17421da177e4SLinus Torvalds 			break;
17431da177e4SLinus Torvalds 
1744a9dd3643SJeff Mahoney 		PROC_INFO_INC(sb, cut_from_item_restarted);
17451da177e4SLinus Torvalds 
1746ee93961bSJeff Mahoney 		ret_value =
1747d68caa95SJeff Mahoney 		    search_for_position_by_key(sb, item_key, path);
1748ee93961bSJeff Mahoney 		if (ret_value == POSITION_FOUND)
17491da177e4SLinus Torvalds 			continue;
17501da177e4SLinus Torvalds 
1751a9dd3643SJeff Mahoney 		reiserfs_warning(sb, "PAP-5610", "item %K not found",
1752d68caa95SJeff Mahoney 				 item_key);
17531da177e4SLinus Torvalds 		unfix_nodes(&s_cut_balance);
1754ee93961bSJeff Mahoney 		return (ret_value == IO_ERROR) ? -EIO : -ENOENT;
17551da177e4SLinus Torvalds 	}			/* while */
17561da177e4SLinus Torvalds 
1757098297b2SJeff Mahoney 	/* check fix_nodes results (IO_ERROR or NO_DISK_SPACE) */
1758ee93961bSJeff Mahoney 	if (ret_value != CARRY_ON) {
1759ee93961bSJeff Mahoney 		if (is_inode_locked) {
1760098297b2SJeff Mahoney 			/*
1761098297b2SJeff Mahoney 			 * FIXME: this seems to be not needed: we are always
1762098297b2SJeff Mahoney 			 * able to cut item
1763098297b2SJeff Mahoney 			 */
1764d68caa95SJeff Mahoney 			indirect_to_direct_roll_back(th, inode, path);
17651da177e4SLinus Torvalds 		}
1766ee93961bSJeff Mahoney 		if (ret_value == NO_DISK_SPACE)
1767a9dd3643SJeff Mahoney 			reiserfs_warning(sb, "reiserfs-5092",
176845b03d5eSJeff Mahoney 					 "NO_DISK_SPACE");
17691da177e4SLinus Torvalds 		unfix_nodes(&s_cut_balance);
17701da177e4SLinus Torvalds 		return -EIO;
17711da177e4SLinus Torvalds 	}
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds 	/* go ahead and perform balancing */
17741da177e4SLinus Torvalds 
1775ee93961bSJeff Mahoney 	RFALSE(mode == M_PASTE || mode == M_INSERT, "invalid mode");
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	/* Calculate number of bytes that need to be cut from the item. */
1778bd4c625cSLinus Torvalds 	quota_cut_bytes =
1779ee93961bSJeff Mahoney 	    (mode ==
17804cf5f7adSJeff Mahoney 	     M_DELETE) ? ih_item_len(tp_item_head(path)) : -s_cut_balance.
1781bd4c625cSLinus Torvalds 	    insert_size[0];
17821da177e4SLinus Torvalds 	if (retval2 == -1)
1783ee93961bSJeff Mahoney 		ret_value = calc_deleted_bytes_number(&s_cut_balance, mode);
17841da177e4SLinus Torvalds 	else
1785ee93961bSJeff Mahoney 		ret_value = retval2;
17861da177e4SLinus Torvalds 
1787098297b2SJeff Mahoney 	/*
1788098297b2SJeff Mahoney 	 * For direct items, we only change the quota when deleting the last
1789098297b2SJeff Mahoney 	 * item.
17901da177e4SLinus Torvalds 	 */
17914cf5f7adSJeff Mahoney 	p_le_ih = tp_item_head(s_cut_balance.tb_path);
1792995c762eSJeff Mahoney 	if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(p_le_ih)) {
1793ee93961bSJeff Mahoney 		if (mode == M_DELETE &&
1794a9dd3643SJeff Mahoney 		    (le_ih_k_offset(p_le_ih) & (sb->s_blocksize - 1)) ==
1795bd4c625cSLinus Torvalds 		    1) {
1796098297b2SJeff Mahoney 			/* FIXME: this is to keep 3.5 happy */
1797995c762eSJeff Mahoney 			REISERFS_I(inode)->i_first_direct_byte = U32_MAX;
1798a9dd3643SJeff Mahoney 			quota_cut_bytes = sb->s_blocksize + UNFM_P_SIZE;
17991da177e4SLinus Torvalds 		} else {
18001da177e4SLinus Torvalds 			quota_cut_bytes = 0;
18011da177e4SLinus Torvalds 		}
18021da177e4SLinus Torvalds 	}
18031da177e4SLinus Torvalds #ifdef CONFIG_REISERFS_CHECK
1804ee93961bSJeff Mahoney 	if (is_inode_locked) {
1805bd4c625cSLinus Torvalds 		struct item_head *le_ih =
18064cf5f7adSJeff Mahoney 		    tp_item_head(s_cut_balance.tb_path);
1807098297b2SJeff Mahoney 		/*
1808098297b2SJeff Mahoney 		 * we are going to complete indirect2direct conversion. Make
1809098297b2SJeff Mahoney 		 * sure, that we exactly remove last unformatted node pointer
1810098297b2SJeff Mahoney 		 * of the item
1811098297b2SJeff Mahoney 		 */
18121da177e4SLinus Torvalds 		if (!is_indirect_le_ih(le_ih))
1813a9dd3643SJeff Mahoney 			reiserfs_panic(sb, "vs-5652",
18141da177e4SLinus Torvalds 				       "item must be indirect %h", le_ih);
18151da177e4SLinus Torvalds 
1816ee93961bSJeff Mahoney 		if (mode == M_DELETE && ih_item_len(le_ih) != UNFM_P_SIZE)
1817a9dd3643SJeff Mahoney 			reiserfs_panic(sb, "vs-5653", "completing "
1818c3a9c210SJeff Mahoney 				       "indirect2direct conversion indirect "
1819c3a9c210SJeff Mahoney 				       "item %h being deleted must be of "
1820c3a9c210SJeff Mahoney 				       "4 byte long", le_ih);
18211da177e4SLinus Torvalds 
1822ee93961bSJeff Mahoney 		if (mode == M_CUT
1823bd4c625cSLinus Torvalds 		    && s_cut_balance.insert_size[0] != -UNFM_P_SIZE) {
1824a9dd3643SJeff Mahoney 			reiserfs_panic(sb, "vs-5654", "can not complete "
1825c3a9c210SJeff Mahoney 				       "indirect2direct conversion of %h "
1826c3a9c210SJeff Mahoney 				       "(CUT, insert_size==%d)",
18271da177e4SLinus Torvalds 				       le_ih, s_cut_balance.insert_size[0]);
18281da177e4SLinus Torvalds 		}
1829098297b2SJeff Mahoney 		/*
1830098297b2SJeff Mahoney 		 * it would be useful to make sure, that right neighboring
1831098297b2SJeff Mahoney 		 * item is direct item of this file
1832098297b2SJeff Mahoney 		 */
18331da177e4SLinus Torvalds 	}
18341da177e4SLinus Torvalds #endif
18351da177e4SLinus Torvalds 
1836ee93961bSJeff Mahoney 	do_balance(&s_cut_balance, NULL, NULL, mode);
1837ee93961bSJeff Mahoney 	if (is_inode_locked) {
1838098297b2SJeff Mahoney 		/*
1839098297b2SJeff Mahoney 		 * we've done an indirect->direct conversion.  when the
1840098297b2SJeff Mahoney 		 * data block was freed, it was removed from the list of
1841098297b2SJeff Mahoney 		 * blocks that must be flushed before the transaction
1842098297b2SJeff Mahoney 		 * commits, make sure to unmap and invalidate it
18431da177e4SLinus Torvalds 		 */
18441da177e4SLinus Torvalds 		unmap_buffers(page, tail_pos);
1845995c762eSJeff Mahoney 		REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
18461da177e4SLinus Torvalds 	}
18471da177e4SLinus Torvalds #ifdef REISERQUOTA_DEBUG
1848995c762eSJeff Mahoney 	reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
1849bd4c625cSLinus Torvalds 		       "reiserquota cut_from_item(): freeing %u id=%u type=%c",
1850995c762eSJeff Mahoney 		       quota_cut_bytes, inode->i_uid, '?');
18511da177e4SLinus Torvalds #endif
1852d2d0395fSJeff Mahoney 	depth = reiserfs_write_unlock_nested(sb);
18535dd4056dSChristoph Hellwig 	dquot_free_space_nodirty(inode, quota_cut_bytes);
1854d2d0395fSJeff Mahoney 	reiserfs_write_lock_nested(sb, depth);
1855ee93961bSJeff Mahoney 	return ret_value;
18561da177e4SLinus Torvalds }
18571da177e4SLinus Torvalds 
truncate_directory(struct reiserfs_transaction_handle * th,struct inode * inode)1858bd4c625cSLinus Torvalds static void truncate_directory(struct reiserfs_transaction_handle *th,
1859bd4c625cSLinus Torvalds 			       struct inode *inode)
18601da177e4SLinus Torvalds {
18611da177e4SLinus Torvalds 	BUG_ON(!th->t_trans_id);
18621da177e4SLinus Torvalds 	if (inode->i_nlink)
18630030b645SJeff Mahoney 		reiserfs_error(inode->i_sb, "vs-5655", "link count != 0");
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds 	set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), DOT_OFFSET);
18661da177e4SLinus Torvalds 	set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_DIRENTRY);
18671da177e4SLinus Torvalds 	reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
18681da177e4SLinus Torvalds 	reiserfs_update_sd(th, inode);
18691da177e4SLinus Torvalds 	set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), SD_OFFSET);
18701da177e4SLinus Torvalds 	set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_STAT_DATA);
18711da177e4SLinus Torvalds }
18721da177e4SLinus Torvalds 
1873098297b2SJeff Mahoney /*
1874098297b2SJeff Mahoney  * Truncate file to the new size. Note, this must be called with a
1875098297b2SJeff Mahoney  * transaction already started
1876098297b2SJeff Mahoney  */
reiserfs_do_truncate(struct reiserfs_transaction_handle * th,struct inode * inode,struct page * page,int update_timestamps)1877995c762eSJeff Mahoney int reiserfs_do_truncate(struct reiserfs_transaction_handle *th,
1878995c762eSJeff Mahoney 			 struct inode *inode,	/* ->i_size contains new size */
18791da177e4SLinus Torvalds 			 struct page *page,	/* up to date for last block */
1880098297b2SJeff Mahoney 			 /*
1881098297b2SJeff Mahoney 			  * when it is called by file_release to convert
1882098297b2SJeff Mahoney 			  * the tail - no timestamps should be updated
1883098297b2SJeff Mahoney 			  */
1884098297b2SJeff Mahoney 			 int update_timestamps
1885bd4c625cSLinus Torvalds     )
1886bd4c625cSLinus Torvalds {
18871da177e4SLinus Torvalds 	INITIALIZE_PATH(s_search_path);	/* Path to the current object item. */
18881da177e4SLinus Torvalds 	struct item_head *p_le_ih;	/* Pointer to an item header. */
1889098297b2SJeff Mahoney 
1890098297b2SJeff Mahoney 	/* Key to search for a previous file item. */
1891098297b2SJeff Mahoney 	struct cpu_key s_item_key;
1892ee93961bSJeff Mahoney 	loff_t file_size,	/* Old file size. */
1893ee93961bSJeff Mahoney 	 new_file_size;	/* New file size. */
1894ee93961bSJeff Mahoney 	int deleted;		/* Number of deleted or truncated bytes. */
18951da177e4SLinus Torvalds 	int retval;
18961da177e4SLinus Torvalds 	int err = 0;
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds 	BUG_ON(!th->t_trans_id);
1899bd4c625cSLinus Torvalds 	if (!
1900995c762eSJeff Mahoney 	    (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1901995c762eSJeff Mahoney 	     || S_ISLNK(inode->i_mode)))
19021da177e4SLinus Torvalds 		return 0;
19031da177e4SLinus Torvalds 
1904098297b2SJeff Mahoney 	/* deletion of directory - no need to update timestamps */
1905995c762eSJeff Mahoney 	if (S_ISDIR(inode->i_mode)) {
1906995c762eSJeff Mahoney 		truncate_directory(th, inode);
19071da177e4SLinus Torvalds 		return 0;
19081da177e4SLinus Torvalds 	}
19091da177e4SLinus Torvalds 
19101da177e4SLinus Torvalds 	/* Get new file size. */
1911ee93961bSJeff Mahoney 	new_file_size = inode->i_size;
19121da177e4SLinus Torvalds 
1913098297b2SJeff Mahoney 	/* FIXME: note, that key type is unimportant here */
1914995c762eSJeff Mahoney 	make_cpu_key(&s_item_key, inode, max_reiserfs_offset(inode),
1915bd4c625cSLinus Torvalds 		     TYPE_DIRECT, 3);
19161da177e4SLinus Torvalds 
1917bd4c625cSLinus Torvalds 	retval =
1918995c762eSJeff Mahoney 	    search_for_position_by_key(inode->i_sb, &s_item_key,
1919bd4c625cSLinus Torvalds 				       &s_search_path);
19201da177e4SLinus Torvalds 	if (retval == IO_ERROR) {
1921995c762eSJeff Mahoney 		reiserfs_error(inode->i_sb, "vs-5657",
1922bd4c625cSLinus Torvalds 			       "i/o failure occurred trying to truncate %K",
1923bd4c625cSLinus Torvalds 			       &s_item_key);
19241da177e4SLinus Torvalds 		err = -EIO;
19251da177e4SLinus Torvalds 		goto out;
19261da177e4SLinus Torvalds 	}
19271da177e4SLinus Torvalds 	if (retval == POSITION_FOUND || retval == FILE_NOT_FOUND) {
1928995c762eSJeff Mahoney 		reiserfs_error(inode->i_sb, "PAP-5660",
1929bd4c625cSLinus Torvalds 			       "wrong result %d of search for %K", retval,
1930bd4c625cSLinus Torvalds 			       &s_item_key);
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 		err = -EIO;
19331da177e4SLinus Torvalds 		goto out;
19341da177e4SLinus Torvalds 	}
19351da177e4SLinus Torvalds 
19361da177e4SLinus Torvalds 	s_search_path.pos_in_item--;
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 	/* Get real file size (total length of all file items) */
19394cf5f7adSJeff Mahoney 	p_le_ih = tp_item_head(&s_search_path);
19401da177e4SLinus Torvalds 	if (is_statdata_le_ih(p_le_ih))
1941ee93961bSJeff Mahoney 		file_size = 0;
19421da177e4SLinus Torvalds 	else {
19431da177e4SLinus Torvalds 		loff_t offset = le_ih_k_offset(p_le_ih);
1944bd4c625cSLinus Torvalds 		int bytes =
1945995c762eSJeff Mahoney 		    op_bytes_number(p_le_ih, inode->i_sb->s_blocksize);
19461da177e4SLinus Torvalds 
1947098297b2SJeff Mahoney 		/*
1948098297b2SJeff Mahoney 		 * this may mismatch with real file size: if last direct item
1949098297b2SJeff Mahoney 		 * had no padding zeros and last unformatted node had no free
1950098297b2SJeff Mahoney 		 * space, this file would have this file size
1951098297b2SJeff Mahoney 		 */
1952ee93961bSJeff Mahoney 		file_size = offset + bytes - 1;
19531da177e4SLinus Torvalds 	}
19541da177e4SLinus Torvalds 	/*
19551da177e4SLinus Torvalds 	 * are we doing a full truncate or delete, if so
19561da177e4SLinus Torvalds 	 * kick in the reada code
19571da177e4SLinus Torvalds 	 */
1958ee93961bSJeff Mahoney 	if (new_file_size == 0)
19591da177e4SLinus Torvalds 		s_search_path.reada = PATH_READA | PATH_READA_BACK;
19601da177e4SLinus Torvalds 
1961ee93961bSJeff Mahoney 	if (file_size == 0 || file_size < new_file_size) {
19621da177e4SLinus Torvalds 		goto update_and_out;
19631da177e4SLinus Torvalds 	}
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds 	/* Update key to search for the last file item. */
1966ee93961bSJeff Mahoney 	set_cpu_key_k_offset(&s_item_key, file_size);
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds 	do {
19691da177e4SLinus Torvalds 		/* Cut or delete file item. */
1970ee93961bSJeff Mahoney 		deleted =
1971bd4c625cSLinus Torvalds 		    reiserfs_cut_from_item(th, &s_search_path, &s_item_key,
1972ee93961bSJeff Mahoney 					   inode, page, new_file_size);
1973ee93961bSJeff Mahoney 		if (deleted < 0) {
1974995c762eSJeff Mahoney 			reiserfs_warning(inode->i_sb, "vs-5665",
197545b03d5eSJeff Mahoney 					 "reiserfs_cut_from_item failed");
19761da177e4SLinus Torvalds 			reiserfs_check_path(&s_search_path);
19771da177e4SLinus Torvalds 			return 0;
19781da177e4SLinus Torvalds 		}
19791da177e4SLinus Torvalds 
1980ee93961bSJeff Mahoney 		RFALSE(deleted > file_size,
19811da177e4SLinus Torvalds 		       "PAP-5670: reiserfs_cut_from_item: too many bytes deleted: deleted %d, file_size %lu, item_key %K",
1982ee93961bSJeff Mahoney 		       deleted, file_size, &s_item_key);
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 		/* Change key to search the last file item. */
1985ee93961bSJeff Mahoney 		file_size -= deleted;
19861da177e4SLinus Torvalds 
1987ee93961bSJeff Mahoney 		set_cpu_key_k_offset(&s_item_key, file_size);
19881da177e4SLinus Torvalds 
1989098297b2SJeff Mahoney 		/*
1990098297b2SJeff Mahoney 		 * While there are bytes to truncate and previous
1991098297b2SJeff Mahoney 		 * file item is presented in the tree.
1992098297b2SJeff Mahoney 		 */
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 		/*
1995098297b2SJeff Mahoney 		 * This loop could take a really long time, and could log
1996098297b2SJeff Mahoney 		 * many more blocks than a transaction can hold.  So, we do
1997098297b2SJeff Mahoney 		 * a polite journal end here, and if the transaction needs
1998098297b2SJeff Mahoney 		 * ending, we make sure the file is consistent before ending
1999098297b2SJeff Mahoney 		 * the current trans and starting a new one
20001da177e4SLinus Torvalds 		 */
200123f9e0f8SAlexander Zarochentzev 		if (journal_transaction_should_end(th, 0) ||
200223f9e0f8SAlexander Zarochentzev 		    reiserfs_transaction_free_space(th) <= JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD) {
20033cd6dbe6SJeff Mahoney 			pathrelse(&s_search_path);
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds 			if (update_timestamps) {
20065e8b820bSJeff Layton 				inode_set_mtime_to_ts(inode,
20075e8b820bSJeff Layton 						      current_time(inode));
2008ae834901SJeff Layton 				inode_set_ctime_current(inode);
20091da177e4SLinus Torvalds 			}
2010995c762eSJeff Mahoney 			reiserfs_update_sd(th, inode);
20111da177e4SLinus Torvalds 
201258d85426SJeff Mahoney 			err = journal_end(th);
20131da177e4SLinus Torvalds 			if (err)
20141da177e4SLinus Torvalds 				goto out;
2015995c762eSJeff Mahoney 			err = journal_begin(th, inode->i_sb,
201623f9e0f8SAlexander Zarochentzev 					    JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD + JOURNAL_PER_BALANCE_CNT * 4) ;
20171da177e4SLinus Torvalds 			if (err)
20181da177e4SLinus Torvalds 				goto out;
2019995c762eSJeff Mahoney 			reiserfs_update_inode_transaction(inode);
20201da177e4SLinus Torvalds 		}
2021ee93961bSJeff Mahoney 	} while (file_size > ROUND_UP(new_file_size) &&
2022995c762eSJeff Mahoney 		 search_for_position_by_key(inode->i_sb, &s_item_key,
2023bd4c625cSLinus Torvalds 					    &s_search_path) == POSITION_FOUND);
20241da177e4SLinus Torvalds 
2025ee93961bSJeff Mahoney 	RFALSE(file_size > ROUND_UP(new_file_size),
202653872ed0SFabian Frederick 	       "PAP-5680: truncate did not finish: new_file_size %lld, current %lld, oid %d",
2027ee93961bSJeff Mahoney 	       new_file_size, file_size, s_item_key.on_disk_key.k_objectid);
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds update_and_out:
20301da177e4SLinus Torvalds 	if (update_timestamps) {
2031098297b2SJeff Mahoney 		/* this is truncate, not file closing */
20325e8b820bSJeff Layton 		inode_set_mtime_to_ts(inode, current_time(inode));
2033ae834901SJeff Layton 		inode_set_ctime_current(inode);
20341da177e4SLinus Torvalds 	}
2035995c762eSJeff Mahoney 	reiserfs_update_sd(th, inode);
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds out:
20381da177e4SLinus Torvalds 	pathrelse(&s_search_path);
20391da177e4SLinus Torvalds 	return err;
20401da177e4SLinus Torvalds }
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds #ifdef CONFIG_REISERFS_CHECK
2043098297b2SJeff Mahoney /* this makes sure, that we __append__, not overwrite or add holes */
check_research_for_paste(struct treepath * path,const struct cpu_key * key)2044fec6d055SJosef "Jeff" Sipek static void check_research_for_paste(struct treepath *path,
2045d68caa95SJeff Mahoney 				     const struct cpu_key *key)
20461da177e4SLinus Torvalds {
20474cf5f7adSJeff Mahoney 	struct item_head *found_ih = tp_item_head(path);
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds 	if (is_direct_le_ih(found_ih)) {
2050bd4c625cSLinus Torvalds 		if (le_ih_k_offset(found_ih) +
2051bd4c625cSLinus Torvalds 		    op_bytes_number(found_ih,
2052bd4c625cSLinus Torvalds 				    get_last_bh(path)->b_size) !=
2053d68caa95SJeff Mahoney 		    cpu_key_k_offset(key)
2054bd4c625cSLinus Torvalds 		    || op_bytes_number(found_ih,
2055bd4c625cSLinus Torvalds 				       get_last_bh(path)->b_size) !=
2056bd4c625cSLinus Torvalds 		    pos_in_item(path))
2057c3a9c210SJeff Mahoney 			reiserfs_panic(NULL, "PAP-5720", "found direct item "
2058c3a9c210SJeff Mahoney 				       "%h or position (%d) does not match "
2059c3a9c210SJeff Mahoney 				       "to key %K", found_ih,
2060d68caa95SJeff Mahoney 				       pos_in_item(path), key);
20611da177e4SLinus Torvalds 	}
20621da177e4SLinus Torvalds 	if (is_indirect_le_ih(found_ih)) {
2063bd4c625cSLinus Torvalds 		if (le_ih_k_offset(found_ih) +
2064bd4c625cSLinus Torvalds 		    op_bytes_number(found_ih,
2065bd4c625cSLinus Torvalds 				    get_last_bh(path)->b_size) !=
2066d68caa95SJeff Mahoney 		    cpu_key_k_offset(key)
2067bd4c625cSLinus Torvalds 		    || I_UNFM_NUM(found_ih) != pos_in_item(path)
2068bd4c625cSLinus Torvalds 		    || get_ih_free_space(found_ih) != 0)
2069c3a9c210SJeff Mahoney 			reiserfs_panic(NULL, "PAP-5730", "found indirect "
2070c3a9c210SJeff Mahoney 				       "item (%h) or position (%d) does not "
2071c3a9c210SJeff Mahoney 				       "match to key (%K)",
2072d68caa95SJeff Mahoney 				       found_ih, pos_in_item(path), key);
20731da177e4SLinus Torvalds 	}
20741da177e4SLinus Torvalds }
20751da177e4SLinus Torvalds #endif				/* config reiserfs check */
20761da177e4SLinus Torvalds 
2077098297b2SJeff Mahoney /*
2078098297b2SJeff Mahoney  * Paste bytes to the existing item.
2079098297b2SJeff Mahoney  * Returns bytes number pasted into the item.
2080098297b2SJeff Mahoney  */
reiserfs_paste_into_item(struct reiserfs_transaction_handle * th,struct treepath * search_path,const struct cpu_key * key,struct inode * inode,const char * body,int pasted_size)2081098297b2SJeff Mahoney int reiserfs_paste_into_item(struct reiserfs_transaction_handle *th,
2082098297b2SJeff Mahoney 			     /* Path to the pasted item. */
2083098297b2SJeff Mahoney 			     struct treepath *search_path,
2084098297b2SJeff Mahoney 			     /* Key to search for the needed item. */
2085098297b2SJeff Mahoney 			     const struct cpu_key *key,
2086098297b2SJeff Mahoney 			     /* Inode item belongs to */
2087098297b2SJeff Mahoney 			     struct inode *inode,
2088098297b2SJeff Mahoney 			     /* Pointer to the bytes to paste. */
2089098297b2SJeff Mahoney 			     const char *body,
2090098297b2SJeff Mahoney 			     /* Size of pasted bytes. */
2091ee93961bSJeff Mahoney 			     int pasted_size)
2092098297b2SJeff Mahoney {
2093d2d0395fSJeff Mahoney 	struct super_block *sb = inode->i_sb;
20941da177e4SLinus Torvalds 	struct tree_balance s_paste_balance;
20951da177e4SLinus Torvalds 	int retval;
20961da177e4SLinus Torvalds 	int fs_gen;
2097d2d0395fSJeff Mahoney 	int depth;
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds 	BUG_ON(!th->t_trans_id);
21001da177e4SLinus Torvalds 
21011da177e4SLinus Torvalds 	fs_gen = get_generation(inode->i_sb);
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds #ifdef REISERQUOTA_DEBUG
2104bd4c625cSLinus Torvalds 	reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
2105bd4c625cSLinus Torvalds 		       "reiserquota paste_into_item(): allocating %u id=%u type=%c",
2106ee93961bSJeff Mahoney 		       pasted_size, inode->i_uid,
2107a228bf8fSJeff Mahoney 		       key2type(&key->on_disk_key));
21081da177e4SLinus Torvalds #endif
21091da177e4SLinus Torvalds 
2110d2d0395fSJeff Mahoney 	depth = reiserfs_write_unlock_nested(sb);
21115dd4056dSChristoph Hellwig 	retval = dquot_alloc_space_nodirty(inode, pasted_size);
2112d2d0395fSJeff Mahoney 	reiserfs_write_lock_nested(sb, depth);
21135dd4056dSChristoph Hellwig 	if (retval) {
2114d68caa95SJeff Mahoney 		pathrelse(search_path);
21155dd4056dSChristoph Hellwig 		return retval;
21161da177e4SLinus Torvalds 	}
2117d68caa95SJeff Mahoney 	init_tb_struct(th, &s_paste_balance, th->t_super, search_path,
2118ee93961bSJeff Mahoney 		       pasted_size);
21191da177e4SLinus Torvalds #ifdef DISPLACE_NEW_PACKING_LOCALITIES
2120d68caa95SJeff Mahoney 	s_paste_balance.key = key->on_disk_key;
21211da177e4SLinus Torvalds #endif
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds 	/* DQUOT_* can schedule, must check before the fix_nodes */
21241da177e4SLinus Torvalds 	if (fs_changed(fs_gen, inode->i_sb)) {
21251da177e4SLinus Torvalds 		goto search_again;
21261da177e4SLinus Torvalds 	}
21271da177e4SLinus Torvalds 
2128bd4c625cSLinus Torvalds 	while ((retval =
2129bd4c625cSLinus Torvalds 		fix_nodes(M_PASTE, &s_paste_balance, NULL,
2130d68caa95SJeff Mahoney 			  body)) == REPEAT_SEARCH) {
21311da177e4SLinus Torvalds search_again:
21321da177e4SLinus Torvalds 		/* file system changed while we were in the fix_nodes */
21331da177e4SLinus Torvalds 		PROC_INFO_INC(th->t_super, paste_into_item_restarted);
2134bd4c625cSLinus Torvalds 		retval =
2135d68caa95SJeff Mahoney 		    search_for_position_by_key(th->t_super, key,
2136d68caa95SJeff Mahoney 					       search_path);
21371da177e4SLinus Torvalds 		if (retval == IO_ERROR) {
21381da177e4SLinus Torvalds 			retval = -EIO;
21391da177e4SLinus Torvalds 			goto error_out;
21401da177e4SLinus Torvalds 		}
21411da177e4SLinus Torvalds 		if (retval == POSITION_FOUND) {
214245b03d5eSJeff Mahoney 			reiserfs_warning(inode->i_sb, "PAP-5710",
214345b03d5eSJeff Mahoney 					 "entry or pasted byte (%K) exists",
2144d68caa95SJeff Mahoney 					 key);
21451da177e4SLinus Torvalds 			retval = -EEXIST;
21461da177e4SLinus Torvalds 			goto error_out;
21471da177e4SLinus Torvalds 		}
21481da177e4SLinus Torvalds #ifdef CONFIG_REISERFS_CHECK
2149d68caa95SJeff Mahoney 		check_research_for_paste(search_path, key);
21501da177e4SLinus Torvalds #endif
21511da177e4SLinus Torvalds 	}
21521da177e4SLinus Torvalds 
2153098297b2SJeff Mahoney 	/*
2154098297b2SJeff Mahoney 	 * Perform balancing after all resources are collected by fix_nodes,
2155098297b2SJeff Mahoney 	 * and accessing them will not risk triggering schedule.
2156098297b2SJeff Mahoney 	 */
21571da177e4SLinus Torvalds 	if (retval == CARRY_ON) {
2158d68caa95SJeff Mahoney 		do_balance(&s_paste_balance, NULL /*ih */ , body, M_PASTE);
21591da177e4SLinus Torvalds 		return 0;
21601da177e4SLinus Torvalds 	}
21611da177e4SLinus Torvalds 	retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
21621da177e4SLinus Torvalds error_out:
21631da177e4SLinus Torvalds 	/* this also releases the path */
21641da177e4SLinus Torvalds 	unfix_nodes(&s_paste_balance);
21651da177e4SLinus Torvalds #ifdef REISERQUOTA_DEBUG
2166bd4c625cSLinus Torvalds 	reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
2167bd4c625cSLinus Torvalds 		       "reiserquota paste_into_item(): freeing %u id=%u type=%c",
2168ee93961bSJeff Mahoney 		       pasted_size, inode->i_uid,
2169a228bf8fSJeff Mahoney 		       key2type(&key->on_disk_key));
21701da177e4SLinus Torvalds #endif
2171d2d0395fSJeff Mahoney 	depth = reiserfs_write_unlock_nested(sb);
21725dd4056dSChristoph Hellwig 	dquot_free_space_nodirty(inode, pasted_size);
2173d2d0395fSJeff Mahoney 	reiserfs_write_lock_nested(sb, depth);
21741da177e4SLinus Torvalds 	return retval;
21751da177e4SLinus Torvalds }
21761da177e4SLinus Torvalds 
2177098297b2SJeff Mahoney /*
2178098297b2SJeff Mahoney  * Insert new item into the buffer at the path.
2179d68caa95SJeff Mahoney  * th   - active transaction handle
2180d68caa95SJeff Mahoney  * path - path to the inserted item
2181d68caa95SJeff Mahoney  * ih   - pointer to the item header to insert
2182d68caa95SJeff Mahoney  * body - pointer to the bytes to insert
2183d68caa95SJeff Mahoney  */
reiserfs_insert_item(struct reiserfs_transaction_handle * th,struct treepath * path,const struct cpu_key * key,struct item_head * ih,struct inode * inode,const char * body)2184d68caa95SJeff Mahoney int reiserfs_insert_item(struct reiserfs_transaction_handle *th,
2185d68caa95SJeff Mahoney 			 struct treepath *path, const struct cpu_key *key,
2186d68caa95SJeff Mahoney 			 struct item_head *ih, struct inode *inode,
2187d68caa95SJeff Mahoney 			 const char *body)
2188d68caa95SJeff Mahoney {
21891da177e4SLinus Torvalds 	struct tree_balance s_ins_balance;
21901da177e4SLinus Torvalds 	int retval;
21911da177e4SLinus Torvalds 	int fs_gen = 0;
21921da177e4SLinus Torvalds 	int quota_bytes = 0;
21931da177e4SLinus Torvalds 
21941da177e4SLinus Torvalds 	BUG_ON(!th->t_trans_id);
21951da177e4SLinus Torvalds 
21961da177e4SLinus Torvalds 	if (inode) {		/* Do we count quotas for item? */
2197d2d0395fSJeff Mahoney 		int depth;
21981da177e4SLinus Torvalds 		fs_gen = get_generation(inode->i_sb);
2199d68caa95SJeff Mahoney 		quota_bytes = ih_item_len(ih);
22001da177e4SLinus Torvalds 
2201098297b2SJeff Mahoney 		/*
2202098297b2SJeff Mahoney 		 * hack so the quota code doesn't have to guess
2203098297b2SJeff Mahoney 		 * if the file has a tail, links are always tails,
2204098297b2SJeff Mahoney 		 * so there's no guessing needed
22051da177e4SLinus Torvalds 		 */
2206d68caa95SJeff Mahoney 		if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(ih))
22071da177e4SLinus Torvalds 			quota_bytes = inode->i_sb->s_blocksize + UNFM_P_SIZE;
22081da177e4SLinus Torvalds #ifdef REISERQUOTA_DEBUG
2209bd4c625cSLinus Torvalds 		reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
2210bd4c625cSLinus Torvalds 			       "reiserquota insert_item(): allocating %u id=%u type=%c",
2211d68caa95SJeff Mahoney 			       quota_bytes, inode->i_uid, head2type(ih));
22121da177e4SLinus Torvalds #endif
2213098297b2SJeff Mahoney 		/*
2214098297b2SJeff Mahoney 		 * We can't dirty inode here. It would be immediately
2215098297b2SJeff Mahoney 		 * written but appropriate stat item isn't inserted yet...
2216098297b2SJeff Mahoney 		 */
2217d2d0395fSJeff Mahoney 		depth = reiserfs_write_unlock_nested(inode->i_sb);
22185dd4056dSChristoph Hellwig 		retval = dquot_alloc_space_nodirty(inode, quota_bytes);
2219d2d0395fSJeff Mahoney 		reiserfs_write_lock_nested(inode->i_sb, depth);
22205dd4056dSChristoph Hellwig 		if (retval) {
2221d68caa95SJeff Mahoney 			pathrelse(path);
22225dd4056dSChristoph Hellwig 			return retval;
22231da177e4SLinus Torvalds 		}
22241da177e4SLinus Torvalds 	}
2225d68caa95SJeff Mahoney 	init_tb_struct(th, &s_ins_balance, th->t_super, path,
2226d68caa95SJeff Mahoney 		       IH_SIZE + ih_item_len(ih));
22271da177e4SLinus Torvalds #ifdef DISPLACE_NEW_PACKING_LOCALITIES
22281da177e4SLinus Torvalds 	s_ins_balance.key = key->on_disk_key;
22291da177e4SLinus Torvalds #endif
2230098297b2SJeff Mahoney 	/*
2231098297b2SJeff Mahoney 	 * DQUOT_* can schedule, must check to be sure calling
2232098297b2SJeff Mahoney 	 * fix_nodes is safe
2233098297b2SJeff Mahoney 	 */
22341da177e4SLinus Torvalds 	if (inode && fs_changed(fs_gen, inode->i_sb)) {
22351da177e4SLinus Torvalds 		goto search_again;
22361da177e4SLinus Torvalds 	}
22371da177e4SLinus Torvalds 
2238bd4c625cSLinus Torvalds 	while ((retval =
2239d68caa95SJeff Mahoney 		fix_nodes(M_INSERT, &s_ins_balance, ih,
2240d68caa95SJeff Mahoney 			  body)) == REPEAT_SEARCH) {
22411da177e4SLinus Torvalds search_again:
22421da177e4SLinus Torvalds 		/* file system changed while we were in the fix_nodes */
22431da177e4SLinus Torvalds 		PROC_INFO_INC(th->t_super, insert_item_restarted);
2244d68caa95SJeff Mahoney 		retval = search_item(th->t_super, key, path);
22451da177e4SLinus Torvalds 		if (retval == IO_ERROR) {
22461da177e4SLinus Torvalds 			retval = -EIO;
22471da177e4SLinus Torvalds 			goto error_out;
22481da177e4SLinus Torvalds 		}
22491da177e4SLinus Torvalds 		if (retval == ITEM_FOUND) {
225045b03d5eSJeff Mahoney 			reiserfs_warning(th->t_super, "PAP-5760",
2251bd4c625cSLinus Torvalds 					 "key %K already exists in the tree",
2252bd4c625cSLinus Torvalds 					 key);
22531da177e4SLinus Torvalds 			retval = -EEXIST;
22541da177e4SLinus Torvalds 			goto error_out;
22551da177e4SLinus Torvalds 		}
22561da177e4SLinus Torvalds 	}
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds 	/* make balancing after all resources will be collected at a time */
22591da177e4SLinus Torvalds 	if (retval == CARRY_ON) {
2260d68caa95SJeff Mahoney 		do_balance(&s_ins_balance, ih, body, M_INSERT);
22611da177e4SLinus Torvalds 		return 0;
22621da177e4SLinus Torvalds 	}
22631da177e4SLinus Torvalds 
22641da177e4SLinus Torvalds 	retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
22651da177e4SLinus Torvalds error_out:
22661da177e4SLinus Torvalds 	/* also releases the path */
22671da177e4SLinus Torvalds 	unfix_nodes(&s_ins_balance);
22681da177e4SLinus Torvalds #ifdef REISERQUOTA_DEBUG
2269aacee544SYunfeng Ye 	if (inode)
2270bd4c625cSLinus Torvalds 		reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
2271bd4c625cSLinus Torvalds 		       "reiserquota insert_item(): freeing %u id=%u type=%c",
2272d68caa95SJeff Mahoney 		       quota_bytes, inode->i_uid, head2type(ih));
22731da177e4SLinus Torvalds #endif
2274d2d0395fSJeff Mahoney 	if (inode) {
2275d2d0395fSJeff Mahoney 		int depth = reiserfs_write_unlock_nested(inode->i_sb);
22765dd4056dSChristoph Hellwig 		dquot_free_space_nodirty(inode, quota_bytes);
2277d2d0395fSJeff Mahoney 		reiserfs_write_lock_nested(inode->i_sb, depth);
2278d2d0395fSJeff Mahoney 	}
22791da177e4SLinus Torvalds 	return retval;
22801da177e4SLinus Torvalds }
2281