bitmap.c 39.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
 */
/* Reiserfs block (de)allocator, bitmap-based. */

#include <linux/time.h>
#include <linux/reiserfs_fs.h>
#include <linux/errno.h>
#include <linux/buffer_head.h>
#include <linux/kernel.h>
#include <linux/pagemap.h>
12
#include <linux/vmalloc.h>
L
Linus Torvalds 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <linux/reiserfs_fs_sb.h>
#include <linux/reiserfs_fs_i.h>
#include <linux/quotaops.h>

#define PREALLOCATION_SIZE 9

/* different reiserfs block allocator options */

#define SB_ALLOC_OPTS(s) (REISERFS_SB(s)->s_alloc_options.bits)

#define  _ALLOC_concentrating_formatted_nodes 0
#define  _ALLOC_displacing_large_files 1
#define  _ALLOC_displacing_new_packing_localities 2
#define  _ALLOC_old_hashed_relocation 3
#define  _ALLOC_new_hashed_relocation 4
#define  _ALLOC_skip_busy 5
#define  _ALLOC_displace_based_on_dirid 6
#define  _ALLOC_hashed_formatted_nodes 7
#define  _ALLOC_old_way 8
#define  _ALLOC_hundredth_slices 9
#define  _ALLOC_dirid_groups 10
#define  _ALLOC_oid_groups 11
#define  _ALLOC_packing_groups 12

#define  concentrating_formatted_nodes(s)	test_bit(_ALLOC_concentrating_formatted_nodes, &SB_ALLOC_OPTS(s))
#define  displacing_large_files(s)		test_bit(_ALLOC_displacing_large_files, &SB_ALLOC_OPTS(s))
#define  displacing_new_packing_localities(s)	test_bit(_ALLOC_displacing_new_packing_localities, &SB_ALLOC_OPTS(s))

