rgrp.c 73.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
David Teigland's avatar
David Teigland committed
2 3
/*
 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
4
 * Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved.
David Teigland's avatar
David Teigland committed
5 6
 */

7 8
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

David Teigland's avatar
David Teigland committed
9 10 11 12
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/completion.h>
#include <linux/buffer_head.h>
13
#include <linux/fs.h>
14
#include <linux/gfs2_ondisk.h>
15
#include <linux/prefetch.h>
16
#include <linux/blkdev.h>
17
#include <linux/rbtree.h>
18
#include <linux/random.h>
David Teigland's avatar
David Teigland committed
19 20

#include "gfs2.h"
21
#include "incore.h"
David Teigland's avatar
David Teigland committed
22 23 24 25 26 27 28 29
#include "glock.h"
#include "glops.h"
#include "lops.h"
#include "meta_io.h"
#include "quota.h"
#include "rgrp.h"
#include "super.h"
#include "trans.h"
30
#include "util.h"
31
#include "log.h"
32
#include "inode.h"
Steven Whitehouse's avatar
Steven Whitehouse committed
33
#include "trace_gfs2.h"
34
#include "dir.h"
David Teigland's avatar
David Teigland committed
35

Steven Whitehouse's avatar
Steven Whitehouse committed
36
#define BFITNOENT ((u32)~0)
37
#define NO_BLOCK ((u64)~0)
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
struct gfs2_rbm {
	struct gfs2_rgrpd *rgd;
	u32 offset;		/* The offset is bitmap relative */
	int bii;		/* Bitmap index */
};

static inline struct gfs2_bitmap *rbm_bi(const struct gfs2_rbm *rbm)
{
	return rbm->rgd->rd_bits + rbm->bii;
}

static inline u64 gfs2_rbm_to_block(const struct gfs2_rbm *rbm)
{
	BUG_ON(rbm->offset >= rbm->rgd->rd_data);
	return rbm->rgd->rd_data0 + (rbm_bi(rbm)->bi_start * GFS2_NBBY) +
		rbm->offset;
}

57 58 59
/*
 * These routines are used by the resource group routines (rgrp.c)
 * to keep track of block allocation.  Each block is represented by two
60 61 62 63 64 65
 * bits.  So, each byte represents GFS2_NBBY (i.e. 4) blocks.
 *
 * 0 = Free
 * 1 = Used (not metadata)
 * 2 = Unlinked (still in use) inode
 * 3 = Used (metadata)
66 67
 */

68 69 70 71 72
struct gfs2_extent {
	struct gfs2_rbm rbm;
	u32 len;
};

73 74
static const char valid_change[16] = {
	        /* current */
75
	/* n */ 0, 1, 1, 1,
76
	/* e */ 1, 0, 0, 0,
77
	/* w */ 0, 0, 0, 1,
78 79 80
	        1, 0, 0, 0
};

81
static int gfs2_rbm_find(struct gfs2_rbm *rbm, u8 state, u32 *minext,
82
			 struct gfs2_blkreserv *rs, bool nowrap);
83 84


85 86
/**
 * gfs2_setbit - Set a bit in the bitmaps
87 88
 * @rbm: The position of the bit to set
 * @do_clone: Also set the clone bitmap, if it exists
89 90 91 92
 * @new_state: the new state of the block
 *
 */

93
static inline void gfs2_setbit(const struct gfs2_rbm *rbm, bool do_clone,
94
			       unsigned char new_state)
95
{
96
	unsigned char *byte1, *byte2, *end, cur_state;
Bob Peterson's avatar
Bob Peterson committed
97
	struct gfs2_bitmap *bi = rbm_bi(rbm);
98
	unsigned int buflen = bi->bi_bytes;
99
	const unsigned int bit = (rbm->offset % GFS2_NBBY) * GFS2_BIT_SIZE;
100

Bob Peterson's avatar
Bob Peterson committed
101 102
	byte1 = bi->bi_bh->b_data + bi->bi_offset + (rbm->offset / GFS2_NBBY);
	end = bi->bi_bh->b_data + bi->bi_offset + buflen;
103

104
	BUG_ON(byte1 >= end);
105

106
	cur_state = (*byte1 >> bit) & GFS2_BIT_MASK;
107

108
	if (unlikely(!valid_change[new_state * 4 + cur_state])) {
109 110 111
		struct gfs2_sbd *sdp = rbm->rgd->rd_sbd;

		fs_warn(sdp, "buf_blk = 0x%x old_state=%d, new_state=%d\n",
112
			rbm->offset, cur_state, new_state);
113 114 115
		fs_warn(sdp, "rgrp=0x%llx bi_start=0x%x biblk: 0x%llx\n",
			(unsigned long long)rbm->rgd->rd_addr, bi->bi_start,
			(unsigned long long)bi->bi_bh->b_blocknr);
116 117
		fs_warn(sdp, "bi_offset=0x%x bi_bytes=0x%x block=0x%llx\n",
			bi->bi_offset, bi->bi_bytes,
118
			(unsigned long long)gfs2_rbm_to_block(rbm));
119
		dump_stack();
120
		gfs2_consist_rgrpd(rbm->rgd);
121 122 123 124
		return;
	}
	*byte1 ^= (cur_state ^ new_state) << bit;

Bob Peterson's avatar
Bob Peterson committed
125 126
	if (do_clone && bi->bi_clone) {
		byte2 = bi->bi_clone + bi->bi_offset + (rbm->offset / GFS2_NBBY);
127 128 129
		cur_state = (*byte2 >> bit) & GFS2_BIT_MASK;
		*byte2 ^= (cur_state ^ new_state) << bit;
	}
130 131 132 133
}

/**
 * gfs2_testbit - test a bit in the bitmaps
134
 * @rbm: The bit to test
135 136 137 138
 * @use_clone: If true, test the clone bitmap, not the official bitmap.
 *
 * Some callers like gfs2_unaligned_extlen need to test the clone bitmaps,
 * not the "real" bitmaps, to avoid allocating recently freed blocks.
139
 *
140
 * Returns: The two bit block state of the requested bit
141 142
 */

143
static inline u8 gfs2_testbit(const struct gfs2_rbm *rbm, bool use_clone)
144
{
Bob Peterson's avatar
Bob Peterson committed
145
	struct gfs2_bitmap *bi = rbm_bi(rbm);
146
	const u8 *buffer;
147
	const u8 *byte;
148 149
	unsigned int bit;

150 151 152 153 154
	if (use_clone && bi->bi_clone)
		buffer = bi->bi_clone;
	else
		buffer = bi->bi_bh->b_data;
	buffer += bi->bi_offset;
155 156
	byte = buffer + (rbm->offset / GFS2_NBBY);
	bit = (rbm->offset % GFS2_NBBY) * GFS2_BIT_SIZE;
157

158
	return (*byte >> bit) & GFS2_BIT_MASK;
159 160
}

161
/**
162
 * gfs2_bit_search - search bitmap for a state
163 164 165 166
 * @ptr: Pointer to bitmap data
 * @mask: Mask to use (normally 0x55555.... but adjusted for search start)
 * @state: The state we are searching for
 *
167 168
 * We xor the bitmap data with a pattern which is the bitwise opposite
 * of what we are looking for. This gives rise to a pattern of ones
169 170 171 172 173 174 175 176 177 178 179 180 181 182
 * wherever there is a match. Since we have two bits per entry, we
 * take this pattern, shift it down by one place and then and it with
 * the original. All the even bit positions (0,2,4, etc) then represent
 * successful matches, so we mask with 0x55555..... to remove the unwanted
 * odd bit positions.
 *
 * This allows searching of a whole u64 at once (32 blocks) with a
 * single test (on 64 bit arches).
 */

static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state)
{
	u64 tmp;
	static const u64 search[] = {
183 184 185 186
		[0] = 0xffffffffffffffffULL,
		[1] = 0xaaaaaaaaaaaaaaaaULL,
		[2] = 0x5555555555555555ULL,
		[3] = 0x0000000000000000ULL,
187 188 189 190 191 192 193
	};
	tmp = le64_to_cpu(*ptr) ^ search[state];
	tmp &= (tmp >> 1);
	tmp &= mask;
	return tmp;
}

194 195
/**
 * rs_cmp - multi-block reservation range compare
196
 * @start: start of the new reservation
197 198 199 200 201 202 203
 * @len: number of blocks in the new reservation
 * @rs: existing reservation to compare against
 *
 * returns: 1 if the block range is beyond the reach of the reservation
 *         -1 if the block range is before the start of the reservation
 *          0 if the block range overlaps with the reservation
 */
204
static inline int rs_cmp(u64 start, u32 len, struct gfs2_blkreserv *rs)
205
{
206
	if (start >= rs->rs_start + rs->rs_requested)
207
		return 1;
208
	if (rs->rs_start >= start + len)
209 210 211 212
		return -1;
	return 0;
}

213 214 215
/**
 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
 *       a block in a given allocation state.
216
 * @buf: the buffer that holds the bitmaps
217
 * @len: the length (in bytes) of the buffer
218
 * @goal: start search at this block's bit-pair (within @buffer)
219
 * @state: GFS2_BLKST_XXX the state of the block we're looking for.
220 221 222
 *
 * Scope of @goal and returned block number is only within this bitmap buffer,
 * not entire rgrp or filesystem.  @buffer will be offset from the actual
223 224 225 226 227
 * beginning of a bitmap block buffer, skipping any header structures, but
 * headers are always a multiple of 64 bits long so that the buffer is
 * always aligned to a 64 bit boundary.
 *
 * The size of the buffer is in bytes, but is it assumed that it is
228
 * always ok to read a complete multiple of 64 bits at the end
229
 * of the block in case the end is no aligned to a natural boundary.
230 231 232 233
 *
 * Return: the block number (bitmap buffer scope) that was found
 */

234 235
static u32 gfs2_bitfit(const u8 *buf, const unsigned int len,
		       u32 goal, u8 state)
236
{
237 238 239 240
	u32 spoint = (goal << 1) & ((8*sizeof(u64)) - 1);
	const __le64 *ptr = ((__le64 *)buf) + (goal >> 5);
	const __le64 *end = (__le64 *)(buf + ALIGN(len, sizeof(u64)));
	u64 tmp;
241
	u64 mask = 0x5555555555555555ULL;
242 243 244 245 246 247 248
	u32 bit;

	/* Mask off bits we don't care about at the start of the search */
	mask <<= spoint;
	tmp = gfs2_bit_search(ptr, mask, state);
	ptr++;
	while(tmp == 0 && ptr < end) {
249
		tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state);
250
		ptr++;
251
	}
252 253 254 255 256 257 258
	/* Mask off any bits which are more than len bytes from the start */
	if (ptr == end && (len & (sizeof(u64) - 1)))
		tmp &= (((u64)~0) >> (64 - 8*(len & (sizeof(u64) - 1))));
	/* Didn't find anything, so return */
	if (tmp == 0)
		return BFITNOENT;
	ptr--;
259
	bit = __ffs64(tmp);
260 261
	bit /= 2;	/* two bits per entry in the bitmap */
	return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit;
262 263
}

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
/**
 * gfs2_rbm_from_block - Set the rbm based upon rgd and block number
 * @rbm: The rbm with rgd already set correctly
 * @block: The block number (filesystem relative)
 *
 * This sets the bi and offset members of an rbm based on a
 * resource group and a filesystem relative block number. The
 * resource group must be set in the rbm on entry, the bi and
 * offset members will be set by this function.
 *
 * Returns: 0 on success, or an error code
 */

static int gfs2_rbm_from_block(struct gfs2_rbm *rbm, u64 block)
{
279
	if (!rgrp_contains_block(rbm->rgd, block))
280
		return -E2BIG;
Bob Peterson's avatar
Bob Peterson committed
281
	rbm->bii = 0;
282
	rbm->offset = block - rbm->rgd->rd_data0;
283
	/* Check if the block is within the first block */
Bob Peterson's avatar
Bob Peterson committed
284
	if (rbm->offset < rbm_bi(rbm)->bi_blocks)
285 286 287 288 289
		return 0;

	/* Adjust for the size diff between gfs2_meta_header and gfs2_rgrp */
	rbm->offset += (sizeof(struct gfs2_rgrp) -
			sizeof(struct gfs2_meta_header)) * GFS2_NBBY;
Bob Peterson's avatar
Bob Peterson committed
290 291
	rbm->bii = rbm->offset / rbm->rgd->rd_sbd->sd_blocks_per_bitmap;
	rbm->offset -= rbm->bii * rbm->rgd->rd_sbd->sd_blocks_per_bitmap;
292 293 294
	return 0;
}

295
/**
296
 * gfs2_rbm_add - add a number of blocks to an rbm
297
 * @rbm: The rbm with rgd already set correctly
298
 * @blocks: The number of blocks to add to rpm
299
 *
300 301
 * This function takes an existing rbm structure and adds a number of blocks to
 * it.
302
 *
303
 * Returns: True if the new rbm would point past the end of the rgrp.
304 305
 */

306
static bool gfs2_rbm_add(struct gfs2_rbm *rbm, u32 blocks)
307
{
308 309 310 311 312
	struct gfs2_rgrpd *rgd = rbm->rgd;
	struct gfs2_bitmap *bi = rgd->rd_bits + rbm->bii;

	if (rbm->offset + blocks < bi->bi_blocks) {
		rbm->offset += blocks;
313 314
		return false;
	}
315
	blocks -= bi->bi_blocks - rbm->offset;
316

317 318 319 320 321 322 323 324 325 326 327
	for(;;) {
		bi++;
		if (bi == rgd->rd_bits + rgd->rd_length)
			return true;
		if (blocks < bi->bi_blocks) {
			rbm->offset = blocks;
			rbm->bii = bi - rgd->rd_bits;
			return false;
		}
		blocks -= bi->bi_blocks;
	}
328 329
}

330 331 332 333 334 335
/**
 * gfs2_unaligned_extlen - Look for free blocks which are not byte aligned
 * @rbm: Position to search (value/result)
 * @n_unaligned: Number of unaligned blocks to check
 * @len: Decremented for each block found (terminate on zero)
 *
336 337
 * Returns: true if a non-free block is encountered or the end of the resource
 *	    group is reached.
338 339 340 341 342 343 344 345
 */

