| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README | 
 | 3 |  */ | 
 | 4 | /* Reiserfs block (de)allocator, bitmap-based. */ | 
 | 5 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/time.h> | 
 | 7 | #include <linux/reiserfs_fs.h> | 
 | 8 | #include <linux/errno.h> | 
 | 9 | #include <linux/buffer_head.h> | 
 | 10 | #include <linux/kernel.h> | 
 | 11 | #include <linux/pagemap.h> | 
| Jeff Mahoney | 6f01046 | 2006-09-30 23:28:43 -0700 | [diff] [blame] | 12 | #include <linux/vmalloc.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/reiserfs_fs_sb.h> | 
 | 14 | #include <linux/reiserfs_fs_i.h> | 
 | 15 | #include <linux/quotaops.h> | 
 | 16 |  | 
 | 17 | #define PREALLOCATION_SIZE 9 | 
 | 18 |  | 
 | 19 | /* different reiserfs block allocator options */ | 
 | 20 |  | 
 | 21 | #define SB_ALLOC_OPTS(s) (REISERFS_SB(s)->s_alloc_options.bits) | 
 | 22 |  | 
 | 23 | #define  _ALLOC_concentrating_formatted_nodes 0 | 
 | 24 | #define  _ALLOC_displacing_large_files 1 | 
 | 25 | #define  _ALLOC_displacing_new_packing_localities 2 | 
 | 26 | #define  _ALLOC_old_hashed_relocation 3 | 
 | 27 | #define  _ALLOC_new_hashed_relocation 4 | 
 | 28 | #define  _ALLOC_skip_busy 5 | 
 | 29 | #define  _ALLOC_displace_based_on_dirid 6 | 
 | 30 | #define  _ALLOC_hashed_formatted_nodes 7 | 
 | 31 | #define  _ALLOC_old_way 8 | 
 | 32 | #define  _ALLOC_hundredth_slices 9 | 
 | 33 | #define  _ALLOC_dirid_groups 10 | 
 | 34 | #define  _ALLOC_oid_groups 11 | 
 | 35 | #define  _ALLOC_packing_groups 12 | 
 | 36 |  | 
 | 37 | #define  concentrating_formatted_nodes(s)	test_bit(_ALLOC_concentrating_formatted_nodes, &SB_ALLOC_OPTS(s)) | 
 | 38 | #define  displacing_large_files(s)		test_bit(_ALLOC_displacing_large_files, &SB_ALLOC_OPTS(s)) | 
 | 39 | #define  displacing_new_packing_localities(s)	test_bit(_ALLOC_displacing_new_packing_localities, &SB_ALLOC_OPTS(s)) | 
 | 40 |  | 
 | 41 | #define SET_OPTION(optname) \ | 
 | 42 |    do { \ | 
| Jeff Mahoney | 1d889d9 | 2009-03-30 14:02:20 -0400 | [diff] [blame] | 43 | 	reiserfs_info(s, "block allocator option \"%s\" is set", #optname); \ | 
 | 44 | 	set_bit(_ALLOC_ ## optname , &SB_ALLOC_OPTS(s)); \ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 |     } while(0) | 
 | 46 | #define TEST_OPTION(optname, s) \ | 
 | 47 |     test_bit(_ALLOC_ ## optname , &SB_ALLOC_OPTS(s)) | 
 | 48 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 49 | static inline void get_bit_address(struct super_block *s, | 
| Jeff Mahoney | 3ee1667 | 2007-10-18 23:39:25 -0700 | [diff] [blame] | 50 | 				   b_blocknr_t block, | 
 | 51 | 				   unsigned int *bmap_nr, | 
 | 52 | 				   unsigned int *offset) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 54 | 	/* It is in the bitmap block number equal to the block | 
 | 55 | 	 * number divided by the number of bits in a block. */ | 
| Jeff Mahoney | e1fabd3 | 2006-09-30 23:28:40 -0700 | [diff] [blame] | 56 | 	*bmap_nr = block >> (s->s_blocksize_bits + 3); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 57 | 	/* Within that bitmap block it is located at bit offset *offset. */ | 
 | 58 | 	*offset = block & ((s->s_blocksize << 3) - 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } | 
 | 60 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 61 | int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { | 
| Jeff Mahoney | 3ee1667 | 2007-10-18 23:39:25 -0700 | [diff] [blame] | 63 | 	unsigned int bmap, offset; | 
| Jeff Mahoney | cb680c1 | 2007-10-18 23:39:27 -0700 | [diff] [blame] | 64 | 	unsigned int bmap_count = reiserfs_bmap_count(s); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 66 | 	if (block == 0 || block >= SB_BLOCK_COUNT(s)) { | 
| Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 67 | 		reiserfs_error(s, "vs-4010", | 
 | 68 | 			       "block number is out of range %lu (%u)", | 
 | 69 | 			       block, SB_BLOCK_COUNT(s)); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 70 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 |  | 
| Jeff Mahoney | e1fabd3 | 2006-09-30 23:28:40 -0700 | [diff] [blame] | 73 | 	get_bit_address(s, block, &bmap, &offset); | 
 | 74 |  | 
 | 75 | 	/* Old format filesystem? Unlikely, but the bitmaps are all up front so | 
 | 76 | 	 * we need to account for it. */ | 
 | 77 | 	if (unlikely(test_bit(REISERFS_OLD_FORMAT, | 
 | 78 | 			      &(REISERFS_SB(s)->s_properties)))) { | 
 | 79 | 		b_blocknr_t bmap1 = REISERFS_SB(s)->s_sbh->b_blocknr + 1; | 
| Jeff Mahoney | cb680c1 | 2007-10-18 23:39:27 -0700 | [diff] [blame] | 80 | 		if (block >= bmap1 && | 
 | 81 | 		    block <= bmap1 + bmap_count) { | 
| Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 82 | 			reiserfs_error(s, "vs-4019", "bitmap block %lu(%u) " | 
 | 83 | 				       "can't be freed or reused", | 
 | 84 | 				       block, bmap_count); | 
| Jeff Mahoney | e1fabd3 | 2006-09-30 23:28:40 -0700 | [diff] [blame] | 85 | 			return 0; | 
 | 86 | 		} | 
 | 87 | 	} else { | 
 | 88 | 		if (offset == 0) { | 
| Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 89 | 			reiserfs_error(s, "vs-4020", "bitmap block %lu(%u) " | 
 | 90 | 				       "can't be freed or reused", | 
 | 91 | 				       block, bmap_count); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 92 | 			return 0; | 
 | 93 | 		} | 
| Jeff Mahoney | e1fabd3 | 2006-09-30 23:28:40 -0700 | [diff] [blame] | 94 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 |  | 
| Jeff Mahoney | cb680c1 | 2007-10-18 23:39:27 -0700 | [diff] [blame] | 96 | 	if (bmap >= bmap_count) { | 
| Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 97 | 		reiserfs_error(s, "vs-4030", "bitmap for requested block " | 
 | 98 | 			       "is out of range: block=%lu, bitmap_nr=%u", | 
 | 99 | 			       block, bmap); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 100 | 		return 0; | 
 | 101 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 103 | 	if (bit_value == 0 && block == SB_ROOT_BLOCK(s)) { | 
| Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 104 | 		reiserfs_error(s, "vs-4050", "this is root block (%u), " | 
 | 105 | 			       "it must be busy", SB_ROOT_BLOCK(s)); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 106 | 		return 0; | 
 | 107 | 	} | 
 | 108 |  | 
 | 109 | 	return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 |  | 
 | 112 | /* searches in journal structures for a given block number (bmap, off). If block | 
 | 113 |    is found in reiserfs journal it suggests next free block candidate to test. */ | 
| Jeff Mahoney | 3ee1667 | 2007-10-18 23:39:25 -0700 | [diff] [blame] | 114 | static inline int is_block_in_journal(struct super_block *s, unsigned int bmap, | 
 | 115 | 				      int off, int *next) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 117 | 	b_blocknr_t tmp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 119 | 	if (reiserfs_in_journal(s, bmap, off, 1, &tmp)) { | 
 | 120 | 		if (tmp) {	/* hint supplied */ | 
 | 121 | 			*next = tmp; | 
 | 122 | 			PROC_INFO_INC(s, scan_bitmap.in_journal_hint); | 
 | 123 | 		} else { | 
 | 124 | 			(*next) = off + 1;	/* inc offset to avoid looping. */ | 
 | 125 | 			PROC_INFO_INC(s, scan_bitmap.in_journal_nohint); | 
 | 126 | 		} | 
 | 127 | 		PROC_INFO_INC(s, scan_bitmap.retry); | 
 | 128 | 		return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | 	} | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 130 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } | 
 | 132 |  | 
 | 133 | /* it searches for a window of zero bits with given minimum and maximum lengths in one bitmap | 
 | 134 |  * block; */ | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 135 | static int scan_bitmap_block(struct reiserfs_transaction_handle *th, | 
| Jeff Mahoney | 3ee1667 | 2007-10-18 23:39:25 -0700 | [diff] [blame] | 136 | 			     unsigned int bmap_n, int *beg, int boundary, | 
 | 137 | 			     int min, int max, int unfm) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 139 | 	struct super_block *s = th->t_super; | 
 | 140 | 	struct reiserfs_bitmap_info *bi = &SB_AP_BITMAP(s)[bmap_n]; | 
| Jeff Mahoney | 0b3dc17 | 2006-09-30 23:28:42 -0700 | [diff] [blame] | 141 | 	struct buffer_head *bh; | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 142 | 	int end, next; | 
 | 143 | 	int org = *beg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 145 | 	BUG_ON(!th->t_trans_id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 |  | 
| Jeff Mahoney | cb680c1 | 2007-10-18 23:39:27 -0700 | [diff] [blame] | 147 | 	RFALSE(bmap_n >= reiserfs_bmap_count(s), "Bitmap %u is out of " | 
 | 148 | 	       "range (0..%u)", bmap_n, reiserfs_bmap_count(s) - 1); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 149 | 	PROC_INFO_INC(s, scan_bitmap.bmap); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | /* this is unclear and lacks comments, explain how journal bitmaps | 
 | 151 |    work here for the reader.  Convey a sense of the design here. What | 
 | 152 |    is a window? */ | 
 | 153 | /* - I mean `a window of zero bits' as in description of this function - Zam. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 155 | 	if (!bi) { | 
| Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 156 | 		reiserfs_error(s, "jdm-4055", "NULL bitmap info pointer " | 
 | 157 | 			       "for bitmap %d", bmap_n); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 158 | 		return 0; | 
 | 159 | 	} | 
| Jeff Mahoney | 0b3dc17 | 2006-09-30 23:28:42 -0700 | [diff] [blame] | 160 |  | 
| Jeff Mahoney | 5065227 | 2006-09-30 23:28:44 -0700 | [diff] [blame] | 161 | 	bh = reiserfs_read_bitmap_block(s, bmap_n); | 
 | 162 | 	if (bh == NULL) | 
 | 163 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 165 | 	while (1) { | 
 | 166 | 	      cont: | 
| Jeff Mahoney | 0b3dc17 | 2006-09-30 23:28:42 -0700 | [diff] [blame] | 167 | 		if (bi->free_count < min) { | 
 | 168 | 			brelse(bh); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 169 | 			return 0;	// No free blocks in this bitmap | 
| Jeff Mahoney | 0b3dc17 | 2006-09-30 23:28:42 -0700 | [diff] [blame] | 170 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 |  | 
| Daniel Mack | 3ad2f3f | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 172 | 		/* search for a first zero bit -- beginning of a window */ | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 173 | 		*beg = reiserfs_find_next_zero_le_bit | 
| Jeff Mahoney | 0b3dc17 | 2006-09-30 23:28:42 -0700 | [diff] [blame] | 174 | 		    ((unsigned long *)(bh->b_data), boundary, *beg); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 175 |  | 
 | 176 | 		if (*beg + min > boundary) {	/* search for a zero bit fails or the rest of bitmap block | 
 | 177 | 						 * cannot contain a zero window of minimum size */ | 
| Jeff Mahoney | 0b3dc17 | 2006-09-30 23:28:42 -0700 | [diff] [blame] | 178 | 			brelse(bh); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 179 | 			return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 182 | 		if (unfm && is_block_in_journal(s, bmap_n, *beg, beg)) | 
 | 183 | 			continue; | 
 | 184 | 		/* first zero bit found; we check next bits */ | 
 | 185 | 		for (end = *beg + 1;; end++) { | 
 | 186 | 			if (end >= *beg + max || end >= boundary | 
| Jeff Mahoney | 0b3dc17 | 2006-09-30 23:28:42 -0700 | [diff] [blame] | 187 | 			    || reiserfs_test_le_bit(end, bh->b_data)) { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 188 | 				next = end; | 
 | 189 | 				break; | 
 | 190 | 			} | 
 | 191 | 			/* finding the other end of zero bit window requires looking into journal structures (in | 
 | 192 | 			 * case of searching for free blocks for unformatted nodes) */ | 
 | 193 | 			if (unfm && is_block_in_journal(s, bmap_n, end, &next)) | 
 | 194 | 				break; | 
 | 195 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 197 | 		/* now (*beg) points to beginning of zero bits window, | 
 | 198 | 		 * (end) points to one bit after the window end */ | 
 | 199 | 		if (end - *beg >= min) {	/* it seems we have found window of proper size */ | 
 | 200 | 			int i; | 
| Jeff Mahoney | 0b3dc17 | 2006-09-30 23:28:42 -0700 | [diff] [blame] | 201 | 			reiserfs_prepare_for_journal(s, bh, 1); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 202 | 			/* try to set all blocks used checking are they still free */ | 
 | 203 | 			for (i = *beg; i < end; i++) { | 
 | 204 | 				/* It seems that we should not check in journal again. */ | 
 | 205 | 				if (reiserfs_test_and_set_le_bit | 
| Jeff Mahoney | 0b3dc17 | 2006-09-30 23:28:42 -0700 | [diff] [blame] | 206 | 				    (i, bh->b_data)) { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 207 | 					/* bit was set by another process | 
 | 208 | 					 * while we slept in prepare_for_journal() */ | 
 | 209 | 					PROC_INFO_INC(s, scan_bitmap.stolen); | 
 | 210 | 					if (i >= *beg + min) {	/* we can continue with smaller set of allocated blocks, | 
 | 211 | 								 * if length of this set is more or equal to `min' */ | 
 | 212 | 						end = i; | 
 | 213 | 						break; | 
 | 214 | 					} | 
 | 215 | 					/* otherwise we clear all bit were set ... */ | 
 | 216 | 					while (--i >= *beg) | 
 | 217 | 						reiserfs_test_and_clear_le_bit | 
| Jeff Mahoney | 0b3dc17 | 2006-09-30 23:28:42 -0700 | [diff] [blame] | 218 | 						    (i, bh->b_data); | 
 | 219 | 					reiserfs_restore_prepared_buffer(s, bh); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 220 | 					*beg = org; | 
 | 221 | 					/* ... and search again in current block from beginning */ | 
 | 222 | 					goto cont; | 
 | 223 | 				} | 
 | 224 | 			} | 
 | 225 | 			bi->free_count -= (end - *beg); | 
| Jeff Mahoney | 0b3dc17 | 2006-09-30 23:28:42 -0700 | [diff] [blame] | 226 | 			journal_mark_dirty(th, s, bh); | 
 | 227 | 			brelse(bh); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 228 |  | 
 | 229 | 			/* free block count calculation */ | 
 | 230 | 			reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), | 
 | 231 | 						     1); | 
 | 232 | 			PUT_SB_FREE_BLOCKS(s, SB_FREE_BLOCKS(s) - (end - *beg)); | 
 | 233 | 			journal_mark_dirty(th, s, SB_BUFFER_WITH_SB(s)); | 
 | 234 |  | 
 | 235 | 			return end - (*beg); | 
 | 236 | 		} else { | 
 | 237 | 			*beg = next; | 
 | 238 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } | 
 | 241 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 242 | static int bmap_hash_id(struct super_block *s, u32 id) | 
 | 243 | { | 
 | 244 | 	char *hash_in = NULL; | 
 | 245 | 	unsigned long hash; | 
 | 246 | 	unsigned bm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 248 | 	if (id <= 2) { | 
 | 249 | 		bm = 1; | 
 | 250 | 	} else { | 
 | 251 | 		hash_in = (char *)(&id); | 
 | 252 | 		hash = keyed_hash(hash_in, 4); | 
| Jeff Mahoney | cb680c1 | 2007-10-18 23:39:27 -0700 | [diff] [blame] | 253 | 		bm = hash % reiserfs_bmap_count(s); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 254 | 		if (!bm) | 
 | 255 | 			bm = 1; | 
 | 256 | 	} | 
 | 257 | 	/* this can only be true when SB_BMAP_NR = 1 */ | 
| Jeff Mahoney | cb680c1 | 2007-10-18 23:39:27 -0700 | [diff] [blame] | 258 | 	if (bm >= reiserfs_bmap_count(s)) | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 259 | 		bm = 0; | 
 | 260 | 	return bm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } | 
 | 262 |  | 
 | 263 | /* | 
 | 264 |  * hashes the id and then returns > 0 if the block group for the | 
 | 265 |  * corresponding hash is full | 
 | 266 |  */ | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 267 | static inline int block_group_used(struct super_block *s, u32 id) | 
 | 268 | { | 
| Jeff Mahoney | 5065227 | 2006-09-30 23:28:44 -0700 | [diff] [blame] | 269 | 	int bm = bmap_hash_id(s, id); | 
 | 270 | 	struct reiserfs_bitmap_info *info = &SB_AP_BITMAP(s)[bm]; | 
 | 271 |  | 
 | 272 | 	/* If we don't have cached information on this bitmap block, we're | 
 | 273 | 	 * going to have to load it later anyway. Loading it here allows us | 
| Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 274 | 	 * to make a better decision. This favors long-term performance gain | 
| Jeff Mahoney | 5065227 | 2006-09-30 23:28:44 -0700 | [diff] [blame] | 275 | 	 * with a better on-disk layout vs. a short term gain of skipping the | 
 | 276 | 	 * read and potentially having a bad placement. */ | 
| Jeff Mahoney | 4d20851 | 2007-10-18 23:39:26 -0700 | [diff] [blame] | 277 | 	if (info->free_count == UINT_MAX) { | 
| Jeff Mahoney | 5065227 | 2006-09-30 23:28:44 -0700 | [diff] [blame] | 278 | 		struct buffer_head *bh = reiserfs_read_bitmap_block(s, bm); | 
 | 279 | 		brelse(bh); | 
 | 280 | 	} | 
 | 281 |  | 
 | 282 | 	if (info->free_count > ((s->s_blocksize << 3) * 60 / 100)) { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 283 | 		return 0; | 
 | 284 | 	} | 
 | 285 | 	return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } | 
 | 287 |  | 
 | 288 | /* | 
 | 289 |  * the packing is returned in disk byte order | 
 | 290 |  */ | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 291 | __le32 reiserfs_choose_packing(struct inode * dir) | 
| Al Viro | 3e8962b | 2005-05-01 08:59:18 -0700 | [diff] [blame] | 292 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 293 | 	__le32 packing; | 
 | 294 | 	if (TEST_OPTION(packing_groups, dir->i_sb)) { | 
 | 295 | 		u32 parent_dir = le32_to_cpu(INODE_PKEY(dir)->k_dir_id); | 
 | 296 | 		/* | 
 | 297 | 		 * some versions of reiserfsck expect packing locality 1 to be | 
 | 298 | 		 * special | 
 | 299 | 		 */ | 
 | 300 | 		if (parent_dir == 1 || block_group_used(dir->i_sb, parent_dir)) | 
 | 301 | 			packing = INODE_PKEY(dir)->k_objectid; | 
 | 302 | 		else | 
 | 303 | 			packing = INODE_PKEY(dir)->k_dir_id; | 
 | 304 | 	} else | 
 | 305 | 		packing = INODE_PKEY(dir)->k_objectid; | 
 | 306 | 	return packing; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 308 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | /* Tries to find contiguous zero bit window (given size) in given region of | 
 | 310 |  * bitmap and place new blocks there. Returns number of allocated blocks. */ | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 311 | static int scan_bitmap(struct reiserfs_transaction_handle *th, | 
 | 312 | 		       b_blocknr_t * start, b_blocknr_t finish, | 
| Jeff Mahoney | 3ee1667 | 2007-10-18 23:39:25 -0700 | [diff] [blame] | 313 | 		       int min, int max, int unfm, sector_t file_block) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 315 | 	int nr_allocated = 0; | 
 | 316 | 	struct super_block *s = th->t_super; | 
 | 317 | 	/* find every bm and bmap and bmap_nr in this file, and change them all to bitmap_blocknr | 
 | 318 | 	 * - Hans, it is not a block number - Zam. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 |  | 
| Jeff Mahoney | 3ee1667 | 2007-10-18 23:39:25 -0700 | [diff] [blame] | 320 | 	unsigned int bm, off; | 
 | 321 | 	unsigned int end_bm, end_off; | 
 | 322 | 	unsigned int off_max = s->s_blocksize << 3; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 324 | 	BUG_ON(!th->t_trans_id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 326 | 	PROC_INFO_INC(s, scan_bitmap.call); | 
 | 327 | 	if (SB_FREE_BLOCKS(s) <= 0) | 
 | 328 | 		return 0;	// No point in looking for more free blocks | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 330 | 	get_bit_address(s, *start, &bm, &off); | 
 | 331 | 	get_bit_address(s, finish, &end_bm, &end_off); | 
| Jeff Mahoney | cb680c1 | 2007-10-18 23:39:27 -0700 | [diff] [blame] | 332 | 	if (bm > reiserfs_bmap_count(s)) | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 333 | 		return 0; | 
| Jeff Mahoney | cb680c1 | 2007-10-18 23:39:27 -0700 | [diff] [blame] | 334 | 	if (end_bm > reiserfs_bmap_count(s)) | 
 | 335 | 		end_bm = reiserfs_bmap_count(s); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 337 | 	/* When the bitmap is more than 10% free, anyone can allocate. | 
 | 338 | 	 * When it's less than 10% free, only files that already use the | 
 | 339 | 	 * bitmap are allowed. Once we pass 80% full, this restriction | 
 | 340 | 	 * is lifted. | 
 | 341 | 	 * | 
 | 342 | 	 * We do this so that files that grow later still have space close to | 
 | 343 | 	 * their original allocation. This improves locality, and presumably | 
 | 344 | 	 * performance as a result. | 
 | 345 | 	 * | 
 | 346 | 	 * This is only an allocation policy and does not make up for getting a | 
 | 347 | 	 * bad hint. Decent hinting must be implemented for this to work well. | 
 | 348 | 	 */ | 
 | 349 | 	if (TEST_OPTION(skip_busy, s) | 
 | 350 | 	    && SB_FREE_BLOCKS(s) > SB_BLOCK_COUNT(s) / 20) { | 
 | 351 | 		for (; bm < end_bm; bm++, off = 0) { | 
 | 352 | 			if ((off && (!unfm || (file_block != 0))) | 
 | 353 | 			    || SB_AP_BITMAP(s)[bm].free_count > | 
 | 354 | 			    (s->s_blocksize << 3) / 10) | 
 | 355 | 				nr_allocated = | 
 | 356 | 				    scan_bitmap_block(th, bm, &off, off_max, | 
 | 357 | 						      min, max, unfm); | 
 | 358 | 			if (nr_allocated) | 
 | 359 | 				goto ret; | 
 | 360 | 		} | 
 | 361 | 		/* we know from above that start is a reasonable number */ | 
 | 362 | 		get_bit_address(s, *start, &bm, &off); | 
 | 363 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 365 | 	for (; bm < end_bm; bm++, off = 0) { | 
 | 366 | 		nr_allocated = | 
 | 367 | 		    scan_bitmap_block(th, bm, &off, off_max, min, max, unfm); | 
 | 368 | 		if (nr_allocated) | 
 | 369 | 			goto ret; | 
 | 370 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 372 | 	nr_allocated = | 
 | 373 | 	    scan_bitmap_block(th, bm, &off, end_off + 1, min, max, unfm); | 
 | 374 |  | 
 | 375 |       ret: | 
 | 376 | 	*start = bm * off_max + off; | 
 | 377 | 	return nr_allocated; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 |  | 
 | 379 | } | 
 | 380 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 381 | static void _reiserfs_free_block(struct reiserfs_transaction_handle *th, | 
 | 382 | 				 struct inode *inode, b_blocknr_t block, | 
 | 383 | 				 int for_unformatted) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 385 | 	struct super_block *s = th->t_super; | 
 | 386 | 	struct reiserfs_super_block *rs; | 
| Jeff Mahoney | 0b3dc17 | 2006-09-30 23:28:42 -0700 | [diff] [blame] | 387 | 	struct buffer_head *sbh, *bmbh; | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 388 | 	struct reiserfs_bitmap_info *apbi; | 
| Jeff Mahoney | 3ee1667 | 2007-10-18 23:39:25 -0700 | [diff] [blame] | 389 | 	unsigned int nr, offset; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 391 | 	BUG_ON(!th->t_trans_id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 393 | 	PROC_INFO_INC(s, free_block); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 395 | 	rs = SB_DISK_SUPER_BLOCK(s); | 
 | 396 | 	sbh = SB_BUFFER_WITH_SB(s); | 
 | 397 | 	apbi = SB_AP_BITMAP(s); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 399 | 	get_bit_address(s, block, &nr, &offset); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 |  | 
| Jeff Mahoney | cb680c1 | 2007-10-18 23:39:27 -0700 | [diff] [blame] | 401 | 	if (nr >= reiserfs_bmap_count(s)) { | 
| Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 402 | 		reiserfs_error(s, "vs-4075", "block %lu is out of range", | 
 | 403 | 			       block); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 404 | 		return; | 
 | 405 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 |  | 
| Jeff Mahoney | 5065227 | 2006-09-30 23:28:44 -0700 | [diff] [blame] | 407 | 	bmbh = reiserfs_read_bitmap_block(s, nr); | 
 | 408 | 	if (!bmbh) | 
 | 409 | 		return; | 
| Jeff Mahoney | 0b3dc17 | 2006-09-30 23:28:42 -0700 | [diff] [blame] | 410 |  | 
 | 411 | 	reiserfs_prepare_for_journal(s, bmbh, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 413 | 	/* clear bit for the given block in bit map */ | 
| Jeff Mahoney | 0b3dc17 | 2006-09-30 23:28:42 -0700 | [diff] [blame] | 414 | 	if (!reiserfs_test_and_clear_le_bit(offset, bmbh->b_data)) { | 
| Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 415 | 		reiserfs_error(s, "vs-4080", | 
 | 416 | 			       "block %lu: bit already cleared", block); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 417 | 	} | 
 | 418 | 	apbi[nr].free_count++; | 
| Jeff Mahoney | 0b3dc17 | 2006-09-30 23:28:42 -0700 | [diff] [blame] | 419 | 	journal_mark_dirty(th, s, bmbh); | 
 | 420 | 	brelse(bmbh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 422 | 	reiserfs_prepare_for_journal(s, sbh, 1); | 
 | 423 | 	/* update super block */ | 
 | 424 | 	set_sb_free_blocks(rs, sb_free_blocks(rs) + 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 426 | 	journal_mark_dirty(th, s, sbh); | 
 | 427 | 	if (for_unformatted) | 
| Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 428 | 		dquot_free_block_nodirty(inode, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | } | 
 | 430 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 431 | void reiserfs_free_block(struct reiserfs_transaction_handle *th, | 
 | 432 | 			 struct inode *inode, b_blocknr_t block, | 
 | 433 | 			 int for_unformatted) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 435 | 	struct super_block *s = th->t_super; | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 436 | 	BUG_ON(!th->t_trans_id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 438 | 	RFALSE(!s, "vs-4061: trying to free block on nonexistent device"); | 
| Jeff Mahoney | d4c3d19 | 2007-10-18 23:39:24 -0700 | [diff] [blame] | 439 | 	if (!is_reusable(s, block, 1)) | 
 | 440 | 		return; | 
 | 441 |  | 
 | 442 | 	if (block > sb_block_count(REISERFS_SB(s)->s_rs)) { | 
| Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 443 | 		reiserfs_error(th->t_super, "bitmap-4072", | 
| Jeff Mahoney | d4c3d19 | 2007-10-18 23:39:24 -0700 | [diff] [blame] | 444 | 			       "Trying to free block outside file system " | 
 | 445 | 			       "boundaries (%lu > %lu)", | 
 | 446 | 			       block, sb_block_count(REISERFS_SB(s)->s_rs)); | 
 | 447 | 		return; | 
 | 448 | 	} | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 449 | 	/* mark it before we clear it, just in case */ | 
 | 450 | 	journal_mark_freed(th, s, block); | 
 | 451 | 	_reiserfs_free_block(th, inode, block, for_unformatted); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } | 
 | 453 |  | 
 | 454 | /* preallocated blocks don't need to be run through journal_mark_freed */ | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 455 | static void reiserfs_free_prealloc_block(struct reiserfs_transaction_handle *th, | 
 | 456 | 					 struct inode *inode, b_blocknr_t block) | 
 | 457 | { | 
| Jeff Mahoney | d4c3d19 | 2007-10-18 23:39:24 -0700 | [diff] [blame] | 458 | 	BUG_ON(!th->t_trans_id); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 459 | 	RFALSE(!th->t_super, | 
 | 460 | 	       "vs-4060: trying to free block on nonexistent device"); | 
| Jeff Mahoney | d4c3d19 | 2007-10-18 23:39:24 -0700 | [diff] [blame] | 461 | 	if (!is_reusable(th->t_super, block, 1)) | 
 | 462 | 		return; | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 463 | 	_reiserfs_free_block(th, inode, block, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } | 
 | 465 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 466 | static void __discard_prealloc(struct reiserfs_transaction_handle *th, | 
 | 467 | 			       struct reiserfs_inode_info *ei) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 469 | 	unsigned long save = ei->i_prealloc_block; | 
 | 470 | 	int dirty = 0; | 
 | 471 | 	struct inode *inode = &ei->vfs_inode; | 
 | 472 | 	BUG_ON(!th->t_trans_id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | #ifdef CONFIG_REISERFS_CHECK | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 474 | 	if (ei->i_prealloc_count < 0) | 
| Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 475 | 		reiserfs_error(th->t_super, "zam-4001", | 
 | 476 | 			       "inode has negative prealloc blocks count."); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | #endif | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 478 | 	while (ei->i_prealloc_count > 0) { | 
 | 479 | 		reiserfs_free_prealloc_block(th, inode, ei->i_prealloc_block); | 
 | 480 | 		ei->i_prealloc_block++; | 
 | 481 | 		ei->i_prealloc_count--; | 
 | 482 | 		dirty = 1; | 
 | 483 | 	} | 
 | 484 | 	if (dirty) | 
 | 485 | 		reiserfs_update_sd(th, inode); | 
 | 486 | 	ei->i_prealloc_block = save; | 
 | 487 | 	list_del_init(&(ei->i_prealloc_list)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } | 
 | 489 |  | 
 | 490 | /* FIXME: It should be inline function */ | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 491 | void reiserfs_discard_prealloc(struct reiserfs_transaction_handle *th, | 
 | 492 | 			       struct inode *inode) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 494 | 	struct reiserfs_inode_info *ei = REISERFS_I(inode); | 
 | 495 | 	BUG_ON(!th->t_trans_id); | 
 | 496 | 	if (ei->i_prealloc_count) | 
 | 497 | 		__discard_prealloc(th, ei); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } | 
 | 499 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 500 | void reiserfs_discard_all_prealloc(struct reiserfs_transaction_handle *th) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 502 | 	struct list_head *plist = &SB_JOURNAL(th->t_super)->j_prealloc_list; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 504 | 	BUG_ON(!th->t_trans_id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 506 | 	while (!list_empty(plist)) { | 
 | 507 | 		struct reiserfs_inode_info *ei; | 
 | 508 | 		ei = list_entry(plist->next, struct reiserfs_inode_info, | 
 | 509 | 				i_prealloc_list); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | #ifdef CONFIG_REISERFS_CHECK | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 511 | 		if (!ei->i_prealloc_count) { | 
| Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 512 | 			reiserfs_error(th->t_super, "zam-4001", | 
 | 513 | 				       "inode is in prealloc list but has " | 
 | 514 | 				       "no preallocated blocks."); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 515 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | #endif | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 517 | 		__discard_prealloc(th, ei); | 
 | 518 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } | 
 | 520 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 521 | void reiserfs_init_alloc_options(struct super_block *s) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 523 | 	set_bit(_ALLOC_skip_busy, &SB_ALLOC_OPTS(s)); | 
 | 524 | 	set_bit(_ALLOC_dirid_groups, &SB_ALLOC_OPTS(s)); | 
 | 525 | 	set_bit(_ALLOC_packing_groups, &SB_ALLOC_OPTS(s)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } | 
 | 527 |  | 
 | 528 | /* block allocator related options are parsed here */ | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 529 | int reiserfs_parse_alloc_options(struct super_block *s, char *options) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 531 | 	char *this_char, *value; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 533 | 	REISERFS_SB(s)->s_alloc_options.bits = 0;	/* clear default settings */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 535 | 	while ((this_char = strsep(&options, ":")) != NULL) { | 
 | 536 | 		if ((value = strchr(this_char, '=')) != NULL) | 
 | 537 | 			*value++ = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 539 | 		if (!strcmp(this_char, "concentrating_formatted_nodes")) { | 
 | 540 | 			int temp; | 
 | 541 | 			SET_OPTION(concentrating_formatted_nodes); | 
 | 542 | 			temp = (value | 
 | 543 | 				&& *value) ? simple_strtoul(value, &value, | 
 | 544 | 							    0) : 10; | 
 | 545 | 			if (temp <= 0 || temp > 100) { | 
 | 546 | 				REISERFS_SB(s)->s_alloc_options.border = 10; | 
 | 547 | 			} else { | 
 | 548 | 				REISERFS_SB(s)->s_alloc_options.border = | 
 | 549 | 				    100 / temp; | 
 | 550 | 			} | 
 | 551 | 			continue; | 
 | 552 | 		} | 
 | 553 | 		if (!strcmp(this_char, "displacing_large_files")) { | 
 | 554 | 			SET_OPTION(displacing_large_files); | 
 | 555 | 			REISERFS_SB(s)->s_alloc_options.large_file_size = | 
 | 556 | 			    (value | 
 | 557 | 			     && *value) ? simple_strtoul(value, &value, 0) : 16; | 
 | 558 | 			continue; | 
 | 559 | 		} | 
 | 560 | 		if (!strcmp(this_char, "displacing_new_packing_localities")) { | 
 | 561 | 			SET_OPTION(displacing_new_packing_localities); | 
 | 562 | 			continue; | 
 | 563 | 		}; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 565 | 		if (!strcmp(this_char, "old_hashed_relocation")) { | 
 | 566 | 			SET_OPTION(old_hashed_relocation); | 
 | 567 | 			continue; | 
 | 568 | 		} | 
 | 569 |  | 
 | 570 | 		if (!strcmp(this_char, "new_hashed_relocation")) { | 
 | 571 | 			SET_OPTION(new_hashed_relocation); | 
 | 572 | 			continue; | 
 | 573 | 		} | 
 | 574 |  | 
 | 575 | 		if (!strcmp(this_char, "dirid_groups")) { | 
 | 576 | 			SET_OPTION(dirid_groups); | 
 | 577 | 			continue; | 
 | 578 | 		} | 
 | 579 | 		if (!strcmp(this_char, "oid_groups")) { | 
 | 580 | 			SET_OPTION(oid_groups); | 
 | 581 | 			continue; | 
 | 582 | 		} | 
 | 583 | 		if (!strcmp(this_char, "packing_groups")) { | 
 | 584 | 			SET_OPTION(packing_groups); | 
 | 585 | 			continue; | 
 | 586 | 		} | 
 | 587 | 		if (!strcmp(this_char, "hashed_formatted_nodes")) { | 
 | 588 | 			SET_OPTION(hashed_formatted_nodes); | 
 | 589 | 			continue; | 
 | 590 | 		} | 
 | 591 |  | 
 | 592 | 		if (!strcmp(this_char, "skip_busy")) { | 
 | 593 | 			SET_OPTION(skip_busy); | 
 | 594 | 			continue; | 
 | 595 | 		} | 
 | 596 |  | 
 | 597 | 		if (!strcmp(this_char, "hundredth_slices")) { | 
 | 598 | 			SET_OPTION(hundredth_slices); | 
 | 599 | 			continue; | 
 | 600 | 		} | 
 | 601 |  | 
 | 602 | 		if (!strcmp(this_char, "old_way")) { | 
 | 603 | 			SET_OPTION(old_way); | 
 | 604 | 			continue; | 
 | 605 | 		} | 
 | 606 |  | 
 | 607 | 		if (!strcmp(this_char, "displace_based_on_dirid")) { | 
 | 608 | 			SET_OPTION(displace_based_on_dirid); | 
 | 609 | 			continue; | 
 | 610 | 		} | 
 | 611 |  | 
 | 612 | 		if (!strcmp(this_char, "preallocmin")) { | 
 | 613 | 			REISERFS_SB(s)->s_alloc_options.preallocmin = | 
 | 614 | 			    (value | 
 | 615 | 			     && *value) ? simple_strtoul(value, &value, 0) : 4; | 
 | 616 | 			continue; | 
 | 617 | 		} | 
 | 618 |  | 
 | 619 | 		if (!strcmp(this_char, "preallocsize")) { | 
 | 620 | 			REISERFS_SB(s)->s_alloc_options.preallocsize = | 
 | 621 | 			    (value | 
 | 622 | 			     && *value) ? simple_strtoul(value, &value, | 
 | 623 | 							 0) : | 
 | 624 | 			    PREALLOCATION_SIZE; | 
 | 625 | 			continue; | 
 | 626 | 		} | 
 | 627 |  | 
| Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 628 | 		reiserfs_warning(s, "zam-4001", "unknown option - %s", | 
 | 629 | 				 this_char); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 630 | 		return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | 	} | 
 | 632 |  | 
| Jeff Mahoney | 1d889d9 | 2009-03-30 14:02:20 -0400 | [diff] [blame] | 633 | 	reiserfs_info(s, "allocator options = [%08x]\n", SB_ALLOC_OPTS(s)); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 634 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 637 | static inline void new_hashed_relocation(reiserfs_blocknr_hint_t * hint) | 
 | 638 | { | 
 | 639 | 	char *hash_in; | 
 | 640 | 	if (hint->formatted_node) { | 
 | 641 | 		hash_in = (char *)&hint->key.k_dir_id; | 
 | 642 | 	} else { | 
 | 643 | 		if (!hint->inode) { | 
 | 644 | 			//hint->search_start = hint->beg; | 
 | 645 | 			hash_in = (char *)&hint->key.k_dir_id; | 
 | 646 | 		} else | 
 | 647 | 		    if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super)) | 
 | 648 | 			hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id); | 
 | 649 | 		else | 
 | 650 | 			hash_in = | 
 | 651 | 			    (char *)(&INODE_PKEY(hint->inode)->k_objectid); | 
 | 652 | 	} | 
 | 653 |  | 
 | 654 | 	hint->search_start = | 
 | 655 | 	    hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | } | 
 | 657 |  | 
 | 658 | /* | 
 | 659 |  * Relocation based on dirid, hashing them into a given bitmap block | 
| Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 660 |  * files. Formatted nodes are unaffected, a separate policy covers them | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 |  */ | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 662 | static void dirid_groups(reiserfs_blocknr_hint_t * hint) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 664 | 	unsigned long hash; | 
 | 665 | 	__u32 dirid = 0; | 
 | 666 | 	int bm = 0; | 
 | 667 | 	struct super_block *sb = hint->th->t_super; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | 	if (hint->inode) | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 669 | 		dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id); | 
 | 670 | 	else if (hint->formatted_node) | 
 | 671 | 		dirid = hint->key.k_dir_id; | 
 | 672 |  | 
 | 673 | 	if (dirid) { | 
 | 674 | 		bm = bmap_hash_id(sb, dirid); | 
 | 675 | 		hash = bm * (sb->s_blocksize << 3); | 
 | 676 | 		/* give a portion of the block group to metadata */ | 
 | 677 | 		if (hint->inode) | 
 | 678 | 			hash += sb->s_blocksize / 2; | 
 | 679 | 		hint->search_start = hash; | 
 | 680 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } | 
 | 682 |  | 
 | 683 | /* | 
 | 684 |  * Relocation based on oid, hashing them into a given bitmap block | 
| Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 685 |  * files. Formatted nodes are unaffected, a separate policy covers them | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 |  */ | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 687 | static void oid_groups(reiserfs_blocknr_hint_t * hint) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 689 | 	if (hint->inode) { | 
 | 690 | 		unsigned long hash; | 
 | 691 | 		__u32 oid; | 
 | 692 | 		__u32 dirid; | 
 | 693 | 		int bm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 695 | 		dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 697 | 		/* keep the root dir and it's first set of subdirs close to | 
 | 698 | 		 * the start of the disk | 
 | 699 | 		 */ | 
 | 700 | 		if (dirid <= 2) | 
 | 701 | 			hash = (hint->inode->i_sb->s_blocksize << 3); | 
 | 702 | 		else { | 
 | 703 | 			oid = le32_to_cpu(INODE_PKEY(hint->inode)->k_objectid); | 
 | 704 | 			bm = bmap_hash_id(hint->inode->i_sb, oid); | 
 | 705 | 			hash = bm * (hint->inode->i_sb->s_blocksize << 3); | 
 | 706 | 		} | 
 | 707 | 		hint->search_start = hash; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | } | 
 | 710 |  | 
 | 711 | /* returns 1 if it finds an indirect item and gets valid hint info | 
 | 712 |  * from it, otherwise 0 | 
 | 713 |  */ | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 714 | static int get_left_neighbor(reiserfs_blocknr_hint_t * hint) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | { | 
| Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 716 | 	struct treepath *path; | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 717 | 	struct buffer_head *bh; | 
 | 718 | 	struct item_head *ih; | 
 | 719 | 	int pos_in_item; | 
 | 720 | 	__le32 *item; | 
 | 721 | 	int ret = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 723 | 	if (!hint->path)	/* reiserfs code can call this function w/o pointer to path | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | 				 * structure supplied; then we rely on supplied search_start */ | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 725 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 727 | 	path = hint->path; | 
 | 728 | 	bh = get_last_bh(path); | 
 | 729 | 	RFALSE(!bh, "green-4002: Illegal path specified to get_left_neighbor"); | 
 | 730 | 	ih = get_ih(path); | 
 | 731 | 	pos_in_item = path->pos_in_item; | 
 | 732 | 	item = get_item(path); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 734 | 	hint->search_start = bh->b_blocknr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 736 | 	if (!hint->formatted_node && is_indirect_le_ih(ih)) { | 
 | 737 | 		/* for indirect item: go to left and look for the first non-hole entry | 
 | 738 | 		   in the indirect item */ | 
 | 739 | 		if (pos_in_item == I_UNFM_NUM(ih)) | 
 | 740 | 			pos_in_item--; | 
 | 741 | //          pos_in_item = I_UNFM_NUM (ih) - 1; | 
 | 742 | 		while (pos_in_item >= 0) { | 
 | 743 | 			int t = get_block_num(item, pos_in_item); | 
 | 744 | 			if (t) { | 
 | 745 | 				hint->search_start = t; | 
 | 746 | 				ret = 1; | 
 | 747 | 				break; | 
 | 748 | 			} | 
 | 749 | 			pos_in_item--; | 
 | 750 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 753 | 	/* does result value fit into specified region? */ | 
 | 754 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | } | 
 | 756 |  | 
 | 757 | /* should be, if formatted node, then try to put on first part of the device | 
 | 758 |    specified as number of percent with mount option device, else try to put | 
 | 759 |    on last of device.  This is not to say it is good code to do so, | 
 | 760 |    but the effect should be measured.  */ | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 761 | static inline void set_border_in_hint(struct super_block *s, | 
 | 762 | 				      reiserfs_blocknr_hint_t * hint) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 764 | 	b_blocknr_t border = | 
 | 765 | 	    SB_BLOCK_COUNT(s) / REISERFS_SB(s)->s_alloc_options.border; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 767 | 	if (hint->formatted_node) | 
 | 768 | 		hint->end = border - 1; | 
 | 769 | 	else | 
 | 770 | 		hint->beg = border; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | } | 
 | 772 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 773 | static inline void displace_large_file(reiserfs_blocknr_hint_t * hint) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 775 | 	if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super)) | 
 | 776 | 		hint->search_start = | 
 | 777 | 		    hint->beg + | 
 | 778 | 		    keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_dir_id), | 
 | 779 | 			       4) % (hint->end - hint->beg); | 
 | 780 | 	else | 
 | 781 | 		hint->search_start = | 
 | 782 | 		    hint->beg + | 
 | 783 | 		    keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_objectid), | 
 | 784 | 			       4) % (hint->end - hint->beg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | } | 
 | 786 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 787 | static inline void hash_formatted_node(reiserfs_blocknr_hint_t * hint) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 789 | 	char *hash_in; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 791 | 	if (!hint->inode) | 
 | 792 | 		hash_in = (char *)&hint->key.k_dir_id; | 
 | 793 | 	else if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super)) | 
 | 794 | 		hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id); | 
 | 795 | 	else | 
 | 796 | 		hash_in = (char *)(&INODE_PKEY(hint->inode)->k_objectid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 798 | 	hint->search_start = | 
 | 799 | 	    hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } | 
 | 801 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 802 | static inline int | 
 | 803 | this_blocknr_allocation_would_make_it_a_large_file(reiserfs_blocknr_hint_t * | 
 | 804 | 						   hint) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 806 | 	return hint->block == | 
 | 807 | 	    REISERFS_SB(hint->th->t_super)->s_alloc_options.large_file_size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | } | 
 | 809 |  | 
 | 810 | #ifdef DISPLACE_NEW_PACKING_LOCALITIES | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 811 | static inline void displace_new_packing_locality(reiserfs_blocknr_hint_t * hint) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 813 | 	struct in_core_key *key = &hint->key; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 815 | 	hint->th->displace_new_blocks = 0; | 
 | 816 | 	hint->search_start = | 
 | 817 | 	    hint->beg + keyed_hash((char *)(&key->k_objectid), | 
 | 818 | 				   4) % (hint->end - hint->beg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 822 | static inline int old_hashed_relocation(reiserfs_blocknr_hint_t * hint) | 
 | 823 | { | 
 | 824 | 	b_blocknr_t border; | 
 | 825 | 	u32 hash_in; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 827 | 	if (hint->formatted_node || hint->inode == NULL) { | 
 | 828 | 		return 0; | 
 | 829 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 831 | 	hash_in = le32_to_cpu((INODE_PKEY(hint->inode))->k_dir_id); | 
 | 832 | 	border = | 
 | 833 | 	    hint->beg + (u32) keyed_hash(((char *)(&hash_in)), | 
 | 834 | 					 4) % (hint->end - hint->beg - 1); | 
 | 835 | 	if (border > hint->search_start) | 
 | 836 | 		hint->search_start = border; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 838 | 	return 1; | 
 | 839 | } | 
 | 840 |  | 
 | 841 | static inline int old_way(reiserfs_blocknr_hint_t * hint) | 
 | 842 | { | 
 | 843 | 	b_blocknr_t border; | 
 | 844 |  | 
 | 845 | 	if (hint->formatted_node || hint->inode == NULL) { | 
 | 846 | 		return 0; | 
 | 847 | 	} | 
 | 848 |  | 
 | 849 | 	border = | 
 | 850 | 	    hint->beg + | 
 | 851 | 	    le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id) % (hint->end - | 
 | 852 | 							      hint->beg); | 
 | 853 | 	if (border > hint->search_start) | 
 | 854 | 		hint->search_start = border; | 
 | 855 |  | 
 | 856 | 	return 1; | 
 | 857 | } | 
 | 858 |  | 
 | 859 | static inline void hundredth_slices(reiserfs_blocknr_hint_t * hint) | 
 | 860 | { | 
 | 861 | 	struct in_core_key *key = &hint->key; | 
 | 862 | 	b_blocknr_t slice_start; | 
 | 863 |  | 
 | 864 | 	slice_start = | 
 | 865 | 	    (keyed_hash((char *)(&key->k_dir_id), 4) % 100) * (hint->end / 100); | 
 | 866 | 	if (slice_start > hint->search_start | 
 | 867 | 	    || slice_start + (hint->end / 100) <= hint->search_start) { | 
 | 868 | 		hint->search_start = slice_start; | 
 | 869 | 	} | 
 | 870 | } | 
 | 871 |  | 
 | 872 | static void determine_search_start(reiserfs_blocknr_hint_t * hint, | 
 | 873 | 				   int amount_needed) | 
 | 874 | { | 
 | 875 | 	struct super_block *s = hint->th->t_super; | 
 | 876 | 	int unfm_hint; | 
 | 877 |  | 
 | 878 | 	hint->beg = 0; | 
 | 879 | 	hint->end = SB_BLOCK_COUNT(s) - 1; | 
 | 880 |  | 
 | 881 | 	/* This is former border algorithm. Now with tunable border offset */ | 
 | 882 | 	if (concentrating_formatted_nodes(s)) | 
 | 883 | 		set_border_in_hint(s, hint); | 
 | 884 |  | 
 | 885 | #ifdef DISPLACE_NEW_PACKING_LOCALITIES | 
 | 886 | 	/* whenever we create a new directory, we displace it.  At first we will | 
 | 887 | 	   hash for location, later we might look for a moderately empty place for | 
 | 888 | 	   it */ | 
 | 889 | 	if (displacing_new_packing_localities(s) | 
 | 890 | 	    && hint->th->displace_new_blocks) { | 
 | 891 | 		displace_new_packing_locality(hint); | 
 | 892 |  | 
 | 893 | 		/* we do not continue determine_search_start, | 
 | 894 | 		 * if new packing locality is being displaced */ | 
 | 895 | 		return; | 
 | 896 | 	} | 
 | 897 | #endif | 
 | 898 |  | 
 | 899 | 	/* all persons should feel encouraged to add more special cases here and | 
 | 900 | 	 * test them */ | 
 | 901 |  | 
 | 902 | 	if (displacing_large_files(s) && !hint->formatted_node | 
 | 903 | 	    && this_blocknr_allocation_would_make_it_a_large_file(hint)) { | 
 | 904 | 		displace_large_file(hint); | 
 | 905 | 		return; | 
 | 906 | 	} | 
 | 907 |  | 
 | 908 | 	/* if none of our special cases is relevant, use the left neighbor in the | 
 | 909 | 	   tree order of the new node we are allocating for */ | 
 | 910 | 	if (hint->formatted_node && TEST_OPTION(hashed_formatted_nodes, s)) { | 
 | 911 | 		hash_formatted_node(hint); | 
 | 912 | 		return; | 
 | 913 | 	} | 
 | 914 |  | 
 | 915 | 	unfm_hint = get_left_neighbor(hint); | 
 | 916 |  | 
 | 917 | 	/* Mimic old block allocator behaviour, that is if VFS allowed for preallocation, | 
 | 918 | 	   new blocks are displaced based on directory ID. Also, if suggested search_start | 
 | 919 | 	   is less than last preallocated block, we start searching from it, assuming that | 
 | 920 | 	   HDD dataflow is faster in forward direction */ | 
 | 921 | 	if (TEST_OPTION(old_way, s)) { | 
 | 922 | 		if (!hint->formatted_node) { | 
 | 923 | 			if (!reiserfs_hashed_relocation(s)) | 
 | 924 | 				old_way(hint); | 
 | 925 | 			else if (!reiserfs_no_unhashed_relocation(s)) | 
 | 926 | 				old_hashed_relocation(hint); | 
 | 927 |  | 
 | 928 | 			if (hint->inode | 
 | 929 | 			    && hint->search_start < | 
 | 930 | 			    REISERFS_I(hint->inode)->i_prealloc_block) | 
 | 931 | 				hint->search_start = | 
 | 932 | 				    REISERFS_I(hint->inode)->i_prealloc_block; | 
 | 933 | 		} | 
 | 934 | 		return; | 
 | 935 | 	} | 
 | 936 |  | 
 | 937 | 	/* This is an approach proposed by Hans */ | 
 | 938 | 	if (TEST_OPTION(hundredth_slices, s) | 
 | 939 | 	    && !(displacing_large_files(s) && !hint->formatted_node)) { | 
 | 940 | 		hundredth_slices(hint); | 
 | 941 | 		return; | 
 | 942 | 	} | 
 | 943 |  | 
 | 944 | 	/* old_hashed_relocation only works on unformatted */ | 
 | 945 | 	if (!unfm_hint && !hint->formatted_node && | 
 | 946 | 	    TEST_OPTION(old_hashed_relocation, s)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | 		old_hashed_relocation(hint); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 948 | 	} | 
 | 949 | 	/* new_hashed_relocation works with both formatted/unformatted nodes */ | 
 | 950 | 	if ((!unfm_hint || hint->formatted_node) && | 
 | 951 | 	    TEST_OPTION(new_hashed_relocation, s)) { | 
 | 952 | 		new_hashed_relocation(hint); | 
 | 953 | 	} | 
 | 954 | 	/* dirid grouping works only on unformatted nodes */ | 
 | 955 | 	if (!unfm_hint && !hint->formatted_node && TEST_OPTION(dirid_groups, s)) { | 
 | 956 | 		dirid_groups(hint); | 
 | 957 | 	} | 
 | 958 | #ifdef DISPLACE_NEW_PACKING_LOCALITIES | 
 | 959 | 	if (hint->formatted_node && TEST_OPTION(dirid_groups, s)) { | 
 | 960 | 		dirid_groups(hint); | 
 | 961 | 	} | 
 | 962 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 964 | 	/* oid grouping works only on unformatted nodes */ | 
 | 965 | 	if (!unfm_hint && !hint->formatted_node && TEST_OPTION(oid_groups, s)) { | 
 | 966 | 		oid_groups(hint); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | 	} | 
 | 968 | 	return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | } | 
 | 970 |  | 
 | 971 | static int determine_prealloc_size(reiserfs_blocknr_hint_t * hint) | 
 | 972 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 973 | 	/* make minimum size a mount option and benchmark both ways */ | 
 | 974 | 	/* we preallocate blocks only for regular files, specific size */ | 
 | 975 | 	/* benchmark preallocating always and see what happens */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 977 | 	hint->prealloc_size = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 979 | 	if (!hint->formatted_node && hint->preallocate) { | 
 | 980 | 		if (S_ISREG(hint->inode->i_mode) | 
 | 981 | 		    && hint->inode->i_size >= | 
 | 982 | 		    REISERFS_SB(hint->th->t_super)->s_alloc_options. | 
 | 983 | 		    preallocmin * hint->inode->i_sb->s_blocksize) | 
 | 984 | 			hint->prealloc_size = | 
 | 985 | 			    REISERFS_SB(hint->th->t_super)->s_alloc_options. | 
 | 986 | 			    preallocsize - 1; | 
 | 987 | 	} | 
 | 988 | 	return CARRY_ON; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | } | 
 | 990 |  | 
 | 991 | /* XXX I know it could be merged with upper-level function; | 
 | 992 |    but may be result function would be too complex. */ | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 993 | static inline int allocate_without_wrapping_disk(reiserfs_blocknr_hint_t * hint, | 
 | 994 | 						 b_blocknr_t * new_blocknrs, | 
 | 995 | 						 b_blocknr_t start, | 
 | 996 | 						 b_blocknr_t finish, int min, | 
 | 997 | 						 int amount_needed, | 
 | 998 | 						 int prealloc_size) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1000 | 	int rest = amount_needed; | 
 | 1001 | 	int nr_allocated; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1003 | 	while (rest > 0 && start <= finish) { | 
 | 1004 | 		nr_allocated = scan_bitmap(hint->th, &start, finish, min, | 
 | 1005 | 					   rest + prealloc_size, | 
 | 1006 | 					   !hint->formatted_node, hint->block); | 
 | 1007 |  | 
 | 1008 | 		if (nr_allocated == 0)	/* no new blocks allocated, return */ | 
 | 1009 | 			break; | 
 | 1010 |  | 
 | 1011 | 		/* fill free_blocknrs array first */ | 
 | 1012 | 		while (rest > 0 && nr_allocated > 0) { | 
 | 1013 | 			*new_blocknrs++ = start++; | 
 | 1014 | 			rest--; | 
 | 1015 | 			nr_allocated--; | 
 | 1016 | 		} | 
 | 1017 |  | 
 | 1018 | 		/* do we have something to fill prealloc. array also ? */ | 
 | 1019 | 		if (nr_allocated > 0) { | 
 | 1020 | 			/* it means prealloc_size was greater that 0 and we do preallocation */ | 
 | 1021 | 			list_add(&REISERFS_I(hint->inode)->i_prealloc_list, | 
 | 1022 | 				 &SB_JOURNAL(hint->th->t_super)-> | 
 | 1023 | 				 j_prealloc_list); | 
 | 1024 | 			REISERFS_I(hint->inode)->i_prealloc_block = start; | 
 | 1025 | 			REISERFS_I(hint->inode)->i_prealloc_count = | 
 | 1026 | 			    nr_allocated; | 
 | 1027 | 			break; | 
 | 1028 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | 	} | 
 | 1030 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1031 | 	return (amount_needed - rest); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | } | 
 | 1033 |  | 
 | 1034 | static inline int blocknrs_and_prealloc_arrays_from_search_start | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1035 |     (reiserfs_blocknr_hint_t * hint, b_blocknr_t * new_blocknrs, | 
 | 1036 |      int amount_needed) { | 
 | 1037 | 	struct super_block *s = hint->th->t_super; | 
 | 1038 | 	b_blocknr_t start = hint->search_start; | 
 | 1039 | 	b_blocknr_t finish = SB_BLOCK_COUNT(s) - 1; | 
 | 1040 | 	int passno = 0; | 
 | 1041 | 	int nr_allocated = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1043 | 	determine_prealloc_size(hint); | 
 | 1044 | 	if (!hint->formatted_node) { | 
 | 1045 | 		int quota_ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | #ifdef REISERQUOTA_DEBUG | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1047 | 		reiserfs_debug(s, REISERFS_DEBUG_CODE, | 
 | 1048 | 			       "reiserquota: allocating %d blocks id=%u", | 
 | 1049 | 			       amount_needed, hint->inode->i_uid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | #endif | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1051 | 		quota_ret = | 
| Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 1052 | 		    dquot_alloc_block_nodirty(hint->inode, amount_needed); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1053 | 		if (quota_ret)	/* Quota exceeded? */ | 
 | 1054 | 			return QUOTA_EXCEEDED; | 
 | 1055 | 		if (hint->preallocate && hint->prealloc_size) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | #ifdef REISERQUOTA_DEBUG | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1057 | 			reiserfs_debug(s, REISERFS_DEBUG_CODE, | 
 | 1058 | 				       "reiserquota: allocating (prealloc) %d blocks id=%u", | 
 | 1059 | 				       hint->prealloc_size, hint->inode->i_uid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | #endif | 
| Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 1061 | 			quota_ret = dquot_prealloc_block_nodirty(hint->inode, | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1062 | 							 hint->prealloc_size); | 
 | 1063 | 			if (quota_ret) | 
 | 1064 | 				hint->preallocate = hint->prealloc_size = 0; | 
 | 1065 | 		} | 
 | 1066 | 		/* for unformatted nodes, force large allocations */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1069 | 	do { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1070 | 		switch (passno++) { | 
 | 1071 | 		case 0:	/* Search from hint->search_start to end of disk */ | 
 | 1072 | 			start = hint->search_start; | 
 | 1073 | 			finish = SB_BLOCK_COUNT(s) - 1; | 
 | 1074 | 			break; | 
 | 1075 | 		case 1:	/* Search from hint->beg to hint->search_start */ | 
 | 1076 | 			start = hint->beg; | 
 | 1077 | 			finish = hint->search_start; | 
 | 1078 | 			break; | 
 | 1079 | 		case 2:	/* Last chance: Search from 0 to hint->beg */ | 
 | 1080 | 			start = 0; | 
 | 1081 | 			finish = hint->beg; | 
 | 1082 | 			break; | 
 | 1083 | 		default:	/* We've tried searching everywhere, not enough space */ | 
 | 1084 | 			/* Free the blocks */ | 
 | 1085 | 			if (!hint->formatted_node) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | #ifdef REISERQUOTA_DEBUG | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1087 | 				reiserfs_debug(s, REISERFS_DEBUG_CODE, | 
 | 1088 | 					       "reiserquota: freeing (nospace) %d blocks id=%u", | 
 | 1089 | 					       amount_needed + | 
 | 1090 | 					       hint->prealloc_size - | 
 | 1091 | 					       nr_allocated, | 
 | 1092 | 					       hint->inode->i_uid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | #endif | 
| Jan Kara | 77db4f2 | 2009-01-26 17:14:18 +0100 | [diff] [blame] | 1094 | 				/* Free not allocated blocks */ | 
| Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 1095 | 				dquot_free_block_nodirty(hint->inode, | 
| Jan Kara | 77db4f2 | 2009-01-26 17:14:18 +0100 | [diff] [blame] | 1096 | 					amount_needed + hint->prealloc_size - | 
 | 1097 | 					nr_allocated); | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1098 | 			} | 
 | 1099 | 			while (nr_allocated--) | 
 | 1100 | 				reiserfs_free_block(hint->th, hint->inode, | 
 | 1101 | 						    new_blocknrs[nr_allocated], | 
 | 1102 | 						    !hint->formatted_node); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1104 | 			return NO_DISK_SPACE; | 
 | 1105 | 		} | 
 | 1106 | 	} while ((nr_allocated += allocate_without_wrapping_disk(hint, | 
 | 1107 | 								 new_blocknrs + | 
 | 1108 | 								 nr_allocated, | 
 | 1109 | 								 start, finish, | 
| Jeff Mahoney | 9ea0f94 | 2006-09-30 23:28:45 -0700 | [diff] [blame] | 1110 | 								 1, | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1111 | 								 amount_needed - | 
 | 1112 | 								 nr_allocated, | 
 | 1113 | 								 hint-> | 
 | 1114 | 								 prealloc_size)) | 
 | 1115 | 		 < amount_needed); | 
 | 1116 | 	if (!hint->formatted_node && | 
 | 1117 | 	    amount_needed + hint->prealloc_size > | 
 | 1118 | 	    nr_allocated + REISERFS_I(hint->inode)->i_prealloc_count) { | 
 | 1119 | 		/* Some of preallocation blocks were not allocated */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | #ifdef REISERQUOTA_DEBUG | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1121 | 		reiserfs_debug(s, REISERFS_DEBUG_CODE, | 
 | 1122 | 			       "reiserquota: freeing (failed prealloc) %d blocks id=%u", | 
 | 1123 | 			       amount_needed + hint->prealloc_size - | 
 | 1124 | 			       nr_allocated - | 
 | 1125 | 			       REISERFS_I(hint->inode)->i_prealloc_count, | 
 | 1126 | 			       hint->inode->i_uid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | #endif | 
| Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 1128 | 		dquot_free_block_nodirty(hint->inode, amount_needed + | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1129 | 					 hint->prealloc_size - nr_allocated - | 
 | 1130 | 					 REISERFS_I(hint->inode)-> | 
 | 1131 | 					 i_prealloc_count); | 
 | 1132 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1134 | 	return CARRY_ON; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | } | 
 | 1136 |  | 
 | 1137 | /* grab new blocknrs from preallocated list */ | 
 | 1138 | /* return amount still needed after using them */ | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1139 | static int use_preallocated_list_if_available(reiserfs_blocknr_hint_t * hint, | 
 | 1140 | 					      b_blocknr_t * new_blocknrs, | 
 | 1141 | 					      int amount_needed) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1143 | 	struct inode *inode = hint->inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1145 | 	if (REISERFS_I(inode)->i_prealloc_count > 0) { | 
 | 1146 | 		while (amount_needed) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1148 | 			*new_blocknrs++ = REISERFS_I(inode)->i_prealloc_block++; | 
 | 1149 | 			REISERFS_I(inode)->i_prealloc_count--; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1151 | 			amount_needed--; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1153 | 			if (REISERFS_I(inode)->i_prealloc_count <= 0) { | 
 | 1154 | 				list_del(&REISERFS_I(inode)->i_prealloc_list); | 
 | 1155 | 				break; | 
 | 1156 | 			} | 
 | 1157 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | 	} | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1159 | 	/* return amount still needed after using preallocated blocks */ | 
 | 1160 | 	return amount_needed; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | } | 
 | 1162 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1163 | int reiserfs_allocate_blocknrs(reiserfs_blocknr_hint_t * hint, b_blocknr_t * new_blocknrs, int amount_needed, int reserved_by_us	/* Amount of blocks we have | 
 | 1164 | 																	   already reserved */ ) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | { | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1166 | 	int initial_amount_needed = amount_needed; | 
 | 1167 | 	int ret; | 
 | 1168 | 	struct super_block *s = hint->th->t_super; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1170 | 	/* Check if there is enough space, taking into account reserved space */ | 
 | 1171 | 	if (SB_FREE_BLOCKS(s) - REISERFS_SB(s)->reserved_blocks < | 
 | 1172 | 	    amount_needed - reserved_by_us) | 
 | 1173 | 		return NO_DISK_SPACE; | 
 | 1174 | 	/* should this be if !hint->inode &&  hint->preallocate? */ | 
 | 1175 | 	/* do you mean hint->formatted_node can be removed ? - Zam */ | 
 | 1176 | 	/* hint->formatted_node cannot be removed because we try to access | 
 | 1177 | 	   inode information here, and there is often no inode assotiated with | 
 | 1178 | 	   metadata allocations - green */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 |  | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1180 | 	if (!hint->formatted_node && hint->preallocate) { | 
 | 1181 | 		amount_needed = use_preallocated_list_if_available | 
 | 1182 | 		    (hint, new_blocknrs, amount_needed); | 
 | 1183 | 		if (amount_needed == 0)	/* all blocknrs we need we got from | 
 | 1184 | 					   prealloc. list */ | 
 | 1185 | 			return CARRY_ON; | 
 | 1186 | 		new_blocknrs += (initial_amount_needed - amount_needed); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | 	} | 
| Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1188 |  | 
 | 1189 | 	/* find search start and save it in hint structure */ | 
 | 1190 | 	determine_search_start(hint, amount_needed); | 
 | 1191 | 	if (hint->search_start >= SB_BLOCK_COUNT(s)) | 
 | 1192 | 		hint->search_start = SB_BLOCK_COUNT(s) - 1; | 
 | 1193 |  | 
 | 1194 | 	/* allocation itself; fill new_blocknrs and preallocation arrays */ | 
 | 1195 | 	ret = blocknrs_and_prealloc_arrays_from_search_start | 
 | 1196 | 	    (hint, new_blocknrs, amount_needed); | 
 | 1197 |  | 
 | 1198 | 	/* we used prealloc. list to fill (partially) new_blocknrs array. If final allocation fails we | 
 | 1199 | 	 * need to return blocks back to prealloc. list or just free them. -- Zam (I chose second | 
 | 1200 | 	 * variant) */ | 
 | 1201 |  | 
 | 1202 | 	if (ret != CARRY_ON) { | 
 | 1203 | 		while (amount_needed++ < initial_amount_needed) { | 
 | 1204 | 			reiserfs_free_block(hint->th, hint->inode, | 
 | 1205 | 					    *(--new_blocknrs), 1); | 
 | 1206 | 		} | 
 | 1207 | 	} | 
 | 1208 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | } | 
 | 1210 |  | 
| Jeff Mahoney | 6f01046 | 2006-09-30 23:28:43 -0700 | [diff] [blame] | 1211 | void reiserfs_cache_bitmap_metadata(struct super_block *sb, | 
 | 1212 |                                     struct buffer_head *bh, | 
 | 1213 |                                     struct reiserfs_bitmap_info *info) | 
 | 1214 | { | 
 | 1215 | 	unsigned long *cur = (unsigned long *)(bh->b_data + bh->b_size); | 
 | 1216 |  | 
| Jeff Mahoney | 4d20851 | 2007-10-18 23:39:26 -0700 | [diff] [blame] | 1217 | 	/* The first bit must ALWAYS be 1 */ | 
| Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 1218 | 	if (!reiserfs_test_le_bit(0, (unsigned long *)bh->b_data)) | 
 | 1219 | 		reiserfs_error(sb, "reiserfs-2025", "bitmap block %lu is " | 
 | 1220 | 			       "corrupted: first bit must be 1", bh->b_blocknr); | 
| Jeff Mahoney | 4d20851 | 2007-10-18 23:39:26 -0700 | [diff] [blame] | 1221 |  | 
 | 1222 | 	info->free_count = 0; | 
| Jeff Mahoney | 6f01046 | 2006-09-30 23:28:43 -0700 | [diff] [blame] | 1223 |  | 
 | 1224 | 	while (--cur >= (unsigned long *)bh->b_data) { | 
| Jeff Mahoney | 4d20851 | 2007-10-18 23:39:26 -0700 | [diff] [blame] | 1225 | 		int i; | 
| Jeff Mahoney | 6f01046 | 2006-09-30 23:28:43 -0700 | [diff] [blame] | 1226 |  | 
 | 1227 | 		/* 0 and ~0 are special, we can optimize for them */ | 
| Jeff Mahoney | 4d20851 | 2007-10-18 23:39:26 -0700 | [diff] [blame] | 1228 | 		if (*cur == 0) | 
| Jeff Mahoney | 6f01046 | 2006-09-30 23:28:43 -0700 | [diff] [blame] | 1229 | 			info->free_count += BITS_PER_LONG; | 
| Jeff Mahoney | 4d20851 | 2007-10-18 23:39:26 -0700 | [diff] [blame] | 1230 | 		else if (*cur != ~0L)	/* A mix, investigate */ | 
 | 1231 | 			for (i = BITS_PER_LONG - 1; i >= 0; i--) | 
 | 1232 | 				if (!reiserfs_test_le_bit(i, cur)) | 
| Jeff Mahoney | 6f01046 | 2006-09-30 23:28:43 -0700 | [diff] [blame] | 1233 | 					info->free_count++; | 
| Jeff Mahoney | 6f01046 | 2006-09-30 23:28:43 -0700 | [diff] [blame] | 1234 | 	} | 
| Jeff Mahoney | 6f01046 | 2006-09-30 23:28:43 -0700 | [diff] [blame] | 1235 | } | 
 | 1236 |  | 
 | 1237 | struct buffer_head *reiserfs_read_bitmap_block(struct super_block *sb, | 
 | 1238 |                                                unsigned int bitmap) | 
 | 1239 | { | 
 | 1240 | 	b_blocknr_t block = (sb->s_blocksize << 3) * bitmap; | 
| Jeff Mahoney | 5065227 | 2006-09-30 23:28:44 -0700 | [diff] [blame] | 1241 | 	struct reiserfs_bitmap_info *info = SB_AP_BITMAP(sb) + bitmap; | 
| Jeff Mahoney | 6f01046 | 2006-09-30 23:28:43 -0700 | [diff] [blame] | 1242 | 	struct buffer_head *bh; | 
 | 1243 |  | 
 | 1244 | 	/* Way old format filesystems had the bitmaps packed up front. | 
 | 1245 | 	 * I doubt there are any of these left, but just in case... */ | 
 | 1246 | 	if (unlikely(test_bit(REISERFS_OLD_FORMAT, | 
 | 1247 | 	                      &(REISERFS_SB(sb)->s_properties)))) | 
 | 1248 | 		block = REISERFS_SB(sb)->s_sbh->b_blocknr + 1 + bitmap; | 
 | 1249 | 	else if (bitmap == 0) | 
 | 1250 | 		block = (REISERFS_DISK_OFFSET_IN_BYTES >> sb->s_blocksize_bits) + 1; | 
 | 1251 |  | 
| Frederic Weisbecker | 4c5efac | 2009-05-01 01:44:57 +0200 | [diff] [blame] | 1252 | 	reiserfs_write_unlock(sb); | 
| Jeff Mahoney | 5065227 | 2006-09-30 23:28:44 -0700 | [diff] [blame] | 1253 | 	bh = sb_bread(sb, block); | 
| Frederic Weisbecker | 4c5efac | 2009-05-01 01:44:57 +0200 | [diff] [blame] | 1254 | 	reiserfs_write_lock(sb); | 
| Jeff Mahoney | 5065227 | 2006-09-30 23:28:44 -0700 | [diff] [blame] | 1255 | 	if (bh == NULL) | 
| Eric Eric Sesterhenn | 00079e0 | 2006-10-06 22:19:45 -0700 | [diff] [blame] | 1256 | 		reiserfs_warning(sb, "sh-2029: %s: bitmap block (#%u) " | 
| Harvey Harrison | fbe5498 | 2008-04-28 02:16:22 -0700 | [diff] [blame] | 1257 | 		                 "reading failed", __func__, block); | 
| Jeff Mahoney | 5065227 | 2006-09-30 23:28:44 -0700 | [diff] [blame] | 1258 | 	else { | 
 | 1259 | 		if (buffer_locked(bh)) { | 
 | 1260 | 			PROC_INFO_INC(sb, scan_bitmap.wait); | 
| Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1261 | 			reiserfs_write_unlock(sb); | 
| Jeff Mahoney | 5065227 | 2006-09-30 23:28:44 -0700 | [diff] [blame] | 1262 | 			__wait_on_buffer(bh); | 
| Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1263 | 			reiserfs_write_lock(sb); | 
| Jeff Mahoney | 5065227 | 2006-09-30 23:28:44 -0700 | [diff] [blame] | 1264 | 		} | 
 | 1265 | 		BUG_ON(!buffer_uptodate(bh)); | 
 | 1266 | 		BUG_ON(atomic_read(&bh->b_count) == 0); | 
 | 1267 |  | 
| Jeff Mahoney | 4d20851 | 2007-10-18 23:39:26 -0700 | [diff] [blame] | 1268 | 		if (info->free_count == UINT_MAX) | 
| Jeff Mahoney | 5065227 | 2006-09-30 23:28:44 -0700 | [diff] [blame] | 1269 | 			reiserfs_cache_bitmap_metadata(sb, bh, info); | 
 | 1270 | 	} | 
| Jeff Mahoney | 6f01046 | 2006-09-30 23:28:43 -0700 | [diff] [blame] | 1271 |  | 
 | 1272 | 	return bh; | 
 | 1273 | } | 
 | 1274 |  | 
 | 1275 | int reiserfs_init_bitmap_cache(struct super_block *sb) | 
 | 1276 | { | 
 | 1277 | 	struct reiserfs_bitmap_info *bitmap; | 
| Jeff Mahoney | cb680c1 | 2007-10-18 23:39:27 -0700 | [diff] [blame] | 1278 | 	unsigned int bmap_nr = reiserfs_bmap_count(sb); | 
| Jeff Mahoney | 6f01046 | 2006-09-30 23:28:43 -0700 | [diff] [blame] | 1279 |  | 
| Frederic Weisbecker | 500f5a0 | 2009-12-13 22:48:54 +0100 | [diff] [blame] | 1280 | 	/* Avoid lock recursion in fault case */ | 
 | 1281 | 	reiserfs_write_unlock(sb); | 
| Jeff Mahoney | cb680c1 | 2007-10-18 23:39:27 -0700 | [diff] [blame] | 1282 | 	bitmap = vmalloc(sizeof(*bitmap) * bmap_nr); | 
| Frederic Weisbecker | 500f5a0 | 2009-12-13 22:48:54 +0100 | [diff] [blame] | 1283 | 	reiserfs_write_lock(sb); | 
| Jeff Mahoney | 6f01046 | 2006-09-30 23:28:43 -0700 | [diff] [blame] | 1284 | 	if (bitmap == NULL) | 
 | 1285 | 		return -ENOMEM; | 
 | 1286 |  | 
| Jeff Mahoney | cb680c1 | 2007-10-18 23:39:27 -0700 | [diff] [blame] | 1287 | 	memset(bitmap, 0xff, sizeof(*bitmap) * bmap_nr); | 
| Jeff Mahoney | 6f01046 | 2006-09-30 23:28:43 -0700 | [diff] [blame] | 1288 |  | 
| Jeff Mahoney | 6f01046 | 2006-09-30 23:28:43 -0700 | [diff] [blame] | 1289 | 	SB_AP_BITMAP(sb) = bitmap; | 
 | 1290 |  | 
 | 1291 | 	return 0; | 
 | 1292 | } | 
| Jeff Mahoney | 5065227 | 2006-09-30 23:28:44 -0700 | [diff] [blame] | 1293 |  | 
 | 1294 | void reiserfs_free_bitmap_cache(struct super_block *sb) | 
 | 1295 | { | 
 | 1296 | 	if (SB_AP_BITMAP(sb)) { | 
 | 1297 | 		vfree(SB_AP_BITMAP(sb)); | 
 | 1298 | 		SB_AP_BITMAP(sb) = NULL; | 
 | 1299 | 	} | 
 | 1300 | } |