#define SET_OPTION(optname) \
   do { \
        reiserfs_warning(s, "reiserfs: option \"%s\" is set", #optname); \
        set_bit(_ALLOC_ ## optname , &SB_ALLOC_OPTS(s)); \
    } while(0)
#define TEST_OPTION(optname, s) \
    test_bit(_ALLOC_ ## optname , &SB_ALLOC_OPTS(s))

49 50
static inline void get_bit_address(struct super_block *s,
				   b_blocknr_t block, int *bmap_nr, int *offset)
L
Linus Torvalds 已提交
51
{
52 53
	/* It is in the bitmap block number equal to the block
	 * number divided by the number of bits in a block. */
54
	*bmap_nr = block >> (s->s_blocksize_bits + 3);
55 56
	/* Within that bitmap block it is located at bit offset *offset. */
	*offset = block & ((s->s_blocksize << 3) - 1);
L
Linus Torvalds 已提交
57 58 59
}

#ifdef CONFIG_REISERFS_CHECK
60
int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value)
L
Linus Torvalds 已提交
61
{
62
	int bmap, offset;
63
	struct buffer_head *bh;
L
Linus Torvalds 已提交
64

65 66 67 68 69
	if (block == 0 || block >= SB_BLOCK_COUNT(s)) {
		reiserfs_warning(s,
				 "vs-4010: is_reusable: block number is out of range %lu (%u)",
				 block, SB_BLOCK_COUNT(s));
		return 0;
L
Linus Torvalds 已提交
70 71
	}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	get_bit_address(s, block, &bmap, &offset);

	/* Old format filesystem? Unlikely, but the bitmaps are all up front so
	 * we need to account for it. */
	if (unlikely(test_bit(REISERFS_OLD_FORMAT,
			      &(REISERFS_SB(s)->s_properties)))) {
		b_blocknr_t bmap1 = REISERFS_SB(s)->s_sbh->b_blocknr + 1;
		if (block >= bmap1 && block <= bmap1 + SB_BMAP_NR(s)) {
			reiserfs_warning(s, "vs: 4019: is_reusable: "
					 "bitmap block %lu(%u) can't be freed or reused",
					 block, SB_BMAP_NR(s));
			return 0;
		}
	} else {
		if (offset == 0) {
87 88 89 90 91
			reiserfs_warning(s, "vs: 4020: is_reusable: "
					 "bitmap block %lu(%u) can't be freed or reused",
					 block, SB_BMAP_NR(s));
			return 0;
		}
92
	}
L
Linus Torvalds 已提交
93

94
	if (bmap >= SB_BMAP_NR(s)) {
95 96
		reiserfs_warning(s,
				 "vs-4030: is_reusable: there is no so many bitmap blocks: "
97
				 "block=%lu, bitmap_nr=%d", block, bmap);
98 99
		return 0;
	}
L
Linus Torvalds 已提交
100

101 102 103 104 105
	bh = SB_AP_BITMAP(s)[bmap].bh;
	get_bh(bh);

	if ((bit_value == 0 && reiserfs_test_le_bit(offset, bh->b_data)) ||
	    (bit_value == 1 && reiserfs_test_le_bit(offset, bh->b_data) == 0)) {
106 107
		reiserfs_warning(s,
				 "vs-4040: is_reusable: corresponding bit of block %lu does not "
108
				 "match required value (bmap==%d, offset==%d) test_bit==%d",
109 110
				 block, bmap, offset,
				 reiserfs_test_le_bit(offset, bh->b_data));
111

112
		brelse(bh);
113 114
		return 0;
	}
115
	brelse(bh);
L
Linus Torvalds 已提交
116

117 118 119 120 121 122 123 124
	if (bit_value == 0 && block == SB_ROOT_BLOCK(s)) {
		reiserfs_warning(s,
				 "vs-4050: is_reusable: this is root block (%u), "
				 "it must be busy", SB_ROOT_BLOCK(s));
		return 0;
	}

	return 1;
L
Linus Torvalds 已提交
125
}
126
#endif				/* CONFIG_REISERFS_CHECK */
L
Linus Torvalds 已提交
127 128 129

/* searches in journal structures for a given block number (bmap, off). If block
   is found in reiserfs journal it suggests next free block candidate to test. */
130 131
static inline int is_block_in_journal(struct super_block *s, int bmap, int
				      off, int *next)
L
Linus Torvalds 已提交
132
{
133 134 135 136 137 138 139 140 141 142 143 144
	b_blocknr_t tmp;

	if (reiserfs_in_journal(s, bmap, off, 1, &tmp)) {
		if (tmp) {	/* hint supplied */
			*next = tmp;
			PROC_INFO_INC(s, scan_bitmap.in_journal_hint);
		} else {
			(*next) = off + 1;	/* inc offset to avoid looping. */
			PROC_INFO_INC(s, scan_bitmap.in_journal_nohint);
		}
		PROC_INFO_INC(s, scan_bitmap.retry);
		return 1;
L
Linus Torvalds 已提交
145
	}
146
	return 0;
L
Linus Torvalds 已提交
147 148 149 150
}

/* it searches for a window of zero bits with given minimum and maximum lengths in one bitmap
 * block; */
151 152 153
static int scan_bitmap_block(struct reiserfs_transaction_handle *th,
			     int bmap_n, int *beg, int boundary, int min,
			     int max, int unfm)
L
Linus Torvalds 已提交
154
{
155 156
	struct super_block *s = th->t_super;
	struct reiserfs_bitmap_info *bi = &SB_AP_BITMAP(s)[bmap_n];
157
	struct buffer_head *bh;
158 159
	int end, next;
	int org = *beg;
L
Linus Torvalds 已提交
160

161
	BUG_ON(!th->t_trans_id);
L
Linus Torvalds 已提交
162

163 164 165
	RFALSE(bmap_n >= SB_BMAP_NR(s), "Bitmap %d is out of range (0..%d)",
	       bmap_n, SB_BMAP_NR(s) - 1);
	PROC_INFO_INC(s, scan_bitmap.bmap);
L
Linus Torvalds 已提交
166 167 168 169 170
/* this is unclear and lacks comments, explain how journal bitmaps
   work here for the reader.  Convey a sense of the design here. What
   is a window? */
/* - I mean `a window of zero bits' as in description of this function - Zam. */

171 172 173 174 175
	if (!bi) {
		reiserfs_warning(s, "NULL bitmap info pointer for bitmap %d",
				 bmap_n);
		return 0;
	}
176 177 178 179
	bh = bi->bh;
	get_bh(bh);

	if (buffer_locked(bh)) {
180
		PROC_INFO_INC(s, scan_bitmap.wait);
181
		__wait_on_buffer(bh);
L
Linus Torvalds 已提交
182 183
	}

184 185
	while (1) {
	      cont:
186 187
		if (bi->free_count < min) {
			brelse(bh);
188
			return 0;	// No free blocks in this bitmap
189
		}
190 191 192

		/* search for a first zero bit -- beggining of a window */
		*beg = reiserfs_find_next_zero_le_bit
193
		    ((unsigned long *)(bh->b_data), boundary, *beg);
194 195 196

		if (*beg + min > boundary) {	/* search for a zero bit fails or the rest of bitmap block
						 * cannot contain a zero window of minimum size */
197
			brelse(bh);
198
			return 0;
L
Linus Torvalds 已提交
199 200
		}

201 202 203 204 205
		if (unfm && is_block_in_journal(s, bmap_n, *beg, beg))
			continue;
		/* first zero bit found; we check next bits */
		for (end = *beg + 1;; end++) {
			if (end >= *beg + max || end >= boundary
206
			    || reiserfs_test_le_bit(end, bh->b_data)) {
207 208 209 210 211 212 213 214
				next = end;
				break;
			}
			/* finding the other end of zero bit window requires looking into journal structures (in
			 * case of searching for free blocks for unformatted nodes) */
			if (unfm && is_block_in_journal(s, bmap_n, end, &next))
				break;
		}
L
Linus Torvalds 已提交
215

216 217 218 219
		/* now (*beg) points to beginning of zero bits window,
		 * (end) points to one bit after the window end */
		if (end - *beg >= min) {	/* it seems we have found window of proper size */
			int i;
220
			reiserfs_prepare_for_journal(s, bh, 1);
221 222 223 224
			/* try to set all blocks used checking are they still free */
			for (i = *beg; i < end; i++) {
				/* It seems that we should not check in journal again. */
				if (reiserfs_test_and_set_le_bit
225
				    (i, bh->b_data)) {
226 227 228 229 230 231 232 233 234 235 236
					/* bit was set by another process
					 * while we slept in prepare_for_journal() */
					PROC_INFO_INC(s, scan_bitmap.stolen);
					if (i >= *beg + min) {	/* we can continue with smaller set of allocated blocks,
								 * if length of this set is more or equal to `min' */
						end = i;
						break;
					}
					/* otherwise we clear all bit were set ... */
					while (--i >= *beg)
						reiserfs_test_and_clear_le_bit
237 238
						    (i, bh->b_data);
					reiserfs_restore_prepared_buffer(s, bh);
239 240 241 242 243 244
					*beg = org;
					/* ... and search again in current block from beginning */
					goto cont;
				}
			}
			bi->free_count -= (end - *beg);
245 246
			journal_mark_dirty(th, s, bh);
			brelse(bh);
247 248 249 250 251 252 253 254 255 256 257

			/* free block count calculation */
			reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
						     1);
			PUT_SB_FREE_BLOCKS(s, SB_FREE_BLOCKS(s) - (end - *beg));
			journal_mark_dirty(th, s, SB_BUFFER_WITH_SB(s));

			return end - (*beg);
		} else {
			*beg = next;
		}
L
Linus Torvalds 已提交
258 259 260
	}
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
static int bmap_hash_id(struct super_block *s, u32 id)
{
	char *hash_in = NULL;
	unsigned long hash;
	unsigned bm;

	if (id <= 2) {
		bm = 1;
	} else {
		hash_in = (char *)(&id);
		hash = keyed_hash(hash_in, 4);
		bm = hash % SB_BMAP_NR(s);
		if (!bm)
			bm = 1;
	}
	/* this can only be true when SB_BMAP_NR = 1 */
	if (bm >= SB_BMAP_NR(s))
		bm = 0;
	return bm;
L
Linus Torvalds 已提交
280 281 282 283 284 285
}

/*
 * hashes the id and then returns > 0 if the block group for the
 * corresponding hash is full
 */
286 287 288 289 290 291 292 293
static inline int block_group_used(struct super_block *s, u32 id)
{
	int bm;
	bm = bmap_hash_id(s, id);
	if (SB_AP_BITMAP(s)[bm].free_count > ((s->s_blocksize << 3) * 60 / 100)) {
		return 0;
	}
	return 1;
L
Linus Torvalds 已提交
294 295 296 297 298
}

/*
 * the packing is returned in disk byte order
 */
299
__le32 reiserfs_choose_packing(struct inode * dir)
300
{
301 302 303 304 305 306 307 308 309 310 311 312 313 314
	__le32 packing;
	if (TEST_OPTION(packing_groups, dir->i_sb)) {
		u32 parent_dir = le32_to_cpu(INODE_PKEY(dir)->k_dir_id);
		/*
		 * some versions of reiserfsck expect packing locality 1 to be
		 * special
		 */
		if (parent_dir == 1 || block_group_used(dir->i_sb, parent_dir))
			packing = INODE_PKEY(dir)->k_objectid;
		else
			packing = INODE_PKEY(dir)->k_dir_id;
	} else
		packing = INODE_PKEY(dir)->k_objectid;
	return packing;
L
Linus Torvalds 已提交
315
}
316

L
Linus Torvalds 已提交
317 318
/* Tries to find contiguous zero bit window (given size) in given region of
 * bitmap and place new blocks there. Returns number of allocated blocks. */
319 320 321
static int scan_bitmap(struct reiserfs_transaction_handle *th,
		       b_blocknr_t * start, b_blocknr_t finish,
		       int min, int max, int unfm, unsigned long file_block)
L
Linus Torvalds 已提交
322
{
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
	int nr_allocated = 0;
	struct super_block *s = th->t_super;
	/* find every bm and bmap and bmap_nr in this file, and change them all to bitmap_blocknr
	 * - Hans, it is not a block number - Zam. */

	int bm, off;
	int end_bm, end_off;
	int off_max = s->s_blocksize << 3;

	BUG_ON(!th->t_trans_id);

	PROC_INFO_INC(s, scan_bitmap.call);
	if (SB_FREE_BLOCKS(s) <= 0)
		return 0;	// No point in looking for more free blocks

	get_bit_address(s, *start, &bm, &off);
	get_bit_address(s, finish, &end_bm, &end_off);
	if (bm > SB_BMAP_NR(s))
		return 0;
	if (end_bm > SB_BMAP_NR(s))
		end_bm = SB_BMAP_NR(s);

	/* When the bitmap is more than 10% free, anyone can allocate.
	 * When it's less than 10% free, only files that already use the
	 * bitmap are allowed. Once we pass 80% full, this restriction
	 * is lifted.
	 *
	 * We do this so that files that grow later still have space close to
	 * their original allocation. This improves locality, and presumably
	 * performance as a result.
	 *
	 * This is only an allocation policy and does not make up for getting a
	 * bad hint. Decent hinting must be implemented for this to work well.
	 */
	if (TEST_OPTION(skip_busy, s)
	    && SB_FREE_BLOCKS(s) > SB_BLOCK_COUNT(s) / 20) {
		for (; bm < end_bm; bm++, off = 0) {
			if ((off && (!unfm || (file_block != 0)))
			    || SB_AP_BITMAP(s)[bm].free_count >
			    (s->s_blocksize << 3) / 10)
				nr_allocated =
				    scan_bitmap_block(th, bm, &off, off_max,
						      min, max, unfm);
			if (nr_allocated)
				goto ret;
		}
		/* we know from above that start is a reasonable number */
		get_bit_address(s, *start, &bm, &off);
	}

	for (; bm < end_bm; bm++, off = 0) {
		nr_allocated =
		    scan_bitmap_block(th, bm, &off, off_max, min, max, unfm);
		if (nr_allocated)
			goto ret;
	}

	nr_allocated =
	    scan_bitmap_block(th, bm, &off, end_off + 1, min, max, unfm);

      ret:
	*start = bm * off_max + off;
	return nr_allocated;
L
Linus Torvalds 已提交
386 387 388

}

389 390 391
static void _reiserfs_free_block(struct reiserfs_transaction_handle *th,
				 struct inode *inode, b_blocknr_t block,
				 int for_unformatted)
L
Linus Torvalds 已提交
392
{
393 394
	struct super_block *s = th->t_super;
	struct reiserfs_super_block *rs;
395
	struct buffer_head *sbh, *bmbh;
396 397
	struct reiserfs_bitmap_info *apbi;
	int nr, offset;
L
Linus Torvalds 已提交
398

399
	BUG_ON(!th->t_trans_id);
L
Linus Torvalds 已提交
400

401
	PROC_INFO_INC(s, free_block);
L
Linus Torvalds 已提交
402

403 404 405
	rs = SB_DISK_SUPER_BLOCK(s);
	sbh = SB_BUFFER_WITH_SB(s);
	apbi = SB_AP_BITMAP(s);
L
Linus Torvalds 已提交
406

407
	get_bit_address(s, block, &nr, &offset);
L
Linus Torvalds 已提交
408

409 410 411 412 413 414 415
	if (nr >= sb_bmap_nr(rs)) {
		reiserfs_warning(s, "vs-4075: reiserfs_free_block: "
				 "block %lu is out of range on %s",
				 block, reiserfs_bdevname(s));
		return;
	}

416 417 418 419
	bmbh = apbi[nr].bh;
	get_bh(bmbh);

	reiserfs_prepare_for_journal(s, bmbh, 1);
420 421

	/* clear bit for the given block in bit map */
422
	if (!reiserfs_test_and_clear_le_bit(offset, bmbh->b_data)) {
423 424 425 426 427
		reiserfs_warning(s, "vs-4080: reiserfs_free_block: "
				 "free_block (%s:%lu)[dev:blocknr]: bit already cleared",
				 reiserfs_bdevname(s), block);
	}
	apbi[nr].free_count++;
428 429
	journal_mark_dirty(th, s, bmbh);
	brelse(bmbh);
430 431 432 433 434 435 436 437

	reiserfs_prepare_for_journal(s, sbh, 1);
	/* update super block */
	set_sb_free_blocks(rs, sb_free_blocks(rs) + 1);

	journal_mark_dirty(th, s, sbh);
	if (for_unformatted)
		DQUOT_FREE_BLOCK_NODIRTY(inode, 1);
L
Linus Torvalds 已提交
438 439
}

440 441 442
void reiserfs_free_block(struct reiserfs_transaction_handle *th,
			 struct inode *inode, b_blocknr_t block,
			 int for_unformatted)
L
Linus Torvalds 已提交
443
{
444
	struct super_block *s = th->t_super;
L
Linus Torvalds 已提交
445

446
	BUG_ON(!th->t_trans_id);
L
Linus Torvalds 已提交
447

448 449 450 451 452 453
	RFALSE(!s, "vs-4061: trying to free block on nonexistent device");
	RFALSE(is_reusable(s, block, 1) == 0,
	       "vs-4071: can not free such block");
	/* mark it before we clear it, just in case */
	journal_mark_freed(th, s, block);
	_reiserfs_free_block(th, inode, block, for_unformatted);
L
Linus Torvalds 已提交
454 455 456
}

/* preallocated blocks don't need to be run through journal_mark_freed */
457 458 459 460 461 462 463 464 465
static void reiserfs_free_prealloc_block(struct reiserfs_transaction_handle *th,
					 struct inode *inode, b_blocknr_t block)
{
	RFALSE(!th->t_super,
	       "vs-4060: trying to free block on nonexistent device");
	RFALSE(is_reusable(th->t_super, block, 1) == 0,
	       "vs-4070: can not free such block");
	BUG_ON(!th->t_trans_id);
	_reiserfs_free_block(th, inode, block, 1);
L
Linus Torvalds 已提交
466 467
}

468 469
static void __discard_prealloc(struct reiserfs_transaction_handle *th,
			       struct reiserfs_inode_info *ei)
L
Linus Torvalds 已提交
470
{
471 472 473 474
	unsigned long save = ei->i_prealloc_block;
	int dirty = 0;
	struct inode *inode = &ei->vfs_inode;
	BUG_ON(!th->t_trans_id);
L
Linus Torvalds 已提交
475
#ifdef CONFIG_REISERFS_CHECK
476 477 478 479
	if (ei->i_prealloc_count < 0)
		reiserfs_warning(th->t_super,
				 "zam-4001:%s: inode has negative prealloc blocks count.",
				 __FUNCTION__);
L
Linus Torvalds 已提交
480
#endif
481 482 483 484 485 486 487 488 489 490
	while (ei->i_prealloc_count > 0) {
		reiserfs_free_prealloc_block(th, inode, ei->i_prealloc_block);
		ei->i_prealloc_block++;
		ei->i_prealloc_count--;
		dirty = 1;
	}
	if (dirty)
		reiserfs_update_sd(th, inode);
	ei->i_prealloc_block = save;
	list_del_init(&(ei->i_prealloc_list));
L
Linus Torvalds 已提交
491 492 493
}

/* FIXME: It should be inline function */
494 495
void reiserfs_discard_prealloc(struct reiserfs_transaction_handle *th,
			       struct inode *inode)
L
Linus Torvalds 已提交
496
{
497 498 499 500
	struct reiserfs_inode_info *ei = REISERFS_I(inode);
	BUG_ON(!th->t_trans_id);
	if (ei->i_prealloc_count)
		__discard_prealloc(th, ei);
L
Linus Torvalds 已提交
501 502
}

503
void reiserfs_discard_all_prealloc(struct reiserfs_transaction_handle *th)
L
Linus Torvalds 已提交
504
{
505
	struct list_head *plist = &SB_JOURNAL(th->t_super)->j_prealloc_list;
L
Linus Torvalds 已提交
506

507
	BUG_ON(!th->t_trans_id);
L
Linus Torvalds 已提交
508

509 510 511 512
	while (!list_empty(plist)) {
		struct reiserfs_inode_info *ei;
		ei = list_entry(plist->next, struct reiserfs_inode_info,
				i_prealloc_list);
L
Linus Torvalds 已提交
513
#ifdef CONFIG_REISERFS_CHECK
514 515 516 517 518
		if (!ei->i_prealloc_count) {
			reiserfs_warning(th->t_super,
					 "zam-4001:%s: inode is in prealloc list but has no preallocated blocks.",
					 __FUNCTION__);
		}
L
Linus Torvalds 已提交
519
#endif
520 521
		__discard_prealloc(th, ei);
	}
L
Linus Torvalds 已提交
522 523
}

524
void reiserfs_init_alloc_options(struct super_block *s)
L
Linus Torvalds 已提交
525
{
526 527 528
	set_bit(_ALLOC_skip_busy, &SB_ALLOC_OPTS(s));
	set_bit(_ALLOC_dirid_groups, &SB_ALLOC_OPTS(s));
	set_bit(_ALLOC_packing_groups, &SB_ALLOC_OPTS(s));
L
Linus Torvalds 已提交
529 530 531
}

/* block allocator related options are parsed here */
532
int reiserfs_parse_alloc_options(struct super_block *s, char *options)
L
Linus Torvalds 已提交
533
{
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
	char *this_char, *value;

	REISERFS_SB(s)->s_alloc_options.bits = 0;	/* clear default settings */

	while ((this_char = strsep(&options, ":")) != NULL) {
		if ((value = strchr(this_char, '=')) != NULL)
			*value++ = 0;

		if (!strcmp(this_char, "concentrating_formatted_nodes")) {
			int temp;
			SET_OPTION(concentrating_formatted_nodes);
			temp = (value
				&& *value) ? simple_strtoul(value, &value,
							    0) : 10;
			if (temp <= 0 || temp > 100) {
				REISERFS_SB(s)->s_alloc_options.border = 10;
			} else {
				REISERFS_SB(s)->s_alloc_options.border =
				    100 / temp;
			}
			continue;
		}
		if (!strcmp(this_char, "displacing_large_files")) {
			SET_OPTION(displacing_large_files);
			REISERFS_SB(s)->s_alloc_options.large_file_size =
			    (value
			     && *value) ? simple_strtoul(value, &value, 0) : 16;
			continue;
		}
		if (!strcmp(this_char, "displacing_new_packing_localities")) {
			SET_OPTION(displacing_new_packing_localities);
			continue;
		};

		if (!strcmp(this_char, "old_hashed_relocation")) {
			SET_OPTION(old_hashed_relocation);
			continue;
		}
L
Linus Torvalds 已提交
572

573 574 575 576
		if (!strcmp(this_char, "new_hashed_relocation")) {
			SET_OPTION(new_hashed_relocation);
			continue;
		}
L
Linus Torvalds 已提交
577

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
		if (!strcmp(this_char, "dirid_groups")) {
			SET_OPTION(dirid_groups);
			continue;
		}
		if (!strcmp(this_char, "oid_groups")) {
			SET_OPTION(oid_groups);
			continue;
		}
		if (!strcmp(this_char, "packing_groups")) {
			SET_OPTION(packing_groups);
			continue;
		}
		if (!strcmp(this_char, "hashed_formatted_nodes")) {
			SET_OPTION(hashed_formatted_nodes);
			continue;
		}
L
Linus Torvalds 已提交
594

595 596 597 598
		if (!strcmp(this_char, "skip_busy")) {
			SET_OPTION(skip_busy);
			continue;
		}
L
Linus Torvalds 已提交
599

600 601 602 603
		if (!strcmp(this_char, "hundredth_slices")) {
			SET_OPTION(hundredth_slices);
			continue;
		}
L
Linus Torvalds 已提交
604

605 606 607 608
		if (!strcmp(this_char, "old_way")) {
			SET_OPTION(old_way);
			continue;
		}
L
Linus Torvalds 已提交
609

610 611 612 613
		if (!strcmp(this_char, "displace_based_on_dirid")) {
			SET_OPTION(displace_based_on_dirid);
			continue;
		}
L
Linus Torvalds 已提交
614

615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
		if (!strcmp(this_char, "preallocmin")) {
			REISERFS_SB(s)->s_alloc_options.preallocmin =
			    (value
			     && *value) ? simple_strtoul(value, &value, 0) : 4;
			continue;
		}

		if (!strcmp(this_char, "preallocsize")) {
			REISERFS_SB(s)->s_alloc_options.preallocsize =
			    (value
			     && *value) ? simple_strtoul(value, &value,
							 0) :
			    PREALLOCATION_SIZE;
			continue;
		}
L
Linus Torvalds 已提交
630

631 632 633
		reiserfs_warning(s, "zam-4001: %s : unknown option - %s",
				 __FUNCTION__, this_char);
		return 1;
L
Linus Torvalds 已提交
634 635
	}

636 637
	reiserfs_warning(s, "allocator options = [%08x]\n", SB_ALLOC_OPTS(s));
	return 0;
L
Linus Torvalds 已提交
638
}
639 640

static inline void new_hashed_relocation(reiserfs_blocknr_hint_t * hint)
L
Linus Torvalds 已提交
641
{
642 643 644 645 646 647 648 649 650 651 652 653 654 655
	char *hash_in;
	if (hint->formatted_node) {
		hash_in = (char *)&hint->key.k_dir_id;
	} else {
		if (!hint->inode) {
			//hint->search_start = hint->beg;
			hash_in = (char *)&hint->key.k_dir_id;
		} else
		    if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
			hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
		else
			hash_in =
			    (char *)(&INODE_PKEY(hint->inode)->k_objectid);
	}
L
Linus Torvalds 已提交
656

657 658
	hint->search_start =
	    hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
L
Linus Torvalds 已提交
659 660 661 662 663 664
}

/*
 * Relocation based on dirid, hashing them into a given bitmap block
 * files. Formatted nodes are unaffected, a seperate policy covers them
 */
665
static void dirid_groups(reiserfs_blocknr_hint_t * hint)
L
Linus Torvalds 已提交
666
{
667 668 669 670
	unsigned long hash;
	__u32 dirid = 0;
	int bm = 0;
	struct super_block *sb = hint->th->t_super;
L
Linus Torvalds 已提交
671
	if (hint->inode)
672 673 674 675 676 677 678 679 680 681 682 683
		dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
	else if (hint->formatted_node)
		dirid = hint->key.k_dir_id;

	if (dirid) {
		bm = bmap_hash_id(sb, dirid);
		hash = bm * (sb->s_blocksize << 3);
		/* give a portion of the block group to metadata */
		if (hint->inode)
			hash += sb->s_blocksize / 2;
		hint->search_start = hash;
	}
L
Linus Torvalds 已提交
684 685 686 687 688 689
}

/*
 * Relocation based on oid, hashing them into a given bitmap block
 * files. Formatted nodes are unaffected, a seperate policy covers them
 */
690
static void oid_groups(reiserfs_blocknr_hint_t * hint)
L
Linus Torvalds 已提交
691
{
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
	if (hint->inode) {
		unsigned long hash;
		__u32 oid;
		__u32 dirid;
		int bm;

		dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);

		/* keep the root dir and it's first set of subdirs close to
		 * the start of the disk
		 */
		if (dirid <= 2)
			hash = (hint->inode->i_sb->s_blocksize << 3);
		else {
			oid = le32_to_cpu(INODE_PKEY(hint->inode)->k_objectid);
			bm = bmap_hash_id(hint->inode->i_sb, oid);
			hash = bm * (hint->inode->i_sb->s_blocksize << 3);
		}
		hint->search_start = hash;
L
Linus Torvalds 已提交
711 712 713 714 715 716
	}
}

/* returns 1 if it finds an indirect item and gets valid hint info
 * from it, otherwise 0
 */
717
static int get_left_neighbor(reiserfs_blocknr_hint_t * hint)
L
Linus Torvalds 已提交
718
{
719 720 721 722 723 724 725 726
	struct path *path;
	struct buffer_head *bh;
	struct item_head *ih;
	int pos_in_item;
	__le32 *item;
	int ret = 0;

	if (!hint->path)	/* reiserfs code can call this function w/o pointer to path
L
Linus Torvalds 已提交
727
				 * structure supplied; then we rely on supplied search_start */
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
		return 0;

	path = hint->path;
	bh = get_last_bh(path);
	RFALSE(!bh, "green-4002: Illegal path specified to get_left_neighbor");
	ih = get_ih(path);
	pos_in_item = path->pos_in_item;
	item = get_item(path);

	hint->search_start = bh->b_blocknr;

	if (!hint->formatted_node && is_indirect_le_ih(ih)) {
		/* for indirect item: go to left and look for the first non-hole entry
		   in the indirect item */
		if (pos_in_item == I_UNFM_NUM(ih))
			pos_in_item--;
//          pos_in_item = I_UNFM_NUM (ih) - 1;
		while (pos_in_item >= 0) {
			int t = get_block_num(item, pos_in_item);
			if (t) {
				hint->search_start = t;
				ret = 1;
				break;
			}
			pos_in_item--;
		}
L
Linus Torvalds 已提交
754 755
	}

756 757
	/* does result value fit into specified region? */
	return ret;
L
Linus Torvalds 已提交
758 759 760 761 762 763
}

/* should be, if formatted node, then try to put on first part of the device
   specified as number of percent with mount option device, else try to put
   on last of device.  This is not to say it is good code to do so,
   but the effect should be measured.  */
764 765
static inline void set_border_in_hint(struct super_block *s,
				      reiserfs_blocknr_hint_t * hint)
L
Linus Torvalds 已提交
766
{
767 768
	b_blocknr_t border =
	    SB_BLOCK_COUNT(s) / REISERFS_SB(s)->s_alloc_options.border;
L
Linus Torvalds 已提交
769

770 771 772 773
	if (hint->formatted_node)
		hint->end = border - 1;
	else
		hint->beg = border;
L
Linus Torvalds 已提交
774 775
}

776
static inline void displace_large_file(reiserfs_blocknr_hint_t * hint)
L
Linus Torvalds 已提交
777
{
778 779 780 781 782 783 784 785 786 787
	if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
		hint->search_start =
		    hint->beg +
		    keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_dir_id),
			       4) % (hint->end - hint->beg);
	else
		hint->search_start =
		    hint->beg +
		    keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_objectid),
			       4) % (hint->end - hint->beg);