static bool gfs2_unaligned_extlen(struct gfs2_rbm *rbm, u32 n_unaligned, u32 *len)
{
	u32 n;
	u8 res;

	for (n = 0; n < n_unaligned; n++) {
346
		res = gfs2_testbit(rbm, true);
347 348 349 350 351
		if (res != GFS2_BLKST_FREE)
			return true;
		(*len)--;
		if (*len == 0)
			return true;
352
		if (gfs2_rbm_add(rbm, 1))
353 354 355 356 357 358 359 360
			return true;
	}

	return false;
}

/**
 * gfs2_free_extlen - Return extent length of free blocks
361
 * @rrbm: Starting position
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
 * @len: Max length to check
 *
 * Starting at the block specified by the rbm, see how many free blocks
 * there are, not reading more than len blocks ahead. This can be done
 * using memchr_inv when the blocks are byte aligned, but has to be done
 * on a block by block basis in case of unaligned blocks. Also this
 * function can cope with bitmap boundaries (although it must stop on
 * a resource group boundary)
 *
 * Returns: Number of free blocks in the extent
 */

static u32 gfs2_free_extlen(const struct gfs2_rbm *rrbm, u32 len)
{
	struct gfs2_rbm rbm = *rrbm;
	u32 n_unaligned = rbm.offset & 3;
	u32 size = len;
	u32 bytes;
	u32 chunk_size;
	u8 *ptr, *start, *end;
	u64 block;
Bob Peterson's avatar
Bob Peterson committed
383
	struct gfs2_bitmap *bi;
384 385 386 387 388

	if (n_unaligned &&
	    gfs2_unaligned_extlen(&rbm, 4 - n_unaligned, &len))
		goto out;

389
	n_unaligned = len & 3;
390 391
	/* Start is now byte aligned */
	while (len > 3) {
Bob Peterson's avatar
Bob Peterson committed
392 393 394 395 396
		bi = rbm_bi(&rbm);
		start = bi->bi_bh->b_data;
		if (bi->bi_clone)
			start = bi->bi_clone;
		start += bi->bi_offset;
397
		end = start + bi->bi_bytes;
398 399 400 401 402 403 404 405 406
		BUG_ON(rbm.offset & 3);
		start += (rbm.offset / GFS2_NBBY);
		bytes = min_t(u32, len / GFS2_NBBY, (end - start));
		ptr = memchr_inv(start, 0, bytes);
		chunk_size = ((ptr == NULL) ? bytes : (ptr - start));
		chunk_size *= GFS2_NBBY;
		BUG_ON(len < chunk_size);
		len -= chunk_size;
		block = gfs2_rbm_to_block(&rbm);
407 408
		if (gfs2_rbm_from_block(&rbm, block + chunk_size)) {
			n_unaligned = 0;
409
			break;
410 411 412 413 414
		}
		if (ptr) {
			n_unaligned = 3;
			break;
		}
415 416 417 418 419 420 421 422 423 424
		n_unaligned = len & 3;
	}

	/* Deal with any bits left over at the end */
	if (n_unaligned)
		gfs2_unaligned_extlen(&rbm, n_unaligned, &len);
out:
	return size - len;
}

425 426
/**
 * gfs2_bitcount - count the number of bits in a certain state
427
 * @rgd: the resource group descriptor
428 429 430 431 432 433 434
 * @buffer: the buffer that holds the bitmaps
 * @buflen: the length (in bytes) of the buffer
 * @state: the state of the block we're looking for
 *
 * Returns: The number of bits
 */

435 436
static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
			 unsigned int buflen, u8 state)
437
{
438 439 440 441 442
	const u8 *byte = buffer;
	const u8 *end = buffer + buflen;
	const u8 state1 = state << 2;
	const u8 state2 = state << 4;
	const u8 state3 = state << 6;
443
	u32 count = 0;
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458

	for (; byte < end; byte++) {
		if (((*byte) & 0x03) == state)
			count++;
		if (((*byte) & 0x0C) == state1)
			count++;
		if (((*byte) & 0x30) == state2)
			count++;
		if (((*byte) & 0xC0) == state3)
			count++;
	}

	return count;
}

David Teigland's avatar
David Teigland committed
459 460 461 462 463 464 465 466 467 468
/**
 * gfs2_rgrp_verify - Verify that a resource group is consistent
 * @rgd: the rgrp
 *
 */

void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
{
	struct gfs2_sbd *sdp = rgd->rd_sbd;
	struct gfs2_bitmap *bi = NULL;
469
	u32 length = rgd->rd_length;
470
	u32 count[4], tmp;
David Teigland's avatar
David Teigland committed
471 472
	int buf, x;

473
	memset(count, 0, 4 * sizeof(u32));
David Teigland's avatar
David Teigland committed
474 475 476 477 478 479 480 481

	/* Count # blocks in each of 4 possible allocation states */
	for (buf = 0; buf < length; buf++) {
		bi = rgd->rd_bits + buf;
		for (x = 0; x < 4; x++)
			count[x] += gfs2_bitcount(rgd,
						  bi->bi_bh->b_data +
						  bi->bi_offset,
482
						  bi->bi_bytes, x);
David Teigland's avatar
David Teigland committed
483 484
	}

485
	if (count[0] != rgd->rd_free) {
486 487 488
		gfs2_lm(sdp, "free data mismatch:  %u != %u\n",
			count[0], rgd->rd_free);
		gfs2_consist_rgrpd(rgd);
David Teigland's avatar
David Teigland committed
489 490 491
		return;
	}

492
	tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes;
493
	if (count[1] != tmp) {
494 495 496
		gfs2_lm(sdp, "used data mismatch:  %u != %u\n",
			count[1], tmp);
		gfs2_consist_rgrpd(rgd);
David Teigland's avatar
David Teigland committed
497 498 499
		return;
	}

500
	if (count[2] + count[3] != rgd->rd_dinodes) {
501 502 503
		gfs2_lm(sdp, "used metadata mismatch:  %u != %u\n",
			count[2] + count[3], rgd->rd_dinodes);
		gfs2_consist_rgrpd(rgd);
David Teigland's avatar
David Teigland committed
504 505 506 507 508 509 510
		return;
	}
}

/**
 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
 * @sdp: The GFS2 superblock
511 512
 * @blk: The data block number
 * @exact: True if this needs to be an exact match
David Teigland's avatar
David Teigland committed
513
 *
514 515 516 517 518 519 520
 * The @exact argument should be set to true by most callers. The exception
 * is when we need to match blocks which are not represented by the rgrp
 * bitmap, but which are part of the rgrp (i.e. padding blocks) which are
 * there for alignment purposes. Another way of looking at it is that @exact
 * matches only valid data/metadata blocks, but with @exact false, it will
 * match any block within the extent of the rgrp.
 *
David Teigland's avatar
David Teigland committed
521 522 523
 * Returns: The resource group, or NULL if not found
 */

524
struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk, bool exact)
David Teigland's avatar
David Teigland committed
525
{
526
	struct rb_node *n, *next;
527
	struct gfs2_rgrpd *cur;
David Teigland's avatar
David Teigland committed
528 529

	spin_lock(&sdp->sd_rindex_spin);
530 531 532 533
	n = sdp->sd_rindex_tree.rb_node;
	while (n) {
		cur = rb_entry(n, struct gfs2_rgrpd, rd_node);
		next = NULL;
534
		if (blk < cur->rd_addr)
535
			next = n->rb_left;
536
		else if (blk >= cur->rd_data0 + cur->rd_data)
537 538
			next = n->rb_right;
		if (next == NULL) {
David Teigland's avatar
David Teigland committed
539
			spin_unlock(&sdp->sd_rindex_spin);
540 541 542 543 544 545
			if (exact) {
				if (blk < cur->rd_addr)
					return NULL;
				if (blk >= cur->rd_data0 + cur->rd_data)
					return NULL;
			}
546
			return cur;
David Teigland's avatar
David Teigland committed
547
		}
548
		n = next;
David Teigland's avatar
David Teigland committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
	}
	spin_unlock(&sdp->sd_rindex_spin);

	return NULL;
}

/**
 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
 * @sdp: The GFS2 superblock
 *
 * Returns: The first rgrp in the filesystem
 */

struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
{
564 565 566
	const struct rb_node *n;
	struct gfs2_rgrpd *rgd;

567
	spin_lock(&sdp->sd_rindex_spin);
568 569
	n = rb_first(&sdp->sd_rindex_tree);
	rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
570
	spin_unlock(&sdp->sd_rindex_spin);
571 572

	return rgd;
David Teigland's avatar
David Teigland committed
573 574 575 576
}

/**
 * gfs2_rgrpd_get_next - get the next RG
577
 * @rgd: the resource group descriptor
David Teigland's avatar
David Teigland committed
578 579 580 581 582 583
 *
 * Returns: The next rgrp
 */

struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
{
584 585 586 587 588 589 590 591 592 593
	struct gfs2_sbd *sdp = rgd->rd_sbd;
	const struct rb_node *n;

	spin_lock(&sdp->sd_rindex_spin);
	n = rb_next(&rgd->rd_node);
	if (n == NULL)
		n = rb_first(&sdp->sd_rindex_tree);

	if (unlikely(&rgd->rd_node == n)) {
		spin_unlock(&sdp->sd_rindex_spin);
David Teigland's avatar
David Teigland committed
594
		return NULL;
595 596 597 598
	}
	rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
	spin_unlock(&sdp->sd_rindex_spin);
	return rgd;
David Teigland's avatar
David Teigland committed
599 600
}

601 602 603 604 605 606 607
void check_and_update_goal(struct gfs2_inode *ip)
{
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
	if (!ip->i_goal || gfs2_blk2rgrpd(sdp, ip->i_goal, 1) == NULL)
		ip->i_goal = ip->i_no_addr;
}

608 609 610 611 612 613 614 615 616 617 618
void gfs2_free_clones(struct gfs2_rgrpd *rgd)
{
	int x;

	for (x = 0; x < rgd->rd_length; x++) {
		struct gfs2_bitmap *bi = rgd->rd_bits + x;
		kfree(bi->bi_clone);
		bi->bi_clone = NULL;
	}
}

619 620
static void dump_rs(struct seq_file *seq, const struct gfs2_blkreserv *rs,
		    const char *fs_id_buf)
621
{
622 623
	struct gfs2_inode *ip = container_of(rs, struct gfs2_inode, i_res);

624 625
	gfs2_print_dbg(seq, "%s  B: n:%llu s:%llu f:%u\n",
		       fs_id_buf,
626
		       (unsigned long long)ip->i_no_addr,
627
		       (unsigned long long)rs->rs_start,
628
		       rs->rs_requested);
629 630
}

631
/**
632 633 634 635
 * __rs_deltree - remove a multi-block reservation from the rgd tree
 * @rs: The reservation to remove
 *
 */
636
static void __rs_deltree(struct gfs2_blkreserv *rs)
637 638 639 640 641 642
{
	struct gfs2_rgrpd *rgd;

	if (!gfs2_rs_active(rs))
		return;

643
	rgd = rs->rs_rgd;
644
	trace_gfs2_rs(rs, TRACE_RS_TREEDEL);
645
	rb_erase(&rs->rs_node, &rgd->rd_rstree);
646
	RB_CLEAR_NODE(&rs->rs_node);
647

648 649 650 651
	if (rs->rs_requested) {
		/* return requested blocks to the rgrp */
		BUG_ON(rs->rs_rgd->rd_requested < rs->rs_requested);
		rs->rs_rgd->rd_requested -= rs->rs_requested;
652

653 654 655 656
		/* The rgrp extent failure point is likely not to increase;
		   it will only do so if the freed blocks are somehow
		   contiguous with a span of free blocks that follows. Still,
		   it will force the number to be recalculated later. */
657 658
		rgd->rd_extfail_pt += rs->rs_requested;
		rs->rs_requested = 0;
659 660 661 662 663 664 665 666
	}
}

/**
 * gfs2_rs_deltree - remove a multi-block reservation from the rgd tree
 * @rs: The reservation to remove
 *
 */
667
void gfs2_rs_deltree(struct gfs2_blkreserv *rs)
668 669 670
{
	struct gfs2_rgrpd *rgd;

671
	rgd = rs->rs_rgd;
672 673
	if (rgd) {
		spin_lock(&rgd->rd_rsspin);
674
		__rs_deltree(rs);
675
		BUG_ON(rs->rs_requested);
676 677
		spin_unlock(&rgd->rd_rsspin);
	}
678 679 680
}

/**
681
 * gfs2_rs_delete - delete a multi-block reservation
682 683 684
 * @ip: The inode for this reservation
 *
 */
685
void gfs2_rs_delete(struct gfs2_inode *ip)
686
{
687 688
	struct inode *inode = &ip->i_inode;

689
	down_write(&ip->i_rw_mutex);
690
	if (atomic_read(&inode->i_writecount) <= 1)
691
		gfs2_rs_deltree(&ip->i_res);
692 693 694
	up_write(&ip->i_rw_mutex);
}

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
/**
 * return_all_reservations - return all reserved blocks back to the rgrp.
 * @rgd: the rgrp that needs its space back
 *
 * We previously reserved a bunch of blocks for allocation. Now we need to
 * give them back. This leave the reservation structures in tact, but removes
 * all of their corresponding "no-fly zones".
 */
static void return_all_reservations(struct gfs2_rgrpd *rgd)
{
	struct rb_node *n;
	struct gfs2_blkreserv *rs;

	spin_lock(&rgd->rd_rsspin);
	while ((n = rb_first(&rgd->rd_rstree))) {
		rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
711
		__rs_deltree(rs);
712 713 714 715
	}
	spin_unlock(&rgd->rd_rsspin);
}

716
void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
David Teigland's avatar
David Teigland committed
717
{
718
	struct rb_node *n;
David Teigland's avatar
David Teigland committed
719 720 721
	struct gfs2_rgrpd *rgd;
	struct gfs2_glock *gl;

722 723
	while ((n = rb_first(&sdp->sd_rindex_tree))) {
		rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
David Teigland's avatar
David Teigland committed
724 725
		gl = rgd->rd_gl;

726
		rb_erase(n, &sdp->sd_rindex_tree);
David Teigland's avatar
David Teigland committed
727 728

		if (gl) {
729 730 731 732
			if (gl->gl_state != LM_ST_UNLOCKED) {
				gfs2_glock_cb(gl, LM_ST_UNLOCKED);
				flush_delayed_work(&gl->gl_work);
			}
733
			gfs2_rgrp_brelse(rgd);
734
			glock_clear_object(gl, rgd);
David Teigland's avatar
David Teigland committed
735 736 737
			gfs2_glock_put(gl);
		}

738
		gfs2_free_clones(rgd);
739
		return_all_reservations(rgd);
David Teigland's avatar
David Teigland committed
740
		kfree(rgd->rd_bits);
741
		rgd->rd_bits = NULL;
742
		kmem_cache_free(gfs2_rgrpd_cachep, rgd);
David Teigland's avatar
David Teigland committed
743 744 745 746
	}
}

