suballoc.c 76.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * suballoc.c
 *
 * metadata alloc and free
 * Inspired by ext3 block groups.
 *
 * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

#include <linux/fs.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/highmem.h>

#include <cluster/masklog.h>

#include "ocfs2.h"

#include "alloc.h"
Joel Becker's avatar
Joel Becker committed
37
#include "blockcheck.h"
38 39 40 41 42 43 44 45
#include "dlmglue.h"
#include "inode.h"
#include "journal.h"
#include "localalloc.h"
#include "suballoc.h"
#include "super.h"
#include "sysfile.h"
#include "uptodate.h"
46
#include "ocfs2_trace.h"
47 48 49

#include "buffer_head_io.h"

50
#define NOT_ALLOC_NEW_GROUP		0
51 52
#define ALLOC_NEW_GROUP			0x1
#define ALLOC_GROUPS_FROM_GLOBAL	0x2
53

54
#define OCFS2_MAX_TO_STEAL		1024
55

56
struct ocfs2_suballoc_result {
57 58 59
	u64		sr_bg_blkno;	/* The bg we allocated from.  Set
					   to 0 when a block group is
					   contiguous. */
60 61 62 63 64 65
	u64		sr_bg_stable_blkno; /*
					     * Doesn't change, always
					     * set to target block
					     * group descriptor
					     * block.
					     */
66
	u64		sr_blkno;	/* The first allocated block */
67 68 69 70
	unsigned int	sr_bit_offset;	/* The bit in the bg */
	unsigned int	sr_bits;	/* How many bits we claimed */
};

71 72 73 74 75 76 77 78 79 80 81
static u64 ocfs2_group_from_res(struct ocfs2_suballoc_result *res)
{
	if (res->sr_blkno == 0)
		return 0;

	if (res->sr_bg_blkno)
		return res->sr_bg_blkno;

	return ocfs2_which_suballoc_group(res->sr_blkno, res->sr_bit_offset);
}

82 83 84
static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
85
static int ocfs2_block_group_fill(handle_t *handle,
86 87 88
				  struct inode *alloc_inode,
				  struct buffer_head *bg_bh,
				  u64 group_blkno,
89
				  unsigned int group_clusters,
90 91 92 93
				  u16 my_chain,
				  struct ocfs2_chain_list *cl);
static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
				   struct inode *alloc_inode,
94
				   struct buffer_head *bh,
95
				   u64 max_block,
96
				   u64 *last_alloc_group,
97
				   int flags);
98 99 100 101

static int ocfs2_cluster_group_search(struct inode *inode,
				      struct buffer_head *group_bh,
				      u32 bits_wanted, u32 min_bits,
102
				      u64 max_block,
103
				      struct ocfs2_suballoc_result *res);
104 105 106
static int ocfs2_block_group_search(struct inode *inode,
				    struct buffer_head *group_bh,
				    u32 bits_wanted, u32 min_bits,
107
				    u64 max_block,
108
				    struct ocfs2_suballoc_result *res);
109
static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
110
				     handle_t *handle,
111 112
				     u32 bits_wanted,
				     u32 min_bits,
113
				     struct ocfs2_suballoc_result *res);
114 115
static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
					 int nr);
116
static int ocfs2_relink_block_group(handle_t *handle,
117 118 119 120 121 122 123 124 125 126 127 128 129 130
				    struct inode *alloc_inode,
				    struct buffer_head *fe_bh,
				    struct buffer_head *bg_bh,
				    struct buffer_head *prev_bg_bh,
				    u16 chain);
static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
						     u32 wanted);
static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
						   u64 bg_blkno,
						   u16 bg_bit_off);
static inline void ocfs2_block_to_cluster_group(struct inode *inode,
						u64 data_blkno,
						u64 *bg_blkno,
						u16 *bg_bit_off);
131 132
static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
					     u32 bits_wanted, u64 max_block,
133
					     int flags,
134
					     struct ocfs2_alloc_context **ac);
135

136
void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
137
{
138 139 140 141
	struct inode *inode = ac->ac_inode;

	if (inode) {
		if (ac->ac_which != OCFS2_AC_USE_LOCAL)
142
			ocfs2_inode_unlock(inode, 1);
143 144 145 146

		mutex_unlock(&inode->i_mutex);

		iput(inode);
147
		ac->ac_inode = NULL;
148
	}
149 150
	brelse(ac->ac_bh);
	ac->ac_bh = NULL;
151
	ac->ac_resv = NULL;
152 153
	kfree(ac->ac_find_loc_priv);
	ac->ac_find_loc_priv = NULL;
154 155 156 157 158
}

void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
{
	ocfs2_free_ac_resource(ac);
159 160 161 162 163 164 165 166
	kfree(ac);
}

static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
{
	return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
}

167
#define do_error(fmt, ...)						\
168 169 170 171 172 173
do {									\
	if (resize)							\
		mlog(ML_ERROR, fmt, ##__VA_ARGS__);			\
	else								\
		return ocfs2_error(sb, fmt, ##__VA_ARGS__);		\
} while (0)
174

175 176
static int ocfs2_validate_gd_self(struct super_block *sb,
				  struct buffer_head *bh,
177
				  int resize)
178 179 180
{
	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;

181
	if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
182
		do_error("Group descriptor #%llu has bad signature %.*s\n",
183
			 (unsigned long long)bh->b_blocknr, 7,
184
			 gd->bg_signature);
185 186
	}

187
	if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
188
		do_error("Group descriptor #%llu has an invalid bg_blkno of %llu\n",
189 190 191 192 193
			 (unsigned long long)bh->b_blocknr,
			 (unsigned long long)le64_to_cpu(gd->bg_blkno));
	}

	if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
194
		do_error("Group descriptor #%llu has an invalid fs_generation of #%u\n",
195 196 197 198
			 (unsigned long long)bh->b_blocknr,
			 le32_to_cpu(gd->bg_generation));
	}

199
	if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
200
		do_error("Group descriptor #%llu has bit count %u but claims that %u are free\n",
201 202 203 204 205 206
			 (unsigned long long)bh->b_blocknr,
			 le16_to_cpu(gd->bg_bits),
			 le16_to_cpu(gd->bg_free_bits_count));
	}

	if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
207
		do_error("Group descriptor #%llu has bit count %u but max bitmap bits of %u\n",
208 209 210 211 212 213 214 215 216 217 218
			 (unsigned long long)bh->b_blocknr,
			 le16_to_cpu(gd->bg_bits),
			 8 * le16_to_cpu(gd->bg_size));
	}

	return 0;
}

static int ocfs2_validate_gd_parent(struct super_block *sb,
				    struct ocfs2_dinode *di,
				    struct buffer_head *bh,
219
				    int resize)
220 221 222 223
{
	unsigned int max_bits;
	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;

224
	if (di->i_blkno != gd->bg_parent_dinode) {
225
		do_error("Group descriptor #%llu has bad parent pointer (%llu, expected %llu)\n",
226
			 (unsigned long long)bh->b_blocknr,
227 228
			 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
			 (unsigned long long)le64_to_cpu(di->i_blkno));
229 230 231 232
	}

	max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
	if (le16_to_cpu(gd->bg_bits) > max_bits) {
233
		do_error("Group descriptor #%llu has bit count of %u\n",
234
			 (unsigned long long)bh->b_blocknr,
235
			 le16_to_cpu(gd->bg_bits));
236 237
	}

238 239 240 241 242
	/* In resize, we may meet the case bg_chain == cl_next_free_rec. */
	if ((le16_to_cpu(gd->bg_chain) >
	     le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
	    ((le16_to_cpu(gd->bg_chain) ==
	     le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
243
		do_error("Group descriptor #%llu has bad chain %u\n",
244
			 (unsigned long long)bh->b_blocknr,
245
			 le16_to_cpu(gd->bg_chain));
246 247
	}

248 249
	return 0;
}
250

251
#undef do_error
252

253 254 255 256 257 258 259 260 261
/*
 * This version only prints errors.  It does not fail the filesystem, and
 * exists only for resize.
 */
int ocfs2_check_group_descriptor(struct super_block *sb,
				 struct ocfs2_dinode *di,
				 struct buffer_head *bh)
{
	int rc;
Joel Becker's avatar
Joel Becker committed
262 263 264
	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;

	BUG_ON(!buffer_uptodate(bh));
265

Joel Becker's avatar
Joel Becker committed
266 267 268 269 270 271
	/*
	 * If the ecc fails, we return the error but otherwise
	 * leave the filesystem running.  We know any error is
	 * local to this block.
	 */
	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
272 273 274 275 276
	if (rc) {
		mlog(ML_ERROR,
		     "Checksum failed for group descriptor %llu\n",
		     (unsigned long long)bh->b_blocknr);
	} else
Joel Becker's avatar
Joel Becker committed
277
		rc = ocfs2_validate_gd_self(sb, bh, 1);
278 279 280 281 282 283 284 285 286
	if (!rc)
		rc = ocfs2_validate_gd_parent(sb, di, bh, 1);

	return rc;
}

static int ocfs2_validate_group_descriptor(struct super_block *sb,
					   struct buffer_head *bh)
{
Joel Becker's avatar
Joel Becker committed
287 288 289
	int rc;
	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;

290 291
	trace_ocfs2_validate_group_descriptor(
					(unsigned long long)bh->b_blocknr);
292

Joel Becker's avatar
Joel Becker committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	BUG_ON(!buffer_uptodate(bh));

	/*
	 * If the ecc fails, we return the error but otherwise
	 * leave the filesystem running.  We know any error is
	 * local to this block.
	 */
	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
	if (rc)
		return rc;

	/*
	 * Errors after here are fatal.
	 */

308
	return ocfs2_validate_gd_self(sb, bh, 0);
309 310
}

311 312 313 314 315 316
int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
				u64 gd_blkno, struct buffer_head **bh)
{
	int rc;
	struct buffer_head *tmp = *bh;

317
	rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
318
			      ocfs2_validate_group_descriptor);
319 320 321
	if (rc)
		goto out;

322
	rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
323 324 325 326 327 328 329 330 331 332 333 334 335
	if (rc) {
		brelse(tmp);
		goto out;
	}

	/* If ocfs2_read_block() got us a new bh, pass it up. */
	if (!*bh)
		*bh = tmp;

out:
	return rc;
}

336 337 338
static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
					  struct ocfs2_group_desc *bg,
					  struct ocfs2_chain_list *cl,
339
					  u64 p_blkno, unsigned int clusters)
340 341 342 343
{
	struct ocfs2_extent_list *el = &bg->bg_list;
	struct ocfs2_extent_rec *rec;

344
	BUG_ON(!ocfs2_supports_discontig_bg(osb));
345 346 347
	if (!el->l_next_free_rec)
		el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
	rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
348
	rec->e_blkno = cpu_to_le64(p_blkno);
349 350
	rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
				  le16_to_cpu(cl->cl_bpc));
351
	rec->e_leaf_clusters = cpu_to_le16(clusters);
352
	le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
353 354
	le16_add_cpu(&bg->bg_free_bits_count,
		     clusters * le16_to_cpu(cl->cl_bpc));
355 356 357
	le16_add_cpu(&el->l_next_free_rec, 1);
}

358
static int ocfs2_block_group_fill(handle_t *handle,
359 360 361
				  struct inode *alloc_inode,
				  struct buffer_head *bg_bh,
				  u64 group_blkno,
362
				  unsigned int group_clusters,
363 364 365 366
				  u16 my_chain,
				  struct ocfs2_chain_list *cl)
{
	int status = 0;
367
	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
368 369 370 371
	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
	struct super_block * sb = alloc_inode->i_sb;