L
Linus Torvalds 已提交
788 789
}

790
static inline void hash_formatted_node(reiserfs_blocknr_hint_t * hint)
L
Linus Torvalds 已提交
791
{
792
	char *hash_in;
L
Linus Torvalds 已提交
793

794 795 796 797 798 799
	if (!hint->inode)
		hash_in = (char *)&hint->key.k_dir_id;
	else if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
		hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
	else
		hash_in = (char *)(&INODE_PKEY(hint->inode)->k_objectid);
L
Linus Torvalds 已提交
800

801 802
	hint->search_start =
	    hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
L
Linus Torvalds 已提交
803 804
}

805 806 807
static inline int
this_blocknr_allocation_would_make_it_a_large_file(reiserfs_blocknr_hint_t *
						   hint)
L
Linus Torvalds 已提交
808
{
809 810
	return hint->block ==
	    REISERFS_SB(hint->th->t_super)->s_alloc_options.large_file_size;
L
Linus Torvalds 已提交
811 812 813
}

#ifdef DISPLACE_NEW_PACKING_LOCALITIES
814
static inline void displace_new_packing_locality(reiserfs_blocknr_hint_t * hint)
L
Linus Torvalds 已提交
815
{
816
	struct in_core_key *key = &hint->key;
L
Linus Torvalds 已提交
817

818 819 820 821
	hint->th->displace_new_blocks = 0;
	hint->search_start =
	    hint->beg + keyed_hash((char *)(&key->k_objectid),
				   4) % (hint->end - hint->beg);
L
Linus Torvalds 已提交
822
}
823
#endif
L
Linus Torvalds 已提交
824