/**
747
 * compute_bitstructs - Compute the bitmap sizes
David Teigland's avatar
David Teigland committed
748 749 750 751 752 753 754 755 756 757 758
 * @rgd: The resource group descriptor
 *
 * Calculates bitmap descriptors, one for each block that contains bitmap data
 *
 * Returns: errno
 */

static int compute_bitstructs(struct gfs2_rgrpd *rgd)
{
	struct gfs2_sbd *sdp = rgd->rd_sbd;
	struct gfs2_bitmap *bi;
759
	u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
760
	u32 bytes_left, bytes;
David Teigland's avatar
David Teigland committed
761 762
	int x;

763 764 765
	if (!length)
		return -EINVAL;

766
	rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
David Teigland's avatar
David Teigland committed
767 768 769
	if (!rgd->rd_bits)
		return -ENOMEM;

770
	bytes_left = rgd->rd_bitbytes;
David Teigland's avatar
David Teigland committed
771 772 773 774

	for (x = 0; x < length; x++) {
		bi = rgd->rd_bits + x;

775
		bi->bi_flags = 0;
David Teigland's avatar
David Teigland committed
776 777 778 779 780
		/* small rgrp; bitmap stored completely in header block */
		if (length == 1) {
			bytes = bytes_left;
			bi->bi_offset = sizeof(struct gfs2_rgrp);
			bi->bi_start = 0;
781
			bi->bi_bytes = bytes;
782
			bi->bi_blocks = bytes * GFS2_NBBY;
David Teigland's avatar
David Teigland committed
783 784 785 786 787
		/* header block */
		} else if (x == 0) {
			bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
			bi->bi_offset = sizeof(struct gfs2_rgrp);
			bi->bi_start = 0;
788
			bi->bi_bytes = bytes;
789
			bi->bi_blocks = bytes * GFS2_NBBY;
David Teigland's avatar
David Teigland committed
790 791 792 793
		/* last block */
		} else if (x + 1 == length) {
			bytes = bytes_left;
			bi->bi_offset = sizeof(struct gfs2_meta_header);
794
			bi->bi_start = rgd->rd_bitbytes - bytes_left;
795
			bi->bi_bytes = bytes;
796
			bi->bi_blocks = bytes * GFS2_NBBY;
David Teigland's avatar
David Teigland committed
797 798
		/* other blocks */
		} else {
799 800
			bytes = sdp->sd_sb.sb_bsize -
				sizeof(struct gfs2_meta_header);
David Teigland's avatar
David Teigland committed
801
			bi->bi_offset = sizeof(struct gfs2_meta_header);
802
			bi->bi_start = rgd->rd_bitbytes - bytes_left;
803
			bi->bi_bytes = bytes;
804
			bi->bi_blocks = bytes * GFS2_NBBY;
David Teigland's avatar
David Teigland committed
805 806 807 808 809 810 811 812 813 814
		}

		bytes_left -= bytes;
	}

	if (bytes_left) {
		gfs2_consist_rgrpd(rgd);
		return -EIO;
	}
	bi = rgd->rd_bits + (length - 1);
815
	if ((bi->bi_start + bi->bi_bytes) * GFS2_NBBY != rgd->rd_data) {
816
		gfs2_lm(sdp,
817 818 819 820 821
			"ri_addr=%llu "
			"ri_length=%u "
			"ri_data0=%llu "
			"ri_data=%u "
			"ri_bitbytes=%u "
822 823 824 825 826 827 828 829
			"start=%u len=%u offset=%u\n",
			(unsigned long long)rgd->rd_addr,
			rgd->rd_length,
			(unsigned long long)rgd->rd_data0,
			rgd->rd_data,
			rgd->rd_bitbytes,
			bi->bi_start, bi->bi_bytes, bi->bi_offset);
		gfs2_consist_rgrpd(rgd);
David Teigland's avatar
David Teigland committed
830 831 832 833 834 835
		return -EIO;
	}

	return 0;
}

836 837
/**
 * gfs2_ri_total - Total up the file system space, according to the rindex.
838
 * @sdp: the filesystem
839 840 841 842 843 844 845 846 847 848 849 850 851
 *
 */
u64 gfs2_ri_total(struct gfs2_sbd *sdp)
{
	u64 total_data = 0;	
	struct inode *inode = sdp->sd_rindex;
	struct gfs2_inode *ip = GFS2_I(inode);
	char buf[sizeof(struct gfs2_rindex)];
	int error, rgrps;

	for (rgrps = 0;; rgrps++) {
		loff_t pos = rgrps * sizeof(struct gfs2_rindex);

852
		if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode))
853
			break;
854
		error = gfs2_internal_read(ip, buf, &pos,
855 856 857
					   sizeof(struct gfs2_rindex));
		if (error != sizeof(struct gfs2_rindex))
			break;
858
		total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
859 860 861 862
	}
	return total_data;
}

863
static int rgd_insert(struct gfs2_rgrpd *rgd)
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
{
	struct gfs2_sbd *sdp = rgd->rd_sbd;
	struct rb_node **newn = &sdp->sd_rindex_tree.rb_node, *parent = NULL;

	/* Figure out where to put new node */
	while (*newn) {
		struct gfs2_rgrpd *cur = rb_entry(*newn, struct gfs2_rgrpd,
						  rd_node);

		parent = *newn;
		if (rgd->rd_addr < cur->rd_addr)
			newn = &((*newn)->rb_left);
		else if (rgd->rd_addr > cur->rd_addr)
			newn = &((*newn)->rb_right);
		else
879
			return -EEXIST;
880 881 882 883
	}

	rb_link_node(&rgd->rd_node, parent, newn);
	rb_insert_color(&rgd->rd_node, &sdp->sd_rindex_tree);
884 885
	sdp->sd_rgrps++;
	return 0;
886 887
}

David Teigland's avatar
David Teigland committed
888
/**
889
 * read_rindex_entry - Pull in a new resource index entry from the disk
890
 * @ip: Pointer to the rindex inode
David Teigland's avatar
David Teigland committed
891
 *
892
 * Returns: 0 on success, > 0 on EOF, error code otherwise
893 894
 */

895
static int read_rindex_entry(struct gfs2_inode *ip)
896 897 898
{
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
	loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
899
	struct gfs2_rindex buf;
900 901 902
	int error;
	struct gfs2_rgrpd *rgd;

903 904 905
	if (pos >= i_size_read(&ip->i_inode))
		return 1;

906
	error = gfs2_internal_read(ip, (char *)&buf, &pos,
907
				   sizeof(struct gfs2_rindex));
908 909 910

	if (error != sizeof(struct gfs2_rindex))
		return (error == 0) ? 1 : error;
911

912
	rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
913 914 915 916 917
	error = -ENOMEM;
	if (!rgd)
		return error;

	rgd->rd_sbd = sdp;
918 919 920 921 922
	rgd->rd_addr = be64_to_cpu(buf.ri_addr);
	rgd->rd_length = be32_to_cpu(buf.ri_length);
	rgd->rd_data0 = be64_to_cpu(buf.ri_data0);
	rgd->rd_data = be32_to_cpu(buf.ri_data);
	rgd->rd_bitbytes = be32_to_cpu(buf.ri_bitbytes);
923
	spin_lock_init(&rgd->rd_rsspin);
924
	mutex_init(&rgd->rd_mutex);
925

926
	error = gfs2_glock_get(sdp, rgd->rd_addr,
927 928
			       &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
	if (error)
929
		goto fail;
930

931 932 933 934
	error = compute_bitstructs(rgd);
	if (error)
		goto fail_glock;

935
	rgd->rd_rgl = (struct gfs2_rgrp_lvb *)rgd->rd_gl->gl_lksb.sb_lvbptr;
936
	rgd->rd_flags &= ~GFS2_RDF_PREFERRED;
937 938
	if (rgd->rd_data > sdp->sd_max_rg_data)
		sdp->sd_max_rg_data = rgd->rd_data;
939
	spin_lock(&sdp->sd_rindex_spin);
940
	error = rgd_insert(rgd);
941
	spin_unlock(&sdp->sd_rindex_spin);
942
	if (!error) {
943
		glock_set_object(rgd->rd_gl, rgd);
944
		return 0;
945
	}
946 947

	error = 0; /* someone else read in the rgrp; free it and ignore it */
948
fail_glock:
949
	gfs2_glock_put(rgd->rd_gl);
950 951 952

fail:
	kfree(rgd->rd_bits);
953
	rgd->rd_bits = NULL;
954
	kmem_cache_free(gfs2_rgrpd_cachep, rgd);
955 956 957
	return error;
}

958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
/**
 * set_rgrp_preferences - Run all the rgrps, selecting some we prefer to use
 * @sdp: the GFS2 superblock
 *
 * The purpose of this function is to select a subset of the resource groups
 * and mark them as PREFERRED. We do it in such a way that each node prefers
 * to use a unique set of rgrps to minimize glock contention.
 */
static void set_rgrp_preferences(struct gfs2_sbd *sdp)
{
	struct gfs2_rgrpd *rgd, *first;
	int i;

	/* Skip an initial number of rgrps, based on this node's journal ID.
	   That should start each node out on its own set. */
	rgd = gfs2_rgrpd_get_first(sdp);
	for (i = 0; i < sdp->sd_lockstruct.ls_jid; i++)
		rgd = gfs2_rgrpd_get_next(rgd);
	first = rgd;

	do {
		rgd->rd_flags |= GFS2_RDF_PREFERRED;
		for (i = 0; i < sdp->sd_journals; i++) {
			rgd = gfs2_rgrpd_get_next(rgd);
982
			if (!rgd || rgd == first)
983 984
				break;
		}
985
	} while (rgd && rgd != first);
986 987
}

988 989 990 991
/**
 * gfs2_ri_update - Pull in a new resource index from the disk
 * @ip: pointer to the rindex inode
 *
David Teigland's avatar
David Teigland committed
992 993 994
 * Returns: 0 on successful update, error code otherwise
 */

995
static int gfs2_ri_update(struct gfs2_inode *ip)
David Teigland's avatar
David Teigland committed
996
{
997
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teigland's avatar
David Teigland committed
998 999
	int error;

1000
	do {
1001
		error = read_rindex_entry(ip);
1002 1003 1004 1005
	} while (error == 0);

	if (error < 0)
		return error;
David Teigland's avatar
David Teigland committed
1006

1007 1008 1009 1010
	if (RB_EMPTY_ROOT(&sdp->sd_rindex_tree)) {
		fs_err(sdp, "no resource groups found in the file system.\n");
		return -ENOENT;
	}
1011 1012
	set_rgrp_preferences(sdp);

1013
	sdp->sd_rindex_uptodate = 1;
1014 1015
	return 0;
}
David Teigland's avatar
David Teigland committed
1016 1017

/**
1018
 * gfs2_rindex_update - Update the rindex if required
David Teigland's avatar
David Teigland committed
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
 * @sdp: The GFS2 superblock
 *
 * We grab a lock on the rindex inode to make sure that it doesn't
 * change whilst we are performing an operation. We keep this lock
 * for quite long periods of time compared to other locks. This
 * doesn't matter, since it is shared and it is very, very rarely
 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
 *
 * This makes sure that we're using the latest copy of the resource index
 * special file, which might have been updated if someone expanded the
 * filesystem (via gfs2_grow utility), which adds new resource groups.
 *
1031
 * Returns: 0 on succeess, error code otherwise
David Teigland's avatar
David Teigland committed
1032 1033
 */

1034
int gfs2_rindex_update(struct gfs2_sbd *sdp)
David Teigland's avatar
David Teigland committed
1035
{
1036
	struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
David Teigland's avatar
David Teigland committed
1037
	struct gfs2_glock *gl = ip->i_gl;
1038 1039
	struct gfs2_holder ri_gh;
	int error = 0;
1040
	int unlock_required = 0;
David Teigland's avatar
David Teigland committed
1041 1042

	/* Read new copy from disk if we don't have the latest */
1043
	if (!sdp->sd_rindex_uptodate) {
1044 1045 1046
		if (!gfs2_glock_is_locked_by_me(gl)) {
			error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &ri_gh);
			if (error)
1047
				return error;
1048 1049
			unlock_required = 1;
		}
1050
		if (!sdp->sd_rindex_uptodate)
David Teigland's avatar
David Teigland committed
1051
			error = gfs2_ri_update(ip);
1052 1053
		if (unlock_required)
			gfs2_glock_dq_uninit(&ri_gh);
David Teigland's avatar
David Teigland committed
1054 1055 1056 1057 1058
	}

	return error;
}

1059
static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
1060 1061
{
	const struct gfs2_rgrp *str = buf;
1062
	u32 rg_flags;
1063

1064
	rg_flags = be32_to_cpu(str->rg_flags);
1065
	rg_flags &= ~GFS2_RDF_MASK;
1066 1067
	rgd->rd_flags &= GFS2_RDF_MASK;
	rgd->rd_flags |= rg_flags;
1068
	rgd->rd_free = be32_to_cpu(str->rg_free);
1069
	rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
1070
	rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
1071
	/* rd_data0, rd_data and rd_bitbytes already set from rindex */
1072 1073
}

1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
static void gfs2_rgrp_ondisk2lvb(struct gfs2_rgrp_lvb *rgl, const void *buf)
{
	const struct gfs2_rgrp *str = buf;

	rgl->rl_magic = cpu_to_be32(GFS2_MAGIC);
	rgl->rl_flags = str->rg_flags;
	rgl->rl_free = str->rg_free;
	rgl->rl_dinodes = str->rg_dinodes;
	rgl->rl_igeneration = str->rg_igeneration;
	rgl->__pad = 0UL;
}

1086
static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
1087
{
1088
	struct gfs2_rgrpd *next = gfs2_rgrpd_get_next(rgd);
1089
	struct gfs2_rgrp *str = buf;
1090
	u32 crc;
1091

1092
	str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
1093
	str->rg_free = cpu_to_be32(rgd->rd_free);
1094
	str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
1095 1096 1097 1098
	if (next == NULL)
		str->rg_skip = 0;
	else if (next->rd_addr > rgd->rd_addr)
		str->rg_skip = cpu_to_be32(next->rd_addr - rgd->rd_addr);
1099
	str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration);