	if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
372 373 374 375
		status = ocfs2_error(alloc_inode->i_sb,
				     "group block (%llu) != b_blocknr (%llu)\n",
				     (unsigned long long)group_blkno,
				     (unsigned long long) bg_bh->b_blocknr);
376 377 378
		goto bail;
	}

379
	status = ocfs2_journal_access_gd(handle,
380
					 INODE_CACHE(alloc_inode),
381 382
					 bg_bh,
					 OCFS2_JOURNAL_ACCESS_CREATE);
383 384 385 386 387 388 389 390
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}

	memset(bg, 0, sb->s_blocksize);
	strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
	bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
391 392
	bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1,
						osb->s_feature_incompat));
393 394 395 396
	bg->bg_chain = cpu_to_le16(my_chain);
	bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
	bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
	bg->bg_blkno = cpu_to_le64(group_blkno);
397 398 399
	if (group_clusters == le16_to_cpu(cl->cl_cpg))
		bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
	else
400
		ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno,
401 402
					      group_clusters);

403 404 405 406
	/* set the 1st bit in the bitmap to account for the descriptor block */
	ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
	bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);

407
	ocfs2_journal_dirty(handle, bg_bh);
408 409 410 411 412 413 414

	/* There is no need to zero out or otherwise initialize the
	 * other blocks in a group - All valid FS metadata in a block
	 * group stores the superblock fs_generation value at
	 * allocation time. */

bail:
Tao Ma's avatar
Tao Ma committed
415 416
	if (status)
		mlog_errno(status);
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	return status;
}

static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
{
	u16 curr, best;

	best = curr = 0;
	while (curr < le16_to_cpu(cl->cl_count)) {
		if (le32_to_cpu(cl->cl_recs[best].c_total) >
		    le32_to_cpu(cl->cl_recs[curr].c_total))
			best = curr;
		curr++;
	}
	return best;
}

434 435 436 437 438 439 440 441 442 443 444 445
static struct buffer_head *
ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
			       struct inode *alloc_inode,
			       struct ocfs2_alloc_context *ac,
			       struct ocfs2_chain_list *cl)
{
	int status;
	u32 bit_off, num_bits;
	u64 bg_blkno;
	struct buffer_head *bg_bh;
	unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);

446
	status = ocfs2_claim_clusters(handle, ac,
447 448 449 450 451 452 453 454 455 456
				      le16_to_cpu(cl->cl_cpg), &bit_off,
				      &num_bits);
	if (status < 0) {
		if (status != -ENOSPC)
			mlog_errno(status);
		goto bail;
	}

	/* setup the group */
	bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
457 458
	trace_ocfs2_block_group_alloc_contig(
	     (unsigned long long)bg_blkno, alloc_rec);
459 460 461

	bg_bh = sb_getblk(osb->sb, bg_blkno);
	if (!bg_bh) {
462
		status = -ENOMEM;
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
		mlog_errno(status);
		goto bail;
	}
	ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);

	status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
					bg_blkno, num_bits, alloc_rec, cl);
	if (status < 0) {
		brelse(bg_bh);
		mlog_errno(status);
	}

bail:
	return status ? ERR_PTR(status) : bg_bh;
}

static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
					handle_t *handle,
					struct ocfs2_alloc_context *ac,
					unsigned int min_bits,
					u32 *bit_off, u32 *num_bits)
{
Joel Becker's avatar
Joel Becker committed
485
	int status = 0;
486 487

	while (min_bits) {
488
		status = ocfs2_claim_clusters(handle, ac, min_bits,
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
					      bit_off, num_bits);
		if (status != -ENOSPC)
			break;

		min_bits >>= 1;
	}

	return status;
}

static int ocfs2_block_group_grow_discontig(handle_t *handle,
					    struct inode *alloc_inode,
					    struct buffer_head *bg_bh,
					    struct ocfs2_alloc_context *ac,
					    struct ocfs2_chain_list *cl,
					    unsigned int min_bits)
{
	int status;
	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
	struct ocfs2_group_desc *bg =
		(struct ocfs2_group_desc *)bg_bh->b_data;
510 511
	unsigned int needed = le16_to_cpu(cl->cl_cpg) -
			 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
	u32 p_cpos, clusters;
	u64 p_blkno;
	struct ocfs2_extent_list *el = &bg->bg_list;

	status = ocfs2_journal_access_gd(handle,
					 INODE_CACHE(alloc_inode),
					 bg_bh,
					 OCFS2_JOURNAL_ACCESS_CREATE);
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}

	while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
				le16_to_cpu(el->l_count))) {
		if (min_bits > needed)
			min_bits = needed;
		status = ocfs2_block_group_claim_bits(osb, handle, ac,
						      min_bits, &p_cpos,
						      &clusters);
		if (status < 0) {
			if (status != -ENOSPC)
				mlog_errno(status);
			goto bail;
		}
		p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
		ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
					      clusters);

		min_bits = clusters;
542 543
		needed = le16_to_cpu(cl->cl_cpg) -
			 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
544 545 546
	}

	if (needed > 0) {
547 548 549 550 551 552
		/*
		 * We have used up all the extent rec but can't fill up
		 * the cpg. So bail out.
		 */
		status = -ENOSPC;
		goto bail;
553 554 555 556 557 558 559 560
	}

	ocfs2_journal_dirty(handle, bg_bh);

bail:
	return status;
}

561 562 563 564
static void ocfs2_bg_alloc_cleanup(handle_t *handle,
				   struct ocfs2_alloc_context *cluster_ac,
				   struct inode *alloc_inode,
				   struct buffer_head *bg_bh)
565
{
566
	int i, ret;
567 568 569 570 571 572 573 574 575 576 577
	struct ocfs2_group_desc *bg;
	struct ocfs2_extent_list *el;
	struct ocfs2_extent_rec *rec;

	if (!bg_bh)
		return;

	bg = (struct ocfs2_group_desc *)bg_bh->b_data;
	el = &bg->bg_list;
	for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
		rec = &el->l_recs[i];
578 579 580
		ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode,
					  cluster_ac->ac_bh,
					  le64_to_cpu(rec->e_blkno),
581
					  le16_to_cpu(rec->e_leaf_clusters));
582 583 584
		if (ret)
			mlog_errno(ret);
		/* Try all the clusters to free */
585 586 587 588 589 590 591 592 593 594
	}

	ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
	brelse(bg_bh);
}

static struct buffer_head *
ocfs2_block_group_alloc_discontig(handle_t *handle,
				  struct inode *alloc_inode,
				  struct ocfs2_alloc_context *ac,
595
				  struct ocfs2_chain_list *cl)
596 597 598 599 600 601 602 603 604
{
	int status;
	u32 bit_off, num_bits;
	u64 bg_blkno;
	unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
	struct buffer_head *bg_bh = NULL;
	unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);

605
	if (!ocfs2_supports_discontig_bg(osb)) {
606 607 608 609
		status = -ENOSPC;
		goto bail;
	}

610 611 612 613 614 615 616
	status = ocfs2_extend_trans(handle,
				    ocfs2_calc_bg_discontig_credits(osb->sb));
	if (status) {
		mlog_errno(status);
		goto bail;
	}

617 618 619 620 621 622
	/*
	 * We're going to be grabbing from multiple cluster groups.
	 * We don't have enough credits to relink them all, and the
	 * cluster groups will be staying in cache for the duration of
	 * this operation.
	 */
623
	ac->ac_disable_chain_relink = 1;
624

625 626 627 628 629 630 631 632 633 634 635 636
	/* Claim the first region */
	status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
					      &bit_off, &num_bits);
	if (status < 0) {
		if (status != -ENOSPC)
			mlog_errno(status);
		goto bail;
	}
	min_bits = num_bits;

	/* setup the group */
	bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
637 638
	trace_ocfs2_block_group_alloc_discontig(
				(unsigned long long)bg_blkno, alloc_rec);
639 640 641

	bg_bh = sb_getblk(osb->sb, bg_blkno);
	if (!bg_bh) {
642
		status = -ENOMEM;
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
		mlog_errno(status);
		goto bail;
	}
	ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);

	status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
					bg_blkno, num_bits, alloc_rec, cl);
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}

	status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
						  bg_bh, ac, cl, min_bits);
	if (status)
		mlog_errno(status);

bail:
	if (status)
662
		ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh);
663 664 665
	return status ? ERR_PTR(status) : bg_bh;
}

666 667 668 669 670
/*
 * We expect the block group allocator to already be locked.
 */
static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
				   struct inode *alloc_inode,
671
				   struct buffer_head *bh,
672
				   u64 max_block,
673
				   u64 *last_alloc_group,
674
				   int flags)
675 676 677 678 679
{
	int status, credits;
	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
	struct ocfs2_chain_list *cl;
	struct ocfs2_alloc_context *ac = NULL;
680
	handle_t *handle = NULL;
681
	u16 alloc_rec;
682 683 684 685 686 687
	struct buffer_head *bg_bh = NULL;
	struct ocfs2_group_desc *bg;

	BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));

	cl = &fe->id2.i_chain;
688 689
	status = ocfs2_reserve_clusters_with_limit(osb,
						   le16_to_cpu(cl->cl_cpg),
690
						   max_block, flags, &ac);
691 692 693 694 695 696 697 698
	if (status < 0) {
		if (status != -ENOSPC)
			mlog_errno(status);
		goto bail;
	}

	credits = ocfs2_calc_group_alloc_credits(osb->sb,
						 le16_to_cpu(cl->cl_cpg));
699
	handle = ocfs2_start_trans(osb, credits);
700 701 702 703 704 705 706
	if (IS_ERR(handle)) {
		status = PTR_ERR(handle);
		handle = NULL;
		mlog_errno(status);
		goto bail;
	}

707
	if (last_alloc_group && *last_alloc_group != 0) {
708 709
		trace_ocfs2_block_group_alloc(
				(unsigned long long)*last_alloc_group);
710 711
		ac->ac_last_group = *last_alloc_group;
	}
712 713 714 715 716 717

	bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
					       ac, cl);
	if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC))
		bg_bh = ocfs2_block_group_alloc_discontig(handle,
							  alloc_inode,
718
							  ac, cl);
719 720 721
	if (IS_ERR(bg_bh)) {
		status = PTR_ERR(bg_bh);
		bg_bh = NULL;
722 723 724 725 726 727
		if (status != -ENOSPC)
			mlog_errno(status);
		goto bail;
	}
	bg = (struct ocfs2_group_desc *) bg_bh->b_data;

728
	status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
729
					 bh, OCFS2_JOURNAL_ACCESS_WRITE);
730 731 732 733 734
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}

735 736
	alloc_rec = le16_to_cpu(bg->bg_chain);
	le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
737
		     le16_to_cpu(bg->bg_free_bits_count));
738
	le32_add_cpu(&cl->cl_recs[alloc_rec].c_total,
739
		     le16_to_cpu(bg->bg_bits));
740
	cl->cl_recs[alloc_rec].c_blkno = bg->bg_blkno;
741 742 743 744 745 746 747 748
	if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
		le16_add_cpu(&cl->cl_next_free_rec, 1);

	le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
					le16_to_cpu(bg->bg_free_bits_count));
	le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
	le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));

749
	ocfs2_journal_dirty(handle, bh);
750 751 752 753 754 755 756

	spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
	OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
	fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
					     le32_to_cpu(fe->i_clusters)));
	spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
	i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
757
	alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
758
	ocfs2_update_inode_fsync_trans(handle, alloc_inode, 0);
759 760

	status = 0;
761 762 763 764 765

	/* save the new last alloc group so that the caller can cache it. */
	if (last_alloc_group)
		*last_alloc_group = ac->ac_last_group;