825
static inline int old_hashed_relocation(reiserfs_blocknr_hint_t * hint)
L
Linus Torvalds 已提交
826
{
827 828
	b_blocknr_t border;
	u32 hash_in;
L
Linus Torvalds 已提交
829

830 831 832
	if (hint->formatted_node || hint->inode == NULL) {
		return 0;
	}
L
Linus Torvalds 已提交
833

834 835 836 837 838 839
	hash_in = le32_to_cpu((INODE_PKEY(hint->inode))->k_dir_id);
	border =
	    hint->beg + (u32) keyed_hash(((char *)(&hash_in)),
					 4) % (hint->end - hint->beg - 1);
	if (border > hint->search_start)
		hint->search_start = border;
L
Linus Torvalds 已提交
840

841
	return 1;
L
Linus Torvalds 已提交
842 843
}

844
static inline int old_way(reiserfs_blocknr_hint_t * hint)
L
Linus Torvalds 已提交
845
{
846 847 848 849 850 851 852 853 854 855 856 857
	b_blocknr_t border;

	if (hint->formatted_node || hint->inode == NULL) {
		return 0;
	}

	border =
	    hint->beg +
	    le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id) % (hint->end -
							      hint->beg);
	if (border > hint->search_start)
		hint->search_start = border;
L
Linus Torvalds 已提交
858