1100 1101 1102
	str->rg_data0 = cpu_to_be64(rgd->rd_data0);
	str->rg_data = cpu_to_be32(rgd->rd_data);
	str->rg_bitbytes = cpu_to_be32(rgd->rd_bitbytes);
1103 1104 1105
	str->rg_crc = 0;
	crc = gfs2_disk_hash(buf, sizeof(struct gfs2_rgrp));
	str->rg_crc = cpu_to_be32(crc);
1106

1107
	memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
1108
	gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, buf);
1109 1110
}

1111 1112 1113 1114
static int gfs2_rgrp_lvb_valid(struct gfs2_rgrpd *rgd)
{
	struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl;
	struct gfs2_rgrp *str = (struct gfs2_rgrp *)rgd->rd_bits[0].bi_bh->b_data;
1115
	struct gfs2_sbd *sdp = rgd->rd_sbd;
1116
	int valid = 1;
1117

1118
	if (rgl->rl_flags != str->rg_flags) {
1119 1120
		fs_warn(sdp, "GFS2: rgd: %llu lvb flag mismatch %u/%u",
			(unsigned long long)rgd->rd_addr,
1121 1122 1123 1124
		       be32_to_cpu(rgl->rl_flags), be32_to_cpu(str->rg_flags));
		valid = 0;
	}
	if (rgl->rl_free != str->rg_free) {
1125 1126 1127
		fs_warn(sdp, "GFS2: rgd: %llu lvb free mismatch %u/%u",
			(unsigned long long)rgd->rd_addr,
			be32_to_cpu(rgl->rl_free), be32_to_cpu(str->rg_free));
1128 1129 1130
		valid = 0;
	}
	if (rgl->rl_dinodes != str->rg_dinodes) {
1131 1132 1133 1134
		fs_warn(sdp, "GFS2: rgd: %llu lvb dinode mismatch %u/%u",
			(unsigned long long)rgd->rd_addr,
			be32_to_cpu(rgl->rl_dinodes),
			be32_to_cpu(str->rg_dinodes));
1135 1136 1137
		valid = 0;
	}
	if (rgl->rl_igeneration != str->rg_igeneration) {
1138 1139 1140 1141
		fs_warn(sdp, "GFS2: rgd: %llu lvb igen mismatch %llu/%llu",
			(unsigned long long)rgd->rd_addr,
			(unsigned long long)be64_to_cpu(rgl->rl_igeneration),
			(unsigned long long)be64_to_cpu(str->rg_igeneration));
1142 1143 1144
		valid = 0;
	}
	return valid;
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
}

static u32 count_unlinked(struct gfs2_rgrpd *rgd)
{
	struct gfs2_bitmap *bi;
	const u32 length = rgd->rd_length;
	const u8 *buffer = NULL;
	u32 i, goal, count = 0;

	for (i = 0, bi = rgd->rd_bits; i < length; i++, bi++) {
		goal = 0;
		buffer = bi->bi_bh->b_data + bi->bi_offset;
		WARN_ON(!buffer_uptodate(bi->bi_bh));
1158 1159
		while (goal < bi->bi_blocks) {
			goal = gfs2_bitfit(buffer, bi->bi_bytes, goal,
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
					   GFS2_BLKST_UNLINKED);
			if (goal == BFITNOENT)
				break;
			count++;
			goal++;
		}
	}

	return count;
}

1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
static void rgrp_set_bitmap_flags(struct gfs2_rgrpd *rgd)
{
	struct gfs2_bitmap *bi;
	int x;

	if (rgd->rd_free) {
		for (x = 0; x < rgd->rd_length; x++) {
			bi = rgd->rd_bits + x;
			clear_bit(GBF_FULL, &bi->bi_flags);
		}
	} else {
		for (x = 0; x < rgd->rd_length; x++) {
			bi = rgd->rd_bits + x;
			set_bit(GBF_FULL, &bi->bi_flags);
		}
	}
}
1188

David Teigland's avatar
David Teigland committed
1189
/**
1190
 * gfs2_rgrp_go_instantiate - Read in a RG's header and bitmaps
1191
 * @gl: the glock representing the rgrpd to read in
David Teigland's avatar
David Teigland committed
1192 1193
 *
 * Read in all of a Resource Group's header and bitmap blocks.
1194
 * Caller must eventually call gfs2_rgrp_brelse() to free the bitmaps.
David Teigland's avatar
David Teigland committed
1195 1196 1197 1198
 *
 * Returns: errno
 */

1199
int gfs2_rgrp_go_instantiate(struct gfs2_glock *gl)
David Teigland's avatar
David Teigland committed
1200
{
1201
	struct gfs2_rgrpd *rgd = gl->gl_object;
David Teigland's avatar
David Teigland committed
1202
	struct gfs2_sbd *sdp = rgd->rd_sbd;
1203
	unsigned int length = rgd->rd_length;
David Teigland's avatar
David Teigland committed
1204 1205 1206 1207
	struct gfs2_bitmap *bi;
	unsigned int x, y;
	int error;

1208 1209 1210
	if (rgd->rd_bits[0].bi_bh != NULL)
		return 0;

David Teigland's avatar
David Teigland committed
1211 1212
	for (x = 0; x < length; x++) {
		bi = rgd->rd_bits + x;
1213
		error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, 0, &bi->bi_bh);
David Teigland's avatar
David Teigland committed
1214 1215 1216 1217 1218 1219
		if (error)
			goto fail;
	}

	for (y = length; y--;) {
		bi = rgd->rd_bits + y;
1220
		error = gfs2_meta_wait(sdp, bi->bi_bh);
David Teigland's avatar
David Teigland committed
1221 1222
		if (error)
			goto fail;
1223
		if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
David Teigland's avatar
David Teigland committed
1224 1225 1226 1227 1228 1229
					      GFS2_METATYPE_RG)) {
			error = -EIO;
			goto fail;
		}
	}

1230 1231 1232 1233 1234 1235 1236
	gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
	rgrp_set_bitmap_flags(rgd);
	rgd->rd_flags |= GFS2_RDF_CHECK;
	rgd->rd_free_clone = rgd->rd_free;
	GLOCK_BUG_ON(rgd->rd_gl, rgd->rd_reserved);
	/* max out the rgrp allocation failure point */
	rgd->rd_extfail_pt = rgd->rd_free;
Al Viro's avatar
Al Viro committed
1237
	if (cpu_to_be32(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) {
1238 1239 1240
		rgd->rd_rgl->rl_unlinked = cpu_to_be32(count_unlinked(rgd));
		gfs2_rgrp_ondisk2lvb(rgd->rd_rgl,
				     rgd->rd_bits[0].bi_bh->b_data);
1241
	} else if (sdp->sd_args.ar_rgrplvb) {
1242 1243 1244 1245 1246 1247 1248 1249
		if (!gfs2_rgrp_lvb_valid(rgd)){
			gfs2_consist_rgrpd(rgd);
			error = -EIO;
			goto fail;
		}
		if (rgd->rd_rgl->rl_unlinked == 0)
			rgd->rd_flags &= ~GFS2_RDF_CHECK;
	}
David Teigland's avatar
David Teigland committed
1250 1251
	return 0;

1252
fail:
David Teigland's avatar
David Teigland committed
1253 1254 1255 1256 1257 1258 1259 1260 1261
	while (x--) {
		bi = rgd->rd_bits + x;
		brelse(bi->bi_bh);
		bi->bi_bh = NULL;
		gfs2_assert_warn(sdp, !bi->bi_clone);
	}
	return error;
}

1262
static int update_rgrp_lvb(struct gfs2_rgrpd *rgd, struct gfs2_holder *gh)
1263 1264 1265
{
	u32 rl_flags;

1266
	if (!test_bit(GLF_INSTANTIATE_NEEDED, &gh->gh_gl->gl_flags))
1267 1268
		return 0;

Al Viro's avatar
Al Viro committed
1269
	if (cpu_to_be32(GFS2_MAGIC) != rgd->rd_rgl->rl_magic)
1270
		return gfs2_instantiate(gh);
1271 1272 1273 1274

	rl_flags = be32_to_cpu(rgd->rd_rgl->rl_flags);
	rl_flags &= ~GFS2_RDF_MASK;
	rgd->rd_flags &= GFS2_RDF_MASK;
1275
	rgd->rd_flags |= (rl_flags | GFS2_RDF_CHECK);
1276 1277 1278
	if (rgd->rd_rgl->rl_unlinked == 0)
		rgd->rd_flags &= ~GFS2_RDF_CHECK;
	rgd->rd_free = be32_to_cpu(rgd->rd_rgl->rl_free);
1279
	rgrp_set_bitmap_flags(rgd);
1280
	rgd->rd_free_clone = rgd->rd_free;
1281
	GLOCK_BUG_ON(rgd->rd_gl, rgd->rd_reserved);
1282 1283
	/* max out the rgrp allocation failure point */
	rgd->rd_extfail_pt = rgd->rd_free;
1284 1285 1286 1287 1288
	rgd->rd_dinodes = be32_to_cpu(rgd->rd_rgl->rl_dinodes);
	rgd->rd_igeneration = be64_to_cpu(rgd->rd_rgl->rl_igeneration);
	return 0;
}

David Teigland's avatar
David Teigland committed
1289
/**
1290 1291
 * gfs2_rgrp_brelse - Release RG bitmaps read in with gfs2_rgrp_bh_get()
 * @rgd: The resource group
David Teigland's avatar
David Teigland committed
1292 1293 1294
 *
 */

1295
void gfs2_rgrp_brelse(struct gfs2_rgrpd *rgd)
David Teigland's avatar
David Teigland committed
1296
{
1297
	int x, length = rgd->rd_length;
David Teigland's avatar
David Teigland committed
1298 1299 1300

	for (x = 0; x < length; x++) {
		struct gfs2_bitmap *bi = rgd->rd_bits + x;
1301 1302 1303 1304
		if (bi->bi_bh) {
			brelse(bi->bi_bh);
			bi->bi_bh = NULL;
		}
David Teigland's avatar
David Teigland committed
1305
	}
1306
	set_bit(GLF_INSTANTIATE_NEEDED, &rgd->rd_gl->gl_flags);
1307 1308
}

1309
int gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
1310
			     struct buffer_head *bh,
1311
			     const struct gfs2_bitmap *bi, unsigned minlen, u64 *ptrimmed)
1312 1313 1314
{
	struct super_block *sb = sdp->sd_vfs;
	u64 blk;
1315
	sector_t start = 0;
1316
	sector_t nr_blks = 0;
1317
	int rv = -EIO;
1318
	unsigned int x;
1319 1320
	u32 trimmed = 0;
	u8 diff;
1321

1322
	for (x = 0; x < bi->bi_bytes; x++) {
1323 1324 1325 1326 1327 1328 1329 1330 1331
		const u8 *clone = bi->bi_clone ? bi->bi_clone : bi->bi_bh->b_data;
		clone += bi->bi_offset;
		clone += x;
		if (bh) {
			const u8 *orig = bh->b_data + bi->bi_offset + x;
			diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1));
		} else {
			diff = ~(*clone | (*clone >> 1));
		}
1332 1333 1334 1335 1336 1337
		diff &= 0x55;
		if (diff == 0)
			continue;
		blk = offset + ((bi->bi_start + x) * GFS2_NBBY);
		while(diff) {
			if (diff & 1) {
1338
				if (nr_blks == 0)
1339
					goto start_new_extent;
1340 1341 1342 1343
				if ((start + nr_blks) != blk) {
					if (nr_blks >= minlen) {
						rv = sb_issue_discard(sb,
							start, nr_blks,
1344 1345 1346
							GFP_NOFS, 0);
						if (rv)
							goto fail;
1347
						trimmed += nr_blks;
1348
					}
1349
					nr_blks = 0;
1350 1351 1352
start_new_extent:
					start = blk;
				}
1353
				nr_blks++;
1354 1355
			}
			diff >>= 2;
1356
			blk++;
1357 1358
		}
	}
1359 1360
	if (nr_blks >= minlen) {
		rv = sb_issue_discard(sb, start, nr_blks, GFP_NOFS, 0);
1361 1362
		if (rv)
			goto fail;
1363
		trimmed += nr_blks;
1364
	}
1365 1366 1367 1368
	if (ptrimmed)
		*ptrimmed = trimmed;
	return 0;

1369
fail:
1370
	if (sdp->sd_args.ar_discard)
1371
		fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem\n", rv);
1372
	sdp->sd_args.ar_discard = 0;
1373
	return rv;
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
}

/**
 * gfs2_fitrim - Generate discard requests for unused bits of the filesystem
 * @filp: Any file on the filesystem
 * @argp: Pointer to the arguments (also used to pass result)
 *
 * Returns: 0 on success, otherwise error code
 */

