xref: /openbmc/linux/fs/ocfs2/suballoc.c (revision dd7b5f9d01ff4af8c6a55cf029032868bc12a474)
1ccd979bdSMark Fasheh /* -*- mode: c; c-basic-offset: 8; -*-
2ccd979bdSMark Fasheh  * vim: noexpandtab sw=8 ts=8 sts=0:
3ccd979bdSMark Fasheh  *
4ccd979bdSMark Fasheh  * suballoc.c
5ccd979bdSMark Fasheh  *
6ccd979bdSMark Fasheh  * metadata alloc and free
7ccd979bdSMark Fasheh  * Inspired by ext3 block groups.
8ccd979bdSMark Fasheh  *
9ccd979bdSMark Fasheh  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
10ccd979bdSMark Fasheh  *
11ccd979bdSMark Fasheh  * This program is free software; you can redistribute it and/or
12ccd979bdSMark Fasheh  * modify it under the terms of the GNU General Public
13ccd979bdSMark Fasheh  * License as published by the Free Software Foundation; either
14ccd979bdSMark Fasheh  * version 2 of the License, or (at your option) any later version.
15ccd979bdSMark Fasheh  *
16ccd979bdSMark Fasheh  * This program is distributed in the hope that it will be useful,
17ccd979bdSMark Fasheh  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18ccd979bdSMark Fasheh  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19ccd979bdSMark Fasheh  * General Public License for more details.
20ccd979bdSMark Fasheh  *
21ccd979bdSMark Fasheh  * You should have received a copy of the GNU General Public
22ccd979bdSMark Fasheh  * License along with this program; if not, write to the
23ccd979bdSMark Fasheh  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24ccd979bdSMark Fasheh  * Boston, MA 021110-1307, USA.
25ccd979bdSMark Fasheh  */
26ccd979bdSMark Fasheh 
27ccd979bdSMark Fasheh #include <linux/fs.h>
28ccd979bdSMark Fasheh #include <linux/types.h>
29ccd979bdSMark Fasheh #include <linux/slab.h>
30ccd979bdSMark Fasheh #include <linux/highmem.h>
31ccd979bdSMark Fasheh 
32ccd979bdSMark Fasheh #include <cluster/masklog.h>
33ccd979bdSMark Fasheh 
34ccd979bdSMark Fasheh #include "ocfs2.h"
35ccd979bdSMark Fasheh 
36ccd979bdSMark Fasheh #include "alloc.h"
37d6b32bbbSJoel Becker #include "blockcheck.h"
38ccd979bdSMark Fasheh #include "dlmglue.h"
39ccd979bdSMark Fasheh #include "inode.h"
40ccd979bdSMark Fasheh #include "journal.h"
41ccd979bdSMark Fasheh #include "localalloc.h"
42ccd979bdSMark Fasheh #include "suballoc.h"
43ccd979bdSMark Fasheh #include "super.h"
44ccd979bdSMark Fasheh #include "sysfile.h"
45ccd979bdSMark Fasheh #include "uptodate.h"
462f73e135STao Ma #include "ocfs2_trace.h"
47ccd979bdSMark Fasheh 
48ccd979bdSMark Fasheh #include "buffer_head_io.h"
49ccd979bdSMark Fasheh 
50ffda89a3STao Ma #define NOT_ALLOC_NEW_GROUP		0
5160ca81e8STao Ma #define ALLOC_NEW_GROUP			0x1
5260ca81e8STao Ma #define ALLOC_GROUPS_FROM_GLOBAL	0x2
53ffda89a3STao Ma 
54b89c5428STiger Yang #define OCFS2_MAX_TO_STEAL		1024
554d0ddb2cSTao Ma 
567d1fe093SJoel Becker struct ocfs2_suballoc_result {
572b6cb576SJoel Becker 	u64		sr_bg_blkno;	/* The bg we allocated from.  Set
582b6cb576SJoel Becker 					   to 0 when a block group is
592b6cb576SJoel Becker 					   contiguous. */
60e49e2767SMark Fasheh 	u64		sr_bg_stable_blkno; /*
61e49e2767SMark Fasheh 					     * Doesn't change, always
62e49e2767SMark Fasheh 					     * set to target block
63e49e2767SMark Fasheh 					     * group descriptor
64e49e2767SMark Fasheh 					     * block.
65e49e2767SMark Fasheh 					     */
66ba206635SJoel Becker 	u64		sr_blkno;	/* The first allocated block */
677d1fe093SJoel Becker 	unsigned int	sr_bit_offset;	/* The bit in the bg */
687d1fe093SJoel Becker 	unsigned int	sr_bits;	/* How many bits we claimed */
697d1fe093SJoel Becker };
707d1fe093SJoel Becker 
71b2b6ebf5SMark Fasheh static u64 ocfs2_group_from_res(struct ocfs2_suballoc_result *res)
72b2b6ebf5SMark Fasheh {
73b2b6ebf5SMark Fasheh 	if (res->sr_blkno == 0)
74b2b6ebf5SMark Fasheh 		return 0;
75b2b6ebf5SMark Fasheh 
76b2b6ebf5SMark Fasheh 	if (res->sr_bg_blkno)
77b2b6ebf5SMark Fasheh 		return res->sr_bg_blkno;
78b2b6ebf5SMark Fasheh 
79b2b6ebf5SMark Fasheh 	return ocfs2_which_suballoc_group(res->sr_blkno, res->sr_bit_offset);
80b2b6ebf5SMark Fasheh }
81b2b6ebf5SMark Fasheh 
82ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
83ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
84ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
851fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle,
86ccd979bdSMark Fasheh 				  struct inode *alloc_inode,
87ccd979bdSMark Fasheh 				  struct buffer_head *bg_bh,
88ccd979bdSMark Fasheh 				  u64 group_blkno,
89798db35fSJoel Becker 				  unsigned int group_clusters,
90ccd979bdSMark Fasheh 				  u16 my_chain,
91ccd979bdSMark Fasheh 				  struct ocfs2_chain_list *cl);
92ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
93ccd979bdSMark Fasheh 				   struct inode *alloc_inode,
941187c968SJoel Becker 				   struct buffer_head *bh,
9560ca81e8STao Ma 				   u64 max_block,
96feb473a6STao Ma 				   u64 *last_alloc_group,
9760ca81e8STao Ma 				   int flags);
98ccd979bdSMark Fasheh 
99ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode,
100ccd979bdSMark Fasheh 				      struct buffer_head *group_bh,
101ccd979bdSMark Fasheh 				      u32 bits_wanted, u32 min_bits,
1021187c968SJoel Becker 				      u64 max_block,
1037d1fe093SJoel Becker 				      struct ocfs2_suballoc_result *res);
104ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode,
105ccd979bdSMark Fasheh 				    struct buffer_head *group_bh,
106ccd979bdSMark Fasheh 				    u32 bits_wanted, u32 min_bits,
1071187c968SJoel Becker 				    u64 max_block,
1087d1fe093SJoel Becker 				    struct ocfs2_suballoc_result *res);
109aa8f8e93SJoel Becker static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
1101fabe148SMark Fasheh 				     handle_t *handle,
111ccd979bdSMark Fasheh 				     u32 bits_wanted,
112ccd979bdSMark Fasheh 				     u32 min_bits,
1137d1fe093SJoel Becker 				     struct ocfs2_suballoc_result *res);
114ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
115ccd979bdSMark Fasheh 					 int nr);
1161fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle,
117ccd979bdSMark Fasheh 				    struct inode *alloc_inode,
118ccd979bdSMark Fasheh 				    struct buffer_head *fe_bh,
119ccd979bdSMark Fasheh 				    struct buffer_head *bg_bh,
120ccd979bdSMark Fasheh 				    struct buffer_head *prev_bg_bh,
121ccd979bdSMark Fasheh 				    u16 chain);
122ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
123ccd979bdSMark Fasheh 						     u32 wanted);
124ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
125ccd979bdSMark Fasheh 						   u64 bg_blkno,
126ccd979bdSMark Fasheh 						   u16 bg_bit_off);
127ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode,
128ccd979bdSMark Fasheh 						u64 data_blkno,
129ccd979bdSMark Fasheh 						u64 *bg_blkno,
130ccd979bdSMark Fasheh 						u16 *bg_bit_off);
1311187c968SJoel Becker static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
1321187c968SJoel Becker 					     u32 bits_wanted, u64 max_block,
13360ca81e8STao Ma 					     int flags,
1341187c968SJoel Becker 					     struct ocfs2_alloc_context **ac);
135ccd979bdSMark Fasheh 
1369c7af40bSMark Fasheh void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
137ccd979bdSMark Fasheh {
138da5cbf2fSMark Fasheh 	struct inode *inode = ac->ac_inode;
139da5cbf2fSMark Fasheh 
140da5cbf2fSMark Fasheh 	if (inode) {
141da5cbf2fSMark Fasheh 		if (ac->ac_which != OCFS2_AC_USE_LOCAL)
142e63aecb6SMark Fasheh 			ocfs2_inode_unlock(inode, 1);
143da5cbf2fSMark Fasheh 
1445955102cSAl Viro 		inode_unlock(inode);
145da5cbf2fSMark Fasheh 
146da5cbf2fSMark Fasheh 		iput(inode);
1474d0ddb2cSTao Ma 		ac->ac_inode = NULL;
148da5cbf2fSMark Fasheh 	}
149ccd979bdSMark Fasheh 	brelse(ac->ac_bh);
1504d0ddb2cSTao Ma 	ac->ac_bh = NULL;
151e3b4a97dSMark Fasheh 	ac->ac_resv = NULL;
152e49e2767SMark Fasheh 	kfree(ac->ac_find_loc_priv);
153e49e2767SMark Fasheh 	ac->ac_find_loc_priv = NULL;
154e49e2767SMark Fasheh }
1554d0ddb2cSTao Ma 
1564d0ddb2cSTao Ma void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
1574d0ddb2cSTao Ma {
1584d0ddb2cSTao Ma 	ocfs2_free_ac_resource(ac);
159ccd979bdSMark Fasheh 	kfree(ac);
160ccd979bdSMark Fasheh }
161ccd979bdSMark Fasheh 
162ccd979bdSMark Fasheh static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
163ccd979bdSMark Fasheh {
164ccd979bdSMark Fasheh 	return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
165ccd979bdSMark Fasheh }
166ccd979bdSMark Fasheh 
16757e3e797SJoel Becker #define do_error(fmt, ...)						\
16857e3e797SJoel Becker do {									\
16978c37eb0STao Ma 	if (resize)							\
1707ecef14aSJoe Perches 		mlog(ML_ERROR, fmt, ##__VA_ARGS__);			\
17157e3e797SJoel Becker 	else								\
17217a5b9abSGoldwyn Rodrigues 		return ocfs2_error(sb, fmt, ##__VA_ARGS__);		\
17357e3e797SJoel Becker } while (0)
17457e3e797SJoel Becker 
175970e4936SJoel Becker static int ocfs2_validate_gd_self(struct super_block *sb,
176970e4936SJoel Becker 				  struct buffer_head *bh,
17778c37eb0STao Ma 				  int resize)
178970e4936SJoel Becker {
179970e4936SJoel Becker 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
180970e4936SJoel Becker 
1817bf72edeSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1827ecef14aSJoe Perches 		do_error("Group descriptor #%llu has bad signature %.*s\n",
18368f64d47SJoel Becker 			 (unsigned long long)bh->b_blocknr, 7,
18457e3e797SJoel Becker 			 gd->bg_signature);
1857bf72edeSMark Fasheh 	}
1867bf72edeSMark Fasheh 
18768f64d47SJoel Becker 	if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
1887ecef14aSJoe Perches 		do_error("Group descriptor #%llu has an invalid bg_blkno of %llu\n",
18968f64d47SJoel Becker 			 (unsigned long long)bh->b_blocknr,
19068f64d47SJoel Becker 			 (unsigned long long)le64_to_cpu(gd->bg_blkno));
19168f64d47SJoel Becker 	}
19268f64d47SJoel Becker 
19368f64d47SJoel Becker 	if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
1947ecef14aSJoe Perches 		do_error("Group descriptor #%llu has an invalid fs_generation of #%u\n",
19568f64d47SJoel Becker 			 (unsigned long long)bh->b_blocknr,
19668f64d47SJoel Becker 			 le32_to_cpu(gd->bg_generation));
19768f64d47SJoel Becker 	}
19868f64d47SJoel Becker 
199970e4936SJoel Becker 	if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
2007ecef14aSJoe Perches 		do_error("Group descriptor #%llu has bit count %u but claims that %u are free\n",
201970e4936SJoel Becker 			 (unsigned long long)bh->b_blocknr,
202970e4936SJoel Becker 			 le16_to_cpu(gd->bg_bits),
203970e4936SJoel Becker 			 le16_to_cpu(gd->bg_free_bits_count));
204970e4936SJoel Becker 	}
205970e4936SJoel Becker 
206970e4936SJoel Becker 	if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
2077ecef14aSJoe Perches 		do_error("Group descriptor #%llu has bit count %u but max bitmap bits of %u\n",
208970e4936SJoel Becker 			 (unsigned long long)bh->b_blocknr,
209970e4936SJoel Becker 			 le16_to_cpu(gd->bg_bits),
210970e4936SJoel Becker 			 8 * le16_to_cpu(gd->bg_size));
211970e4936SJoel Becker 	}
212970e4936SJoel Becker 
213970e4936SJoel Becker 	return 0;
214970e4936SJoel Becker }
215970e4936SJoel Becker 
216970e4936SJoel Becker static int ocfs2_validate_gd_parent(struct super_block *sb,
217970e4936SJoel Becker 				    struct ocfs2_dinode *di,
218970e4936SJoel Becker 				    struct buffer_head *bh,
21978c37eb0STao Ma 				    int resize)
220970e4936SJoel Becker {
221970e4936SJoel Becker 	unsigned int max_bits;
222970e4936SJoel Becker 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
223970e4936SJoel Becker 
2247bf72edeSMark Fasheh 	if (di->i_blkno != gd->bg_parent_dinode) {
2257ecef14aSJoe Perches 		do_error("Group descriptor #%llu has bad parent pointer (%llu, expected %llu)\n",
22668f64d47SJoel Becker 			 (unsigned long long)bh->b_blocknr,
2277bf72edeSMark Fasheh 			 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
2287bf72edeSMark Fasheh 			 (unsigned long long)le64_to_cpu(di->i_blkno));
2297bf72edeSMark Fasheh 	}
2307bf72edeSMark Fasheh 
2317bf72edeSMark Fasheh 	max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
2327bf72edeSMark Fasheh 	if (le16_to_cpu(gd->bg_bits) > max_bits) {
2337ecef14aSJoe Perches 		do_error("Group descriptor #%llu has bit count of %u\n",
23468f64d47SJoel Becker 			 (unsigned long long)bh->b_blocknr,
2357bf72edeSMark Fasheh 			 le16_to_cpu(gd->bg_bits));
2367bf72edeSMark Fasheh 	}
2377bf72edeSMark Fasheh 
23878c37eb0STao Ma 	/* In resize, we may meet the case bg_chain == cl_next_free_rec. */
23978c37eb0STao Ma 	if ((le16_to_cpu(gd->bg_chain) >
24078c37eb0STao Ma 	     le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
24178c37eb0STao Ma 	    ((le16_to_cpu(gd->bg_chain) ==
24278c37eb0STao Ma 	     le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
2437ecef14aSJoe Perches 		do_error("Group descriptor #%llu has bad chain %u\n",
24468f64d47SJoel Becker 			 (unsigned long long)bh->b_blocknr,
2457bf72edeSMark Fasheh 			 le16_to_cpu(gd->bg_chain));
2467bf72edeSMark Fasheh 	}
2477bf72edeSMark Fasheh 
248970e4936SJoel Becker 	return 0;
2497bf72edeSMark Fasheh }
2507bf72edeSMark Fasheh 
25157e3e797SJoel Becker #undef do_error
2527bf72edeSMark Fasheh 
253970e4936SJoel Becker /*
254970e4936SJoel Becker  * This version only prints errors.  It does not fail the filesystem, and
255970e4936SJoel Becker  * exists only for resize.
256970e4936SJoel Becker  */
257970e4936SJoel Becker int ocfs2_check_group_descriptor(struct super_block *sb,
258970e4936SJoel Becker 				 struct ocfs2_dinode *di,
259970e4936SJoel Becker 				 struct buffer_head *bh)
260970e4936SJoel Becker {
261970e4936SJoel Becker 	int rc;
262d6b32bbbSJoel Becker 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
263970e4936SJoel Becker 
264d6b32bbbSJoel Becker 	BUG_ON(!buffer_uptodate(bh));
265d6b32bbbSJoel Becker 
266d6b32bbbSJoel Becker 	/*
267d6b32bbbSJoel Becker 	 * If the ecc fails, we return the error but otherwise
268d6b32bbbSJoel Becker 	 * leave the filesystem running.  We know any error is
269d6b32bbbSJoel Becker 	 * local to this block.
270d6b32bbbSJoel Becker 	 */
271d6b32bbbSJoel Becker 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
27213723d00SJoel Becker 	if (rc) {
27313723d00SJoel Becker 		mlog(ML_ERROR,
27413723d00SJoel Becker 		     "Checksum failed for group descriptor %llu\n",
27513723d00SJoel Becker 		     (unsigned long long)bh->b_blocknr);
27613723d00SJoel Becker 	} else
277970e4936SJoel Becker 		rc = ocfs2_validate_gd_self(sb, bh, 1);
278970e4936SJoel Becker 	if (!rc)
279970e4936SJoel Becker 		rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
280970e4936SJoel Becker 
281970e4936SJoel Becker 	return rc;
282970e4936SJoel Becker }
283970e4936SJoel Becker 
284970e4936SJoel Becker static int ocfs2_validate_group_descriptor(struct super_block *sb,
285970e4936SJoel Becker 					   struct buffer_head *bh)
286970e4936SJoel Becker {
287d6b32bbbSJoel Becker 	int rc;
288d6b32bbbSJoel Becker 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
289d6b32bbbSJoel Becker 
2902f73e135STao Ma 	trace_ocfs2_validate_group_descriptor(
291970e4936SJoel Becker 					(unsigned long long)bh->b_blocknr);
292970e4936SJoel Becker 
293d6b32bbbSJoel Becker 	BUG_ON(!buffer_uptodate(bh));
294d6b32bbbSJoel Becker 
295d6b32bbbSJoel Becker 	/*
296d6b32bbbSJoel Becker 	 * If the ecc fails, we return the error but otherwise
297d6b32bbbSJoel Becker 	 * leave the filesystem running.  We know any error is
298d6b32bbbSJoel Becker 	 * local to this block.
299d6b32bbbSJoel Becker 	 */
300d6b32bbbSJoel Becker 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
301d6b32bbbSJoel Becker 	if (rc)
302d6b32bbbSJoel Becker 		return rc;
303d6b32bbbSJoel Becker 
304d6b32bbbSJoel Becker 	/*
305d6b32bbbSJoel Becker 	 * Errors after here are fatal.
306d6b32bbbSJoel Becker 	 */
307d6b32bbbSJoel Becker 
308970e4936SJoel Becker 	return ocfs2_validate_gd_self(sb, bh, 0);
3097bf72edeSMark Fasheh }
3107bf72edeSMark Fasheh 
31168f64d47SJoel Becker int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
31268f64d47SJoel Becker 				u64 gd_blkno, struct buffer_head **bh)
31368f64d47SJoel Becker {
31468f64d47SJoel Becker 	int rc;
31568f64d47SJoel Becker 	struct buffer_head *tmp = *bh;
31668f64d47SJoel Becker 
3178cb471e8SJoel Becker 	rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
318970e4936SJoel Becker 			      ocfs2_validate_group_descriptor);
31968f64d47SJoel Becker 	if (rc)
32068f64d47SJoel Becker 		goto out;
32168f64d47SJoel Becker 
322970e4936SJoel Becker 	rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
32368f64d47SJoel Becker 	if (rc) {
32468f64d47SJoel Becker 		brelse(tmp);
32568f64d47SJoel Becker 		goto out;
32668f64d47SJoel Becker 	}
32768f64d47SJoel Becker 
32868f64d47SJoel Becker 	/* If ocfs2_read_block() got us a new bh, pass it up. */
32968f64d47SJoel Becker 	if (!*bh)
33068f64d47SJoel Becker 		*bh = tmp;
33168f64d47SJoel Becker 
33268f64d47SJoel Becker out:
33368f64d47SJoel Becker 	return rc;
33468f64d47SJoel Becker }
33568f64d47SJoel Becker 
336798db35fSJoel Becker static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
337798db35fSJoel Becker 					  struct ocfs2_group_desc *bg,
338798db35fSJoel Becker 					  struct ocfs2_chain_list *cl,
33947dea423STao Ma 					  u64 p_blkno, unsigned int clusters)
340798db35fSJoel Becker {
341798db35fSJoel Becker 	struct ocfs2_extent_list *el = &bg->bg_list;
342798db35fSJoel Becker 	struct ocfs2_extent_rec *rec;
343798db35fSJoel Becker 
3444711954eSTao Ma 	BUG_ON(!ocfs2_supports_discontig_bg(osb));
345798db35fSJoel Becker 	if (!el->l_next_free_rec)
346798db35fSJoel Becker 		el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
347798db35fSJoel Becker 	rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
3484711954eSTao Ma 	rec->e_blkno = cpu_to_le64(p_blkno);
349798db35fSJoel Becker 	rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
350798db35fSJoel Becker 				  le16_to_cpu(cl->cl_bpc));
35147dea423STao Ma 	rec->e_leaf_clusters = cpu_to_le16(clusters);
352798db35fSJoel Becker 	le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
3534711954eSTao Ma 	le16_add_cpu(&bg->bg_free_bits_count,
3544711954eSTao Ma 		     clusters * le16_to_cpu(cl->cl_bpc));
355798db35fSJoel Becker 	le16_add_cpu(&el->l_next_free_rec, 1);
356798db35fSJoel Becker }
357798db35fSJoel Becker 
3581fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle,
359ccd979bdSMark Fasheh 				  struct inode *alloc_inode,
360ccd979bdSMark Fasheh 				  struct buffer_head *bg_bh,
361ccd979bdSMark Fasheh 				  u64 group_blkno,
362798db35fSJoel Becker 				  unsigned int group_clusters,
363ccd979bdSMark Fasheh 				  u16 my_chain,
364ccd979bdSMark Fasheh 				  struct ocfs2_chain_list *cl)
365ccd979bdSMark Fasheh {
366ccd979bdSMark Fasheh 	int status = 0;
367798db35fSJoel Becker 	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
368ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
369ccd979bdSMark Fasheh 	struct super_block * sb = alloc_inode->i_sb;
370ccd979bdSMark Fasheh 
371ccd979bdSMark Fasheh 	if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
3727ecef14aSJoe Perches 		status = ocfs2_error(alloc_inode->i_sb,
3737ecef14aSJoe Perches 				     "group block (%llu) != b_blocknr (%llu)\n",
374b0697053SMark Fasheh 				     (unsigned long long)group_blkno,
375ccd979bdSMark Fasheh 				     (unsigned long long) bg_bh->b_blocknr);
376ccd979bdSMark Fasheh 		goto bail;
377ccd979bdSMark Fasheh 	}
378ccd979bdSMark Fasheh 
37913723d00SJoel Becker 	status = ocfs2_journal_access_gd(handle,
3800cf2f763SJoel Becker 					 INODE_CACHE(alloc_inode),
381ccd979bdSMark Fasheh 					 bg_bh,
382ccd979bdSMark Fasheh 					 OCFS2_JOURNAL_ACCESS_CREATE);
383ccd979bdSMark Fasheh 	if (status < 0) {
384ccd979bdSMark Fasheh 		mlog_errno(status);
385ccd979bdSMark Fasheh 		goto bail;
386ccd979bdSMark Fasheh 	}
387ccd979bdSMark Fasheh 
388ccd979bdSMark Fasheh 	memset(bg, 0, sb->s_blocksize);
389ccd979bdSMark Fasheh 	strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
390ccd979bdSMark Fasheh 	bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
3918571882cSTao Ma 	bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1,
3928571882cSTao Ma 						osb->s_feature_incompat));
393ccd979bdSMark Fasheh 	bg->bg_chain = cpu_to_le16(my_chain);
394ccd979bdSMark Fasheh 	bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
395ccd979bdSMark Fasheh 	bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
396ccd979bdSMark Fasheh 	bg->bg_blkno = cpu_to_le64(group_blkno);
397798db35fSJoel Becker 	if (group_clusters == le16_to_cpu(cl->cl_cpg))
398798db35fSJoel Becker 		bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
399798db35fSJoel Becker 	else
4004711954eSTao Ma 		ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno,
401798db35fSJoel Becker 					      group_clusters);
402798db35fSJoel Becker 
403ccd979bdSMark Fasheh 	/* set the 1st bit in the bitmap to account for the descriptor block */
404ccd979bdSMark Fasheh 	ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
405ccd979bdSMark Fasheh 	bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
406ccd979bdSMark Fasheh 
407ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, bg_bh);
408ccd979bdSMark Fasheh 
409ccd979bdSMark Fasheh 	/* There is no need to zero out or otherwise initialize the
410ccd979bdSMark Fasheh 	 * other blocks in a group - All valid FS metadata in a block
411ccd979bdSMark Fasheh 	 * group stores the superblock fs_generation value at
412ccd979bdSMark Fasheh 	 * allocation time. */
413ccd979bdSMark Fasheh 
414ccd979bdSMark Fasheh bail:
415c1e8d35eSTao Ma 	if (status)
416c1e8d35eSTao Ma 		mlog_errno(status);
417ccd979bdSMark Fasheh 	return status;
418ccd979bdSMark Fasheh }
419ccd979bdSMark Fasheh 
420ccd979bdSMark Fasheh static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
421ccd979bdSMark Fasheh {
422ccd979bdSMark Fasheh 	u16 curr, best;
423ccd979bdSMark Fasheh 
424ccd979bdSMark Fasheh 	best = curr = 0;
425ccd979bdSMark Fasheh 	while (curr < le16_to_cpu(cl->cl_count)) {
426ccd979bdSMark Fasheh 		if (le32_to_cpu(cl->cl_recs[best].c_total) >
427ccd979bdSMark Fasheh 		    le32_to_cpu(cl->cl_recs[curr].c_total))
428ccd979bdSMark Fasheh 			best = curr;
429ccd979bdSMark Fasheh 		curr++;
430ccd979bdSMark Fasheh 	}
431ccd979bdSMark Fasheh 	return best;
432ccd979bdSMark Fasheh }
433ccd979bdSMark Fasheh 
434798db35fSJoel Becker static struct buffer_head *
435798db35fSJoel Becker ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
436798db35fSJoel Becker 			       struct inode *alloc_inode,
437798db35fSJoel Becker 			       struct ocfs2_alloc_context *ac,
438798db35fSJoel Becker 			       struct ocfs2_chain_list *cl)
439798db35fSJoel Becker {
440798db35fSJoel Becker 	int status;
441798db35fSJoel Becker 	u32 bit_off, num_bits;
442798db35fSJoel Becker 	u64 bg_blkno;
443798db35fSJoel Becker 	struct buffer_head *bg_bh;
444798db35fSJoel Becker 	unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
445798db35fSJoel Becker 
4461ed9b777SJoel Becker 	status = ocfs2_claim_clusters(handle, ac,
447798db35fSJoel Becker 				      le16_to_cpu(cl->cl_cpg), &bit_off,
448798db35fSJoel Becker 				      &num_bits);
449798db35fSJoel Becker 	if (status < 0) {
450798db35fSJoel Becker 		if (status != -ENOSPC)
451798db35fSJoel Becker 			mlog_errno(status);
452798db35fSJoel Becker 		goto bail;
453798db35fSJoel Becker 	}
454798db35fSJoel Becker 
455798db35fSJoel Becker 	/* setup the group */
456798db35fSJoel Becker 	bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4572f73e135STao Ma 	trace_ocfs2_block_group_alloc_contig(
4582f73e135STao Ma 	     (unsigned long long)bg_blkno, alloc_rec);
459798db35fSJoel Becker 
460798db35fSJoel Becker 	bg_bh = sb_getblk(osb->sb, bg_blkno);
461798db35fSJoel Becker 	if (!bg_bh) {
4627391a294SRui Xiang 		status = -ENOMEM;
463798db35fSJoel Becker 		mlog_errno(status);
464798db35fSJoel Becker 		goto bail;
465798db35fSJoel Becker 	}
466798db35fSJoel Becker 	ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
467798db35fSJoel Becker 
468798db35fSJoel Becker 	status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
469798db35fSJoel Becker 					bg_blkno, num_bits, alloc_rec, cl);
470798db35fSJoel Becker 	if (status < 0) {
471798db35fSJoel Becker 		brelse(bg_bh);
472798db35fSJoel Becker 		mlog_errno(status);
473798db35fSJoel Becker 	}
474798db35fSJoel Becker 
475798db35fSJoel Becker bail:
476798db35fSJoel Becker 	return status ? ERR_PTR(status) : bg_bh;
477798db35fSJoel Becker }
478798db35fSJoel Becker 
479798db35fSJoel Becker static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
480798db35fSJoel Becker 					handle_t *handle,
481798db35fSJoel Becker 					struct ocfs2_alloc_context *ac,
482798db35fSJoel Becker 					unsigned int min_bits,
483798db35fSJoel Becker 					u32 *bit_off, u32 *num_bits)
484798db35fSJoel Becker {
48518d3a98fSJoel Becker 	int status = 0;
486798db35fSJoel Becker 
487798db35fSJoel Becker 	while (min_bits) {
4881ed9b777SJoel Becker 		status = ocfs2_claim_clusters(handle, ac, min_bits,
489798db35fSJoel Becker 					      bit_off, num_bits);
490798db35fSJoel Becker 		if (status != -ENOSPC)
491798db35fSJoel Becker 			break;
492798db35fSJoel Becker 
493798db35fSJoel Becker 		min_bits >>= 1;
494798db35fSJoel Becker 	}
495798db35fSJoel Becker 
496798db35fSJoel Becker 	return status;
497798db35fSJoel Becker }
498798db35fSJoel Becker 
499798db35fSJoel Becker static int ocfs2_block_group_grow_discontig(handle_t *handle,
500798db35fSJoel Becker 					    struct inode *alloc_inode,
501798db35fSJoel Becker 					    struct buffer_head *bg_bh,
502798db35fSJoel Becker 					    struct ocfs2_alloc_context *ac,
503798db35fSJoel Becker 					    struct ocfs2_chain_list *cl,
504798db35fSJoel Becker 					    unsigned int min_bits)
505798db35fSJoel Becker {
506798db35fSJoel Becker 	int status;
507798db35fSJoel Becker 	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
508798db35fSJoel Becker 	struct ocfs2_group_desc *bg =
509798db35fSJoel Becker 		(struct ocfs2_group_desc *)bg_bh->b_data;
5104711954eSTao Ma 	unsigned int needed = le16_to_cpu(cl->cl_cpg) -
5114711954eSTao Ma 			 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
512798db35fSJoel Becker 	u32 p_cpos, clusters;
513798db35fSJoel Becker 	u64 p_blkno;
514798db35fSJoel Becker 	struct ocfs2_extent_list *el = &bg->bg_list;
515798db35fSJoel Becker 
516798db35fSJoel Becker 	status = ocfs2_journal_access_gd(handle,
517798db35fSJoel Becker 					 INODE_CACHE(alloc_inode),
518798db35fSJoel Becker 					 bg_bh,
519798db35fSJoel Becker 					 OCFS2_JOURNAL_ACCESS_CREATE);
520798db35fSJoel Becker 	if (status < 0) {
521798db35fSJoel Becker 		mlog_errno(status);
522798db35fSJoel Becker 		goto bail;
523798db35fSJoel Becker 	}
524798db35fSJoel Becker 
525798db35fSJoel Becker 	while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
526798db35fSJoel Becker 				le16_to_cpu(el->l_count))) {
527798db35fSJoel Becker 		if (min_bits > needed)
528798db35fSJoel Becker 			min_bits = needed;
529798db35fSJoel Becker 		status = ocfs2_block_group_claim_bits(osb, handle, ac,
530798db35fSJoel Becker 						      min_bits, &p_cpos,
531798db35fSJoel Becker 						      &clusters);
532798db35fSJoel Becker 		if (status < 0) {
533798db35fSJoel Becker 			if (status != -ENOSPC)
534798db35fSJoel Becker 				mlog_errno(status);
535798db35fSJoel Becker 			goto bail;
536798db35fSJoel Becker 		}
537798db35fSJoel Becker 		p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
538798db35fSJoel Becker 		ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
539798db35fSJoel Becker 					      clusters);
540798db35fSJoel Becker 
541798db35fSJoel Becker 		min_bits = clusters;
5424711954eSTao Ma 		needed = le16_to_cpu(cl->cl_cpg) -
5434711954eSTao Ma 			 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
544798db35fSJoel Becker 	}
545798db35fSJoel Becker 
546798db35fSJoel Becker 	if (needed > 0) {
5474711954eSTao Ma 		/*
5484711954eSTao Ma 		 * We have used up all the extent rec but can't fill up
5494711954eSTao Ma 		 * the cpg. So bail out.
5504711954eSTao Ma 		 */
5514711954eSTao Ma 		status = -ENOSPC;
5524711954eSTao Ma 		goto bail;
553798db35fSJoel Becker 	}
554798db35fSJoel Becker 
555798db35fSJoel Becker 	ocfs2_journal_dirty(handle, bg_bh);
556798db35fSJoel Becker 
557798db35fSJoel Becker bail:
558798db35fSJoel Becker 	return status;
559798db35fSJoel Becker }
560798db35fSJoel Becker 
5618b06bc59SJoel Becker static void ocfs2_bg_alloc_cleanup(handle_t *handle,
5628b06bc59SJoel Becker 				   struct ocfs2_alloc_context *cluster_ac,
5638b06bc59SJoel Becker 				   struct inode *alloc_inode,
5648b06bc59SJoel Becker 				   struct buffer_head *bg_bh)
565798db35fSJoel Becker {
5668b06bc59SJoel Becker 	int i, ret;
567798db35fSJoel Becker 	struct ocfs2_group_desc *bg;
568798db35fSJoel Becker 	struct ocfs2_extent_list *el;
569798db35fSJoel Becker 	struct ocfs2_extent_rec *rec;
570798db35fSJoel Becker 
571798db35fSJoel Becker 	if (!bg_bh)
572798db35fSJoel Becker 		return;
573798db35fSJoel Becker 
574798db35fSJoel Becker 	bg = (struct ocfs2_group_desc *)bg_bh->b_data;
575798db35fSJoel Becker 	el = &bg->bg_list;
576798db35fSJoel Becker 	for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
577798db35fSJoel Becker 		rec = &el->l_recs[i];
5788b06bc59SJoel Becker 		ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode,
5798b06bc59SJoel Becker 					  cluster_ac->ac_bh,
580798db35fSJoel Becker 					  le64_to_cpu(rec->e_blkno),
58172094e43SAl Viro 					  le16_to_cpu(rec->e_leaf_clusters));
5828b06bc59SJoel Becker 		if (ret)
5838b06bc59SJoel Becker 			mlog_errno(ret);
5848b06bc59SJoel Becker 		/* Try all the clusters to free */
585798db35fSJoel Becker 	}
586798db35fSJoel Becker 
587798db35fSJoel Becker 	ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
588798db35fSJoel Becker 	brelse(bg_bh);
589798db35fSJoel Becker }
590798db35fSJoel Becker 
591798db35fSJoel Becker static struct buffer_head *
592798db35fSJoel Becker ocfs2_block_group_alloc_discontig(handle_t *handle,
593798db35fSJoel Becker 				  struct inode *alloc_inode,
594798db35fSJoel Becker 				  struct ocfs2_alloc_context *ac,
5958b06bc59SJoel Becker 				  struct ocfs2_chain_list *cl)
596798db35fSJoel Becker {
597798db35fSJoel Becker 	int status;
598798db35fSJoel Becker 	u32 bit_off, num_bits;
599798db35fSJoel Becker 	u64 bg_blkno;
600798db35fSJoel Becker 	unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
601798db35fSJoel Becker 	struct buffer_head *bg_bh = NULL;
602798db35fSJoel Becker 	unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
603798db35fSJoel Becker 	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
604798db35fSJoel Becker 
6054711954eSTao Ma 	if (!ocfs2_supports_discontig_bg(osb)) {
606798db35fSJoel Becker 		status = -ENOSPC;
607798db35fSJoel Becker 		goto bail;
608798db35fSJoel Becker 	}
609798db35fSJoel Becker 
6108b06bc59SJoel Becker 	status = ocfs2_extend_trans(handle,
6118b06bc59SJoel Becker 				    ocfs2_calc_bg_discontig_credits(osb->sb));
6128b06bc59SJoel Becker 	if (status) {
6138b06bc59SJoel Becker 		mlog_errno(status);
6148b06bc59SJoel Becker 		goto bail;
6158b06bc59SJoel Becker 	}
6168b06bc59SJoel Becker 
61795ec0adfSJoel Becker 	/*
61895ec0adfSJoel Becker 	 * We're going to be grabbing from multiple cluster groups.
61995ec0adfSJoel Becker 	 * We don't have enough credits to relink them all, and the
62095ec0adfSJoel Becker 	 * cluster groups will be staying in cache for the duration of
62195ec0adfSJoel Becker 	 * this operation.
62295ec0adfSJoel Becker 	 */
623309a85b6SXiaowei.Hu 	ac->ac_disable_chain_relink = 1;
62495ec0adfSJoel Becker 
625798db35fSJoel Becker 	/* Claim the first region */
626798db35fSJoel Becker 	status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
627798db35fSJoel Becker 					      &bit_off, &num_bits);
628798db35fSJoel Becker 	if (status < 0) {
629798db35fSJoel Becker 		if (status != -ENOSPC)
630798db35fSJoel Becker 			mlog_errno(status);
631798db35fSJoel Becker 		goto bail;
632798db35fSJoel Becker 	}
633798db35fSJoel Becker 	min_bits = num_bits;
634798db35fSJoel Becker 
635798db35fSJoel Becker 	/* setup the group */
636798db35fSJoel Becker 	bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
6372f73e135STao Ma 	trace_ocfs2_block_group_alloc_discontig(
6382f73e135STao Ma 				(unsigned long long)bg_blkno, alloc_rec);
639798db35fSJoel Becker 
640798db35fSJoel Becker 	bg_bh = sb_getblk(osb->sb, bg_blkno);
641798db35fSJoel Becker 	if (!bg_bh) {
6427391a294SRui Xiang 		status = -ENOMEM;
643798db35fSJoel Becker 		mlog_errno(status);
644798db35fSJoel Becker 		goto bail;
645798db35fSJoel Becker 	}
646798db35fSJoel Becker 	ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
647798db35fSJoel Becker 
648798db35fSJoel Becker 	status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
649798db35fSJoel Becker 					bg_blkno, num_bits, alloc_rec, cl);
650798db35fSJoel Becker 	if (status < 0) {
651798db35fSJoel Becker 		mlog_errno(status);
652798db35fSJoel Becker 		goto bail;
653798db35fSJoel Becker 	}
654798db35fSJoel Becker 
655798db35fSJoel Becker 	status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
656798db35fSJoel Becker 						  bg_bh, ac, cl, min_bits);
657798db35fSJoel Becker 	if (status)
658798db35fSJoel Becker 		mlog_errno(status);
659798db35fSJoel Becker 
660798db35fSJoel Becker bail:
661798db35fSJoel Becker 	if (status)
6628b06bc59SJoel Becker 		ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh);
663798db35fSJoel Becker 	return status ? ERR_PTR(status) : bg_bh;
664798db35fSJoel Becker }
665798db35fSJoel Becker 
666ccd979bdSMark Fasheh /*
667ccd979bdSMark Fasheh  * We expect the block group allocator to already be locked.
668ccd979bdSMark Fasheh  */
669ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
670ccd979bdSMark Fasheh 				   struct inode *alloc_inode,
6711187c968SJoel Becker 				   struct buffer_head *bh,
67260ca81e8STao Ma 				   u64 max_block,
673feb473a6STao Ma 				   u64 *last_alloc_group,
67460ca81e8STao Ma 				   int flags)
675ccd979bdSMark Fasheh {
676ccd979bdSMark Fasheh 	int status, credits;
677ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
678ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl;
679ccd979bdSMark Fasheh 	struct ocfs2_alloc_context *ac = NULL;
6801fabe148SMark Fasheh 	handle_t *handle = NULL;
6814711954eSTao Ma 	u16 alloc_rec;
682ccd979bdSMark Fasheh 	struct buffer_head *bg_bh = NULL;
683ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg;
684ccd979bdSMark Fasheh 
685ccd979bdSMark Fasheh 	BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
686ccd979bdSMark Fasheh 
687ccd979bdSMark Fasheh 	cl = &fe->id2.i_chain;
6881187c968SJoel Becker 	status = ocfs2_reserve_clusters_with_limit(osb,
689ccd979bdSMark Fasheh 						   le16_to_cpu(cl->cl_cpg),
69060ca81e8STao Ma 						   max_block, flags, &ac);
691ccd979bdSMark Fasheh 	if (status < 0) {
692ccd979bdSMark Fasheh 		if (status != -ENOSPC)
693ccd979bdSMark Fasheh 			mlog_errno(status);
694ccd979bdSMark Fasheh 		goto bail;
695ccd979bdSMark Fasheh 	}
696ccd979bdSMark Fasheh 
697ccd979bdSMark Fasheh 	credits = ocfs2_calc_group_alloc_credits(osb->sb,
698ccd979bdSMark Fasheh 						 le16_to_cpu(cl->cl_cpg));
69965eff9ccSMark Fasheh 	handle = ocfs2_start_trans(osb, credits);
700ccd979bdSMark Fasheh 	if (IS_ERR(handle)) {
701ccd979bdSMark Fasheh 		status = PTR_ERR(handle);
702ccd979bdSMark Fasheh 		handle = NULL;
703ccd979bdSMark Fasheh 		mlog_errno(status);
704ccd979bdSMark Fasheh 		goto bail;
705ccd979bdSMark Fasheh 	}
706ccd979bdSMark Fasheh 
707feb473a6STao Ma 	if (last_alloc_group && *last_alloc_group != 0) {
7082f73e135STao Ma 		trace_ocfs2_block_group_alloc(
709feb473a6STao Ma 				(unsigned long long)*last_alloc_group);
710feb473a6STao Ma 		ac->ac_last_group = *last_alloc_group;
711feb473a6STao Ma 	}
712798db35fSJoel Becker 
713798db35fSJoel Becker 	bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
714798db35fSJoel Becker 					       ac, cl);
715798db35fSJoel Becker 	if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC))
716798db35fSJoel Becker 		bg_bh = ocfs2_block_group_alloc_discontig(handle,
717798db35fSJoel Becker 							  alloc_inode,
7188b06bc59SJoel Becker 							  ac, cl);
719798db35fSJoel Becker 	if (IS_ERR(bg_bh)) {
720798db35fSJoel Becker 		status = PTR_ERR(bg_bh);
721798db35fSJoel Becker 		bg_bh = NULL;
722ccd979bdSMark Fasheh 		if (status != -ENOSPC)
723ccd979bdSMark Fasheh 			mlog_errno(status);
724ccd979bdSMark Fasheh 		goto bail;
725ccd979bdSMark Fasheh 	}
726ccd979bdSMark Fasheh 	bg = (struct ocfs2_group_desc *) bg_bh->b_data;
727ccd979bdSMark Fasheh 
7280cf2f763SJoel Becker 	status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
729ccd979bdSMark Fasheh 					 bh, OCFS2_JOURNAL_ACCESS_WRITE);
730ccd979bdSMark Fasheh 	if (status < 0) {
731ccd979bdSMark Fasheh 		mlog_errno(status);
732ccd979bdSMark Fasheh 		goto bail;
733ccd979bdSMark Fasheh 	}
734ccd979bdSMark Fasheh 
7354711954eSTao Ma 	alloc_rec = le16_to_cpu(bg->bg_chain);
7364711954eSTao Ma 	le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
737ccd979bdSMark Fasheh 		     le16_to_cpu(bg->bg_free_bits_count));
7384711954eSTao Ma 	le32_add_cpu(&cl->cl_recs[alloc_rec].c_total,
739798db35fSJoel Becker 		     le16_to_cpu(bg->bg_bits));
7400a463b74STao Ma 	cl->cl_recs[alloc_rec].c_blkno = bg->bg_blkno;
741ccd979bdSMark Fasheh 	if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
742ccd979bdSMark Fasheh 		le16_add_cpu(&cl->cl_next_free_rec, 1);
743ccd979bdSMark Fasheh 
744ccd979bdSMark Fasheh 	le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
745ccd979bdSMark Fasheh 					le16_to_cpu(bg->bg_free_bits_count));
746ccd979bdSMark Fasheh 	le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
747ccd979bdSMark Fasheh 	le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
748ccd979bdSMark Fasheh 
749ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, bh);
750ccd979bdSMark Fasheh 
751ccd979bdSMark Fasheh 	spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
752ccd979bdSMark Fasheh 	OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
753ccd979bdSMark Fasheh 	fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
754ccd979bdSMark Fasheh 					     le32_to_cpu(fe->i_clusters)));
755ccd979bdSMark Fasheh 	spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
756ccd979bdSMark Fasheh 	i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
7578110b073SMark Fasheh 	alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
7586fdb702dSDarrick J. Wong 	ocfs2_update_inode_fsync_trans(handle, alloc_inode, 0);
759ccd979bdSMark Fasheh 
760ccd979bdSMark Fasheh 	status = 0;
761feb473a6STao Ma 
762feb473a6STao Ma 	/* save the new last alloc group so that the caller can cache it. */
763feb473a6STao Ma 	if (last_alloc_group)
764feb473a6STao Ma 		*last_alloc_group = ac->ac_last_group;
765feb473a6STao Ma 
766ccd979bdSMark Fasheh bail:
767ccd979bdSMark Fasheh 	if (handle)
76802dc1af4SMark Fasheh 		ocfs2_commit_trans(osb, handle);
769ccd979bdSMark Fasheh 
770ccd979bdSMark Fasheh 	if (ac)
771ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(ac);
772ccd979bdSMark Fasheh 
773ccd979bdSMark Fasheh 	brelse(bg_bh);
774ccd979bdSMark Fasheh 
775c1e8d35eSTao Ma 	if (status)
776c1e8d35eSTao Ma 		mlog_errno(status);
777ccd979bdSMark Fasheh 	return status;
778ccd979bdSMark Fasheh }
779ccd979bdSMark Fasheh 
780ccd979bdSMark Fasheh static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
781da5cbf2fSMark Fasheh 				       struct ocfs2_alloc_context *ac,
782da5cbf2fSMark Fasheh 				       int type,
783ffda89a3STao Ma 				       u32 slot,
784feb473a6STao Ma 				       u64 *last_alloc_group,
78560ca81e8STao Ma 				       int flags)
786ccd979bdSMark Fasheh {
787ccd979bdSMark Fasheh 	int status;
788ccd979bdSMark Fasheh 	u32 bits_wanted = ac->ac_bits_wanted;
789da5cbf2fSMark Fasheh 	struct inode *alloc_inode;
790ccd979bdSMark Fasheh 	struct buffer_head *bh = NULL;
791ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
792ccd979bdSMark Fasheh 	u32 free_bits;
793ccd979bdSMark Fasheh 
794da5cbf2fSMark Fasheh 	alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
795da5cbf2fSMark Fasheh 	if (!alloc_inode) {
796da5cbf2fSMark Fasheh 		mlog_errno(-EINVAL);
797da5cbf2fSMark Fasheh 		return -EINVAL;
798ccd979bdSMark Fasheh 	}
799ccd979bdSMark Fasheh 
8005955102cSAl Viro 	inode_lock(alloc_inode);
801da5cbf2fSMark Fasheh 
802e63aecb6SMark Fasheh 	status = ocfs2_inode_lock(alloc_inode, &bh, 1);
803da5cbf2fSMark Fasheh 	if (status < 0) {
8045955102cSAl Viro 		inode_unlock(alloc_inode);
805da5cbf2fSMark Fasheh 		iput(alloc_inode);
806da5cbf2fSMark Fasheh 
807da5cbf2fSMark Fasheh 		mlog_errno(status);
808da5cbf2fSMark Fasheh 		return status;
809da5cbf2fSMark Fasheh 	}
810da5cbf2fSMark Fasheh 
811da5cbf2fSMark Fasheh 	ac->ac_inode = alloc_inode;
812a4a48911STao Ma 	ac->ac_alloc_slot = slot;
813da5cbf2fSMark Fasheh 
814ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) bh->b_data;
81510995aa2SJoel Becker 
81610995aa2SJoel Becker 	/* The bh was validated by the inode read inside
81710995aa2SJoel Becker 	 * ocfs2_inode_lock().  Any corruption is a code bug. */
81810995aa2SJoel Becker 	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
81910995aa2SJoel Becker 
820ccd979bdSMark Fasheh 	if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
8217ecef14aSJoe Perches 		status = ocfs2_error(alloc_inode->i_sb,
8227ecef14aSJoe Perches 				     "Invalid chain allocator %llu\n",
823b0697053SMark Fasheh 				     (unsigned long long)le64_to_cpu(fe->i_blkno));
824ccd979bdSMark Fasheh 		goto bail;
825ccd979bdSMark Fasheh 	}
826ccd979bdSMark Fasheh 
827ccd979bdSMark Fasheh 	free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
828ccd979bdSMark Fasheh 		le32_to_cpu(fe->id1.bitmap1.i_used);
829ccd979bdSMark Fasheh 
830ccd979bdSMark Fasheh 	if (bits_wanted > free_bits) {
831ccd979bdSMark Fasheh 		/* cluster bitmap never grows */
832ccd979bdSMark Fasheh 		if (ocfs2_is_cluster_bitmap(alloc_inode)) {
8332f73e135STao Ma 			trace_ocfs2_reserve_suballoc_bits_nospc(bits_wanted,
8342f73e135STao Ma 								free_bits);
835ccd979bdSMark Fasheh 			status = -ENOSPC;
836ccd979bdSMark Fasheh 			goto bail;
837ccd979bdSMark Fasheh 		}
838ccd979bdSMark Fasheh 
83960ca81e8STao Ma 		if (!(flags & ALLOC_NEW_GROUP)) {
8402f73e135STao Ma 			trace_ocfs2_reserve_suballoc_bits_no_new_group(
841ffda89a3STao Ma 						slot, bits_wanted, free_bits);
842ffda89a3STao Ma 			status = -ENOSPC;
843ffda89a3STao Ma 			goto bail;
844ffda89a3STao Ma 		}
845ffda89a3STao Ma 
8461187c968SJoel Becker 		status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
847feb473a6STao Ma 						 ac->ac_max_block,
848feb473a6STao Ma 						 last_alloc_group, flags);
849ccd979bdSMark Fasheh 		if (status < 0) {
850ccd979bdSMark Fasheh 			if (status != -ENOSPC)
851ccd979bdSMark Fasheh 				mlog_errno(status);
852ccd979bdSMark Fasheh 			goto bail;
853ccd979bdSMark Fasheh 		}
854ccd979bdSMark Fasheh 		atomic_inc(&osb->alloc_stats.bg_extends);
855ccd979bdSMark Fasheh 
856ccd979bdSMark Fasheh 		/* You should never ask for this much metadata */
857ccd979bdSMark Fasheh 		BUG_ON(bits_wanted >
858ccd979bdSMark Fasheh 		       (le32_to_cpu(fe->id1.bitmap1.i_total)
859ccd979bdSMark Fasheh 			- le32_to_cpu(fe->id1.bitmap1.i_used)));
860ccd979bdSMark Fasheh 	}
861ccd979bdSMark Fasheh 
862ccd979bdSMark Fasheh 	get_bh(bh);
863ccd979bdSMark Fasheh 	ac->ac_bh = bh;
864ccd979bdSMark Fasheh bail:
865ccd979bdSMark Fasheh 	brelse(bh);
866ccd979bdSMark Fasheh 
867c1e8d35eSTao Ma 	if (status)
868c1e8d35eSTao Ma 		mlog_errno(status);
869ccd979bdSMark Fasheh 	return status;
870ccd979bdSMark Fasheh }
871ccd979bdSMark Fasheh 
872b89c5428STiger Yang static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
873b89c5428STiger Yang {
874b89c5428STiger Yang 	spin_lock(&osb->osb_lock);
875b89c5428STiger Yang 	osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
876b89c5428STiger Yang 	spin_unlock(&osb->osb_lock);
877b89c5428STiger Yang 	atomic_set(&osb->s_num_inodes_stolen, 0);
878b89c5428STiger Yang }
879b89c5428STiger Yang 
880b89c5428STiger Yang static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
881b89c5428STiger Yang {
882b89c5428STiger Yang 	spin_lock(&osb->osb_lock);
883b89c5428STiger Yang 	osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
884b89c5428STiger Yang 	spin_unlock(&osb->osb_lock);
885b89c5428STiger Yang 	atomic_set(&osb->s_num_meta_stolen, 0);
886b89c5428STiger Yang }
887b89c5428STiger Yang 
888b89c5428STiger Yang void ocfs2_init_steal_slots(struct ocfs2_super *osb)
889b89c5428STiger Yang {
890b89c5428STiger Yang 	ocfs2_init_inode_steal_slot(osb);
891b89c5428STiger Yang 	ocfs2_init_meta_steal_slot(osb);
892b89c5428STiger Yang }
893b89c5428STiger Yang 
894b89c5428STiger Yang static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
895b89c5428STiger Yang {
896b89c5428STiger Yang 	spin_lock(&osb->osb_lock);
897b89c5428STiger Yang 	if (type == INODE_ALLOC_SYSTEM_INODE)
898b89c5428STiger Yang 		osb->s_inode_steal_slot = slot;
899b89c5428STiger Yang 	else if (type == EXTENT_ALLOC_SYSTEM_INODE)
900b89c5428STiger Yang 		osb->s_meta_steal_slot = slot;
901b89c5428STiger Yang 	spin_unlock(&osb->osb_lock);
902b89c5428STiger Yang }
903b89c5428STiger Yang 
904b89c5428STiger Yang static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
905b89c5428STiger Yang {
906b89c5428STiger Yang 	int slot = OCFS2_INVALID_SLOT;
907b89c5428STiger Yang 
908b89c5428STiger Yang 	spin_lock(&osb->osb_lock);
909b89c5428STiger Yang 	if (type == INODE_ALLOC_SYSTEM_INODE)
910b89c5428STiger Yang 		slot = osb->s_inode_steal_slot;
911b89c5428STiger Yang 	else if (type == EXTENT_ALLOC_SYSTEM_INODE)
912b89c5428STiger Yang 		slot = osb->s_meta_steal_slot;
913b89c5428STiger Yang 	spin_unlock(&osb->osb_lock);
914b89c5428STiger Yang 
915b89c5428STiger Yang 	return slot;
916b89c5428STiger Yang }
917b89c5428STiger Yang 
918b89c5428STiger Yang static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
919b89c5428STiger Yang {
920b89c5428STiger Yang 	return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
921b89c5428STiger Yang }
922b89c5428STiger Yang 
923b89c5428STiger Yang static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
924b89c5428STiger Yang {
925b89c5428STiger Yang 	return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
926b89c5428STiger Yang }
927b89c5428STiger Yang 
928b89c5428STiger Yang static int ocfs2_steal_resource(struct ocfs2_super *osb,
929b89c5428STiger Yang 				struct ocfs2_alloc_context *ac,
930b89c5428STiger Yang 				int type)
931b89c5428STiger Yang {
932b89c5428STiger Yang 	int i, status = -ENOSPC;
933b89c5428STiger Yang 	int slot = __ocfs2_get_steal_slot(osb, type);
934b89c5428STiger Yang 
935b89c5428STiger Yang 	/* Start to steal resource from the first slot after ours. */
936b89c5428STiger Yang 	if (slot == OCFS2_INVALID_SLOT)
937b89c5428STiger Yang 		slot = osb->slot_num + 1;
938b89c5428STiger Yang 
939b89c5428STiger Yang 	for (i = 0; i < osb->max_slots; i++, slot++) {
940b89c5428STiger Yang 		if (slot == osb->max_slots)
941b89c5428STiger Yang 			slot = 0;
942b89c5428STiger Yang 
943b89c5428STiger Yang 		if (slot == osb->slot_num)
944b89c5428STiger Yang 			continue;
945b89c5428STiger Yang 
946b89c5428STiger Yang 		status = ocfs2_reserve_suballoc_bits(osb, ac,
947b89c5428STiger Yang 						     type,
948b89c5428STiger Yang 						     (u32)slot, NULL,
949b89c5428STiger Yang 						     NOT_ALLOC_NEW_GROUP);
950b89c5428STiger Yang 		if (status >= 0) {
951b89c5428STiger Yang 			__ocfs2_set_steal_slot(osb, slot, type);
952b89c5428STiger Yang 			break;
953b89c5428STiger Yang 		}
954b89c5428STiger Yang 
955b89c5428STiger Yang 		ocfs2_free_ac_resource(ac);
956b89c5428STiger Yang 	}
957b89c5428STiger Yang 
958b89c5428STiger Yang 	return status;
959b89c5428STiger Yang }
960b89c5428STiger Yang 
961b89c5428STiger Yang static int ocfs2_steal_inode(struct ocfs2_super *osb,
962b89c5428STiger Yang 			     struct ocfs2_alloc_context *ac)
963b89c5428STiger Yang {
964b89c5428STiger Yang 	return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
965b89c5428STiger Yang }
966b89c5428STiger Yang 
967b89c5428STiger Yang static int ocfs2_steal_meta(struct ocfs2_super *osb,
968b89c5428STiger Yang 			    struct ocfs2_alloc_context *ac)
969b89c5428STiger Yang {
970b89c5428STiger Yang 	return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
971b89c5428STiger Yang }
972b89c5428STiger Yang 
973cf1d6c76STiger Yang int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
974cf1d6c76STiger Yang 				      int blocks,
975ccd979bdSMark Fasheh 				      struct ocfs2_alloc_context **ac)
976ccd979bdSMark Fasheh {
977ccd979bdSMark Fasheh 	int status;
978b89c5428STiger Yang 	int slot = ocfs2_get_meta_steal_slot(osb);
979ccd979bdSMark Fasheh 
980cd861280SRobert P. J. Day 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
981ccd979bdSMark Fasheh 	if (!(*ac)) {
982ccd979bdSMark Fasheh 		status = -ENOMEM;
983ccd979bdSMark Fasheh 		mlog_errno(status);
984ccd979bdSMark Fasheh 		goto bail;
985ccd979bdSMark Fasheh 	}
986ccd979bdSMark Fasheh 
987cf1d6c76STiger Yang 	(*ac)->ac_bits_wanted = blocks;
988ccd979bdSMark Fasheh 	(*ac)->ac_which = OCFS2_AC_USE_META;
989ccd979bdSMark Fasheh 	(*ac)->ac_group_search = ocfs2_block_group_search;
990ccd979bdSMark Fasheh 
991b89c5428STiger Yang 	if (slot != OCFS2_INVALID_SLOT &&
992b89c5428STiger Yang 		atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
993b89c5428STiger Yang 		goto extent_steal;
994b89c5428STiger Yang 
995b89c5428STiger Yang 	atomic_set(&osb->s_num_meta_stolen, 0);
996da5cbf2fSMark Fasheh 	status = ocfs2_reserve_suballoc_bits(osb, (*ac),
997ffda89a3STao Ma 					     EXTENT_ALLOC_SYSTEM_INODE,
998b89c5428STiger Yang 					     (u32)osb->slot_num, NULL,
99933d5d380SMark Fasheh 					     ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
1000b89c5428STiger Yang 
1001b89c5428STiger Yang 
1002b89c5428STiger Yang 	if (status >= 0) {
1003b89c5428STiger Yang 		status = 0;
1004b89c5428STiger Yang 		if (slot != OCFS2_INVALID_SLOT)
1005b89c5428STiger Yang 			ocfs2_init_meta_steal_slot(osb);
1006b89c5428STiger Yang 		goto bail;
1007b89c5428STiger Yang 	} else if (status < 0 && status != -ENOSPC) {
1008b89c5428STiger Yang 		mlog_errno(status);
1009b89c5428STiger Yang 		goto bail;
1010b89c5428STiger Yang 	}
1011b89c5428STiger Yang 
1012b89c5428STiger Yang 	ocfs2_free_ac_resource(*ac);
1013b89c5428STiger Yang 
1014b89c5428STiger Yang extent_steal:
1015b89c5428STiger Yang 	status = ocfs2_steal_meta(osb, *ac);
1016b89c5428STiger Yang 	atomic_inc(&osb->s_num_meta_stolen);
1017ccd979bdSMark Fasheh 	if (status < 0) {
1018ccd979bdSMark Fasheh 		if (status != -ENOSPC)
1019ccd979bdSMark Fasheh 			mlog_errno(status);
1020ccd979bdSMark Fasheh 		goto bail;
1021ccd979bdSMark Fasheh 	}
1022ccd979bdSMark Fasheh 
1023ccd979bdSMark Fasheh 	status = 0;
1024ccd979bdSMark Fasheh bail:
1025ccd979bdSMark Fasheh 	if ((status < 0) && *ac) {
1026ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(*ac);
1027ccd979bdSMark Fasheh 		*ac = NULL;
1028ccd979bdSMark Fasheh 	}
1029ccd979bdSMark Fasheh 
1030c1e8d35eSTao Ma 	if (status)
1031c1e8d35eSTao Ma 		mlog_errno(status);
1032ccd979bdSMark Fasheh 	return status;
1033ccd979bdSMark Fasheh }
1034ccd979bdSMark Fasheh 
1035cf1d6c76STiger Yang int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
1036cf1d6c76STiger Yang 			       struct ocfs2_extent_list *root_el,
1037cf1d6c76STiger Yang 			       struct ocfs2_alloc_context **ac)
1038cf1d6c76STiger Yang {
1039cf1d6c76STiger Yang 	return ocfs2_reserve_new_metadata_blocks(osb,
1040cf1d6c76STiger Yang 					ocfs2_extend_meta_needed(root_el),
1041cf1d6c76STiger Yang 					ac);
1042cf1d6c76STiger Yang }
1043cf1d6c76STiger Yang 
1044ccd979bdSMark Fasheh int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
1045ccd979bdSMark Fasheh 			    struct ocfs2_alloc_context **ac)
1046ccd979bdSMark Fasheh {
1047ccd979bdSMark Fasheh 	int status;
1048b89c5428STiger Yang 	int slot = ocfs2_get_inode_steal_slot(osb);
1049feb473a6STao Ma 	u64 alloc_group;
1050ccd979bdSMark Fasheh 
1051cd861280SRobert P. J. Day 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
1052ccd979bdSMark Fasheh 	if (!(*ac)) {
1053ccd979bdSMark Fasheh 		status = -ENOMEM;
1054ccd979bdSMark Fasheh 		mlog_errno(status);
1055ccd979bdSMark Fasheh 		goto bail;
1056ccd979bdSMark Fasheh 	}
1057ccd979bdSMark Fasheh 
1058ccd979bdSMark Fasheh 	(*ac)->ac_bits_wanted = 1;
1059ccd979bdSMark Fasheh 	(*ac)->ac_which = OCFS2_AC_USE_INODE;
1060ccd979bdSMark Fasheh 
1061ccd979bdSMark Fasheh 	(*ac)->ac_group_search = ocfs2_block_group_search;
1062ccd979bdSMark Fasheh 
10634d0ddb2cSTao Ma 	/*
10641187c968SJoel Becker 	 * stat(2) can't handle i_ino > 32bits, so we tell the
10651187c968SJoel Becker 	 * lower levels not to allocate us a block group past that
106612462f1dSJoel Becker 	 * limit.  The 'inode64' mount option avoids this behavior.
10671187c968SJoel Becker 	 */
106812462f1dSJoel Becker 	if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
10691187c968SJoel Becker 		(*ac)->ac_max_block = (u32)~0U;
10701187c968SJoel Becker 
10711187c968SJoel Becker 	/*
10724d0ddb2cSTao Ma 	 * slot is set when we successfully steal inode from other nodes.
10734d0ddb2cSTao Ma 	 * It is reset in 3 places:
10744d0ddb2cSTao Ma 	 * 1. when we flush the truncate log
10754d0ddb2cSTao Ma 	 * 2. when we complete local alloc recovery.
10764d0ddb2cSTao Ma 	 * 3. when we successfully allocate from our own slot.
10774d0ddb2cSTao Ma 	 * After it is set, we will go on stealing inodes until we find the
10784d0ddb2cSTao Ma 	 * need to check our slots to see whether there is some space for us.
10794d0ddb2cSTao Ma 	 */
10804d0ddb2cSTao Ma 	if (slot != OCFS2_INVALID_SLOT &&
1081b89c5428STiger Yang 	    atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
10824d0ddb2cSTao Ma 		goto inode_steal;
10834d0ddb2cSTao Ma 
10844d0ddb2cSTao Ma 	atomic_set(&osb->s_num_inodes_stolen, 0);
1085feb473a6STao Ma 	alloc_group = osb->osb_inode_alloc_group;
1086da5cbf2fSMark Fasheh 	status = ocfs2_reserve_suballoc_bits(osb, *ac,
1087da5cbf2fSMark Fasheh 					     INODE_ALLOC_SYSTEM_INODE,
1088b89c5428STiger Yang 					     (u32)osb->slot_num,
1089feb473a6STao Ma 					     &alloc_group,
109060ca81e8STao Ma 					     ALLOC_NEW_GROUP |
109160ca81e8STao Ma 					     ALLOC_GROUPS_FROM_GLOBAL);
10924d0ddb2cSTao Ma 	if (status >= 0) {
10934d0ddb2cSTao Ma 		status = 0;
10944d0ddb2cSTao Ma 
1095feb473a6STao Ma 		spin_lock(&osb->osb_lock);
1096feb473a6STao Ma 		osb->osb_inode_alloc_group = alloc_group;
1097feb473a6STao Ma 		spin_unlock(&osb->osb_lock);
10982f73e135STao Ma 		trace_ocfs2_reserve_new_inode_new_group(
10992f73e135STao Ma 			(unsigned long long)alloc_group);
1100feb473a6STao Ma 
11014d0ddb2cSTao Ma 		/*
11024d0ddb2cSTao Ma 		 * Some inodes must be freed by us, so try to allocate
11034d0ddb2cSTao Ma 		 * from our own next time.
11044d0ddb2cSTao Ma 		 */
11054d0ddb2cSTao Ma 		if (slot != OCFS2_INVALID_SLOT)
11064d0ddb2cSTao Ma 			ocfs2_init_inode_steal_slot(osb);
11074d0ddb2cSTao Ma 		goto bail;
11084d0ddb2cSTao Ma 	} else if (status < 0 && status != -ENOSPC) {
11094d0ddb2cSTao Ma 		mlog_errno(status);
11104d0ddb2cSTao Ma 		goto bail;
11114d0ddb2cSTao Ma 	}
11124d0ddb2cSTao Ma 
11134d0ddb2cSTao Ma 	ocfs2_free_ac_resource(*ac);
11144d0ddb2cSTao Ma 
11154d0ddb2cSTao Ma inode_steal:
1116b89c5428STiger Yang 	status = ocfs2_steal_inode(osb, *ac);
11174d0ddb2cSTao Ma 	atomic_inc(&osb->s_num_inodes_stolen);
1118ccd979bdSMark Fasheh 	if (status < 0) {
1119ccd979bdSMark Fasheh 		if (status != -ENOSPC)
1120ccd979bdSMark Fasheh 			mlog_errno(status);
1121ccd979bdSMark Fasheh 		goto bail;
1122ccd979bdSMark Fasheh 	}
1123ccd979bdSMark Fasheh 
1124ccd979bdSMark Fasheh 	status = 0;
1125ccd979bdSMark Fasheh bail:
1126ccd979bdSMark Fasheh 	if ((status < 0) && *ac) {
1127ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(*ac);
1128ccd979bdSMark Fasheh 		*ac = NULL;
1129ccd979bdSMark Fasheh 	}
1130ccd979bdSMark Fasheh 
1131c1e8d35eSTao Ma 	if (status)
1132c1e8d35eSTao Ma 		mlog_errno(status);
1133ccd979bdSMark Fasheh 	return status;
1134ccd979bdSMark Fasheh }
1135ccd979bdSMark Fasheh 
1136ccd979bdSMark Fasheh /* local alloc code has to do the same thing, so rather than do this
1137ccd979bdSMark Fasheh  * twice.. */
1138ccd979bdSMark Fasheh int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
1139ccd979bdSMark Fasheh 				      struct ocfs2_alloc_context *ac)
1140ccd979bdSMark Fasheh {
1141ccd979bdSMark Fasheh 	int status;
1142ccd979bdSMark Fasheh 
1143ccd979bdSMark Fasheh 	ac->ac_which = OCFS2_AC_USE_MAIN;
1144ccd979bdSMark Fasheh 	ac->ac_group_search = ocfs2_cluster_group_search;
1145ccd979bdSMark Fasheh 
1146da5cbf2fSMark Fasheh 	status = ocfs2_reserve_suballoc_bits(osb, ac,
1147da5cbf2fSMark Fasheh 					     GLOBAL_BITMAP_SYSTEM_INODE,
1148feb473a6STao Ma 					     OCFS2_INVALID_SLOT, NULL,
1149ffda89a3STao Ma 					     ALLOC_NEW_GROUP);
115047ee9d89SGuozhonghua 	if (status < 0 && status != -ENOSPC)
1151ccd979bdSMark Fasheh 		mlog_errno(status);
1152da5cbf2fSMark Fasheh 
1153ccd979bdSMark Fasheh 	return status;
1154ccd979bdSMark Fasheh }
1155ccd979bdSMark Fasheh 
1156ccd979bdSMark Fasheh /* Callers don't need to care which bitmap (local alloc or main) to
1157ccd979bdSMark Fasheh  * use so we figure it out for them, but unfortunately this clutters
1158ccd979bdSMark Fasheh  * things a bit. */
11591187c968SJoel Becker static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
11601187c968SJoel Becker 					     u32 bits_wanted, u64 max_block,
116160ca81e8STao Ma 					     int flags,
1162ccd979bdSMark Fasheh 					     struct ocfs2_alloc_context **ac)
1163ccd979bdSMark Fasheh {
11642070ad1aSEric Ren 	int status, ret = 0;
11652070ad1aSEric Ren 	int retried = 0;
1166ccd979bdSMark Fasheh 
1167cd861280SRobert P. J. Day 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
1168ccd979bdSMark Fasheh 	if (!(*ac)) {
1169ccd979bdSMark Fasheh 		status = -ENOMEM;
1170ccd979bdSMark Fasheh 		mlog_errno(status);
1171ccd979bdSMark Fasheh 		goto bail;
1172ccd979bdSMark Fasheh 	}
1173ccd979bdSMark Fasheh 
1174ccd979bdSMark Fasheh 	(*ac)->ac_bits_wanted = bits_wanted;
11751187c968SJoel Becker 	(*ac)->ac_max_block = max_block;
1176ccd979bdSMark Fasheh 
1177ccd979bdSMark Fasheh 	status = -ENOSPC;
117860ca81e8STao Ma 	if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
117960ca81e8STao Ma 	    ocfs2_alloc_should_use_local(osb, bits_wanted)) {
1180ccd979bdSMark Fasheh 		status = ocfs2_reserve_local_alloc_bits(osb,
1181ccd979bdSMark Fasheh 							bits_wanted,
1182ccd979bdSMark Fasheh 							*ac);
1183a57c8fd2SMark Fasheh 		if ((status < 0) && (status != -ENOSPC)) {
1184ccd979bdSMark Fasheh 			mlog_errno(status);
1185ccd979bdSMark Fasheh 			goto bail;
1186ccd979bdSMark Fasheh 		}
1187ccd979bdSMark Fasheh 	}
1188ccd979bdSMark Fasheh 
1189ccd979bdSMark Fasheh 	if (status == -ENOSPC) {
11902070ad1aSEric Ren retry:
1191ccd979bdSMark Fasheh 		status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
11922070ad1aSEric Ren 		/* Retry if there is sufficient space cached in truncate log */
11932070ad1aSEric Ren 		if (status == -ENOSPC && !retried) {
11942070ad1aSEric Ren 			retried = 1;
11952070ad1aSEric Ren 			ocfs2_inode_unlock((*ac)->ac_inode, 1);
11962070ad1aSEric Ren 			inode_unlock((*ac)->ac_inode);
11972070ad1aSEric Ren 
11982070ad1aSEric Ren 			ret = ocfs2_try_to_free_truncate_log(osb, bits_wanted);
11993bb8b653SJoseph Qi 			if (ret == 1) {
12003bb8b653SJoseph Qi 				iput((*ac)->ac_inode);
12013bb8b653SJoseph Qi 				(*ac)->ac_inode = NULL;
12022070ad1aSEric Ren 				goto retry;
12033bb8b653SJoseph Qi 			}
12042070ad1aSEric Ren 
12052070ad1aSEric Ren 			if (ret < 0)
12062070ad1aSEric Ren 				mlog_errno(ret);
12072070ad1aSEric Ren 
12082070ad1aSEric Ren 			inode_lock((*ac)->ac_inode);
12093bb8b653SJoseph Qi 			ret = ocfs2_inode_lock((*ac)->ac_inode, NULL, 1);
12103bb8b653SJoseph Qi 			if (ret < 0) {
12113bb8b653SJoseph Qi 				mlog_errno(ret);
12123bb8b653SJoseph Qi 				inode_unlock((*ac)->ac_inode);
12133bb8b653SJoseph Qi 				iput((*ac)->ac_inode);
12143bb8b653SJoseph Qi 				(*ac)->ac_inode = NULL;
12153bb8b653SJoseph Qi 				goto bail;
12163bb8b653SJoseph Qi 			}
12172070ad1aSEric Ren 		}
1218ccd979bdSMark Fasheh 		if (status < 0) {
1219ccd979bdSMark Fasheh 			if (status != -ENOSPC)
1220ccd979bdSMark Fasheh 				mlog_errno(status);
1221ccd979bdSMark Fasheh 			goto bail;
1222ccd979bdSMark Fasheh 		}
1223ccd979bdSMark Fasheh 	}
1224ccd979bdSMark Fasheh 
1225ccd979bdSMark Fasheh 	status = 0;
1226ccd979bdSMark Fasheh bail:
1227ccd979bdSMark Fasheh 	if ((status < 0) && *ac) {
1228ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(*ac);
1229ccd979bdSMark Fasheh 		*ac = NULL;
1230ccd979bdSMark Fasheh 	}
1231ccd979bdSMark Fasheh 
1232c1e8d35eSTao Ma 	if (status)
1233c1e8d35eSTao Ma 		mlog_errno(status);
1234ccd979bdSMark Fasheh 	return status;
1235ccd979bdSMark Fasheh }
1236ccd979bdSMark Fasheh 
12371187c968SJoel Becker int ocfs2_reserve_clusters(struct ocfs2_super *osb,
12381187c968SJoel Becker 			   u32 bits_wanted,
12391187c968SJoel Becker 			   struct ocfs2_alloc_context **ac)
12401187c968SJoel Becker {
124160ca81e8STao Ma 	return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
124260ca81e8STao Ma 						 ALLOC_NEW_GROUP, ac);
12431187c968SJoel Becker }
12441187c968SJoel Becker 
1245ccd979bdSMark Fasheh /*
1246ccd979bdSMark Fasheh  * More or less lifted from ext3. I'll leave their description below:
1247ccd979bdSMark Fasheh  *
1248ccd979bdSMark Fasheh  * "For ext3 allocations, we must not reuse any blocks which are
1249ccd979bdSMark Fasheh  * allocated in the bitmap buffer's "last committed data" copy.  This
1250ccd979bdSMark Fasheh  * prevents deletes from freeing up the page for reuse until we have
1251ccd979bdSMark Fasheh  * committed the delete transaction.
1252ccd979bdSMark Fasheh  *
1253ccd979bdSMark Fasheh  * If we didn't do this, then deleting something and reallocating it as
1254ccd979bdSMark Fasheh  * data would allow the old block to be overwritten before the
1255ccd979bdSMark Fasheh  * transaction committed (because we force data to disk before commit).
1256ccd979bdSMark Fasheh  * This would lead to corruption if we crashed between overwriting the
1257ccd979bdSMark Fasheh  * data and committing the delete.
1258ccd979bdSMark Fasheh  *
1259ccd979bdSMark Fasheh  * @@@ We may want to make this allocation behaviour conditional on
1260ccd979bdSMark Fasheh  * data-writes at some point, and disable it for metadata allocations or
1261ccd979bdSMark Fasheh  * sync-data inodes."
1262ccd979bdSMark Fasheh  *
1263ccd979bdSMark Fasheh  * Note: OCFS2 already does this differently for metadata vs data
1264c78bad11SJoe Perches  * allocations, as those bitmaps are separate and undo access is never
1265ccd979bdSMark Fasheh  * called on a metadata group descriptor.
1266ccd979bdSMark Fasheh  */
1267ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
1268ccd979bdSMark Fasheh 					 int nr)
1269ccd979bdSMark Fasheh {
1270ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
127194e41ecfSSunil Mushran 	int ret;
1272ccd979bdSMark Fasheh 
1273ccd979bdSMark Fasheh 	if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
1274ccd979bdSMark Fasheh 		return 0;
127594e41ecfSSunil Mushran 
127694e41ecfSSunil Mushran 	if (!buffer_jbd(bg_bh))
1277ccd979bdSMark Fasheh 		return 1;
1278ccd979bdSMark Fasheh 
127994e41ecfSSunil Mushran 	jbd_lock_bh_state(bg_bh);
1280ccd979bdSMark Fasheh 	bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
128194e41ecfSSunil Mushran 	if (bg)
128294e41ecfSSunil Mushran 		ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
128394e41ecfSSunil Mushran 	else
128494e41ecfSSunil Mushran 		ret = 1;
128594e41ecfSSunil Mushran 	jbd_unlock_bh_state(bg_bh);
128694e41ecfSSunil Mushran 
128794e41ecfSSunil Mushran 	return ret;
1288ccd979bdSMark Fasheh }
1289ccd979bdSMark Fasheh 
1290ccd979bdSMark Fasheh static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1291ccd979bdSMark Fasheh 					     struct buffer_head *bg_bh,
1292ccd979bdSMark Fasheh 					     unsigned int bits_wanted,
12937bf72edeSMark Fasheh 					     unsigned int total_bits,
12947d1fe093SJoel Becker 					     struct ocfs2_suballoc_result *res)
1295ccd979bdSMark Fasheh {
1296ccd979bdSMark Fasheh 	void *bitmap;
1297ccd979bdSMark Fasheh 	u16 best_offset, best_size;
1298ccd979bdSMark Fasheh 	int offset, start, found, status = 0;
1299ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1300ccd979bdSMark Fasheh 
130142035306SJoel Becker 	/* Callers got this descriptor from
130242035306SJoel Becker 	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
130342035306SJoel Becker 	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1304ccd979bdSMark Fasheh 
1305ccd979bdSMark Fasheh 	found = start = best_offset = best_size = 0;
1306ccd979bdSMark Fasheh 	bitmap = bg->bg_bitmap;
1307ccd979bdSMark Fasheh 
13087bf72edeSMark Fasheh 	while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
13097bf72edeSMark Fasheh 		if (offset == total_bits)
1310ccd979bdSMark Fasheh 			break;
1311ccd979bdSMark Fasheh 
1312ccd979bdSMark Fasheh 		if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
1313ccd979bdSMark Fasheh 			/* We found a zero, but we can't use it as it
1314ccd979bdSMark Fasheh 			 * hasn't been put to disk yet! */
1315ccd979bdSMark Fasheh 			found = 0;
1316ccd979bdSMark Fasheh 			start = offset + 1;
1317ccd979bdSMark Fasheh 		} else if (offset == start) {
1318ccd979bdSMark Fasheh 			/* we found a zero */
1319ccd979bdSMark Fasheh 			found++;
1320ccd979bdSMark Fasheh 			/* move start to the next bit to test */
1321ccd979bdSMark Fasheh 			start++;
1322ccd979bdSMark Fasheh 		} else {
1323ccd979bdSMark Fasheh 			/* got a zero after some ones */
1324ccd979bdSMark Fasheh 			found = 1;
1325ccd979bdSMark Fasheh 			start = offset + 1;
1326ccd979bdSMark Fasheh 		}
1327ccd979bdSMark Fasheh 		if (found > best_size) {
1328ccd979bdSMark Fasheh 			best_size = found;
1329ccd979bdSMark Fasheh 			best_offset = start - found;
1330ccd979bdSMark Fasheh 		}
1331ccd979bdSMark Fasheh 		/* we got everything we needed */
1332ccd979bdSMark Fasheh 		if (found == bits_wanted) {
1333ccd979bdSMark Fasheh 			/* mlog(0, "Found it all!\n"); */
1334ccd979bdSMark Fasheh 			break;
1335ccd979bdSMark Fasheh 		}
1336ccd979bdSMark Fasheh 	}
1337ccd979bdSMark Fasheh 
13387d1fe093SJoel Becker 	if (best_size) {
13397d1fe093SJoel Becker 		res->sr_bit_offset = best_offset;
13407d1fe093SJoel Becker 		res->sr_bits = best_size;
1341ccd979bdSMark Fasheh 	} else {
1342ccd979bdSMark Fasheh 		status = -ENOSPC;
1343ccd979bdSMark Fasheh 		/* No error log here -- see the comment above
1344ccd979bdSMark Fasheh 		 * ocfs2_test_bg_bit_allocatable */
1345ccd979bdSMark Fasheh 	}
1346ccd979bdSMark Fasheh 
1347ccd979bdSMark Fasheh 	return status;
1348ccd979bdSMark Fasheh }
1349ccd979bdSMark Fasheh 
13500a2fcd89SYounger Liu int ocfs2_block_group_set_bits(handle_t *handle,
1351ccd979bdSMark Fasheh 					     struct inode *alloc_inode,
1352ccd979bdSMark Fasheh 					     struct ocfs2_group_desc *bg,
1353ccd979bdSMark Fasheh 					     struct buffer_head *group_bh,
1354ccd979bdSMark Fasheh 					     unsigned int bit_off,
1355ccd979bdSMark Fasheh 					     unsigned int num_bits)
1356ccd979bdSMark Fasheh {
1357ccd979bdSMark Fasheh 	int status;
1358ccd979bdSMark Fasheh 	void *bitmap = bg->bg_bitmap;
1359ccd979bdSMark Fasheh 	int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1360ccd979bdSMark Fasheh 
136142035306SJoel Becker 	/* All callers get the descriptor via
136242035306SJoel Becker 	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
136342035306SJoel Becker 	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1364ccd979bdSMark Fasheh 	BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
1365ccd979bdSMark Fasheh 
13662f73e135STao Ma 	trace_ocfs2_block_group_set_bits(bit_off, num_bits);
1367ccd979bdSMark Fasheh 
1368ccd979bdSMark Fasheh 	if (ocfs2_is_cluster_bitmap(alloc_inode))
1369ccd979bdSMark Fasheh 		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1370ccd979bdSMark Fasheh 
137113723d00SJoel Becker 	status = ocfs2_journal_access_gd(handle,
13720cf2f763SJoel Becker 					 INODE_CACHE(alloc_inode),
1373ccd979bdSMark Fasheh 					 group_bh,
1374ccd979bdSMark Fasheh 					 journal_type);
1375ccd979bdSMark Fasheh 	if (status < 0) {
1376ccd979bdSMark Fasheh 		mlog_errno(status);
1377ccd979bdSMark Fasheh 		goto bail;
1378ccd979bdSMark Fasheh 	}
1379ccd979bdSMark Fasheh 
1380ccd979bdSMark Fasheh 	le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
13819b5cd10eSSrinivas Eeda 	if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
13827ecef14aSJoe Perches 		return ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit count %u but claims %u are freed. num_bits %d\n",
13839b5cd10eSSrinivas Eeda 				   (unsigned long long)le64_to_cpu(bg->bg_blkno),
13849b5cd10eSSrinivas Eeda 				   le16_to_cpu(bg->bg_bits),
13857ecef14aSJoe Perches 				   le16_to_cpu(bg->bg_free_bits_count),
13867ecef14aSJoe Perches 				   num_bits);
13879b5cd10eSSrinivas Eeda 	}
1388ccd979bdSMark Fasheh 	while(num_bits--)
1389ccd979bdSMark Fasheh 		ocfs2_set_bit(bit_off++, bitmap);
1390ccd979bdSMark Fasheh 
1391ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, group_bh);
1392ccd979bdSMark Fasheh 
1393ccd979bdSMark Fasheh bail:
1394ccd979bdSMark Fasheh 	return status;
1395ccd979bdSMark Fasheh }
1396ccd979bdSMark Fasheh 
1397ccd979bdSMark Fasheh /* find the one with the most empty bits */
1398ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1399ccd979bdSMark Fasheh {
1400ccd979bdSMark Fasheh 	u16 curr, best;
1401ccd979bdSMark Fasheh 
1402ccd979bdSMark Fasheh 	BUG_ON(!cl->cl_next_free_rec);
1403ccd979bdSMark Fasheh 
1404ccd979bdSMark Fasheh 	best = curr = 0;
1405ccd979bdSMark Fasheh 	while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1406ccd979bdSMark Fasheh 		if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1407ccd979bdSMark Fasheh 		    le32_to_cpu(cl->cl_recs[best].c_free))
1408ccd979bdSMark Fasheh 			best = curr;
1409ccd979bdSMark Fasheh 		curr++;
1410ccd979bdSMark Fasheh 	}
1411ccd979bdSMark Fasheh 
1412ccd979bdSMark Fasheh 	BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1413ccd979bdSMark Fasheh 	return best;
1414ccd979bdSMark Fasheh }
1415ccd979bdSMark Fasheh 
14161fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle,
1417ccd979bdSMark Fasheh 				    struct inode *alloc_inode,
1418ccd979bdSMark Fasheh 				    struct buffer_head *fe_bh,
1419ccd979bdSMark Fasheh 				    struct buffer_head *bg_bh,
1420ccd979bdSMark Fasheh 				    struct buffer_head *prev_bg_bh,
1421ccd979bdSMark Fasheh 				    u16 chain)
1422ccd979bdSMark Fasheh {
1423ccd979bdSMark Fasheh 	int status;
1424ccd979bdSMark Fasheh 	/* there is a really tiny chance the journal calls could fail,
1425ccd979bdSMark Fasheh 	 * but we wouldn't want inconsistent blocks in *any* case. */
142649309841SJie Liu 	u64 bg_ptr, prev_bg_ptr;
1427ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1428ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1429ccd979bdSMark Fasheh 	struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1430ccd979bdSMark Fasheh 
143142035306SJoel Becker 	/* The caller got these descriptors from
143242035306SJoel Becker 	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
143342035306SJoel Becker 	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
143442035306SJoel Becker 	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
1435ccd979bdSMark Fasheh 
14362f73e135STao Ma 	trace_ocfs2_relink_block_group(
14371ca1a111SMark Fasheh 		(unsigned long long)le64_to_cpu(fe->i_blkno), chain,
14381ca1a111SMark Fasheh 		(unsigned long long)le64_to_cpu(bg->bg_blkno),
14391ca1a111SMark Fasheh 		(unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
1440ccd979bdSMark Fasheh 
1441ccd979bdSMark Fasheh 	bg_ptr = le64_to_cpu(bg->bg_next_group);
1442ccd979bdSMark Fasheh 	prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1443ccd979bdSMark Fasheh 
14440cf2f763SJoel Becker 	status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
14450cf2f763SJoel Becker 					 prev_bg_bh,
1446ccd979bdSMark Fasheh 					 OCFS2_JOURNAL_ACCESS_WRITE);
144725e28921SAndrew Morton 	if (status < 0)
144849309841SJie Liu 		goto out;
1449ccd979bdSMark Fasheh 
1450ccd979bdSMark Fasheh 	prev_bg->bg_next_group = bg->bg_next_group;
1451ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, prev_bg_bh);
1452ccd979bdSMark Fasheh 
14530cf2f763SJoel Becker 	status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
14540cf2f763SJoel Becker 					 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
145525e28921SAndrew Morton 	if (status < 0)
145649309841SJie Liu 		goto out_rollback_prev_bg;
1457ccd979bdSMark Fasheh 
1458ccd979bdSMark Fasheh 	bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
1459ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, bg_bh);
1460ccd979bdSMark Fasheh 
14610cf2f763SJoel Becker 	status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
14620cf2f763SJoel Becker 					 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
146325e28921SAndrew Morton 	if (status < 0)
146449309841SJie Liu 		goto out_rollback_bg;
1465ccd979bdSMark Fasheh 
1466ccd979bdSMark Fasheh 	fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
1467ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, fe_bh);
1468ccd979bdSMark Fasheh 
146949309841SJie Liu out:
147025e28921SAndrew Morton 	if (status < 0)
147125e28921SAndrew Morton 		mlog_errno(status);
147249309841SJie Liu 	return status;
147342035306SJoel Becker 
147449309841SJie Liu out_rollback_bg:
147549309841SJie Liu 	bg->bg_next_group = cpu_to_le64(bg_ptr);
147649309841SJie Liu out_rollback_prev_bg:
147749309841SJie Liu 	prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
147825e28921SAndrew Morton 	goto out;
1479ccd979bdSMark Fasheh }
1480ccd979bdSMark Fasheh 
1481ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1482ccd979bdSMark Fasheh 						     u32 wanted)
1483ccd979bdSMark Fasheh {
1484ccd979bdSMark Fasheh 	return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1485ccd979bdSMark Fasheh }
1486ccd979bdSMark Fasheh 
1487ccd979bdSMark Fasheh /* return 0 on success, -ENOSPC to keep searching and any other < 0
1488ccd979bdSMark Fasheh  * value on error. */
1489ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode,
1490ccd979bdSMark Fasheh 				      struct buffer_head *group_bh,
1491ccd979bdSMark Fasheh 				      u32 bits_wanted, u32 min_bits,
14921187c968SJoel Becker 				      u64 max_block,
14937d1fe093SJoel Becker 				      struct ocfs2_suballoc_result *res)
1494ccd979bdSMark Fasheh {
1495ccd979bdSMark Fasheh 	int search = -ENOSPC;
1496ccd979bdSMark Fasheh 	int ret;
14971187c968SJoel Becker 	u64 blkoff;
14987bf72edeSMark Fasheh 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
14999c7af40bSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
15007bf72edeSMark Fasheh 	unsigned int max_bits, gd_cluster_off;
1501ccd979bdSMark Fasheh 
1502ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1503ccd979bdSMark Fasheh 
15047bf72edeSMark Fasheh 	if (gd->bg_free_bits_count) {
15057bf72edeSMark Fasheh 		max_bits = le16_to_cpu(gd->bg_bits);
15067bf72edeSMark Fasheh 
15077bf72edeSMark Fasheh 		/* Tail groups in cluster bitmaps which aren't cpg
150825985edcSLucas De Marchi 		 * aligned are prone to partial extension by a failed
15097bf72edeSMark Fasheh 		 * fs resize. If the file system resize never got to
15107bf72edeSMark Fasheh 		 * update the dinode cluster count, then we don't want
15117bf72edeSMark Fasheh 		 * to trust any clusters past it, regardless of what
15127bf72edeSMark Fasheh 		 * the group descriptor says. */
15137bf72edeSMark Fasheh 		gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
15147bf72edeSMark Fasheh 							  le64_to_cpu(gd->bg_blkno));
15157bf72edeSMark Fasheh 		if ((gd_cluster_off + max_bits) >
15167bf72edeSMark Fasheh 		    OCFS2_I(inode)->ip_clusters) {
15177bf72edeSMark Fasheh 			max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
15182f73e135STao Ma 			trace_ocfs2_cluster_group_search_wrong_max_bits(
15197bf72edeSMark Fasheh 				(unsigned long long)le64_to_cpu(gd->bg_blkno),
15207bf72edeSMark Fasheh 				le16_to_cpu(gd->bg_bits),
15217bf72edeSMark Fasheh 				OCFS2_I(inode)->ip_clusters, max_bits);
15227bf72edeSMark Fasheh 		}
15237bf72edeSMark Fasheh 
1524ccd979bdSMark Fasheh 		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1525ccd979bdSMark Fasheh 							group_bh, bits_wanted,
15267d1fe093SJoel Becker 							max_bits, res);
1527ccd979bdSMark Fasheh 		if (ret)
1528ccd979bdSMark Fasheh 			return ret;
1529ccd979bdSMark Fasheh 
15301187c968SJoel Becker 		if (max_block) {
15311187c968SJoel Becker 			blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
15321187c968SJoel Becker 							  gd_cluster_off +
15337d1fe093SJoel Becker 							  res->sr_bit_offset +
15347d1fe093SJoel Becker 							  res->sr_bits);
15352f73e135STao Ma 			trace_ocfs2_cluster_group_search_max_block(
15361187c968SJoel Becker 				(unsigned long long)blkoff,
15371187c968SJoel Becker 				(unsigned long long)max_block);
15381187c968SJoel Becker 			if (blkoff > max_block)
15391187c968SJoel Becker 				return -ENOSPC;
15401187c968SJoel Becker 		}
15411187c968SJoel Becker 
1542ccd979bdSMark Fasheh 		/* ocfs2_block_group_find_clear_bits() might
1543ccd979bdSMark Fasheh 		 * return success, but we still want to return
1544ccd979bdSMark Fasheh 		 * -ENOSPC unless it found the minimum number
1545ccd979bdSMark Fasheh 		 * of bits. */
15467d1fe093SJoel Becker 		if (min_bits <= res->sr_bits)
1547ccd979bdSMark Fasheh 			search = 0; /* success */
15487d1fe093SJoel Becker 		else if (res->sr_bits) {
15499c7af40bSMark Fasheh 			/*
15509c7af40bSMark Fasheh 			 * Don't show bits which we'll be returning
15519c7af40bSMark Fasheh 			 * for allocation to the local alloc bitmap.
15529c7af40bSMark Fasheh 			 */
15537d1fe093SJoel Becker 			ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
1554ccd979bdSMark Fasheh 		}
1555ccd979bdSMark Fasheh 	}
1556ccd979bdSMark Fasheh 
1557ccd979bdSMark Fasheh 	return search;
1558ccd979bdSMark Fasheh }
1559ccd979bdSMark Fasheh 
1560ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode,
1561ccd979bdSMark Fasheh 				    struct buffer_head *group_bh,
1562ccd979bdSMark Fasheh 				    u32 bits_wanted, u32 min_bits,
15631187c968SJoel Becker 				    u64 max_block,
15647d1fe093SJoel Becker 				    struct ocfs2_suballoc_result *res)
1565ccd979bdSMark Fasheh {
1566ccd979bdSMark Fasheh 	int ret = -ENOSPC;
15671187c968SJoel Becker 	u64 blkoff;
1568ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1569ccd979bdSMark Fasheh 
1570ccd979bdSMark Fasheh 	BUG_ON(min_bits != 1);
1571ccd979bdSMark Fasheh 	BUG_ON(ocfs2_is_cluster_bitmap(inode));
1572ccd979bdSMark Fasheh 
15731187c968SJoel Becker 	if (bg->bg_free_bits_count) {
1574ccd979bdSMark Fasheh 		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1575ccd979bdSMark Fasheh 							group_bh, bits_wanted,
15767bf72edeSMark Fasheh 							le16_to_cpu(bg->bg_bits),
15777d1fe093SJoel Becker 							res);
15781187c968SJoel Becker 		if (!ret && max_block) {
15797d1fe093SJoel Becker 			blkoff = le64_to_cpu(bg->bg_blkno) +
15807d1fe093SJoel Becker 				res->sr_bit_offset + res->sr_bits;
15812f73e135STao Ma 			trace_ocfs2_block_group_search_max_block(
15821187c968SJoel Becker 				(unsigned long long)blkoff,
15831187c968SJoel Becker 				(unsigned long long)max_block);
15841187c968SJoel Becker 			if (blkoff > max_block)
15851187c968SJoel Becker 				ret = -ENOSPC;
15861187c968SJoel Becker 		}
15871187c968SJoel Becker 	}
1588ccd979bdSMark Fasheh 
1589ccd979bdSMark Fasheh 	return ret;
1590ccd979bdSMark Fasheh }
1591ccd979bdSMark Fasheh 
15920a2fcd89SYounger Liu int ocfs2_alloc_dinode_update_counts(struct inode *inode,
15931fabe148SMark Fasheh 				       handle_t *handle,
1594883d4caeSMark Fasheh 				       struct buffer_head *di_bh,
1595883d4caeSMark Fasheh 				       u32 num_bits,
1596883d4caeSMark Fasheh 				       u16 chain)
1597883d4caeSMark Fasheh {
1598883d4caeSMark Fasheh 	int ret;
1599883d4caeSMark Fasheh 	u32 tmp_used;
1600883d4caeSMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1601883d4caeSMark Fasheh 	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1602883d4caeSMark Fasheh 
16030cf2f763SJoel Becker 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
1604883d4caeSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
1605883d4caeSMark Fasheh 	if (ret < 0) {
1606883d4caeSMark Fasheh 		mlog_errno(ret);
1607883d4caeSMark Fasheh 		goto out;
1608883d4caeSMark Fasheh 	}
1609883d4caeSMark Fasheh 
1610883d4caeSMark Fasheh 	tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1611883d4caeSMark Fasheh 	di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1612883d4caeSMark Fasheh 	le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1613ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, di_bh);
1614883d4caeSMark Fasheh 
1615883d4caeSMark Fasheh out:
1616883d4caeSMark Fasheh 	return ret;
1617883d4caeSMark Fasheh }
1618883d4caeSMark Fasheh 
1619db66c715SYounger Liu void ocfs2_rollback_alloc_dinode_counts(struct inode *inode,
1620db66c715SYounger Liu 				       struct buffer_head *di_bh,
1621db66c715SYounger Liu 				       u32 num_bits,
1622db66c715SYounger Liu 				       u16 chain)
1623db66c715SYounger Liu {
1624db66c715SYounger Liu 	u32 tmp_used;
1625db66c715SYounger Liu 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1626db66c715SYounger Liu 	struct ocfs2_chain_list *cl;
1627db66c715SYounger Liu 
1628db66c715SYounger Liu 	cl = (struct ocfs2_chain_list *)&di->id2.i_chain;
1629db66c715SYounger Liu 	tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1630db66c715SYounger Liu 	di->id1.bitmap1.i_used = cpu_to_le32(tmp_used - num_bits);
1631db66c715SYounger Liu 	le32_add_cpu(&cl->cl_recs[chain].c_free, num_bits);
1632db66c715SYounger Liu }
1633db66c715SYounger Liu 
1634ba206635SJoel Becker static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
163513e434cfSJoel Becker 					 struct ocfs2_extent_rec *rec,
163613e434cfSJoel Becker 					 struct ocfs2_chain_list *cl)
163713e434cfSJoel Becker {
163813e434cfSJoel Becker 	unsigned int bpc = le16_to_cpu(cl->cl_bpc);
163913e434cfSJoel Becker 	unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
164072094e43SAl Viro 	unsigned int bitcount = le16_to_cpu(rec->e_leaf_clusters) * bpc;
164113e434cfSJoel Becker 
164213e434cfSJoel Becker 	if (res->sr_bit_offset < bitoff)
164313e434cfSJoel Becker 		return 0;
164413e434cfSJoel Becker 	if (res->sr_bit_offset >= (bitoff + bitcount))
164513e434cfSJoel Becker 		return 0;
1646ba206635SJoel Becker 	res->sr_blkno = le64_to_cpu(rec->e_blkno) +
1647ba206635SJoel Becker 		(res->sr_bit_offset - bitoff);
164813e434cfSJoel Becker 	if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
164913e434cfSJoel Becker 		res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
165013e434cfSJoel Becker 	return 1;
165113e434cfSJoel Becker }
165213e434cfSJoel Becker 
1653ba206635SJoel Becker static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
165413e434cfSJoel Becker 					  struct ocfs2_group_desc *bg,
165513e434cfSJoel Becker 					  struct ocfs2_suballoc_result *res)
165613e434cfSJoel Becker {
165713e434cfSJoel Becker 	int i;
16582b6cb576SJoel Becker 	u64 bg_blkno = res->sr_bg_blkno;  /* Save off */
165913e434cfSJoel Becker 	struct ocfs2_extent_rec *rec;
166013e434cfSJoel Becker 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
166113e434cfSJoel Becker 	struct ocfs2_chain_list *cl = &di->id2.i_chain;
166213e434cfSJoel Becker 
1663ba206635SJoel Becker 	if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
1664ba206635SJoel Becker 		res->sr_blkno = 0;
166513e434cfSJoel Becker 		return;
1666ba206635SJoel Becker 	}
166713e434cfSJoel Becker 
1668ba206635SJoel Becker 	res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
16692b6cb576SJoel Becker 	res->sr_bg_blkno = 0;  /* Clear it for contig block groups */
16704711954eSTao Ma 	if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) ||
1671ba206635SJoel Becker 	    !bg->bg_list.l_next_free_rec)
167213e434cfSJoel Becker 		return;
167313e434cfSJoel Becker 
167413e434cfSJoel Becker 	for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
167513e434cfSJoel Becker 		rec = &bg->bg_list.l_recs[i];
16762b6cb576SJoel Becker 		if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) {
16772b6cb576SJoel Becker 			res->sr_bg_blkno = bg_blkno;  /* Restore */
167813e434cfSJoel Becker 			break;
167913e434cfSJoel Becker 		}
168013e434cfSJoel Becker 	}
16812b6cb576SJoel Becker }
168213e434cfSJoel Becker 
1683883d4caeSMark Fasheh static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
16841fabe148SMark Fasheh 				  handle_t *handle,
1685883d4caeSMark Fasheh 				  u32 bits_wanted,
1686883d4caeSMark Fasheh 				  u32 min_bits,
16877d1fe093SJoel Becker 				  struct ocfs2_suballoc_result *res,
1688883d4caeSMark Fasheh 				  u16 *bits_left)
1689883d4caeSMark Fasheh {
1690883d4caeSMark Fasheh 	int ret;
1691883d4caeSMark Fasheh 	struct buffer_head *group_bh = NULL;
1692883d4caeSMark Fasheh 	struct ocfs2_group_desc *gd;
169368f64d47SJoel Becker 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1694883d4caeSMark Fasheh 	struct inode *alloc_inode = ac->ac_inode;
1695883d4caeSMark Fasheh 
16967d1fe093SJoel Becker 	ret = ocfs2_read_group_descriptor(alloc_inode, di,
16977d1fe093SJoel Becker 					  res->sr_bg_blkno, &group_bh);
1698883d4caeSMark Fasheh 	if (ret < 0) {
1699883d4caeSMark Fasheh 		mlog_errno(ret);
1700883d4caeSMark Fasheh 		return ret;
1701883d4caeSMark Fasheh 	}
1702883d4caeSMark Fasheh 
1703883d4caeSMark Fasheh 	gd = (struct ocfs2_group_desc *) group_bh->b_data;
1704883d4caeSMark Fasheh 	ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
17057d1fe093SJoel Becker 				  ac->ac_max_block, res);
1706883d4caeSMark Fasheh 	if (ret < 0) {
1707883d4caeSMark Fasheh 		if (ret != -ENOSPC)
1708883d4caeSMark Fasheh 			mlog_errno(ret);
1709883d4caeSMark Fasheh 		goto out;
1710883d4caeSMark Fasheh 	}
1711883d4caeSMark Fasheh 
171213e434cfSJoel Becker 	if (!ret)
1713ba206635SJoel Becker 		ocfs2_bg_discontig_fix_result(ac, gd, res);
171413e434cfSJoel Becker 
1715e49e2767SMark Fasheh 	/*
1716e49e2767SMark Fasheh 	 * sr_bg_blkno might have been changed by
1717e49e2767SMark Fasheh 	 * ocfs2_bg_discontig_fix_result
1718e49e2767SMark Fasheh 	 */
1719e49e2767SMark Fasheh 	res->sr_bg_stable_blkno = group_bh->b_blocknr;
1720e49e2767SMark Fasheh 
1721e49e2767SMark Fasheh 	if (ac->ac_find_loc_only)
1722e49e2767SMark Fasheh 		goto out_loc_only;
1723e49e2767SMark Fasheh 
1724883d4caeSMark Fasheh 	ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
17257d1fe093SJoel Becker 					       res->sr_bits,
1726883d4caeSMark Fasheh 					       le16_to_cpu(gd->bg_chain));
1727883d4caeSMark Fasheh 	if (ret < 0) {
1728883d4caeSMark Fasheh 		mlog_errno(ret);
1729883d4caeSMark Fasheh 		goto out;
1730883d4caeSMark Fasheh 	}
1731883d4caeSMark Fasheh 
1732883d4caeSMark Fasheh 	ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
17337d1fe093SJoel Becker 					 res->sr_bit_offset, res->sr_bits);
1734db66c715SYounger Liu 	if (ret < 0) {
1735db66c715SYounger Liu 		ocfs2_rollback_alloc_dinode_counts(alloc_inode, ac->ac_bh,
1736db66c715SYounger Liu 					       res->sr_bits,
1737db66c715SYounger Liu 					       le16_to_cpu(gd->bg_chain));
1738883d4caeSMark Fasheh 		mlog_errno(ret);
1739db66c715SYounger Liu 	}
1740883d4caeSMark Fasheh 
1741e49e2767SMark Fasheh out_loc_only:
1742883d4caeSMark Fasheh 	*bits_left = le16_to_cpu(gd->bg_free_bits_count);
1743883d4caeSMark Fasheh 
1744883d4caeSMark Fasheh out:
1745883d4caeSMark Fasheh 	brelse(group_bh);
1746883d4caeSMark Fasheh 
1747883d4caeSMark Fasheh 	return ret;
1748883d4caeSMark Fasheh }
1749883d4caeSMark Fasheh 
1750ccd979bdSMark Fasheh static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
17511fabe148SMark Fasheh 			      handle_t *handle,
1752ccd979bdSMark Fasheh 			      u32 bits_wanted,
1753ccd979bdSMark Fasheh 			      u32 min_bits,
17547d1fe093SJoel Becker 			      struct ocfs2_suballoc_result *res,
1755883d4caeSMark Fasheh 			      u16 *bits_left)
1756ccd979bdSMark Fasheh {
1757ccd979bdSMark Fasheh 	int status;
17587d1fe093SJoel Becker 	u16 chain;
1759ccd979bdSMark Fasheh 	u64 next_group;
1760ccd979bdSMark Fasheh 	struct inode *alloc_inode = ac->ac_inode;
1761ccd979bdSMark Fasheh 	struct buffer_head *group_bh = NULL;
1762ccd979bdSMark Fasheh 	struct buffer_head *prev_group_bh = NULL;
1763ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1764ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1765ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg;
1766ccd979bdSMark Fasheh 
1767ccd979bdSMark Fasheh 	chain = ac->ac_chain;
17682f73e135STao Ma 	trace_ocfs2_search_chain_begin(
17692f73e135STao Ma 		(unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
17702f73e135STao Ma 		bits_wanted, chain);
1771ccd979bdSMark Fasheh 
177268f64d47SJoel Becker 	status = ocfs2_read_group_descriptor(alloc_inode, fe,
1773ccd979bdSMark Fasheh 					     le64_to_cpu(cl->cl_recs[chain].c_blkno),
17740fcaa56aSJoel Becker 					     &group_bh);
1775ccd979bdSMark Fasheh 	if (status < 0) {
1776ccd979bdSMark Fasheh 		mlog_errno(status);
1777ccd979bdSMark Fasheh 		goto bail;
1778ccd979bdSMark Fasheh 	}
1779ccd979bdSMark Fasheh 	bg = (struct ocfs2_group_desc *) group_bh->b_data;
1780ccd979bdSMark Fasheh 
1781ccd979bdSMark Fasheh 	status = -ENOSPC;
1782ccd979bdSMark Fasheh 	/* for now, the chain search is a bit simplistic. We just use
1783ccd979bdSMark Fasheh 	 * the 1st group with any empty bits. */
17841187c968SJoel Becker 	while ((status = ac->ac_group_search(alloc_inode, group_bh,
17851187c968SJoel Becker 					     bits_wanted, min_bits,
17867d1fe093SJoel Becker 					     ac->ac_max_block,
17877d1fe093SJoel Becker 					     res)) == -ENOSPC) {
1788ccd979bdSMark Fasheh 		if (!bg->bg_next_group)
1789ccd979bdSMark Fasheh 			break;
1790a81cb88bSMark Fasheh 
1791ccd979bdSMark Fasheh 		brelse(prev_group_bh);
1792ccd979bdSMark Fasheh 		prev_group_bh = NULL;
1793a81cb88bSMark Fasheh 
1794ccd979bdSMark Fasheh 		next_group = le64_to_cpu(bg->bg_next_group);
1795ccd979bdSMark Fasheh 		prev_group_bh = group_bh;
1796ccd979bdSMark Fasheh 		group_bh = NULL;
179768f64d47SJoel Becker 		status = ocfs2_read_group_descriptor(alloc_inode, fe,
17980fcaa56aSJoel Becker 						     next_group, &group_bh);
1799ccd979bdSMark Fasheh 		if (status < 0) {
1800ccd979bdSMark Fasheh 			mlog_errno(status);
1801ccd979bdSMark Fasheh 			goto bail;
1802ccd979bdSMark Fasheh 		}
1803ccd979bdSMark Fasheh 		bg = (struct ocfs2_group_desc *) group_bh->b_data;
1804ccd979bdSMark Fasheh 	}
1805ccd979bdSMark Fasheh 	if (status < 0) {
1806ccd979bdSMark Fasheh 		if (status != -ENOSPC)
1807ccd979bdSMark Fasheh 			mlog_errno(status);
1808ccd979bdSMark Fasheh 		goto bail;
1809ccd979bdSMark Fasheh 	}
1810ccd979bdSMark Fasheh 
18112f73e135STao Ma 	trace_ocfs2_search_chain_succ(
18122f73e135STao Ma 		(unsigned long long)le64_to_cpu(bg->bg_blkno), res->sr_bits);
1813ccd979bdSMark Fasheh 
18147d1fe093SJoel Becker 	res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
1815ccd979bdSMark Fasheh 
18167d1fe093SJoel Becker 	BUG_ON(res->sr_bits == 0);
181713e434cfSJoel Becker 	if (!status)
1818ba206635SJoel Becker 		ocfs2_bg_discontig_fix_result(ac, bg, res);
181913e434cfSJoel Becker 
1820e49e2767SMark Fasheh 	/*
1821e49e2767SMark Fasheh 	 * sr_bg_blkno might have been changed by
1822e49e2767SMark Fasheh 	 * ocfs2_bg_discontig_fix_result
1823e49e2767SMark Fasheh 	 */
1824e49e2767SMark Fasheh 	res->sr_bg_stable_blkno = group_bh->b_blocknr;
1825ccd979bdSMark Fasheh 
1826ccd979bdSMark Fasheh 	/*
1827ccd979bdSMark Fasheh 	 * Keep track of previous block descriptor read. When
1828ccd979bdSMark Fasheh 	 * we find a target, if we have read more than X
1829ccd979bdSMark Fasheh 	 * number of descriptors, and the target is reasonably
1830ccd979bdSMark Fasheh 	 * empty, relink him to top of his chain.
1831ccd979bdSMark Fasheh 	 *
1832ccd979bdSMark Fasheh 	 * We've read 0 extra blocks and only send one more to
1833ccd979bdSMark Fasheh 	 * the transaction, yet the next guy to search has a
1834ccd979bdSMark Fasheh 	 * much easier time.
1835ccd979bdSMark Fasheh 	 *
1836ccd979bdSMark Fasheh 	 * Do this *after* figuring out how many bits we're taking out
1837ccd979bdSMark Fasheh 	 * of our target group.
1838ccd979bdSMark Fasheh 	 */
1839309a85b6SXiaowei.Hu 	if (!ac->ac_disable_chain_relink &&
1840ccd979bdSMark Fasheh 	    (prev_group_bh) &&
18417d1fe093SJoel Becker 	    (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
1842ccd979bdSMark Fasheh 		status = ocfs2_relink_block_group(handle, alloc_inode,
1843ccd979bdSMark Fasheh 						  ac->ac_bh, group_bh,
1844ccd979bdSMark Fasheh 						  prev_group_bh, chain);
1845ccd979bdSMark Fasheh 		if (status < 0) {
1846ccd979bdSMark Fasheh 			mlog_errno(status);
1847ccd979bdSMark Fasheh 			goto bail;
1848ccd979bdSMark Fasheh 		}
1849ccd979bdSMark Fasheh 	}
1850ccd979bdSMark Fasheh 
1851e49e2767SMark Fasheh 	if (ac->ac_find_loc_only)
1852e49e2767SMark Fasheh 		goto out_loc_only;
1853e49e2767SMark Fasheh 
1854d5134982SMark Fasheh 	status = ocfs2_alloc_dinode_update_counts(alloc_inode, handle,
1855d5134982SMark Fasheh 						  ac->ac_bh, res->sr_bits,
1856d5134982SMark Fasheh 						  chain);
1857d5134982SMark Fasheh 	if (status) {
1858ccd979bdSMark Fasheh 		mlog_errno(status);
1859ccd979bdSMark Fasheh 		goto bail;
1860ccd979bdSMark Fasheh 	}
1861ccd979bdSMark Fasheh 
1862ccd979bdSMark Fasheh 	status = ocfs2_block_group_set_bits(handle,
1863ccd979bdSMark Fasheh 					    alloc_inode,
1864ccd979bdSMark Fasheh 					    bg,
1865ccd979bdSMark Fasheh 					    group_bh,
18667d1fe093SJoel Becker 					    res->sr_bit_offset,
18677d1fe093SJoel Becker 					    res->sr_bits);
1868ccd979bdSMark Fasheh 	if (status < 0) {
1869db66c715SYounger Liu 		ocfs2_rollback_alloc_dinode_counts(alloc_inode,
1870db66c715SYounger Liu 					ac->ac_bh, res->sr_bits, chain);
1871ccd979bdSMark Fasheh 		mlog_errno(status);
1872ccd979bdSMark Fasheh 		goto bail;
1873ccd979bdSMark Fasheh 	}
1874ccd979bdSMark Fasheh 
18752f73e135STao Ma 	trace_ocfs2_search_chain_end(
18762f73e135STao Ma 			(unsigned long long)le64_to_cpu(fe->i_blkno),
18772f73e135STao Ma 			res->sr_bits);
1878ccd979bdSMark Fasheh 
1879e49e2767SMark Fasheh out_loc_only:
1880883d4caeSMark Fasheh 	*bits_left = le16_to_cpu(bg->bg_free_bits_count);
1881ccd979bdSMark Fasheh bail:
1882ccd979bdSMark Fasheh 	brelse(group_bh);
1883ccd979bdSMark Fasheh 	brelse(prev_group_bh);
1884ccd979bdSMark Fasheh 
1885c1e8d35eSTao Ma 	if (status)
1886c1e8d35eSTao Ma 		mlog_errno(status);
1887ccd979bdSMark Fasheh 	return status;
1888ccd979bdSMark Fasheh }
1889ccd979bdSMark Fasheh 
1890ccd979bdSMark Fasheh /* will give out up to bits_wanted contiguous bits. */
1891aa8f8e93SJoel Becker static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
18921fabe148SMark Fasheh 				     handle_t *handle,
1893ccd979bdSMark Fasheh 				     u32 bits_wanted,
1894ccd979bdSMark Fasheh 				     u32 min_bits,
18957d1fe093SJoel Becker 				     struct ocfs2_suballoc_result *res)
1896ccd979bdSMark Fasheh {
1897ccd979bdSMark Fasheh 	int status;
1898ccd979bdSMark Fasheh 	u16 victim, i;
1899883d4caeSMark Fasheh 	u16 bits_left = 0;
1900b2b6ebf5SMark Fasheh 	u64 hint = ac->ac_last_group;
1901ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl;
1902ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
1903ccd979bdSMark Fasheh 
1904ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1905ccd979bdSMark Fasheh 	BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1906ccd979bdSMark Fasheh 	BUG_ON(!ac->ac_bh);
1907ccd979bdSMark Fasheh 
1908ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
190910995aa2SJoel Becker 
191010995aa2SJoel Becker 	/* The bh was validated by the inode read during
191110995aa2SJoel Becker 	 * ocfs2_reserve_suballoc_bits().  Any corruption is a code bug. */
191210995aa2SJoel Becker 	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
191310995aa2SJoel Becker 
1914ccd979bdSMark Fasheh 	if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1915ccd979bdSMark Fasheh 	    le32_to_cpu(fe->id1.bitmap1.i_total)) {
191617a5b9abSGoldwyn Rodrigues 		status = ocfs2_error(ac->ac_inode->i_sb,
19177ecef14aSJoe Perches 				     "Chain allocator dinode %llu has %u used bits but only %u total\n",
1918b0697053SMark Fasheh 				     (unsigned long long)le64_to_cpu(fe->i_blkno),
1919ccd979bdSMark Fasheh 				     le32_to_cpu(fe->id1.bitmap1.i_used),
1920ccd979bdSMark Fasheh 				     le32_to_cpu(fe->id1.bitmap1.i_total));
1921ccd979bdSMark Fasheh 		goto bail;
1922ccd979bdSMark Fasheh 	}
1923ccd979bdSMark Fasheh 
1924b2b6ebf5SMark Fasheh 	res->sr_bg_blkno = hint;
19257d1fe093SJoel Becker 	if (res->sr_bg_blkno) {
1926883d4caeSMark Fasheh 		/* Attempt to short-circuit the usual search mechanism
1927883d4caeSMark Fasheh 		 * by jumping straight to the most recently used
1928b595076aSUwe Kleine-König 		 * allocation group. This helps us maintain some
1929883d4caeSMark Fasheh 		 * contiguousness across allocations. */
1930da5cbf2fSMark Fasheh 		status = ocfs2_search_one_group(ac, handle, bits_wanted,
19317d1fe093SJoel Becker 						min_bits, res, &bits_left);
19327d1fe093SJoel Becker 		if (!status)
1933883d4caeSMark Fasheh 			goto set_hint;
1934883d4caeSMark Fasheh 		if (status < 0 && status != -ENOSPC) {
1935883d4caeSMark Fasheh 			mlog_errno(status);
1936883d4caeSMark Fasheh 			goto bail;
1937883d4caeSMark Fasheh 		}
1938883d4caeSMark Fasheh 	}
1939883d4caeSMark Fasheh 
1940ccd979bdSMark Fasheh 	cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1941ccd979bdSMark Fasheh 
1942ccd979bdSMark Fasheh 	victim = ocfs2_find_victim_chain(cl);
1943ccd979bdSMark Fasheh 	ac->ac_chain = victim;
1944ccd979bdSMark Fasheh 
19457d1fe093SJoel Becker 	status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
19467d1fe093SJoel Becker 				    res, &bits_left);
1947b2b6ebf5SMark Fasheh 	if (!status) {
19481d1aff8cSJoseph Qi 		if (ocfs2_is_cluster_bitmap(ac->ac_inode))
19491d1aff8cSJoseph Qi 			hint = res->sr_bg_blkno;
19501d1aff8cSJoseph Qi 		else
1951b2b6ebf5SMark Fasheh 			hint = ocfs2_group_from_res(res);
1952883d4caeSMark Fasheh 		goto set_hint;
1953b2b6ebf5SMark Fasheh 	}
1954ccd979bdSMark Fasheh 	if (status < 0 && status != -ENOSPC) {
1955ccd979bdSMark Fasheh 		mlog_errno(status);
1956ccd979bdSMark Fasheh 		goto bail;
1957ccd979bdSMark Fasheh 	}
1958ccd979bdSMark Fasheh 
19592f73e135STao Ma 	trace_ocfs2_claim_suballoc_bits(victim);
1960ccd979bdSMark Fasheh 
1961ccd979bdSMark Fasheh 	/* If we didn't pick a good victim, then just default to
1962ccd979bdSMark Fasheh 	 * searching each chain in order. Don't allow chain relinking
1963ccd979bdSMark Fasheh 	 * because we only calculate enough journal credits for one
1964ccd979bdSMark Fasheh 	 * relink per alloc. */
1965309a85b6SXiaowei.Hu 	ac->ac_disable_chain_relink = 1;
1966ccd979bdSMark Fasheh 	for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1967ccd979bdSMark Fasheh 		if (i == victim)
1968ccd979bdSMark Fasheh 			continue;
1969ccd979bdSMark Fasheh 		if (!cl->cl_recs[i].c_free)
1970ccd979bdSMark Fasheh 			continue;
1971ccd979bdSMark Fasheh 
1972ccd979bdSMark Fasheh 		ac->ac_chain = i;
1973da5cbf2fSMark Fasheh 		status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
19747d1fe093SJoel Becker 					    res, &bits_left);
1975b2b6ebf5SMark Fasheh 		if (!status) {
1976b2b6ebf5SMark Fasheh 			hint = ocfs2_group_from_res(res);
1977ccd979bdSMark Fasheh 			break;
1978b2b6ebf5SMark Fasheh 		}
1979ccd979bdSMark Fasheh 		if (status < 0 && status != -ENOSPC) {
1980ccd979bdSMark Fasheh 			mlog_errno(status);
1981ccd979bdSMark Fasheh 			goto bail;
1982ccd979bdSMark Fasheh 		}
1983ccd979bdSMark Fasheh 	}
1984ccd979bdSMark Fasheh 
1985883d4caeSMark Fasheh set_hint:
1986883d4caeSMark Fasheh 	if (status != -ENOSPC) {
1987883d4caeSMark Fasheh 		/* If the next search of this group is not likely to
1988883d4caeSMark Fasheh 		 * yield a suitable extent, then we reset the last
1989883d4caeSMark Fasheh 		 * group hint so as to not waste a disk read */
1990883d4caeSMark Fasheh 		if (bits_left < min_bits)
1991883d4caeSMark Fasheh 			ac->ac_last_group = 0;
1992883d4caeSMark Fasheh 		else
1993b2b6ebf5SMark Fasheh 			ac->ac_last_group = hint;
1994883d4caeSMark Fasheh 	}
1995883d4caeSMark Fasheh 
1996883d4caeSMark Fasheh bail:
1997c1e8d35eSTao Ma 	if (status)
1998c1e8d35eSTao Ma 		mlog_errno(status);
1999ccd979bdSMark Fasheh 	return status;
2000ccd979bdSMark Fasheh }
2001ccd979bdSMark Fasheh 
20021ed9b777SJoel Becker int ocfs2_claim_metadata(handle_t *handle,
2003ccd979bdSMark Fasheh 			 struct ocfs2_alloc_context *ac,
2004ccd979bdSMark Fasheh 			 u32 bits_wanted,
20052b6cb576SJoel Becker 			 u64 *suballoc_loc,
2006ccd979bdSMark Fasheh 			 u16 *suballoc_bit_start,
2007ccd979bdSMark Fasheh 			 unsigned int *num_bits,
2008ccd979bdSMark Fasheh 			 u64 *blkno_start)
2009ccd979bdSMark Fasheh {
2010ccd979bdSMark Fasheh 	int status;
2011ba206635SJoel Becker 	struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
2012ccd979bdSMark Fasheh 
2013ccd979bdSMark Fasheh 	BUG_ON(!ac);
2014ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
2015ccd979bdSMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
2016ccd979bdSMark Fasheh 
2017aa8f8e93SJoel Becker 	status = ocfs2_claim_suballoc_bits(ac,
2018da5cbf2fSMark Fasheh 					   handle,
2019ccd979bdSMark Fasheh 					   bits_wanted,
2020ccd979bdSMark Fasheh 					   1,
20217d1fe093SJoel Becker 					   &res);
2022ccd979bdSMark Fasheh 	if (status < 0) {
2023ccd979bdSMark Fasheh 		mlog_errno(status);
2024ccd979bdSMark Fasheh 		goto bail;
2025ccd979bdSMark Fasheh 	}
20261ed9b777SJoel Becker 	atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
2027ccd979bdSMark Fasheh 
20282b6cb576SJoel Becker 	*suballoc_loc = res.sr_bg_blkno;
20297d1fe093SJoel Becker 	*suballoc_bit_start = res.sr_bit_offset;
2030ba206635SJoel Becker 	*blkno_start = res.sr_blkno;
20317d1fe093SJoel Becker 	ac->ac_bits_given += res.sr_bits;
20327d1fe093SJoel Becker 	*num_bits = res.sr_bits;
2033ccd979bdSMark Fasheh 	status = 0;
2034ccd979bdSMark Fasheh bail:
2035c1e8d35eSTao Ma 	if (status)
2036c1e8d35eSTao Ma 		mlog_errno(status);
2037ccd979bdSMark Fasheh 	return status;
2038ccd979bdSMark Fasheh }
2039ccd979bdSMark Fasheh 
204013821151STao Ma static void ocfs2_init_inode_ac_group(struct inode *dir,
2041abf1b3cbSTao Ma 				      struct buffer_head *parent_di_bh,
204213821151STao Ma 				      struct ocfs2_alloc_context *ac)
204313821151STao Ma {
2044abf1b3cbSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data;
204513821151STao Ma 	/*
204613821151STao Ma 	 * Try to allocate inodes from some specific group.
204713821151STao Ma 	 *
204813821151STao Ma 	 * If the parent dir has recorded the last group used in allocation,
204913821151STao Ma 	 * cool, use it. Otherwise if we try to allocate new inode from the
205013821151STao Ma 	 * same slot the parent dir belongs to, use the same chunk.
205113821151STao Ma 	 *
205213821151STao Ma 	 * We are very careful here to avoid the mistake of setting
205313821151STao Ma 	 * ac_last_group to a group descriptor from a different (unlocked) slot.
205413821151STao Ma 	 */
205513821151STao Ma 	if (OCFS2_I(dir)->ip_last_used_group &&
205613821151STao Ma 	    OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
205713821151STao Ma 		ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
2058abf1b3cbSTao Ma 	else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) {
2059abf1b3cbSTao Ma 		if (di->i_suballoc_loc)
2060abf1b3cbSTao Ma 			ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc);
2061abf1b3cbSTao Ma 		else
206213821151STao Ma 			ac->ac_last_group = ocfs2_which_suballoc_group(
2063abf1b3cbSTao Ma 					le64_to_cpu(di->i_blkno),
2064abf1b3cbSTao Ma 					le16_to_cpu(di->i_suballoc_bit));
2065abf1b3cbSTao Ma 	}
206613821151STao Ma }
206713821151STao Ma 
206813821151STao Ma static inline void ocfs2_save_inode_ac_group(struct inode *dir,
206913821151STao Ma 					     struct ocfs2_alloc_context *ac)
207013821151STao Ma {
207113821151STao Ma 	OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
207213821151STao Ma 	OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
207313821151STao Ma }
207413821151STao Ma 
2075e49e2767SMark Fasheh int ocfs2_find_new_inode_loc(struct inode *dir,
2076e49e2767SMark Fasheh 			     struct buffer_head *parent_fe_bh,
2077e49e2767SMark Fasheh 			     struct ocfs2_alloc_context *ac,
2078e49e2767SMark Fasheh 			     u64 *fe_blkno)
2079e49e2767SMark Fasheh {
2080e49e2767SMark Fasheh 	int ret;
2081e49e2767SMark Fasheh 	handle_t *handle = NULL;
2082e49e2767SMark Fasheh 	struct ocfs2_suballoc_result *res;
2083e49e2767SMark Fasheh 
2084e49e2767SMark Fasheh 	BUG_ON(!ac);
2085e49e2767SMark Fasheh 	BUG_ON(ac->ac_bits_given != 0);
2086e49e2767SMark Fasheh 	BUG_ON(ac->ac_bits_wanted != 1);
2087e49e2767SMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
2088e49e2767SMark Fasheh 
2089e49e2767SMark Fasheh 	res = kzalloc(sizeof(*res), GFP_NOFS);
2090e49e2767SMark Fasheh 	if (res == NULL) {
2091e49e2767SMark Fasheh 		ret = -ENOMEM;
2092e49e2767SMark Fasheh 		mlog_errno(ret);
2093e49e2767SMark Fasheh 		goto out;
2094e49e2767SMark Fasheh 	}
2095e49e2767SMark Fasheh 
2096e49e2767SMark Fasheh 	ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
2097e49e2767SMark Fasheh 
2098e49e2767SMark Fasheh 	/*
2099e49e2767SMark Fasheh 	 * The handle started here is for chain relink. Alternatively,
2100e49e2767SMark Fasheh 	 * we could just disable relink for these calls.
2101e49e2767SMark Fasheh 	 */
2102e49e2767SMark Fasheh 	handle = ocfs2_start_trans(OCFS2_SB(dir->i_sb), OCFS2_SUBALLOC_ALLOC);
2103e49e2767SMark Fasheh 	if (IS_ERR(handle)) {
2104e49e2767SMark Fasheh 		ret = PTR_ERR(handle);
2105e49e2767SMark Fasheh 		handle = NULL;
2106e49e2767SMark Fasheh 		mlog_errno(ret);
2107e49e2767SMark Fasheh 		goto out;
2108e49e2767SMark Fasheh 	}
2109e49e2767SMark Fasheh 
2110e49e2767SMark Fasheh 	/*
2111e49e2767SMark Fasheh 	 * This will instruct ocfs2_claim_suballoc_bits and
2112e49e2767SMark Fasheh 	 * ocfs2_search_one_group to search but save actual allocation
2113e49e2767SMark Fasheh 	 * for later.
2114e49e2767SMark Fasheh 	 */
2115e49e2767SMark Fasheh 	ac->ac_find_loc_only = 1;
2116e49e2767SMark Fasheh 
2117e49e2767SMark Fasheh 	ret = ocfs2_claim_suballoc_bits(ac, handle, 1, 1, res);
2118e49e2767SMark Fasheh 	if (ret < 0) {
2119e49e2767SMark Fasheh 		mlog_errno(ret);
2120e49e2767SMark Fasheh 		goto out;
2121e49e2767SMark Fasheh 	}
2122e49e2767SMark Fasheh 
2123e49e2767SMark Fasheh 	ac->ac_find_loc_priv = res;
2124e49e2767SMark Fasheh 	*fe_blkno = res->sr_blkno;
21256fdb702dSDarrick J. Wong 	ocfs2_update_inode_fsync_trans(handle, dir, 0);
2126e49e2767SMark Fasheh out:
2127e49e2767SMark Fasheh 	if (handle)
2128e49e2767SMark Fasheh 		ocfs2_commit_trans(OCFS2_SB(dir->i_sb), handle);
2129e49e2767SMark Fasheh 
2130e49e2767SMark Fasheh 	if (ret)
2131e49e2767SMark Fasheh 		kfree(res);
2132e49e2767SMark Fasheh 
2133e49e2767SMark Fasheh 	return ret;
2134e49e2767SMark Fasheh }
2135e49e2767SMark Fasheh 
2136e49e2767SMark Fasheh int ocfs2_claim_new_inode_at_loc(handle_t *handle,
2137e49e2767SMark Fasheh 				 struct inode *dir,
2138e49e2767SMark Fasheh 				 struct ocfs2_alloc_context *ac,
2139e49e2767SMark Fasheh 				 u64 *suballoc_loc,
2140e49e2767SMark Fasheh 				 u16 *suballoc_bit,
2141e49e2767SMark Fasheh 				 u64 di_blkno)
2142e49e2767SMark Fasheh {
2143e49e2767SMark Fasheh 	int ret;
2144e49e2767SMark Fasheh 	u16 chain;
2145e49e2767SMark Fasheh 	struct ocfs2_suballoc_result *res = ac->ac_find_loc_priv;
2146e49e2767SMark Fasheh 	struct buffer_head *bg_bh = NULL;
2147e49e2767SMark Fasheh 	struct ocfs2_group_desc *bg;
2148e49e2767SMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) ac->ac_bh->b_data;
2149e49e2767SMark Fasheh 
2150e49e2767SMark Fasheh 	/*
2151e49e2767SMark Fasheh 	 * Since di_blkno is being passed back in, we check for any
2152e49e2767SMark Fasheh 	 * inconsistencies which may have happened between
2153e49e2767SMark Fasheh 	 * calls. These are code bugs as di_blkno is not expected to
2154e49e2767SMark Fasheh 	 * change once returned from ocfs2_find_new_inode_loc()
2155e49e2767SMark Fasheh 	 */
2156e49e2767SMark Fasheh 	BUG_ON(res->sr_blkno != di_blkno);
2157e49e2767SMark Fasheh 
2158e49e2767SMark Fasheh 	ret = ocfs2_read_group_descriptor(ac->ac_inode, di,
2159e49e2767SMark Fasheh 					  res->sr_bg_stable_blkno, &bg_bh);
2160e49e2767SMark Fasheh 	if (ret) {
2161e49e2767SMark Fasheh 		mlog_errno(ret);
2162e49e2767SMark Fasheh 		goto out;
2163e49e2767SMark Fasheh 	}
2164e49e2767SMark Fasheh 
2165e49e2767SMark Fasheh 	bg = (struct ocfs2_group_desc *) bg_bh->b_data;
2166e49e2767SMark Fasheh 	chain = le16_to_cpu(bg->bg_chain);
2167e49e2767SMark Fasheh 
2168e49e2767SMark Fasheh 	ret = ocfs2_alloc_dinode_update_counts(ac->ac_inode, handle,
2169e49e2767SMark Fasheh 					       ac->ac_bh, res->sr_bits,
2170e49e2767SMark Fasheh 					       chain);
2171e49e2767SMark Fasheh 	if (ret) {
2172e49e2767SMark Fasheh 		mlog_errno(ret);
2173e49e2767SMark Fasheh 		goto out;
2174e49e2767SMark Fasheh 	}
2175e49e2767SMark Fasheh 
2176e49e2767SMark Fasheh 	ret = ocfs2_block_group_set_bits(handle,
2177e49e2767SMark Fasheh 					 ac->ac_inode,
2178e49e2767SMark Fasheh 					 bg,
2179e49e2767SMark Fasheh 					 bg_bh,
2180e49e2767SMark Fasheh 					 res->sr_bit_offset,
2181e49e2767SMark Fasheh 					 res->sr_bits);
2182e49e2767SMark Fasheh 	if (ret < 0) {
2183db66c715SYounger Liu 		ocfs2_rollback_alloc_dinode_counts(ac->ac_inode,
2184db66c715SYounger Liu 					       ac->ac_bh, res->sr_bits, chain);
2185e49e2767SMark Fasheh 		mlog_errno(ret);
2186e49e2767SMark Fasheh 		goto out;
2187e49e2767SMark Fasheh 	}
2188e49e2767SMark Fasheh 
21892f73e135STao Ma 	trace_ocfs2_claim_new_inode_at_loc((unsigned long long)di_blkno,
21902f73e135STao Ma 					   res->sr_bits);
2191e49e2767SMark Fasheh 
2192e49e2767SMark Fasheh 	atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
2193e49e2767SMark Fasheh 
2194e49e2767SMark Fasheh 	BUG_ON(res->sr_bits != 1);
2195e49e2767SMark Fasheh 
2196e49e2767SMark Fasheh 	*suballoc_loc = res->sr_bg_blkno;
2197e49e2767SMark Fasheh 	*suballoc_bit = res->sr_bit_offset;
2198e49e2767SMark Fasheh 	ac->ac_bits_given++;
2199e49e2767SMark Fasheh 	ocfs2_save_inode_ac_group(dir, ac);
2200e49e2767SMark Fasheh 
2201e49e2767SMark Fasheh out:
2202e49e2767SMark Fasheh 	brelse(bg_bh);
2203e49e2767SMark Fasheh 
2204e49e2767SMark Fasheh 	return ret;
2205e49e2767SMark Fasheh }
2206e49e2767SMark Fasheh 
22071ed9b777SJoel Becker int ocfs2_claim_new_inode(handle_t *handle,
220813821151STao Ma 			  struct inode *dir,
220913821151STao Ma 			  struct buffer_head *parent_fe_bh,
2210ccd979bdSMark Fasheh 			  struct ocfs2_alloc_context *ac,
22112b6cb576SJoel Becker 			  u64 *suballoc_loc,
2212ccd979bdSMark Fasheh 			  u16 *suballoc_bit,
2213ccd979bdSMark Fasheh 			  u64 *fe_blkno)
2214ccd979bdSMark Fasheh {
2215ccd979bdSMark Fasheh 	int status;
22162b6cb576SJoel Becker 	struct ocfs2_suballoc_result res;
2217ccd979bdSMark Fasheh 
2218ccd979bdSMark Fasheh 	BUG_ON(!ac);
2219ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_given != 0);
2220ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_wanted != 1);
2221ccd979bdSMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
2222ccd979bdSMark Fasheh 
222313821151STao Ma 	ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
222413821151STao Ma 
2225aa8f8e93SJoel Becker 	status = ocfs2_claim_suballoc_bits(ac,
2226da5cbf2fSMark Fasheh 					   handle,
2227ccd979bdSMark Fasheh 					   1,
2228ccd979bdSMark Fasheh 					   1,
22297d1fe093SJoel Becker 					   &res);
2230ccd979bdSMark Fasheh 	if (status < 0) {
2231ccd979bdSMark Fasheh 		mlog_errno(status);
2232ccd979bdSMark Fasheh 		goto bail;
2233ccd979bdSMark Fasheh 	}
22341ed9b777SJoel Becker 	atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
2235ccd979bdSMark Fasheh 
22367d1fe093SJoel Becker 	BUG_ON(res.sr_bits != 1);
2237ccd979bdSMark Fasheh 
22382b6cb576SJoel Becker 	*suballoc_loc = res.sr_bg_blkno;
22397d1fe093SJoel Becker 	*suballoc_bit = res.sr_bit_offset;
2240ba206635SJoel Becker 	*fe_blkno = res.sr_blkno;
2241ccd979bdSMark Fasheh 	ac->ac_bits_given++;
224213821151STao Ma 	ocfs2_save_inode_ac_group(dir, ac);
2243ccd979bdSMark Fasheh 	status = 0;
2244ccd979bdSMark Fasheh bail:
2245c1e8d35eSTao Ma 	if (status)
2246c1e8d35eSTao Ma 		mlog_errno(status);
2247ccd979bdSMark Fasheh 	return status;
2248ccd979bdSMark Fasheh }
2249ccd979bdSMark Fasheh 
2250ccd979bdSMark Fasheh /* translate a group desc. blkno and it's bitmap offset into
2251ccd979bdSMark Fasheh  * disk cluster offset. */
2252ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
2253ccd979bdSMark Fasheh 						   u64 bg_blkno,
2254ccd979bdSMark Fasheh 						   u16 bg_bit_off)
2255ccd979bdSMark Fasheh {
2256ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2257ccd979bdSMark Fasheh 	u32 cluster = 0;
2258ccd979bdSMark Fasheh 
2259ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2260ccd979bdSMark Fasheh 
2261ccd979bdSMark Fasheh 	if (bg_blkno != osb->first_cluster_group_blkno)
2262ccd979bdSMark Fasheh 		cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
2263ccd979bdSMark Fasheh 	cluster += (u32) bg_bit_off;
2264ccd979bdSMark Fasheh 	return cluster;
2265ccd979bdSMark Fasheh }
2266ccd979bdSMark Fasheh 
2267ccd979bdSMark Fasheh /* given a cluster offset, calculate which block group it belongs to
2268ccd979bdSMark Fasheh  * and return that block offset. */
2269d659072fSTao Ma u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
2270ccd979bdSMark Fasheh {
2271ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2272ccd979bdSMark Fasheh 	u32 group_no;
2273ccd979bdSMark Fasheh 
2274ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2275ccd979bdSMark Fasheh 
2276ccd979bdSMark Fasheh 	group_no = cluster / osb->bitmap_cpg;
2277ccd979bdSMark Fasheh 	if (!group_no)
2278ccd979bdSMark Fasheh 		return osb->first_cluster_group_blkno;
2279ccd979bdSMark Fasheh 	return ocfs2_clusters_to_blocks(inode->i_sb,
2280ccd979bdSMark Fasheh 					group_no * osb->bitmap_cpg);
2281ccd979bdSMark Fasheh }
2282ccd979bdSMark Fasheh 
2283ccd979bdSMark Fasheh /* given the block number of a cluster start, calculate which cluster
2284ccd979bdSMark Fasheh  * group and descriptor bitmap offset that corresponds to. */
2285ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode,
2286ccd979bdSMark Fasheh 						u64 data_blkno,
2287ccd979bdSMark Fasheh 						u64 *bg_blkno,
2288ccd979bdSMark Fasheh 						u16 *bg_bit_off)
2289ccd979bdSMark Fasheh {
2290ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2291ccd979bdSMark Fasheh 	u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
2292ccd979bdSMark Fasheh 
2293ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2294ccd979bdSMark Fasheh 
2295ccd979bdSMark Fasheh 	*bg_blkno = ocfs2_which_cluster_group(inode,
2296ccd979bdSMark Fasheh 					      data_cluster);
2297ccd979bdSMark Fasheh 
2298ccd979bdSMark Fasheh 	if (*bg_blkno == osb->first_cluster_group_blkno)
2299ccd979bdSMark Fasheh 		*bg_bit_off = (u16) data_cluster;
2300ccd979bdSMark Fasheh 	else
2301ccd979bdSMark Fasheh 		*bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
2302ccd979bdSMark Fasheh 							     data_blkno - *bg_blkno);
2303ccd979bdSMark Fasheh }
2304ccd979bdSMark Fasheh 
2305ccd979bdSMark Fasheh /*
2306ccd979bdSMark Fasheh  * min_bits - minimum contiguous chunk from this total allocation we
2307ccd979bdSMark Fasheh  * can handle. set to what we asked for originally for a full
2308ccd979bdSMark Fasheh  * contig. allocation, set to '1' to indicate we can deal with extents
2309ccd979bdSMark Fasheh  * of any size.
2310ccd979bdSMark Fasheh  */
23111ed9b777SJoel Becker int __ocfs2_claim_clusters(handle_t *handle,
2312ccd979bdSMark Fasheh 			   struct ocfs2_alloc_context *ac,
2313ccd979bdSMark Fasheh 			   u32 min_clusters,
2314415cb800SMark Fasheh 			   u32 max_clusters,
2315ccd979bdSMark Fasheh 			   u32 *cluster_start,
2316ccd979bdSMark Fasheh 			   u32 *num_clusters)
2317ccd979bdSMark Fasheh {
2318ccd979bdSMark Fasheh 	int status;
2319415cb800SMark Fasheh 	unsigned int bits_wanted = max_clusters;
2320ba206635SJoel Becker 	struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
23211ed9b777SJoel Becker 	struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
2322ccd979bdSMark Fasheh 
2323ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
2324ccd979bdSMark Fasheh 
2325ccd979bdSMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
2326ccd979bdSMark Fasheh 	       && ac->ac_which != OCFS2_AC_USE_MAIN);
2327ccd979bdSMark Fasheh 
2328ccd979bdSMark Fasheh 	if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
232933d5d380SMark Fasheh 		WARN_ON(min_clusters > 1);
233033d5d380SMark Fasheh 
2331ccd979bdSMark Fasheh 		status = ocfs2_claim_local_alloc_bits(osb,
2332ccd979bdSMark Fasheh 						      handle,
2333ccd979bdSMark Fasheh 						      ac,
2334ccd979bdSMark Fasheh 						      bits_wanted,
2335ccd979bdSMark Fasheh 						      cluster_start,
2336ccd979bdSMark Fasheh 						      num_clusters);
2337ccd979bdSMark Fasheh 		if (!status)
2338ccd979bdSMark Fasheh 			atomic_inc(&osb->alloc_stats.local_data);
2339ccd979bdSMark Fasheh 	} else {
2340ccd979bdSMark Fasheh 		if (min_clusters > (osb->bitmap_cpg - 1)) {
2341ccd979bdSMark Fasheh 			/* The only paths asking for contiguousness
2342ccd979bdSMark Fasheh 			 * should know about this already. */
23432fbe8d1eSSunil Mushran 			mlog(ML_ERROR, "minimum allocation requested %u exceeds "
23442fbe8d1eSSunil Mushran 			     "group bitmap size %u!\n", min_clusters,
23452fbe8d1eSSunil Mushran 			     osb->bitmap_cpg);
2346ccd979bdSMark Fasheh 			status = -ENOSPC;
2347ccd979bdSMark Fasheh 			goto bail;
2348ccd979bdSMark Fasheh 		}
2349ccd979bdSMark Fasheh 		/* clamp the current request down to a realistic size. */
2350ccd979bdSMark Fasheh 		if (bits_wanted > (osb->bitmap_cpg - 1))
2351ccd979bdSMark Fasheh 			bits_wanted = osb->bitmap_cpg - 1;
2352ccd979bdSMark Fasheh 
2353aa8f8e93SJoel Becker 		status = ocfs2_claim_suballoc_bits(ac,
2354da5cbf2fSMark Fasheh 						   handle,
2355ccd979bdSMark Fasheh 						   bits_wanted,
2356ccd979bdSMark Fasheh 						   min_clusters,
23577d1fe093SJoel Becker 						   &res);
2358ccd979bdSMark Fasheh 		if (!status) {
2359ba206635SJoel Becker 			BUG_ON(res.sr_blkno); /* cluster alloc can't set */
2360ccd979bdSMark Fasheh 			*cluster_start =
2361ccd979bdSMark Fasheh 				ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
23627d1fe093SJoel Becker 								 res.sr_bg_blkno,
23637d1fe093SJoel Becker 								 res.sr_bit_offset);
2364ccd979bdSMark Fasheh 			atomic_inc(&osb->alloc_stats.bitmap_data);
23654711954eSTao Ma 			*num_clusters = res.sr_bits;
2366ccd979bdSMark Fasheh 		}
2367ccd979bdSMark Fasheh 	}
2368ccd979bdSMark Fasheh 	if (status < 0) {
2369ccd979bdSMark Fasheh 		if (status != -ENOSPC)
2370ccd979bdSMark Fasheh 			mlog_errno(status);
2371ccd979bdSMark Fasheh 		goto bail;
2372ccd979bdSMark Fasheh 	}
2373ccd979bdSMark Fasheh 
23744711954eSTao Ma 	ac->ac_bits_given += *num_clusters;
2375ccd979bdSMark Fasheh 
2376ccd979bdSMark Fasheh bail:
2377c1e8d35eSTao Ma 	if (status)
2378c1e8d35eSTao Ma 		mlog_errno(status);
2379ccd979bdSMark Fasheh 	return status;
2380ccd979bdSMark Fasheh }
2381ccd979bdSMark Fasheh 
23821ed9b777SJoel Becker int ocfs2_claim_clusters(handle_t *handle,
2383415cb800SMark Fasheh 			 struct ocfs2_alloc_context *ac,
2384415cb800SMark Fasheh 			 u32 min_clusters,
2385415cb800SMark Fasheh 			 u32 *cluster_start,
2386415cb800SMark Fasheh 			 u32 *num_clusters)
2387415cb800SMark Fasheh {
2388415cb800SMark Fasheh 	unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
2389415cb800SMark Fasheh 
23901ed9b777SJoel Becker 	return __ocfs2_claim_clusters(handle, ac, min_clusters,
2391415cb800SMark Fasheh 				      bits_wanted, cluster_start, num_clusters);
2392415cb800SMark Fasheh }
2393415cb800SMark Fasheh 
2394b4414eeaSMark Fasheh static int ocfs2_block_group_clear_bits(handle_t *handle,
2395ccd979bdSMark Fasheh 					struct inode *alloc_inode,
2396ccd979bdSMark Fasheh 					struct ocfs2_group_desc *bg,
2397ccd979bdSMark Fasheh 					struct buffer_head *group_bh,
2398ccd979bdSMark Fasheh 					unsigned int bit_off,
2399b4414eeaSMark Fasheh 					unsigned int num_bits,
2400b4414eeaSMark Fasheh 					void (*undo_fn)(unsigned int bit,
2401b4414eeaSMark Fasheh 							unsigned long *bmap))
2402ccd979bdSMark Fasheh {
2403ccd979bdSMark Fasheh 	int status;
2404ccd979bdSMark Fasheh 	unsigned int tmp;
2405ccd979bdSMark Fasheh 	struct ocfs2_group_desc *undo_bg = NULL;
2406ccd979bdSMark Fasheh 
240742035306SJoel Becker 	/* The caller got this descriptor from
240842035306SJoel Becker 	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
240942035306SJoel Becker 	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
2410ccd979bdSMark Fasheh 
24112f73e135STao Ma 	trace_ocfs2_block_group_clear_bits(bit_off, num_bits);
2412ccd979bdSMark Fasheh 
2413b4414eeaSMark Fasheh 	BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
24140cf2f763SJoel Becker 	status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
2415b4414eeaSMark Fasheh 					 group_bh,
2416b4414eeaSMark Fasheh 					 undo_fn ?
2417b4414eeaSMark Fasheh 					 OCFS2_JOURNAL_ACCESS_UNDO :
2418b4414eeaSMark Fasheh 					 OCFS2_JOURNAL_ACCESS_WRITE);
2419ccd979bdSMark Fasheh 	if (status < 0) {
2420ccd979bdSMark Fasheh 		mlog_errno(status);
2421ccd979bdSMark Fasheh 		goto bail;
2422ccd979bdSMark Fasheh 	}
2423ccd979bdSMark Fasheh 
2424b4414eeaSMark Fasheh 	if (undo_fn) {
242594e41ecfSSunil Mushran 		jbd_lock_bh_state(group_bh);
242694e41ecfSSunil Mushran 		undo_bg = (struct ocfs2_group_desc *)
242794e41ecfSSunil Mushran 					bh2jh(group_bh)->b_committed_data;
242894e41ecfSSunil Mushran 		BUG_ON(!undo_bg);
242994e41ecfSSunil Mushran 	}
2430ccd979bdSMark Fasheh 
2431ccd979bdSMark Fasheh 	tmp = num_bits;
2432ccd979bdSMark Fasheh 	while(tmp--) {
2433ccd979bdSMark Fasheh 		ocfs2_clear_bit((bit_off + tmp),
2434ccd979bdSMark Fasheh 				(unsigned long *) bg->bg_bitmap);
2435b4414eeaSMark Fasheh 		if (undo_fn)
2436b4414eeaSMark Fasheh 			undo_fn(bit_off + tmp,
2437ccd979bdSMark Fasheh 				(unsigned long *) undo_bg->bg_bitmap);
2438ccd979bdSMark Fasheh 	}
2439ccd979bdSMark Fasheh 	le16_add_cpu(&bg->bg_free_bits_count, num_bits);
24409b5cd10eSSrinivas Eeda 	if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
24417ecef14aSJoe Perches 		return ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit count %u but claims %u are freed. num_bits %d\n",
24429b5cd10eSSrinivas Eeda 				   (unsigned long long)le64_to_cpu(bg->bg_blkno),
24439b5cd10eSSrinivas Eeda 				   le16_to_cpu(bg->bg_bits),
24447ecef14aSJoe Perches 				   le16_to_cpu(bg->bg_free_bits_count),
24457ecef14aSJoe Perches 				   num_bits);
24469b5cd10eSSrinivas Eeda 	}
2447ccd979bdSMark Fasheh 
2448b4414eeaSMark Fasheh 	if (undo_fn)
244994e41ecfSSunil Mushran 		jbd_unlock_bh_state(group_bh);
245094e41ecfSSunil Mushran 
2451ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, group_bh);
2452ccd979bdSMark Fasheh bail:
2453ccd979bdSMark Fasheh 	return status;
2454ccd979bdSMark Fasheh }
2455ccd979bdSMark Fasheh 
2456ccd979bdSMark Fasheh /*
2457ccd979bdSMark Fasheh  * expects the suballoc inode to already be locked.
2458ccd979bdSMark Fasheh  */
2459b4414eeaSMark Fasheh static int _ocfs2_free_suballoc_bits(handle_t *handle,
2460ccd979bdSMark Fasheh 				     struct inode *alloc_inode,
2461ccd979bdSMark Fasheh 				     struct buffer_head *alloc_bh,
2462ccd979bdSMark Fasheh 				     unsigned int start_bit,
2463ccd979bdSMark Fasheh 				     u64 bg_blkno,
2464b4414eeaSMark Fasheh 				     unsigned int count,
2465b4414eeaSMark Fasheh 				     void (*undo_fn)(unsigned int bit,
2466b4414eeaSMark Fasheh 						     unsigned long *bitmap))
2467ccd979bdSMark Fasheh {
2468ccd979bdSMark Fasheh 	int status = 0;
2469ccd979bdSMark Fasheh 	u32 tmp_used;
2470ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2471ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2472ccd979bdSMark Fasheh 	struct buffer_head *group_bh = NULL;
2473ccd979bdSMark Fasheh 	struct ocfs2_group_desc *group;
2474ccd979bdSMark Fasheh 
247510995aa2SJoel Becker 	/* The alloc_bh comes from ocfs2_free_dinode() or
247610995aa2SJoel Becker 	 * ocfs2_free_clusters().  The callers have all locked the
247710995aa2SJoel Becker 	 * allocator and gotten alloc_bh from the lock call.  This
247825985edcSLucas De Marchi 	 * validates the dinode buffer.  Any corruption that has happened
247910995aa2SJoel Becker 	 * is a code bug. */
248010995aa2SJoel Becker 	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
2481ccd979bdSMark Fasheh 	BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2482ccd979bdSMark Fasheh 
24832f73e135STao Ma 	trace_ocfs2_free_suballoc_bits(
24842f73e135STao Ma 		(unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
24852f73e135STao Ma 		(unsigned long long)bg_blkno,
24862f73e135STao Ma 		start_bit, count);
2487ccd979bdSMark Fasheh 
248868f64d47SJoel Becker 	status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
248968f64d47SJoel Becker 					     &group_bh);
2490ccd979bdSMark Fasheh 	if (status < 0) {
2491ccd979bdSMark Fasheh 		mlog_errno(status);
2492ccd979bdSMark Fasheh 		goto bail;
2493ccd979bdSMark Fasheh 	}
2494ccd979bdSMark Fasheh 	group = (struct ocfs2_group_desc *) group_bh->b_data;
249568f64d47SJoel Becker 
2496ccd979bdSMark Fasheh 	BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2497ccd979bdSMark Fasheh 
2498ccd979bdSMark Fasheh 	status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2499ccd979bdSMark Fasheh 					      group, group_bh,
2500b4414eeaSMark Fasheh 					      start_bit, count, undo_fn);
2501ccd979bdSMark Fasheh 	if (status < 0) {
2502ccd979bdSMark Fasheh 		mlog_errno(status);
2503ccd979bdSMark Fasheh 		goto bail;
2504ccd979bdSMark Fasheh 	}
2505ccd979bdSMark Fasheh 
25060cf2f763SJoel Becker 	status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
25070cf2f763SJoel Becker 					 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
2508ccd979bdSMark Fasheh 	if (status < 0) {
2509ccd979bdSMark Fasheh 		mlog_errno(status);
2510a47726bcSJoseph Qi 		ocfs2_block_group_set_bits(handle, alloc_inode, group, group_bh,
2511a47726bcSJoseph Qi 				start_bit, count);
2512ccd979bdSMark Fasheh 		goto bail;
2513ccd979bdSMark Fasheh 	}
2514ccd979bdSMark Fasheh 
2515ccd979bdSMark Fasheh 	le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2516ccd979bdSMark Fasheh 		     count);
2517ccd979bdSMark Fasheh 	tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2518ccd979bdSMark Fasheh 	fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
2519ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, alloc_bh);
2520ccd979bdSMark Fasheh 
2521ccd979bdSMark Fasheh bail:
2522ccd979bdSMark Fasheh 	brelse(group_bh);
2523ccd979bdSMark Fasheh 
2524c1e8d35eSTao Ma 	if (status)
2525c1e8d35eSTao Ma 		mlog_errno(status);
2526ccd979bdSMark Fasheh 	return status;
2527ccd979bdSMark Fasheh }
2528ccd979bdSMark Fasheh 
2529b4414eeaSMark Fasheh int ocfs2_free_suballoc_bits(handle_t *handle,
2530b4414eeaSMark Fasheh 			     struct inode *alloc_inode,
2531b4414eeaSMark Fasheh 			     struct buffer_head *alloc_bh,
2532b4414eeaSMark Fasheh 			     unsigned int start_bit,
2533b4414eeaSMark Fasheh 			     u64 bg_blkno,
2534b4414eeaSMark Fasheh 			     unsigned int count)
2535b4414eeaSMark Fasheh {
2536b4414eeaSMark Fasheh 	return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2537b4414eeaSMark Fasheh 					 start_bit, bg_blkno, count, NULL);
2538b4414eeaSMark Fasheh }
2539b4414eeaSMark Fasheh 
25401fabe148SMark Fasheh int ocfs2_free_dinode(handle_t *handle,
2541ccd979bdSMark Fasheh 		      struct inode *inode_alloc_inode,
2542ccd979bdSMark Fasheh 		      struct buffer_head *inode_alloc_bh,
2543ccd979bdSMark Fasheh 		      struct ocfs2_dinode *di)
2544ccd979bdSMark Fasheh {
2545ccd979bdSMark Fasheh 	u64 blk = le64_to_cpu(di->i_blkno);
2546ccd979bdSMark Fasheh 	u16 bit = le16_to_cpu(di->i_suballoc_bit);
2547ccd979bdSMark Fasheh 	u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2548ccd979bdSMark Fasheh 
254974380c47STao Ma 	if (di->i_suballoc_loc)
255074380c47STao Ma 		bg_blkno = le64_to_cpu(di->i_suballoc_loc);
2551ccd979bdSMark Fasheh 	return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2552ccd979bdSMark Fasheh 					inode_alloc_bh, bit, bg_blkno, 1);
2553ccd979bdSMark Fasheh }
2554ccd979bdSMark Fasheh 
2555b4414eeaSMark Fasheh static int _ocfs2_free_clusters(handle_t *handle,
2556ccd979bdSMark Fasheh 				struct inode *bitmap_inode,
2557ccd979bdSMark Fasheh 				struct buffer_head *bitmap_bh,
2558ccd979bdSMark Fasheh 				u64 start_blk,
2559b4414eeaSMark Fasheh 				unsigned int num_clusters,
2560b4414eeaSMark Fasheh 				void (*undo_fn)(unsigned int bit,
2561b4414eeaSMark Fasheh 						unsigned long *bitmap))
2562ccd979bdSMark Fasheh {
2563ccd979bdSMark Fasheh 	int status;
2564ccd979bdSMark Fasheh 	u16 bg_start_bit;
2565ccd979bdSMark Fasheh 	u64 bg_blkno;
2566ccd979bdSMark Fasheh 
2567ccd979bdSMark Fasheh 	/* You can't ever have a contiguous set of clusters
2568ccd979bdSMark Fasheh 	 * bigger than a block group bitmap so we never have to worry
2569ef6b689bSTao Ma 	 * about looping on them.
2570ef6b689bSTao Ma 	 * This is expensive. We can safely remove once this stuff has
2571ccd979bdSMark Fasheh 	 * gotten tested really well. */
2572*dd7b5f9dSChangwei Ge 	BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb,
2573*dd7b5f9dSChangwei Ge 				ocfs2_blocks_to_clusters(bitmap_inode->i_sb,
2574*dd7b5f9dSChangwei Ge 							 start_blk)));
2575ccd979bdSMark Fasheh 
2576ccd979bdSMark Fasheh 
2577ccd979bdSMark Fasheh 	ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2578ccd979bdSMark Fasheh 				     &bg_start_bit);
2579ccd979bdSMark Fasheh 
25802f73e135STao Ma 	trace_ocfs2_free_clusters((unsigned long long)bg_blkno,
25812f73e135STao Ma 			(unsigned long long)start_blk,
25822f73e135STao Ma 			bg_start_bit, num_clusters);
2583ccd979bdSMark Fasheh 
2584b4414eeaSMark Fasheh 	status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2585ccd979bdSMark Fasheh 					   bg_start_bit, bg_blkno,
2586b4414eeaSMark Fasheh 					   num_clusters, undo_fn);
25879c7af40bSMark Fasheh 	if (status < 0) {
2588ccd979bdSMark Fasheh 		mlog_errno(status);
25899c7af40bSMark Fasheh 		goto out;
25909c7af40bSMark Fasheh 	}
2591ccd979bdSMark Fasheh 
25929c7af40bSMark Fasheh 	ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
25939c7af40bSMark Fasheh 					 num_clusters);
25949c7af40bSMark Fasheh 
25959c7af40bSMark Fasheh out:
2596c1e8d35eSTao Ma 	if (status)
2597c1e8d35eSTao Ma 		mlog_errno(status);
2598ccd979bdSMark Fasheh 	return status;
2599ccd979bdSMark Fasheh }
2600ccd979bdSMark Fasheh 
2601b4414eeaSMark Fasheh int ocfs2_free_clusters(handle_t *handle,
2602b4414eeaSMark Fasheh 			struct inode *bitmap_inode,
2603b4414eeaSMark Fasheh 			struct buffer_head *bitmap_bh,
2604b4414eeaSMark Fasheh 			u64 start_blk,
2605b4414eeaSMark Fasheh 			unsigned int num_clusters)
2606b4414eeaSMark Fasheh {
2607b4414eeaSMark Fasheh 	return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2608b4414eeaSMark Fasheh 				    start_blk, num_clusters,
2609b4414eeaSMark Fasheh 				    _ocfs2_set_bit);
2610b4414eeaSMark Fasheh }
2611b4414eeaSMark Fasheh 
2612b4414eeaSMark Fasheh /*
2613b4414eeaSMark Fasheh  * Give never-used clusters back to the global bitmap.  We don't need
2614b4414eeaSMark Fasheh  * to protect these bits in the undo buffer.
2615b4414eeaSMark Fasheh  */
2616b4414eeaSMark Fasheh int ocfs2_release_clusters(handle_t *handle,
2617b4414eeaSMark Fasheh 			   struct inode *bitmap_inode,
2618b4414eeaSMark Fasheh 			   struct buffer_head *bitmap_bh,
2619b4414eeaSMark Fasheh 			   u64 start_blk,
2620b4414eeaSMark Fasheh 			   unsigned int num_clusters)
2621b4414eeaSMark Fasheh {
2622b4414eeaSMark Fasheh 	return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2623b4414eeaSMark Fasheh 				    start_blk, num_clusters,
2624b4414eeaSMark Fasheh 				    _ocfs2_clear_bit);
2625b4414eeaSMark Fasheh }
2626b4414eeaSMark Fasheh 
2627ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2628ccd979bdSMark Fasheh {
2629ccd979bdSMark Fasheh 	printk("Block Group:\n");
2630ccd979bdSMark Fasheh 	printk("bg_signature:       %s\n", bg->bg_signature);
2631ccd979bdSMark Fasheh 	printk("bg_size:            %u\n", bg->bg_size);
2632ccd979bdSMark Fasheh 	printk("bg_bits:            %u\n", bg->bg_bits);
2633ccd979bdSMark Fasheh 	printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2634ccd979bdSMark Fasheh 	printk("bg_chain:           %u\n", bg->bg_chain);
2635ccd979bdSMark Fasheh 	printk("bg_generation:      %u\n", le32_to_cpu(bg->bg_generation));
2636b0697053SMark Fasheh 	printk("bg_next_group:      %llu\n",
2637b0697053SMark Fasheh 	       (unsigned long long)bg->bg_next_group);
2638b0697053SMark Fasheh 	printk("bg_parent_dinode:   %llu\n",
2639b0697053SMark Fasheh 	       (unsigned long long)bg->bg_parent_dinode);
2640b0697053SMark Fasheh 	printk("bg_blkno:           %llu\n",
2641b0697053SMark Fasheh 	       (unsigned long long)bg->bg_blkno);
2642ccd979bdSMark Fasheh }
2643ccd979bdSMark Fasheh 
2644ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2645ccd979bdSMark Fasheh {
2646ccd979bdSMark Fasheh 	int i;
2647ccd979bdSMark Fasheh 
2648b0697053SMark Fasheh 	printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
2649ccd979bdSMark Fasheh 	printk("i_signature:                  %s\n", fe->i_signature);
2650b0697053SMark Fasheh 	printk("i_size:                       %llu\n",
2651b0697053SMark Fasheh 	       (unsigned long long)fe->i_size);
2652ccd979bdSMark Fasheh 	printk("i_clusters:                   %u\n", fe->i_clusters);
2653ccd979bdSMark Fasheh 	printk("i_generation:                 %u\n",
2654ccd979bdSMark Fasheh 	       le32_to_cpu(fe->i_generation));
2655ccd979bdSMark Fasheh 	printk("id1.bitmap1.i_used:           %u\n",
2656ccd979bdSMark Fasheh 	       le32_to_cpu(fe->id1.bitmap1.i_used));
2657ccd979bdSMark Fasheh 	printk("id1.bitmap1.i_total:          %u\n",
2658ccd979bdSMark Fasheh 	       le32_to_cpu(fe->id1.bitmap1.i_total));
2659ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_cpg:           %u\n", fe->id2.i_chain.cl_cpg);
2660ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_bpc:           %u\n", fe->id2.i_chain.cl_bpc);
2661ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_count:         %u\n", fe->id2.i_chain.cl_count);
2662ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_next_free_rec: %u\n",
2663ccd979bdSMark Fasheh 	       fe->id2.i_chain.cl_next_free_rec);
2664ccd979bdSMark Fasheh 	for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2665ccd979bdSMark Fasheh 		printk("fe->id2.i_chain.cl_recs[%d].c_free:  %u\n", i,
2666ccd979bdSMark Fasheh 		       fe->id2.i_chain.cl_recs[i].c_free);
2667ccd979bdSMark Fasheh 		printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2668ccd979bdSMark Fasheh 		       fe->id2.i_chain.cl_recs[i].c_total);
2669b0697053SMark Fasheh 		printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2670b0697053SMark Fasheh 		       (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
2671ccd979bdSMark Fasheh 	}
2672ccd979bdSMark Fasheh }
2673e7d4cb6bSTao Ma 
2674e7d4cb6bSTao Ma /*
2675e7d4cb6bSTao Ma  * For a given allocation, determine which allocators will need to be
2676e7d4cb6bSTao Ma  * accessed, and lock them, reserving the appropriate number of bits.
2677e7d4cb6bSTao Ma  *
2678e7d4cb6bSTao Ma  * Sparse file systems call this from ocfs2_write_begin_nolock()
2679e7d4cb6bSTao Ma  * and ocfs2_allocate_unwritten_extents().
2680e7d4cb6bSTao Ma  *
2681e7d4cb6bSTao Ma  * File systems which don't support holes call this from
2682e7d4cb6bSTao Ma  * ocfs2_extend_allocation().
2683e7d4cb6bSTao Ma  */
2684f99b9b7cSJoel Becker int ocfs2_lock_allocators(struct inode *inode,
2685f99b9b7cSJoel Becker 			  struct ocfs2_extent_tree *et,
2686e7d4cb6bSTao Ma 			  u32 clusters_to_add, u32 extents_to_split,
2687e7d4cb6bSTao Ma 			  struct ocfs2_alloc_context **data_ac,
2688f99b9b7cSJoel Becker 			  struct ocfs2_alloc_context **meta_ac)
2689e7d4cb6bSTao Ma {
2690e7d4cb6bSTao Ma 	int ret = 0, num_free_extents;
2691e7d4cb6bSTao Ma 	unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2692e7d4cb6bSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2693e7d4cb6bSTao Ma 
2694e7d4cb6bSTao Ma 	*meta_ac = NULL;
2695e7d4cb6bSTao Ma 	if (data_ac)
2696e7d4cb6bSTao Ma 		*data_ac = NULL;
2697e7d4cb6bSTao Ma 
2698e7d4cb6bSTao Ma 	BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2699e7d4cb6bSTao Ma 
2700964f14a0SJun Piao 	num_free_extents = ocfs2_num_free_extents(et);
2701e7d4cb6bSTao Ma 	if (num_free_extents < 0) {
2702e7d4cb6bSTao Ma 		ret = num_free_extents;
2703e7d4cb6bSTao Ma 		mlog_errno(ret);
2704e7d4cb6bSTao Ma 		goto out;
2705e7d4cb6bSTao Ma 	}
2706e7d4cb6bSTao Ma 
2707e7d4cb6bSTao Ma 	/*
2708e7d4cb6bSTao Ma 	 * Sparse allocation file systems need to be more conservative
2709e7d4cb6bSTao Ma 	 * with reserving room for expansion - the actual allocation
2710e7d4cb6bSTao Ma 	 * happens while we've got a journal handle open so re-taking
2711e7d4cb6bSTao Ma 	 * a cluster lock (because we ran out of room for another
2712e7d4cb6bSTao Ma 	 * extent) will violate ordering rules.
2713e7d4cb6bSTao Ma 	 *
2714e7d4cb6bSTao Ma 	 * Most of the time we'll only be seeing this 1 cluster at a time
2715e7d4cb6bSTao Ma 	 * anyway.
2716e7d4cb6bSTao Ma 	 *
2717e7d4cb6bSTao Ma 	 * Always lock for any unwritten extents - we might want to
2718e7d4cb6bSTao Ma 	 * add blocks during a split.
2719e7d4cb6bSTao Ma 	 */
2720e7d4cb6bSTao Ma 	if (!num_free_extents ||
2721e7d4cb6bSTao Ma 	    (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
2722f99b9b7cSJoel Becker 		ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
2723e7d4cb6bSTao Ma 		if (ret < 0) {
2724e7d4cb6bSTao Ma 			if (ret != -ENOSPC)
2725e7d4cb6bSTao Ma 				mlog_errno(ret);
2726e7d4cb6bSTao Ma 			goto out;
2727e7d4cb6bSTao Ma 		}
2728e7d4cb6bSTao Ma 	}
2729e7d4cb6bSTao Ma 
2730e7d4cb6bSTao Ma 	if (clusters_to_add == 0)
2731e7d4cb6bSTao Ma 		goto out;
2732e7d4cb6bSTao Ma 
2733e7d4cb6bSTao Ma 	ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2734e7d4cb6bSTao Ma 	if (ret < 0) {
2735e7d4cb6bSTao Ma 		if (ret != -ENOSPC)
2736e7d4cb6bSTao Ma 			mlog_errno(ret);
2737e7d4cb6bSTao Ma 		goto out;
2738e7d4cb6bSTao Ma 	}
2739e7d4cb6bSTao Ma 
2740e7d4cb6bSTao Ma out:
2741e7d4cb6bSTao Ma 	if (ret) {
2742e7d4cb6bSTao Ma 		if (*meta_ac) {
2743e7d4cb6bSTao Ma 			ocfs2_free_alloc_context(*meta_ac);
2744e7d4cb6bSTao Ma 			*meta_ac = NULL;
2745e7d4cb6bSTao Ma 		}
2746e7d4cb6bSTao Ma 
2747e7d4cb6bSTao Ma 		/*
2748e7d4cb6bSTao Ma 		 * We cannot have an error and a non null *data_ac.
2749e7d4cb6bSTao Ma 		 */
2750e7d4cb6bSTao Ma 	}
2751e7d4cb6bSTao Ma 
2752e7d4cb6bSTao Ma 	return ret;
2753e7d4cb6bSTao Ma }
27546ca497a8Swengang wang 
27556ca497a8Swengang wang /*
27566ca497a8Swengang wang  * Read the inode specified by blkno to get suballoc_slot and
27576ca497a8Swengang wang  * suballoc_bit.
27586ca497a8Swengang wang  */
27596ca497a8Swengang wang static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2760889f004aSTao Ma 				       u16 *suballoc_slot, u64 *group_blkno,
2761889f004aSTao Ma 				       u16 *suballoc_bit)
27626ca497a8Swengang wang {
27636ca497a8Swengang wang 	int status;
27646ca497a8Swengang wang 	struct buffer_head *inode_bh = NULL;
27656ca497a8Swengang wang 	struct ocfs2_dinode *inode_fe;
27666ca497a8Swengang wang 
27672f73e135STao Ma 	trace_ocfs2_get_suballoc_slot_bit((unsigned long long)blkno);
27686ca497a8Swengang wang 
27696ca497a8Swengang wang 	/* dirty read disk */
27706ca497a8Swengang wang 	status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
27716ca497a8Swengang wang 	if (status < 0) {
27725b09b507SJoel Becker 		mlog(ML_ERROR, "read block %llu failed %d\n",
27735b09b507SJoel Becker 		     (unsigned long long)blkno, status);
27746ca497a8Swengang wang 		goto bail;
27756ca497a8Swengang wang 	}
27766ca497a8Swengang wang 
27776ca497a8Swengang wang 	inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
27786ca497a8Swengang wang 	if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
27795b09b507SJoel Becker 		mlog(ML_ERROR, "invalid inode %llu requested\n",
27805b09b507SJoel Becker 		     (unsigned long long)blkno);
27816ca497a8Swengang wang 		status = -EINVAL;
27826ca497a8Swengang wang 		goto bail;
27836ca497a8Swengang wang 	}
27846ca497a8Swengang wang 
27850fba8137STao Ma 	if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
27866ca497a8Swengang wang 	    (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
27876ca497a8Swengang wang 		mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
27885b09b507SJoel Becker 		     (unsigned long long)blkno,
27895b09b507SJoel Becker 		     (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
27906ca497a8Swengang wang 		status = -EINVAL;
27916ca497a8Swengang wang 		goto bail;
27926ca497a8Swengang wang 	}
27936ca497a8Swengang wang 
27946ca497a8Swengang wang 	if (suballoc_slot)
27956ca497a8Swengang wang 		*suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
27966ca497a8Swengang wang 	if (suballoc_bit)
27976ca497a8Swengang wang 		*suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2798889f004aSTao Ma 	if (group_blkno)
2799889f004aSTao Ma 		*group_blkno = le64_to_cpu(inode_fe->i_suballoc_loc);
28006ca497a8Swengang wang 
28016ca497a8Swengang wang bail:
28026ca497a8Swengang wang 	brelse(inode_bh);
28036ca497a8Swengang wang 
2804c1e8d35eSTao Ma 	if (status)
2805c1e8d35eSTao Ma 		mlog_errno(status);
28066ca497a8Swengang wang 	return status;
28076ca497a8Swengang wang }
28086ca497a8Swengang wang 
28096ca497a8Swengang wang /*
28106ca497a8Swengang wang  * test whether bit is SET in allocator bitmap or not.  on success, 0
28116ca497a8Swengang wang  * is returned and *res is 1 for SET; 0 otherwise.  when fails, errno
28126ca497a8Swengang wang  * is returned and *res is meaningless.  Call this after you have
28136ca497a8Swengang wang  * cluster locked against suballoc, or you may get a result based on
28146ca497a8Swengang wang  * non-up2date contents
28156ca497a8Swengang wang  */
28166ca497a8Swengang wang static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
28176ca497a8Swengang wang 				   struct inode *suballoc,
2818889f004aSTao Ma 				   struct buffer_head *alloc_bh,
2819889f004aSTao Ma 				   u64 group_blkno, u64 blkno,
28206ca497a8Swengang wang 				   u16 bit, int *res)
28216ca497a8Swengang wang {
2822abf1b3cbSTao Ma 	struct ocfs2_dinode *alloc_di;
28236ca497a8Swengang wang 	struct ocfs2_group_desc *group;
28246ca497a8Swengang wang 	struct buffer_head *group_bh = NULL;
28256ca497a8Swengang wang 	u64 bg_blkno;
28266ca497a8Swengang wang 	int status;
28276ca497a8Swengang wang 
28282f73e135STao Ma 	trace_ocfs2_test_suballoc_bit((unsigned long long)blkno,
28295b09b507SJoel Becker 				      (unsigned int)bit);
28306ca497a8Swengang wang 
2831abf1b3cbSTao Ma 	alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data;
2832abf1b3cbSTao Ma 	if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) {
28336ca497a8Swengang wang 		mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
28346ca497a8Swengang wang 		     (unsigned int)bit,
2835abf1b3cbSTao Ma 		     ocfs2_bits_per_group(&alloc_di->id2.i_chain));
28366ca497a8Swengang wang 		status = -EINVAL;
28376ca497a8Swengang wang 		goto bail;
28386ca497a8Swengang wang 	}
28396ca497a8Swengang wang 
2840889f004aSTao Ma 	bg_blkno = group_blkno ? group_blkno :
2841889f004aSTao Ma 		   ocfs2_which_suballoc_group(blkno, bit);
2842abf1b3cbSTao Ma 	status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno,
28436ca497a8Swengang wang 					     &group_bh);
28446ca497a8Swengang wang 	if (status < 0) {
28455b09b507SJoel Becker 		mlog(ML_ERROR, "read group %llu failed %d\n",
28465b09b507SJoel Becker 		     (unsigned long long)bg_blkno, status);
28476ca497a8Swengang wang 		goto bail;
28486ca497a8Swengang wang 	}
28496ca497a8Swengang wang 
28506ca497a8Swengang wang 	group = (struct ocfs2_group_desc *) group_bh->b_data;
28516ca497a8Swengang wang 	*res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
28526ca497a8Swengang wang 
28536ca497a8Swengang wang bail:
28546ca497a8Swengang wang 	brelse(group_bh);
28556ca497a8Swengang wang 
2856c1e8d35eSTao Ma 	if (status)
2857c1e8d35eSTao Ma 		mlog_errno(status);
28586ca497a8Swengang wang 	return status;
28596ca497a8Swengang wang }
28606ca497a8Swengang wang 
28616ca497a8Swengang wang /*
28626ca497a8Swengang wang  * Test if the bit representing this inode (blkno) is set in the
28636ca497a8Swengang wang  * suballocator.
28646ca497a8Swengang wang  *
28656ca497a8Swengang wang  * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
28666ca497a8Swengang wang  *
28676ca497a8Swengang wang  * In the event of failure, a negative value is returned and *res is
28686ca497a8Swengang wang  * meaningless.
28696ca497a8Swengang wang  *
28706ca497a8Swengang wang  * Callers must make sure to hold nfs_sync_lock to prevent
28716ca497a8Swengang wang  * ocfs2_delete_inode() on another node from accessing the same
28726ca497a8Swengang wang  * suballocator concurrently.
28736ca497a8Swengang wang  */
28746ca497a8Swengang wang int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
28756ca497a8Swengang wang {
28766ca497a8Swengang wang 	int status;
2877889f004aSTao Ma 	u64 group_blkno = 0;
28786ca497a8Swengang wang 	u16 suballoc_bit = 0, suballoc_slot = 0;
28796ca497a8Swengang wang 	struct inode *inode_alloc_inode;
28806ca497a8Swengang wang 	struct buffer_head *alloc_bh = NULL;
28816ca497a8Swengang wang 
28822f73e135STao Ma 	trace_ocfs2_test_inode_bit((unsigned long long)blkno);
28836ca497a8Swengang wang 
28846ca497a8Swengang wang 	status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2885889f004aSTao Ma 					     &group_blkno, &suballoc_bit);
28866ca497a8Swengang wang 	if (status < 0) {
28876ca497a8Swengang wang 		mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
28886ca497a8Swengang wang 		goto bail;
28896ca497a8Swengang wang 	}
28906ca497a8Swengang wang 
28916ca497a8Swengang wang 	inode_alloc_inode =
28926ca497a8Swengang wang 		ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
28936ca497a8Swengang wang 					    suballoc_slot);
28946ca497a8Swengang wang 	if (!inode_alloc_inode) {
28956ca497a8Swengang wang 		/* the error code could be inaccurate, but we are not able to
28966ca497a8Swengang wang 		 * get the correct one. */
28976ca497a8Swengang wang 		status = -EINVAL;
28986ca497a8Swengang wang 		mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
28996ca497a8Swengang wang 		     (u32)suballoc_slot);
29006ca497a8Swengang wang 		goto bail;
29016ca497a8Swengang wang 	}
29026ca497a8Swengang wang 
29035955102cSAl Viro 	inode_lock(inode_alloc_inode);
29046ca497a8Swengang wang 	status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
29056ca497a8Swengang wang 	if (status < 0) {
29065955102cSAl Viro 		inode_unlock(inode_alloc_inode);
29077dc3e839Sjiangyiwen 		iput(inode_alloc_inode);
29086ca497a8Swengang wang 		mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
29096ca497a8Swengang wang 		     (u32)suballoc_slot, status);
29106ca497a8Swengang wang 		goto bail;
29116ca497a8Swengang wang 	}
29126ca497a8Swengang wang 
29136ca497a8Swengang wang 	status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2914889f004aSTao Ma 					 group_blkno, blkno, suballoc_bit, res);
29156ca497a8Swengang wang 	if (status < 0)
29166ca497a8Swengang wang 		mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
29176ca497a8Swengang wang 
29186ca497a8Swengang wang 	ocfs2_inode_unlock(inode_alloc_inode, 0);
29195955102cSAl Viro 	inode_unlock(inode_alloc_inode);
29206ca497a8Swengang wang 
29216ca497a8Swengang wang 	iput(inode_alloc_inode);
29226ca497a8Swengang wang 	brelse(alloc_bh);
29236ca497a8Swengang wang bail:
2924c1e8d35eSTao Ma 	if (status)
2925c1e8d35eSTao Ma 		mlog_errno(status);
29266ca497a8Swengang wang 	return status;
29276ca497a8Swengang wang }
2928