859 860 861 862 863 864 865 866 867 868 869 870 871 872
	return 1;
}

static inline void hundredth_slices(reiserfs_blocknr_hint_t * hint)
{
	struct in_core_key *key = &hint->key;
	b_blocknr_t slice_start;

	slice_start =
	    (keyed_hash((char *)(&key->k_dir_id), 4) % 100) * (hint->end / 100);
	if (slice_start > hint->search_start
	    || slice_start + (hint->end / 100) <= hint->search_start) {
		hint->search_start = slice_start;
	}
L
Linus Torvalds 已提交
873
}
874 875 876

static void determine_search_start(reiserfs_blocknr_hint_t * hint,
				   int amount_needed)
L
Linus Torvalds 已提交
877
{
878 879
	struct super_block *s = hint->th->t_super;
	int unfm_hint;
L
Linus Torvalds 已提交
880

881 882
	hint->beg = 0;
	hint->end = SB_BLOCK_COUNT(s) - 1;
L
Linus Torvalds 已提交
883

884 885 886
	/* This is former border algorithm. Now with tunable border offset */
	if (concentrating_formatted_nodes(s))
		set_border_in_hint(s, hint);
L
Linus Torvalds 已提交
887 888

#ifdef DISPLACE_NEW_PACKING_LOCALITIES
889 890 891 892 893 894 895 896 897 898 899
	/* whenever we create a new directory, we displace it.  At first we will
	   hash for location, later we might look for a moderately empty place for
	   it */
	if (displacing_new_packing_localities(s)
	    && hint->th->displace_new_blocks) {
		displace_new_packing_locality(hint);

		/* we do not continue determine_search_start,
		 * if new packing locality is being displaced */
		return;
	}
L
Linus Torvalds 已提交
900 901
#endif

902 903
	/* all persons should feel encouraged to add more special cases here and
	 * test them */
L
Linus Torvalds 已提交
904

905 906 907 908 909
	if (displacing_large_files(s) && !hint->formatted_node
	    && this_blocknr_allocation_would_make_it_a_large_file(hint)) {
		displace_large_file(hint);
		return;
	}
L
Linus Torvalds 已提交
910

911 912 913 914 915 916
	/* if none of our special cases is relevant, use the left neighbor in the
	   tree order of the new node we are allocating for */
	if (hint->formatted_node && TEST_OPTION(hashed_formatted_nodes, s)) {
		hash_formatted_node(hint);
		return;
	}
L
Linus Torvalds 已提交
917

918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
	unfm_hint = get_left_neighbor(hint);

	/* Mimic old block allocator behaviour, that is if VFS allowed for preallocation,
	   new blocks are displaced based on directory ID. Also, if suggested search_start
	   is less than last preallocated block, we start searching from it, assuming that
	   HDD dataflow is faster in forward direction */
	if (TEST_OPTION(old_way, s)) {
		if (!hint->formatted_node) {
			if (!reiserfs_hashed_relocation(s))
				old_way(hint);
			else if (!reiserfs_no_unhashed_relocation(s))
				old_hashed_relocation(hint);

			if (hint->inode
			    && hint->search_start <
			    REISERFS_I(hint->inode)->i_prealloc_block)
				hint->search_start =
				    REISERFS_I(hint->inode)->i_prealloc_block;
		}
		return;
L
Linus Torvalds 已提交
938 939
	}

940 941 942 943 944 945
	/* This is an approach proposed by Hans */
	if (TEST_OPTION(hundredth_slices, s)
	    && !(displacing_large_files(s) && !hint->formatted_node)) {
		hundredth_slices(hint);
		return;
	}
L
Linus Torvalds 已提交
946

947 948 949 950 951 952 953 954 955 956 957 958 959 960
	/* old_hashed_relocation only works on unformatted */
	if (!unfm_hint && !hint->formatted_node &&
	    TEST_OPTION(old_hashed_relocation, s)) {
		old_hashed_relocation(hint);
	}
	/* new_hashed_relocation works with both formatted/unformatted nodes */
	if ((!unfm_hint || hint->formatted_node) &&
	    TEST_OPTION(new_hashed_relocation, s)) {
		new_hashed_relocation(hint);
	}
	/* dirid grouping works only on unformatted nodes */
	if (!unfm_hint && !hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
		dirid_groups(hint);
	}
L
Linus Torvalds 已提交
961
#ifdef DISPLACE_NEW_PACKING_LOCALITIES
962 963 964
	if (hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
		dirid_groups(hint);
	}
L
Linus Torvalds 已提交
965 966
#endif

967 968 969 970 971
	/* oid grouping works only on unformatted nodes */
	if (!unfm_hint && !hint->formatted_node && TEST_OPTION(oid_groups, s)) {
		oid_groups(hint);
	}
	return;
L
Linus Torvalds 已提交
972 973 974 975
}