int gfs2_fitrim(struct file *filp, void __user *argp)
{
Al Viro's avatar
Al Viro committed
1386
	struct inode *inode = file_inode(filp);
1387
	struct gfs2_sbd *sdp = GFS2_SB(inode);
1388
	struct block_device *bdev = sdp->sd_vfs->s_bdev;
1389 1390 1391 1392 1393 1394 1395 1396
	struct buffer_head *bh;
	struct gfs2_rgrpd *rgd;
	struct gfs2_rgrpd *rgd_end;
	struct gfs2_holder gh;
	struct fstrim_range r;
	int ret = 0;
	u64 amt;
	u64 trimmed = 0;
1397
	u64 start, end, minlen;
1398
	unsigned int x;
1399
	unsigned bs_shift = sdp->sd_sb.sb_bsize_shift;
1400 1401 1402 1403

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

1404 1405 1406
	if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
		return -EROFS;

1407
	if (!bdev_max_discard_sectors(bdev))
1408 1409
		return -EOPNOTSUPP;

1410
	if (copy_from_user(&r, argp, sizeof(r)))
1411 1412
		return -EFAULT;

1413 1414 1415 1416
	ret = gfs2_rindex_update(sdp);
	if (ret)
		return ret;

1417 1418
	start = r.start >> bs_shift;
	end = start + (r.len >> bs_shift);
1419
	minlen = max_t(u64, r.minlen, sdp->sd_sb.sb_bsize);
1420
	minlen = max_t(u64, minlen, bdev_discard_granularity(bdev)) >> bs_shift;
1421

1422 1423 1424
	if (end <= start || minlen > sdp->sd_max_rg_data)
		return -EINVAL;

1425
	rgd = gfs2_blk2rgrpd(sdp, start, 0);
1426
	rgd_end = gfs2_blk2rgrpd(sdp, end, 0);
1427

1428 1429 1430
	if ((gfs2_rgrpd_get_first(sdp) == gfs2_rgrpd_get_next(rgd_end))
	    && (start > rgd_end->rd_data0 + rgd_end->rd_data))
		return -EINVAL; /* start is beyond the end of the fs */
1431 1432 1433

	while (1) {

1434 1435
		ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
					 LM_FLAG_NODE_SCOPE, &gh);
1436 1437 1438 1439 1440 1441 1442
		if (ret)
			goto out;

		if (!(rgd->rd_flags & GFS2_RGF_TRIMMED)) {
			/* Trim each bitmap in the rgrp */
			for (x = 0; x < rgd->rd_length; x++) {
				struct gfs2_bitmap *bi = rgd->rd_bits + x;
1443
				rgrp_lock_local(rgd);
1444 1445 1446
				ret = gfs2_rgrp_send_discards(sdp,
						rgd->rd_data0, NULL, bi, minlen,
						&amt);
1447
				rgrp_unlock_local(rgd);
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
				if (ret) {
					gfs2_glock_dq_uninit(&gh);
					goto out;
				}
				trimmed += amt;
			}

			/* Mark rgrp as having been trimmed */
			ret = gfs2_trans_begin(sdp, RES_RG_HDR, 0);
			if (ret == 0) {
				bh = rgd->rd_bits[0].bi_bh;
1459
				rgrp_lock_local(rgd);
1460
				rgd->rd_flags |= GFS2_RGF_TRIMMED;
1461
				gfs2_trans_add_meta(rgd->rd_gl, bh);
1462
				gfs2_rgrp_out(rgd, bh->b_data);
1463
				rgrp_unlock_local(rgd);
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
				gfs2_trans_end(sdp);
			}
		}
		gfs2_glock_dq_uninit(&gh);

		if (rgd == rgd_end)
			break;

		rgd = gfs2_rgrpd_get_next(rgd);
	}

out:
1476
	r.len = trimmed << bs_shift;
1477
	if (copy_to_user(argp, &r, sizeof(r)))
1478 1479 1480
		return -EFAULT;

	return ret;
1481 1482
}

1483 1484 1485 1486 1487
/**
 * rs_insert - insert a new multi-block reservation into the rgrp's rb_tree
 * @ip: the inode structure
 *
 */
1488
static void rs_insert(struct gfs2_inode *ip)
1489 1490 1491
{
	struct rb_node **newn, *parent = NULL;
	int rc;
1492
	struct gfs2_blkreserv *rs = &ip->i_res;
1493
	struct gfs2_rgrpd *rgd = rs->rs_rgd;
1494 1495

	BUG_ON(gfs2_rs_active(rs));
1496

1497 1498
	spin_lock(&rgd->rd_rsspin);
	newn = &rgd->rd_rstree.rb_node;
1499 1500 1501 1502 1503
	while (*newn) {
		struct gfs2_blkreserv *cur =
			rb_entry(*newn, struct gfs2_blkreserv, rs_node);

		parent = *newn;
1504
		rc = rs_cmp(rs->rs_start, rs->rs_requested, cur);
1505 1506 1507 1508 1509 1510
		if (rc > 0)
			newn = &((*newn)->rb_right);
		else if (rc < 0)
			newn = &((*newn)->rb_left);
		else {
			spin_unlock(&rgd->rd_rsspin);
1511 1512
			WARN_ON(1);
			return;
1513 1514 1515 1516 1517 1518 1519
		}
	}

	rb_link_node(&rs->rs_node, parent, newn);
	rb_insert_color(&rs->rs_node, &rgd->rd_rstree);

	/* Do our rgrp accounting for the reservation */
1520
	rgd->rd_requested += rs->rs_requested; /* blocks requested */
1521
	spin_unlock(&rgd->rd_rsspin);
1522
	trace_gfs2_rs(rs, TRACE_RS_INSERT);
1523 1524
}

1525
/**
1526
 * rgd_free - return the number of free blocks we can allocate
1527
 * @rgd: the resource group
1528
 * @rs: The reservation to free
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
 *
 * This function returns the number of free blocks for an rgrp.
 * That's the clone-free blocks (blocks that are free, not including those
 * still being used for unlinked files that haven't been deleted.)
 *
 * It also subtracts any blocks reserved by someone else, but does not
 * include free blocks that are still part of our current reservation,
 * because obviously we can (and will) allocate them.
 */
static inline u32 rgd_free(struct gfs2_rgrpd *rgd, struct gfs2_blkreserv *rs)
{
	u32 tot_reserved, tot_free;

1542
	if (WARN_ON_ONCE(rgd->rd_requested < rs->rs_requested))
1543
		return 0;
1544
	tot_reserved = rgd->rd_requested - rs->rs_requested;
1545 1546 1547 1548 1549 1550 1551 1552 1553

	if (rgd->rd_free_clone < tot_reserved)
		tot_reserved = 0;

	tot_free = rgd->rd_free_clone - tot_reserved;

	return tot_free;
}

1554
/**
1555
 * rg_mblk_search - find a group of multiple free blocks to form a reservation
1556 1557
 * @rgd: the resource group descriptor
 * @ip: pointer to the inode for which we're reserving blocks
1558
 * @ap: the allocation parameters
1559 1560 1561
 *
 */

1562
static void rg_mblk_search(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip,
1563
			   const struct gfs2_alloc_parms *ap)
1564
{
1565 1566
	struct gfs2_rbm rbm = { .rgd = rgd, };
	u64 goal;
1567
	struct gfs2_blkreserv *rs = &ip->i_res;
1568
	u32 extlen;
1569
	u32 free_blocks, blocks_available;
1570
	int ret;
1571
	struct inode *inode = &ip->i_inode;
1572

1573 1574 1575 1576 1577 1578 1579 1580 1581
	spin_lock(&rgd->rd_rsspin);
	free_blocks = rgd_free(rgd, rs);
	if (rgd->rd_free_clone < rgd->rd_requested)
		free_blocks = 0;
	blocks_available = rgd->rd_free_clone - rgd->rd_reserved;
	if (rgd == rs->rs_rgd)
		blocks_available += rs->rs_reserved;
	spin_unlock(&rgd->rd_rsspin);

1582 1583 1584
	if (S_ISDIR(inode->i_mode))
		extlen = 1;
	else {
1585
		extlen = max_t(u32, atomic_read(&ip->i_sizehint), ap->target);
1586
		extlen = clamp(extlen, (u32)RGRP_RSRV_MINBLKS, free_blocks);
1587
	}
1588
	if (free_blocks < extlen || blocks_available < extlen)
1589 1590
		return;

1591 1592
	/* Find bitmap block that contains bits for goal block */
	if (rgrp_contains_block(rgd, ip->i_goal))
1593
		goal = ip->i_goal;
1594
	else
1595
		goal = rgd->rd_last_alloc + rgd->rd_data0;
1596

1597 1598
	if (WARN_ON(gfs2_rbm_from_block(&rbm, goal)))
		return;
1599

1600
	ret = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, &extlen, &ip->i_res, true);
1601
	if (ret == 0) {
1602
		rs->rs_start = gfs2_rbm_to_block(&rbm);
1603
		rs->rs_requested = extlen;
1604
		rs_insert(ip);
1605 1606 1607
	} else {
		if (goal == rgd->rd_last_alloc + rgd->rd_data0)
			rgd->rd_last_alloc = 0;
1608
	}
1609 1610
}

1611 1612 1613 1614
/**
 * gfs2_next_unreserved_block - Return next block that is not reserved
 * @rgd: The resource group
 * @block: The starting block
1615
 * @length: The required length
1616
 * @ignore_rs: Reservation to ignore
1617 1618 1619 1620 1621 1622 1623 1624
 *
 * If the block does not appear in any reservation, then return the
 * block number unchanged. If it does appear in the reservation, then
 * keep looking through the tree of reservations in order to find the
 * first block number which is not reserved.
 */

static u64 gfs2_next_unreserved_block(struct gfs2_rgrpd *rgd, u64 block,
1625
				      u32 length,
1626
				      struct gfs2_blkreserv *ignore_rs)
1627 1628 1629 1630 1631 1632
{
	struct gfs2_blkreserv *rs;
	struct rb_node *n;
	int rc;

	spin_lock(&rgd->rd_rsspin);
1633
	n = rgd->rd_rstree.rb_node;
1634 1635
	while (n) {
		rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
1636
		rc = rs_cmp(block, length, rs);
1637 1638 1639 1640 1641 1642 1643 1644 1645
		if (rc < 0)
			n = n->rb_left;
		else if (rc > 0)
			n = n->rb_right;
		else
			break;
	}

	if (n) {
1646
		while (rs_cmp(block, length, rs) == 0 && rs != ignore_rs) {
1647
			block = rs->rs_start + rs->rs_requested;
1648
			n = n->rb_right;
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
			if (n == NULL)
				break;
			rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
		}
	}

	spin_unlock(&rgd->rd_rsspin);
	return block;
}

/**
 * gfs2_reservation_check_and_update - Check for reservations during block alloc
 * @rbm: The current position in the resource group
1662
 * @rs: Our own reservation
1663
 * @minext: The minimum extent length
1664
 * @maxext: A pointer to the maximum extent structure
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
 *
 * This checks the current position in the rgrp to see whether there is
 * a reservation covering this block. If not then this function is a
 * no-op. If there is, then the position is moved to the end of the
 * contiguous reservation(s) so that we are pointing at the first
 * non-reserved block.
 *
 * Returns: 0 if no reservation, 1 if @rbm has changed, otherwise an error
 */

static int gfs2_reservation_check_and_update(struct gfs2_rbm *rbm,
1676
					     struct gfs2_blkreserv *rs,
1677 1678
					     u32 minext,
					     struct gfs2_extent *maxext)
1679 1680
{
	u64 block = gfs2_rbm_to_block(rbm);
1681
	u32 extlen = 1;
1682 1683
	u64 nblock;

1684 1685 1686 1687
	/*
	 * If we have a minimum extent length, then skip over any extent
	 * which is less than the min extent length in size.
	 */
1688
	if (minext > 1) {
1689
		extlen = gfs2_free_extlen(rbm, minext);
1690
		if (extlen <= maxext->len)
1691 1692 1693 1694 1695 1696 1697
			goto fail;
	}

	/*
	 * Check the extent which has been found against the reservations
	 * and skip if parts of it are already reserved
	 */
1698
	nblock = gfs2_next_unreserved_block(rbm->rgd, block, extlen, rs);
1699 1700 1701 1702 1703 1704 1705 1706
	if (nblock == block) {
		if (!minext || extlen >= minext)
			return 0;

		if (extlen > maxext->len) {
			maxext->len = extlen;
			maxext->rbm = *rbm;
		}
1707 1708 1709 1710 1711
	} else {
		u64 len = nblock - block;
		if (len >= (u64)1 << 32)
			return -E2BIG;
		extlen = len;
1712
	}
1713 1714 1715
fail:
	if (gfs2_rbm_add(rbm, extlen))
		return -E2BIG;
1716 1717 1718 1719 1720 1721 1722
	return 1;
}

/**
 * gfs2_rbm_find - Look for blocks of a particular state
 * @rbm: Value/result starting position and final position
 * @state: The state which we want to find
1723
 * @minext: Pointer to the requested extent length
1724
 *          This is updated to be the actual reservation size.
1725
 * @rs: Our own reservation (NULL to skip checking for reservations)
1726 1727 1728 1729 1730 1731
 * @nowrap: Stop looking at the end of the rgrp, rather than wrapping
 *          around until we've reached the starting point.
 *
 * Side effects:
 * - If looking for free blocks, we set GBF_FULL on each bitmap which
 *   has no free blocks in it.
1732 1733
 * - If looking for free blocks, we set rd_extfail_pt on each rgrp which
 *   has come up short on a free block search.
1734 1735 1736 1737
 *
 * Returns: 0 on success, -ENOSPC if there is no block of the requested state
 */

1738
static int gfs2_rbm_find(struct gfs2_rbm *rbm, u8 state, u32 *minext,
1739
			 struct gfs2_blkreserv *rs, bool nowrap)
1740
{
1741
	bool scan_from_start = rbm->bii == 0 && rbm->offset == 0;
1742
	struct buffer_head *bh;
1743
	int last_bii;
1744 1745
	u32 offset;
	u8 *buffer;
1746
	bool wrapped = false;
1747
	int ret;
Bob Peterson's avatar
Bob Peterson committed
1748
	struct gfs2_bitmap *bi;
1749
	struct gfs2_extent maxext = { .rbm.rgd = rbm->rgd, };
1750

1751 1752 1753 1754
	/*
	 * Determine the last bitmap to search.  If we're not starting at the
	 * beginning of a bitmap, we need to search that bitmap twice to scan
	 * the entire resource group.
1755
	 */
1756
	last_bii = rbm->bii - (rbm->offset == 0);
1757 1758

	while(1) {
Bob Peterson's avatar
Bob Peterson committed
1759
		bi = rbm_bi(rbm);
1760
		if (test_bit(GBF_FULL, &bi->bi_flags) &&
1761 1762 1763
		    (state == GFS2_BLKST_FREE))
			goto next_bitmap;

Bob Peterson's avatar
Bob Peterson committed
1764 1765
		bh = bi->bi_bh;
		buffer = bh->b_data + bi->bi_offset;
1766
		WARN_ON(!buffer_uptodate(bh));
Bob Peterson's avatar
Bob Peterson committed
1767 1768
		if (state != GFS2_BLKST_UNLINKED && bi->bi_clone)
			buffer = bi->bi_clone + bi->bi_offset;
1769
		offset = gfs2_bitfit(buffer, bi->bi_bytes, rbm->offset, state);
1770 1771 1772 1773 1774
		if (offset == BFITNOENT) {
			if (state == GFS2_BLKST_FREE && rbm->offset == 0)
				set_bit(GBF_FULL, &bi->bi_flags);
			goto next_bitmap;
		}
1775
		rbm->offset = offset;
1776
		if (!rs || !minext)
1777 1778
			return 0;

1779
		ret = gfs2_reservation_check_and_update(rbm, rs, *minext,
1780
							&maxext);
1781 1782
		if (ret == 0)
			return 0;
1783
		if (ret > 0)
Bob Peterson's avatar
Bob Peterson committed
1784
			goto next_iter;
1785
		if (ret == -E2BIG) {
Bob Peterson's avatar
Bob Peterson committed
1786
			rbm->bii = 0;
1787 1788 1789
			rbm->offset = 0;
			goto res_covered_end_of_rgrp;
		}
1790 1791 1792 1793
		return ret;

next_bitmap:	/* Find next bitmap in the rgrp */
		rbm->offset = 0;
Bob Peterson's avatar
Bob Peterson committed
1794 1795 1796
		rbm->bii++;
		if (rbm->bii == rbm->rgd->rd_length)
			rbm->bii = 0;
1797
res_covered_end_of_rgrp:
1798 1799 1800 1801 1802 1803 1804
		if (rbm->bii == 0) {
			if (wrapped)
				break;
			wrapped = true;
			if (nowrap)
				break;
		}
Bob Peterson's avatar
Bob Peterson committed
1805
next_iter:
1806 1807
		/* Have we scanned the entire resource group? */
		if (wrapped && rbm->bii > last_bii)
1808 1809 1810
			break;
	}

1811
	if (state != GFS2_BLKST_FREE)
1812 1813
		return -ENOSPC;

1814 1815 1816
	/* If the extent was too small, and it's smaller than the smallest
	   to have failed before, remember for future reference that it's
	   useless to search this rgrp again for this amount or more. */
1817 1818
	if (wrapped && (scan_from_start || rbm->bii > last_bii) &&
	    *minext < rbm->rgd->rd_extfail_pt)
1819
		rbm->rgd->rd_extfail_pt = *minext - 1;
1820

1821 1822 1823 1824 1825 1826 1827 1828
	/* If the maximum extent we found is big enough to fulfill the
	   minimum requirements, use it anyway. */
	if (maxext.len) {
		*rbm = maxext.rbm;
		*minext = maxext.len;
		return 0;
	}

1829 1830 1831
	return -ENOSPC;
}