766 767
bail:
	if (handle)
768
		ocfs2_commit_trans(osb, handle);
769 770 771 772

	if (ac)
		ocfs2_free_alloc_context(ac);

773
	brelse(bg_bh);
774

Tao Ma's avatar
Tao Ma committed
775 776
	if (status)
		mlog_errno(status);
777 778 779 780
	return status;
}

static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
781 782
				       struct ocfs2_alloc_context *ac,
				       int type,
783
				       u32 slot,
784
				       u64 *last_alloc_group,
785
				       int flags)
786 787 788
{
	int status;
	u32 bits_wanted = ac->ac_bits_wanted;
789
	struct inode *alloc_inode;
790 791 792 793
	struct buffer_head *bh = NULL;
	struct ocfs2_dinode *fe;
	u32 free_bits;

794 795 796 797 798
	alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
	if (!alloc_inode) {
		mlog_errno(-EINVAL);
		return -EINVAL;
	}
799

800 801
	mutex_lock(&alloc_inode->i_mutex);

802
	status = ocfs2_inode_lock(alloc_inode, &bh, 1);
803
	if (status < 0) {
804 805 806
		mutex_unlock(&alloc_inode->i_mutex);
		iput(alloc_inode);

807
		mlog_errno(status);
808
		return status;
809 810
	}

811
	ac->ac_inode = alloc_inode;
812
	ac->ac_alloc_slot = slot;
813

814
	fe = (struct ocfs2_dinode *) bh->b_data;
815 816 817 818 819

	/* The bh was validated by the inode read inside
	 * ocfs2_inode_lock().  Any corruption is a code bug. */
	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));

820
	if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
821 822 823
		status = ocfs2_error(alloc_inode->i_sb,
				     "Invalid chain allocator %llu\n",
				     (unsigned long long)le64_to_cpu(fe->i_blkno));
824 825 826 827 828 829 830 831 832
		goto bail;
	}

	free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
		le32_to_cpu(fe->id1.bitmap1.i_used);

	if (bits_wanted > free_bits) {
		/* cluster bitmap never grows */
		if (ocfs2_is_cluster_bitmap(alloc_inode)) {
833 834
			trace_ocfs2_reserve_suballoc_bits_nospc(bits_wanted,
								free_bits);
835 836 837 838
			status = -ENOSPC;
			goto bail;
		}

839
		if (!(flags & ALLOC_NEW_GROUP)) {
840 841
			trace_ocfs2_reserve_suballoc_bits_no_new_group(
						slot, bits_wanted, free_bits);
842 843 844 845
			status = -ENOSPC;
			goto bail;
		}

846
		status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
847 848
						 ac->ac_max_block,
						 last_alloc_group, flags);
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
		if (status < 0) {
			if (status != -ENOSPC)
				mlog_errno(status);
			goto bail;
		}
		atomic_inc(&osb->alloc_stats.bg_extends);

		/* You should never ask for this much metadata */
		BUG_ON(bits_wanted >
		       (le32_to_cpu(fe->id1.bitmap1.i_total)
			- le32_to_cpu(fe->id1.bitmap1.i_used)));
	}

	get_bh(bh);
	ac->ac_bh = bh;
bail:
865
	brelse(bh);
866

Tao Ma's avatar
Tao Ma committed
867 868
	if (status)
		mlog_errno(status);
869 870 871
	return status;
}

872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
{
	spin_lock(&osb->osb_lock);
	osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
	spin_unlock(&osb->osb_lock);
	atomic_set(&osb->s_num_inodes_stolen, 0);
}

static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
{
	spin_lock(&osb->osb_lock);
	osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
	spin_unlock(&osb->osb_lock);
	atomic_set(&osb->s_num_meta_stolen, 0);
}

void ocfs2_init_steal_slots(struct ocfs2_super *osb)
{
	ocfs2_init_inode_steal_slot(osb);
	ocfs2_init_meta_steal_slot(osb);
}

static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
{
	spin_lock(&osb->osb_lock);
	if (type == INODE_ALLOC_SYSTEM_INODE)
		osb->s_inode_steal_slot = slot;
	else if (type == EXTENT_ALLOC_SYSTEM_INODE)
		osb->s_meta_steal_slot = slot;
	spin_unlock(&osb->osb_lock);
}

static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
{
	int slot = OCFS2_INVALID_SLOT;

	spin_lock(&osb->osb_lock);
	if (type == INODE_ALLOC_SYSTEM_INODE)
		slot = osb->s_inode_steal_slot;
	else if (type == EXTENT_ALLOC_SYSTEM_INODE)
		slot = osb->s_meta_steal_slot;
	spin_unlock(&osb->osb_lock);

	return slot;
}

static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
{
	return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
}

static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
{
	return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
}

static int ocfs2_steal_resource(struct ocfs2_super *osb,
				struct ocfs2_alloc_context *ac,
				int type)
{
	int i, status = -ENOSPC;
	int slot = __ocfs2_get_steal_slot(osb, type);

	/* Start to steal resource from the first slot after ours. */
	if (slot == OCFS2_INVALID_SLOT)
		slot = osb->slot_num + 1;

	for (i = 0; i < osb->max_slots; i++, slot++) {
		if (slot == osb->max_slots)
			slot = 0;

		if (slot == osb->slot_num)
			continue;

		status = ocfs2_reserve_suballoc_bits(osb, ac,
						     type,
						     (u32)slot, NULL,
						     NOT_ALLOC_NEW_GROUP);
		if (status >= 0) {
			__ocfs2_set_steal_slot(osb, slot, type);
			break;
		}

		ocfs2_free_ac_resource(ac);
	}

	return status;
}

static int ocfs2_steal_inode(struct ocfs2_super *osb,
			     struct ocfs2_alloc_context *ac)
{
	return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
}

static int ocfs2_steal_meta(struct ocfs2_super *osb,
			    struct ocfs2_alloc_context *ac)
{
	return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
}

973 974 975
int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
				      int blocks,
				      struct ocfs2_alloc_context **ac)
976 977
{
	int status;
978
	int slot = ocfs2_get_meta_steal_slot(osb);
979

980
	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
981 982 983 984 985 986
	if (!(*ac)) {
		status = -ENOMEM;
		mlog_errno(status);
		goto bail;
	}

987
	(*ac)->ac_bits_wanted = blocks;
988 989 990
	(*ac)->ac_which = OCFS2_AC_USE_META;
	(*ac)->ac_group_search = ocfs2_block_group_search;

991 992 993 994 995
	if (slot != OCFS2_INVALID_SLOT &&
		atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
		goto extent_steal;

	atomic_set(&osb->s_num_meta_stolen, 0);
996
	status = ocfs2_reserve_suballoc_bits(osb, (*ac),
997
					     EXTENT_ALLOC_SYSTEM_INODE,
998
					     (u32)osb->slot_num, NULL,
999
					     ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016


	if (status >= 0) {
		status = 0;
		if (slot != OCFS2_INVALID_SLOT)
			ocfs2_init_meta_steal_slot(osb);
		goto bail;
	} else if (status < 0 && status != -ENOSPC) {
		mlog_errno(status);
		goto bail;
	}

	ocfs2_free_ac_resource(*ac);

extent_steal:
	status = ocfs2_steal_meta(osb, *ac);
	atomic_inc(&osb->s_num_meta_stolen);
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
	if (status < 0) {
		if (status != -ENOSPC)
			mlog_errno(status);
		goto bail;
	}

	status = 0;
bail:
	if ((status < 0) && *ac) {
		ocfs2_free_alloc_context(*ac);
		*ac = NULL;
	}

Tao Ma's avatar
Tao Ma committed
1030 1031
	if (status)
		mlog_errno(status);
1032 1033 1034
	return status;
}

1035 1036 1037 1038 1039 1040 1041 1042 1043
int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
			       struct ocfs2_extent_list *root_el,
			       struct ocfs2_alloc_context **ac)
{
	return ocfs2_reserve_new_metadata_blocks(osb,
					ocfs2_extend_meta_needed(root_el),
					ac);
}

1044 1045 1046 1047
int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
			    struct ocfs2_alloc_context **ac)
{
	int status;
1048
	int slot = ocfs2_get_inode_steal_slot(osb);
1049
	u64 alloc_group;
1050

1051
	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
	if (!(*ac)) {
		status = -ENOMEM;
		mlog_errno(status);
		goto bail;
	}

	(*ac)->ac_bits_wanted = 1;
	(*ac)->ac_which = OCFS2_AC_USE_INODE;

	(*ac)->ac_group_search = ocfs2_block_group_search;

1063 1064 1065
	/*
	 * stat(2) can't handle i_ino > 32bits, so we tell the
	 * lower levels not to allocate us a block group past that
1066
	 * limit.  The 'inode64' mount option avoids this behavior.
1067
	 */
1068 1069
	if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
		(*ac)->ac_max_block = (u32)~0U;
1070

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	/*
	 * slot is set when we successfully steal inode from other nodes.
	 * It is reset in 3 places:
	 * 1. when we flush the truncate log
	 * 2. when we complete local alloc recovery.
	 * 3. when we successfully allocate from our own slot.
	 * After it is set, we will go on stealing inodes until we find the
	 * need to check our slots to see whether there is some space for us.
	 */
	if (slot != OCFS2_INVALID_SLOT &&
1081
	    atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
1082 1083 1084
		goto inode_steal;

	atomic_set(&osb->s_num_inodes_stolen, 0);
1085
	alloc_group = osb->osb_inode_alloc_group;
1086 1087
	status = ocfs2_reserve_suballoc_bits(osb, *ac,
					     INODE_ALLOC_SYSTEM_INODE,
1088
					     (u32)osb->slot_num,
1089
					     &alloc_group,
1090 1091
					     ALLOC_NEW_GROUP |
					     ALLOC_GROUPS_FROM_GLOBAL);
1092 1093 1094
	if (status >= 0) {
		status = 0;

1095 1096 1097
		spin_lock(&osb->osb_lock);
		osb->osb_inode_alloc_group = alloc_group;
		spin_unlock(&osb->osb_lock);
1098 1099
		trace_ocfs2_reserve_new_inode_new_group(
			(unsigned long long)alloc_group);
1100

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
		/*
		 * Some inodes must be freed by us, so try to allocate
		 * from our own next time.
		 */
		if (slot != OCFS2_INVALID_SLOT)
			ocfs2_init_inode_steal_slot(osb);
		goto bail;
	} else if (status < 0 && status != -ENOSPC) {
		mlog_errno(status);
		goto bail;
	}

	ocfs2_free_ac_resource(*ac);

inode_steal:
1116
	status = ocfs2_steal_inode(osb, *ac);
1117
	atomic_inc(&osb->s_num_inodes_stolen);
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
	if (status < 0) {
		if (status != -ENOSPC)
			mlog_errno(status);
		goto bail;
	}

	status = 0;
bail:
	if ((status < 0) && *ac) {
		ocfs2_free_alloc_context(*ac);
		*ac = NULL;
	}

Tao Ma's avatar
Tao Ma committed
1131 1132
	if (status)
		mlog_errno(status);
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
	return status;
}

/* local alloc code has to do the same thing, so rather than do this
 * twice.. */
int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
				      struct ocfs2_alloc_context *ac)
{
	int status;

	ac->ac_which = OCFS2_AC_USE_MAIN;
	ac->ac_group_search = ocfs2_cluster_group_search;

1146 1147
	status = ocfs2_reserve_suballoc_bits(osb, ac,
					     GLOBAL_BITMAP_SYSTEM_INODE,
1148
					     OCFS2_INVALID_SLOT, NULL,
1149
					     ALLOC_NEW_GROUP);
1150
	if (status < 0 && status != -ENOSPC) {
1151
		mlog_errno(status);
1152 1153 1154
		goto bail;
	}

1155 1156 1157 1158 1159 1160 1161
bail:
	return status;
}