static int determine_prealloc_size(reiserfs_blocknr_hint_t * hint)
{
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
	/* make minimum size a mount option and benchmark both ways */
	/* we preallocate blocks only for regular files, specific size */
	/* benchmark preallocating always and see what happens */

	hint->prealloc_size = 0;

	if (!hint->formatted_node && hint->preallocate) {
		if (S_ISREG(hint->inode->i_mode)
		    && hint->inode->i_size >=
		    REISERFS_SB(hint->th->t_super)->s_alloc_options.
		    preallocmin * hint->inode->i_sb->s_blocksize)
			hint->prealloc_size =
			    REISERFS_SB(hint->th->t_super)->s_alloc_options.
			    preallocsize - 1;
	}
	return CARRY_ON;
L
Linus Torvalds 已提交
992 993 994 995
}

/* XXX I know it could be merged with upper-level function;
   but may be result function would be too complex. */
996 997 998 999 1000 1001
static inline int allocate_without_wrapping_disk(reiserfs_blocknr_hint_t * hint,
						 b_blocknr_t * new_blocknrs,
						 b_blocknr_t start,
						 b_blocknr_t finish, int min,
						 int amount_needed,
						 int prealloc_size)
L
Linus Torvalds 已提交
1002
{
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
	int rest = amount_needed;
	int nr_allocated;

	while (rest > 0 && start <= finish) {
		nr_allocated = scan_bitmap(hint->th, &start, finish, min,
					   rest + prealloc_size,
					   !hint->formatted_node, hint->block);

		if (nr_allocated == 0)	/* no new blocks allocated, return */
			break;

		/* fill free_blocknrs array first */
		while (rest > 0 && nr_allocated > 0) {
			*new_blocknrs++ = start++;
			rest--;
			nr_allocated--;
		}
L
Linus Torvalds 已提交
1020

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
		/* do we have something to fill prealloc. array also ? */
		if (nr_allocated > 0) {
			/* it means prealloc_size was greater that 0 and we do preallocation */
			list_add(&REISERFS_I(hint->inode)->i_prealloc_list,
				 &SB_JOURNAL(hint->th->t_super)->
				 j_prealloc_list);
			REISERFS_I(hint->inode)->i_prealloc_block = start;
			REISERFS_I(hint->inode)->i_prealloc_count =
			    nr_allocated;
			break;
		}
L
Linus Torvalds 已提交
1032 1033
	}

1034
	return (amount_needed - rest);
L
Linus Torvalds 已提交
1035 1036 1037
}

