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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /* -*- mode: c; c-basic-offset: 8; -*-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * vim: noexpandtab sw=8 ts=8 sts=0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * suballoc.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * metadata alloc and free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * Inspired by ext3 block groups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) 
^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/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <cluster/masklog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include "ocfs2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include "alloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include "blockcheck.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include "dlmglue.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include "inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include "journal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include "localalloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include "suballoc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include "super.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include "sysfile.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include "uptodate.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include "ocfs2_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include "buffer_head_io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define NOT_ALLOC_NEW_GROUP		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define ALLOC_NEW_GROUP			0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #define ALLOC_GROUPS_FROM_GLOBAL	0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #define OCFS2_MAX_TO_STEAL		1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) struct ocfs2_suballoc_result {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	u64		sr_bg_blkno;	/* The bg we allocated from.  Set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 					   to 0 when a block group is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 					   contiguous. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	u64		sr_bg_stable_blkno; /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 					     * Doesn't change, always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 					     * set to target block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 					     * group descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 					     * block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 					     */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	u64		sr_blkno;	/* The first allocated block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	unsigned int	sr_bit_offset;	/* The bit in the bg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	unsigned int	sr_bits;	/* How many bits we claimed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) static u64 ocfs2_group_from_res(struct ocfs2_suballoc_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	if (res->sr_blkno == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	if (res->sr_bg_blkno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 		return res->sr_bg_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	return ocfs2_which_suballoc_group(res->sr_blkno, res->sr_bit_offset);
^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) static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) static int ocfs2_block_group_fill(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 				  struct inode *alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 				  struct buffer_head *bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 				  u64 group_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 				  unsigned int group_clusters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 				  u16 my_chain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 				  struct ocfs2_chain_list *cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 				   struct inode *alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 				   struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 				   u64 max_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 				   u64 *last_alloc_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 				   int flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) static int ocfs2_cluster_group_search(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 				      struct buffer_head *group_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 				      u32 bits_wanted, u32 min_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 				      u64 max_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 				      struct ocfs2_suballoc_result *res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) static int ocfs2_block_group_search(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 				    struct buffer_head *group_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 				    u32 bits_wanted, u32 min_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 				    u64 max_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 				    struct ocfs2_suballoc_result *res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 				     handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 				     u32 bits_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 				     u32 min_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 				     struct ocfs2_suballoc_result *res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 					 int nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) static int ocfs2_relink_block_group(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 				    struct inode *alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 				    struct buffer_head *fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 				    struct buffer_head *bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 				    struct buffer_head *prev_bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 				    u16 chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 						     u32 wanted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 						   u64 bg_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 						   u16 bg_bit_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) static inline void ocfs2_block_to_cluster_group(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 						u64 data_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 						u64 *bg_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 						u16 *bg_bit_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 					     u32 bits_wanted, u64 max_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 					     int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 					     struct ocfs2_alloc_context **ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	struct inode *inode = ac->ac_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 		if (ac->ac_which != OCFS2_AC_USE_LOCAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 			ocfs2_inode_unlock(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		ac->ac_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	brelse(ac->ac_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	ac->ac_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	ac->ac_resv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	kfree(ac->ac_find_loc_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	ac->ac_find_loc_priv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	ocfs2_free_ac_resource(ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	kfree(ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) #define do_error(fmt, ...)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) do {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	if (resize)							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		mlog(ML_ERROR, fmt, ##__VA_ARGS__);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	else								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		return ocfs2_error(sb, fmt, ##__VA_ARGS__);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) static int ocfs2_validate_gd_self(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 				  struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 				  int resize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		do_error("Group descriptor #%llu has bad signature %.*s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 			 (unsigned long long)bh->b_blocknr, 7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 			 gd->bg_signature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		do_error("Group descriptor #%llu has an invalid bg_blkno of %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 			 (unsigned long long)bh->b_blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 			 (unsigned long long)le64_to_cpu(gd->bg_blkno));
^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) 	if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		do_error("Group descriptor #%llu has an invalid fs_generation of #%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 			 (unsigned long long)bh->b_blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 			 le32_to_cpu(gd->bg_generation));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		do_error("Group descriptor #%llu has bit count %u but claims that %u are free\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 			 (unsigned long long)bh->b_blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 			 le16_to_cpu(gd->bg_bits),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 			 le16_to_cpu(gd->bg_free_bits_count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		do_error("Group descriptor #%llu has bit count %u but max bitmap bits of %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 			 (unsigned long long)bh->b_blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 			 le16_to_cpu(gd->bg_bits),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 			 8 * le16_to_cpu(gd->bg_size));
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) static int ocfs2_validate_gd_parent(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 				    struct ocfs2_dinode *di,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 				    struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 				    int resize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	unsigned int max_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	if (di->i_blkno != gd->bg_parent_dinode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		do_error("Group descriptor #%llu has bad parent pointer (%llu, expected %llu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 			 (unsigned long long)bh->b_blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 			 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 			 (unsigned long long)le64_to_cpu(di->i_blkno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	if (le16_to_cpu(gd->bg_bits) > max_bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 		do_error("Group descriptor #%llu has bit count of %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 			 (unsigned long long)bh->b_blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 			 le16_to_cpu(gd->bg_bits));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	/* In resize, we may meet the case bg_chain == cl_next_free_rec. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	if ((le16_to_cpu(gd->bg_chain) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	     le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	    ((le16_to_cpu(gd->bg_chain) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	     le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		do_error("Group descriptor #%llu has bad chain %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 			 (unsigned long long)bh->b_blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 			 le16_to_cpu(gd->bg_chain));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) #undef do_error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238)  * This version only prints errors.  It does not fail the filesystem, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239)  * exists only for resize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) int ocfs2_check_group_descriptor(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 				 struct ocfs2_dinode *di,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 				 struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	BUG_ON(!buffer_uptodate(bh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	 * If the ecc fails, we return the error but otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	 * leave the filesystem running.  We know any error is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	 * local to this block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		mlog(ML_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		     "Checksum failed for group descriptor %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		     (unsigned long long)bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		rc = ocfs2_validate_gd_self(sb, bh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) static int ocfs2_validate_group_descriptor(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 					   struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	trace_ocfs2_validate_group_descriptor(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 					(unsigned long long)bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	BUG_ON(!buffer_uptodate(bh));
^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) 	 * If the ecc fails, we return the error but otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	 * leave the filesystem running.  We know any error is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	 * local to this block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	 * Errors after here are fatal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	return ocfs2_validate_gd_self(sb, bh, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 				u64 gd_blkno, struct buffer_head **bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	struct buffer_head *tmp = *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 			      ocfs2_validate_group_descriptor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		brelse(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	/* If ocfs2_read_block() got us a new bh, pass it up. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	if (!*bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		*bh = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 					  struct ocfs2_group_desc *bg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 					  struct ocfs2_chain_list *cl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 					  u64 p_blkno, unsigned int clusters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	struct ocfs2_extent_list *el = &bg->bg_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	struct ocfs2_extent_rec *rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	BUG_ON(!ocfs2_supports_discontig_bg(osb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	if (!el->l_next_free_rec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	rec->e_blkno = cpu_to_le64(p_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 				  le16_to_cpu(cl->cl_bpc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	rec->e_leaf_clusters = cpu_to_le16(clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	le16_add_cpu(&bg->bg_free_bits_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		     clusters * le16_to_cpu(cl->cl_bpc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	le16_add_cpu(&el->l_next_free_rec, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) static int ocfs2_block_group_fill(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 				  struct inode *alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 				  struct buffer_head *bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 				  u64 group_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 				  unsigned int group_clusters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 				  u16 my_chain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 				  struct ocfs2_chain_list *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	struct super_block * sb = alloc_inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		status = ocfs2_error(alloc_inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 				     "group block (%llu) != b_blocknr (%llu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 				     (unsigned long long)group_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 				     (unsigned long long) bg_bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	status = ocfs2_journal_access_gd(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 					 INODE_CACHE(alloc_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 					 bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 					 OCFS2_JOURNAL_ACCESS_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	memset(bg, 0, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	bg->bg_generation = cpu_to_le32(osb->fs_generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 						osb->s_feature_incompat));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	bg->bg_chain = cpu_to_le16(my_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	bg->bg_blkno = cpu_to_le64(group_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	if (group_clusters == le16_to_cpu(cl->cl_cpg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 					      group_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	/* set the 1st bit in the bitmap to account for the descriptor block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	ocfs2_journal_dirty(handle, bg_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	/* There is no need to zero out or otherwise initialize the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	 * other blocks in a group - All valid FS metadata in a block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	 * group stores the superblock fs_generation value at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	 * allocation time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	u16 curr, best;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	best = curr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	while (curr < le16_to_cpu(cl->cl_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		if (le32_to_cpu(cl->cl_recs[best].c_total) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		    le32_to_cpu(cl->cl_recs[curr].c_total))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 			best = curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		curr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	return best;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) static struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 			       struct inode *alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 			       struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 			       struct ocfs2_chain_list *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	u32 bit_off, num_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	u64 bg_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	struct buffer_head *bg_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	status = ocfs2_claim_clusters(handle, ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 				      le16_to_cpu(cl->cl_cpg), &bit_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 				      &num_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		if (status != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 			mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		goto bail;
^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) 	/* setup the group */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	trace_ocfs2_block_group_alloc_contig(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	     (unsigned long long)bg_blkno, alloc_rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	bg_bh = sb_getblk(osb->sb, bg_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	if (!bg_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 					bg_blkno, num_bits, alloc_rec, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		brelse(bg_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		mlog_errno(status);
^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) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	return status ? ERR_PTR(status) : bg_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 					handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 					struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 					unsigned int min_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 					u32 *bit_off, u32 *num_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	while (min_bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		status = ocfs2_claim_clusters(handle, ac, min_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 					      bit_off, num_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		if (status != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		min_bits >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	return status;
^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) static int ocfs2_block_group_grow_discontig(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 					    struct inode *alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 					    struct buffer_head *bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 					    struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 					    struct ocfs2_chain_list *cl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 					    unsigned int min_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	struct ocfs2_group_desc *bg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 		(struct ocfs2_group_desc *)bg_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	unsigned int needed = le16_to_cpu(cl->cl_cpg) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 			 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	u32 p_cpos, clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	u64 p_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	struct ocfs2_extent_list *el = &bg->bg_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	status = ocfs2_journal_access_gd(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 					 INODE_CACHE(alloc_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 					 bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 					 OCFS2_JOURNAL_ACCESS_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 				le16_to_cpu(el->l_count))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		if (min_bits > needed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 			min_bits = needed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		status = ocfs2_block_group_claim_bits(osb, handle, ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 						      min_bits, &p_cpos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 						      &clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 			if (status != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 				mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 			goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 					      clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		min_bits = clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		needed = le16_to_cpu(cl->cl_cpg) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 			 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
^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) 	if (needed > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		 * We have used up all the extent rec but can't fill up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		 * the cpg. So bail out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		status = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	ocfs2_journal_dirty(handle, bg_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) static void ocfs2_bg_alloc_cleanup(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 				   struct ocfs2_alloc_context *cluster_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 				   struct inode *alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 				   struct buffer_head *bg_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	struct ocfs2_group_desc *bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	struct ocfs2_extent_list *el;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	struct ocfs2_extent_rec *rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	if (!bg_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	bg = (struct ocfs2_group_desc *)bg_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	el = &bg->bg_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		rec = &el->l_recs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 					  cluster_ac->ac_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 					  le64_to_cpu(rec->e_blkno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 					  le16_to_cpu(rec->e_leaf_clusters));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 			mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		/* Try all the clusters to free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	brelse(bg_bh);
^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) static struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) ocfs2_block_group_alloc_discontig(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 				  struct inode *alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 				  struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 				  struct ocfs2_chain_list *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	u32 bit_off, num_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	u64 bg_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	struct buffer_head *bg_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	if (!ocfs2_supports_discontig_bg(osb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		status = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	status = ocfs2_extend_trans(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 				    ocfs2_calc_bg_discontig_credits(osb->sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	 * We're going to be grabbing from multiple cluster groups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	 * We don't have enough credits to relink them all, and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	 * cluster groups will be staying in cache for the duration of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	 * this operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	ac->ac_disable_chain_relink = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	/* Claim the first region */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 					      &bit_off, &num_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		if (status != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 			mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	min_bits = num_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	/* setup the group */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	trace_ocfs2_block_group_alloc_discontig(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 				(unsigned long long)bg_blkno, alloc_rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	bg_bh = sb_getblk(osb->sb, bg_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	if (!bg_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 					bg_blkno, num_bits, alloc_rec, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 						  bg_bh, ac, cl, min_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	return status ? ERR_PTR(status) : bg_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) }
^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)  * We expect the block group allocator to already be locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 				   struct inode *alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 				   struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 				   u64 max_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 				   u64 *last_alloc_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 				   int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	int status, credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	struct ocfs2_chain_list *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	struct ocfs2_alloc_context *ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	handle_t *handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	u16 alloc_rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	struct buffer_head *bg_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	struct ocfs2_group_desc *bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	cl = &fe->id2.i_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	status = ocfs2_reserve_clusters_with_limit(osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 						   le16_to_cpu(cl->cl_cpg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 						   max_block, flags, &ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		if (status != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 			mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	credits = ocfs2_calc_group_alloc_credits(osb->sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 						 le16_to_cpu(cl->cl_cpg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	handle = ocfs2_start_trans(osb, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		status = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	if (last_alloc_group && *last_alloc_group != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		trace_ocfs2_block_group_alloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 				(unsigned long long)*last_alloc_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		ac->ac_last_group = *last_alloc_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 					       ac, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	if (PTR_ERR(bg_bh) == -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		bg_bh = ocfs2_block_group_alloc_discontig(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 							  alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 							  ac, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	if (IS_ERR(bg_bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		status = PTR_ERR(bg_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		bg_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		if (status != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 			mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	bg = (struct ocfs2_group_desc *) bg_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 					 bh, OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	alloc_rec = le16_to_cpu(bg->bg_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		     le16_to_cpu(bg->bg_free_bits_count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	le32_add_cpu(&cl->cl_recs[alloc_rec].c_total,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		     le16_to_cpu(bg->bg_bits));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	cl->cl_recs[alloc_rec].c_blkno = bg->bg_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		le16_add_cpu(&cl->cl_next_free_rec, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 					le16_to_cpu(bg->bg_free_bits_count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	ocfs2_journal_dirty(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 					     le32_to_cpu(fe->i_clusters)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	ocfs2_update_inode_fsync_trans(handle, alloc_inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	/* save the new last alloc group so that the caller can cache it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	if (last_alloc_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		*last_alloc_group = ac->ac_last_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		ocfs2_commit_trans(osb, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	if (ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		ocfs2_free_alloc_context(ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	brelse(bg_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 				       struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 				       int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 				       u32 slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 				       u64 *last_alloc_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 				       int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	u32 bits_wanted = ac->ac_bits_wanted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	struct inode *alloc_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	struct ocfs2_dinode *fe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	u32 free_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	if (!alloc_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		mlog_errno(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	inode_lock(alloc_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	status = ocfs2_inode_lock(alloc_inode, &bh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		inode_unlock(alloc_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		iput(alloc_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	ac->ac_inode = alloc_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	ac->ac_alloc_slot = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	fe = (struct ocfs2_dinode *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	/* The bh was validated by the inode read inside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	 * ocfs2_inode_lock().  Any corruption is a code bug. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		status = ocfs2_error(alloc_inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 				     "Invalid chain allocator %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 				     (unsigned long long)le64_to_cpu(fe->i_blkno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		le32_to_cpu(fe->id1.bitmap1.i_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	if (bits_wanted > free_bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		/* cluster bitmap never grows */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		if (ocfs2_is_cluster_bitmap(alloc_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 			trace_ocfs2_reserve_suballoc_bits_nospc(bits_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 								free_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 			status = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 			goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		if (!(flags & ALLOC_NEW_GROUP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 			trace_ocfs2_reserve_suballoc_bits_no_new_group(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 						slot, bits_wanted, free_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 			status = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 						 ac->ac_max_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 						 last_alloc_group, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 			if (status != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 				mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 			goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		atomic_inc(&osb->alloc_stats.bg_extends);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		/* You should never ask for this much metadata */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		BUG_ON(bits_wanted >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		       (le32_to_cpu(fe->id1.bitmap1.i_total)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 			- le32_to_cpu(fe->id1.bitmap1.i_used)));
^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) 	get_bh(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	ac->ac_bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	spin_lock(&osb->osb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	spin_unlock(&osb->osb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	atomic_set(&osb->s_num_inodes_stolen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	spin_lock(&osb->osb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	spin_unlock(&osb->osb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	atomic_set(&osb->s_num_meta_stolen, 0);
^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) void ocfs2_init_steal_slots(struct ocfs2_super *osb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	ocfs2_init_inode_steal_slot(osb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	ocfs2_init_meta_steal_slot(osb);
^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 void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	spin_lock(&osb->osb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	if (type == INODE_ALLOC_SYSTEM_INODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		osb->s_inode_steal_slot = (u16)slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	else if (type == EXTENT_ALLOC_SYSTEM_INODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		osb->s_meta_steal_slot = (u16)slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	spin_unlock(&osb->osb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	int slot = OCFS2_INVALID_SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	spin_lock(&osb->osb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	if (type == INODE_ALLOC_SYSTEM_INODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		slot = osb->s_inode_steal_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	else if (type == EXTENT_ALLOC_SYSTEM_INODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		slot = osb->s_meta_steal_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	spin_unlock(&osb->osb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	return slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) static int ocfs2_steal_resource(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 				struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 				int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	int i, status = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	int slot = __ocfs2_get_steal_slot(osb, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	/* Start to steal resource from the first slot after ours. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	if (slot == OCFS2_INVALID_SLOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		slot = osb->slot_num + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	for (i = 0; i < osb->max_slots; i++, slot++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		if (slot == osb->max_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 			slot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		if (slot == osb->slot_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		status = ocfs2_reserve_suballoc_bits(osb, ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 						     type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 						     (u32)slot, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 						     NOT_ALLOC_NEW_GROUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		if (status >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 			__ocfs2_set_steal_slot(osb, slot, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 			break;
^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) 		ocfs2_free_ac_resource(ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) static int ocfs2_steal_inode(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 			     struct ocfs2_alloc_context *ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) static int ocfs2_steal_meta(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			    struct ocfs2_alloc_context *ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 				      int blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 				      struct ocfs2_alloc_context **ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	int slot = ocfs2_get_meta_steal_slot(osb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	if (!(*ac)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	(*ac)->ac_bits_wanted = blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	(*ac)->ac_which = OCFS2_AC_USE_META;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	(*ac)->ac_group_search = ocfs2_block_group_search;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	if (slot != OCFS2_INVALID_SLOT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		goto extent_steal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	atomic_set(&osb->s_num_meta_stolen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	status = ocfs2_reserve_suballoc_bits(osb, (*ac),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 					     EXTENT_ALLOC_SYSTEM_INODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 					     (u32)osb->slot_num, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 					     ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	if (status >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		if (slot != OCFS2_INVALID_SLOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 			ocfs2_init_meta_steal_slot(osb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	} else if (status < 0 && status != -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	ocfs2_free_ac_resource(*ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) extent_steal:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	status = ocfs2_steal_meta(osb, *ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	atomic_inc(&osb->s_num_meta_stolen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		if (status != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 			mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	if ((status < 0) && *ac) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		ocfs2_free_alloc_context(*ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		*ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 			       struct ocfs2_extent_list *root_el,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 			       struct ocfs2_alloc_context **ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	return ocfs2_reserve_new_metadata_blocks(osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 					ocfs2_extend_meta_needed(root_el),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 					ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 			    struct ocfs2_alloc_context **ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	int slot = ocfs2_get_inode_steal_slot(osb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	u64 alloc_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	if (!(*ac)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	(*ac)->ac_bits_wanted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	(*ac)->ac_which = OCFS2_AC_USE_INODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	(*ac)->ac_group_search = ocfs2_block_group_search;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	 * stat(2) can't handle i_ino > 32bits, so we tell the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	 * lower levels not to allocate us a block group past that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	 * limit.  The 'inode64' mount option avoids this behavior.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		(*ac)->ac_max_block = (u32)~0U;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	 * slot is set when we successfully steal inode from other nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	 * It is reset in 3 places:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	 * 1. when we flush the truncate log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	 * 2. when we complete local alloc recovery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	 * 3. when we successfully allocate from our own slot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	 * After it is set, we will go on stealing inodes until we find the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	 * need to check our slots to see whether there is some space for us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	if (slot != OCFS2_INVALID_SLOT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	    atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		goto inode_steal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	atomic_set(&osb->s_num_inodes_stolen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	alloc_group = osb->osb_inode_alloc_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	status = ocfs2_reserve_suballoc_bits(osb, *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 					     INODE_ALLOC_SYSTEM_INODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 					     (u32)osb->slot_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 					     &alloc_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 					     ALLOC_NEW_GROUP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 					     ALLOC_GROUPS_FROM_GLOBAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	if (status >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		spin_lock(&osb->osb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		osb->osb_inode_alloc_group = alloc_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		spin_unlock(&osb->osb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		trace_ocfs2_reserve_new_inode_new_group(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 			(unsigned long long)alloc_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		 * Some inodes must be freed by us, so try to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		 * from our own next time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		if (slot != OCFS2_INVALID_SLOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 			ocfs2_init_inode_steal_slot(osb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	} else if (status < 0 && status != -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	ocfs2_free_ac_resource(*ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) inode_steal:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	status = ocfs2_steal_inode(osb, *ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	atomic_inc(&osb->s_num_inodes_stolen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		if (status != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 			mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	if ((status < 0) && *ac) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		ocfs2_free_alloc_context(*ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		*ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) /* local alloc code has to do the same thing, so rather than do this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)  * twice.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 				      struct ocfs2_alloc_context *ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	ac->ac_which = OCFS2_AC_USE_MAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	ac->ac_group_search = ocfs2_cluster_group_search;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	status = ocfs2_reserve_suballoc_bits(osb, ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 					     GLOBAL_BITMAP_SYSTEM_INODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 					     OCFS2_INVALID_SLOT, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 					     ALLOC_NEW_GROUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	if (status < 0 && status != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) /* Callers don't need to care which bitmap (local alloc or main) to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141)  * use so we figure it out for them, but unfortunately this clutters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)  * things a bit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 					     u32 bits_wanted, u64 max_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 					     int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 					     struct ocfs2_alloc_context **ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	int status, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	int retried = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	if (!(*ac)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	(*ac)->ac_bits_wanted = bits_wanted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	(*ac)->ac_max_block = max_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	status = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	    ocfs2_alloc_should_use_local(osb, bits_wanted)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		status = ocfs2_reserve_local_alloc_bits(osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 							bits_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 							*ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		if ((status < 0) && (status != -ENOSPC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 			mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 			goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	if (status == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		/* Retry if there is sufficient space cached in truncate log */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		if (status == -ENOSPC && !retried) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 			retried = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 			ocfs2_inode_unlock((*ac)->ac_inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 			inode_unlock((*ac)->ac_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 			ret = ocfs2_try_to_free_truncate_log(osb, bits_wanted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 			if (ret == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 				iput((*ac)->ac_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 				(*ac)->ac_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 				goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 				mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 			inode_lock((*ac)->ac_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 			ret = ocfs2_inode_lock((*ac)->ac_inode, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 			if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 				mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 				inode_unlock((*ac)->ac_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 				iput((*ac)->ac_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 				(*ac)->ac_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 				goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 			if (status != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 				mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 			goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	if ((status < 0) && *ac) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		ocfs2_free_alloc_context(*ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		*ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) int ocfs2_reserve_clusters(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 			   u32 bits_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 			   struct ocfs2_alloc_context **ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 						 ALLOC_NEW_GROUP, ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) }
^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)  * More or less lifted from ext3. I'll leave their description below:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)  * "For ext3 allocations, we must not reuse any blocks which are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)  * allocated in the bitmap buffer's "last committed data" copy.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)  * prevents deletes from freeing up the page for reuse until we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235)  * committed the delete transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237)  * If we didn't do this, then deleting something and reallocating it as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)  * data would allow the old block to be overwritten before the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)  * transaction committed (because we force data to disk before commit).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)  * This would lead to corruption if we crashed between overwriting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241)  * data and committing the delete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243)  * @@@ We may want to make this allocation behaviour conditional on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)  * data-writes at some point, and disable it for metadata allocations or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)  * sync-data inodes."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247)  * Note: OCFS2 already does this differently for metadata vs data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)  * allocations, as those bitmaps are separate and undo access is never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249)  * called on a metadata group descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 					 int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	struct journal_head *jh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	jh = jbd2_journal_grab_journal_head(bg_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	if (!jh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	spin_lock(&jh->b_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	bg = (struct ocfs2_group_desc *) jh->b_committed_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	if (bg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 		ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	spin_unlock(&jh->b_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	jbd2_journal_put_journal_head(jh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 					     struct buffer_head *bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 					     unsigned int bits_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 					     unsigned int total_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 					     struct ocfs2_suballoc_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	void *bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	u16 best_offset, best_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	int offset, start, found, status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	/* Callers got this descriptor from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	found = start = best_offset = best_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	bitmap = bg->bg_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		if (offset == total_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 			/* We found a zero, but we can't use it as it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 			 * hasn't been put to disk yet! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 			found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 			start = offset + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		} else if (offset == start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 			/* we found a zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 			found++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 			/* move start to the next bit to test */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 			start++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 			/* got a zero after some ones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 			found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 			start = offset + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		if (found > best_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 			best_size = found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 			best_offset = start - found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		/* we got everything we needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		if (found == bits_wanted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 			/* mlog(0, "Found it all!\n"); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	if (best_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 		res->sr_bit_offset = best_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		res->sr_bits = best_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		status = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		/* No error log here -- see the comment above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		 * ocfs2_test_bg_bit_allocatable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) int ocfs2_block_group_set_bits(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 					     struct inode *alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 					     struct ocfs2_group_desc *bg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 					     struct buffer_head *group_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 					     unsigned int bit_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 					     unsigned int num_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	void *bitmap = bg->bg_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	/* All callers get the descriptor via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	trace_ocfs2_block_group_set_bits(bit_off, num_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	if (ocfs2_is_cluster_bitmap(alloc_inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	status = ocfs2_journal_access_gd(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 					 INODE_CACHE(alloc_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 					 group_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 					 journal_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 		return ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit count %u but claims %u are freed. num_bits %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 				   (unsigned long long)le64_to_cpu(bg->bg_blkno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 				   le16_to_cpu(bg->bg_bits),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 				   le16_to_cpu(bg->bg_free_bits_count),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 				   num_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	while(num_bits--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		ocfs2_set_bit(bit_off++, bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	ocfs2_journal_dirty(handle, group_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) /* find the one with the most empty bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	u16 curr, best;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	BUG_ON(!cl->cl_next_free_rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	best = curr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 		if (le32_to_cpu(cl->cl_recs[curr].c_free) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		    le32_to_cpu(cl->cl_recs[best].c_free))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 			best = curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 		curr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	return best;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) static int ocfs2_relink_block_group(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 				    struct inode *alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 				    struct buffer_head *fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 				    struct buffer_head *bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 				    struct buffer_head *prev_bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 				    u16 chain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	/* there is a really tiny chance the journal calls could fail,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	 * but we wouldn't want inconsistent blocks in *any* case. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	u64 bg_ptr, prev_bg_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	/* The caller got these descriptors from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	trace_ocfs2_relink_block_group(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		(unsigned long long)le64_to_cpu(fe->i_blkno), chain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		(unsigned long long)le64_to_cpu(bg->bg_blkno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		(unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	bg_ptr = le64_to_cpu(bg->bg_next_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 					 prev_bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 					 OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	prev_bg->bg_next_group = bg->bg_next_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	ocfs2_journal_dirty(handle, prev_bg_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 					 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 		goto out_rollback_prev_bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	ocfs2_journal_dirty(handle, bg_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 					 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 		goto out_rollback_bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	ocfs2_journal_dirty(handle, fe_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) out_rollback_bg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	bg->bg_next_group = cpu_to_le64(bg_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) out_rollback_prev_bg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 						     u32 wanted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	return le16_to_cpu(bg->bg_free_bits_count) > wanted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) /* return 0 on success, -ENOSPC to keep searching and any other < 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475)  * value on error. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) static int ocfs2_cluster_group_search(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 				      struct buffer_head *group_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 				      u32 bits_wanted, u32 min_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 				      u64 max_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 				      struct ocfs2_suballoc_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	int search = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	u64 blkoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	unsigned int max_bits, gd_cluster_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	if (gd->bg_free_bits_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		max_bits = le16_to_cpu(gd->bg_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 		/* Tail groups in cluster bitmaps which aren't cpg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 		 * aligned are prone to partial extension by a failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		 * fs resize. If the file system resize never got to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 		 * update the dinode cluster count, then we don't want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		 * to trust any clusters past it, regardless of what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 		 * the group descriptor says. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 							  le64_to_cpu(gd->bg_blkno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		if ((gd_cluster_off + max_bits) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 		    OCFS2_I(inode)->ip_clusters) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 			max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 			trace_ocfs2_cluster_group_search_wrong_max_bits(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 				(unsigned long long)le64_to_cpu(gd->bg_blkno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 				le16_to_cpu(gd->bg_bits),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 				OCFS2_I(inode)->ip_clusters, max_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 		ret = ocfs2_block_group_find_clear_bits(osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 							group_bh, bits_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 							max_bits, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 		if (max_block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 			blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 							  gd_cluster_off +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 							  res->sr_bit_offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 							  res->sr_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 			trace_ocfs2_cluster_group_search_max_block(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 				(unsigned long long)blkoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 				(unsigned long long)max_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 			if (blkoff > max_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 				return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 		/* ocfs2_block_group_find_clear_bits() might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 		 * return success, but we still want to return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		 * -ENOSPC unless it found the minimum number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 		 * of bits. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		if (min_bits <= res->sr_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 			search = 0; /* success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 		else if (res->sr_bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 			 * Don't show bits which we'll be returning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 			 * for allocation to the local alloc bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 			ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	return search;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) static int ocfs2_block_group_search(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 				    struct buffer_head *group_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 				    u32 bits_wanted, u32 min_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 				    u64 max_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 				    struct ocfs2_suballoc_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	int ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	u64 blkoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	BUG_ON(min_bits != 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	BUG_ON(ocfs2_is_cluster_bitmap(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	if (bg->bg_free_bits_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 							group_bh, bits_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 							le16_to_cpu(bg->bg_bits),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 							res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 		if (!ret && max_block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 			blkoff = le64_to_cpu(bg->bg_blkno) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 				res->sr_bit_offset + res->sr_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 			trace_ocfs2_block_group_search_max_block(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 				(unsigned long long)blkoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 				(unsigned long long)max_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 			if (blkoff > max_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 				ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) int ocfs2_alloc_dinode_update_counts(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 				       handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 				       struct buffer_head *di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 				       u32 num_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 				       u16 chain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	u32 tmp_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 				      OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	ocfs2_journal_dirty(handle, di_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) void ocfs2_rollback_alloc_dinode_counts(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 				       struct buffer_head *di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 				       u32 num_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 				       u16 chain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	u32 tmp_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	struct ocfs2_chain_list *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	cl = (struct ocfs2_chain_list *)&di->id2.i_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	di->id1.bitmap1.i_used = cpu_to_le32(tmp_used - num_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	le32_add_cpu(&cl->cl_recs[chain].c_free, num_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 					 struct ocfs2_extent_rec *rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 					 struct ocfs2_chain_list *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	unsigned int bpc = le16_to_cpu(cl->cl_bpc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	unsigned int bitcount = le16_to_cpu(rec->e_leaf_clusters) * bpc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	if (res->sr_bit_offset < bitoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	if (res->sr_bit_offset >= (bitoff + bitcount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	res->sr_blkno = le64_to_cpu(rec->e_blkno) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 		(res->sr_bit_offset - bitoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 					  struct ocfs2_group_desc *bg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 					  struct ocfs2_suballoc_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	u64 bg_blkno = res->sr_bg_blkno;  /* Save off */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	struct ocfs2_extent_rec *rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	struct ocfs2_chain_list *cl = &di->id2.i_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 		res->sr_blkno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 		return;
^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) 	res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	res->sr_bg_blkno = 0;  /* Clear it for contig block groups */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	    !bg->bg_list.l_next_free_rec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 		rec = &bg->bg_list.l_recs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 		if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 			res->sr_bg_blkno = bg_blkno;  /* Restore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 				  handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 				  u32 bits_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 				  u32 min_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 				  struct ocfs2_suballoc_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 				  u16 *bits_left)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	struct buffer_head *group_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	struct ocfs2_group_desc *gd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	struct inode *alloc_inode = ac->ac_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	ret = ocfs2_read_group_descriptor(alloc_inode, di,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 					  res->sr_bg_blkno, &group_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	gd = (struct ocfs2_group_desc *) group_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 				  ac->ac_max_block, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 		if (ret != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 			mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 		ocfs2_bg_discontig_fix_result(ac, gd, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	 * sr_bg_blkno might have been changed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	 * ocfs2_bg_discontig_fix_result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	res->sr_bg_stable_blkno = group_bh->b_blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	if (ac->ac_find_loc_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 		goto out_loc_only;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 					       res->sr_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 					       le16_to_cpu(gd->bg_chain));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 		mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 					 res->sr_bit_offset, res->sr_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		ocfs2_rollback_alloc_dinode_counts(alloc_inode, ac->ac_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 					       res->sr_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 					       le16_to_cpu(gd->bg_chain));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 		mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) out_loc_only:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	*bits_left = le16_to_cpu(gd->bg_free_bits_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	brelse(group_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 			      handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 			      u32 bits_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 			      u32 min_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 			      struct ocfs2_suballoc_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 			      u16 *bits_left)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	u16 chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	u64 next_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	struct inode *alloc_inode = ac->ac_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	struct buffer_head *group_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	struct buffer_head *prev_group_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	struct ocfs2_group_desc *bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	chain = ac->ac_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	trace_ocfs2_search_chain_begin(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 		(unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 		bits_wanted, chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	status = ocfs2_read_group_descriptor(alloc_inode, fe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 					     le64_to_cpu(cl->cl_recs[chain].c_blkno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 					     &group_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	bg = (struct ocfs2_group_desc *) group_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	status = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	/* for now, the chain search is a bit simplistic. We just use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	 * the 1st group with any empty bits. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	while ((status = ac->ac_group_search(alloc_inode, group_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 					     bits_wanted, min_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 					     ac->ac_max_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 					     res)) == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 		if (!bg->bg_next_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 		brelse(prev_group_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 		prev_group_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 		next_group = le64_to_cpu(bg->bg_next_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 		prev_group_bh = group_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 		group_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 		status = ocfs2_read_group_descriptor(alloc_inode, fe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 						     next_group, &group_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 			mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 			goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 		bg = (struct ocfs2_group_desc *) group_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 		if (status != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 			mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	trace_ocfs2_search_chain_succ(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 		(unsigned long long)le64_to_cpu(bg->bg_blkno), res->sr_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	BUG_ON(res->sr_bits == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	if (!status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 		ocfs2_bg_discontig_fix_result(ac, bg, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	 * sr_bg_blkno might have been changed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	 * ocfs2_bg_discontig_fix_result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	res->sr_bg_stable_blkno = group_bh->b_blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	 * Keep track of previous block descriptor read. When
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	 * we find a target, if we have read more than X
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	 * number of descriptors, and the target is reasonably
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	 * empty, relink him to top of his chain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	 * We've read 0 extra blocks and only send one more to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	 * the transaction, yet the next guy to search has a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	 * much easier time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	 * Do this *after* figuring out how many bits we're taking out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	 * of our target group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	if (!ac->ac_disable_chain_relink &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	    (prev_group_bh) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	    (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 		status = ocfs2_relink_block_group(handle, alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 						  ac->ac_bh, group_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 						  prev_group_bh, chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 		if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 			mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 			goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	if (ac->ac_find_loc_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 		goto out_loc_only;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	status = ocfs2_alloc_dinode_update_counts(alloc_inode, handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 						  ac->ac_bh, res->sr_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 						  chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	status = ocfs2_block_group_set_bits(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 					    alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 					    bg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 					    group_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 					    res->sr_bit_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 					    res->sr_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 		ocfs2_rollback_alloc_dinode_counts(alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 					ac->ac_bh, res->sr_bits, chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	trace_ocfs2_search_chain_end(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 			(unsigned long long)le64_to_cpu(fe->i_blkno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 			res->sr_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) out_loc_only:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	*bits_left = le16_to_cpu(bg->bg_free_bits_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	brelse(group_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	brelse(prev_group_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) /* will give out up to bits_wanted contiguous bits. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 				     handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 				     u32 bits_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 				     u32 min_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 				     struct ocfs2_suballoc_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	u16 victim, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	u16 bits_left = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	u64 hint = ac->ac_last_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 	struct ocfs2_chain_list *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	struct ocfs2_dinode *fe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	BUG_ON(!ac->ac_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 	/* The bh was validated by the inode read during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	 * ocfs2_reserve_suballoc_bits().  Any corruption is a code bug. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	    le32_to_cpu(fe->id1.bitmap1.i_total)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 		status = ocfs2_error(ac->ac_inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 				     "Chain allocator dinode %llu has %u used bits but only %u total\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 				     (unsigned long long)le64_to_cpu(fe->i_blkno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 				     le32_to_cpu(fe->id1.bitmap1.i_used),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 				     le32_to_cpu(fe->id1.bitmap1.i_total));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	res->sr_bg_blkno = hint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	if (res->sr_bg_blkno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 		/* Attempt to short-circuit the usual search mechanism
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 		 * by jumping straight to the most recently used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 		 * allocation group. This helps us maintain some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 		 * contiguousness across allocations. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 		status = ocfs2_search_one_group(ac, handle, bits_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 						min_bits, res, &bits_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 		if (!status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 			goto set_hint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 		if (status < 0 && status != -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 			mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 			goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 	cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	victim = ocfs2_find_victim_chain(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 	ac->ac_chain = victim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 				    res, &bits_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	if (!status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 		if (ocfs2_is_cluster_bitmap(ac->ac_inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 			hint = res->sr_bg_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 			hint = ocfs2_group_from_res(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 		goto set_hint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	if (status < 0 && status != -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	trace_ocfs2_claim_suballoc_bits(victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 	/* If we didn't pick a good victim, then just default to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 	 * searching each chain in order. Don't allow chain relinking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	 * because we only calculate enough journal credits for one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	 * relink per alloc. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	ac->ac_disable_chain_relink = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 		if (i == victim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 		if (!cl->cl_recs[i].c_free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 		ac->ac_chain = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 		status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 					    res, &bits_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 		if (!status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 			hint = ocfs2_group_from_res(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 		if (status < 0 && status != -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 			mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 			goto bail;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) set_hint:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	if (status != -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 		/* If the next search of this group is not likely to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 		 * yield a suitable extent, then we reset the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 		 * group hint so as to not waste a disk read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 		if (bits_left < min_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 			ac->ac_last_group = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 			ac->ac_last_group = hint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	return status;
^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) int ocfs2_claim_metadata(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 			 struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 			 u32 bits_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 			 u64 *suballoc_loc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 			 u16 *suballoc_bit_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 			 unsigned int *num_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 			 u64 *blkno_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 	BUG_ON(!ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 	BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	status = ocfs2_claim_suballoc_bits(ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 					   handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 					   bits_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 					   1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 					   &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 	*suballoc_loc = res.sr_bg_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	*suballoc_bit_start = res.sr_bit_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 	*blkno_start = res.sr_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	ac->ac_bits_given += res.sr_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	*num_bits = res.sr_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) static void ocfs2_init_inode_ac_group(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 				      struct buffer_head *parent_di_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 				      struct ocfs2_alloc_context *ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	 * Try to allocate inodes from some specific group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 	 * If the parent dir has recorded the last group used in allocation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	 * cool, use it. Otherwise if we try to allocate new inode from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 	 * same slot the parent dir belongs to, use the same chunk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 	 * We are very careful here to avoid the mistake of setting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	 * ac_last_group to a group descriptor from a different (unlocked) slot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 	if (OCFS2_I(dir)->ip_last_used_group &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	    OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 		ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 		if (di->i_suballoc_loc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 			ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 			ac->ac_last_group = ocfs2_which_suballoc_group(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 					le64_to_cpu(di->i_blkno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 					le16_to_cpu(di->i_suballoc_bit));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) static inline void ocfs2_save_inode_ac_group(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 					     struct ocfs2_alloc_context *ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) int ocfs2_find_new_inode_loc(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 			     struct buffer_head *parent_fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 			     struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 			     u64 *fe_blkno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 	handle_t *handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 	struct ocfs2_suballoc_result *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 	BUG_ON(!ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	BUG_ON(ac->ac_bits_given != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	BUG_ON(ac->ac_bits_wanted != 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 	BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 	res = kzalloc(sizeof(*res), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 	if (res == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 		mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 	ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 	 * The handle started here is for chain relink. Alternatively,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 	 * we could just disable relink for these calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 	handle = ocfs2_start_trans(OCFS2_SB(dir->i_sb), OCFS2_SUBALLOC_ALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 	if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 		ret = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 		handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 		mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	 * This will instruct ocfs2_claim_suballoc_bits and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	 * ocfs2_search_one_group to search but save actual allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	 * for later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	ac->ac_find_loc_only = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 	ret = ocfs2_claim_suballoc_bits(ac, handle, 1, 1, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 		mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	ac->ac_find_loc_priv = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	*fe_blkno = res->sr_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	ocfs2_update_inode_fsync_trans(handle, dir, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 		ocfs2_commit_trans(OCFS2_SB(dir->i_sb), handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 		kfree(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) int ocfs2_claim_new_inode_at_loc(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 				 struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 				 struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 				 u64 *suballoc_loc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 				 u16 *suballoc_bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 				 u64 di_blkno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	u16 chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	struct ocfs2_suballoc_result *res = ac->ac_find_loc_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	struct buffer_head *bg_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 	struct ocfs2_group_desc *bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) ac->ac_bh->b_data;
^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) 	 * Since di_blkno is being passed back in, we check for any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	 * inconsistencies which may have happened between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	 * calls. These are code bugs as di_blkno is not expected to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	 * change once returned from ocfs2_find_new_inode_loc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 	BUG_ON(res->sr_blkno != di_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	ret = ocfs2_read_group_descriptor(ac->ac_inode, di,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 					  res->sr_bg_stable_blkno, &bg_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 		mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	bg = (struct ocfs2_group_desc *) bg_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 	chain = le16_to_cpu(bg->bg_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	ret = ocfs2_alloc_dinode_update_counts(ac->ac_inode, handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 					       ac->ac_bh, res->sr_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 					       chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 		mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 	ret = ocfs2_block_group_set_bits(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 					 ac->ac_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 					 bg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 					 bg_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 					 res->sr_bit_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 					 res->sr_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 		ocfs2_rollback_alloc_dinode_counts(ac->ac_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 					       ac->ac_bh, res->sr_bits, chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 		mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 		goto out;
^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) 	trace_ocfs2_claim_new_inode_at_loc((unsigned long long)di_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 					   res->sr_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 	BUG_ON(res->sr_bits != 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 	*suballoc_loc = res->sr_bg_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 	*suballoc_bit = res->sr_bit_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 	ac->ac_bits_given++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 	ocfs2_save_inode_ac_group(dir, ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 	brelse(bg_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) int ocfs2_claim_new_inode(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 			  struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 			  struct buffer_head *parent_fe_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 			  struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 			  u64 *suballoc_loc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 			  u16 *suballoc_bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 			  u64 *fe_blkno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 	struct ocfs2_suballoc_result res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	BUG_ON(!ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 	BUG_ON(ac->ac_bits_given != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 	BUG_ON(ac->ac_bits_wanted != 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	status = ocfs2_claim_suballoc_bits(ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 					   handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 					   1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 					   1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 					   &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 	atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	BUG_ON(res.sr_bits != 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 	*suballoc_loc = res.sr_bg_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	*suballoc_bit = res.sr_bit_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 	*fe_blkno = res.sr_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	ac->ac_bits_given++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	ocfs2_save_inode_ac_group(dir, ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 	status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) /* translate a group desc. blkno and it's bitmap offset into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238)  * disk cluster offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 						   u64 bg_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 						   u16 bg_bit_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	u32 cluster = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 	if (bg_blkno != osb->first_cluster_group_blkno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 		cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 	cluster += (u32) bg_bit_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 	return cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) /* given a cluster offset, calculate which block group it belongs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255)  * and return that block offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 	u32 group_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 	group_no = cluster / osb->bitmap_cpg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 	if (!group_no)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 		return osb->first_cluster_group_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 	return ocfs2_clusters_to_blocks(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 					group_no * osb->bitmap_cpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) /* given the block number of a cluster start, calculate which cluster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271)  * group and descriptor bitmap offset that corresponds to. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) static inline void ocfs2_block_to_cluster_group(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 						u64 data_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 						u64 *bg_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 						u16 *bg_bit_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 	u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 	*bg_blkno = ocfs2_which_cluster_group(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 					      data_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 	if (*bg_blkno == osb->first_cluster_group_blkno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 		*bg_bit_off = (u16) data_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 		*bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 							     data_blkno - *bg_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293)  * min_bits - minimum contiguous chunk from this total allocation we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294)  * can handle. set to what we asked for originally for a full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295)  * contig. allocation, set to '1' to indicate we can deal with extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296)  * of any size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) int __ocfs2_claim_clusters(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 			   struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 			   u32 min_clusters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 			   u32 max_clusters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 			   u32 *cluster_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 			   u32 *num_clusters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	unsigned int bits_wanted = max_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 	struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 	struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 	BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 	       && ac->ac_which != OCFS2_AC_USE_MAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 	if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 		WARN_ON(min_clusters > 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 		status = ocfs2_claim_local_alloc_bits(osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 						      handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 						      ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 						      bits_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 						      cluster_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 						      num_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 		if (!status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 			atomic_inc(&osb->alloc_stats.local_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 		if (min_clusters > (osb->bitmap_cpg - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 			/* The only paths asking for contiguousness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 			 * should know about this already. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 			mlog(ML_ERROR, "minimum allocation requested %u exceeds "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 			     "group bitmap size %u!\n", min_clusters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 			     osb->bitmap_cpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 			status = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 			goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 		/* clamp the current request down to a realistic size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 		if (bits_wanted > (osb->bitmap_cpg - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 			bits_wanted = osb->bitmap_cpg - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 		status = ocfs2_claim_suballoc_bits(ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 						   handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 						   bits_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 						   min_clusters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 						   &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 		if (!status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 			BUG_ON(res.sr_blkno); /* cluster alloc can't set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 			*cluster_start =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 				ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 								 res.sr_bg_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 								 res.sr_bit_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 			atomic_inc(&osb->alloc_stats.bitmap_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 			*num_clusters = res.sr_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 		if (status != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 			mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 	ac->ac_bits_given += *num_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) int ocfs2_claim_clusters(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 			 struct ocfs2_alloc_context *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 			 u32 min_clusters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 			 u32 *cluster_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 			 u32 *num_clusters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 	unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 	return __ocfs2_claim_clusters(handle, ac, min_clusters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 				      bits_wanted, cluster_start, num_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) static int ocfs2_block_group_clear_bits(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 					struct inode *alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 					struct ocfs2_group_desc *bg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 					struct buffer_head *group_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 					unsigned int bit_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 					unsigned int num_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 					void (*undo_fn)(unsigned int bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 							unsigned long *bmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 	unsigned int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 	struct ocfs2_group_desc *undo_bg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 	struct journal_head *jh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 	/* The caller got this descriptor from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 	trace_ocfs2_block_group_clear_bits(bit_off, num_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 	BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 	status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 					 group_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 					 undo_fn ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 					 OCFS2_JOURNAL_ACCESS_UNDO :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 					 OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	jh = bh2jh(group_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 	if (undo_fn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 		spin_lock(&jh->b_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 		undo_bg = (struct ocfs2_group_desc *) jh->b_committed_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 		BUG_ON(!undo_bg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 	tmp = num_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 	while(tmp--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 		ocfs2_clear_bit((bit_off + tmp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 				(unsigned long *) bg->bg_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 		if (undo_fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 			undo_fn(bit_off + tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 				(unsigned long *) undo_bg->bg_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 	le16_add_cpu(&bg->bg_free_bits_count, num_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 	if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 		if (undo_fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 			spin_unlock(&jh->b_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 		return ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit count %u but claims %u are freed. num_bits %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 				   (unsigned long long)le64_to_cpu(bg->bg_blkno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 				   le16_to_cpu(bg->bg_bits),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 				   le16_to_cpu(bg->bg_free_bits_count),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 				   num_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 	if (undo_fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 		spin_unlock(&jh->b_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 	ocfs2_journal_dirty(handle, group_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447)  * expects the suballoc inode to already be locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) static int _ocfs2_free_suballoc_bits(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 				     struct inode *alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 				     struct buffer_head *alloc_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 				     unsigned int start_bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 				     u64 bg_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 				     unsigned int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 				     void (*undo_fn)(unsigned int bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 						     unsigned long *bitmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 	int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 	u32 tmp_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 	struct ocfs2_chain_list *cl = &fe->id2.i_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 	struct buffer_head *group_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 	struct ocfs2_group_desc *group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 	/* The alloc_bh comes from ocfs2_free_dinode() or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 	 * ocfs2_free_clusters().  The callers have all locked the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 	 * allocator and gotten alloc_bh from the lock call.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 	 * validates the dinode buffer.  Any corruption that has happened
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 	 * is a code bug. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 	BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 	trace_ocfs2_free_suballoc_bits(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 		(unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 		(unsigned long long)bg_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 		start_bit, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 	status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 					     &group_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	group = (struct ocfs2_group_desc *) group_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 	BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	status = ocfs2_block_group_clear_bits(handle, alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 					      group, group_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 					      start_bit, count, undo_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 					 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 		ocfs2_block_group_set_bits(handle, alloc_inode, group, group_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 				start_bit, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 	le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 		     count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 	tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 	fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	ocfs2_journal_dirty(handle, alloc_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 	brelse(group_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) int ocfs2_free_suballoc_bits(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 			     struct inode *alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 			     struct buffer_head *alloc_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 			     unsigned int start_bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 			     u64 bg_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 			     unsigned int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 					 start_bit, bg_blkno, count, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) int ocfs2_free_dinode(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 		      struct inode *inode_alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 		      struct buffer_head *inode_alloc_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 		      struct ocfs2_dinode *di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	u64 blk = le64_to_cpu(di->i_blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 	u16 bit = le16_to_cpu(di->i_suballoc_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 	u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 	if (di->i_suballoc_loc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 		bg_blkno = le64_to_cpu(di->i_suballoc_loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 	return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 					inode_alloc_bh, bit, bg_blkno, 1);
^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) static int _ocfs2_free_clusters(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 				struct inode *bitmap_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 				struct buffer_head *bitmap_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 				u64 start_blk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 				unsigned int num_clusters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 				void (*undo_fn)(unsigned int bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 						unsigned long *bitmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 	u16 bg_start_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 	u64 bg_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 	/* You can't ever have a contiguous set of clusters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 	 * bigger than a block group bitmap so we never have to worry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 	 * about looping on them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 	 * This is expensive. We can safely remove once this stuff has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 	 * gotten tested really well. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 	BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 				ocfs2_blocks_to_clusters(bitmap_inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 							 start_blk)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 	ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 				     &bg_start_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 	trace_ocfs2_free_clusters((unsigned long long)bg_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 			(unsigned long long)start_blk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 			bg_start_bit, num_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 	status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 					   bg_start_bit, bg_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 					   num_clusters, undo_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 	ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 					 num_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) int ocfs2_free_clusters(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 			struct inode *bitmap_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 			struct buffer_head *bitmap_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 			u64 start_blk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 			unsigned int num_clusters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 	return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 				    start_blk, num_clusters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 				    _ocfs2_set_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598)  * Give never-used clusters back to the global bitmap.  We don't need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599)  * to protect these bits in the undo buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) int ocfs2_release_clusters(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 			   struct inode *bitmap_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 			   struct buffer_head *bitmap_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 			   u64 start_blk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 			   unsigned int num_clusters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 	return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 				    start_blk, num_clusters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 				    _ocfs2_clear_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) }
^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)  * For a given allocation, determine which allocators will need to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614)  * accessed, and lock them, reserving the appropriate number of bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616)  * Sparse file systems call this from ocfs2_write_begin_nolock()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617)  * and ocfs2_allocate_unwritten_extents().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619)  * File systems which don't support holes call this from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620)  * ocfs2_extend_allocation().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) int ocfs2_lock_allocators(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 			  struct ocfs2_extent_tree *et,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 			  u32 clusters_to_add, u32 extents_to_split,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 			  struct ocfs2_alloc_context **data_ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 			  struct ocfs2_alloc_context **meta_ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	int ret = 0, num_free_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 	unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 	*meta_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 	if (data_ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 		*data_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 	BUG_ON(clusters_to_add != 0 && data_ac == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 	num_free_extents = ocfs2_num_free_extents(et);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 	if (num_free_extents < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 		ret = num_free_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 		mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 	 * Sparse allocation file systems need to be more conservative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 	 * with reserving room for expansion - the actual allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 	 * happens while we've got a journal handle open so re-taking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 	 * a cluster lock (because we ran out of room for another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 	 * extent) will violate ordering rules.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 	 * Most of the time we'll only be seeing this 1 cluster at a time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 	 * anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 	 * Always lock for any unwritten extents - we might want to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 	 * add blocks during a split.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 	if (!num_free_extents ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 	    (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 		ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 			if (ret != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 				mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 	if (clusters_to_add == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 	ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 		if (ret != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 			mlog_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 		if (*meta_ac) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 			ocfs2_free_alloc_context(*meta_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 			*meta_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 		 * We cannot have an error and a non null *data_ac.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694)  * Read the inode specified by blkno to get suballoc_slot and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695)  * suballoc_bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 				       u16 *suballoc_slot, u64 *group_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 				       u16 *suballoc_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 	struct buffer_head *inode_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 	struct ocfs2_dinode *inode_fe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 	trace_ocfs2_get_suballoc_slot_bit((unsigned long long)blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 	/* dirty read disk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 	status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 		mlog(ML_ERROR, "read block %llu failed %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 		     (unsigned long long)blkno, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 	inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 	if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 		mlog(ML_ERROR, "invalid inode %llu requested\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 		     (unsigned long long)blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 		status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 	if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 	    (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 		mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 		     (unsigned long long)blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 		     (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 		status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 	if (suballoc_slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 		*suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 	if (suballoc_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 		*suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 	if (group_blkno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 		*group_blkno = le64_to_cpu(inode_fe->i_suballoc_loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	brelse(inode_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748)  * test whether bit is SET in allocator bitmap or not.  on success, 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749)  * is returned and *res is 1 for SET; 0 otherwise.  when fails, errno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750)  * is returned and *res is meaningless.  Call this after you have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751)  * cluster locked against suballoc, or you may get a result based on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752)  * non-up2date contents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 				   struct inode *suballoc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 				   struct buffer_head *alloc_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 				   u64 group_blkno, u64 blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 				   u16 bit, int *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 	struct ocfs2_dinode *alloc_di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 	struct ocfs2_group_desc *group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 	struct buffer_head *group_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 	u64 bg_blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 	trace_ocfs2_test_suballoc_bit((unsigned long long)blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 				      (unsigned int)bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 	alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 	if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 		mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 		     (unsigned int)bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 		     ocfs2_bits_per_group(&alloc_di->id2.i_chain));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 		status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 	bg_blkno = group_blkno ? group_blkno :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 		   ocfs2_which_suballoc_group(blkno, bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 	status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 					     &group_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 		mlog(ML_ERROR, "read group %llu failed %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 		     (unsigned long long)bg_blkno, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 	group = (struct ocfs2_group_desc *) group_bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 	*res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 	brelse(group_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800)  * Test if the bit representing this inode (blkno) is set in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801)  * suballocator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803)  * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805)  * In the event of failure, a negative value is returned and *res is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806)  * meaningless.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808)  * Callers must make sure to hold nfs_sync_lock to prevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809)  * ocfs2_delete_inode() on another node from accessing the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810)  * suballocator concurrently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	u64 group_blkno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 	u16 suballoc_bit = 0, suballoc_slot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 	struct inode *inode_alloc_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 	struct buffer_head *alloc_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 	trace_ocfs2_test_inode_bit((unsigned long long)blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 	status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 					     &group_blkno, &suballoc_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 		mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 	if (suballoc_slot == (u16)OCFS2_INVALID_SLOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 		inode_alloc_inode = ocfs2_get_system_file_inode(osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 			GLOBAL_INODE_ALLOC_SYSTEM_INODE, suballoc_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 		inode_alloc_inode = ocfs2_get_system_file_inode(osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 			INODE_ALLOC_SYSTEM_INODE, suballoc_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 	if (!inode_alloc_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 		/* the error code could be inaccurate, but we are not able to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 		 * get the correct one. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 		status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 		mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 		     (u32)suballoc_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 	inode_lock(inode_alloc_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 	status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 		inode_unlock(inode_alloc_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 		iput(inode_alloc_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 		mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 		     (u32)suballoc_slot, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 	status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 					 group_blkno, blkno, suballoc_bit, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 		mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 	ocfs2_inode_unlock(inode_alloc_inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 	inode_unlock(inode_alloc_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 	iput(inode_alloc_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 	brelse(alloc_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) }