/* Callers don't need to care which bitmap (local alloc or main) to
 * use so we figure it out for them, but unfortunately this clutters
 * things a bit. */
1162 1163
static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
					     u32 bits_wanted, u64 max_block,
1164
					     int flags,
1165
					     struct ocfs2_alloc_context **ac)
1166 1167 1168
{
	int status;

1169
	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
1170 1171 1172 1173 1174 1175 1176
	if (!(*ac)) {
		status = -ENOMEM;
		mlog_errno(status);
		goto bail;
	}

	(*ac)->ac_bits_wanted = bits_wanted;
1177
	(*ac)->ac_max_block = max_block;
1178 1179

	status = -ENOSPC;
1180 1181
	if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
	    ocfs2_alloc_should_use_local(osb, bits_wanted)) {
1182 1183 1184
		status = ocfs2_reserve_local_alloc_bits(osb,
							bits_wanted,
							*ac);
1185
		if ((status < 0) && (status != -ENOSPC)) {
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
			mlog_errno(status);
			goto bail;
		}
	}

	if (status == -ENOSPC) {
		status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
		if (status < 0) {
			if (status != -ENOSPC)
				mlog_errno(status);
			goto bail;
		}
	}

	status = 0;
bail:
	if ((status < 0) && *ac) {
		ocfs2_free_alloc_context(*ac);
		*ac = NULL;
	}

Tao Ma's avatar
Tao Ma committed
1207 1208
	if (status)
		mlog_errno(status);
1209 1210 1211
	return status;
}

1212 1213 1214 1215
int ocfs2_reserve_clusters(struct ocfs2_super *osb,
			   u32 bits_wanted,
			   struct ocfs2_alloc_context **ac)
{
1216 1217
	return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
						 ALLOC_NEW_GROUP, ac);
1218 1219
}

1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
/*
 * More or less lifted from ext3. I'll leave their description below:
 *
 * "For ext3 allocations, we must not reuse any blocks which are
 * allocated in the bitmap buffer's "last committed data" copy.  This
 * prevents deletes from freeing up the page for reuse until we have
 * committed the delete transaction.
 *
 * If we didn't do this, then deleting something and reallocating it as
 * data would allow the old block to be overwritten before the
 * transaction committed (because we force data to disk before commit).
 * This would lead to corruption if we crashed between overwriting the
 * data and committing the delete.
 *
 * @@@ We may want to make this allocation behaviour conditional on
 * data-writes at some point, and disable it for metadata allocations or
 * sync-data inodes."
 *
 * Note: OCFS2 already does this differently for metadata vs data
Joe Perches's avatar
Joe Perches committed
1239
 * allocations, as those bitmaps are separate and undo access is never
1240 1241 1242 1243 1244 1245
 * called on a metadata group descriptor.
 */
static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
					 int nr)
{
	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1246
	int ret;
1247 1248 1249

	if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
		return 0;
1250 1251

	if (!buffer_jbd(bg_bh))
1252 1253
		return 1;

1254
	jbd_lock_bh_state(bg_bh);
1255
	bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
1256 1257 1258 1259 1260 1261 1262
	if (bg)
		ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
	else
		ret = 1;
	jbd_unlock_bh_state(bg_bh);

	return ret;
1263 1264 1265 1266 1267
}

static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
					     struct buffer_head *bg_bh,
					     unsigned int bits_wanted,
1268
					     unsigned int total_bits,
1269
					     struct ocfs2_suballoc_result *res)
1270 1271 1272 1273 1274 1275
{
	void *bitmap;
	u16 best_offset, best_size;
	int offset, start, found, status = 0;
	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;

1276 1277 1278
	/* Callers got this descriptor from
	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1279 1280 1281 1282

	found = start = best_offset = best_size = 0;
	bitmap = bg->bg_bitmap;

1283 1284
	while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
		if (offset == total_bits)
1285 1286 1287 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
			break;

		if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
			/* We found a zero, but we can't use it as it
			 * hasn't been put to disk yet! */
			found = 0;
			start = offset + 1;
		} else if (offset == start) {
			/* we found a zero */
			found++;
			/* move start to the next bit to test */
			start++;
		} else {
			/* got a zero after some ones */
			found = 1;
			start = offset + 1;
		}
		if (found > best_size) {
			best_size = found;
			best_offset = start - found;
		}
		/* we got everything we needed */
		if (found == bits_wanted) {
			/* mlog(0, "Found it all!\n"); */
			break;
		}
	}

1313 1314 1315
	if (best_size) {
		res->sr_bit_offset = best_offset;
		res->sr_bits = best_size;
1316 1317 1318 1319 1320 1321 1322 1323 1324
	} else {
		status = -ENOSPC;
		/* No error log here -- see the comment above
		 * ocfs2_test_bg_bit_allocatable */
	}

	return status;
}

1325
int ocfs2_block_group_set_bits(handle_t *handle,
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
					     struct inode *alloc_inode,
					     struct ocfs2_group_desc *bg,
					     struct buffer_head *group_bh,
					     unsigned int bit_off,
					     unsigned int num_bits)
{
	int status;
	void *bitmap = bg->bg_bitmap;
	int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;

1336 1337 1338
	/* All callers get the descriptor via
	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1339 1340
	BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);

1341
	trace_ocfs2_block_group_set_bits(bit_off, num_bits);
1342 1343 1344 1345

	if (ocfs2_is_cluster_bitmap(alloc_inode))
		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;

1346
	status = ocfs2_journal_access_gd(handle,
1347
					 INODE_CACHE(alloc_inode),
1348 1349
					 group_bh,
					 journal_type);
1350 1351 1352 1353 1354 1355
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}

	le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
1356
	if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
1357 1358 1359 1360 1361
		return ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit count %u but claims %u are freed. num_bits %d\n",
				   (unsigned long long)le64_to_cpu(bg->bg_blkno),
				   le16_to_cpu(bg->bg_bits),
				   le16_to_cpu(bg->bg_free_bits_count),
				   num_bits);
1362
	}
1363 1364 1365
	while(num_bits--)
		ocfs2_set_bit(bit_off++, bitmap);

1366
	ocfs2_journal_dirty(handle, group_bh);
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390

bail:
	return status;
}

/* find the one with the most empty bits */
static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
{
	u16 curr, best;

	BUG_ON(!cl->cl_next_free_rec);

	best = curr = 0;
	while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
		if (le32_to_cpu(cl->cl_recs[curr].c_free) >
		    le32_to_cpu(cl->cl_recs[best].c_free))
			best = curr;
		curr++;
	}

	BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
	return best;
}

1391
static int ocfs2_relink_block_group(handle_t *handle,
1392 1393 1394 1395 1396 1397 1398 1399 1400
				    struct inode *alloc_inode,
				    struct buffer_head *fe_bh,
				    struct buffer_head *bg_bh,
				    struct buffer_head *prev_bg_bh,
				    u16 chain)
{
	int status;
	/* there is a really tiny chance the journal calls could fail,
	 * but we wouldn't want inconsistent blocks in *any* case. */
1401
	u64 bg_ptr, prev_bg_ptr;
1402 1403 1404 1405
	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
	struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;

1406 1407 1408 1409
	/* The caller got these descriptors from
	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
1410

1411 1412 1413 1414
	trace_ocfs2_relink_block_group(
		(unsigned long long)le64_to_cpu(fe->i_blkno), chain,
		(unsigned long long)le64_to_cpu(bg->bg_blkno),
		(unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
1415 1416 1417 1418

	bg_ptr = le64_to_cpu(bg->bg_next_group);
	prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);

1419 1420
	status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
					 prev_bg_bh,
1421
					 OCFS2_JOURNAL_ACCESS_WRITE);
1422
	if (status < 0)
1423
		goto out;
1424 1425

	prev_bg->bg_next_group = bg->bg_next_group;
1426
	ocfs2_journal_dirty(handle, prev_bg_bh);
1427

1428 1429
	status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
					 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
1430
	if (status < 0)
1431
		goto out_rollback_prev_bg;
1432 1433

	bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
1434
	ocfs2_journal_dirty(handle, bg_bh);
1435

1436 1437
	status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
					 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
1438
	if (status < 0)
1439
		goto out_rollback_bg;
1440 1441

	fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
1442
	ocfs2_journal_dirty(handle, fe_bh);
1443

1444
out:
1445 1446
	if (status < 0)
		mlog_errno(status);
1447
	return status;
1448

1449 1450 1451 1452
out_rollback_bg:
	bg->bg_next_group = cpu_to_le64(bg_ptr);
out_rollback_prev_bg:
	prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1453
	goto out;
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
}

static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
						     u32 wanted)
{
	return le16_to_cpu(bg->bg_free_bits_count) > wanted;
}

/* return 0 on success, -ENOSPC to keep searching and any other < 0
 * value on error. */
static int ocfs2_cluster_group_search(struct inode *inode,
				      struct buffer_head *group_bh,
				      u32 bits_wanted, u32 min_bits,
1467
				      u64 max_block,
1468
				      struct ocfs2_suballoc_result *res)
1469 1470 1471
{
	int search = -ENOSPC;
	int ret;
1472
	u64 blkoff;
1473
	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
1474
	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1475
	unsigned int max_bits, gd_cluster_off;
1476 1477 1478

	BUG_ON(!ocfs2_is_cluster_bitmap(inode));

1479 1480 1481 1482
	if (gd->bg_free_bits_count) {
		max_bits = le16_to_cpu(gd->bg_bits);

		/* Tail groups in cluster bitmaps which aren't cpg
Lucas De Marchi's avatar
Lucas De Marchi committed
1483
		 * aligned are prone to partial extension by a failed
1484 1485 1486 1487 1488 1489 1490 1491 1492
		 * fs resize. If the file system resize never got to
		 * update the dinode cluster count, then we don't want
		 * to trust any clusters past it, regardless of what
		 * the group descriptor says. */
		gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
							  le64_to_cpu(gd->bg_blkno));
		if ((gd_cluster_off + max_bits) >
		    OCFS2_I(inode)->ip_clusters) {
			max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1493 1494 1495 1496
			trace_ocfs2_cluster_group_search_wrong_max_bits(
				(unsigned long long)le64_to_cpu(gd->bg_blkno),
				le16_to_cpu(gd->bg_bits),
				OCFS2_I(inode)->ip_clusters, max_bits);
1497 1498
		}

1499 1500
		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
							group_bh, bits_wanted,
1501
							max_bits, res);
1502 1503 1504
		if (ret)
			return ret;

1505 1506 1507
		if (max_block) {
			blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
							  gd_cluster_off +
1508 1509
							  res->sr_bit_offset +
							  res->sr_bits);
1510 1511 1512
			trace_ocfs2_cluster_group_search_max_block(
				(unsigned long long)blkoff,
				(unsigned long long)max_block);
1513 1514 1515 1516
			if (blkoff > max_block)
				return -ENOSPC;
		}

1517 1518 1519 1520
		/* ocfs2_block_group_find_clear_bits() might
		 * return success, but we still want to return
		 * -ENOSPC unless it found the minimum number
		 * of bits. */
1521
		if (min_bits <= res->sr_bits)
1522
			search = 0; /* success */
1523
		else if (res->sr_bits) {
1524 1525 1526 1527
			/*
			 * Don't show bits which we'll be returning
			 * for allocation to the local alloc bitmap.
			 */
1528
			ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
1529 1530 1531 1532 1533 1534 1535 1536 1537
		}
	}

	return search;
}