static inline int blocknrs_and_prealloc_arrays_from_search_start
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
    (reiserfs_blocknr_hint_t * hint, b_blocknr_t * new_blocknrs,
     int amount_needed) {
	struct super_block *s = hint->th->t_super;
	b_blocknr_t start = hint->search_start;
	b_blocknr_t finish = SB_BLOCK_COUNT(s) - 1;
	int passno = 0;
	int nr_allocated = 0;
	int bigalloc = 0;

	determine_prealloc_size(hint);
	if (!hint->formatted_node) {
		int quota_ret;
L
Linus Torvalds 已提交
1050
#ifdef REISERQUOTA_DEBUG
1051 1052 1053
		reiserfs_debug(s, REISERFS_DEBUG_CODE,
			       "reiserquota: allocating %d blocks id=%u",
			       amount_needed, hint->inode->i_uid);
L
Linus Torvalds 已提交
1054
#endif
1055 1056 1057 1058 1059
		quota_ret =
		    DQUOT_ALLOC_BLOCK_NODIRTY(hint->inode, amount_needed);
		if (quota_ret)	/* Quota exceeded? */
			return QUOTA_EXCEEDED;
		if (hint->preallocate && hint->prealloc_size) {
L
Linus Torvalds 已提交
1060
#ifdef REISERQUOTA_DEBUG
1061 1062 1063
			reiserfs_debug(s, REISERFS_DEBUG_CODE,
				       "reiserquota: allocating (prealloc) %d blocks id=%u",
				       hint->prealloc_size, hint->inode->i_uid);
L
Linus Torvalds 已提交
1064
#endif
1065 1066 1067 1068 1069 1070 1071 1072
			quota_ret =
			    DQUOT_PREALLOC_BLOCK_NODIRTY(hint->inode,
							 hint->prealloc_size);
			if (quota_ret)
				hint->preallocate = hint->prealloc_size = 0;
		}
		/* for unformatted nodes, force large allocations */
		bigalloc = amount_needed;
L
Linus Torvalds 已提交
1073 1074
	}

1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
	do {
		/* in bigalloc mode, nr_allocated should stay zero until
		 * the entire allocation is filled
		 */
		if (unlikely(bigalloc && nr_allocated)) {
			reiserfs_warning(s, "bigalloc is %d, nr_allocated %d\n",
					 bigalloc, nr_allocated);
			/* reset things to a sane value */
			bigalloc = amount_needed - nr_allocated;
		}
		/*
		 * try pass 0 and pass 1 looking for a nice big
		 * contiguous allocation.  Then reset and look
		 * for anything you can find.
		 */
		if (passno == 2 && bigalloc) {
			passno = 0;
			bigalloc = 0;
		}
		switch (passno++) {
		case 0:	/* Search from hint->search_start to end of disk */
			start = hint->search_start;
			finish = SB_BLOCK_COUNT(s) - 1;
			break;
		case 1:	/* Search from hint->beg to hint->search_start */
			start = hint->beg;
			finish = hint->search_start;
			break;
		case 2:	/* Last chance: Search from 0 to hint->beg */
			start = 0;
			finish = hint->beg;
			break;
		default:	/* We've tried searching everywhere, not enough space */
			/* Free the blocks */
			if (!hint->formatted_node) {
L
Linus Torvalds 已提交
1110
#ifdef REISERQUOTA_DEBUG
1111 1112 1113 1114 1115 1116
				reiserfs_debug(s, REISERFS_DEBUG_CODE,
					       "reiserquota: freeing (nospace) %d blocks id=%u",
					       amount_needed +
					       hint->prealloc_size -
					       nr_allocated,
					       hint->inode->i_uid);
L
Linus Torvalds 已提交
1117
#endif
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
				DQUOT_FREE_BLOCK_NODIRTY(hint->inode, amount_needed + hint->prealloc_size - nr_allocated);	/* Free not allocated blocks */
			}
			while (nr_allocated--)
				reiserfs_free_block(hint->th, hint->inode,
						    new_blocknrs[nr_allocated],
						    !hint->formatted_node);

			return NO_DISK_SPACE;
		}
	} while ((nr_allocated += allocate_without_wrapping_disk(hint,
								 new_blocknrs +
								 nr_allocated,
								 start, finish,
								 bigalloc ?
								 bigalloc : 1,
								 amount_needed -
								 nr_allocated,
								 hint->
								 prealloc_size))
		 < amount_needed);
	if (!hint->formatted_node &&
	    amount_needed + hint->prealloc_size >
	    nr_allocated + REISERFS_I(hint->inode)->i_prealloc_count) {
		/* Some of preallocation blocks were not allocated */
L
Linus Torvalds 已提交
1142
#ifdef REISERQUOTA_DEBUG
1143 1144 1145 1146 1147 1148
		reiserfs_debug(s, REISERFS_DEBUG_CODE,
			       "reiserquota: freeing (failed prealloc) %d blocks id=%u",
			       amount_needed + hint->prealloc_size -
			       nr_allocated -
			       REISERFS_I(hint->inode)->i_prealloc_count,
			       hint->inode->i_uid);
L
Linus Torvalds 已提交
1149
#endif
1150 1151 1152 1153 1154
		DQUOT_FREE_BLOCK_NODIRTY(hint->inode, amount_needed +
					 hint->prealloc_size - nr_allocated -
					 REISERFS_I(hint->inode)->
					 i_prealloc_count);
	}
L
Linus Torvalds 已提交
1155

1156
	return CARRY_ON;
L
Linus Torvalds 已提交
1157 1158 1159 1160
}

/* grab new blocknrs from preallocated list */
/* return amount still needed after using them */
1161 1162 1163
static int use_preallocated_list_if_available(reiserfs_blocknr_hint_t * hint,
					      b_blocknr_t * new_blocknrs,
					      int amount_needed)
L
Linus Torvalds 已提交
1164
{
1165
	struct inode *inode = hint->inode;
L
Linus Torvalds 已提交
1166

1167 1168
	if (REISERFS_I(inode)->i_prealloc_count > 0) {
		while (amount_needed) {
L
Linus Torvalds 已提交
1169

1170 1171
			*new_blocknrs++ = REISERFS_I(inode)->i_prealloc_block++;
			REISERFS_I(inode)->i_prealloc_count--;
L
Linus Torvalds 已提交
1172

1173
			amount_needed--;
L
Linus Torvalds 已提交
1174

1175 1176 1177 1178 1179
			if (REISERFS_I(inode)->i_prealloc_count <= 0) {
				list_del(&REISERFS_I(inode)->i_prealloc_list);
				break;
			}
		}
L
Linus Torvalds 已提交
1180
	}
1181 1182
	/* return amount still needed after using preallocated blocks */
	return amount_needed;
L
Linus Torvalds 已提交
1183 1184
}

1185 1186
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
																	   already reserved */ )
