Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/gfs2_ondisk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/prefetch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/rbtree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include "gfs2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include "incore.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include "glock.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include "glops.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include "lops.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include "meta_io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include "quota.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include "rgrp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include "super.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include "trans.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include "util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include "log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include "inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include "trace_gfs2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include "dir.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define BFITNOENT ((u32)~0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define NO_BLOCK ((u64)~0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  * These routines are used by the resource group routines (rgrp.c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * to keep track of block allocation.  Each block is represented by two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * bits.  So, each byte represents GFS2_NBBY (i.e. 4) blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  * 0 = Free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * 1 = Used (not metadata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  * 2 = Unlinked (still in use) inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  * 3 = Used (metadata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) struct gfs2_extent {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	struct gfs2_rbm rbm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) static const char valid_change[16] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	        /* current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	/* n */ 0, 1, 1, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	/* e */ 1, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	/* w */ 0, 0, 0, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	        1, 0, 0, 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) static int gfs2_rbm_find(struct gfs2_rbm *rbm, u8 state, u32 *minext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 			 const struct gfs2_inode *ip, bool nowrap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68)  * gfs2_setbit - Set a bit in the bitmaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * @rbm: The position of the bit to set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * @do_clone: Also set the clone bitmap, if it exists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * @new_state: the new state of the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) static inline void gfs2_setbit(const struct gfs2_rbm *rbm, bool do_clone,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 			       unsigned char new_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	unsigned char *byte1, *byte2, *end, cur_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	struct gfs2_bitmap *bi = rbm_bi(rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	unsigned int buflen = bi->bi_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	const unsigned int bit = (rbm->offset % GFS2_NBBY) * GFS2_BIT_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	byte1 = bi->bi_bh->b_data + bi->bi_offset + (rbm->offset / GFS2_NBBY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	end = bi->bi_bh->b_data + bi->bi_offset + buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	BUG_ON(byte1 >= end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	cur_state = (*byte1 >> bit) & GFS2_BIT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	if (unlikely(!valid_change[new_state * 4 + cur_state])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 		struct gfs2_sbd *sdp = rbm->rgd->rd_sbd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 		fs_warn(sdp, "buf_blk = 0x%x old_state=%d, new_state=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 			rbm->offset, cur_state, new_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 		fs_warn(sdp, "rgrp=0x%llx bi_start=0x%x biblk: 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 			(unsigned long long)rbm->rgd->rd_addr, bi->bi_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 			(unsigned long long)bi->bi_bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 		fs_warn(sdp, "bi_offset=0x%x bi_bytes=0x%x block=0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 			bi->bi_offset, bi->bi_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 			(unsigned long long)gfs2_rbm_to_block(rbm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 		gfs2_consist_rgrpd(rbm->rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	*byte1 ^= (cur_state ^ new_state) << bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	if (do_clone && bi->bi_clone) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 		byte2 = bi->bi_clone + bi->bi_offset + (rbm->offset / GFS2_NBBY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 		cur_state = (*byte2 >> bit) & GFS2_BIT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 		*byte2 ^= (cur_state ^ new_state) << bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  * gfs2_testbit - test a bit in the bitmaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116)  * @rbm: The bit to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117)  * @use_clone: If true, test the clone bitmap, not the official bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119)  * Some callers like gfs2_unaligned_extlen need to test the clone bitmaps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120)  * not the "real" bitmaps, to avoid allocating recently freed blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122)  * Returns: The two bit block state of the requested bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) static inline u8 gfs2_testbit(const struct gfs2_rbm *rbm, bool use_clone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	struct gfs2_bitmap *bi = rbm_bi(rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	const u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	const u8 *byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	unsigned int bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	if (use_clone && bi->bi_clone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 		buffer = bi->bi_clone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		buffer = bi->bi_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	buffer += bi->bi_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	byte = buffer + (rbm->offset / GFS2_NBBY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	bit = (rbm->offset % GFS2_NBBY) * GFS2_BIT_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	return (*byte >> bit) & GFS2_BIT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144)  * gfs2_bit_search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145)  * @ptr: Pointer to bitmap data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146)  * @mask: Mask to use (normally 0x55555.... but adjusted for search start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147)  * @state: The state we are searching for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149)  * We xor the bitmap data with a patter which is the bitwise opposite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150)  * of what we are looking for, this gives rise to a pattern of ones
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151)  * wherever there is a match. Since we have two bits per entry, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152)  * take this pattern, shift it down by one place and then and it with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153)  * the original. All the even bit positions (0,2,4, etc) then represent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154)  * successful matches, so we mask with 0x55555..... to remove the unwanted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155)  * odd bit positions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157)  * This allows searching of a whole u64 at once (32 blocks) with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158)  * single test (on 64 bit arches).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	u64 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	static const u64 search[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		[0] = 0xffffffffffffffffULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		[1] = 0xaaaaaaaaaaaaaaaaULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		[2] = 0x5555555555555555ULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		[3] = 0x0000000000000000ULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	tmp = le64_to_cpu(*ptr) ^ search[state];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	tmp &= (tmp >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	tmp &= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	return tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177)  * rs_cmp - multi-block reservation range compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178)  * @blk: absolute file system block number of the new reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179)  * @len: number of blocks in the new reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180)  * @rs: existing reservation to compare against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182)  * returns: 1 if the block range is beyond the reach of the reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183)  *         -1 if the block range is before the start of the reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184)  *          0 if the block range overlaps with the reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) static inline int rs_cmp(u64 blk, u32 len, struct gfs2_blkreserv *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	u64 startblk = gfs2_rbm_to_block(&rs->rs_rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	if (blk >= startblk + rs->rs_free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	if (blk + len - 1 < startblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198)  * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199)  *       a block in a given allocation state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200)  * @buf: the buffer that holds the bitmaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201)  * @len: the length (in bytes) of the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202)  * @goal: start search at this block's bit-pair (within @buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203)  * @state: GFS2_BLKST_XXX the state of the block we're looking for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  * Scope of @goal and returned block number is only within this bitmap buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206)  * not entire rgrp or filesystem.  @buffer will be offset from the actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207)  * beginning of a bitmap block buffer, skipping any header structures, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208)  * headers are always a multiple of 64 bits long so that the buffer is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209)  * always aligned to a 64 bit boundary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211)  * The size of the buffer is in bytes, but is it assumed that it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212)  * always ok to read a complete multiple of 64 bits at the end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  * of the block in case the end is no aligned to a natural boundary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215)  * Return: the block number (bitmap buffer scope) that was found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) static u32 gfs2_bitfit(const u8 *buf, const unsigned int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 		       u32 goal, u8 state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	u32 spoint = (goal << 1) & ((8*sizeof(u64)) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	const __le64 *ptr = ((__le64 *)buf) + (goal >> 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	const __le64 *end = (__le64 *)(buf + ALIGN(len, sizeof(u64)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	u64 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	u64 mask = 0x5555555555555555ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	u32 bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	/* Mask off bits we don't care about at the start of the search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	mask <<= spoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	tmp = gfs2_bit_search(ptr, mask, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	ptr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	while(tmp == 0 && ptr < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		ptr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	/* Mask off any bits which are more than len bytes from the start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	if (ptr == end && (len & (sizeof(u64) - 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		tmp &= (((u64)~0) >> (64 - 8*(len & (sizeof(u64) - 1))));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	/* Didn't find anything, so return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	if (tmp == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		return BFITNOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	ptr--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	bit = __ffs64(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	bit /= 2;	/* two bits per entry in the bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249)  * gfs2_rbm_from_block - Set the rbm based upon rgd and block number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250)  * @rbm: The rbm with rgd already set correctly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251)  * @block: The block number (filesystem relative)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253)  * This sets the bi and offset members of an rbm based on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254)  * resource group and a filesystem relative block number. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255)  * resource group must be set in the rbm on entry, the bi and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256)  * offset members will be set by this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258)  * Returns: 0 on success, or an error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) static int gfs2_rbm_from_block(struct gfs2_rbm *rbm, u64 block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	if (!rgrp_contains_block(rbm->rgd, block))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	rbm->bii = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	rbm->offset = block - rbm->rgd->rd_data0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	/* Check if the block is within the first block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	if (rbm->offset < rbm_bi(rbm)->bi_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	/* Adjust for the size diff between gfs2_meta_header and gfs2_rgrp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	rbm->offset += (sizeof(struct gfs2_rgrp) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 			sizeof(struct gfs2_meta_header)) * GFS2_NBBY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	rbm->bii = rbm->offset / rbm->rgd->rd_sbd->sd_blocks_per_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	rbm->offset -= rbm->bii * rbm->rgd->rd_sbd->sd_blocks_per_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280)  * gfs2_rbm_incr - increment an rbm structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281)  * @rbm: The rbm with rgd already set correctly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283)  * This function takes an existing rbm structure and increments it to the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284)  * viable block offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286)  * Returns: If incrementing the offset would cause the rbm to go past the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287)  *          end of the rgrp, true is returned, otherwise false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) static bool gfs2_rbm_incr(struct gfs2_rbm *rbm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	if (rbm->offset + 1 < rbm_bi(rbm)->bi_blocks) { /* in the same bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		rbm->offset++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	if (rbm->bii == rbm->rgd->rd_length - 1) /* at the last bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	rbm->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	rbm->bii++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306)  * gfs2_unaligned_extlen - Look for free blocks which are not byte aligned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307)  * @rbm: Position to search (value/result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308)  * @n_unaligned: Number of unaligned blocks to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309)  * @len: Decremented for each block found (terminate on zero)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311)  * Returns: true if a non-free block is encountered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) static bool gfs2_unaligned_extlen(struct gfs2_rbm *rbm, u32 n_unaligned, u32 *len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	u32 n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	u8 res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	for (n = 0; n < n_unaligned; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		res = gfs2_testbit(rbm, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		if (res != GFS2_BLKST_FREE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		(*len)--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		if (*len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		if (gfs2_rbm_incr(rbm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334)  * gfs2_free_extlen - Return extent length of free blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335)  * @rrbm: Starting position
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336)  * @len: Max length to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338)  * Starting at the block specified by the rbm, see how many free blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339)  * there are, not reading more than len blocks ahead. This can be done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340)  * using memchr_inv when the blocks are byte aligned, but has to be done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341)  * on a block by block basis in case of unaligned blocks. Also this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342)  * function can cope with bitmap boundaries (although it must stop on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343)  * a resource group boundary)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345)  * Returns: Number of free blocks in the extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) static u32 gfs2_free_extlen(const struct gfs2_rbm *rrbm, u32 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	struct gfs2_rbm rbm = *rrbm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	u32 n_unaligned = rbm.offset & 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	u32 size = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	u32 bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	u32 chunk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	u8 *ptr, *start, *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	u64 block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	struct gfs2_bitmap *bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	if (n_unaligned &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	    gfs2_unaligned_extlen(&rbm, 4 - n_unaligned, &len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	n_unaligned = len & 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	/* Start is now byte aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	while (len > 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		bi = rbm_bi(&rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		start = bi->bi_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		if (bi->bi_clone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 			start = bi->bi_clone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		start += bi->bi_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		end = start + bi->bi_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		BUG_ON(rbm.offset & 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		start += (rbm.offset / GFS2_NBBY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		bytes = min_t(u32, len / GFS2_NBBY, (end - start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		ptr = memchr_inv(start, 0, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		chunk_size = ((ptr == NULL) ? bytes : (ptr - start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		chunk_size *= GFS2_NBBY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		BUG_ON(len < chunk_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		len -= chunk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		block = gfs2_rbm_to_block(&rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		if (gfs2_rbm_from_block(&rbm, block + chunk_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 			n_unaligned = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		if (ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 			n_unaligned = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		n_unaligned = len & 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	/* Deal with any bits left over at the end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	if (n_unaligned)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		gfs2_unaligned_extlen(&rbm, n_unaligned, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	return size - len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400)  * gfs2_bitcount - count the number of bits in a certain state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401)  * @rgd: the resource group descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402)  * @buffer: the buffer that holds the bitmaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403)  * @buflen: the length (in bytes) of the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404)  * @state: the state of the block we're looking for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406)  * Returns: The number of bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 			 unsigned int buflen, u8 state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	const u8 *byte = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	const u8 *end = buffer + buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	const u8 state1 = state << 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	const u8 state2 = state << 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	const u8 state3 = state << 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	u32 count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	for (; byte < end; byte++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		if (((*byte) & 0x03) == state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 			count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		if (((*byte) & 0x0C) == state1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 			count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		if (((*byte) & 0x30) == state2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 			count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		if (((*byte) & 0xC0) == state3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 			count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434)  * gfs2_rgrp_verify - Verify that a resource group is consistent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435)  * @rgd: the rgrp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	struct gfs2_sbd *sdp = rgd->rd_sbd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	struct gfs2_bitmap *bi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	u32 length = rgd->rd_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	u32 count[4], tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	int buf, x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	memset(count, 0, 4 * sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	/* Count # blocks in each of 4 possible allocation states */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	for (buf = 0; buf < length; buf++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		bi = rgd->rd_bits + buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		for (x = 0; x < 4; x++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 			count[x] += gfs2_bitcount(rgd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 						  bi->bi_bh->b_data +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 						  bi->bi_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 						  bi->bi_bytes, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	if (count[0] != rgd->rd_free) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		gfs2_lm(sdp, "free data mismatch:  %u != %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 			count[0], rgd->rd_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		gfs2_consist_rgrpd(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	if (count[1] != tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 		gfs2_lm(sdp, "used data mismatch:  %u != %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 			count[1], tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		gfs2_consist_rgrpd(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	if (count[2] + count[3] != rgd->rd_dinodes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		gfs2_lm(sdp, "used metadata mismatch:  %u != %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 			count[2] + count[3], rgd->rd_dinodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		gfs2_consist_rgrpd(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483)  * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484)  * @sdp: The GFS2 superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485)  * @blk: The data block number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486)  * @exact: True if this needs to be an exact match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488)  * The @exact argument should be set to true by most callers. The exception
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489)  * is when we need to match blocks which are not represented by the rgrp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490)  * bitmap, but which are part of the rgrp (i.e. padding blocks) which are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491)  * there for alignment purposes. Another way of looking at it is that @exact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492)  * matches only valid data/metadata blocks, but with @exact false, it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493)  * match any block within the extent of the rgrp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495)  * Returns: The resource group, or NULL if not found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk, bool exact)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	struct rb_node *n, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	struct gfs2_rgrpd *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	spin_lock(&sdp->sd_rindex_spin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	n = sdp->sd_rindex_tree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	while (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		cur = rb_entry(n, struct gfs2_rgrpd, rd_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		if (blk < cur->rd_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 			next = n->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		else if (blk >= cur->rd_data0 + cur->rd_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 			next = n->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		if (next == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 			spin_unlock(&sdp->sd_rindex_spin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 			if (exact) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 				if (blk < cur->rd_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 					return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 				if (blk >= cur->rd_data0 + cur->rd_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 					return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 			return cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		n = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	spin_unlock(&sdp->sd_rindex_spin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530)  * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531)  * @sdp: The GFS2 superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533)  * Returns: The first rgrp in the filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	const struct rb_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	struct gfs2_rgrpd *rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	spin_lock(&sdp->sd_rindex_spin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	n = rb_first(&sdp->sd_rindex_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	spin_unlock(&sdp->sd_rindex_spin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	return rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550)  * gfs2_rgrpd_get_next - get the next RG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551)  * @rgd: the resource group descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553)  * Returns: The next rgrp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	struct gfs2_sbd *sdp = rgd->rd_sbd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	const struct rb_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	spin_lock(&sdp->sd_rindex_spin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	n = rb_next(&rgd->rd_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	if (n == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		n = rb_first(&sdp->sd_rindex_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	if (unlikely(&rgd->rd_node == n)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		spin_unlock(&sdp->sd_rindex_spin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	spin_unlock(&sdp->sd_rindex_spin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	return rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) void check_and_update_goal(struct gfs2_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	if (!ip->i_goal || gfs2_blk2rgrpd(sdp, ip->i_goal, 1) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		ip->i_goal = ip->i_no_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) void gfs2_free_clones(struct gfs2_rgrpd *rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	int x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	for (x = 0; x < rgd->rd_length; x++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		struct gfs2_bitmap *bi = rgd->rd_bits + x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		kfree(bi->bi_clone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		bi->bi_clone = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) static void dump_rs(struct seq_file *seq, const struct gfs2_blkreserv *rs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		    const char *fs_id_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	struct gfs2_inode *ip = container_of(rs, struct gfs2_inode, i_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	gfs2_print_dbg(seq, "%s  B: n:%llu s:%llu b:%u f:%u\n", fs_id_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		       (unsigned long long)ip->i_no_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		       (unsigned long long)gfs2_rbm_to_block(&rs->rs_rbm),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		       rs->rs_rbm.offset, rs->rs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605)  * __rs_deltree - remove a multi-block reservation from the rgd tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606)  * @rs: The reservation to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) static void __rs_deltree(struct gfs2_blkreserv *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	struct gfs2_rgrpd *rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	if (!gfs2_rs_active(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	rgd = rs->rs_rbm.rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	trace_gfs2_rs(rs, TRACE_RS_TREEDEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	rb_erase(&rs->rs_node, &rgd->rd_rstree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	RB_CLEAR_NODE(&rs->rs_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	if (rs->rs_free) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		u64 last_block = gfs2_rbm_to_block(&rs->rs_rbm) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 				 rs->rs_free - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		struct gfs2_rbm last_rbm = { .rgd = rs->rs_rbm.rgd, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		struct gfs2_bitmap *start, *last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		/* return reserved blocks to the rgrp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		BUG_ON(rs->rs_rbm.rgd->rd_reserved < rs->rs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		rs->rs_rbm.rgd->rd_reserved -= rs->rs_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		/* The rgrp extent failure point is likely not to increase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		   it will only do so if the freed blocks are somehow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		   contiguous with a span of free blocks that follows. Still,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		   it will force the number to be recalculated later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		rgd->rd_extfail_pt += rs->rs_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		rs->rs_free = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		if (gfs2_rbm_from_block(&last_rbm, last_block))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		start = rbm_bi(&rs->rs_rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		last = rbm_bi(&last_rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 			clear_bit(GBF_FULL, &start->bi_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		while (start++ != last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647)  * gfs2_rs_deltree - remove a multi-block reservation from the rgd tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648)  * @rs: The reservation to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) void gfs2_rs_deltree(struct gfs2_blkreserv *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	struct gfs2_rgrpd *rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	rgd = rs->rs_rbm.rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	if (rgd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		spin_lock(&rgd->rd_rsspin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		__rs_deltree(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		BUG_ON(rs->rs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		spin_unlock(&rgd->rd_rsspin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665)  * gfs2_rs_delete - delete a multi-block reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666)  * @ip: The inode for this reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667)  * @wcount: The inode's write count, or NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) void gfs2_rs_delete(struct gfs2_inode *ip, atomic_t *wcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	down_write(&ip->i_rw_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	if ((wcount == NULL) || (atomic_read(wcount) <= 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		gfs2_rs_deltree(&ip->i_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	up_write(&ip->i_rw_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679)  * return_all_reservations - return all reserved blocks back to the rgrp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680)  * @rgd: the rgrp that needs its space back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682)  * We previously reserved a bunch of blocks for allocation. Now we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683)  * give them back. This leave the reservation structures in tact, but removes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684)  * all of their corresponding "no-fly zones".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) static void return_all_reservations(struct gfs2_rgrpd *rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	struct rb_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	struct gfs2_blkreserv *rs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	spin_lock(&rgd->rd_rsspin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	while ((n = rb_first(&rgd->rd_rstree))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		__rs_deltree(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	spin_unlock(&rgd->rd_rsspin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	struct rb_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	struct gfs2_rgrpd *rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	struct gfs2_glock *gl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	while ((n = rb_first(&sdp->sd_rindex_tree))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		gl = rgd->rd_gl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		rb_erase(n, &sdp->sd_rindex_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		if (gl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 			if (gl->gl_state != LM_ST_UNLOCKED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 				gfs2_glock_cb(gl, LM_ST_UNLOCKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 				flush_delayed_work(&gl->gl_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 			gfs2_rgrp_brelse(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 			glock_clear_object(gl, rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 			gfs2_glock_put(gl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		gfs2_free_clones(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		return_all_reservations(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		kfree(rgd->rd_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		rgd->rd_bits = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		kmem_cache_free(gfs2_rgrpd_cachep, rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730)  * gfs2_compute_bitstructs - Compute the bitmap sizes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731)  * @rgd: The resource group descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733)  * Calculates bitmap descriptors, one for each block that contains bitmap data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735)  * Returns: errno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) static int compute_bitstructs(struct gfs2_rgrpd *rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	struct gfs2_sbd *sdp = rgd->rd_sbd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	struct gfs2_bitmap *bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	u32 bytes_left, bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	int x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	if (!length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	if (!rgd->rd_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	bytes_left = rgd->rd_bitbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	for (x = 0; x < length; x++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		bi = rgd->rd_bits + x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		bi->bi_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		/* small rgrp; bitmap stored completely in header block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		if (length == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 			bytes = bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 			bi->bi_offset = sizeof(struct gfs2_rgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 			bi->bi_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 			bi->bi_bytes = bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 			bi->bi_blocks = bytes * GFS2_NBBY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		/* header block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		} else if (x == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 			bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 			bi->bi_offset = sizeof(struct gfs2_rgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 			bi->bi_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 			bi->bi_bytes = bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 			bi->bi_blocks = bytes * GFS2_NBBY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		/* last block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		} else if (x + 1 == length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 			bytes = bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 			bi->bi_offset = sizeof(struct gfs2_meta_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 			bi->bi_start = rgd->rd_bitbytes - bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 			bi->bi_bytes = bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 			bi->bi_blocks = bytes * GFS2_NBBY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		/* other blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 			bytes = sdp->sd_sb.sb_bsize -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 				sizeof(struct gfs2_meta_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 			bi->bi_offset = sizeof(struct gfs2_meta_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 			bi->bi_start = rgd->rd_bitbytes - bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 			bi->bi_bytes = bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 			bi->bi_blocks = bytes * GFS2_NBBY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		bytes_left -= bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	if (bytes_left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		gfs2_consist_rgrpd(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	bi = rgd->rd_bits + (length - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	if ((bi->bi_start + bi->bi_bytes) * GFS2_NBBY != rgd->rd_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		gfs2_lm(sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 			"ri_addr = %llu\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 			"ri_length = %u\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 			"ri_data0 = %llu\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 			"ri_data = %u\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 			"ri_bitbytes = %u\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 			"start=%u len=%u offset=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 			(unsigned long long)rgd->rd_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 			rgd->rd_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 			(unsigned long long)rgd->rd_data0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 			rgd->rd_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 			rgd->rd_bitbytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 			bi->bi_start, bi->bi_bytes, bi->bi_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		gfs2_consist_rgrpd(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820)  * gfs2_ri_total - Total up the file system space, according to the rindex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821)  * @sdp: the filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) u64 gfs2_ri_total(struct gfs2_sbd *sdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	u64 total_data = 0;	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	struct inode *inode = sdp->sd_rindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	struct gfs2_inode *ip = GFS2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	char buf[sizeof(struct gfs2_rindex)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	int error, rgrps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	for (rgrps = 0;; rgrps++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		loff_t pos = rgrps * sizeof(struct gfs2_rindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		error = gfs2_internal_read(ip, buf, &pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 					   sizeof(struct gfs2_rindex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		if (error != sizeof(struct gfs2_rindex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	return total_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) static int rgd_insert(struct gfs2_rgrpd *rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	struct gfs2_sbd *sdp = rgd->rd_sbd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	struct rb_node **newn = &sdp->sd_rindex_tree.rb_node, *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	/* Figure out where to put new node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	while (*newn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		struct gfs2_rgrpd *cur = rb_entry(*newn, struct gfs2_rgrpd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 						  rd_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		parent = *newn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		if (rgd->rd_addr < cur->rd_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 			newn = &((*newn)->rb_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		else if (rgd->rd_addr > cur->rd_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 			newn = &((*newn)->rb_right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 			return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	rb_link_node(&rgd->rd_node, parent, newn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	rb_insert_color(&rgd->rd_node, &sdp->sd_rindex_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	sdp->sd_rgrps++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872)  * read_rindex_entry - Pull in a new resource index entry from the disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873)  * @ip: Pointer to the rindex inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875)  * Returns: 0 on success, > 0 on EOF, error code otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) static int read_rindex_entry(struct gfs2_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	struct gfs2_rindex buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	struct gfs2_rgrpd *rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	if (pos >= i_size_read(&ip->i_inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	error = gfs2_internal_read(ip, (char *)&buf, &pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 				   sizeof(struct gfs2_rindex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	if (error != sizeof(struct gfs2_rindex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		return (error == 0) ? 1 : error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	if (!rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	rgd->rd_sbd = sdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	rgd->rd_addr = be64_to_cpu(buf.ri_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	rgd->rd_length = be32_to_cpu(buf.ri_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	rgd->rd_data0 = be64_to_cpu(buf.ri_data0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	rgd->rd_data = be32_to_cpu(buf.ri_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	rgd->rd_bitbytes = be32_to_cpu(buf.ri_bitbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	spin_lock_init(&rgd->rd_rsspin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	error = compute_bitstructs(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	error = gfs2_glock_get(sdp, rgd->rd_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 			       &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	rgd->rd_rgl = (struct gfs2_rgrp_lvb *)rgd->rd_gl->gl_lksb.sb_lvbptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	rgd->rd_flags &= ~(GFS2_RDF_UPTODATE | GFS2_RDF_PREFERRED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	if (rgd->rd_data > sdp->sd_max_rg_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		sdp->sd_max_rg_data = rgd->rd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	spin_lock(&sdp->sd_rindex_spin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	error = rgd_insert(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	spin_unlock(&sdp->sd_rindex_spin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		glock_set_object(rgd->rd_gl, rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	error = 0; /* someone else read in the rgrp; free it and ignore it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	gfs2_glock_put(rgd->rd_gl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	kfree(rgd->rd_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	rgd->rd_bits = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	kmem_cache_free(gfs2_rgrpd_cachep, rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940)  * set_rgrp_preferences - Run all the rgrps, selecting some we prefer to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941)  * @sdp: the GFS2 superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943)  * The purpose of this function is to select a subset of the resource groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944)  * and mark them as PREFERRED. We do it in such a way that each node prefers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945)  * to use a unique set of rgrps to minimize glock contention.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) static void set_rgrp_preferences(struct gfs2_sbd *sdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	struct gfs2_rgrpd *rgd, *first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	/* Skip an initial number of rgrps, based on this node's journal ID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	   That should start each node out on its own set. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	rgd = gfs2_rgrpd_get_first(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	for (i = 0; i < sdp->sd_lockstruct.ls_jid; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		rgd = gfs2_rgrpd_get_next(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	first = rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		rgd->rd_flags |= GFS2_RDF_PREFERRED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		for (i = 0; i < sdp->sd_journals; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 			rgd = gfs2_rgrpd_get_next(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			if (!rgd || rgd == first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	} while (rgd && rgd != first);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970)  * gfs2_ri_update - Pull in a new resource index from the disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971)  * @ip: pointer to the rindex inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973)  * Returns: 0 on successful update, error code otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) static int gfs2_ri_update(struct gfs2_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		error = read_rindex_entry(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	} while (error == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	if (RB_EMPTY_ROOT(&sdp->sd_rindex_tree)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		fs_err(sdp, "no resource groups found in the file system.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	set_rgrp_preferences(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	sdp->sd_rindex_uptodate = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999)  * gfs2_rindex_update - Update the rindex if required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)  * @sdp: The GFS2 superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)  * We grab a lock on the rindex inode to make sure that it doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)  * change whilst we are performing an operation. We keep this lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)  * for quite long periods of time compared to other locks. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)  * doesn't matter, since it is shared and it is very, very rarely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)  * accessed in the exclusive mode (i.e. only when expanding the filesystem).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)  * This makes sure that we're using the latest copy of the resource index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)  * special file, which might have been updated if someone expanded the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)  * filesystem (via gfs2_grow utility), which adds new resource groups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)  * Returns: 0 on succeess, error code otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) int gfs2_rindex_update(struct gfs2_sbd *sdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	struct gfs2_glock *gl = ip->i_gl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	struct gfs2_holder ri_gh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	int unlock_required = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	/* Read new copy from disk if we don't have the latest */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	if (!sdp->sd_rindex_uptodate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		if (!gfs2_glock_is_locked_by_me(gl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 			error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &ri_gh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 				return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 			unlock_required = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		if (!sdp->sd_rindex_uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 			error = gfs2_ri_update(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		if (unlock_required)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 			gfs2_glock_dq_uninit(&ri_gh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	const struct gfs2_rgrp *str = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	u32 rg_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	rg_flags = be32_to_cpu(str->rg_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	rg_flags &= ~GFS2_RDF_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	rgd->rd_flags &= GFS2_RDF_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	rgd->rd_flags |= rg_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	rgd->rd_free = be32_to_cpu(str->rg_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	/* rd_data0, rd_data and rd_bitbytes already set from rindex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) static void gfs2_rgrp_ondisk2lvb(struct gfs2_rgrp_lvb *rgl, const void *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	const struct gfs2_rgrp *str = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	rgl->rl_magic = cpu_to_be32(GFS2_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	rgl->rl_flags = str->rg_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	rgl->rl_free = str->rg_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	rgl->rl_dinodes = str->rg_dinodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	rgl->rl_igeneration = str->rg_igeneration;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	rgl->__pad = 0UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	struct gfs2_rgrpd *next = gfs2_rgrpd_get_next(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	struct gfs2_rgrp *str = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	u32 crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	str->rg_free = cpu_to_be32(rgd->rd_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	if (next == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		str->rg_skip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	else if (next->rd_addr > rgd->rd_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		str->rg_skip = cpu_to_be32(next->rd_addr - rgd->rd_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	str->rg_data0 = cpu_to_be64(rgd->rd_data0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	str->rg_data = cpu_to_be32(rgd->rd_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	str->rg_bitbytes = cpu_to_be32(rgd->rd_bitbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	str->rg_crc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	crc = gfs2_disk_hash(buf, sizeof(struct gfs2_rgrp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	str->rg_crc = cpu_to_be32(crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) static int gfs2_rgrp_lvb_valid(struct gfs2_rgrpd *rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	struct gfs2_rgrp *str = (struct gfs2_rgrp *)rgd->rd_bits[0].bi_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	struct gfs2_sbd *sdp = rgd->rd_sbd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	int valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	if (rgl->rl_flags != str->rg_flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		fs_warn(sdp, "GFS2: rgd: %llu lvb flag mismatch %u/%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 			(unsigned long long)rgd->rd_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		       be32_to_cpu(rgl->rl_flags), be32_to_cpu(str->rg_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	if (rgl->rl_free != str->rg_free) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		fs_warn(sdp, "GFS2: rgd: %llu lvb free mismatch %u/%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 			(unsigned long long)rgd->rd_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 			be32_to_cpu(rgl->rl_free), be32_to_cpu(str->rg_free));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	if (rgl->rl_dinodes != str->rg_dinodes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		fs_warn(sdp, "GFS2: rgd: %llu lvb dinode mismatch %u/%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 			(unsigned long long)rgd->rd_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 			be32_to_cpu(rgl->rl_dinodes),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 			be32_to_cpu(str->rg_dinodes));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	if (rgl->rl_igeneration != str->rg_igeneration) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		fs_warn(sdp, "GFS2: rgd: %llu lvb igen mismatch %llu/%llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 			(unsigned long long)rgd->rd_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 			(unsigned long long)be64_to_cpu(rgl->rl_igeneration),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 			(unsigned long long)be64_to_cpu(str->rg_igeneration));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	return valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) static u32 count_unlinked(struct gfs2_rgrpd *rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	struct gfs2_bitmap *bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	const u32 length = rgd->rd_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	const u8 *buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	u32 i, goal, count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	for (i = 0, bi = rgd->rd_bits; i < length; i++, bi++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		goal = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		buffer = bi->bi_bh->b_data + bi->bi_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		WARN_ON(!buffer_uptodate(bi->bi_bh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		while (goal < bi->bi_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 			goal = gfs2_bitfit(buffer, bi->bi_bytes, goal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 					   GFS2_BLKST_UNLINKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 			if (goal == BFITNOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 			count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 			goal++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)  * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)  * @rgd: the struct gfs2_rgrpd describing the RG to read in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)  * Read in all of a Resource Group's header and bitmap blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158)  * Caller must eventually call gfs2_rgrp_brelse() to free the bitmaps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160)  * Returns: errno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) static int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	struct gfs2_sbd *sdp = rgd->rd_sbd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	struct gfs2_glock *gl = rgd->rd_gl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	unsigned int length = rgd->rd_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	struct gfs2_bitmap *bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	unsigned int x, y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	if (rgd->rd_bits[0].bi_bh != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	for (x = 0; x < length; x++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		bi = rgd->rd_bits + x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, 0, &bi->bi_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	for (y = length; y--;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		bi = rgd->rd_bits + y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		error = gfs2_meta_wait(sdp, bi->bi_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 					      GFS2_METATYPE_RG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 			error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		for (x = 0; x < length; x++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 			clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		rgd->rd_free_clone = rgd->rd_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		/* max out the rgrp allocation failure point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		rgd->rd_extfail_pt = rgd->rd_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	if (cpu_to_be32(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		rgd->rd_rgl->rl_unlinked = cpu_to_be32(count_unlinked(rgd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 		gfs2_rgrp_ondisk2lvb(rgd->rd_rgl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 				     rgd->rd_bits[0].bi_bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	else if (sdp->sd_args.ar_rgrplvb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		if (!gfs2_rgrp_lvb_valid(rgd)){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 			gfs2_consist_rgrpd(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 			error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		if (rgd->rd_rgl->rl_unlinked == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 			rgd->rd_flags &= ~GFS2_RDF_CHECK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	while (x--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		bi = rgd->rd_bits + x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		brelse(bi->bi_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		bi->bi_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		gfs2_assert_warn(sdp, !bi->bi_clone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) static int update_rgrp_lvb(struct gfs2_rgrpd *rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	u32 rl_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	if (rgd->rd_flags & GFS2_RDF_UPTODATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	if (cpu_to_be32(GFS2_MAGIC) != rgd->rd_rgl->rl_magic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		return gfs2_rgrp_bh_get(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	rl_flags = be32_to_cpu(rgd->rd_rgl->rl_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	rl_flags &= ~GFS2_RDF_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	rgd->rd_flags &= GFS2_RDF_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	rgd->rd_flags |= (rl_flags | GFS2_RDF_CHECK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	if (rgd->rd_rgl->rl_unlinked == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		rgd->rd_flags &= ~GFS2_RDF_CHECK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	rgd->rd_free = be32_to_cpu(rgd->rd_rgl->rl_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	rgd->rd_free_clone = rgd->rd_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	rgd->rd_dinodes = be32_to_cpu(rgd->rd_rgl->rl_dinodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	rgd->rd_igeneration = be64_to_cpu(rgd->rd_rgl->rl_igeneration);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) int gfs2_rgrp_go_lock(struct gfs2_holder *gh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	struct gfs2_sbd *sdp = rgd->rd_sbd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	if (gh->gh_flags & GL_SKIP && sdp->sd_args.ar_rgrplvb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	return gfs2_rgrp_bh_get(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264)  * gfs2_rgrp_brelse - Release RG bitmaps read in with gfs2_rgrp_bh_get()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265)  * @rgd: The resource group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) void gfs2_rgrp_brelse(struct gfs2_rgrpd *rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	int x, length = rgd->rd_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	for (x = 0; x < length; x++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		struct gfs2_bitmap *bi = rgd->rd_bits + x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		if (bi->bi_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 			brelse(bi->bi_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 			bi->bi_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) int gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 			     struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 			     const struct gfs2_bitmap *bi, unsigned minlen, u64 *ptrimmed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	struct super_block *sb = sdp->sd_vfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	u64 blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	sector_t start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	sector_t nr_blks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	unsigned int x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	u32 trimmed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	u8 diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	for (x = 0; x < bi->bi_bytes; x++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		const u8 *clone = bi->bi_clone ? bi->bi_clone : bi->bi_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		clone += bi->bi_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		clone += x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		if (bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 			const u8 *orig = bh->b_data + bi->bi_offset + x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 			diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 			diff = ~(*clone | (*clone >> 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		diff &= 0x55;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		if (diff == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		blk = offset + ((bi->bi_start + x) * GFS2_NBBY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		while(diff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 			if (diff & 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 				if (nr_blks == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 					goto start_new_extent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 				if ((start + nr_blks) != blk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 					if (nr_blks >= minlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 						rv = sb_issue_discard(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 							start, nr_blks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 							GFP_NOFS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 						if (rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 							goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 						trimmed += nr_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 					nr_blks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) start_new_extent:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 					start = blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 				nr_blks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 			diff >>= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 			blk++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	if (nr_blks >= minlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 		rv = sb_issue_discard(sb, start, nr_blks, GFP_NOFS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 		if (rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		trimmed += nr_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	if (ptrimmed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		*ptrimmed = trimmed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	if (sdp->sd_args.ar_discard)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	sdp->sd_args.ar_discard = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350)  * gfs2_fitrim - Generate discard requests for unused bits of the filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351)  * @filp: Any file on the filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352)  * @argp: Pointer to the arguments (also used to pass result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354)  * Returns: 0 on success, otherwise error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) int gfs2_fitrim(struct file *filp, void __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	struct inode *inode = file_inode(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	struct gfs2_sbd *sdp = GFS2_SB(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	struct request_queue *q = bdev_get_queue(sdp->sd_vfs->s_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	struct gfs2_rgrpd *rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	struct gfs2_rgrpd *rgd_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	struct gfs2_holder gh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	struct fstrim_range r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	u64 amt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	u64 trimmed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	u64 start, end, minlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	unsigned int x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	unsigned bs_shift = sdp->sd_sb.sb_bsize_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	if (!blk_queue_discard(q))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	if (copy_from_user(&r, argp, sizeof(r)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	ret = gfs2_rindex_update(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	start = r.start >> bs_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	end = start + (r.len >> bs_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	minlen = max_t(u64, r.minlen, sdp->sd_sb.sb_bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	minlen = max_t(u64, minlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		       q->limits.discard_granularity) >> bs_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	if (end <= start || minlen > sdp->sd_max_rg_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	rgd = gfs2_blk2rgrpd(sdp, start, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	rgd_end = gfs2_blk2rgrpd(sdp, end, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	if ((gfs2_rgrpd_get_first(sdp) == gfs2_rgrpd_get_next(rgd_end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	    && (start > rgd_end->rd_data0 + rgd_end->rd_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 		return -EINVAL; /* start is beyond the end of the fs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 		ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 		if (!(rgd->rd_flags & GFS2_RGF_TRIMMED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 			/* Trim each bitmap in the rgrp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 			for (x = 0; x < rgd->rd_length; x++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 				struct gfs2_bitmap *bi = rgd->rd_bits + x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 				ret = gfs2_rgrp_send_discards(sdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 						rgd->rd_data0, NULL, bi, minlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 						&amt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 				if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 					gfs2_glock_dq_uninit(&gh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 				trimmed += amt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 			/* Mark rgrp as having been trimmed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 			ret = gfs2_trans_begin(sdp, RES_RG_HDR, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 			if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 				bh = rgd->rd_bits[0].bi_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 				rgd->rd_flags |= GFS2_RGF_TRIMMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 				gfs2_trans_add_meta(rgd->rd_gl, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 				gfs2_rgrp_out(rgd, bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 				gfs2_trans_end(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 		gfs2_glock_dq_uninit(&gh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		if (rgd == rgd_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		rgd = gfs2_rgrpd_get_next(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	r.len = trimmed << bs_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	if (copy_to_user(argp, &r, sizeof(r)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453)  * rs_insert - insert a new multi-block reservation into the rgrp's rb_tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454)  * @ip: the inode structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) static void rs_insert(struct gfs2_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	struct rb_node **newn, *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	struct gfs2_blkreserv *rs = &ip->i_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	struct gfs2_rgrpd *rgd = rs->rs_rbm.rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	u64 fsblock = gfs2_rbm_to_block(&rs->rs_rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	BUG_ON(gfs2_rs_active(rs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	spin_lock(&rgd->rd_rsspin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	newn = &rgd->rd_rstree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	while (*newn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		struct gfs2_blkreserv *cur =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 			rb_entry(*newn, struct gfs2_blkreserv, rs_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		parent = *newn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		rc = rs_cmp(fsblock, rs->rs_free, cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		if (rc > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 			newn = &((*newn)->rb_right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		else if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 			newn = &((*newn)->rb_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 			spin_unlock(&rgd->rd_rsspin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 			WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	rb_link_node(&rs->rs_node, parent, newn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	rb_insert_color(&rs->rs_node, &rgd->rd_rstree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	/* Do our rgrp accounting for the reservation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	rgd->rd_reserved += rs->rs_free; /* blocks reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	spin_unlock(&rgd->rd_rsspin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	trace_gfs2_rs(rs, TRACE_RS_INSERT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496)  * rgd_free - return the number of free blocks we can allocate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497)  * @rgd: the resource group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499)  * This function returns the number of free blocks for an rgrp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500)  * That's the clone-free blocks (blocks that are free, not including those
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501)  * still being used for unlinked files that haven't been deleted.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503)  * It also subtracts any blocks reserved by someone else, but does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504)  * include free blocks that are still part of our current reservation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)  * because obviously we can (and will) allocate them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) static inline u32 rgd_free(struct gfs2_rgrpd *rgd, struct gfs2_blkreserv *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	u32 tot_reserved, tot_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	if (WARN_ON_ONCE(rgd->rd_reserved < rs->rs_free))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	tot_reserved = rgd->rd_reserved - rs->rs_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	if (rgd->rd_free_clone < tot_reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		tot_reserved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	tot_free = rgd->rd_free_clone - tot_reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	return tot_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524)  * rg_mblk_search - find a group of multiple free blocks to form a reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525)  * @rgd: the resource group descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526)  * @ip: pointer to the inode for which we're reserving blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527)  * @ap: the allocation parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) static void rg_mblk_search(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 			   const struct gfs2_alloc_parms *ap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	struct gfs2_rbm rbm = { .rgd = rgd, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	u64 goal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	struct gfs2_blkreserv *rs = &ip->i_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	u32 extlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	u32 free_blocks = rgd_free(rgd, rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	struct inode *inode = &ip->i_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	if (S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 		extlen = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 		extlen = max_t(u32, atomic_read(&ip->i_sizehint), ap->target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		extlen = clamp(extlen, (u32)RGRP_RSRV_MINBLKS, free_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	if ((rgd->rd_free_clone < rgd->rd_reserved) || (free_blocks < extlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	/* Find bitmap block that contains bits for goal block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	if (rgrp_contains_block(rgd, ip->i_goal))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		goal = ip->i_goal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		goal = rgd->rd_last_alloc + rgd->rd_data0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	if (WARN_ON(gfs2_rbm_from_block(&rbm, goal)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	ret = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, &extlen, ip, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		rs->rs_rbm = rbm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 		rs->rs_free = extlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		rs_insert(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		if (goal == rgd->rd_last_alloc + rgd->rd_data0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 			rgd->rd_last_alloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572)  * gfs2_next_unreserved_block - Return next block that is not reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573)  * @rgd: The resource group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574)  * @block: The starting block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575)  * @length: The required length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576)  * @ip: Ignore any reservations for this inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578)  * If the block does not appear in any reservation, then return the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579)  * block number unchanged. If it does appear in the reservation, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580)  * keep looking through the tree of reservations in order to find the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581)  * first block number which is not reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) static u64 gfs2_next_unreserved_block(struct gfs2_rgrpd *rgd, u64 block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 				      u32 length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 				      const struct gfs2_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	struct gfs2_blkreserv *rs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	struct rb_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	spin_lock(&rgd->rd_rsspin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	n = rgd->rd_rstree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	while (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 		rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 		rc = rs_cmp(block, length, rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 			n = n->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		else if (rc > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 			n = n->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	if (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 		while ((rs_cmp(block, length, rs) == 0) && (&ip->i_res != rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 			block = gfs2_rbm_to_block(&rs->rs_rbm) + rs->rs_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 			n = n->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 			if (n == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 			rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	spin_unlock(&rgd->rd_rsspin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	return block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620)  * gfs2_reservation_check_and_update - Check for reservations during block alloc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621)  * @rbm: The current position in the resource group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622)  * @ip: The inode for which we are searching for blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623)  * @minext: The minimum extent length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624)  * @maxext: A pointer to the maximum extent structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626)  * This checks the current position in the rgrp to see whether there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627)  * a reservation covering this block. If not then this function is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628)  * no-op. If there is, then the position is moved to the end of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629)  * contiguous reservation(s) so that we are pointing at the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630)  * non-reserved block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632)  * Returns: 0 if no reservation, 1 if @rbm has changed, otherwise an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) static int gfs2_reservation_check_and_update(struct gfs2_rbm *rbm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 					     const struct gfs2_inode *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 					     u32 minext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 					     struct gfs2_extent *maxext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	u64 block = gfs2_rbm_to_block(rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	u32 extlen = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	u64 nblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	 * If we have a minimum extent length, then skip over any extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	 * which is less than the min extent length in size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	if (minext) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		extlen = gfs2_free_extlen(rbm, minext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 		if (extlen <= maxext->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	 * Check the extent which has been found against the reservations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	 * and skip if parts of it are already reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	nblock = gfs2_next_unreserved_block(rbm->rgd, block, extlen, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	if (nblock == block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 		if (!minext || extlen >= minext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 		if (extlen > maxext->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 			maxext->len = extlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 			maxext->rbm = *rbm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 		nblock = block + extlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	ret = gfs2_rbm_from_block(rbm, nblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678)  * gfs2_rbm_find - Look for blocks of a particular state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679)  * @rbm: Value/result starting position and final position
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680)  * @state: The state which we want to find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681)  * @minext: Pointer to the requested extent length (NULL for a single block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682)  *          This is updated to be the actual reservation size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683)  * @ip: If set, check for reservations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684)  * @nowrap: Stop looking at the end of the rgrp, rather than wrapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685)  *          around until we've reached the starting point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687)  * Side effects:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688)  * - If looking for free blocks, we set GBF_FULL on each bitmap which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689)  *   has no free blocks in it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690)  * - If looking for free blocks, we set rd_extfail_pt on each rgrp which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691)  *   has come up short on a free block search.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693)  * Returns: 0 on success, -ENOSPC if there is no block of the requested state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) static int gfs2_rbm_find(struct gfs2_rbm *rbm, u8 state, u32 *minext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 			 const struct gfs2_inode *ip, bool nowrap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	bool scan_from_start = rbm->bii == 0 && rbm->offset == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	int last_bii;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	u32 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	bool wrapped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	struct gfs2_bitmap *bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	struct gfs2_extent maxext = { .rbm.rgd = rbm->rgd, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	 * Determine the last bitmap to search.  If we're not starting at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	 * beginning of a bitmap, we need to search that bitmap twice to scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	 * the entire resource group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	last_bii = rbm->bii - (rbm->offset == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	while(1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 		bi = rbm_bi(rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 		if ((ip == NULL || !gfs2_rs_active(&ip->i_res)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 		    test_bit(GBF_FULL, &bi->bi_flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 		    (state == GFS2_BLKST_FREE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 			goto next_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		bh = bi->bi_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 		buffer = bh->b_data + bi->bi_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 		WARN_ON(!buffer_uptodate(bh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 		if (state != GFS2_BLKST_UNLINKED && bi->bi_clone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 			buffer = bi->bi_clone + bi->bi_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 		offset = gfs2_bitfit(buffer, bi->bi_bytes, rbm->offset, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 		if (offset == BFITNOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 			if (state == GFS2_BLKST_FREE && rbm->offset == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 				set_bit(GBF_FULL, &bi->bi_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 			goto next_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 		rbm->offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		if (ip == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		ret = gfs2_reservation_check_and_update(rbm, ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 							minext ? *minext : 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 							&maxext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 			goto next_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		if (ret == -E2BIG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 			rbm->bii = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 			rbm->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 			goto res_covered_end_of_rgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) next_bitmap:	/* Find next bitmap in the rgrp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 		rbm->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 		rbm->bii++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 		if (rbm->bii == rbm->rgd->rd_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 			rbm->bii = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) res_covered_end_of_rgrp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 		if (rbm->bii == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 			if (wrapped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 			wrapped = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 			if (nowrap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) next_iter:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		/* Have we scanned the entire resource group? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 		if (wrapped && rbm->bii > last_bii)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	if (minext == NULL || state != GFS2_BLKST_FREE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	/* If the extent was too small, and it's smaller than the smallest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	   to have failed before, remember for future reference that it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	   useless to search this rgrp again for this amount or more. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	if (wrapped && (scan_from_start || rbm->bii > last_bii) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	    *minext < rbm->rgd->rd_extfail_pt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 		rbm->rgd->rd_extfail_pt = *minext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	/* If the maximum extent we found is big enough to fulfill the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	   minimum requirements, use it anyway. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	if (maxext.len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 		*rbm = maxext.rbm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 		*minext = maxext.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793)  * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794)  * @rgd: The rgrp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795)  * @last_unlinked: block address of the last dinode we unlinked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796)  * @skip: block address we should explicitly not unlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798)  * Returns: 0 if no error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799)  *          The inode, if one has been found, in inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	u64 block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	struct gfs2_sbd *sdp = rgd->rd_sbd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	struct gfs2_glock *gl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	struct gfs2_inode *ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	int found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	struct gfs2_rbm rbm = { .rgd = rgd, .bii = 0, .offset = 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		error = gfs2_rbm_find(&rbm, GFS2_BLKST_UNLINKED, NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 				      true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 		if (error == -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 		if (WARN_ON_ONCE(error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		block = gfs2_rbm_to_block(&rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 		if (gfs2_rbm_from_block(&rbm, block + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 		if (*last_unlinked != NO_BLOCK && block <= *last_unlinked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 		if (block == skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 		*last_unlinked = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 		error = gfs2_glock_get(sdp, block, &gfs2_iopen_glops, CREATE, &gl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 		/* If the inode is already in cache, we can ignore it here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		 * because the existing inode disposal code will deal with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 		 * it when all refs have gone away. Accessing gl_object like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 		 * this is not safe in general. Here it is ok because we do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 		 * not dereference the pointer, and we only need an approx
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		 * answer to whether it is NULL or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		ip = gl->gl_object;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 		if (ip || !gfs2_queue_delete_work(gl, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 			gfs2_glock_put(gl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 			found++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 		/* Limit reclaim to sensible number of tasks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 		if (found > NR_CPUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	rgd->rd_flags &= ~GFS2_RDF_CHECK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857)  * gfs2_rgrp_congested - Use stats to figure out whether an rgrp is congested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858)  * @rgd: The rgrp in question
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859)  * @loops: An indication of how picky we can be (0=very, 1=less so)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861)  * This function uses the recently added glock statistics in order to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862)  * figure out whether a parciular resource group is suffering from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863)  * contention from multiple nodes. This is done purely on the basis
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864)  * of timings, since this is the only data we have to work with and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865)  * our aim here is to reject a resource group which is highly contended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866)  * but (very important) not to do this too often in order to ensure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867)  * we do not land up introducing fragmentation by changing resource
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868)  * groups when not actually required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870)  * The calculation is fairly simple, we want to know whether the SRTTB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871)  * (i.e. smoothed round trip time for blocking operations) to acquire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872)  * the lock for this rgrp's glock is significantly greater than the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873)  * time taken for resource groups on average. We introduce a margin in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874)  * the form of the variable @var which is computed as the sum of the two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875)  * respective variences, and multiplied by a factor depending on @loops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876)  * and whether we have a lot of data to base the decision on. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877)  * then tested against the square difference of the means in order to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878)  * decide whether the result is statistically significant or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880)  * Returns: A boolean verdict on the congestion status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) static bool gfs2_rgrp_congested(const struct gfs2_rgrpd *rgd, int loops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	const struct gfs2_glock *gl = rgd->rd_gl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	const struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	struct gfs2_lkstats *st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 	u64 r_dcount, l_dcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	u64 l_srttb, a_srttb = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	s64 srttb_diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	u64 sqr_diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	u64 var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	int cpu, nonzero = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	for_each_present_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 		st = &per_cpu_ptr(sdp->sd_lkstats, cpu)->lkstats[LM_TYPE_RGRP];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 		if (st->stats[GFS2_LKS_SRTTB]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 			a_srttb += st->stats[GFS2_LKS_SRTTB];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 			nonzero++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	st = &this_cpu_ptr(sdp->sd_lkstats)->lkstats[LM_TYPE_RGRP];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	if (nonzero)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 		do_div(a_srttb, nonzero);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	r_dcount = st->stats[GFS2_LKS_DCOUNT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 	var = st->stats[GFS2_LKS_SRTTVARB] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	      gl->gl_stats.stats[GFS2_LKS_SRTTVARB];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	l_srttb = gl->gl_stats.stats[GFS2_LKS_SRTTB];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	l_dcount = gl->gl_stats.stats[GFS2_LKS_DCOUNT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	if ((l_dcount < 1) || (r_dcount < 1) || (a_srttb == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	srttb_diff = a_srttb - l_srttb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	sqr_diff = srttb_diff * srttb_diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	var *= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	if (l_dcount < 8 || r_dcount < 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 		var *= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	if (loops == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 		var *= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 	return ((srttb_diff < 0) && (sqr_diff > var));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930)  * gfs2_rgrp_used_recently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931)  * @rs: The block reservation with the rgrp to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932)  * @msecs: The time limit in milliseconds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934)  * Returns: True if the rgrp glock has been used within the time limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) static bool gfs2_rgrp_used_recently(const struct gfs2_blkreserv *rs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 				    u64 msecs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	u64 tdiff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	tdiff = ktime_to_ns(ktime_sub(ktime_get_real(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942)                             rs->rs_rbm.rgd->rd_gl->gl_dstamp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	return tdiff > (msecs * 1000 * 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) static u32 gfs2_orlov_skip(const struct gfs2_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 	const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	u32 skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	get_random_bytes(&skip, sizeof(skip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	return skip % sdp->sd_rgrps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) static bool gfs2_select_rgrp(struct gfs2_rgrpd **pos, const struct gfs2_rgrpd *begin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	struct gfs2_rgrpd *rgd = *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	struct gfs2_sbd *sdp = rgd->rd_sbd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	rgd = gfs2_rgrpd_get_next(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	if (rgd == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 		rgd = gfs2_rgrpd_get_first(sdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	*pos = rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	if (rgd != begin) /* If we didn't wrap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971)  * fast_to_acquire - determine if a resource group will be fast to acquire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973)  * If this is one of our preferred rgrps, it should be quicker to acquire,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974)  * because we tried to set ourselves up as dlm lock master.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) static inline int fast_to_acquire(struct gfs2_rgrpd *rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	struct gfs2_glock *gl = rgd->rd_gl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	if (gl->gl_state != LM_ST_UNLOCKED && list_empty(&gl->gl_holders) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	    !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 	    !test_bit(GLF_DEMOTE, &gl->gl_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	if (rgd->rd_flags & GFS2_RDF_PREFERRED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990)  * gfs2_inplace_reserve - Reserve space in the filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991)  * @ip: the inode to reserve space for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992)  * @ap: the allocation parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994)  * We try our best to find an rgrp that has at least ap->target blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995)  * available. After a couple of passes (loops == 2), the prospects of finding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996)  * such an rgrp diminish. At this stage, we return the first rgrp that has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997)  * at least ap->min_target blocks available. Either way, we set ap->allowed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998)  * the number of blocks available in the chosen rgrp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000)  * Returns: 0 on success,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001)  *          -ENOMEM if a suitable rgrp can't be found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002)  *          errno otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) int gfs2_inplace_reserve(struct gfs2_inode *ip, struct gfs2_alloc_parms *ap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	struct gfs2_rgrpd *begin = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 	struct gfs2_blkreserv *rs = &ip->i_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 	int error = 0, rg_locked, flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 	u64 last_unlinked = NO_BLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	int loops = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	u32 free_blocks, skip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 	if (sdp->sd_args.ar_rgrplvb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 		flags |= GL_SKIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 	if (gfs2_assert_warn(sdp, ap->target))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	if (gfs2_rs_active(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 		begin = rs->rs_rbm.rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	} else if (rs->rs_rbm.rgd &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 		   rgrp_contains_block(rs->rs_rbm.rgd, ip->i_goal)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 		begin = rs->rs_rbm.rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 		check_and_update_goal(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 		rs->rs_rbm.rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	if (S_ISDIR(ip->i_inode.i_mode) && (ap->aflags & GFS2_AF_ORLOV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 		skip = gfs2_orlov_skip(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 	if (rs->rs_rbm.rgd == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 		return -EBADSLT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	while (loops < 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 		rg_locked = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 		if (!gfs2_glock_is_locked_by_me(rs->rs_rbm.rgd->rd_gl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 			rg_locked = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 			if (skip && skip--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 				goto next_rgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 			if (!gfs2_rs_active(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 				if (loops == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 				    !fast_to_acquire(rs->rs_rbm.rgd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 					goto next_rgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 				if ((loops < 2) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 				    gfs2_rgrp_used_recently(rs, 1000) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 				    gfs2_rgrp_congested(rs->rs_rbm.rgd, loops))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 					goto next_rgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 			error = gfs2_glock_nq_init(rs->rs_rbm.rgd->rd_gl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 						   LM_ST_EXCLUSIVE, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 						   &ip->i_rgd_gh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 			if (unlikely(error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 				return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 			if (!gfs2_rs_active(rs) && (loops < 2) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 			    gfs2_rgrp_congested(rs->rs_rbm.rgd, loops))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 				goto skip_rgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 			if (sdp->sd_args.ar_rgrplvb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 				error = update_rgrp_lvb(rs->rs_rbm.rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 				if (unlikely(error)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 					gfs2_glock_dq_uninit(&ip->i_rgd_gh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 					return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 		/* Skip unusable resource groups */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 		if ((rs->rs_rbm.rgd->rd_flags & (GFS2_RGF_NOALLOC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 						 GFS2_RDF_ERROR)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 		    (loops == 0 && ap->target > rs->rs_rbm.rgd->rd_extfail_pt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 			goto skip_rgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 		if (sdp->sd_args.ar_rgrplvb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 			gfs2_rgrp_bh_get(rs->rs_rbm.rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 		/* Get a reservation if we don't already have one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 		if (!gfs2_rs_active(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 			rg_mblk_search(rs->rs_rbm.rgd, ip, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 		/* Skip rgrps when we can't get a reservation on first pass */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 		if (!gfs2_rs_active(rs) && (loops < 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 			goto check_rgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 		/* If rgrp has enough free space, use it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 		free_blocks = rgd_free(rs->rs_rbm.rgd, rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 		if (free_blocks >= ap->target ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 		    (loops == 2 && ap->min_target &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 		     free_blocks >= ap->min_target)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 			ap->allowed = free_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) check_rgrp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 		/* Check for unlinked inodes which can be reclaimed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 		if (rs->rs_rbm.rgd->rd_flags & GFS2_RDF_CHECK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 			try_rgrp_unlink(rs->rs_rbm.rgd, &last_unlinked,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 					ip->i_no_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) skip_rgrp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 		/* Drop reservation, if we couldn't use reserved rgrp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 		if (gfs2_rs_active(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 			gfs2_rs_deltree(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 		/* Unlock rgrp if required */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 		if (!rg_locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 			gfs2_glock_dq_uninit(&ip->i_rgd_gh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) next_rgrp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 		/* Find the next rgrp, and continue looking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 		if (gfs2_select_rgrp(&rs->rs_rbm.rgd, begin))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 		if (skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 		/* If we've scanned all the rgrps, but found no free blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 		 * then this checks for some less likely conditions before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 		 * trying again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 		loops++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 		/* Check that fs hasn't grown if writing to rindex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 		if (ip == GFS2_I(sdp->sd_rindex) && !sdp->sd_rindex_uptodate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 			error = gfs2_ri_update(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 				return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 		/* Flushing the log may release space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 		if (loops == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 			gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 				       GFS2_LFC_INPLACE_RESERVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132)  * gfs2_inplace_release - release an inplace reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133)  * @ip: the inode the reservation was taken out on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135)  * Release a reservation made by gfs2_inplace_reserve().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) void gfs2_inplace_release(struct gfs2_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	if (gfs2_holder_initialized(&ip->i_rgd_gh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 		gfs2_glock_dq_uninit(&ip->i_rgd_gh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145)  * gfs2_alloc_extent - allocate an extent from a given bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146)  * @rbm: the resource group information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147)  * @dinode: TRUE if the first block we allocate is for a dinode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148)  * @n: The extent length (value/result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150)  * Add the bitmap buffer to the transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151)  * Set the found bits to @new_state to change block's allocation state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) static void gfs2_alloc_extent(const struct gfs2_rbm *rbm, bool dinode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 			     unsigned int *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	struct gfs2_rbm pos = { .rgd = rbm->rgd, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	const unsigned int elen = *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 	u64 block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	*n = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	block = gfs2_rbm_to_block(rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 	gfs2_trans_add_meta(rbm->rgd->rd_gl, rbm_bi(rbm)->bi_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 	gfs2_setbit(rbm, true, dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 	block++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 	while (*n < elen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 		ret = gfs2_rbm_from_block(&pos, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 		if (ret || gfs2_testbit(&pos, true) != GFS2_BLKST_FREE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 		gfs2_trans_add_meta(pos.rgd->rd_gl, rbm_bi(&pos)->bi_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 		gfs2_setbit(&pos, true, GFS2_BLKST_USED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 		(*n)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 		block++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178)  * rgblk_free - Change alloc state of given block(s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179)  * @sdp: the filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180)  * @rgd: the resource group the blocks are in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181)  * @bstart: the start of a run of blocks to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182)  * @blen: the length of the block run (all must lie within ONE RG!)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183)  * @new_state: GFS2_BLKST_XXX the after-allocation block state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) static void rgblk_free(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 		       u64 bstart, u32 blen, unsigned char new_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 	struct gfs2_rbm rbm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 	struct gfs2_bitmap *bi, *bi_prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 	rbm.rgd = rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 	if (WARN_ON_ONCE(gfs2_rbm_from_block(&rbm, bstart)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 	while (blen--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 		bi = rbm_bi(&rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 		if (bi != bi_prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 			if (!bi->bi_clone) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 				bi->bi_clone = kmalloc(bi->bi_bh->b_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 						      GFP_NOFS | __GFP_NOFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 				memcpy(bi->bi_clone + bi->bi_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 				       bi->bi_bh->b_data + bi->bi_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 				       bi->bi_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 			gfs2_trans_add_meta(rbm.rgd->rd_gl, bi->bi_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 			bi_prev = bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 		gfs2_setbit(&rbm, false, new_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 		gfs2_rbm_incr(&rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214)  * gfs2_rgrp_dump - print out an rgrp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215)  * @seq: The iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216)  * @rgd: The rgrp in question
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217)  * @fs_id_buf: pointer to file system id (if requested)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) void gfs2_rgrp_dump(struct seq_file *seq, struct gfs2_rgrpd *rgd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 		    const char *fs_id_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 	struct gfs2_blkreserv *trs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 	const struct rb_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 	gfs2_print_dbg(seq, "%s R: n:%llu f:%02x b:%u/%u i:%u r:%u e:%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 		       fs_id_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 		       (unsigned long long)rgd->rd_addr, rgd->rd_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 		       rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 		       rgd->rd_reserved, rgd->rd_extfail_pt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 	if (rgd->rd_sbd->sd_args.ar_rgrplvb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 		struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 		gfs2_print_dbg(seq, "%s  L: f:%02x b:%u i:%u\n", fs_id_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 			       be32_to_cpu(rgl->rl_flags),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 			       be32_to_cpu(rgl->rl_free),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 			       be32_to_cpu(rgl->rl_dinodes));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 	spin_lock(&rgd->rd_rsspin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 	for (n = rb_first(&rgd->rd_rstree); n; n = rb_next(&trs->rs_node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 		trs = rb_entry(n, struct gfs2_blkreserv, rs_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 		dump_rs(seq, trs, fs_id_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 	spin_unlock(&rgd->rd_rsspin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 	struct gfs2_sbd *sdp = rgd->rd_sbd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 	char fs_id_buf[sizeof(sdp->sd_fsname) + 7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 	fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 		(unsigned long long)rgd->rd_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 	fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 	sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 	gfs2_rgrp_dump(NULL, rgd, fs_id_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 	rgd->rd_flags |= GFS2_RDF_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262)  * gfs2_adjust_reservation - Adjust (or remove) a reservation after allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263)  * @ip: The inode we have just allocated blocks for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264)  * @rbm: The start of the allocated blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265)  * @len: The extent length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267)  * Adjusts a reservation after an allocation has taken place. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268)  * reservation does not match the allocation, or if it is now empty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269)  * then it is removed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) static void gfs2_adjust_reservation(struct gfs2_inode *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 				    const struct gfs2_rbm *rbm, unsigned len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 	struct gfs2_blkreserv *rs = &ip->i_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 	struct gfs2_rgrpd *rgd = rbm->rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 	unsigned rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 	u64 block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 	spin_lock(&rgd->rd_rsspin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 	if (gfs2_rs_active(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 		if (gfs2_rbm_eq(&rs->rs_rbm, rbm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 			block = gfs2_rbm_to_block(rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 			ret = gfs2_rbm_from_block(&rs->rs_rbm, block + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 			rlen = min(rs->rs_free, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 			rs->rs_free -= rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 			rgd->rd_reserved -= rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 			trace_gfs2_rs(rs, TRACE_RS_CLAIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 			if (rs->rs_free && !ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 			/* We used up our block reservation, so we should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 			   reserve more blocks next time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 			atomic_add(RGRP_RSRV_ADDBLKS, &ip->i_sizehint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 		__rs_deltree(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	spin_unlock(&rgd->rd_rsspin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303)  * gfs2_set_alloc_start - Set starting point for block allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304)  * @rbm: The rbm which will be set to the required location
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305)  * @ip: The gfs2 inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306)  * @dinode: Flag to say if allocation includes a new inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308)  * This sets the starting point from the reservation if one is active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309)  * otherwise it falls back to guessing a start point based on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310)  * inode's goal block or the last allocation point in the rgrp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) static void gfs2_set_alloc_start(struct gfs2_rbm *rbm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 				 const struct gfs2_inode *ip, bool dinode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 	u64 goal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 	if (gfs2_rs_active(&ip->i_res)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 		*rbm = ip->i_res.rs_rbm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	if (!dinode && rgrp_contains_block(rbm->rgd, ip->i_goal))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 		goal = ip->i_goal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 		goal = rbm->rgd->rd_last_alloc + rbm->rgd->rd_data0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 	if (WARN_ON_ONCE(gfs2_rbm_from_block(rbm, goal))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 		rbm->bii = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 		rbm->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335)  * gfs2_alloc_blocks - Allocate one or more blocks of data and/or a dinode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336)  * @ip: the inode to allocate the block for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337)  * @bn: Used to return the starting block number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338)  * @nblocks: requested number of blocks/extent length (value/result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339)  * @dinode: 1 if we're allocating a dinode block, else 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340)  * @generation: the generation number of the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342)  * Returns: 0 or error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 		      bool dinode, u64 *generation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 	struct buffer_head *dibh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 	struct gfs2_rbm rbm = { .rgd = ip->i_res.rs_rbm.rgd, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	unsigned int ndata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 	u64 block; /* block, within the file system scope */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 	gfs2_set_alloc_start(&rbm, ip, dinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 	error = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, NULL, ip, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 	if (error == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 		gfs2_set_alloc_start(&rbm, ip, dinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 		error = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, NULL, NULL, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 	/* Since all blocks are reserved in advance, this shouldn't happen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 		fs_warn(sdp, "inum=%llu error=%d, nblocks=%u, full=%d fail_pt=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 			(unsigned long long)ip->i_no_addr, error, *nblocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 			test_bit(GBF_FULL, &rbm.rgd->rd_bits->bi_flags),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 			rbm.rgd->rd_extfail_pt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 		goto rgrp_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 	gfs2_alloc_extent(&rbm, dinode, nblocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 	block = gfs2_rbm_to_block(&rbm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 	rbm.rgd->rd_last_alloc = block - rbm.rgd->rd_data0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 	if (gfs2_rs_active(&ip->i_res))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 		gfs2_adjust_reservation(ip, &rbm, *nblocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 	ndata = *nblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 	if (dinode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 		ndata--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	if (!dinode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 		ip->i_goal = block + ndata - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 		error = gfs2_meta_inode_buffer(ip, &dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 		if (error == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 			struct gfs2_dinode *di =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 				(struct gfs2_dinode *)dibh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 			gfs2_trans_add_meta(ip->i_gl, dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 			di->di_goal_meta = di->di_goal_data =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 				cpu_to_be64(ip->i_goal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 			brelse(dibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 	if (rbm.rgd->rd_free < *nblocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 		fs_warn(sdp, "nblocks=%u\n", *nblocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 		goto rgrp_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 	rbm.rgd->rd_free -= *nblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 	if (dinode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 		rbm.rgd->rd_dinodes++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 		*generation = rbm.rgd->rd_igeneration++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 		if (*generation == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 			*generation = rbm.rgd->rd_igeneration++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 	gfs2_trans_add_meta(rbm.rgd->rd_gl, rbm.rgd->rd_bits[0].bi_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 	gfs2_rgrp_out(rbm.rgd, rbm.rgd->rd_bits[0].bi_bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 	gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 	if (dinode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 		gfs2_trans_remove_revoke(sdp, block, *nblocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 	gfs2_quota_change(ip, *nblocks, ip->i_inode.i_uid, ip->i_inode.i_gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 	rbm.rgd->rd_free_clone -= *nblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 	trace_gfs2_block_alloc(ip, rbm.rgd, block, *nblocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 			       dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 	*bn = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) rgrp_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 	gfs2_rgrp_error(rbm.rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427)  * __gfs2_free_blocks - free a contiguous run of block(s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428)  * @ip: the inode these blocks are being freed from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429)  * @rgd: the resource group the blocks are in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430)  * @bstart: first block of a run of contiguous blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431)  * @blen: the length of the block run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432)  * @meta: 1 if the blocks represent metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) void __gfs2_free_blocks(struct gfs2_inode *ip, struct gfs2_rgrpd *rgd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 			u64 bstart, u32 blen, int meta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 	rgblk_free(sdp, rgd, bstart, blen, GFS2_BLKST_FREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 	trace_gfs2_block_alloc(ip, rgd, bstart, blen, GFS2_BLKST_FREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 	rgd->rd_free += blen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 	rgd->rd_flags &= ~GFS2_RGF_TRIMMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 	gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 	gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 	/* Directories keep their data in the metadata address space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 	if (meta || ip->i_depth || gfs2_is_jdata(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 		gfs2_journal_wipe(ip, bstart, blen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454)  * gfs2_free_meta - free a contiguous run of data block(s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455)  * @ip: the inode these blocks are being freed from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456)  * @rgd: the resource group the blocks are in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457)  * @bstart: first block of a run of contiguous blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458)  * @blen: the length of the block run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) void gfs2_free_meta(struct gfs2_inode *ip, struct gfs2_rgrpd *rgd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 		    u64 bstart, u32 blen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 	__gfs2_free_blocks(ip, rgd, bstart, blen, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 	gfs2_statfs_change(sdp, 0, +blen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 	gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) void gfs2_unlink_di(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 	struct gfs2_inode *ip = GFS2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 	struct gfs2_sbd *sdp = GFS2_SB(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 	struct gfs2_rgrpd *rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 	u64 blkno = ip->i_no_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 	rgd = gfs2_blk2rgrpd(sdp, blkno, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 	if (!rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 	rgblk_free(sdp, rgd, blkno, 1, GFS2_BLKST_UNLINKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	trace_gfs2_block_alloc(ip, rgd, blkno, 1, GFS2_BLKST_UNLINKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 	be32_add_cpu(&rgd->rd_rgl->rl_unlinked, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	struct gfs2_sbd *sdp = rgd->rd_sbd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 	rgblk_free(sdp, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	if (!rgd->rd_dinodes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 		gfs2_consist_rgrpd(rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	rgd->rd_dinodes--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 	rgd->rd_free++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 	gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 	gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 	be32_add_cpu(&rgd->rd_rgl->rl_unlinked, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	gfs2_statfs_change(sdp, 0, +1, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 	trace_gfs2_block_alloc(ip, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 	gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 	gfs2_journal_wipe(ip, ip->i_no_addr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510)  * gfs2_check_blk_type - Check the type of a block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511)  * @sdp: The superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512)  * @no_addr: The block number to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513)  * @type: The block type we are looking for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515)  * Returns: 0 if the block type matches the expected type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516)  *          -ESTALE if it doesn't match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517)  *          or -ve errno if something went wrong while checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	struct gfs2_rgrpd *rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	struct gfs2_holder rgd_gh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	struct gfs2_rbm rbm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 	int error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 	rgd = gfs2_blk2rgrpd(sdp, no_addr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 	if (!rgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 	error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 	rbm.rgd = rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 	error = gfs2_rbm_from_block(&rbm, no_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 	if (!WARN_ON_ONCE(error)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 		if (gfs2_testbit(&rbm, false) != type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 			error = -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 	gfs2_glock_dq_uninit(&rgd_gh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549)  * gfs2_rlist_add - add a RG to a list of RGs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550)  * @ip: the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551)  * @rlist: the list of resource groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552)  * @block: the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554)  * Figure out what RG a block belongs to and add that RG to the list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556)  * FIXME: Don't use NOFAIL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) void gfs2_rlist_add(struct gfs2_inode *ip, struct gfs2_rgrp_list *rlist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 		    u64 block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 	struct gfs2_rgrpd *rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 	struct gfs2_rgrpd **tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 	unsigned int new_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 	unsigned int x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 	if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 	 * The resource group last accessed is kept in the last position.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 	if (rlist->rl_rgrps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 		rgd = rlist->rl_rgd[rlist->rl_rgrps - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 		if (rgrp_contains_block(rgd, block))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 		rgd = gfs2_blk2rgrpd(sdp, block, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 		rgd = ip->i_res.rs_rbm.rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 		if (!rgd || !rgrp_contains_block(rgd, block))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 			rgd = gfs2_blk2rgrpd(sdp, block, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 	if (!rgd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 		fs_err(sdp, "rlist_add: no rgrp for block %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 		       (unsigned long long)block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 	for (x = 0; x < rlist->rl_rgrps; x++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 		if (rlist->rl_rgd[x] == rgd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 			swap(rlist->rl_rgd[x],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 			     rlist->rl_rgd[rlist->rl_rgrps - 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 	if (rlist->rl_rgrps == rlist->rl_space) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 		new_space = rlist->rl_space + 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 		tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 			      GFP_NOFS | __GFP_NOFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 		if (rlist->rl_rgd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 			memcpy(tmp, rlist->rl_rgd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 			       rlist->rl_space * sizeof(struct gfs2_rgrpd *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 			kfree(rlist->rl_rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 		rlist->rl_space = new_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 		rlist->rl_rgd = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 	rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621)  * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622)  *      and initialize an array of glock holders for them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623)  * @rlist: the list of resource groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625)  * FIXME: Don't use NOFAIL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 	unsigned int x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 	rlist->rl_ghs = kmalloc_array(rlist->rl_rgrps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 				      sizeof(struct gfs2_holder),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 				      GFP_NOFS | __GFP_NOFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 	for (x = 0; x < rlist->rl_rgrps; x++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 		gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 				LM_ST_EXCLUSIVE, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 				&rlist->rl_ghs[x]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643)  * gfs2_rlist_free - free a resource group list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644)  * @rlist: the list of resource groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 	unsigned int x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 	kfree(rlist->rl_rgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 	if (rlist->rl_ghs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 		for (x = 0; x < rlist->rl_rgrps; x++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 			gfs2_holder_uninit(&rlist->rl_ghs[x]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 		kfree(rlist->rl_ghs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 		rlist->rl_ghs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661)