static int ocfs2_block_group_search(struct inode *inode,
				    struct buffer_head *group_bh,
				    u32 bits_wanted, u32 min_bits,
1538
				    u64 max_block,
1539
				    struct ocfs2_suballoc_result *res)
1540 1541
{
	int ret = -ENOSPC;
1542
	u64 blkoff;
1543 1544 1545 1546 1547
	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;

	BUG_ON(min_bits != 1);
	BUG_ON(ocfs2_is_cluster_bitmap(inode));

1548
	if (bg->bg_free_bits_count) {
1549 1550
		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
							group_bh, bits_wanted,
1551
							le16_to_cpu(bg->bg_bits),
1552
							res);
1553
		if (!ret && max_block) {
1554 1555
			blkoff = le64_to_cpu(bg->bg_blkno) +
				res->sr_bit_offset + res->sr_bits;
1556 1557 1558
			trace_ocfs2_block_group_search_max_block(
				(unsigned long long)blkoff,
				(unsigned long long)max_block);
1559 1560 1561 1562
			if (blkoff > max_block)
				ret = -ENOSPC;
		}
	}
1563 1564 1565 1566

	return ret;
}

1567
int ocfs2_alloc_dinode_update_counts(struct inode *inode,
1568
				       handle_t *handle,
Mark Fasheh's avatar
Mark Fasheh committed
1569 1570 1571 1572 1573 1574 1575 1576 1577
				       struct buffer_head *di_bh,
				       u32 num_bits,
				       u16 chain)
{
	int ret;
	u32 tmp_used;
	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;

1578
	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
1579
				      OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh's avatar
Mark Fasheh committed
1580 1581 1582 1583 1584 1585 1586 1587
	if (ret < 0) {
		mlog_errno(ret);
		goto out;
	}

	tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
	di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
	le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1588
	ocfs2_journal_dirty(handle, di_bh);
Mark Fasheh's avatar
Mark Fasheh committed
1589 1590 1591 1592 1593

out:
	return ret;
}

1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
void ocfs2_rollback_alloc_dinode_counts(struct inode *inode,
				       struct buffer_head *di_bh,
				       u32 num_bits,
				       u16 chain)
{
	u32 tmp_used;
	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
	struct ocfs2_chain_list *cl;

	cl = (struct ocfs2_chain_list *)&di->id2.i_chain;
	tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
	di->id1.bitmap1.i_used = cpu_to_le32(tmp_used - num_bits);
	le32_add_cpu(&cl->cl_recs[chain].c_free, num_bits);
}

1609 1610 1611
static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
					 struct ocfs2_extent_rec *rec,
					 struct ocfs2_chain_list *cl)
1612 1613 1614
{
	unsigned int bpc = le16_to_cpu(cl->cl_bpc);
	unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
1615
	unsigned int bitcount = le16_to_cpu(rec->e_leaf_clusters) * bpc;
1616 1617 1618 1619 1620

	if (res->sr_bit_offset < bitoff)
		return 0;
	if (res->sr_bit_offset >= (bitoff + bitcount))
		return 0;
1621 1622
	res->sr_blkno = le64_to_cpu(rec->e_blkno) +
		(res->sr_bit_offset - bitoff);
1623 1624 1625 1626 1627
	if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
		res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
	return 1;
}

1628 1629 1630
static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
					  struct ocfs2_group_desc *bg,
					  struct ocfs2_suballoc_result *res)
1631 1632
{
	int i;
1633
	u64 bg_blkno = res->sr_bg_blkno;  /* Save off */
1634 1635 1636 1637
	struct ocfs2_extent_rec *rec;
	struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
	struct ocfs2_chain_list *cl = &di->id2.i_chain;

1638 1639
	if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
		res->sr_blkno = 0;
1640
		return;
1641
	}
1642

1643
	res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
1644
	res->sr_bg_blkno = 0;  /* Clear it for contig block groups */
1645
	if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) ||
1646
	    !bg->bg_list.l_next_free_rec)
1647 1648 1649 1650
		return;

	for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
		rec = &bg->bg_list.l_recs[i];
1651 1652
		if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) {
			res->sr_bg_blkno = bg_blkno;  /* Restore */
1653
			break;
1654
		}
1655 1656 1657
	}
}

Mark Fasheh's avatar
Mark Fasheh committed
1658
static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1659
				  handle_t *handle,
Mark Fasheh's avatar
Mark Fasheh committed
1660 1661
				  u32 bits_wanted,
				  u32 min_bits,
1662
				  struct ocfs2_suballoc_result *res,
Mark Fasheh's avatar
Mark Fasheh committed
1663 1664 1665 1666 1667
				  u16 *bits_left)
{
	int ret;
	struct buffer_head *group_bh = NULL;
	struct ocfs2_group_desc *gd;
1668
	struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
Mark Fasheh's avatar
Mark Fasheh committed
1669 1670
	struct inode *alloc_inode = ac->ac_inode;

1671 1672
	ret = ocfs2_read_group_descriptor(alloc_inode, di,
					  res->sr_bg_blkno, &group_bh);
Mark Fasheh's avatar
Mark Fasheh committed
1673 1674 1675 1676 1677 1678 1679
	if (ret < 0) {
		mlog_errno(ret);
		return ret;
	}

	gd = (struct ocfs2_group_desc *) group_bh->b_data;
	ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1680
				  ac->ac_max_block, res);
Mark Fasheh's avatar
Mark Fasheh committed
1681 1682 1683 1684 1685 1686
	if (ret < 0) {
		if (ret != -ENOSPC)
			mlog_errno(ret);
		goto out;
	}

1687
	if (!ret)
1688
		ocfs2_bg_discontig_fix_result(ac, gd, res);
1689

1690 1691 1692 1693 1694 1695 1696 1697 1698
	/*
	 * sr_bg_blkno might have been changed by
	 * ocfs2_bg_discontig_fix_result
	 */
	res->sr_bg_stable_blkno = group_bh->b_blocknr;

	if (ac->ac_find_loc_only)
		goto out_loc_only;

Mark Fasheh's avatar
Mark Fasheh committed
1699
	ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1700
					       res->sr_bits,
Mark Fasheh's avatar
Mark Fasheh committed
1701 1702 1703 1704 1705 1706 1707
					       le16_to_cpu(gd->bg_chain));
	if (ret < 0) {
		mlog_errno(ret);
		goto out;
	}

	ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1708
					 res->sr_bit_offset, res->sr_bits);
1709 1710 1711 1712
	if (ret < 0) {
		ocfs2_rollback_alloc_dinode_counts(alloc_inode, ac->ac_bh,
					       res->sr_bits,
					       le16_to_cpu(gd->bg_chain));
Mark Fasheh's avatar
Mark Fasheh committed
1713
		mlog_errno(ret);
1714
	}
Mark Fasheh's avatar
Mark Fasheh committed
1715

1716
out_loc_only:
Mark Fasheh's avatar
Mark Fasheh committed
1717 1718 1719 1720 1721 1722 1723 1724
	*bits_left = le16_to_cpu(gd->bg_free_bits_count);

out:
	brelse(group_bh);

	return ret;
}

1725
static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1726
			      handle_t *handle,
1727 1728
			      u32 bits_wanted,
			      u32 min_bits,
1729
			      struct ocfs2_suballoc_result *res,
Mark Fasheh's avatar
Mark Fasheh committed
1730
			      u16 *bits_left)
1731 1732
{
	int status;
1733
	u16 chain;
1734 1735 1736 1737 1738 1739 1740 1741 1742
	u64 next_group;
	struct inode *alloc_inode = ac->ac_inode;
	struct buffer_head *group_bh = NULL;
	struct buffer_head *prev_group_bh = NULL;
	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
	struct ocfs2_group_desc *bg;

	chain = ac->ac_chain;
1743 1744 1745
	trace_ocfs2_search_chain_begin(
		(unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
		bits_wanted, chain);
1746

1747 1748 1749
	status = ocfs2_read_group_descriptor(alloc_inode, fe,
					     le64_to_cpu(cl->cl_recs[chain].c_blkno),
					     &group_bh);
1750 1751 1752 1753 1754 1755 1756 1757 1758
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}
	bg = (struct ocfs2_group_desc *) group_bh->b_data;

	status = -ENOSPC;
	/* for now, the chain search is a bit simplistic. We just use
	 * the 1st group with any empty bits. */
1759 1760
	while ((status = ac->ac_group_search(alloc_inode, group_bh,
					     bits_wanted, min_bits,
1761 1762
					     ac->ac_max_block,
					     res)) == -ENOSPC) {
1763 1764
		if (!bg->bg_next_group)
			break;
1765 1766 1767 1768

		brelse(prev_group_bh);
		prev_group_bh = NULL;

1769 1770 1771
		next_group = le64_to_cpu(bg->bg_next_group);
		prev_group_bh = group_bh;
		group_bh = NULL;
1772 1773
		status = ocfs2_read_group_descriptor(alloc_inode, fe,
						     next_group, &group_bh);
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
		if (status < 0) {
			mlog_errno(status);
			goto bail;
		}
		bg = (struct ocfs2_group_desc *) group_bh->b_data;
	}
	if (status < 0) {
		if (status != -ENOSPC)
			mlog_errno(status);
		goto bail;
	}

1786 1787
	trace_ocfs2_search_chain_succ(
		(unsigned long long)le64_to_cpu(bg->bg_blkno), res->sr_bits);
1788

1789
	res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
1790

1791
	BUG_ON(res->sr_bits == 0);
1792
	if (!status)
1793
		ocfs2_bg_discontig_fix_result(ac, bg, res);
1794

1795 1796 1797 1798 1799
	/*
	 * sr_bg_blkno might have been changed by
	 * ocfs2_bg_discontig_fix_result
	 */
	res->sr_bg_stable_blkno = group_bh->b_blocknr;
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813

	/*
	 * Keep track of previous block descriptor read. When
	 * we find a target, if we have read more than X
	 * number of descriptors, and the target is reasonably
	 * empty, relink him to top of his chain.
	 *
	 * We've read 0 extra blocks and only send one more to
	 * the transaction, yet the next guy to search has a
	 * much easier time.
	 *
	 * Do this *after* figuring out how many bits we're taking out
	 * of our target group.
	 */
1814
	if (!ac->ac_disable_chain_relink &&
1815
	    (prev_group_bh) &&
1816
	    (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
1817 1818 1819 1820 1821 1822 1823 1824 1825
		status = ocfs2_relink_block_group(handle, alloc_inode,
						  ac->ac_bh, group_bh,
						  prev_group_bh, chain);
		if (status < 0) {
			mlog_errno(status);
			goto bail;
		}
	}

1826 1827 1828
	if (ac->ac_find_loc_only)
		goto out_loc_only;

1829 1830 1831 1832
	status = ocfs2_alloc_dinode_update_counts(alloc_inode, handle,
						  ac->ac_bh, res->sr_bits,
						  chain);
	if (status) {
1833 1834 1835 1836 1837 1838 1839 1840
		mlog_errno(status);
		goto bail;
	}

	status = ocfs2_block_group_set_bits(handle,
					    alloc_inode,
					    bg,
					    group_bh,
1841 1842
					    res->sr_bit_offset,
					    res->sr_bits);
1843
	if (status < 0) {
1844 1845
		ocfs2_rollback_alloc_dinode_counts(alloc_inode,
					ac->ac_bh, res->sr_bits, chain);
1846 1847 1848 1849
		mlog_errno(status);
		goto bail;
	}

1850 1851 1852
	trace_ocfs2_search_chain_end(
			(unsigned long long)le64_to_cpu(fe->i_blkno),
			res->sr_bits);
1853

1854
out_loc_only:
Mark Fasheh's avatar
Mark Fasheh committed
1855
	*bits_left = le16_to_cpu(bg->bg_free_bits_count);
1856
bail:
1857 1858
	brelse(group_bh);
	brelse(prev_group_bh);
1859

Tao Ma's avatar
Tao Ma committed
1860 1861
	if (status)
		mlog_errno(status);
1862 1863 1864 1865
	return status;
}

/* will give out up to bits_wanted contiguous bits. */
1866
static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
1867
				     handle_t *handle,
1868 1869
				     u32 bits_wanted,
				     u32 min_bits,
1870
				     struct ocfs2_suballoc_result *res)