L
Linus Torvalds 已提交
1187
{
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
	int initial_amount_needed = amount_needed;
	int ret;
	struct super_block *s = hint->th->t_super;

	/* Check if there is enough space, taking into account reserved space */
	if (SB_FREE_BLOCKS(s) - REISERFS_SB(s)->reserved_blocks <
	    amount_needed - reserved_by_us)
		return NO_DISK_SPACE;
	/* should this be if !hint->inode &&  hint->preallocate? */
	/* do you mean hint->formatted_node can be removed ? - Zam */
	/* hint->formatted_node cannot be removed because we try to access
	   inode information here, and there is often no inode assotiated with
	   metadata allocations - green */

	if (!hint->formatted_node && hint->preallocate) {
		amount_needed = use_preallocated_list_if_available
		    (hint, new_blocknrs, amount_needed);
		if (amount_needed == 0)	/* all blocknrs we need we got from
					   prealloc. list */
			return CARRY_ON;
		new_blocknrs += (initial_amount_needed - amount_needed);
	}

	/* find search start and save it in hint structure */
	determine_search_start(hint, amount_needed);
	if (hint->search_start >= SB_BLOCK_COUNT(s))
		hint->search_start = SB_BLOCK_COUNT(s) - 1;

	/* allocation itself; fill new_blocknrs and preallocation arrays */
	ret = blocknrs_and_prealloc_arrays_from_search_start
L
Linus Torvalds 已提交
1218
	    (hint, new_blocknrs, amount_needed);
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228

	/* we used prealloc. list to fill (partially) new_blocknrs array. If final allocation fails we
	 * need to return blocks back to prealloc. list or just free them. -- Zam (I chose second
	 * variant) */

	if (ret != CARRY_ON) {
		while (amount_needed++ < initial_amount_needed) {
			reiserfs_free_block(hint->th, hint->inode,
					    *(--new_blocknrs), 1);
		}
L
Linus Torvalds 已提交
1229
	}
1230
	return ret;
L
Linus Torvalds 已提交
1231 1232 1233 1234 1235
}

/* These 2 functions are here to provide blocks reservation to the rest of kernel */
/* Reserve @blocks amount of blocks in fs pointed by @sb. Caller must make sure
   there are actually this much blocks on the FS available */
1236 1237 1238 1239 1240 1241
void reiserfs_claim_blocks_to_be_allocated(struct super_block *sb,	/* super block of
									   filesystem where
									   blocks should be
									   reserved */
					   int blocks	/* How much to reserve */
    )
L
Linus Torvalds 已提交
1242 1243
{

1244 1245 1246
	/* Fast case, if reservation is zero - exit immediately. */
	if (!blocks)
		return;
L
Linus Torvalds 已提交
1247

1248 1249 1250
	spin_lock(&REISERFS_SB(sb)->bitmap_lock);
	REISERFS_SB(sb)->reserved_blocks += blocks;
	spin_unlock(&REISERFS_SB(sb)->bitmap_lock);
L
Linus Torvalds 已提交
1251 1252 1253
}

/* Unreserve @blocks amount of blocks in fs pointed by @sb */
1254 1255 1256 1257 1258 1259
void reiserfs_release_claimed_blocks(struct super_block *sb,	/* super block of
								   filesystem where
								   blocks should be
								   reserved */
				     int blocks	/* How much to unreserve */
    )
L
Linus Torvalds 已提交
1260 1261
{

1262 1263 1264
	/* Fast case, if unreservation is zero - exit immediately. */
	if (!blocks)
		return;
L
Linus Torvalds 已提交
1265

1266 1267 1268 1269 1270
	spin_lock(&REISERFS_SB(sb)->bitmap_lock);
	REISERFS_SB(sb)->reserved_blocks -= blocks;
	spin_unlock(&REISERFS_SB(sb)->bitmap_lock);
	RFALSE(REISERFS_SB(sb)->reserved_blocks < 0,
	       "amount of blocks reserved became zero?");
L
Linus Torvalds 已提交
1271 1272 1273 1274
}

/* This function estimates how much pages we will be able to write to FS
   used for reiserfs_file_write() purposes for now. */
1275 1276
int reiserfs_can_fit_pages(struct super_block *sb	/* superblock of filesystem
							   to estimate space */ )
L
Linus Torvalds 已提交
1277 1278 1279 1280
{
	int space;

	spin_lock(&REISERFS_SB(sb)->bitmap_lock);
1281 1282 1283 1284
	space =
	    (SB_FREE_BLOCKS(sb) -
	     REISERFS_SB(sb)->reserved_blocks) >> (PAGE_CACHE_SHIFT -
						   sb->s_blocksize_bits);
L
Linus Torvalds 已提交
1285 1286
	spin_unlock(&REISERFS_SB(sb)->bitmap_lock);

1287
	return space > 0 ? space : 0;
L
Linus Torvalds 已提交
1288
}
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375

void reiserfs_cache_bitmap_metadata(struct super_block *sb,
                                    struct buffer_head *bh,
                                    struct reiserfs_bitmap_info *info)
{
	unsigned long *cur = (unsigned long *)(bh->b_data + bh->b_size);

	info->first_zero_hint = 1 << (sb->s_blocksize_bits + 3);

	while (--cur >= (unsigned long *)bh->b_data) {
		int base = ((char *)cur - bh->b_data) << 3;

		/* 0 and ~0 are special, we can optimize for them */
		if (*cur == 0) {
			info->first_zero_hint = base;
			info->free_count += BITS_PER_LONG;
		} else if (*cur != ~0L) {       /* A mix, investigate */
			int b;
			for (b = BITS_PER_LONG - 1; b >= 0; b--) {
				if (!reiserfs_test_le_bit(b, cur)) {
					info->first_zero_hint = base + b;
					info->free_count++;
				}
			}
		}
	}
	/* The first bit must ALWAYS be 1 */
	BUG_ON(info->first_zero_hint == 0);
}

struct buffer_head *reiserfs_read_bitmap_block(struct super_block *sb,
                                               unsigned int bitmap)
{
	b_blocknr_t block = (sb->s_blocksize << 3) * bitmap;
	struct buffer_head *bh;

	/* Way old format filesystems had the bitmaps packed up front.
	 * I doubt there are any of these left, but just in case... */
	if (unlikely(test_bit(REISERFS_OLD_FORMAT,
	                      &(REISERFS_SB(sb)->s_properties))))
		block = REISERFS_SB(sb)->s_sbh->b_blocknr + 1 + bitmap;
	else if (bitmap == 0)
		block = (REISERFS_DISK_OFFSET_IN_BYTES >> sb->s_blocksize_bits) + 1;

	bh = sb_getblk(sb, block);
	if (!buffer_uptodate(bh))
		ll_rw_block(READ, 1, &bh);

	return bh;
}

int reiserfs_init_bitmap_cache(struct super_block *sb)
{
	struct reiserfs_bitmap_info *bitmap;
	int i;

	bitmap = vmalloc(sizeof (*bitmap) * SB_BMAP_NR(sb));
	if (bitmap == NULL)
		return -ENOMEM;

	memset(bitmap, 0, sizeof (*bitmap) * SB_BMAP_NR(sb));

	for (i = 0; i < SB_BMAP_NR(sb); i++)
		bitmap[i].bh = reiserfs_read_bitmap_block(sb, i);

	/* make sure we have them all */
	for (i = 0; i < SB_BMAP_NR(sb); i++) {
		wait_on_buffer(bitmap[i].bh);
		if (!buffer_uptodate(bitmap[i].bh)) {
			reiserfs_warning(sb, "sh-2029: %s: "
					 "bitmap block (#%lu) reading failed",
			                 __FUNCTION__, bitmap[i].bh->b_blocknr);
			for (i = 0; i < SB_BMAP_NR(sb); i++)
				brelse(bitmap[i].bh);
			vfree(bitmap);
			return -EIO;
		}
	}

	/* Cache the info on the bitmaps before we get rolling */
	for (i = 0; i < SB_BMAP_NR(sb); i++)
		reiserfs_cache_bitmap_metadata(sb, bitmap[i].bh, &bitmap[i]);

	SB_AP_BITMAP(sb) = bitmap;

	return 0;
}