1832 1833 1834
/**
 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
 * @rgd: The rgrp
1835 1836
 * @last_unlinked: block address of the last dinode we unlinked
 * @skip: block address we should explicitly not unlink
1837
 *
Bob Peterson's avatar
Bob Peterson committed
1838 1839
 * Returns: 0 if no error
 *          The inode, if one has been found, in inode.
1840 1841
 */

1842
static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
1843
{
1844
	u64 block;
1845
	struct gfs2_sbd *sdp = rgd->rd_sbd;
1846 1847 1848 1849
	struct gfs2_glock *gl;
	struct gfs2_inode *ip;
	int error;
	int found = 0;
Bob Peterson's avatar
Bob Peterson committed
1850
	struct gfs2_rbm rbm = { .rgd = rgd, .bii = 0, .offset = 0 };
1851

1852
	while (1) {
1853
		error = gfs2_rbm_find(&rbm, GFS2_BLKST_UNLINKED, NULL, NULL,
1854
				      true);
1855 1856 1857
		if (error == -ENOSPC)
			break;
		if (WARN_ON_ONCE(error))
1858
			break;
1859

1860 1861 1862 1863
		block = gfs2_rbm_to_block(&rbm);
		if (gfs2_rbm_from_block(&rbm, block + 1))
			break;
		if (*last_unlinked != NO_BLOCK && block <= *last_unlinked)
1864
			continue;
1865
		if (block == skip)
1866
			continue;
1867
		*last_unlinked = block;
1868

1869
		error = gfs2_glock_get(sdp, block, &gfs2_iopen_glops, CREATE, &gl);
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
		if (error)
			continue;

		/* If the inode is already in cache, we can ignore it here
		 * because the existing inode disposal code will deal with
		 * it when all refs have gone away. Accessing gl_object like
		 * this is not safe in general. Here it is ok because we do
		 * not dereference the pointer, and we only need an approx
		 * answer to whether it is NULL or not.
		 */
		ip = gl->gl_object;

1882
		if (ip || !gfs2_queue_try_to_evict(gl))
1883 1884 1885 1886 1887
			gfs2_glock_put(gl);
		else
			found++;

		/* Limit reclaim to sensible number of tasks */
1888
		if (found > NR_CPUS)
1889
			return;
1890 1891 1892
	}

	rgd->rd_flags &= ~GFS2_RDF_CHECK;
1893
	return;
1894 1895
}

1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
/**
 * gfs2_rgrp_congested - Use stats to figure out whether an rgrp is congested
 * @rgd: The rgrp in question
 * @loops: An indication of how picky we can be (0=very, 1=less so)
 *
 * This function uses the recently added glock statistics in order to
 * figure out whether a parciular resource group is suffering from
 * contention from multiple nodes. This is done purely on the basis
 * of timings, since this is the only data we have to work with and
 * our aim here is to reject a resource group which is highly contended
 * but (very important) not to do this too often in order to ensure that
 * we do not land up introducing fragmentation by changing resource
 * groups when not actually required.
 *
 * The calculation is fairly simple, we want to know whether the SRTTB
 * (i.e. smoothed round trip time for blocking operations) to acquire
 * the lock for this rgrp's glock is significantly greater than the
 * time taken for resource groups on average. We introduce a margin in
 * the form of the variable @var which is computed as the sum of the two
 * respective variences, and multiplied by a factor depending on @loops
 * and whether we have a lot of data to base the decision on. This is
 * then tested against the square difference of the means in order to
 * decide whether the result is statistically significant or not.
 *
 * Returns: A boolean verdict on the congestion status
 */

static bool gfs2_rgrp_congested(const struct gfs2_rgrpd *rgd, int loops)
{
	const struct gfs2_glock *gl = rgd->rd_gl;
1926
	const struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1927
	struct gfs2_lkstats *st;
1928 1929
	u64 r_dcount, l_dcount;
	u64 l_srttb, a_srttb = 0;
1930
	s64 srttb_diff;
1931 1932
	u64 sqr_diff;
	u64 var;
1933
	int cpu, nonzero = 0;
1934 1935

	preempt_disable();
1936 1937
	for_each_present_cpu(cpu) {
		st = &per_cpu_ptr(sdp->sd_lkstats, cpu)->lkstats[LM_TYPE_RGRP];
1938 1939 1940 1941
		if (st->stats[GFS2_LKS_SRTTB]) {
			a_srttb += st->stats[GFS2_LKS_SRTTB];
			nonzero++;
		}
1942
	}
1943
	st = &this_cpu_ptr(sdp->sd_lkstats)->lkstats[LM_TYPE_RGRP];
1944 1945
	if (nonzero)
		do_div(a_srttb, nonzero);
1946 1947 1948 1949 1950 1951 1952 1953
	r_dcount = st->stats[GFS2_LKS_DCOUNT];
	var = st->stats[GFS2_LKS_SRTTVARB] +
	      gl->gl_stats.stats[GFS2_LKS_SRTTVARB];
	preempt_enable();

	l_srttb = gl->gl_stats.stats[GFS2_LKS_SRTTB];
	l_dcount = gl->gl_stats.stats[GFS2_LKS_DCOUNT];

1954
	if ((l_dcount < 1) || (r_dcount < 1) || (a_srttb == 0))
1955 1956
		return false;

1957
	srttb_diff = a_srttb - l_srttb;
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
	sqr_diff = srttb_diff * srttb_diff;

	var *= 2;
	if (l_dcount < 8 || r_dcount < 8)
		var *= 2;
	if (loops == 1)
		var *= 2;

	return ((srttb_diff < 0) && (sqr_diff > var));
}

/**
1970
 * gfs2_rgrp_used_recently - test if an rgrp has been used recently
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
 * @rs: The block reservation with the rgrp to test
 * @msecs: The time limit in milliseconds
 *
 * Returns: True if the rgrp glock has been used within the time limit
 */
static bool gfs2_rgrp_used_recently(const struct gfs2_blkreserv *rs,
				    u64 msecs)
{
	u64 tdiff;

	tdiff = ktime_to_ns(ktime_sub(ktime_get_real(),
1982
                            rs->rs_rgd->rd_gl->gl_dstamp));
1983 1984 1985 1986

	return tdiff > (msecs * 1000 * 1000);
}

1987 1988 1989 1990 1991 1992 1993 1994 1995
static u32 gfs2_orlov_skip(const struct gfs2_inode *ip)
{
	const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
	u32 skip;

	get_random_bytes(&skip, sizeof(skip));
	return skip % sdp->sd_rgrps;
}

1996 1997 1998
static bool gfs2_select_rgrp(struct gfs2_rgrpd **pos, const struct gfs2_rgrpd *begin)
{
	struct gfs2_rgrpd *rgd = *pos;
1999
	struct gfs2_sbd *sdp = rgd->rd_sbd;
2000 2001 2002

	rgd = gfs2_rgrpd_get_next(rgd);
	if (rgd == NULL)
2003
		rgd = gfs2_rgrpd_get_first(sdp);
2004 2005 2006 2007 2008 2009
	*pos = rgd;
	if (rgd != begin) /* If we didn't wrap */
		return true;
	return false;
}

2010 2011
/**
 * fast_to_acquire - determine if a resource group will be fast to acquire
2012
 * @rgd: The rgrp
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
 *
 * If this is one of our preferred rgrps, it should be quicker to acquire,
 * because we tried to set ourselves up as dlm lock master.
 */
static inline int fast_to_acquire(struct gfs2_rgrpd *rgd)
{
	struct gfs2_glock *gl = rgd->rd_gl;

	if (gl->gl_state != LM_ST_UNLOCKED && list_empty(&gl->gl_holders) &&
	    !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
	    !test_bit(GLF_DEMOTE, &gl->gl_flags))
		return 1;
	if (rgd->rd_flags & GFS2_RDF_PREFERRED)
		return 1;
	return 0;
}

David Teigland's avatar
David Teigland committed
2030
/**
2031
 * gfs2_inplace_reserve - Reserve space in the filesystem
David Teigland's avatar
David Teigland committed
2032
 * @ip: the inode to reserve space for
2033
 * @ap: the allocation parameters
David Teigland's avatar
David Teigland committed
2034
 *
2035 2036 2037
 * We try our best to find an rgrp that has at least ap->target blocks
 * available. After a couple of passes (loops == 2), the prospects of finding
 * such an rgrp diminish. At this stage, we return the first rgrp that has
2038
 * at least ap->min_target blocks available.
2039 2040 2041 2042
 *
 * Returns: 0 on success,
 *          -ENOMEM if a suitable rgrp can't be found
 *          errno otherwise
David Teigland's avatar
David Teigland committed
2043 2044
 */

2045
int gfs2_inplace_reserve(struct gfs2_inode *ip, struct gfs2_alloc_parms *ap)
David Teigland's avatar
David Teigland committed
2046
{
2047
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
2048
	struct gfs2_rgrpd *begin = NULL;
2049
	struct gfs2_blkreserv *rs = &ip->i_res;
2050
	int error = 0, flags = LM_FLAG_NODE_SCOPE;
2051
	bool rg_locked;
2052
	u64 last_unlinked = NO_BLOCK;
2053
	u32 target = ap->target;
2054
	int loops = 0;
2055 2056 2057
	u32 free_blocks, blocks_available, skip = 0;

	BUG_ON(rs->rs_reserved);
David Teigland's avatar
David Teigland committed
2058

2059 2060
	if (sdp->sd_args.ar_rgrplvb)
		flags |= GL_SKIP;
2061
	if (gfs2_assert_warn(sdp, target))
2062
		return -EINVAL;
2063
	if (gfs2_rs_active(rs)) {
2064 2065 2066 2067
		begin = rs->rs_rgd;
	} else if (rs->rs_rgd &&
		   rgrp_contains_block(rs->rs_rgd, ip->i_goal)) {
		begin = rs->rs_rgd;
2068
	} else {
2069
		check_and_update_goal(ip);
2070
		rs->rs_rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal, 1);
2071
	}
2072
	if (S_ISDIR(ip->i_inode.i_mode) && (ap->aflags & GFS2_AF_ORLOV))
2073
		skip = gfs2_orlov_skip(ip);
2074
	if (rs->rs_rgd == NULL)