1871 1872 1873
{
	int status;
	u16 victim, i;
Mark Fasheh's avatar
Mark Fasheh committed
1874
	u16 bits_left = 0;
1875
	u64 hint = ac->ac_last_group;
1876 1877 1878 1879 1880 1881 1882 1883
	struct ocfs2_chain_list *cl;
	struct ocfs2_dinode *fe;

	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
	BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
	BUG_ON(!ac->ac_bh);

	fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1884 1885 1886 1887 1888

	/* The bh was validated by the inode read during
	 * ocfs2_reserve_suballoc_bits().  Any corruption is a code bug. */
	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));

1889 1890
	if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
	    le32_to_cpu(fe->id1.bitmap1.i_total)) {
1891
		status = ocfs2_error(ac->ac_inode->i_sb,
1892 1893 1894 1895
				     "Chain allocator dinode %llu has %u used bits but only %u total\n",
				     (unsigned long long)le64_to_cpu(fe->i_blkno),
				     le32_to_cpu(fe->id1.bitmap1.i_used),
				     le32_to_cpu(fe->id1.bitmap1.i_total));
1896 1897 1898
		goto bail;
	}

1899
	res->sr_bg_blkno = hint;
1900
	if (res->sr_bg_blkno) {
Mark Fasheh's avatar
Mark Fasheh committed
1901 1902
		/* Attempt to short-circuit the usual search mechanism
		 * by jumping straight to the most recently used
1903
		 * allocation group. This helps us maintain some
Mark Fasheh's avatar
Mark Fasheh committed
1904
		 * contiguousness across allocations. */
1905
		status = ocfs2_search_one_group(ac, handle, bits_wanted,
1906 1907
						min_bits, res, &bits_left);
		if (!status)
Mark Fasheh's avatar
Mark Fasheh committed
1908 1909 1910 1911 1912 1913 1914
			goto set_hint;
		if (status < 0 && status != -ENOSPC) {
			mlog_errno(status);
			goto bail;
		}
	}

1915 1916 1917 1918 1919
	cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;

	victim = ocfs2_find_victim_chain(cl);
	ac->ac_chain = victim;

1920 1921
	status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
				    res, &bits_left);
1922 1923
	if (!status) {
		hint = ocfs2_group_from_res(res);
Mark Fasheh's avatar
Mark Fasheh committed
1924
		goto set_hint;
1925
	}
1926 1927 1928 1929 1930
	if (status < 0 && status != -ENOSPC) {
		mlog_errno(status);
		goto bail;
	}

1931
	trace_ocfs2_claim_suballoc_bits(victim);
1932 1933 1934 1935 1936

	/* If we didn't pick a good victim, then just default to
	 * searching each chain in order. Don't allow chain relinking
	 * because we only calculate enough journal credits for one
	 * relink per alloc. */
1937
	ac->ac_disable_chain_relink = 1;
1938 1939 1940 1941 1942 1943 1944
	for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
		if (i == victim)
			continue;
		if (!cl->cl_recs[i].c_free)
			continue;

		ac->ac_chain = i;
1945
		status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1946
					    res, &bits_left);
1947 1948
		if (!status) {
			hint = ocfs2_group_from_res(res);
1949
			break;
1950
		}
1951 1952 1953 1954 1955 1956
		if (status < 0 && status != -ENOSPC) {
			mlog_errno(status);
			goto bail;
		}
	}

Mark Fasheh's avatar
Mark Fasheh committed
1957 1958 1959 1960 1961 1962 1963 1964
set_hint:
	if (status != -ENOSPC) {
		/* If the next search of this group is not likely to
		 * yield a suitable extent, then we reset the last
		 * group hint so as to not waste a disk read */
		if (bits_left < min_bits)
			ac->ac_last_group = 0;
		else
1965
			ac->ac_last_group = hint;
Mark Fasheh's avatar
Mark Fasheh committed
1966 1967 1968
	}

bail:
Tao Ma's avatar
Tao Ma committed
1969 1970
	if (status)
		mlog_errno(status);
1971 1972 1973
	return status;
}

1974
int ocfs2_claim_metadata(handle_t *handle,
1975 1976
			 struct ocfs2_alloc_context *ac,
			 u32 bits_wanted,
1977
			 u64 *suballoc_loc,
1978 1979 1980 1981 1982
			 u16 *suballoc_bit_start,
			 unsigned int *num_bits,
			 u64 *blkno_start)
{
	int status;
1983
	struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
1984 1985 1986 1987 1988

	BUG_ON(!ac);
	BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
	BUG_ON(ac->ac_which != OCFS2_AC_USE_META);

1989
	status = ocfs2_claim_suballoc_bits(ac,
1990
					   handle,
1991 1992
					   bits_wanted,
					   1,
1993
					   &res);
1994 1995 1996 1997
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}
1998
	atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
1999

2000
	*suballoc_loc = res.sr_bg_blkno;
2001
	*suballoc_bit_start = res.sr_bit_offset;
2002
	*blkno_start = res.sr_blkno;
2003 2004
	ac->ac_bits_given += res.sr_bits;
	*num_bits = res.sr_bits;
2005 2006
	status = 0;
bail:
Tao Ma's avatar
Tao Ma committed
2007 2008
	if (status)
		mlog_errno(status);
2009 2010 2011
	return status;
}

2012
static void ocfs2_init_inode_ac_group(struct inode *dir,
2013
				      struct buffer_head *parent_di_bh,
2014 2015
				      struct ocfs2_alloc_context *ac)
{
2016
	struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data;
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
	/*
	 * Try to allocate inodes from some specific group.
	 *
	 * If the parent dir has recorded the last group used in allocation,
	 * cool, use it. Otherwise if we try to allocate new inode from the
	 * same slot the parent dir belongs to, use the same chunk.
	 *
	 * We are very careful here to avoid the mistake of setting
	 * ac_last_group to a group descriptor from a different (unlocked) slot.
	 */
	if (OCFS2_I(dir)->ip_last_used_group &&
	    OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
		ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
2030 2031 2032 2033 2034 2035 2036 2037
	else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) {
		if (di->i_suballoc_loc)
			ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc);
		else
			ac->ac_last_group = ocfs2_which_suballoc_group(
					le64_to_cpu(di->i_blkno),
					le16_to_cpu(di->i_suballoc_bit));
	}
2038 2039 2040 2041 2042 2043 2044 2045 2046
}

static inline void ocfs2_save_inode_ac_group(struct inode *dir,
					     struct ocfs2_alloc_context *ac)
{
	OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
	OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
}

2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
int ocfs2_find_new_inode_loc(struct inode *dir,
			     struct buffer_head *parent_fe_bh,
			     struct ocfs2_alloc_context *ac,
			     u64 *fe_blkno)
{
	int ret;
	handle_t *handle = NULL;
	struct ocfs2_suballoc_result *res;

	BUG_ON(!ac);
	BUG_ON(ac->ac_bits_given != 0);
	BUG_ON(ac->ac_bits_wanted != 1);
	BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);

	res = kzalloc(sizeof(*res), GFP_NOFS);
	if (res == NULL) {
		ret = -ENOMEM;
		mlog_errno(ret);
		goto out;
	}

	ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);

	/*
	 * The handle started here is for chain relink. Alternatively,
	 * we could just disable relink for these calls.
	 */
	handle = ocfs2_start_trans(OCFS2_SB(dir->i_sb), OCFS2_SUBALLOC_ALLOC);
	if (IS_ERR(handle)) {
		ret = PTR_ERR(handle);
		handle = NULL;
		mlog_errno(ret);
		goto out;
	}

	/*
	 * This will instruct ocfs2_claim_suballoc_bits and
	 * ocfs2_search_one_group to search but save actual allocation
	 * for later.
	 */
	ac->ac_find_loc_only = 1;

	ret = ocfs2_claim_suballoc_bits(ac, handle, 1, 1, res);
	if (ret < 0) {
		mlog_errno(ret);
		goto out;
	}

	ac->ac_find_loc_priv = res;
	*fe_blkno = res->sr_blkno;
2097
	ocfs2_update_inode_fsync_trans(handle, dir, 0);
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
out:
	if (handle)
		ocfs2_commit_trans(OCFS2_SB(dir->i_sb), handle);

	if (ret)
		kfree(res);

	return ret;
}

int ocfs2_claim_new_inode_at_loc(handle_t *handle,
				 struct inode *dir,
				 struct ocfs2_alloc_context *ac,
				 u64 *suballoc_loc,
				 u16 *suballoc_bit,
				 u64 di_blkno)
{
	int ret;
	u16 chain;
	struct ocfs2_suballoc_result *res = ac->ac_find_loc_priv;
	struct buffer_head *bg_bh = NULL;
	struct ocfs2_group_desc *bg;
	struct ocfs2_dinode *di = (struct ocfs2_dinode *) ac->ac_bh->b_data;

	/*
	 * Since di_blkno is being passed back in, we check for any
	 * inconsistencies which may have happened between
	 * calls. These are code bugs as di_blkno is not expected to
	 * change once returned from ocfs2_find_new_inode_loc()
	 */
	BUG_ON(res->sr_blkno != di_blkno);

	ret = ocfs2_read_group_descriptor(ac->ac_inode, di,
					  res->sr_bg_stable_blkno, &bg_bh);
	if (ret) {
		mlog_errno(ret);
		goto out;
	}

	bg = (struct ocfs2_group_desc *) bg_bh->b_data;
	chain = le16_to_cpu(bg->bg_chain);

	ret = ocfs2_alloc_dinode_update_counts(ac->ac_inode, handle,
					       ac->ac_bh, res->sr_bits,
					       chain);
	if (ret) {
		mlog_errno(ret);
		goto out;
	}

	ret = ocfs2_block_group_set_bits(handle,
					 ac->ac_inode,
					 bg,
					 bg_bh,
					 res->sr_bit_offset,
					 res->sr_bits);
	if (ret < 0) {
2155 2156
		ocfs2_rollback_alloc_dinode_counts(ac->ac_inode,
					       ac->ac_bh, res->sr_bits, chain);
2157 2158 2159 2160
		mlog_errno(ret);
		goto out;
	}

2161 2162
	trace_ocfs2_claim_new_inode_at_loc((unsigned long long)di_blkno,
					   res->sr_bits);
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178

	atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);

	BUG_ON(res->sr_bits != 1);

	*suballoc_loc = res->sr_bg_blkno;
	*suballoc_bit = res->sr_bit_offset;
	ac->ac_bits_given++;
	ocfs2_save_inode_ac_group(dir, ac);

out:
	brelse(bg_bh);

	return ret;
}

2179
int ocfs2_claim_new_inode(handle_t *handle,
2180 2181
			  struct inode *dir,
			  struct buffer_head *parent_fe_bh,
2182
			  struct ocfs2_alloc_context *ac,
2183
			  u64 *suballoc_loc,
2184 2185 2186 2187
			  u16 *suballoc_bit,
			  u64 *fe_blkno)
{
	int status;
2188
	struct ocfs2_suballoc_result res;
2189 2190 2191 2192 2193 2194

	BUG_ON(!ac);
	BUG_ON(ac->ac_bits_given != 0);
	BUG_ON(ac->ac_bits_wanted != 1);
	BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);

2195 2196
	ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);

2197
	status = ocfs2_claim_suballoc_bits(ac,
2198
					   handle,
2199 2200
					   1,
					   1,
2201
					   &res);
2202 2203 2204 2205
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}
2206
	atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
2207

2208
	BUG_ON(res.sr_bits != 1);
2209

2210
	*suballoc_loc = res.sr_bg_blkno;
2211
	*suballoc_bit = res.sr_bit_offset;
2212
	*fe_blkno = res.sr_blkno;
2213
	ac->ac_bits_given++;
2214
	ocfs2_save_inode_ac_group(dir, ac);
2215 2216
	status = 0;
bail:
Tao Ma's avatar
Tao Ma committed
2217 2218
	if (status)
		mlog_errno(status);
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
	return status;
}

/* translate a group desc. blkno and it's bitmap offset into
 * disk cluster offset. */
static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
						   u64 bg_blkno,
						   u16 bg_bit_off)
{
	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
	u32 cluster = 0;

	BUG_ON(!ocfs2_is_cluster_bitmap(inode));

	if (bg_blkno != osb->first_cluster_group_blkno)
		cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
	cluster += (u32) bg_bit_off;
	return cluster;
}

/* given a cluster offset, calculate which block group it belongs to
 * and return that block offset. */
2241
u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
{
	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
	u32 group_no;

	BUG_ON(!ocfs2_is_cluster_bitmap(inode));

	group_no = cluster / osb->bitmap_cpg;
	if (!group_no)
		return osb->first_cluster_group_blkno;
	return ocfs2_clusters_to_blocks(inode->i_sb,
					group_no * osb->bitmap_cpg);
}

/* given the block number of a cluster start, calculate which cluster
 * group and descriptor bitmap offset that corresponds to. */
static inline void ocfs2_block_to_cluster_group(struct inode *inode,
						u64 data_blkno,
						u64 *bg_blkno,
						u16 *bg_bit_off)
{
	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
	u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);

	BUG_ON(!ocfs2_is_cluster_bitmap(inode));

	*bg_blkno = ocfs2_which_cluster_group(inode,
					      data_cluster);

	if (*bg_blkno == osb->first_cluster_group_blkno)
		*bg_bit_off = (u16) data_cluster;
	else
		*bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
							     data_blkno - *bg_blkno);
}

/*
 * min_bits - minimum contiguous chunk from this total allocation we
 * can handle. set to what we asked for originally for a full
 * contig. allocation, set to '1' to indicate we can deal with extents
 * of any size.
 */
2283
int __ocfs2_claim_clusters(handle_t *handle,
2284 2285 2286 2287 2288
			   struct ocfs2_alloc_context *ac,
			   u32 min_clusters,
			   u32 max_clusters,
			   u32 *cluster_start,
			   u32 *num_clusters)
2289 2290
{
	int status;
2291
	unsigned int bits_wanted = max_clusters;
2292
	struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
2293
	struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
2294 2295 2296 2297 2298 2299 2300

	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);

	BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
	       && ac->ac_which != OCFS2_AC_USE_MAIN);

	if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
2301 2302
		WARN_ON(min_clusters > 1);

2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314
		status = ocfs2_claim_local_alloc_bits(osb,
						      handle,
						      ac,
						      bits_wanted,
						      cluster_start,
						      num_clusters);
		if (!status)
			atomic_inc(&osb->alloc_stats.local_data);
	} else {
		if (min_clusters > (osb->bitmap_cpg - 1)) {
			/* The only paths asking for contiguousness
			 * should know about this already. */
2315 2316 2317
			mlog(ML_ERROR, "minimum allocation requested %u exceeds "
			     "group bitmap size %u!\n", min_clusters,
			     osb->bitmap_cpg);
2318 2319 2320 2321 2322 2323 2324
			status = -ENOSPC;
			goto bail;
		}
		/* clamp the current request down to a realistic size. */
		if (bits_wanted > (osb->bitmap_cpg - 1))
			bits_wanted = osb->bitmap_cpg - 1;

2325
		status = ocfs2_claim_suballoc_bits(ac,
2326
						   handle,
2327 2328
						   bits_wanted,
						   min_clusters,
2329
						   &res);
2330
		if (!status) {
2331
			BUG_ON(res.sr_blkno); /* cluster alloc can't set */
2332 2333
			*cluster_start =
				ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
2334 2335
								 res.sr_bg_blkno,
								 res.sr_bit_offset);
2336
			atomic_inc(&osb->alloc_stats.bitmap_data);
2337
			*num_clusters = res.sr_bits;
2338 2339 2340 2341 2342 2343 2344 2345
		}
	}
	if (status < 0) {
		if (status != -ENOSPC)
			mlog_errno(status);
		goto bail;
	}

2346
	ac->ac_bits_given += *num_clusters;
2347 2348

bail:
Tao Ma's avatar
Tao Ma committed
2349 2350
	if (status)
		mlog_errno(status);
2351 2352 2353
	return status;
}

2354
int ocfs2_claim_clusters(handle_t *handle,
2355 2356 2357 2358 2359 2360 2361
			 struct ocfs2_alloc_context *ac,
			 u32 min_clusters,
			 u32 *cluster_start,
			 u32 *num_clusters)
{
	unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;

2362
	return __ocfs2_claim_clusters(handle, ac, min_clusters,
2363 2364 2365
				      bits_wanted, cluster_start, num_clusters);
}

2366 2367 2368 2369 2370 2371 2372 2373
static int ocfs2_block_group_clear_bits(handle_t *handle,
					struct inode *alloc_inode,
					struct ocfs2_group_desc *bg,
					struct buffer_head *group_bh,
					unsigned int bit_off,
					unsigned int num_bits,
					void (*undo_fn)(unsigned int bit,
							unsigned long *bmap))
2374 2375 2376 2377 2378
{
	int status;
	unsigned int tmp;
	struct ocfs2_group_desc *undo_bg = NULL;

2379 2380 2381
	/* The caller got this descriptor from
	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
2382

2383
	trace_ocfs2_block_group_clear_bits(bit_off, num_bits);
2384

2385
	BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
2386
	status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
2387 2388 2389 2390
					 group_bh,
					 undo_fn ?
					 OCFS2_JOURNAL_ACCESS_UNDO :
					 OCFS2_JOURNAL_ACCESS_WRITE);
2391 2392 2393 2394 2395
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}

2396
	if (undo_fn) {
2397 2398 2399 2400 2401
		jbd_lock_bh_state(group_bh);
		undo_bg = (struct ocfs2_group_desc *)
					bh2jh(group_bh)->b_committed_data;
		BUG_ON(!undo_bg);
	}
2402 2403 2404 2405 2406

	tmp = num_bits;
	while(tmp--) {
		ocfs2_clear_bit((bit_off + tmp),
				(unsigned long *) bg->bg_bitmap);
2407 2408 2409
		if (undo_fn)
			undo_fn(bit_off + tmp,
				(unsigned long *) undo_bg->bg_bitmap);
2410 2411
	}
	le16_add_cpu(&bg->bg_free_bits_count, num_bits);
2412
	if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
2413 2414 2415 2416 2417
		return ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit count %u but claims %u are freed. num_bits %d\n",
				   (unsigned long long)le64_to_cpu(bg->bg_blkno),
				   le16_to_cpu(bg->bg_bits),
				   le16_to_cpu(bg->bg_free_bits_count),
				   num_bits);
2418
	}
2419

2420
	if (undo_fn)
2421 2422
		jbd_unlock_bh_state(group_bh);

2423
	ocfs2_journal_dirty(handle, group_bh);
2424 2425 2426 2427 2428 2429 2430
bail:
	return status;
}

/*
 * expects the suballoc inode to already be locked.
 */
2431 2432 2433 2434 2435 2436 2437 2438
static int _ocfs2_free_suballoc_bits(handle_t *handle,
				     struct inode *alloc_inode,
				     struct buffer_head *alloc_bh,
				     unsigned int start_bit,
				     u64 bg_blkno,
				     unsigned int count,
				     void (*undo_fn)(unsigned int bit,
						     unsigned long *bitmap))
2439 2440 2441 2442 2443 2444 2445 2446
{
	int status = 0;
	u32 tmp_used;
	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
	struct ocfs2_chain_list *cl = &fe->id2.i_chain;
	struct buffer_head *group_bh = NULL;
	struct ocfs2_group_desc *group;

2447 2448 2449
	/* The alloc_bh comes from ocfs2_free_dinode() or
	 * ocfs2_free_clusters().  The callers have all locked the
	 * allocator and gotten alloc_bh from the lock call.  This
Lucas De Marchi's avatar
Lucas De Marchi committed
2450
	 * validates the dinode buffer.  Any corruption that has happened
2451 2452
	 * is a code bug. */
	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
2453 2454
	BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));

2455 2456 2457 2458
	trace_ocfs2_free_suballoc_bits(
		(unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
		(unsigned long long)bg_blkno,
		start_bit, count);
2459

2460 2461
	status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
					     &group_bh);
2462 2463 2464 2465 2466
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}
	group = (struct ocfs2_group_desc *) group_bh->b_data;
2467

2468 2469 2470 2471
	BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));

	status = ocfs2_block_group_clear_bits(handle, alloc_inode,
					      group, group_bh,
2472
					      start_bit, count, undo_fn);
2473 2474 2475 2476 2477
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}

2478 2479
	status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
					 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
2480 2481
	if (status < 0) {
		mlog_errno(status);
2482 2483
		ocfs2_block_group_set_bits(handle, alloc_inode, group, group_bh,
				start_bit, count);
2484 2485 2486 2487 2488 2489 2490
		goto bail;
	}

	le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
		     count);
	tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
	fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
2491
	ocfs2_journal_dirty(handle, alloc_bh);
2492 2493

bail:
2494
	brelse(group_bh);
2495

Tao Ma's avatar
Tao Ma committed
2496 2497
	if (status)
		mlog_errno(status);
2498 2499 2500
	return status;
}

2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511
int ocfs2_free_suballoc_bits(handle_t *handle,
			     struct inode *alloc_inode,
			     struct buffer_head *alloc_bh,
			     unsigned int start_bit,
			     u64 bg_blkno,
			     unsigned int count)
{
	return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
					 start_bit, bg_blkno, count, NULL);
}

2512
int ocfs2_free_dinode(handle_t *handle,
2513 2514 2515 2516 2517 2518 2519 2520
		      struct inode *inode_alloc_inode,
		      struct buffer_head *inode_alloc_bh,
		      struct ocfs2_dinode *di)
{
	u64 blk = le64_to_cpu(di->i_blkno);
	u16 bit = le16_to_cpu(di->i_suballoc_bit);
	u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);

2521 2522
	if (di->i_suballoc_loc)
		bg_blkno = le64_to_cpu(di->i_suballoc_loc);
2523 2524 2525 2526
	return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
					inode_alloc_bh, bit, bg_blkno, 1);
}

2527 2528 2529 2530 2531 2532 2533
static int _ocfs2_free_clusters(handle_t *handle,
				struct inode *bitmap_inode,
				struct buffer_head *bitmap_bh,
				u64 start_blk,
				unsigned int num_clusters,
				void (*undo_fn)(unsigned int bit,
						unsigned long *bitmap))