2075 2076 2077
		return -EBADSLT;

	while (loops < 3) {
2078 2079
		struct gfs2_rgrpd *rgd;

2080 2081 2082 2083
		rg_locked = gfs2_glock_is_locked_by_me(rs->rs_rgd->rd_gl);
		if (rg_locked) {
			rgrp_lock_local(rs->rs_rgd);
		} else {
2084 2085
			if (skip && skip--)
				goto next_rgrp;
2086 2087
			if (!gfs2_rs_active(rs)) {
				if (loops == 0 &&
2088
				    !fast_to_acquire(rs->rs_rgd))
2089 2090 2091
					goto next_rgrp;
				if ((loops < 2) &&
				    gfs2_rgrp_used_recently(rs, 1000) &&
2092
				    gfs2_rgrp_congested(rs->rs_rgd, loops))
2093 2094
					goto next_rgrp;
			}
2095
			error = gfs2_glock_nq_init(rs->rs_rgd->rd_gl,
2096
						   LM_ST_EXCLUSIVE, flags,
2097
						   &ip->i_rgd_gh);
2098 2099
			if (unlikely(error))
				return error;
2100
			rgrp_lock_local(rs->rs_rgd);
2101
			if (!gfs2_rs_active(rs) && (loops < 2) &&
2102
			    gfs2_rgrp_congested(rs->rs_rgd, loops))
2103
				goto skip_rgrp;
2104
			if (sdp->sd_args.ar_rgrplvb) {
2105 2106
				error = update_rgrp_lvb(rs->rs_rgd,
							&ip->i_rgd_gh);
2107
				if (unlikely(error)) {
2108
					rgrp_unlock_local(rs->rs_rgd);
2109
					gfs2_glock_dq_uninit(&ip->i_rgd_gh);
2110 2111 2112
					return error;
				}
			}
2113
		}
2114

2115
		/* Skip unusable resource groups */
2116
		if ((rs->rs_rgd->rd_flags & (GFS2_RGF_NOALLOC |
2117
						 GFS2_RDF_ERROR)) ||
2118
		    (loops == 0 && target > rs->rs_rgd->rd_extfail_pt))
2119 2120
			goto skip_rgrp;

2121 2122 2123 2124 2125
		if (sdp->sd_args.ar_rgrplvb) {
			error = gfs2_instantiate(&ip->i_rgd_gh);
			if (error)
				goto skip_rgrp;
		}
2126 2127 2128

		/* Get a reservation if we don't already have one */
		if (!gfs2_rs_active(rs))
2129
			rg_mblk_search(rs->rs_rgd, ip, ap);
2130 2131 2132 2133 2134 2135

		/* Skip rgrps when we can't get a reservation on first pass */
		if (!gfs2_rs_active(rs) && (loops < 1))
			goto check_rgrp;

		/* If rgrp has enough free space, use it */
2136 2137 2138 2139 2140 2141 2142
		rgd = rs->rs_rgd;
		spin_lock(&rgd->rd_rsspin);
		free_blocks = rgd_free(rgd, rs);
		blocks_available = rgd->rd_free_clone - rgd->rd_reserved;
		if (free_blocks < target || blocks_available < target) {
			spin_unlock(&rgd->rd_rsspin);
			goto check_rgrp;
David Teigland's avatar
David Teigland committed
2143
		}
2144 2145 2146 2147 2148
		rs->rs_reserved = ap->target;
		if (rs->rs_reserved > blocks_available)
			rs->rs_reserved = blocks_available;
		rgd->rd_reserved += rs->rs_reserved;
		spin_unlock(&rgd->rd_rsspin);
2149
		rgrp_unlock_local(rs->rs_rgd);
2150
		return 0;
2151 2152
check_rgrp:
		/* Check for unlinked inodes which can be reclaimed */
2153 2154
		if (rs->rs_rgd->rd_flags & GFS2_RDF_CHECK)
			try_rgrp_unlink(rs->rs_rgd, &last_unlinked,
2155 2156
					ip->i_no_addr);
skip_rgrp:
2157 2158
		rgrp_unlock_local(rs->rs_rgd);

2159 2160 2161 2162
		/* Drop reservation, if we couldn't use reserved rgrp */
		if (gfs2_rs_active(rs))
			gfs2_rs_deltree(rs);

2163 2164
		/* Unlock rgrp if required */
		if (!rg_locked)
2165
			gfs2_glock_dq_uninit(&ip->i_rgd_gh);
2166 2167
next_rgrp:
		/* Find the next rgrp, and continue looking */
2168
		if (gfs2_select_rgrp(&rs->rs_rgd, begin))
2169
			continue;
2170 2171
		if (skip)
			continue;
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184

		/* If we've scanned all the rgrps, but found no free blocks
		 * then this checks for some less likely conditions before
		 * trying again.
		 */
		loops++;
		/* Check that fs hasn't grown if writing to rindex */
		if (ip == GFS2_I(sdp->sd_rindex) && !sdp->sd_rindex_uptodate) {
			error = gfs2_ri_update(ip);
			if (error)
				return error;
		}
		/* Flushing the log may release space */
2185 2186 2187
		if (loops == 2) {
			if (ap->min_target)
				target = ap->min_target;
2188 2189
			gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
				       GFS2_LFC_INPLACE_RESERVE);
2190
		}
David Teigland's avatar
David Teigland committed
2191 2192
	}

2193
	return -ENOSPC;
David Teigland's avatar
David Teigland committed
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
}

/**
 * gfs2_inplace_release - release an inplace reservation
 * @ip: the inode the reservation was taken out on
 *
 * Release a reservation made by gfs2_inplace_reserve().
 */

void gfs2_inplace_release(struct gfs2_inode *ip)
{
2205 2206 2207 2208 2209 2210
	struct gfs2_blkreserv *rs = &ip->i_res;

	if (rs->rs_reserved) {
		struct gfs2_rgrpd *rgd = rs->rs_rgd;

		spin_lock(&rgd->rd_rsspin);
2211
		GLOCK_BUG_ON(rgd->rd_gl, rgd->rd_reserved < rs->rs_reserved);
2212 2213 2214 2215
		rgd->rd_reserved -= rs->rs_reserved;
		spin_unlock(&rgd->rd_rsspin);
		rs->rs_reserved = 0;
	}
2216 2217
	if (gfs2_holder_initialized(&ip->i_rgd_gh))
		gfs2_glock_dq_uninit(&ip->i_rgd_gh);
David Teigland's avatar
David Teigland committed
2218 2219
}

2220 2221
/**
 * gfs2_alloc_extent - allocate an extent from a given bitmap
2222
 * @rbm: the resource group information
2223
 * @dinode: TRUE if the first block we allocate is for a dinode
2224
 * @n: The extent length (value/result)
2225
 *
2226
 * Add the bitmap buffer to the transaction.
2227 2228
 * Set the found bits to @new_state to change block's allocation state.
 */
2229
static void gfs2_alloc_extent(const struct gfs2_rbm *rbm, bool dinode,
2230
			     unsigned int *n)
2231
{
2232
	struct gfs2_rbm pos = { .rgd = rbm->rgd, };
2233
	const unsigned int elen = *n;
2234 2235
	u64 block;
	int ret;
2236

2237 2238
	*n = 1;
	block = gfs2_rbm_to_block(rbm);
Bob Peterson's avatar
Bob Peterson committed
2239
	gfs2_trans_add_meta(rbm->rgd->rd_gl, rbm_bi(rbm)->bi_bh);
2240
	gfs2_setbit(rbm, true, dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
2241
	block++;
2242
	while (*n < elen) {
2243
		ret = gfs2_rbm_from_block(&pos, block);
2244
		if (ret || gfs2_testbit(&pos, true) != GFS2_BLKST_FREE)
2245
			break;
Bob Peterson's avatar
Bob Peterson committed
2246
		gfs2_trans_add_meta(pos.rgd->rd_gl, rbm_bi(&pos)->bi_bh);
2247
		gfs2_setbit(&pos, true, GFS2_BLKST_USED);
2248
		(*n)++;
2249
		block++;
2250
	}
David Teigland's avatar
David Teigland committed
2251 2252 2253 2254 2255
}

/**
 * rgblk_free - Change alloc state of given block(s)
 * @sdp: the filesystem
2256
 * @rgd: the resource group the blocks are in
David Teigland's avatar
David Teigland committed
2257 2258 2259 2260 2261
 * @bstart: the start of a run of blocks to free
 * @blen: the length of the block run (all must lie within ONE RG!)
 * @new_state: GFS2_BLKST_XXX the after-allocation block state
 */

2262 2263
static void rgblk_free(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd,
		       u64 bstart, u32 blen, unsigned char new_state)
David Teigland's avatar
David Teigland committed
2264
{
2265
	struct gfs2_rbm rbm;
2266
	struct gfs2_bitmap *bi, *bi_prev = NULL;
David Teigland's avatar
David Teigland committed
2267

2268
	rbm.rgd = rgd;
2269
	if (WARN_ON_ONCE(gfs2_rbm_from_block(&rbm, bstart)))
2270
		return;
David Teigland's avatar
David Teigland committed
2271
	while (blen--) {
Bob Peterson's avatar
Bob Peterson committed
2272
		bi = rbm_bi(&rbm);
2273 2274 2275 2276 2277 2278
		if (bi != bi_prev) {
			if (!bi->bi_clone) {
				bi->bi_clone = kmalloc(bi->bi_bh->b_size,
						      GFP_NOFS | __GFP_NOFAIL);
				memcpy(bi->bi_clone + bi->bi_offset,
				       bi->bi_bh->b_data + bi->bi_offset,
2279
				       bi->bi_bytes);
2280 2281 2282
			}
			gfs2_trans_add_meta(rbm.rgd->rd_gl, bi->bi_bh);
			bi_prev = bi;
David Teigland's avatar
David Teigland committed
2283
		}
2284
		gfs2_setbit(&rbm, false, new_state);
2285
		gfs2_rbm_add(&rbm, 1);
David Teigland's avatar
David Teigland committed
2286 2287 2288 2289
	}
}

/**
2290 2291
 * gfs2_rgrp_dump - print out an rgrp
 * @seq: The iterator
2292
 * @rgd: The rgrp in question
2293
 * @fs_id_buf: pointer to file system id (if requested)
2294 2295 2296
 *
 */

2297
void gfs2_rgrp_dump(struct seq_file *seq, struct gfs2_rgrpd *rgd,
2298
		    const char *fs_id_buf)
2299
{
2300 2301 2302
	struct gfs2_blkreserv *trs;
	const struct rb_node *n;

2303
	spin_lock(&rgd->rd_rsspin);
2304
	gfs2_print_dbg(seq, "%s R: n:%llu f:%02x b:%u/%u i:%u q:%u r:%u e:%u\n",
2305
		       fs_id_buf,
2306
		       (unsigned long long)rgd->rd_addr, rgd->rd_flags,
2307
		       rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes,
2308
		       rgd->rd_requested, rgd->rd_reserved, rgd->rd_extfail_pt);
2309
	if (rgd->rd_sbd->sd_args.ar_rgrplvb && rgd->rd_rgl) {
2310 2311
		struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl;

2312
		gfs2_print_dbg(seq, "%s  L: f:%02x b:%u i:%u\n", fs_id_buf,
2313 2314 2315 2316
			       be32_to_cpu(rgl->rl_flags),
			       be32_to_cpu(rgl->rl_free),
			       be32_to_cpu(rgl->rl_dinodes));
	}
2317 2318
	for (n = rb_first(&rgd->rd_rstree); n; n = rb_next(&trs->rs_node)) {
		trs = rb_entry(n, struct gfs2_blkreserv, rs_node);
2319
		dump_rs(seq, trs, fs_id_buf);
2320 2321
	}
	spin_unlock(&rgd->rd_rsspin);
2322 2323
}

2324 2325 2326
static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
{
	struct gfs2_sbd *sdp = rgd->rd_sbd;
2327
	char fs_id_buf[sizeof(sdp->sd_fsname) + 7];
2328

2329
	fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
Steven Whitehouse's avatar
Steven Whitehouse committed
2330
		(unsigned long long)rgd->rd_addr);
2331
	fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
2332
	sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname);
2333
	gfs2_rgrp_dump(NULL, rgd, fs_id_buf);
2334 2335 2336
	rgd->rd_flags |= GFS2_RDF_ERROR;
}

2337
/**
2338 2339 2340 2341
 * gfs2_adjust_reservation - Adjust (or remove) a reservation after allocation
 * @ip: The inode we have just allocated blocks for
 * @rbm: The start of the allocated blocks
 * @len: The extent length
2342
 *
2343 2344 2345
 * Adjusts a reservation after an allocation has taken place. If the
 * reservation does not match the allocation, or if it is now empty
 * then it is removed.
2346
 */
2347 2348 2349

static void gfs2_adjust_reservation(struct gfs2_inode *ip,
				    const struct gfs2_rbm *rbm, unsigned len)
2350
{
2351
	struct gfs2_blkreserv *rs = &ip->i_res;
2352
	struct gfs2_rgrpd *rgd = rbm->rgd;
2353

2354 2355
	BUG_ON(rs->rs_reserved < len);
	rs->rs_reserved -= len;
2356
	if (gfs2_rs_active(rs)) {
2357 2358 2359 2360 2361 2362
		u64 start = gfs2_rbm_to_block(rbm);

		if (rs->rs_start == start) {
			unsigned int rlen;

			rs->rs_start += len;
2363 2364 2365
			rlen = min(rs->rs_requested, len);
			rs->rs_requested -= rlen;
			rgd->rd_requested -= rlen;
2366
			trace_gfs2_rs(rs, TRACE_RS_CLAIM);
2367
			if (rs->rs_start < rgd->rd_data0 + rgd->rd_data &&
2368
			    rs->rs_requested)
2369
				return;
2370 2371
			/* We used up our block reservation, so we should
			   reserve more blocks next time. */
2372
			atomic_add(RGRP_RSRV_ADDBLKS, &ip->i_sizehint);
2373
		}
2374
		__rs_deltree(rs);
2375 2376 2377
	}
}

2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393
/**
 * gfs2_set_alloc_start - Set starting point for block allocation
 * @rbm: The rbm which will be set to the required location
 * @ip: The gfs2 inode
 * @dinode: Flag to say if allocation includes a new inode
 *
 * This sets the starting point from the reservation if one is active
 * otherwise it falls back to guessing a start point based on the
 * inode's goal block or the last allocation point in the rgrp.
 */

static void gfs2_set_alloc_start(struct gfs2_rbm *rbm,
				 const struct gfs2_inode *ip, bool dinode)
{
	u64 goal;

2394
	if (gfs2_rs_active(&ip->i_res)) {
2395 2396 2397 2398 2399 2400
		goal = ip->i_res.rs_start;
	} else {
		if (!dinode && rgrp_contains_block(rbm->rgd, ip->i_goal))
			goal = ip->i_goal;
		else
			goal = rbm->rgd->rd_last_alloc + rbm->rgd->rd_data0;
2401
	}
2402 2403 2404 2405
	if (WARN_ON_ONCE(gfs2_rbm_from_block(rbm, goal))) {
		rbm->bii = 0;
		rbm->offset = 0;
	}
2406 2407
}

2408
/**
2409
 * gfs2_alloc_blocks - Allocate one or more blocks of data and/or a dinode
2410
 * @ip: the inode to allocate the block for
2411
 * @bn: Used to return the starting block number
2412
 * @nblocks: requested number of blocks/extent length (value/result)
2413
 * @dinode: 1 if we're allocating a dinode block, else 0
David Teigland's avatar
David Teigland committed
2414
 *
2415
 * Returns: 0 or error
David Teigland's avatar
David Teigland committed
2416 2417
 */

2418
int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks,
2419
		      bool dinode)
David Teigland's avatar
David Teigland committed
2420
{
2421
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
2422
	struct buffer_head *dibh;
2423
	struct gfs2_rbm rbm = { .rgd = ip->i_res.rs_rgd, };
2424
	u64 block; /* block, within the file system scope */
2425
	u32 minext = 1;
2426
	int error = -ENOSPC;
2427

2428 2429
	BUG_ON(ip->i_res.rs_reserved < *nblocks);

2430
	rgrp_lock_local(rbm.rgd);
2431 2432 2433 2434
	if (gfs2_rs_active(&ip->i_res)) {
		gfs2_set_alloc_start(&rbm, ip, dinode);
		error = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, &minext, &ip->i_res, false);
	}
2435
	if (error == -ENOSPC) {
2436
		gfs2_set_alloc_start(&rbm, ip, dinode);
2437
		error = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, &minext, NULL, false);
2438 2439
	}

2440
	/* Since all blocks are reserved in advance, this shouldn't happen */
2441
	if (error) {
2442
		fs_warn(sdp, "inum=%llu error=%d, nblocks=%u, full=%d fail_pt=%d\n",
2443
			(unsigned long long)ip->i_no_addr, error, *nblocks,
2444 2445
			test_bit(GBF_FULL, &rbm.rgd->rd_bits->bi_flags),
			rbm.rgd->rd_extfail_pt);
2446
		goto rgrp_error;
2447
	}