2534 2535 2536 2537 2538 2539 2540 2541
{
	int status;
	u16 bg_start_bit;
	u64 bg_blkno;
	struct ocfs2_dinode *fe;

	/* You can't ever have a contiguous set of clusters
	 * bigger than a block group bitmap so we never have to worry
Tao Ma's avatar
Tao Ma committed
2542 2543
	 * about looping on them.
	 * This is expensive. We can safely remove once this stuff has
2544 2545 2546 2547 2548 2549 2550 2551
	 * gotten tested really well. */
	BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));

	fe = (struct ocfs2_dinode *) bitmap_bh->b_data;

	ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
				     &bg_start_bit);

2552 2553 2554
	trace_ocfs2_free_clusters((unsigned long long)bg_blkno,
			(unsigned long long)start_blk,
			bg_start_bit, num_clusters);
2555

2556 2557 2558
	status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
					   bg_start_bit, bg_blkno,
					   num_clusters, undo_fn);
2559
	if (status < 0) {
2560
		mlog_errno(status);
2561 2562 2563 2564 2565
		goto out;
	}

	ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
					 num_clusters);
2566

2567
out:
Tao Ma's avatar
Tao Ma committed
2568 2569
	if (status)
		mlog_errno(status);
2570 2571 2572
	return status;
}

2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598
int ocfs2_free_clusters(handle_t *handle,
			struct inode *bitmap_inode,
			struct buffer_head *bitmap_bh,
			u64 start_blk,
			unsigned int num_clusters)
{
	return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
				    start_blk, num_clusters,
				    _ocfs2_set_bit);
}

/*
 * Give never-used clusters back to the global bitmap.  We don't need
 * to protect these bits in the undo buffer.
 */
int ocfs2_release_clusters(handle_t *handle,
			   struct inode *bitmap_inode,
			   struct buffer_head *bitmap_bh,
			   u64 start_blk,
			   unsigned int num_clusters)
{
	return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
				    start_blk, num_clusters,
				    _ocfs2_clear_bit);
}

2599 2600 2601 2602 2603 2604 2605 2606 2607
static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
{
	printk("Block Group:\n");
	printk("bg_signature:       %s\n", bg->bg_signature);
	printk("bg_size:            %u\n", bg->bg_size);
	printk("bg_bits:            %u\n", bg->bg_bits);
	printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
	printk("bg_chain:           %u\n", bg->bg_chain);
	printk("bg_generation:      %u\n", le32_to_cpu(bg->bg_generation));
2608 2609 2610 2611 2612 2613
	printk("bg_next_group:      %llu\n",
	       (unsigned long long)bg->bg_next_group);
	printk("bg_parent_dinode:   %llu\n",
	       (unsigned long long)bg->bg_parent_dinode);
	printk("bg_blkno:           %llu\n",
	       (unsigned long long)bg->bg_blkno);
2614 2615 2616 2617 2618 2619
}

static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
{
	int i;

2620
	printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
2621
	printk("i_signature:                  %s\n", fe->i_signature);
2622 2623
	printk("i_size:                       %llu\n",
	       (unsigned long long)fe->i_size);
2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640
	printk("i_clusters:                   %u\n", fe->i_clusters);
	printk("i_generation:                 %u\n",
	       le32_to_cpu(fe->i_generation));
	printk("id1.bitmap1.i_used:           %u\n",
	       le32_to_cpu(fe->id1.bitmap1.i_used));
	printk("id1.bitmap1.i_total:          %u\n",
	       le32_to_cpu(fe->id1.bitmap1.i_total));
	printk("id2.i_chain.cl_cpg:           %u\n", fe->id2.i_chain.cl_cpg);
	printk("id2.i_chain.cl_bpc:           %u\n", fe->id2.i_chain.cl_bpc);
	printk("id2.i_chain.cl_count:         %u\n", fe->id2.i_chain.cl_count);
	printk("id2.i_chain.cl_next_free_rec: %u\n",
	       fe->id2.i_chain.cl_next_free_rec);
	for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
		printk("fe->id2.i_chain.cl_recs[%d].c_free:  %u\n", i,
		       fe->id2.i_chain.cl_recs[i].c_free);
		printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
		       fe->id2.i_chain.cl_recs[i].c_total);
2641 2642
		printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
		       (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
2643 2644
	}
}
2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655

/*
 * For a given allocation, determine which allocators will need to be
 * accessed, and lock them, reserving the appropriate number of bits.
 *
 * Sparse file systems call this from ocfs2_write_begin_nolock()
 * and ocfs2_allocate_unwritten_extents().
 *
 * File systems which don't support holes call this from
 * ocfs2_extend_allocation().
 */
2656 2657
int ocfs2_lock_allocators(struct inode *inode,
			  struct ocfs2_extent_tree *et,
2658 2659
			  u32 clusters_to_add, u32 extents_to_split,
			  struct ocfs2_alloc_context **data_ac,
2660
			  struct ocfs2_alloc_context **meta_ac)
2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671
{
	int ret = 0, num_free_extents;
	unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);

	*meta_ac = NULL;
	if (data_ac)
		*data_ac = NULL;

	BUG_ON(clusters_to_add != 0 && data_ac == NULL);

2672
	num_free_extents = ocfs2_num_free_extents(osb, et);
2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693
	if (num_free_extents < 0) {
		ret = num_free_extents;
		mlog_errno(ret);
		goto out;
	}

	/*
	 * Sparse allocation file systems need to be more conservative
	 * with reserving room for expansion - the actual allocation
	 * happens while we've got a journal handle open so re-taking
	 * a cluster lock (because we ran out of room for another
	 * extent) will violate ordering rules.
	 *
	 * Most of the time we'll only be seeing this 1 cluster at a time
	 * anyway.
	 *
	 * Always lock for any unwritten extents - we might want to
	 * add blocks during a split.
	 */
	if (!num_free_extents ||
	    (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
2694
		ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725
		if (ret < 0) {
			if (ret != -ENOSPC)
				mlog_errno(ret);
			goto out;
		}
	}

	if (clusters_to_add == 0)
		goto out;

	ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
	if (ret < 0) {
		if (ret != -ENOSPC)
			mlog_errno(ret);
		goto out;
	}

out:
	if (ret) {
		if (*meta_ac) {
			ocfs2_free_alloc_context(*meta_ac);
			*meta_ac = NULL;
		}

		/*
		 * We cannot have an error and a non null *data_ac.
		 */
	}

	return ret;
}
2726 2727 2728 2729 2730 2731

/*
 * Read the inode specified by blkno to get suballoc_slot and
 * suballoc_bit.
 */
static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2732 2733
				       u16 *suballoc_slot, u64 *group_blkno,
				       u16 *suballoc_bit)
2734 2735 2736 2737 2738
{
	int status;
	struct buffer_head *inode_bh = NULL;
	struct ocfs2_dinode *inode_fe;

2739
	trace_ocfs2_get_suballoc_slot_bit((unsigned long long)blkno);
2740 2741 2742 2743

	/* dirty read disk */
	status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
	if (status < 0) {
2744 2745
		mlog(ML_ERROR, "read block %llu failed %d\n",
		     (unsigned long long)blkno, status);
2746 2747 2748 2749 2750
		goto bail;
	}

	inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
	if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
2751 2752
		mlog(ML_ERROR, "invalid inode %llu requested\n",
		     (unsigned long long)blkno);
2753 2754 2755 2756
		status = -EINVAL;
		goto bail;
	}

2757
	if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
2758 2759
	    (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
		mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
2760 2761
		     (unsigned long long)blkno,
		     (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
2762 2763 2764 2765 2766 2767 2768 2769
		status = -EINVAL;
		goto bail;
	}

	if (suballoc_slot)
		*suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
	if (suballoc_bit)
		*suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2770 2771
	if (group_blkno)
		*group_blkno = le64_to_cpu(inode_fe->i_suballoc_loc);
2772 2773 2774 2775

bail:
	brelse(inode_bh);

Tao Ma's avatar
Tao Ma committed
2776 2777
	if (status)
		mlog_errno(status);
2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789
	return status;
}

/*
 * test whether bit is SET in allocator bitmap or not.  on success, 0
 * is returned and *res is 1 for SET; 0 otherwise.  when fails, errno
 * is returned and *res is meaningless.  Call this after you have
 * cluster locked against suballoc, or you may get a result based on
 * non-up2date contents
 */
static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
				   struct inode *suballoc,
2790 2791
				   struct buffer_head *alloc_bh,
				   u64 group_blkno, u64 blkno,
2792 2793
				   u16 bit, int *res)
{
2794
	struct ocfs2_dinode *alloc_di;
2795 2796 2797 2798 2799
	struct ocfs2_group_desc *group;
	struct buffer_head *group_bh = NULL;
	u64 bg_blkno;
	int status;

2800 2801
	trace_ocfs2_test_suballoc_bit((unsigned long long)blkno,
				      (unsigned int)bit);
2802

2803 2804
	alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data;
	if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) {
2805 2806
		mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
		     (unsigned int)bit,
2807
		     ocfs2_bits_per_group(&alloc_di->id2.i_chain));
2808 2809 2810 2811
		status = -EINVAL;
		goto bail;
	}

2812 2813
	bg_blkno = group_blkno ? group_blkno :
		   ocfs2_which_suballoc_group(blkno, bit);
2814
	status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno,
2815 2816
					     &group_bh);
	if (status < 0) {
2817 2818
		mlog(ML_ERROR, "read group %llu failed %d\n",
		     (unsigned long long)bg_blkno, status);
2819 2820 2821 2822 2823 2824 2825 2826 2827
		goto bail;
	}

	group = (struct ocfs2_group_desc *) group_bh->b_data;
	*res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);

bail:
	brelse(group_bh);

Tao Ma's avatar
Tao Ma committed
2828 2829
	if (status)
		mlog_errno(status);
2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848
	return status;
}

/*
 * Test if the bit representing this inode (blkno) is set in the
 * suballocator.
 *
 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
 *
 * In the event of failure, a negative value is returned and *res is
 * meaningless.
 *
 * Callers must make sure to hold nfs_sync_lock to prevent
 * ocfs2_delete_inode() on another node from accessing the same
 * suballocator concurrently.
 */
int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
{
	int status;
2849
	u64 group_blkno = 0;
2850 2851 2852 2853
	u16 suballoc_bit = 0, suballoc_slot = 0;
	struct inode *inode_alloc_inode;
	struct buffer_head *alloc_bh = NULL;

2854
	trace_ocfs2_test_inode_bit((unsigned long long)blkno);
2855 2856

	status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2857
					     &group_blkno, &suballoc_bit);
2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878
	if (status < 0) {
		mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
		goto bail;
	}

	inode_alloc_inode =
		ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
					    suballoc_slot);
	if (!inode_alloc_inode) {
		/* the error code could be inaccurate, but we are not able to
		 * get the correct one. */
		status = -EINVAL;
		mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
		     (u32)suballoc_slot);
		goto bail;
	}

	mutex_lock(&inode_alloc_inode->i_mutex);
	status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
	if (status < 0) {
		mutex_unlock(&inode_alloc_inode->i_mutex);
2879
		iput(inode_alloc_inode);
2880 2881 2882 2883 2884 2885
		mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
		     (u32)suballoc_slot, status);
		goto bail;
	}

	status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2886
					 group_blkno, blkno, suballoc_bit, res);
2887 2888 2889 2890 2891 2892 2893 2894 2895
	if (status < 0)
		mlog(ML_ERROR, "test suballoc bit failed %d\n", status);

	ocfs2_inode_unlock(inode_alloc_inode, 0);
	mutex_unlock(&inode_alloc_inode->i_mutex);

	iput(inode_alloc_inode);
	brelse(alloc_bh);
bail:
Tao Ma's avatar
Tao Ma committed
2896 2897
	if (status)
		mlog_errno(status);
2898 2899
	return status;
}