2448

2449 2450
	gfs2_alloc_extent(&rbm, dinode, nblocks);
	block = gfs2_rbm_to_block(&rbm);
2451
	rbm.rgd->rd_last_alloc = block - rbm.rgd->rd_data0;
2452
	if (!dinode) {
2453
		ip->i_goal = block + *nblocks - 1;
2454 2455 2456 2457
		error = gfs2_meta_inode_buffer(ip, &dibh);
		if (error == 0) {
			struct gfs2_dinode *di =
				(struct gfs2_dinode *)dibh->b_data;
2458
			gfs2_trans_add_meta(ip->i_gl, dibh);
2459 2460 2461 2462
			di->di_goal_meta = di->di_goal_data =
				cpu_to_be64(ip->i_goal);
			brelse(dibh);
		}
2463
	}
2464 2465 2466
	spin_lock(&rbm.rgd->rd_rsspin);
	gfs2_adjust_reservation(ip, &rbm, *nblocks);
	if (rbm.rgd->rd_free < *nblocks || rbm.rgd->rd_reserved < *nblocks) {
2467
		fs_warn(sdp, "nblocks=%u\n", *nblocks);
2468
		spin_unlock(&rbm.rgd->rd_rsspin);
2469
		goto rgrp_error;
2470
	}
2471 2472 2473
	GLOCK_BUG_ON(rbm.rgd->rd_gl, rbm.rgd->rd_reserved < *nblocks);
	GLOCK_BUG_ON(rbm.rgd->rd_gl, rbm.rgd->rd_free_clone < *nblocks);
	GLOCK_BUG_ON(rbm.rgd->rd_gl, rbm.rgd->rd_free < *nblocks);
2474 2475
	rbm.rgd->rd_reserved -= *nblocks;
	rbm.rgd->rd_free_clone -= *nblocks;
2476
	rbm.rgd->rd_free -= *nblocks;
2477
	spin_unlock(&rbm.rgd->rd_rsspin);
2478
	if (dinode) {
2479 2480
		u64 generation;

2481
		rbm.rgd->rd_dinodes++;
2482 2483 2484 2485
		generation = rbm.rgd->rd_igeneration++;
		if (generation == 0)
			generation = rbm.rgd->rd_igeneration++;
		ip->i_generation = generation;
2486
	}
David Teigland's avatar
David Teigland committed
2487

2488
	gfs2_trans_add_meta(rbm.rgd->rd_gl, rbm.rgd->rd_bits[0].bi_bh);
2489
	gfs2_rgrp_out(rbm.rgd, rbm.rgd->rd_bits[0].bi_bh->b_data);
2490
	rgrp_unlock_local(rbm.rgd);
David Teigland's avatar
David Teigland committed
2491

2492
	gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0);
2493
	if (dinode)
2494
		gfs2_trans_remove_revoke(sdp, block, *nblocks);
2495

2496
	gfs2_quota_change(ip, *nblocks, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teigland's avatar
David Teigland committed
2497

2498
	trace_gfs2_block_alloc(ip, rbm.rgd, block, *nblocks,
2499
			       dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
2500 2501 2502 2503
	*bn = block;
	return 0;

rgrp_error:
2504
	rgrp_unlock_local(rbm.rgd);
2505
	gfs2_rgrp_error(rbm.rgd);
2506
	return -EIO;
David Teigland's avatar
David Teigland committed
2507 2508 2509
}

/**
2510
 * __gfs2_free_blocks - free a contiguous run of block(s)
David Teigland's avatar
David Teigland committed
2511
 * @ip: the inode these blocks are being freed from
2512
 * @rgd: the resource group the blocks are in
David Teigland's avatar
David Teigland committed
2513 2514
 * @bstart: first block of a run of contiguous blocks
 * @blen: the length of the block run
2515
 * @meta: 1 if the blocks represent metadata
David Teigland's avatar
David Teigland committed
2516 2517 2518
 *
 */

2519 2520
void __gfs2_free_blocks(struct gfs2_inode *ip, struct gfs2_rgrpd *rgd,
			u64 bstart, u32 blen, int meta)
David Teigland's avatar
David Teigland committed
2521
{
2522
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teigland's avatar
David Teigland committed
2523

2524
	rgrp_lock_local(rgd);
2525
	rgblk_free(sdp, rgd, bstart, blen, GFS2_BLKST_FREE);
2526
	trace_gfs2_block_alloc(ip, rgd, bstart, blen, GFS2_BLKST_FREE);
2527
	rgd->rd_free += blen;
2528
	rgd->rd_flags &= ~GFS2_RGF_TRIMMED;
2529
	gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
2530
	gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
2531
	rgrp_unlock_local(rgd);
David Teigland's avatar
David Teigland committed
2532

2533
	/* Directories keep their data in the metadata address space */
2534 2535
	if (meta || ip->i_depth || gfs2_is_jdata(ip))
		gfs2_journal_wipe(ip, bstart, blen);
2536
}
David Teigland's avatar
David Teigland committed
2537

2538 2539 2540
/**
 * gfs2_free_meta - free a contiguous run of data block(s)
 * @ip: the inode these blocks are being freed from
2541
 * @rgd: the resource group the blocks are in
2542 2543 2544 2545 2546
 * @bstart: first block of a run of contiguous blocks
 * @blen: the length of the block run
 *
 */

2547 2548
void gfs2_free_meta(struct gfs2_inode *ip, struct gfs2_rgrpd *rgd,
		    u64 bstart, u32 blen)
2549 2550 2551
{
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);

2552
	__gfs2_free_blocks(ip, rgd, bstart, blen, 1);
David Teigland's avatar
David Teigland committed
2553
	gfs2_statfs_change(sdp, 0, +blen, 0);
2554
	gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teigland's avatar
David Teigland committed
2555 2556
}

2557 2558 2559 2560 2561
void gfs2_unlink_di(struct inode *inode)
{
	struct gfs2_inode *ip = GFS2_I(inode);
	struct gfs2_sbd *sdp = GFS2_SB(inode);
	struct gfs2_rgrpd *rgd;
2562
	u64 blkno = ip->i_no_addr;
2563

2564
	rgd = gfs2_blk2rgrpd(sdp, blkno, true);
2565 2566
	if (!rgd)
		return;
2567
	rgrp_lock_local(rgd);
2568
	rgblk_free(sdp, rgd, blkno, 1, GFS2_BLKST_UNLINKED);
2569
	trace_gfs2_block_alloc(ip, rgd, blkno, 1, GFS2_BLKST_UNLINKED);
2570
	gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
2571
	gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
2572
	be32_add_cpu(&rgd->rd_rgl->rl_unlinked, 1);
2573
	rgrp_unlock_local(rgd);
2574 2575
}

2576
void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
David Teigland's avatar
David Teigland committed
2577 2578 2579
{
	struct gfs2_sbd *sdp = rgd->rd_sbd;

2580
	rgrp_lock_local(rgd);
2581
	rgblk_free(sdp, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE);
2582
	if (!rgd->rd_dinodes)
David Teigland's avatar
David Teigland committed
2583
		gfs2_consist_rgrpd(rgd);
2584
	rgd->rd_dinodes--;
2585
	rgd->rd_free++;
David Teigland's avatar
David Teigland committed
2586

2587
	gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
2588
	gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
2589
	be32_add_cpu(&rgd->rd_rgl->rl_unlinked, -1);
2590
	rgrp_unlock_local(rgd);
David Teigland's avatar
David Teigland committed
2591 2592

	gfs2_statfs_change(sdp, 0, +1, -1);
2593
	trace_gfs2_block_alloc(ip, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE);
2594
	gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
2595
	gfs2_journal_wipe(ip, ip->i_no_addr, 1);
David Teigland's avatar
David Teigland committed
2596 2597
}

2598 2599 2600 2601 2602 2603
/**
 * gfs2_check_blk_type - Check the type of a block
 * @sdp: The superblock
 * @no_addr: The block number to check
 * @type: The block type we are looking for
 *
2604 2605 2606 2607
 * The inode glock of @no_addr must be held.  The @type to check for is either
 * GFS2_BLKST_DINODE or GFS2_BLKST_UNLINKED; checking for type GFS2_BLKST_FREE
 * or GFS2_BLKST_USED would make no sense.
 *
2608 2609 2610 2611 2612 2613 2614 2615
 * Returns: 0 if the block type matches the expected type
 *          -ESTALE if it doesn't match
 *          or -ve errno if something went wrong while checking
 */

int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
{
	struct gfs2_rgrpd *rgd;
2616
	struct gfs2_holder rgd_gh;
2617
	struct gfs2_rbm rbm;
2618
	int error = -EINVAL;
2619

2620
	rgd = gfs2_blk2rgrpd(sdp, no_addr, 1);
2621
	if (!rgd)
2622
		goto fail;
2623 2624 2625

	error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
	if (error)
2626
		goto fail;
2627

2628 2629
	rbm.rgd = rgd;
	error = gfs2_rbm_from_block(&rbm, no_addr);
2630
	if (!WARN_ON_ONCE(error)) {
2631 2632 2633 2634 2635 2636 2637
		/*
		 * No need to take the local resource group lock here; the
		 * inode glock of @no_addr provides the necessary
		 * synchronization in case the block is an inode.  (In case
		 * the block is not an inode, the block type will not match
		 * the @type we are looking for.)
		 */
2638 2639 2640
		if (gfs2_testbit(&rbm, false) != type)
			error = -ESTALE;
	}
2641 2642

	gfs2_glock_dq_uninit(&rgd_gh);
2643

2644 2645 2646 2647
fail:
	return error;
}

David Teigland's avatar
David Teigland committed
2648 2649
/**
 * gfs2_rlist_add - add a RG to a list of RGs
2650
 * @ip: the inode
David Teigland's avatar
David Teigland committed
2651 2652 2653 2654 2655 2656 2657 2658 2659
 * @rlist: the list of resource groups
 * @block: the block
 *
 * Figure out what RG a block belongs to and add that RG to the list
 *
 * FIXME: Don't use NOFAIL
 *
 */

2660
void gfs2_rlist_add(struct gfs2_inode *ip, struct gfs2_rgrp_list *rlist,
2661
		    u64 block)
David Teigland's avatar
David Teigland committed
2662
{
2663
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teigland's avatar
David Teigland committed
2664 2665 2666 2667 2668 2669 2670 2671
	struct gfs2_rgrpd *rgd;
	struct gfs2_rgrpd **tmp;
	unsigned int new_space;
	unsigned int x;

	if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
		return;

2672 2673 2674 2675 2676 2677 2678 2679
	/*
	 * The resource group last accessed is kept in the last position.
	 */

	if (rlist->rl_rgrps) {
		rgd = rlist->rl_rgd[rlist->rl_rgrps - 1];
		if (rgrp_contains_block(rgd, block))
			return;
2680
		rgd = gfs2_blk2rgrpd(sdp, block, 1);
2681
	} else {
2682
		rgd = ip->i_res.rs_rgd;
2683 2684 2685 2686
		if (!rgd || !rgrp_contains_block(rgd, block))
			rgd = gfs2_blk2rgrpd(sdp, block, 1);
	}

David Teigland's avatar
David Teigland committed
2687
	if (!rgd) {
2688 2689
		fs_err(sdp, "rlist_add: no rgrp for block %llu\n",
		       (unsigned long long)block);
David Teigland's avatar
David Teigland committed
2690 2691 2692
		return;
	}

2693 2694 2695 2696
	for (x = 0; x < rlist->rl_rgrps; x++) {
		if (rlist->rl_rgd[x] == rgd) {
			swap(rlist->rl_rgd[x],
			     rlist->rl_rgd[rlist->rl_rgrps - 1]);
David Teigland's avatar
David Teigland committed
2697
			return;
2698 2699
		}
	}
David Teigland's avatar
David Teigland committed
2700 2701 2702 2703 2704

	if (rlist->rl_rgrps == rlist->rl_space) {
		new_space = rlist->rl_space + 10;

		tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
2705
			      GFP_NOFS | __GFP_NOFAIL);
David Teigland's avatar
David Teigland committed
2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723

		if (rlist->rl_rgd) {
			memcpy(tmp, rlist->rl_rgd,
			       rlist->rl_space * sizeof(struct gfs2_rgrpd *));
			kfree(rlist->rl_rgd);
		}

		rlist->rl_space = new_space;
		rlist->rl_rgd = tmp;
	}

	rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
}

/**
 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
 *      and initialize an array of glock holders for them
 * @rlist: the list of resource groups
2724 2725
 * @state: the state we're requesting
 * @flags: the modifier flags
David Teigland's avatar
David Teigland committed
2726 2727 2728 2729 2730
 *
 * FIXME: Don't use NOFAIL
 *
 */

2731 2732
void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist,
		      unsigned int state, u16 flags)
David Teigland's avatar
David Teigland committed
2733 2734 2735
{
	unsigned int x;

2736 2737 2738
	rlist->rl_ghs = kmalloc_array(rlist->rl_rgrps,
				      sizeof(struct gfs2_holder),
				      GFP_NOFS | __GFP_NOFAIL);
David Teigland's avatar
David Teigland committed
2739
	for (x = 0; x < rlist->rl_rgrps; x++)
2740 2741
		gfs2_holder_init(rlist->rl_rgd[x]->rd_gl, state, flags,
				 &rlist->rl_ghs[x]);
David Teigland's avatar
David Teigland committed
2742 2743 2744 2745
}

/**
 * gfs2_rlist_free - free a resource group list
2746
 * @rlist: the list of resource groups
David Teigland's avatar
David Teigland committed
2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759
 *
 */

void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
{
	unsigned int x;

	kfree(rlist->rl_rgd);

	if (rlist->rl_ghs) {
		for (x = 0; x < rlist->rl_rgrps; x++)
			gfs2_holder_uninit(&rlist->rl_ghs[x]);
		kfree(rlist->rl_ghs);
2760
		rlist->rl_ghs = NULL;
David Teigland's avatar
David Teigland committed
2761 2762 2763
	}
}

2764 2765 2766 2767 2768 2769 2770 2771 2772
void rgrp_lock_local(struct gfs2_rgrpd *rgd)
{
	mutex_lock(&rgd->rd_mutex);
}

void rgrp_unlock_local(struct gfs2_rgrpd *rgd)
{
	mutex_unlock(&rgd->rd_mutex);
}