xref: /openbmc/linux/fs/ocfs2/suballoc.c (revision 12462f1d9f0b96389497438dc2730c6f7410be82)
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 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33ccd979bdSMark Fasheh #include <cluster/masklog.h>
34ccd979bdSMark Fasheh 
35ccd979bdSMark Fasheh #include "ocfs2.h"
36ccd979bdSMark Fasheh 
37ccd979bdSMark Fasheh #include "alloc.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"
46ccd979bdSMark Fasheh 
47ccd979bdSMark Fasheh #include "buffer_head_io.h"
48ccd979bdSMark Fasheh 
49ffda89a3STao Ma #define NOT_ALLOC_NEW_GROUP		0
50ffda89a3STao Ma #define ALLOC_NEW_GROUP			1
51ffda89a3STao Ma 
524d0ddb2cSTao Ma #define OCFS2_MAX_INODES_TO_STEAL	1024
534d0ddb2cSTao Ma 
54ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
55ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
56ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
571fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle,
58ccd979bdSMark Fasheh 				  struct inode *alloc_inode,
59ccd979bdSMark Fasheh 				  struct buffer_head *bg_bh,
60ccd979bdSMark Fasheh 				  u64 group_blkno,
61ccd979bdSMark Fasheh 				  u16 my_chain,
62ccd979bdSMark Fasheh 				  struct ocfs2_chain_list *cl);
63ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
64ccd979bdSMark Fasheh 				   struct inode *alloc_inode,
651187c968SJoel Becker 				   struct buffer_head *bh,
661187c968SJoel Becker 				   u64 max_block);
67ccd979bdSMark Fasheh 
68ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode,
69ccd979bdSMark Fasheh 				      struct buffer_head *group_bh,
70ccd979bdSMark Fasheh 				      u32 bits_wanted, u32 min_bits,
711187c968SJoel Becker 				      u64 max_block,
72ccd979bdSMark Fasheh 				      u16 *bit_off, u16 *bits_found);
73ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode,
74ccd979bdSMark Fasheh 				    struct buffer_head *group_bh,
75ccd979bdSMark Fasheh 				    u32 bits_wanted, u32 min_bits,
761187c968SJoel Becker 				    u64 max_block,
77ccd979bdSMark Fasheh 				    u16 *bit_off, u16 *bits_found);
78ccd979bdSMark Fasheh static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
79ccd979bdSMark Fasheh 				     struct ocfs2_alloc_context *ac,
801fabe148SMark Fasheh 				     handle_t *handle,
81ccd979bdSMark Fasheh 				     u32 bits_wanted,
82ccd979bdSMark Fasheh 				     u32 min_bits,
83ccd979bdSMark Fasheh 				     u16 *bit_off,
84ccd979bdSMark Fasheh 				     unsigned int *num_bits,
85ccd979bdSMark Fasheh 				     u64 *bg_blkno);
86ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
87ccd979bdSMark Fasheh 					 int nr);
881fabe148SMark Fasheh static inline int ocfs2_block_group_set_bits(handle_t *handle,
89ccd979bdSMark Fasheh 					     struct inode *alloc_inode,
90ccd979bdSMark Fasheh 					     struct ocfs2_group_desc *bg,
91ccd979bdSMark Fasheh 					     struct buffer_head *group_bh,
92ccd979bdSMark Fasheh 					     unsigned int bit_off,
93ccd979bdSMark Fasheh 					     unsigned int num_bits);
941fabe148SMark Fasheh static inline int ocfs2_block_group_clear_bits(handle_t *handle,
95ccd979bdSMark Fasheh 					       struct inode *alloc_inode,
96ccd979bdSMark Fasheh 					       struct ocfs2_group_desc *bg,
97ccd979bdSMark Fasheh 					       struct buffer_head *group_bh,
98ccd979bdSMark Fasheh 					       unsigned int bit_off,
99ccd979bdSMark Fasheh 					       unsigned int num_bits);
100ccd979bdSMark Fasheh 
1011fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle,
102ccd979bdSMark Fasheh 				    struct inode *alloc_inode,
103ccd979bdSMark Fasheh 				    struct buffer_head *fe_bh,
104ccd979bdSMark Fasheh 				    struct buffer_head *bg_bh,
105ccd979bdSMark Fasheh 				    struct buffer_head *prev_bg_bh,
106ccd979bdSMark Fasheh 				    u16 chain);
107ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
108ccd979bdSMark Fasheh 						     u32 wanted);
109ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
110ccd979bdSMark Fasheh 						   u64 bg_blkno,
111ccd979bdSMark Fasheh 						   u16 bg_bit_off);
112ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode,
113ccd979bdSMark Fasheh 						u64 data_blkno,
114ccd979bdSMark Fasheh 						u64 *bg_blkno,
115ccd979bdSMark Fasheh 						u16 *bg_bit_off);
1161187c968SJoel Becker static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
1171187c968SJoel Becker 					     u32 bits_wanted, u64 max_block,
1181187c968SJoel Becker 					     struct ocfs2_alloc_context **ac);
119ccd979bdSMark Fasheh 
1209c7af40bSMark Fasheh void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
121ccd979bdSMark Fasheh {
122da5cbf2fSMark Fasheh 	struct inode *inode = ac->ac_inode;
123da5cbf2fSMark Fasheh 
124da5cbf2fSMark Fasheh 	if (inode) {
125da5cbf2fSMark Fasheh 		if (ac->ac_which != OCFS2_AC_USE_LOCAL)
126e63aecb6SMark Fasheh 			ocfs2_inode_unlock(inode, 1);
127da5cbf2fSMark Fasheh 
128da5cbf2fSMark Fasheh 		mutex_unlock(&inode->i_mutex);
129da5cbf2fSMark Fasheh 
130da5cbf2fSMark Fasheh 		iput(inode);
1314d0ddb2cSTao Ma 		ac->ac_inode = NULL;
132da5cbf2fSMark Fasheh 	}
1334d0ddb2cSTao Ma 	if (ac->ac_bh) {
134ccd979bdSMark Fasheh 		brelse(ac->ac_bh);
1354d0ddb2cSTao Ma 		ac->ac_bh = NULL;
1364d0ddb2cSTao Ma 	}
1374d0ddb2cSTao Ma }
1384d0ddb2cSTao Ma 
1394d0ddb2cSTao Ma void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
1404d0ddb2cSTao Ma {
1414d0ddb2cSTao Ma 	ocfs2_free_ac_resource(ac);
142ccd979bdSMark Fasheh 	kfree(ac);
143ccd979bdSMark Fasheh }
144ccd979bdSMark Fasheh 
145ccd979bdSMark Fasheh static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
146ccd979bdSMark Fasheh {
147ccd979bdSMark Fasheh 	return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
148ccd979bdSMark Fasheh }
149ccd979bdSMark Fasheh 
1507bf72edeSMark Fasheh /* somewhat more expensive than our other checks, so use sparingly. */
151d659072fSTao Ma int ocfs2_check_group_descriptor(struct super_block *sb,
1527bf72edeSMark Fasheh 				 struct ocfs2_dinode *di,
1537bf72edeSMark Fasheh 				 struct ocfs2_group_desc *gd)
1547bf72edeSMark Fasheh {
1557bf72edeSMark Fasheh 	unsigned int max_bits;
1567bf72edeSMark Fasheh 
1577bf72edeSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1587bf72edeSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(sb, gd);
1597bf72edeSMark Fasheh 		return -EIO;
1607bf72edeSMark Fasheh 	}
1617bf72edeSMark Fasheh 
1627bf72edeSMark Fasheh 	if (di->i_blkno != gd->bg_parent_dinode) {
1637bf72edeSMark Fasheh 		ocfs2_error(sb, "Group descriptor # %llu has bad parent "
1647bf72edeSMark Fasheh 			    "pointer (%llu, expected %llu)",
1657bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_blkno),
1667bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
1677bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(di->i_blkno));
1687bf72edeSMark Fasheh 		return -EIO;
1697bf72edeSMark Fasheh 	}
1707bf72edeSMark Fasheh 
1717bf72edeSMark Fasheh 	max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
1727bf72edeSMark Fasheh 	if (le16_to_cpu(gd->bg_bits) > max_bits) {
1737bf72edeSMark Fasheh 		ocfs2_error(sb, "Group descriptor # %llu has bit count of %u",
1747bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_blkno),
1757bf72edeSMark Fasheh 			    le16_to_cpu(gd->bg_bits));
1767bf72edeSMark Fasheh 		return -EIO;
1777bf72edeSMark Fasheh 	}
1787bf72edeSMark Fasheh 
1797bf72edeSMark Fasheh 	if (le16_to_cpu(gd->bg_chain) >=
1807bf72edeSMark Fasheh 	    le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
1817bf72edeSMark Fasheh 		ocfs2_error(sb, "Group descriptor # %llu has bad chain %u",
1827bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_blkno),
1837bf72edeSMark Fasheh 			    le16_to_cpu(gd->bg_chain));
1847bf72edeSMark Fasheh 		return -EIO;
1857bf72edeSMark Fasheh 	}
1867bf72edeSMark Fasheh 
1877bf72edeSMark Fasheh 	if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
1887bf72edeSMark Fasheh 		ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
1897bf72edeSMark Fasheh 			    "claims that %u are free",
1907bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_blkno),
1917bf72edeSMark Fasheh 			    le16_to_cpu(gd->bg_bits),
1927bf72edeSMark Fasheh 			    le16_to_cpu(gd->bg_free_bits_count));
1937bf72edeSMark Fasheh 		return -EIO;
1947bf72edeSMark Fasheh 	}
1957bf72edeSMark Fasheh 
1967bf72edeSMark Fasheh 	if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
1977bf72edeSMark Fasheh 		ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
1987bf72edeSMark Fasheh 			    "max bitmap bits of %u",
1997bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_blkno),
2007bf72edeSMark Fasheh 			    le16_to_cpu(gd->bg_bits),
2017bf72edeSMark Fasheh 			    8 * le16_to_cpu(gd->bg_size));
2027bf72edeSMark Fasheh 		return -EIO;
2037bf72edeSMark Fasheh 	}
2047bf72edeSMark Fasheh 
2057bf72edeSMark Fasheh 	return 0;
2067bf72edeSMark Fasheh }
2077bf72edeSMark Fasheh 
2081fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle,
209ccd979bdSMark Fasheh 				  struct inode *alloc_inode,
210ccd979bdSMark Fasheh 				  struct buffer_head *bg_bh,
211ccd979bdSMark Fasheh 				  u64 group_blkno,
212ccd979bdSMark Fasheh 				  u16 my_chain,
213ccd979bdSMark Fasheh 				  struct ocfs2_chain_list *cl)
214ccd979bdSMark Fasheh {
215ccd979bdSMark Fasheh 	int status = 0;
216ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
217ccd979bdSMark Fasheh 	struct super_block * sb = alloc_inode->i_sb;
218ccd979bdSMark Fasheh 
219ccd979bdSMark Fasheh 	mlog_entry_void();
220ccd979bdSMark Fasheh 
221ccd979bdSMark Fasheh 	if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
222b0697053SMark Fasheh 		ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
223b0697053SMark Fasheh 			    "b_blocknr (%llu)",
224b0697053SMark Fasheh 			    (unsigned long long)group_blkno,
225ccd979bdSMark Fasheh 			    (unsigned long long) bg_bh->b_blocknr);
226ccd979bdSMark Fasheh 		status = -EIO;
227ccd979bdSMark Fasheh 		goto bail;
228ccd979bdSMark Fasheh 	}
229ccd979bdSMark Fasheh 
230ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle,
231ccd979bdSMark Fasheh 				      alloc_inode,
232ccd979bdSMark Fasheh 				      bg_bh,
233ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_CREATE);
234ccd979bdSMark Fasheh 	if (status < 0) {
235ccd979bdSMark Fasheh 		mlog_errno(status);
236ccd979bdSMark Fasheh 		goto bail;
237ccd979bdSMark Fasheh 	}
238ccd979bdSMark Fasheh 
239ccd979bdSMark Fasheh 	memset(bg, 0, sb->s_blocksize);
240ccd979bdSMark Fasheh 	strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
241ccd979bdSMark Fasheh 	bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
242ccd979bdSMark Fasheh 	bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
243ccd979bdSMark Fasheh 	bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
244ccd979bdSMark Fasheh 	bg->bg_chain = cpu_to_le16(my_chain);
245ccd979bdSMark Fasheh 	bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
246ccd979bdSMark Fasheh 	bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
247ccd979bdSMark Fasheh 	bg->bg_blkno = cpu_to_le64(group_blkno);
248ccd979bdSMark Fasheh 	/* set the 1st bit in the bitmap to account for the descriptor block */
249ccd979bdSMark Fasheh 	ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
250ccd979bdSMark Fasheh 	bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
251ccd979bdSMark Fasheh 
252ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, bg_bh);
253ccd979bdSMark Fasheh 	if (status < 0)
254ccd979bdSMark Fasheh 		mlog_errno(status);
255ccd979bdSMark Fasheh 
256ccd979bdSMark Fasheh 	/* There is no need to zero out or otherwise initialize the
257ccd979bdSMark Fasheh 	 * other blocks in a group - All valid FS metadata in a block
258ccd979bdSMark Fasheh 	 * group stores the superblock fs_generation value at
259ccd979bdSMark Fasheh 	 * allocation time. */
260ccd979bdSMark Fasheh 
261ccd979bdSMark Fasheh bail:
262ccd979bdSMark Fasheh 	mlog_exit(status);
263ccd979bdSMark Fasheh 	return status;
264ccd979bdSMark Fasheh }
265ccd979bdSMark Fasheh 
266ccd979bdSMark Fasheh static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
267ccd979bdSMark Fasheh {
268ccd979bdSMark Fasheh 	u16 curr, best;
269ccd979bdSMark Fasheh 
270ccd979bdSMark Fasheh 	best = curr = 0;
271ccd979bdSMark Fasheh 	while (curr < le16_to_cpu(cl->cl_count)) {
272ccd979bdSMark Fasheh 		if (le32_to_cpu(cl->cl_recs[best].c_total) >
273ccd979bdSMark Fasheh 		    le32_to_cpu(cl->cl_recs[curr].c_total))
274ccd979bdSMark Fasheh 			best = curr;
275ccd979bdSMark Fasheh 		curr++;
276ccd979bdSMark Fasheh 	}
277ccd979bdSMark Fasheh 	return best;
278ccd979bdSMark Fasheh }
279ccd979bdSMark Fasheh 
280ccd979bdSMark Fasheh /*
281ccd979bdSMark Fasheh  * We expect the block group allocator to already be locked.
282ccd979bdSMark Fasheh  */
283ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
284ccd979bdSMark Fasheh 				   struct inode *alloc_inode,
2851187c968SJoel Becker 				   struct buffer_head *bh,
2861187c968SJoel Becker 				   u64 max_block)
287ccd979bdSMark Fasheh {
288ccd979bdSMark Fasheh 	int status, credits;
289ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
290ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl;
291ccd979bdSMark Fasheh 	struct ocfs2_alloc_context *ac = NULL;
2921fabe148SMark Fasheh 	handle_t *handle = NULL;
293ccd979bdSMark Fasheh 	u32 bit_off, num_bits;
294ccd979bdSMark Fasheh 	u16 alloc_rec;
295ccd979bdSMark Fasheh 	u64 bg_blkno;
296ccd979bdSMark Fasheh 	struct buffer_head *bg_bh = NULL;
297ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg;
298ccd979bdSMark Fasheh 
299ccd979bdSMark Fasheh 	BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
300ccd979bdSMark Fasheh 
301ccd979bdSMark Fasheh 	mlog_entry_void();
302ccd979bdSMark Fasheh 
303ccd979bdSMark Fasheh 	cl = &fe->id2.i_chain;
3041187c968SJoel Becker 	status = ocfs2_reserve_clusters_with_limit(osb,
305ccd979bdSMark Fasheh 						   le16_to_cpu(cl->cl_cpg),
3061187c968SJoel Becker 						   max_block, &ac);
307ccd979bdSMark Fasheh 	if (status < 0) {
308ccd979bdSMark Fasheh 		if (status != -ENOSPC)
309ccd979bdSMark Fasheh 			mlog_errno(status);
310ccd979bdSMark Fasheh 		goto bail;
311ccd979bdSMark Fasheh 	}
312ccd979bdSMark Fasheh 
313ccd979bdSMark Fasheh 	credits = ocfs2_calc_group_alloc_credits(osb->sb,
314ccd979bdSMark Fasheh 						 le16_to_cpu(cl->cl_cpg));
31565eff9ccSMark Fasheh 	handle = ocfs2_start_trans(osb, credits);
316ccd979bdSMark Fasheh 	if (IS_ERR(handle)) {
317ccd979bdSMark Fasheh 		status = PTR_ERR(handle);
318ccd979bdSMark Fasheh 		handle = NULL;
319ccd979bdSMark Fasheh 		mlog_errno(status);
320ccd979bdSMark Fasheh 		goto bail;
321ccd979bdSMark Fasheh 	}
322ccd979bdSMark Fasheh 
323ccd979bdSMark Fasheh 	status = ocfs2_claim_clusters(osb,
324ccd979bdSMark Fasheh 				      handle,
325ccd979bdSMark Fasheh 				      ac,
326ccd979bdSMark Fasheh 				      le16_to_cpu(cl->cl_cpg),
327ccd979bdSMark Fasheh 				      &bit_off,
328ccd979bdSMark Fasheh 				      &num_bits);
329ccd979bdSMark Fasheh 	if (status < 0) {
330ccd979bdSMark Fasheh 		if (status != -ENOSPC)
331ccd979bdSMark Fasheh 			mlog_errno(status);
332ccd979bdSMark Fasheh 		goto bail;
333ccd979bdSMark Fasheh 	}
334ccd979bdSMark Fasheh 
335ccd979bdSMark Fasheh 	alloc_rec = ocfs2_find_smallest_chain(cl);
336ccd979bdSMark Fasheh 
337ccd979bdSMark Fasheh 	/* setup the group */
338ccd979bdSMark Fasheh 	bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
339b0697053SMark Fasheh 	mlog(0, "new descriptor, record %u, at block %llu\n",
340b0697053SMark Fasheh 	     alloc_rec, (unsigned long long)bg_blkno);
341ccd979bdSMark Fasheh 
342ccd979bdSMark Fasheh 	bg_bh = sb_getblk(osb->sb, bg_blkno);
343ccd979bdSMark Fasheh 	if (!bg_bh) {
344ccd979bdSMark Fasheh 		status = -EIO;
345ccd979bdSMark Fasheh 		mlog_errno(status);
346ccd979bdSMark Fasheh 		goto bail;
347ccd979bdSMark Fasheh 	}
348ccd979bdSMark Fasheh 	ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
349ccd979bdSMark Fasheh 
350ccd979bdSMark Fasheh 	status = ocfs2_block_group_fill(handle,
351ccd979bdSMark Fasheh 					alloc_inode,
352ccd979bdSMark Fasheh 					bg_bh,
353ccd979bdSMark Fasheh 					bg_blkno,
354ccd979bdSMark Fasheh 					alloc_rec,
355ccd979bdSMark Fasheh 					cl);
356ccd979bdSMark Fasheh 	if (status < 0) {
357ccd979bdSMark Fasheh 		mlog_errno(status);
358ccd979bdSMark Fasheh 		goto bail;
359ccd979bdSMark Fasheh 	}
360ccd979bdSMark Fasheh 
361ccd979bdSMark Fasheh 	bg = (struct ocfs2_group_desc *) bg_bh->b_data;
362ccd979bdSMark Fasheh 
363ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode,
364ccd979bdSMark Fasheh 				      bh, OCFS2_JOURNAL_ACCESS_WRITE);
365ccd979bdSMark Fasheh 	if (status < 0) {
366ccd979bdSMark Fasheh 		mlog_errno(status);
367ccd979bdSMark Fasheh 		goto bail;
368ccd979bdSMark Fasheh 	}
369ccd979bdSMark Fasheh 
370ccd979bdSMark Fasheh 	le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
371ccd979bdSMark Fasheh 		     le16_to_cpu(bg->bg_free_bits_count));
372ccd979bdSMark Fasheh 	le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
373ccd979bdSMark Fasheh 	cl->cl_recs[alloc_rec].c_blkno  = cpu_to_le64(bg_blkno);
374ccd979bdSMark Fasheh 	if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
375ccd979bdSMark Fasheh 		le16_add_cpu(&cl->cl_next_free_rec, 1);
376ccd979bdSMark Fasheh 
377ccd979bdSMark Fasheh 	le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
378ccd979bdSMark Fasheh 					le16_to_cpu(bg->bg_free_bits_count));
379ccd979bdSMark Fasheh 	le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
380ccd979bdSMark Fasheh 	le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
381ccd979bdSMark Fasheh 
382ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, bh);
383ccd979bdSMark Fasheh 	if (status < 0) {
384ccd979bdSMark Fasheh 		mlog_errno(status);
385ccd979bdSMark Fasheh 		goto bail;
386ccd979bdSMark Fasheh 	}
387ccd979bdSMark Fasheh 
388ccd979bdSMark Fasheh 	spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
389ccd979bdSMark Fasheh 	OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
390ccd979bdSMark Fasheh 	fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
391ccd979bdSMark Fasheh 					     le32_to_cpu(fe->i_clusters)));
392ccd979bdSMark Fasheh 	spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
393ccd979bdSMark Fasheh 	i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
3948110b073SMark Fasheh 	alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
395ccd979bdSMark Fasheh 
396ccd979bdSMark Fasheh 	status = 0;
397ccd979bdSMark Fasheh bail:
398ccd979bdSMark Fasheh 	if (handle)
39902dc1af4SMark Fasheh 		ocfs2_commit_trans(osb, handle);
400ccd979bdSMark Fasheh 
401ccd979bdSMark Fasheh 	if (ac)
402ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(ac);
403ccd979bdSMark Fasheh 
404ccd979bdSMark Fasheh 	if (bg_bh)
405ccd979bdSMark Fasheh 		brelse(bg_bh);
406ccd979bdSMark Fasheh 
407ccd979bdSMark Fasheh 	mlog_exit(status);
408ccd979bdSMark Fasheh 	return status;
409ccd979bdSMark Fasheh }
410ccd979bdSMark Fasheh 
411ccd979bdSMark Fasheh static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
412da5cbf2fSMark Fasheh 				       struct ocfs2_alloc_context *ac,
413da5cbf2fSMark Fasheh 				       int type,
414ffda89a3STao Ma 				       u32 slot,
415ffda89a3STao Ma 				       int alloc_new_group)
416ccd979bdSMark Fasheh {
417ccd979bdSMark Fasheh 	int status;
418ccd979bdSMark Fasheh 	u32 bits_wanted = ac->ac_bits_wanted;
419da5cbf2fSMark Fasheh 	struct inode *alloc_inode;
420ccd979bdSMark Fasheh 	struct buffer_head *bh = NULL;
421ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
422ccd979bdSMark Fasheh 	u32 free_bits;
423ccd979bdSMark Fasheh 
424ccd979bdSMark Fasheh 	mlog_entry_void();
425ccd979bdSMark Fasheh 
426da5cbf2fSMark Fasheh 	alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
427da5cbf2fSMark Fasheh 	if (!alloc_inode) {
428da5cbf2fSMark Fasheh 		mlog_errno(-EINVAL);
429da5cbf2fSMark Fasheh 		return -EINVAL;
430ccd979bdSMark Fasheh 	}
431ccd979bdSMark Fasheh 
432da5cbf2fSMark Fasheh 	mutex_lock(&alloc_inode->i_mutex);
433da5cbf2fSMark Fasheh 
434e63aecb6SMark Fasheh 	status = ocfs2_inode_lock(alloc_inode, &bh, 1);
435da5cbf2fSMark Fasheh 	if (status < 0) {
436da5cbf2fSMark Fasheh 		mutex_unlock(&alloc_inode->i_mutex);
437da5cbf2fSMark Fasheh 		iput(alloc_inode);
438da5cbf2fSMark Fasheh 
439da5cbf2fSMark Fasheh 		mlog_errno(status);
440da5cbf2fSMark Fasheh 		return status;
441da5cbf2fSMark Fasheh 	}
442da5cbf2fSMark Fasheh 
443da5cbf2fSMark Fasheh 	ac->ac_inode = alloc_inode;
444a4a48911STao Ma 	ac->ac_alloc_slot = slot;
445da5cbf2fSMark Fasheh 
446ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) bh->b_data;
447ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
448ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
449ccd979bdSMark Fasheh 		status = -EIO;
450ccd979bdSMark Fasheh 		goto bail;
451ccd979bdSMark Fasheh 	}
452ccd979bdSMark Fasheh 	if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
453b0697053SMark Fasheh 		ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
454b0697053SMark Fasheh 			    (unsigned long long)le64_to_cpu(fe->i_blkno));
455ccd979bdSMark Fasheh 		status = -EIO;
456ccd979bdSMark Fasheh 		goto bail;
457ccd979bdSMark Fasheh 	}
458ccd979bdSMark Fasheh 
459ccd979bdSMark Fasheh 	free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
460ccd979bdSMark Fasheh 		le32_to_cpu(fe->id1.bitmap1.i_used);
461ccd979bdSMark Fasheh 
462ccd979bdSMark Fasheh 	if (bits_wanted > free_bits) {
463ccd979bdSMark Fasheh 		/* cluster bitmap never grows */
464ccd979bdSMark Fasheh 		if (ocfs2_is_cluster_bitmap(alloc_inode)) {
465ccd979bdSMark Fasheh 			mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
466ccd979bdSMark Fasheh 			     bits_wanted, free_bits);
467ccd979bdSMark Fasheh 			status = -ENOSPC;
468ccd979bdSMark Fasheh 			goto bail;
469ccd979bdSMark Fasheh 		}
470ccd979bdSMark Fasheh 
471ffda89a3STao Ma 		if (alloc_new_group != ALLOC_NEW_GROUP) {
472ffda89a3STao Ma 			mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
473ffda89a3STao Ma 			     "and we don't alloc a new group for it.\n",
474ffda89a3STao Ma 			     slot, bits_wanted, free_bits);
475ffda89a3STao Ma 			status = -ENOSPC;
476ffda89a3STao Ma 			goto bail;
477ffda89a3STao Ma 		}
478ffda89a3STao Ma 
4791187c968SJoel Becker 		status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
4801187c968SJoel Becker 						 ac->ac_max_block);
481ccd979bdSMark Fasheh 		if (status < 0) {
482ccd979bdSMark Fasheh 			if (status != -ENOSPC)
483ccd979bdSMark Fasheh 				mlog_errno(status);
484ccd979bdSMark Fasheh 			goto bail;
485ccd979bdSMark Fasheh 		}
486ccd979bdSMark Fasheh 		atomic_inc(&osb->alloc_stats.bg_extends);
487ccd979bdSMark Fasheh 
488ccd979bdSMark Fasheh 		/* You should never ask for this much metadata */
489ccd979bdSMark Fasheh 		BUG_ON(bits_wanted >
490ccd979bdSMark Fasheh 		       (le32_to_cpu(fe->id1.bitmap1.i_total)
491ccd979bdSMark Fasheh 			- le32_to_cpu(fe->id1.bitmap1.i_used)));
492ccd979bdSMark Fasheh 	}
493ccd979bdSMark Fasheh 
494ccd979bdSMark Fasheh 	get_bh(bh);
495ccd979bdSMark Fasheh 	ac->ac_bh = bh;
496ccd979bdSMark Fasheh bail:
497ccd979bdSMark Fasheh 	if (bh)
498ccd979bdSMark Fasheh 		brelse(bh);
499ccd979bdSMark Fasheh 
500ccd979bdSMark Fasheh 	mlog_exit(status);
501ccd979bdSMark Fasheh 	return status;
502ccd979bdSMark Fasheh }
503ccd979bdSMark Fasheh 
504cf1d6c76STiger Yang int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
505cf1d6c76STiger Yang 				      int blocks,
506ccd979bdSMark Fasheh 				      struct ocfs2_alloc_context **ac)
507ccd979bdSMark Fasheh {
508ccd979bdSMark Fasheh 	int status;
509da5cbf2fSMark Fasheh 	u32 slot;
510ccd979bdSMark Fasheh 
511cd861280SRobert P. J. Day 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
512ccd979bdSMark Fasheh 	if (!(*ac)) {
513ccd979bdSMark Fasheh 		status = -ENOMEM;
514ccd979bdSMark Fasheh 		mlog_errno(status);
515ccd979bdSMark Fasheh 		goto bail;
516ccd979bdSMark Fasheh 	}
517ccd979bdSMark Fasheh 
518cf1d6c76STiger Yang 	(*ac)->ac_bits_wanted = blocks;
519ccd979bdSMark Fasheh 	(*ac)->ac_which = OCFS2_AC_USE_META;
520da5cbf2fSMark Fasheh 	slot = osb->slot_num;
521ccd979bdSMark Fasheh 	(*ac)->ac_group_search = ocfs2_block_group_search;
522ccd979bdSMark Fasheh 
523da5cbf2fSMark Fasheh 	status = ocfs2_reserve_suballoc_bits(osb, (*ac),
524ffda89a3STao Ma 					     EXTENT_ALLOC_SYSTEM_INODE,
525ffda89a3STao Ma 					     slot, ALLOC_NEW_GROUP);
526ccd979bdSMark Fasheh 	if (status < 0) {
527ccd979bdSMark Fasheh 		if (status != -ENOSPC)
528ccd979bdSMark Fasheh 			mlog_errno(status);
529ccd979bdSMark Fasheh 		goto bail;
530ccd979bdSMark Fasheh 	}
531ccd979bdSMark Fasheh 
532ccd979bdSMark Fasheh 	status = 0;
533ccd979bdSMark Fasheh bail:
534ccd979bdSMark Fasheh 	if ((status < 0) && *ac) {
535ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(*ac);
536ccd979bdSMark Fasheh 		*ac = NULL;
537ccd979bdSMark Fasheh 	}
538ccd979bdSMark Fasheh 
539ccd979bdSMark Fasheh 	mlog_exit(status);
540ccd979bdSMark Fasheh 	return status;
541ccd979bdSMark Fasheh }
542ccd979bdSMark Fasheh 
543cf1d6c76STiger Yang int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
544cf1d6c76STiger Yang 			       struct ocfs2_extent_list *root_el,
545cf1d6c76STiger Yang 			       struct ocfs2_alloc_context **ac)
546cf1d6c76STiger Yang {
547cf1d6c76STiger Yang 	return ocfs2_reserve_new_metadata_blocks(osb,
548cf1d6c76STiger Yang 					ocfs2_extend_meta_needed(root_el),
549cf1d6c76STiger Yang 					ac);
550cf1d6c76STiger Yang }
551cf1d6c76STiger Yang 
5524d0ddb2cSTao Ma static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
5534d0ddb2cSTao Ma 					      struct ocfs2_alloc_context *ac)
5544d0ddb2cSTao Ma {
5554d0ddb2cSTao Ma 	int i, status = -ENOSPC;
5564d0ddb2cSTao Ma 	s16 slot = ocfs2_get_inode_steal_slot(osb);
5574d0ddb2cSTao Ma 
5584d0ddb2cSTao Ma 	/* Start to steal inodes from the first slot after ours. */
5594d0ddb2cSTao Ma 	if (slot == OCFS2_INVALID_SLOT)
5604d0ddb2cSTao Ma 		slot = osb->slot_num + 1;
5614d0ddb2cSTao Ma 
5624d0ddb2cSTao Ma 	for (i = 0; i < osb->max_slots; i++, slot++) {
5634d0ddb2cSTao Ma 		if (slot == osb->max_slots)
5644d0ddb2cSTao Ma 			slot = 0;
5654d0ddb2cSTao Ma 
5664d0ddb2cSTao Ma 		if (slot == osb->slot_num)
5674d0ddb2cSTao Ma 			continue;
5684d0ddb2cSTao Ma 
5694d0ddb2cSTao Ma 		status = ocfs2_reserve_suballoc_bits(osb, ac,
5704d0ddb2cSTao Ma 						     INODE_ALLOC_SYSTEM_INODE,
5714d0ddb2cSTao Ma 						     slot, NOT_ALLOC_NEW_GROUP);
5724d0ddb2cSTao Ma 		if (status >= 0) {
5734d0ddb2cSTao Ma 			ocfs2_set_inode_steal_slot(osb, slot);
5744d0ddb2cSTao Ma 			break;
5754d0ddb2cSTao Ma 		}
5764d0ddb2cSTao Ma 
5774d0ddb2cSTao Ma 		ocfs2_free_ac_resource(ac);
5784d0ddb2cSTao Ma 	}
5794d0ddb2cSTao Ma 
5804d0ddb2cSTao Ma 	return status;
5814d0ddb2cSTao Ma }
5824d0ddb2cSTao Ma 
583ccd979bdSMark Fasheh int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
584ccd979bdSMark Fasheh 			    struct ocfs2_alloc_context **ac)
585ccd979bdSMark Fasheh {
586ccd979bdSMark Fasheh 	int status;
5874d0ddb2cSTao Ma 	s16 slot = ocfs2_get_inode_steal_slot(osb);
588ccd979bdSMark Fasheh 
589cd861280SRobert P. J. Day 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
590ccd979bdSMark Fasheh 	if (!(*ac)) {
591ccd979bdSMark Fasheh 		status = -ENOMEM;
592ccd979bdSMark Fasheh 		mlog_errno(status);
593ccd979bdSMark Fasheh 		goto bail;
594ccd979bdSMark Fasheh 	}
595ccd979bdSMark Fasheh 
596ccd979bdSMark Fasheh 	(*ac)->ac_bits_wanted = 1;
597ccd979bdSMark Fasheh 	(*ac)->ac_which = OCFS2_AC_USE_INODE;
598ccd979bdSMark Fasheh 
599ccd979bdSMark Fasheh 	(*ac)->ac_group_search = ocfs2_block_group_search;
600ccd979bdSMark Fasheh 
6014d0ddb2cSTao Ma 	/*
6021187c968SJoel Becker 	 * stat(2) can't handle i_ino > 32bits, so we tell the
6031187c968SJoel Becker 	 * lower levels not to allocate us a block group past that
604*12462f1dSJoel Becker 	 * limit.  The 'inode64' mount option avoids this behavior.
6051187c968SJoel Becker 	 */
606*12462f1dSJoel Becker 	if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
6071187c968SJoel Becker 		(*ac)->ac_max_block = (u32)~0U;
6081187c968SJoel Becker 
6091187c968SJoel Becker 	/*
6104d0ddb2cSTao Ma 	 * slot is set when we successfully steal inode from other nodes.
6114d0ddb2cSTao Ma 	 * It is reset in 3 places:
6124d0ddb2cSTao Ma 	 * 1. when we flush the truncate log
6134d0ddb2cSTao Ma 	 * 2. when we complete local alloc recovery.
6144d0ddb2cSTao Ma 	 * 3. when we successfully allocate from our own slot.
6154d0ddb2cSTao Ma 	 * After it is set, we will go on stealing inodes until we find the
6164d0ddb2cSTao Ma 	 * need to check our slots to see whether there is some space for us.
6174d0ddb2cSTao Ma 	 */
6184d0ddb2cSTao Ma 	if (slot != OCFS2_INVALID_SLOT &&
6194d0ddb2cSTao Ma 	    atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_INODES_TO_STEAL)
6204d0ddb2cSTao Ma 		goto inode_steal;
6214d0ddb2cSTao Ma 
6224d0ddb2cSTao Ma 	atomic_set(&osb->s_num_inodes_stolen, 0);
623da5cbf2fSMark Fasheh 	status = ocfs2_reserve_suballoc_bits(osb, *ac,
624da5cbf2fSMark Fasheh 					     INODE_ALLOC_SYSTEM_INODE,
625ffda89a3STao Ma 					     osb->slot_num, ALLOC_NEW_GROUP);
6264d0ddb2cSTao Ma 	if (status >= 0) {
6274d0ddb2cSTao Ma 		status = 0;
6284d0ddb2cSTao Ma 
6294d0ddb2cSTao Ma 		/*
6304d0ddb2cSTao Ma 		 * Some inodes must be freed by us, so try to allocate
6314d0ddb2cSTao Ma 		 * from our own next time.
6324d0ddb2cSTao Ma 		 */
6334d0ddb2cSTao Ma 		if (slot != OCFS2_INVALID_SLOT)
6344d0ddb2cSTao Ma 			ocfs2_init_inode_steal_slot(osb);
6354d0ddb2cSTao Ma 		goto bail;
6364d0ddb2cSTao Ma 	} else if (status < 0 && status != -ENOSPC) {
6374d0ddb2cSTao Ma 		mlog_errno(status);
6384d0ddb2cSTao Ma 		goto bail;
6394d0ddb2cSTao Ma 	}
6404d0ddb2cSTao Ma 
6414d0ddb2cSTao Ma 	ocfs2_free_ac_resource(*ac);
6424d0ddb2cSTao Ma 
6434d0ddb2cSTao Ma inode_steal:
6444d0ddb2cSTao Ma 	status = ocfs2_steal_inode_from_other_nodes(osb, *ac);
6454d0ddb2cSTao Ma 	atomic_inc(&osb->s_num_inodes_stolen);
646ccd979bdSMark Fasheh 	if (status < 0) {
647ccd979bdSMark Fasheh 		if (status != -ENOSPC)
648ccd979bdSMark Fasheh 			mlog_errno(status);
649ccd979bdSMark Fasheh 		goto bail;
650ccd979bdSMark Fasheh 	}
651ccd979bdSMark Fasheh 
652ccd979bdSMark Fasheh 	status = 0;
653ccd979bdSMark Fasheh bail:
654ccd979bdSMark Fasheh 	if ((status < 0) && *ac) {
655ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(*ac);
656ccd979bdSMark Fasheh 		*ac = NULL;
657ccd979bdSMark Fasheh 	}
658ccd979bdSMark Fasheh 
659ccd979bdSMark Fasheh 	mlog_exit(status);
660ccd979bdSMark Fasheh 	return status;
661ccd979bdSMark Fasheh }
662ccd979bdSMark Fasheh 
663ccd979bdSMark Fasheh /* local alloc code has to do the same thing, so rather than do this
664ccd979bdSMark Fasheh  * twice.. */
665ccd979bdSMark Fasheh int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
666ccd979bdSMark Fasheh 				      struct ocfs2_alloc_context *ac)
667ccd979bdSMark Fasheh {
668ccd979bdSMark Fasheh 	int status;
669ccd979bdSMark Fasheh 
670ccd979bdSMark Fasheh 	ac->ac_which = OCFS2_AC_USE_MAIN;
671ccd979bdSMark Fasheh 	ac->ac_group_search = ocfs2_cluster_group_search;
672ccd979bdSMark Fasheh 
673da5cbf2fSMark Fasheh 	status = ocfs2_reserve_suballoc_bits(osb, ac,
674da5cbf2fSMark Fasheh 					     GLOBAL_BITMAP_SYSTEM_INODE,
675ffda89a3STao Ma 					     OCFS2_INVALID_SLOT,
676ffda89a3STao Ma 					     ALLOC_NEW_GROUP);
677da5cbf2fSMark Fasheh 	if (status < 0 && status != -ENOSPC) {
678ccd979bdSMark Fasheh 		mlog_errno(status);
679da5cbf2fSMark Fasheh 		goto bail;
680da5cbf2fSMark Fasheh 	}
681da5cbf2fSMark Fasheh 
682ccd979bdSMark Fasheh bail:
683ccd979bdSMark Fasheh 	return status;
684ccd979bdSMark Fasheh }
685ccd979bdSMark Fasheh 
686ccd979bdSMark Fasheh /* Callers don't need to care which bitmap (local alloc or main) to
687ccd979bdSMark Fasheh  * use so we figure it out for them, but unfortunately this clutters
688ccd979bdSMark Fasheh  * things a bit. */
6891187c968SJoel Becker static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
6901187c968SJoel Becker 					     u32 bits_wanted, u64 max_block,
691ccd979bdSMark Fasheh 					     struct ocfs2_alloc_context **ac)
692ccd979bdSMark Fasheh {
693ccd979bdSMark Fasheh 	int status;
694ccd979bdSMark Fasheh 
695ccd979bdSMark Fasheh 	mlog_entry_void();
696ccd979bdSMark Fasheh 
697cd861280SRobert P. J. Day 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
698ccd979bdSMark Fasheh 	if (!(*ac)) {
699ccd979bdSMark Fasheh 		status = -ENOMEM;
700ccd979bdSMark Fasheh 		mlog_errno(status);
701ccd979bdSMark Fasheh 		goto bail;
702ccd979bdSMark Fasheh 	}
703ccd979bdSMark Fasheh 
704ccd979bdSMark Fasheh 	(*ac)->ac_bits_wanted = bits_wanted;
7051187c968SJoel Becker 	(*ac)->ac_max_block = max_block;
706ccd979bdSMark Fasheh 
707ccd979bdSMark Fasheh 	status = -ENOSPC;
708ccd979bdSMark Fasheh 	if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
709ccd979bdSMark Fasheh 		status = ocfs2_reserve_local_alloc_bits(osb,
710ccd979bdSMark Fasheh 							bits_wanted,
711ccd979bdSMark Fasheh 							*ac);
7121187c968SJoel Becker 		if (status == -EFBIG) {
7131187c968SJoel Becker 			/* The local alloc window is outside ac_max_block.
7141187c968SJoel Becker 			 * use the main bitmap. */
7151187c968SJoel Becker 			status = -ENOSPC;
7161187c968SJoel Becker 		} else if ((status < 0) && (status != -ENOSPC)) {
717ccd979bdSMark Fasheh 			mlog_errno(status);
718ccd979bdSMark Fasheh 			goto bail;
719ccd979bdSMark Fasheh 		}
720ccd979bdSMark Fasheh 	}
721ccd979bdSMark Fasheh 
722ccd979bdSMark Fasheh 	if (status == -ENOSPC) {
723ccd979bdSMark Fasheh 		status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
724ccd979bdSMark Fasheh 		if (status < 0) {
725ccd979bdSMark Fasheh 			if (status != -ENOSPC)
726ccd979bdSMark Fasheh 				mlog_errno(status);
727ccd979bdSMark Fasheh 			goto bail;
728ccd979bdSMark Fasheh 		}
729ccd979bdSMark Fasheh 	}
730ccd979bdSMark Fasheh 
731ccd979bdSMark Fasheh 	status = 0;
732ccd979bdSMark Fasheh bail:
733ccd979bdSMark Fasheh 	if ((status < 0) && *ac) {
734ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(*ac);
735ccd979bdSMark Fasheh 		*ac = NULL;
736ccd979bdSMark Fasheh 	}
737ccd979bdSMark Fasheh 
738ccd979bdSMark Fasheh 	mlog_exit(status);
739ccd979bdSMark Fasheh 	return status;
740ccd979bdSMark Fasheh }
741ccd979bdSMark Fasheh 
7421187c968SJoel Becker int ocfs2_reserve_clusters(struct ocfs2_super *osb,
7431187c968SJoel Becker 			   u32 bits_wanted,
7441187c968SJoel Becker 			   struct ocfs2_alloc_context **ac)
7451187c968SJoel Becker {
7461187c968SJoel Becker 	return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0, ac);
7471187c968SJoel Becker }
7481187c968SJoel Becker 
749ccd979bdSMark Fasheh /*
750ccd979bdSMark Fasheh  * More or less lifted from ext3. I'll leave their description below:
751ccd979bdSMark Fasheh  *
752ccd979bdSMark Fasheh  * "For ext3 allocations, we must not reuse any blocks which are
753ccd979bdSMark Fasheh  * allocated in the bitmap buffer's "last committed data" copy.  This
754ccd979bdSMark Fasheh  * prevents deletes from freeing up the page for reuse until we have
755ccd979bdSMark Fasheh  * committed the delete transaction.
756ccd979bdSMark Fasheh  *
757ccd979bdSMark Fasheh  * If we didn't do this, then deleting something and reallocating it as
758ccd979bdSMark Fasheh  * data would allow the old block to be overwritten before the
759ccd979bdSMark Fasheh  * transaction committed (because we force data to disk before commit).
760ccd979bdSMark Fasheh  * This would lead to corruption if we crashed between overwriting the
761ccd979bdSMark Fasheh  * data and committing the delete.
762ccd979bdSMark Fasheh  *
763ccd979bdSMark Fasheh  * @@@ We may want to make this allocation behaviour conditional on
764ccd979bdSMark Fasheh  * data-writes at some point, and disable it for metadata allocations or
765ccd979bdSMark Fasheh  * sync-data inodes."
766ccd979bdSMark Fasheh  *
767ccd979bdSMark Fasheh  * Note: OCFS2 already does this differently for metadata vs data
768c78bad11SJoe Perches  * allocations, as those bitmaps are separate and undo access is never
769ccd979bdSMark Fasheh  * called on a metadata group descriptor.
770ccd979bdSMark Fasheh  */
771ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
772ccd979bdSMark Fasheh 					 int nr)
773ccd979bdSMark Fasheh {
774ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
775ccd979bdSMark Fasheh 
776ccd979bdSMark Fasheh 	if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
777ccd979bdSMark Fasheh 		return 0;
778ccd979bdSMark Fasheh 	if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
779ccd979bdSMark Fasheh 		return 1;
780ccd979bdSMark Fasheh 
781ccd979bdSMark Fasheh 	bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
782ccd979bdSMark Fasheh 	return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
783ccd979bdSMark Fasheh }
784ccd979bdSMark Fasheh 
785ccd979bdSMark Fasheh static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
786ccd979bdSMark Fasheh 					     struct buffer_head *bg_bh,
787ccd979bdSMark Fasheh 					     unsigned int bits_wanted,
7887bf72edeSMark Fasheh 					     unsigned int total_bits,
789ccd979bdSMark Fasheh 					     u16 *bit_off,
790ccd979bdSMark Fasheh 					     u16 *bits_found)
791ccd979bdSMark Fasheh {
792ccd979bdSMark Fasheh 	void *bitmap;
793ccd979bdSMark Fasheh 	u16 best_offset, best_size;
794ccd979bdSMark Fasheh 	int offset, start, found, status = 0;
795ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
796ccd979bdSMark Fasheh 
797ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
798ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg);
799ccd979bdSMark Fasheh 		return -EIO;
800ccd979bdSMark Fasheh 	}
801ccd979bdSMark Fasheh 
802ccd979bdSMark Fasheh 	found = start = best_offset = best_size = 0;
803ccd979bdSMark Fasheh 	bitmap = bg->bg_bitmap;
804ccd979bdSMark Fasheh 
8057bf72edeSMark Fasheh 	while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
8067bf72edeSMark Fasheh 		if (offset == total_bits)
807ccd979bdSMark Fasheh 			break;
808ccd979bdSMark Fasheh 
809ccd979bdSMark Fasheh 		if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
810ccd979bdSMark Fasheh 			/* We found a zero, but we can't use it as it
811ccd979bdSMark Fasheh 			 * hasn't been put to disk yet! */
812ccd979bdSMark Fasheh 			found = 0;
813ccd979bdSMark Fasheh 			start = offset + 1;
814ccd979bdSMark Fasheh 		} else if (offset == start) {
815ccd979bdSMark Fasheh 			/* we found a zero */
816ccd979bdSMark Fasheh 			found++;
817ccd979bdSMark Fasheh 			/* move start to the next bit to test */
818ccd979bdSMark Fasheh 			start++;
819ccd979bdSMark Fasheh 		} else {
820ccd979bdSMark Fasheh 			/* got a zero after some ones */
821ccd979bdSMark Fasheh 			found = 1;
822ccd979bdSMark Fasheh 			start = offset + 1;
823ccd979bdSMark Fasheh 		}
824ccd979bdSMark Fasheh 		if (found > best_size) {
825ccd979bdSMark Fasheh 			best_size = found;
826ccd979bdSMark Fasheh 			best_offset = start - found;
827ccd979bdSMark Fasheh 		}
828ccd979bdSMark Fasheh 		/* we got everything we needed */
829ccd979bdSMark Fasheh 		if (found == bits_wanted) {
830ccd979bdSMark Fasheh 			/* mlog(0, "Found it all!\n"); */
831ccd979bdSMark Fasheh 			break;
832ccd979bdSMark Fasheh 		}
833ccd979bdSMark Fasheh 	}
834ccd979bdSMark Fasheh 
835ccd979bdSMark Fasheh 	/* XXX: I think the first clause is equivalent to the second
836ccd979bdSMark Fasheh 	 * 	- jlbec */
837ccd979bdSMark Fasheh 	if (found == bits_wanted) {
838ccd979bdSMark Fasheh 		*bit_off = start - found;
839ccd979bdSMark Fasheh 		*bits_found = found;
840ccd979bdSMark Fasheh 	} else if (best_size) {
841ccd979bdSMark Fasheh 		*bit_off = best_offset;
842ccd979bdSMark Fasheh 		*bits_found = best_size;
843ccd979bdSMark Fasheh 	} else {
844ccd979bdSMark Fasheh 		status = -ENOSPC;
845ccd979bdSMark Fasheh 		/* No error log here -- see the comment above
846ccd979bdSMark Fasheh 		 * ocfs2_test_bg_bit_allocatable */
847ccd979bdSMark Fasheh 	}
848ccd979bdSMark Fasheh 
849ccd979bdSMark Fasheh 	return status;
850ccd979bdSMark Fasheh }
851ccd979bdSMark Fasheh 
8521fabe148SMark Fasheh static inline int ocfs2_block_group_set_bits(handle_t *handle,
853ccd979bdSMark Fasheh 					     struct inode *alloc_inode,
854ccd979bdSMark Fasheh 					     struct ocfs2_group_desc *bg,
855ccd979bdSMark Fasheh 					     struct buffer_head *group_bh,
856ccd979bdSMark Fasheh 					     unsigned int bit_off,
857ccd979bdSMark Fasheh 					     unsigned int num_bits)
858ccd979bdSMark Fasheh {
859ccd979bdSMark Fasheh 	int status;
860ccd979bdSMark Fasheh 	void *bitmap = bg->bg_bitmap;
861ccd979bdSMark Fasheh 	int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
862ccd979bdSMark Fasheh 
863ccd979bdSMark Fasheh 	mlog_entry_void();
864ccd979bdSMark Fasheh 
865ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
866ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
867ccd979bdSMark Fasheh 		status = -EIO;
868ccd979bdSMark Fasheh 		goto bail;
869ccd979bdSMark Fasheh 	}
870ccd979bdSMark Fasheh 	BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
871ccd979bdSMark Fasheh 
872ccd979bdSMark Fasheh 	mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
873ccd979bdSMark Fasheh 	     num_bits);
874ccd979bdSMark Fasheh 
875ccd979bdSMark Fasheh 	if (ocfs2_is_cluster_bitmap(alloc_inode))
876ccd979bdSMark Fasheh 		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
877ccd979bdSMark Fasheh 
878ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle,
879ccd979bdSMark Fasheh 				      alloc_inode,
880ccd979bdSMark Fasheh 				      group_bh,
881ccd979bdSMark Fasheh 				      journal_type);
882ccd979bdSMark Fasheh 	if (status < 0) {
883ccd979bdSMark Fasheh 		mlog_errno(status);
884ccd979bdSMark Fasheh 		goto bail;
885ccd979bdSMark Fasheh 	}
886ccd979bdSMark Fasheh 
887ccd979bdSMark Fasheh 	le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
888ccd979bdSMark Fasheh 
889ccd979bdSMark Fasheh 	while(num_bits--)
890ccd979bdSMark Fasheh 		ocfs2_set_bit(bit_off++, bitmap);
891ccd979bdSMark Fasheh 
892ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle,
893ccd979bdSMark Fasheh 				     group_bh);
894ccd979bdSMark Fasheh 	if (status < 0) {
895ccd979bdSMark Fasheh 		mlog_errno(status);
896ccd979bdSMark Fasheh 		goto bail;
897ccd979bdSMark Fasheh 	}
898ccd979bdSMark Fasheh 
899ccd979bdSMark Fasheh bail:
900ccd979bdSMark Fasheh 	mlog_exit(status);
901ccd979bdSMark Fasheh 	return status;
902ccd979bdSMark Fasheh }
903ccd979bdSMark Fasheh 
904ccd979bdSMark Fasheh /* find the one with the most empty bits */
905ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
906ccd979bdSMark Fasheh {
907ccd979bdSMark Fasheh 	u16 curr, best;
908ccd979bdSMark Fasheh 
909ccd979bdSMark Fasheh 	BUG_ON(!cl->cl_next_free_rec);
910ccd979bdSMark Fasheh 
911ccd979bdSMark Fasheh 	best = curr = 0;
912ccd979bdSMark Fasheh 	while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
913ccd979bdSMark Fasheh 		if (le32_to_cpu(cl->cl_recs[curr].c_free) >
914ccd979bdSMark Fasheh 		    le32_to_cpu(cl->cl_recs[best].c_free))
915ccd979bdSMark Fasheh 			best = curr;
916ccd979bdSMark Fasheh 		curr++;
917ccd979bdSMark Fasheh 	}
918ccd979bdSMark Fasheh 
919ccd979bdSMark Fasheh 	BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
920ccd979bdSMark Fasheh 	return best;
921ccd979bdSMark Fasheh }
922ccd979bdSMark Fasheh 
9231fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle,
924ccd979bdSMark Fasheh 				    struct inode *alloc_inode,
925ccd979bdSMark Fasheh 				    struct buffer_head *fe_bh,
926ccd979bdSMark Fasheh 				    struct buffer_head *bg_bh,
927ccd979bdSMark Fasheh 				    struct buffer_head *prev_bg_bh,
928ccd979bdSMark Fasheh 				    u16 chain)
929ccd979bdSMark Fasheh {
930ccd979bdSMark Fasheh 	int status;
931ccd979bdSMark Fasheh 	/* there is a really tiny chance the journal calls could fail,
932ccd979bdSMark Fasheh 	 * but we wouldn't want inconsistent blocks in *any* case. */
933ccd979bdSMark Fasheh 	u64 fe_ptr, bg_ptr, prev_bg_ptr;
934ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
935ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
936ccd979bdSMark Fasheh 	struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
937ccd979bdSMark Fasheh 
938ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
939ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
940ccd979bdSMark Fasheh 		status = -EIO;
941ccd979bdSMark Fasheh 		goto out;
942ccd979bdSMark Fasheh 	}
943ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
944ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
945ccd979bdSMark Fasheh 		status = -EIO;
946ccd979bdSMark Fasheh 		goto out;
947ccd979bdSMark Fasheh 	}
948ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) {
949ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg);
950ccd979bdSMark Fasheh 		status = -EIO;
951ccd979bdSMark Fasheh 		goto out;
952ccd979bdSMark Fasheh 	}
953ccd979bdSMark Fasheh 
954b0697053SMark Fasheh 	mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
9551ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
9561ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(bg->bg_blkno),
9571ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
958ccd979bdSMark Fasheh 
959ccd979bdSMark Fasheh 	fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
960ccd979bdSMark Fasheh 	bg_ptr = le64_to_cpu(bg->bg_next_group);
961ccd979bdSMark Fasheh 	prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
962ccd979bdSMark Fasheh 
963ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh,
964ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
965ccd979bdSMark Fasheh 	if (status < 0) {
966ccd979bdSMark Fasheh 		mlog_errno(status);
967ccd979bdSMark Fasheh 		goto out_rollback;
968ccd979bdSMark Fasheh 	}
969ccd979bdSMark Fasheh 
970ccd979bdSMark Fasheh 	prev_bg->bg_next_group = bg->bg_next_group;
971ccd979bdSMark Fasheh 
972ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, prev_bg_bh);
973ccd979bdSMark Fasheh 	if (status < 0) {
974ccd979bdSMark Fasheh 		mlog_errno(status);
975ccd979bdSMark Fasheh 		goto out_rollback;
976ccd979bdSMark Fasheh 	}
977ccd979bdSMark Fasheh 
978ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, bg_bh,
979ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
980ccd979bdSMark Fasheh 	if (status < 0) {
981ccd979bdSMark Fasheh 		mlog_errno(status);
982ccd979bdSMark Fasheh 		goto out_rollback;
983ccd979bdSMark Fasheh 	}
984ccd979bdSMark Fasheh 
985ccd979bdSMark Fasheh 	bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
986ccd979bdSMark Fasheh 
987ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, bg_bh);
988ccd979bdSMark Fasheh 	if (status < 0) {
989ccd979bdSMark Fasheh 		mlog_errno(status);
990ccd979bdSMark Fasheh 		goto out_rollback;
991ccd979bdSMark Fasheh 	}
992ccd979bdSMark Fasheh 
993ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
994ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
995ccd979bdSMark Fasheh 	if (status < 0) {
996ccd979bdSMark Fasheh 		mlog_errno(status);
997ccd979bdSMark Fasheh 		goto out_rollback;
998ccd979bdSMark Fasheh 	}
999ccd979bdSMark Fasheh 
1000ccd979bdSMark Fasheh 	fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
1001ccd979bdSMark Fasheh 
1002ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, fe_bh);
1003ccd979bdSMark Fasheh 	if (status < 0) {
1004ccd979bdSMark Fasheh 		mlog_errno(status);
1005ccd979bdSMark Fasheh 		goto out_rollback;
1006ccd979bdSMark Fasheh 	}
1007ccd979bdSMark Fasheh 
1008ccd979bdSMark Fasheh 	status = 0;
1009ccd979bdSMark Fasheh out_rollback:
1010ccd979bdSMark Fasheh 	if (status < 0) {
1011ccd979bdSMark Fasheh 		fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1012ccd979bdSMark Fasheh 		bg->bg_next_group = cpu_to_le64(bg_ptr);
1013ccd979bdSMark Fasheh 		prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1014ccd979bdSMark Fasheh 	}
1015ccd979bdSMark Fasheh out:
1016ccd979bdSMark Fasheh 	mlog_exit(status);
1017ccd979bdSMark Fasheh 	return status;
1018ccd979bdSMark Fasheh }
1019ccd979bdSMark Fasheh 
1020ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1021ccd979bdSMark Fasheh 						     u32 wanted)
1022ccd979bdSMark Fasheh {
1023ccd979bdSMark Fasheh 	return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1024ccd979bdSMark Fasheh }
1025ccd979bdSMark Fasheh 
1026ccd979bdSMark Fasheh /* return 0 on success, -ENOSPC to keep searching and any other < 0
1027ccd979bdSMark Fasheh  * value on error. */
1028ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode,
1029ccd979bdSMark Fasheh 				      struct buffer_head *group_bh,
1030ccd979bdSMark Fasheh 				      u32 bits_wanted, u32 min_bits,
10311187c968SJoel Becker 				      u64 max_block,
1032ccd979bdSMark Fasheh 				      u16 *bit_off, u16 *bits_found)
1033ccd979bdSMark Fasheh {
1034ccd979bdSMark Fasheh 	int search = -ENOSPC;
1035ccd979bdSMark Fasheh 	int ret;
10361187c968SJoel Becker 	u64 blkoff;
10377bf72edeSMark Fasheh 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
10389c7af40bSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1039ccd979bdSMark Fasheh 	u16 tmp_off, tmp_found;
10407bf72edeSMark Fasheh 	unsigned int max_bits, gd_cluster_off;
1041ccd979bdSMark Fasheh 
1042ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1043ccd979bdSMark Fasheh 
10447bf72edeSMark Fasheh 	if (gd->bg_free_bits_count) {
10457bf72edeSMark Fasheh 		max_bits = le16_to_cpu(gd->bg_bits);
10467bf72edeSMark Fasheh 
10477bf72edeSMark Fasheh 		/* Tail groups in cluster bitmaps which aren't cpg
10487bf72edeSMark Fasheh 		 * aligned are prone to partial extention by a failed
10497bf72edeSMark Fasheh 		 * fs resize. If the file system resize never got to
10507bf72edeSMark Fasheh 		 * update the dinode cluster count, then we don't want
10517bf72edeSMark Fasheh 		 * to trust any clusters past it, regardless of what
10527bf72edeSMark Fasheh 		 * the group descriptor says. */
10537bf72edeSMark Fasheh 		gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
10547bf72edeSMark Fasheh 							  le64_to_cpu(gd->bg_blkno));
10557bf72edeSMark Fasheh 		if ((gd_cluster_off + max_bits) >
10567bf72edeSMark Fasheh 		    OCFS2_I(inode)->ip_clusters) {
10577bf72edeSMark Fasheh 			max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
10587bf72edeSMark Fasheh 			mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
10597bf72edeSMark Fasheh 			     (unsigned long long)le64_to_cpu(gd->bg_blkno),
10607bf72edeSMark Fasheh 			     le16_to_cpu(gd->bg_bits),
10617bf72edeSMark Fasheh 			     OCFS2_I(inode)->ip_clusters, max_bits);
10627bf72edeSMark Fasheh 		}
10637bf72edeSMark Fasheh 
1064ccd979bdSMark Fasheh 		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1065ccd979bdSMark Fasheh 							group_bh, bits_wanted,
10667bf72edeSMark Fasheh 							max_bits,
1067ccd979bdSMark Fasheh 							&tmp_off, &tmp_found);
1068ccd979bdSMark Fasheh 		if (ret)
1069ccd979bdSMark Fasheh 			return ret;
1070ccd979bdSMark Fasheh 
10711187c968SJoel Becker 		if (max_block) {
10721187c968SJoel Becker 			blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
10731187c968SJoel Becker 							  gd_cluster_off +
10741187c968SJoel Becker 							  tmp_off + tmp_found);
10751187c968SJoel Becker 			mlog(0, "Checking %llu against %llu\n",
10761187c968SJoel Becker 			     (unsigned long long)blkoff,
10771187c968SJoel Becker 			     (unsigned long long)max_block);
10781187c968SJoel Becker 			if (blkoff > max_block)
10791187c968SJoel Becker 				return -ENOSPC;
10801187c968SJoel Becker 		}
10811187c968SJoel Becker 
1082ccd979bdSMark Fasheh 		/* ocfs2_block_group_find_clear_bits() might
1083ccd979bdSMark Fasheh 		 * return success, but we still want to return
1084ccd979bdSMark Fasheh 		 * -ENOSPC unless it found the minimum number
1085ccd979bdSMark Fasheh 		 * of bits. */
1086ccd979bdSMark Fasheh 		if (min_bits <= tmp_found) {
1087ccd979bdSMark Fasheh 			*bit_off = tmp_off;
1088ccd979bdSMark Fasheh 			*bits_found = tmp_found;
1089ccd979bdSMark Fasheh 			search = 0; /* success */
10909c7af40bSMark Fasheh 		} else if (tmp_found) {
10919c7af40bSMark Fasheh 			/*
10929c7af40bSMark Fasheh 			 * Don't show bits which we'll be returning
10939c7af40bSMark Fasheh 			 * for allocation to the local alloc bitmap.
10949c7af40bSMark Fasheh 			 */
10959c7af40bSMark Fasheh 			ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
1096ccd979bdSMark Fasheh 		}
1097ccd979bdSMark Fasheh 	}
1098ccd979bdSMark Fasheh 
1099ccd979bdSMark Fasheh 	return search;
1100ccd979bdSMark Fasheh }
1101ccd979bdSMark Fasheh 
1102ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode,
1103ccd979bdSMark Fasheh 				    struct buffer_head *group_bh,
1104ccd979bdSMark Fasheh 				    u32 bits_wanted, u32 min_bits,
11051187c968SJoel Becker 				    u64 max_block,
1106ccd979bdSMark Fasheh 				    u16 *bit_off, u16 *bits_found)
1107ccd979bdSMark Fasheh {
1108ccd979bdSMark Fasheh 	int ret = -ENOSPC;
11091187c968SJoel Becker 	u64 blkoff;
1110ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1111ccd979bdSMark Fasheh 
1112ccd979bdSMark Fasheh 	BUG_ON(min_bits != 1);
1113ccd979bdSMark Fasheh 	BUG_ON(ocfs2_is_cluster_bitmap(inode));
1114ccd979bdSMark Fasheh 
11151187c968SJoel Becker 	if (bg->bg_free_bits_count) {
1116ccd979bdSMark Fasheh 		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1117ccd979bdSMark Fasheh 							group_bh, bits_wanted,
11187bf72edeSMark Fasheh 							le16_to_cpu(bg->bg_bits),
1119ccd979bdSMark Fasheh 							bit_off, bits_found);
11201187c968SJoel Becker 		if (!ret && max_block) {
11211187c968SJoel Becker 			blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
11221187c968SJoel Becker 				*bits_found;
11231187c968SJoel Becker 			mlog(0, "Checking %llu against %llu\n",
11241187c968SJoel Becker 			     (unsigned long long)blkoff,
11251187c968SJoel Becker 			     (unsigned long long)max_block);
11261187c968SJoel Becker 			if (blkoff > max_block)
11271187c968SJoel Becker 				ret = -ENOSPC;
11281187c968SJoel Becker 		}
11291187c968SJoel Becker 	}
1130ccd979bdSMark Fasheh 
1131ccd979bdSMark Fasheh 	return ret;
1132ccd979bdSMark Fasheh }
1133ccd979bdSMark Fasheh 
1134883d4caeSMark Fasheh static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
11351fabe148SMark Fasheh 				       handle_t *handle,
1136883d4caeSMark Fasheh 				       struct buffer_head *di_bh,
1137883d4caeSMark Fasheh 				       u32 num_bits,
1138883d4caeSMark Fasheh 				       u16 chain)
1139883d4caeSMark Fasheh {
1140883d4caeSMark Fasheh 	int ret;
1141883d4caeSMark Fasheh 	u32 tmp_used;
1142883d4caeSMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1143883d4caeSMark Fasheh 	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1144883d4caeSMark Fasheh 
1145883d4caeSMark Fasheh 	ret = ocfs2_journal_access(handle, inode, di_bh,
1146883d4caeSMark Fasheh 				   OCFS2_JOURNAL_ACCESS_WRITE);
1147883d4caeSMark Fasheh 	if (ret < 0) {
1148883d4caeSMark Fasheh 		mlog_errno(ret);
1149883d4caeSMark Fasheh 		goto out;
1150883d4caeSMark Fasheh 	}
1151883d4caeSMark Fasheh 
1152883d4caeSMark Fasheh 	tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1153883d4caeSMark Fasheh 	di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1154883d4caeSMark Fasheh 	le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1155883d4caeSMark Fasheh 
1156883d4caeSMark Fasheh 	ret = ocfs2_journal_dirty(handle, di_bh);
1157883d4caeSMark Fasheh 	if (ret < 0)
1158883d4caeSMark Fasheh 		mlog_errno(ret);
1159883d4caeSMark Fasheh 
1160883d4caeSMark Fasheh out:
1161883d4caeSMark Fasheh 	return ret;
1162883d4caeSMark Fasheh }
1163883d4caeSMark Fasheh 
1164883d4caeSMark Fasheh static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
11651fabe148SMark Fasheh 				  handle_t *handle,
1166883d4caeSMark Fasheh 				  u32 bits_wanted,
1167883d4caeSMark Fasheh 				  u32 min_bits,
1168883d4caeSMark Fasheh 				  u16 *bit_off,
1169883d4caeSMark Fasheh 				  unsigned int *num_bits,
1170883d4caeSMark Fasheh 				  u64 gd_blkno,
1171883d4caeSMark Fasheh 				  u16 *bits_left)
1172883d4caeSMark Fasheh {
1173883d4caeSMark Fasheh 	int ret;
1174883d4caeSMark Fasheh 	u16 found;
1175883d4caeSMark Fasheh 	struct buffer_head *group_bh = NULL;
1176883d4caeSMark Fasheh 	struct ocfs2_group_desc *gd;
1177883d4caeSMark Fasheh 	struct inode *alloc_inode = ac->ac_inode;
1178883d4caeSMark Fasheh 
1179883d4caeSMark Fasheh 	ret = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), gd_blkno,
1180883d4caeSMark Fasheh 			       &group_bh, OCFS2_BH_CACHED, alloc_inode);
1181883d4caeSMark Fasheh 	if (ret < 0) {
1182883d4caeSMark Fasheh 		mlog_errno(ret);
1183883d4caeSMark Fasheh 		return ret;
1184883d4caeSMark Fasheh 	}
1185883d4caeSMark Fasheh 
1186883d4caeSMark Fasheh 	gd = (struct ocfs2_group_desc *) group_bh->b_data;
1187883d4caeSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1188883d4caeSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, gd);
1189883d4caeSMark Fasheh 		ret = -EIO;
1190883d4caeSMark Fasheh 		goto out;
1191883d4caeSMark Fasheh 	}
1192883d4caeSMark Fasheh 
1193883d4caeSMark Fasheh 	ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
11941187c968SJoel Becker 				  ac->ac_max_block, bit_off, &found);
1195883d4caeSMark Fasheh 	if (ret < 0) {
1196883d4caeSMark Fasheh 		if (ret != -ENOSPC)
1197883d4caeSMark Fasheh 			mlog_errno(ret);
1198883d4caeSMark Fasheh 		goto out;
1199883d4caeSMark Fasheh 	}
1200883d4caeSMark Fasheh 
1201883d4caeSMark Fasheh 	*num_bits = found;
1202883d4caeSMark Fasheh 
1203883d4caeSMark Fasheh 	ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1204883d4caeSMark Fasheh 					       *num_bits,
1205883d4caeSMark Fasheh 					       le16_to_cpu(gd->bg_chain));
1206883d4caeSMark Fasheh 	if (ret < 0) {
1207883d4caeSMark Fasheh 		mlog_errno(ret);
1208883d4caeSMark Fasheh 		goto out;
1209883d4caeSMark Fasheh 	}
1210883d4caeSMark Fasheh 
1211883d4caeSMark Fasheh 	ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1212883d4caeSMark Fasheh 					 *bit_off, *num_bits);
1213883d4caeSMark Fasheh 	if (ret < 0)
1214883d4caeSMark Fasheh 		mlog_errno(ret);
1215883d4caeSMark Fasheh 
1216883d4caeSMark Fasheh 	*bits_left = le16_to_cpu(gd->bg_free_bits_count);
1217883d4caeSMark Fasheh 
1218883d4caeSMark Fasheh out:
1219883d4caeSMark Fasheh 	brelse(group_bh);
1220883d4caeSMark Fasheh 
1221883d4caeSMark Fasheh 	return ret;
1222883d4caeSMark Fasheh }
1223883d4caeSMark Fasheh 
1224ccd979bdSMark Fasheh static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
12251fabe148SMark Fasheh 			      handle_t *handle,
1226ccd979bdSMark Fasheh 			      u32 bits_wanted,
1227ccd979bdSMark Fasheh 			      u32 min_bits,
1228ccd979bdSMark Fasheh 			      u16 *bit_off,
1229ccd979bdSMark Fasheh 			      unsigned int *num_bits,
1230883d4caeSMark Fasheh 			      u64 *bg_blkno,
1231883d4caeSMark Fasheh 			      u16 *bits_left)
1232ccd979bdSMark Fasheh {
1233ccd979bdSMark Fasheh 	int status;
1234ccd979bdSMark Fasheh 	u16 chain, tmp_bits;
1235ccd979bdSMark Fasheh 	u32 tmp_used;
1236ccd979bdSMark Fasheh 	u64 next_group;
1237ccd979bdSMark Fasheh 	struct inode *alloc_inode = ac->ac_inode;
1238ccd979bdSMark Fasheh 	struct buffer_head *group_bh = NULL;
1239ccd979bdSMark Fasheh 	struct buffer_head *prev_group_bh = NULL;
1240ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1241ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1242ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg;
1243ccd979bdSMark Fasheh 
1244ccd979bdSMark Fasheh 	chain = ac->ac_chain;
1245b0697053SMark Fasheh 	mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1246b0697053SMark Fasheh 	     bits_wanted, chain,
1247b0697053SMark Fasheh 	     (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
1248ccd979bdSMark Fasheh 
1249ccd979bdSMark Fasheh 	status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1250ccd979bdSMark Fasheh 				  le64_to_cpu(cl->cl_recs[chain].c_blkno),
1251ccd979bdSMark Fasheh 				  &group_bh, OCFS2_BH_CACHED, alloc_inode);
1252ccd979bdSMark Fasheh 	if (status < 0) {
1253ccd979bdSMark Fasheh 		mlog_errno(status);
1254ccd979bdSMark Fasheh 		goto bail;
1255ccd979bdSMark Fasheh 	}
1256ccd979bdSMark Fasheh 	bg = (struct ocfs2_group_desc *) group_bh->b_data;
12577bf72edeSMark Fasheh 	status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
12587bf72edeSMark Fasheh 	if (status) {
12597bf72edeSMark Fasheh 		mlog_errno(status);
1260ccd979bdSMark Fasheh 		goto bail;
1261ccd979bdSMark Fasheh 	}
1262ccd979bdSMark Fasheh 
1263ccd979bdSMark Fasheh 	status = -ENOSPC;
1264ccd979bdSMark Fasheh 	/* for now, the chain search is a bit simplistic. We just use
1265ccd979bdSMark Fasheh 	 * the 1st group with any empty bits. */
12661187c968SJoel Becker 	while ((status = ac->ac_group_search(alloc_inode, group_bh,
12671187c968SJoel Becker 					     bits_wanted, min_bits,
12681187c968SJoel Becker 					     ac->ac_max_block, bit_off,
12691187c968SJoel Becker 					     &tmp_bits)) == -ENOSPC) {
1270ccd979bdSMark Fasheh 		if (!bg->bg_next_group)
1271ccd979bdSMark Fasheh 			break;
1272ccd979bdSMark Fasheh 		if (prev_group_bh) {
1273ccd979bdSMark Fasheh 			brelse(prev_group_bh);
1274ccd979bdSMark Fasheh 			prev_group_bh = NULL;
1275ccd979bdSMark Fasheh 		}
1276ccd979bdSMark Fasheh 		next_group = le64_to_cpu(bg->bg_next_group);
1277ccd979bdSMark Fasheh 		prev_group_bh = group_bh;
1278ccd979bdSMark Fasheh 		group_bh = NULL;
1279ccd979bdSMark Fasheh 		status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1280ccd979bdSMark Fasheh 					  next_group, &group_bh,
1281ccd979bdSMark Fasheh 					  OCFS2_BH_CACHED, alloc_inode);
1282ccd979bdSMark Fasheh 		if (status < 0) {
1283ccd979bdSMark Fasheh 			mlog_errno(status);
1284ccd979bdSMark Fasheh 			goto bail;
1285ccd979bdSMark Fasheh 		}
1286ccd979bdSMark Fasheh 		bg = (struct ocfs2_group_desc *) group_bh->b_data;
12877bf72edeSMark Fasheh 		status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
12887bf72edeSMark Fasheh 		if (status) {
12897bf72edeSMark Fasheh 			mlog_errno(status);
1290ccd979bdSMark Fasheh 			goto bail;
1291ccd979bdSMark Fasheh 		}
1292ccd979bdSMark Fasheh 	}
1293ccd979bdSMark Fasheh 	if (status < 0) {
1294ccd979bdSMark Fasheh 		if (status != -ENOSPC)
1295ccd979bdSMark Fasheh 			mlog_errno(status);
1296ccd979bdSMark Fasheh 		goto bail;
1297ccd979bdSMark Fasheh 	}
1298ccd979bdSMark Fasheh 
1299b0697053SMark Fasheh 	mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
13001ca1a111SMark Fasheh 	     tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
1301ccd979bdSMark Fasheh 
1302ccd979bdSMark Fasheh 	*num_bits = tmp_bits;
1303ccd979bdSMark Fasheh 
1304ccd979bdSMark Fasheh 	BUG_ON(*num_bits == 0);
1305ccd979bdSMark Fasheh 
1306ccd979bdSMark Fasheh 	/*
1307ccd979bdSMark Fasheh 	 * Keep track of previous block descriptor read. When
1308ccd979bdSMark Fasheh 	 * we find a target, if we have read more than X
1309ccd979bdSMark Fasheh 	 * number of descriptors, and the target is reasonably
1310ccd979bdSMark Fasheh 	 * empty, relink him to top of his chain.
1311ccd979bdSMark Fasheh 	 *
1312ccd979bdSMark Fasheh 	 * We've read 0 extra blocks and only send one more to
1313ccd979bdSMark Fasheh 	 * the transaction, yet the next guy to search has a
1314ccd979bdSMark Fasheh 	 * much easier time.
1315ccd979bdSMark Fasheh 	 *
1316ccd979bdSMark Fasheh 	 * Do this *after* figuring out how many bits we're taking out
1317ccd979bdSMark Fasheh 	 * of our target group.
1318ccd979bdSMark Fasheh 	 */
1319ccd979bdSMark Fasheh 	if (ac->ac_allow_chain_relink &&
1320ccd979bdSMark Fasheh 	    (prev_group_bh) &&
1321ccd979bdSMark Fasheh 	    (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1322ccd979bdSMark Fasheh 		status = ocfs2_relink_block_group(handle, alloc_inode,
1323ccd979bdSMark Fasheh 						  ac->ac_bh, group_bh,
1324ccd979bdSMark Fasheh 						  prev_group_bh, chain);
1325ccd979bdSMark Fasheh 		if (status < 0) {
1326ccd979bdSMark Fasheh 			mlog_errno(status);
1327ccd979bdSMark Fasheh 			goto bail;
1328ccd979bdSMark Fasheh 		}
1329ccd979bdSMark Fasheh 	}
1330ccd979bdSMark Fasheh 
1331ccd979bdSMark Fasheh 	/* Ok, claim our bits now: set the info on dinode, chainlist
1332ccd979bdSMark Fasheh 	 * and then the group */
1333ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle,
1334ccd979bdSMark Fasheh 				      alloc_inode,
1335ccd979bdSMark Fasheh 				      ac->ac_bh,
1336ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
1337ccd979bdSMark Fasheh 	if (status < 0) {
1338ccd979bdSMark Fasheh 		mlog_errno(status);
1339ccd979bdSMark Fasheh 		goto bail;
1340ccd979bdSMark Fasheh 	}
1341ccd979bdSMark Fasheh 
1342ccd979bdSMark Fasheh 	tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1343ccd979bdSMark Fasheh 	fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1344ccd979bdSMark Fasheh 	le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1345ccd979bdSMark Fasheh 
1346ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle,
1347ccd979bdSMark Fasheh 				     ac->ac_bh);
1348ccd979bdSMark Fasheh 	if (status < 0) {
1349ccd979bdSMark Fasheh 		mlog_errno(status);
1350ccd979bdSMark Fasheh 		goto bail;
1351ccd979bdSMark Fasheh 	}
1352ccd979bdSMark Fasheh 
1353ccd979bdSMark Fasheh 	status = ocfs2_block_group_set_bits(handle,
1354ccd979bdSMark Fasheh 					    alloc_inode,
1355ccd979bdSMark Fasheh 					    bg,
1356ccd979bdSMark Fasheh 					    group_bh,
1357ccd979bdSMark Fasheh 					    *bit_off,
1358ccd979bdSMark Fasheh 					    *num_bits);
1359ccd979bdSMark Fasheh 	if (status < 0) {
1360ccd979bdSMark Fasheh 		mlog_errno(status);
1361ccd979bdSMark Fasheh 		goto bail;
1362ccd979bdSMark Fasheh 	}
1363ccd979bdSMark Fasheh 
1364b0697053SMark Fasheh 	mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
13651ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(fe->i_blkno));
1366ccd979bdSMark Fasheh 
1367ccd979bdSMark Fasheh 	*bg_blkno = le64_to_cpu(bg->bg_blkno);
1368883d4caeSMark Fasheh 	*bits_left = le16_to_cpu(bg->bg_free_bits_count);
1369ccd979bdSMark Fasheh bail:
1370ccd979bdSMark Fasheh 	if (group_bh)
1371ccd979bdSMark Fasheh 		brelse(group_bh);
1372ccd979bdSMark Fasheh 	if (prev_group_bh)
1373ccd979bdSMark Fasheh 		brelse(prev_group_bh);
1374ccd979bdSMark Fasheh 
1375ccd979bdSMark Fasheh 	mlog_exit(status);
1376ccd979bdSMark Fasheh 	return status;
1377ccd979bdSMark Fasheh }
1378ccd979bdSMark Fasheh 
1379ccd979bdSMark Fasheh /* will give out up to bits_wanted contiguous bits. */
1380ccd979bdSMark Fasheh static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1381ccd979bdSMark Fasheh 				     struct ocfs2_alloc_context *ac,
13821fabe148SMark Fasheh 				     handle_t *handle,
1383ccd979bdSMark Fasheh 				     u32 bits_wanted,
1384ccd979bdSMark Fasheh 				     u32 min_bits,
1385ccd979bdSMark Fasheh 				     u16 *bit_off,
1386ccd979bdSMark Fasheh 				     unsigned int *num_bits,
1387ccd979bdSMark Fasheh 				     u64 *bg_blkno)
1388ccd979bdSMark Fasheh {
1389ccd979bdSMark Fasheh 	int status;
1390ccd979bdSMark Fasheh 	u16 victim, i;
1391883d4caeSMark Fasheh 	u16 bits_left = 0;
1392883d4caeSMark Fasheh 	u64 hint_blkno = ac->ac_last_group;
1393ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl;
1394ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
1395ccd979bdSMark Fasheh 
1396ccd979bdSMark Fasheh 	mlog_entry_void();
1397ccd979bdSMark Fasheh 
1398ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1399ccd979bdSMark Fasheh 	BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1400ccd979bdSMark Fasheh 	BUG_ON(!ac->ac_bh);
1401ccd979bdSMark Fasheh 
1402ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1403ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
1404ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(osb->sb, fe);
1405ccd979bdSMark Fasheh 		status = -EIO;
1406ccd979bdSMark Fasheh 		goto bail;
1407ccd979bdSMark Fasheh 	}
1408ccd979bdSMark Fasheh 	if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1409ccd979bdSMark Fasheh 	    le32_to_cpu(fe->id1.bitmap1.i_total)) {
1410b0697053SMark Fasheh 		ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1411b0697053SMark Fasheh 			    "bits but only %u total.",
1412b0697053SMark Fasheh 			    (unsigned long long)le64_to_cpu(fe->i_blkno),
1413ccd979bdSMark Fasheh 			    le32_to_cpu(fe->id1.bitmap1.i_used),
1414ccd979bdSMark Fasheh 			    le32_to_cpu(fe->id1.bitmap1.i_total));
1415ccd979bdSMark Fasheh 		status = -EIO;
1416ccd979bdSMark Fasheh 		goto bail;
1417ccd979bdSMark Fasheh 	}
1418ccd979bdSMark Fasheh 
1419883d4caeSMark Fasheh 	if (hint_blkno) {
1420883d4caeSMark Fasheh 		/* Attempt to short-circuit the usual search mechanism
1421883d4caeSMark Fasheh 		 * by jumping straight to the most recently used
1422883d4caeSMark Fasheh 		 * allocation group. This helps us mantain some
1423883d4caeSMark Fasheh 		 * contiguousness across allocations. */
1424da5cbf2fSMark Fasheh 		status = ocfs2_search_one_group(ac, handle, bits_wanted,
1425da5cbf2fSMark Fasheh 						min_bits, bit_off, num_bits,
1426883d4caeSMark Fasheh 						hint_blkno, &bits_left);
1427883d4caeSMark Fasheh 		if (!status) {
1428883d4caeSMark Fasheh 			/* Be careful to update *bg_blkno here as the
1429883d4caeSMark Fasheh 			 * caller is expecting it to be filled in, and
1430883d4caeSMark Fasheh 			 * ocfs2_search_one_group() won't do that for
1431883d4caeSMark Fasheh 			 * us. */
1432883d4caeSMark Fasheh 			*bg_blkno = hint_blkno;
1433883d4caeSMark Fasheh 			goto set_hint;
1434883d4caeSMark Fasheh 		}
1435883d4caeSMark Fasheh 		if (status < 0 && status != -ENOSPC) {
1436883d4caeSMark Fasheh 			mlog_errno(status);
1437883d4caeSMark Fasheh 			goto bail;
1438883d4caeSMark Fasheh 		}
1439883d4caeSMark Fasheh 	}
1440883d4caeSMark Fasheh 
1441ccd979bdSMark Fasheh 	cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1442ccd979bdSMark Fasheh 
1443ccd979bdSMark Fasheh 	victim = ocfs2_find_victim_chain(cl);
1444ccd979bdSMark Fasheh 	ac->ac_chain = victim;
1445ccd979bdSMark Fasheh 	ac->ac_allow_chain_relink = 1;
1446ccd979bdSMark Fasheh 
1447da5cbf2fSMark Fasheh 	status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
1448883d4caeSMark Fasheh 				    num_bits, bg_blkno, &bits_left);
1449ccd979bdSMark Fasheh 	if (!status)
1450883d4caeSMark Fasheh 		goto set_hint;
1451ccd979bdSMark Fasheh 	if (status < 0 && status != -ENOSPC) {
1452ccd979bdSMark Fasheh 		mlog_errno(status);
1453ccd979bdSMark Fasheh 		goto bail;
1454ccd979bdSMark Fasheh 	}
1455ccd979bdSMark Fasheh 
1456ccd979bdSMark Fasheh 	mlog(0, "Search of victim chain %u came up with nothing, "
1457ccd979bdSMark Fasheh 	     "trying all chains now.\n", victim);
1458ccd979bdSMark Fasheh 
1459ccd979bdSMark Fasheh 	/* If we didn't pick a good victim, then just default to
1460ccd979bdSMark Fasheh 	 * searching each chain in order. Don't allow chain relinking
1461ccd979bdSMark Fasheh 	 * because we only calculate enough journal credits for one
1462ccd979bdSMark Fasheh 	 * relink per alloc. */
1463ccd979bdSMark Fasheh 	ac->ac_allow_chain_relink = 0;
1464ccd979bdSMark Fasheh 	for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1465ccd979bdSMark Fasheh 		if (i == victim)
1466ccd979bdSMark Fasheh 			continue;
1467ccd979bdSMark Fasheh 		if (!cl->cl_recs[i].c_free)
1468ccd979bdSMark Fasheh 			continue;
1469ccd979bdSMark Fasheh 
1470ccd979bdSMark Fasheh 		ac->ac_chain = i;
1471da5cbf2fSMark Fasheh 		status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1472883d4caeSMark Fasheh 					    bit_off, num_bits, bg_blkno,
1473883d4caeSMark Fasheh 					    &bits_left);
1474ccd979bdSMark Fasheh 		if (!status)
1475ccd979bdSMark Fasheh 			break;
1476ccd979bdSMark Fasheh 		if (status < 0 && status != -ENOSPC) {
1477ccd979bdSMark Fasheh 			mlog_errno(status);
1478ccd979bdSMark Fasheh 			goto bail;
1479ccd979bdSMark Fasheh 		}
1480ccd979bdSMark Fasheh 	}
1481ccd979bdSMark Fasheh 
1482883d4caeSMark Fasheh set_hint:
1483883d4caeSMark Fasheh 	if (status != -ENOSPC) {
1484883d4caeSMark Fasheh 		/* If the next search of this group is not likely to
1485883d4caeSMark Fasheh 		 * yield a suitable extent, then we reset the last
1486883d4caeSMark Fasheh 		 * group hint so as to not waste a disk read */
1487883d4caeSMark Fasheh 		if (bits_left < min_bits)
1488883d4caeSMark Fasheh 			ac->ac_last_group = 0;
1489883d4caeSMark Fasheh 		else
1490883d4caeSMark Fasheh 			ac->ac_last_group = *bg_blkno;
1491883d4caeSMark Fasheh 	}
1492883d4caeSMark Fasheh 
1493883d4caeSMark Fasheh bail:
1494ccd979bdSMark Fasheh 	mlog_exit(status);
1495ccd979bdSMark Fasheh 	return status;
1496ccd979bdSMark Fasheh }
1497ccd979bdSMark Fasheh 
1498ccd979bdSMark Fasheh int ocfs2_claim_metadata(struct ocfs2_super *osb,
14991fabe148SMark Fasheh 			 handle_t *handle,
1500ccd979bdSMark Fasheh 			 struct ocfs2_alloc_context *ac,
1501ccd979bdSMark Fasheh 			 u32 bits_wanted,
1502ccd979bdSMark Fasheh 			 u16 *suballoc_bit_start,
1503ccd979bdSMark Fasheh 			 unsigned int *num_bits,
1504ccd979bdSMark Fasheh 			 u64 *blkno_start)
1505ccd979bdSMark Fasheh {
1506ccd979bdSMark Fasheh 	int status;
1507ccd979bdSMark Fasheh 	u64 bg_blkno;
1508ccd979bdSMark Fasheh 
1509ccd979bdSMark Fasheh 	BUG_ON(!ac);
1510ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1511ccd979bdSMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
1512ccd979bdSMark Fasheh 
1513ccd979bdSMark Fasheh 	status = ocfs2_claim_suballoc_bits(osb,
1514ccd979bdSMark Fasheh 					   ac,
1515da5cbf2fSMark Fasheh 					   handle,
1516ccd979bdSMark Fasheh 					   bits_wanted,
1517ccd979bdSMark Fasheh 					   1,
1518ccd979bdSMark Fasheh 					   suballoc_bit_start,
1519ccd979bdSMark Fasheh 					   num_bits,
1520ccd979bdSMark Fasheh 					   &bg_blkno);
1521ccd979bdSMark Fasheh 	if (status < 0) {
1522ccd979bdSMark Fasheh 		mlog_errno(status);
1523ccd979bdSMark Fasheh 		goto bail;
1524ccd979bdSMark Fasheh 	}
1525ccd979bdSMark Fasheh 	atomic_inc(&osb->alloc_stats.bg_allocs);
1526ccd979bdSMark Fasheh 
1527ccd979bdSMark Fasheh 	*blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1528ccd979bdSMark Fasheh 	ac->ac_bits_given += (*num_bits);
1529ccd979bdSMark Fasheh 	status = 0;
1530ccd979bdSMark Fasheh bail:
1531ccd979bdSMark Fasheh 	mlog_exit(status);
1532ccd979bdSMark Fasheh 	return status;
1533ccd979bdSMark Fasheh }
1534ccd979bdSMark Fasheh 
1535ccd979bdSMark Fasheh int ocfs2_claim_new_inode(struct ocfs2_super *osb,
15361fabe148SMark Fasheh 			  handle_t *handle,
1537ccd979bdSMark Fasheh 			  struct ocfs2_alloc_context *ac,
1538ccd979bdSMark Fasheh 			  u16 *suballoc_bit,
1539ccd979bdSMark Fasheh 			  u64 *fe_blkno)
1540ccd979bdSMark Fasheh {
1541ccd979bdSMark Fasheh 	int status;
1542ccd979bdSMark Fasheh 	unsigned int num_bits;
1543ccd979bdSMark Fasheh 	u64 bg_blkno;
1544ccd979bdSMark Fasheh 
1545ccd979bdSMark Fasheh 	mlog_entry_void();
1546ccd979bdSMark Fasheh 
1547ccd979bdSMark Fasheh 	BUG_ON(!ac);
1548ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_given != 0);
1549ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_wanted != 1);
1550ccd979bdSMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
1551ccd979bdSMark Fasheh 
1552ccd979bdSMark Fasheh 	status = ocfs2_claim_suballoc_bits(osb,
1553ccd979bdSMark Fasheh 					   ac,
1554da5cbf2fSMark Fasheh 					   handle,
1555ccd979bdSMark Fasheh 					   1,
1556ccd979bdSMark Fasheh 					   1,
1557ccd979bdSMark Fasheh 					   suballoc_bit,
1558ccd979bdSMark Fasheh 					   &num_bits,
1559ccd979bdSMark Fasheh 					   &bg_blkno);
1560ccd979bdSMark Fasheh 	if (status < 0) {
1561ccd979bdSMark Fasheh 		mlog_errno(status);
1562ccd979bdSMark Fasheh 		goto bail;
1563ccd979bdSMark Fasheh 	}
1564ccd979bdSMark Fasheh 	atomic_inc(&osb->alloc_stats.bg_allocs);
1565ccd979bdSMark Fasheh 
1566ccd979bdSMark Fasheh 	BUG_ON(num_bits != 1);
1567ccd979bdSMark Fasheh 
1568ccd979bdSMark Fasheh 	*fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1569ccd979bdSMark Fasheh 	ac->ac_bits_given++;
1570ccd979bdSMark Fasheh 	status = 0;
1571ccd979bdSMark Fasheh bail:
1572ccd979bdSMark Fasheh 	mlog_exit(status);
1573ccd979bdSMark Fasheh 	return status;
1574ccd979bdSMark Fasheh }
1575ccd979bdSMark Fasheh 
1576ccd979bdSMark Fasheh /* translate a group desc. blkno and it's bitmap offset into
1577ccd979bdSMark Fasheh  * disk cluster offset. */
1578ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1579ccd979bdSMark Fasheh 						   u64 bg_blkno,
1580ccd979bdSMark Fasheh 						   u16 bg_bit_off)
1581ccd979bdSMark Fasheh {
1582ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1583ccd979bdSMark Fasheh 	u32 cluster = 0;
1584ccd979bdSMark Fasheh 
1585ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1586ccd979bdSMark Fasheh 
1587ccd979bdSMark Fasheh 	if (bg_blkno != osb->first_cluster_group_blkno)
1588ccd979bdSMark Fasheh 		cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1589ccd979bdSMark Fasheh 	cluster += (u32) bg_bit_off;
1590ccd979bdSMark Fasheh 	return cluster;
1591ccd979bdSMark Fasheh }
1592ccd979bdSMark Fasheh 
1593ccd979bdSMark Fasheh /* given a cluster offset, calculate which block group it belongs to
1594ccd979bdSMark Fasheh  * and return that block offset. */
1595d659072fSTao Ma u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
1596ccd979bdSMark Fasheh {
1597ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1598ccd979bdSMark Fasheh 	u32 group_no;
1599ccd979bdSMark Fasheh 
1600ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1601ccd979bdSMark Fasheh 
1602ccd979bdSMark Fasheh 	group_no = cluster / osb->bitmap_cpg;
1603ccd979bdSMark Fasheh 	if (!group_no)
1604ccd979bdSMark Fasheh 		return osb->first_cluster_group_blkno;
1605ccd979bdSMark Fasheh 	return ocfs2_clusters_to_blocks(inode->i_sb,
1606ccd979bdSMark Fasheh 					group_no * osb->bitmap_cpg);
1607ccd979bdSMark Fasheh }
1608ccd979bdSMark Fasheh 
1609ccd979bdSMark Fasheh /* given the block number of a cluster start, calculate which cluster
1610ccd979bdSMark Fasheh  * group and descriptor bitmap offset that corresponds to. */
1611ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1612ccd979bdSMark Fasheh 						u64 data_blkno,
1613ccd979bdSMark Fasheh 						u64 *bg_blkno,
1614ccd979bdSMark Fasheh 						u16 *bg_bit_off)
1615ccd979bdSMark Fasheh {
1616ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1617ccd979bdSMark Fasheh 	u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1618ccd979bdSMark Fasheh 
1619ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1620ccd979bdSMark Fasheh 
1621ccd979bdSMark Fasheh 	*bg_blkno = ocfs2_which_cluster_group(inode,
1622ccd979bdSMark Fasheh 					      data_cluster);
1623ccd979bdSMark Fasheh 
1624ccd979bdSMark Fasheh 	if (*bg_blkno == osb->first_cluster_group_blkno)
1625ccd979bdSMark Fasheh 		*bg_bit_off = (u16) data_cluster;
1626ccd979bdSMark Fasheh 	else
1627ccd979bdSMark Fasheh 		*bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1628ccd979bdSMark Fasheh 							     data_blkno - *bg_blkno);
1629ccd979bdSMark Fasheh }
1630ccd979bdSMark Fasheh 
1631ccd979bdSMark Fasheh /*
1632ccd979bdSMark Fasheh  * min_bits - minimum contiguous chunk from this total allocation we
1633ccd979bdSMark Fasheh  * can handle. set to what we asked for originally for a full
1634ccd979bdSMark Fasheh  * contig. allocation, set to '1' to indicate we can deal with extents
1635ccd979bdSMark Fasheh  * of any size.
1636ccd979bdSMark Fasheh  */
1637415cb800SMark Fasheh int __ocfs2_claim_clusters(struct ocfs2_super *osb,
16381fabe148SMark Fasheh 			   handle_t *handle,
1639ccd979bdSMark Fasheh 			   struct ocfs2_alloc_context *ac,
1640ccd979bdSMark Fasheh 			   u32 min_clusters,
1641415cb800SMark Fasheh 			   u32 max_clusters,
1642ccd979bdSMark Fasheh 			   u32 *cluster_start,
1643ccd979bdSMark Fasheh 			   u32 *num_clusters)
1644ccd979bdSMark Fasheh {
1645ccd979bdSMark Fasheh 	int status;
1646415cb800SMark Fasheh 	unsigned int bits_wanted = max_clusters;
1647883d4caeSMark Fasheh 	u64 bg_blkno = 0;
1648ccd979bdSMark Fasheh 	u16 bg_bit_off;
1649ccd979bdSMark Fasheh 
1650ccd979bdSMark Fasheh 	mlog_entry_void();
1651ccd979bdSMark Fasheh 
1652ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1653ccd979bdSMark Fasheh 
1654ccd979bdSMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1655ccd979bdSMark Fasheh 	       && ac->ac_which != OCFS2_AC_USE_MAIN);
1656ccd979bdSMark Fasheh 
1657ccd979bdSMark Fasheh 	if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1658ccd979bdSMark Fasheh 		status = ocfs2_claim_local_alloc_bits(osb,
1659ccd979bdSMark Fasheh 						      handle,
1660ccd979bdSMark Fasheh 						      ac,
1661ccd979bdSMark Fasheh 						      bits_wanted,
1662ccd979bdSMark Fasheh 						      cluster_start,
1663ccd979bdSMark Fasheh 						      num_clusters);
1664ccd979bdSMark Fasheh 		if (!status)
1665ccd979bdSMark Fasheh 			atomic_inc(&osb->alloc_stats.local_data);
1666ccd979bdSMark Fasheh 	} else {
1667ccd979bdSMark Fasheh 		if (min_clusters > (osb->bitmap_cpg - 1)) {
1668ccd979bdSMark Fasheh 			/* The only paths asking for contiguousness
1669ccd979bdSMark Fasheh 			 * should know about this already. */
16702fbe8d1eSSunil Mushran 			mlog(ML_ERROR, "minimum allocation requested %u exceeds "
16712fbe8d1eSSunil Mushran 			     "group bitmap size %u!\n", min_clusters,
16722fbe8d1eSSunil Mushran 			     osb->bitmap_cpg);
1673ccd979bdSMark Fasheh 			status = -ENOSPC;
1674ccd979bdSMark Fasheh 			goto bail;
1675ccd979bdSMark Fasheh 		}
1676ccd979bdSMark Fasheh 		/* clamp the current request down to a realistic size. */
1677ccd979bdSMark Fasheh 		if (bits_wanted > (osb->bitmap_cpg - 1))
1678ccd979bdSMark Fasheh 			bits_wanted = osb->bitmap_cpg - 1;
1679ccd979bdSMark Fasheh 
1680ccd979bdSMark Fasheh 		status = ocfs2_claim_suballoc_bits(osb,
1681ccd979bdSMark Fasheh 						   ac,
1682da5cbf2fSMark Fasheh 						   handle,
1683ccd979bdSMark Fasheh 						   bits_wanted,
1684ccd979bdSMark Fasheh 						   min_clusters,
1685ccd979bdSMark Fasheh 						   &bg_bit_off,
1686ccd979bdSMark Fasheh 						   num_clusters,
1687ccd979bdSMark Fasheh 						   &bg_blkno);
1688ccd979bdSMark Fasheh 		if (!status) {
1689ccd979bdSMark Fasheh 			*cluster_start =
1690ccd979bdSMark Fasheh 				ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1691ccd979bdSMark Fasheh 								 bg_blkno,
1692ccd979bdSMark Fasheh 								 bg_bit_off);
1693ccd979bdSMark Fasheh 			atomic_inc(&osb->alloc_stats.bitmap_data);
1694ccd979bdSMark Fasheh 		}
1695ccd979bdSMark Fasheh 	}
1696ccd979bdSMark Fasheh 	if (status < 0) {
1697ccd979bdSMark Fasheh 		if (status != -ENOSPC)
1698ccd979bdSMark Fasheh 			mlog_errno(status);
1699ccd979bdSMark Fasheh 		goto bail;
1700ccd979bdSMark Fasheh 	}
1701ccd979bdSMark Fasheh 
1702ccd979bdSMark Fasheh 	ac->ac_bits_given += *num_clusters;
1703ccd979bdSMark Fasheh 
1704ccd979bdSMark Fasheh bail:
1705ccd979bdSMark Fasheh 	mlog_exit(status);
1706ccd979bdSMark Fasheh 	return status;
1707ccd979bdSMark Fasheh }
1708ccd979bdSMark Fasheh 
1709415cb800SMark Fasheh int ocfs2_claim_clusters(struct ocfs2_super *osb,
1710415cb800SMark Fasheh 			 handle_t *handle,
1711415cb800SMark Fasheh 			 struct ocfs2_alloc_context *ac,
1712415cb800SMark Fasheh 			 u32 min_clusters,
1713415cb800SMark Fasheh 			 u32 *cluster_start,
1714415cb800SMark Fasheh 			 u32 *num_clusters)
1715415cb800SMark Fasheh {
1716415cb800SMark Fasheh 	unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1717415cb800SMark Fasheh 
1718415cb800SMark Fasheh 	return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1719415cb800SMark Fasheh 				      bits_wanted, cluster_start, num_clusters);
1720415cb800SMark Fasheh }
1721415cb800SMark Fasheh 
17221fabe148SMark Fasheh static inline int ocfs2_block_group_clear_bits(handle_t *handle,
1723ccd979bdSMark Fasheh 					       struct inode *alloc_inode,
1724ccd979bdSMark Fasheh 					       struct ocfs2_group_desc *bg,
1725ccd979bdSMark Fasheh 					       struct buffer_head *group_bh,
1726ccd979bdSMark Fasheh 					       unsigned int bit_off,
1727ccd979bdSMark Fasheh 					       unsigned int num_bits)
1728ccd979bdSMark Fasheh {
1729ccd979bdSMark Fasheh 	int status;
1730ccd979bdSMark Fasheh 	unsigned int tmp;
1731ccd979bdSMark Fasheh 	int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1732ccd979bdSMark Fasheh 	struct ocfs2_group_desc *undo_bg = NULL;
1733ccd979bdSMark Fasheh 
1734ccd979bdSMark Fasheh 	mlog_entry_void();
1735ccd979bdSMark Fasheh 
1736ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
1737ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
1738ccd979bdSMark Fasheh 		status = -EIO;
1739ccd979bdSMark Fasheh 		goto bail;
1740ccd979bdSMark Fasheh 	}
1741ccd979bdSMark Fasheh 
1742ccd979bdSMark Fasheh 	mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1743ccd979bdSMark Fasheh 
1744ccd979bdSMark Fasheh 	if (ocfs2_is_cluster_bitmap(alloc_inode))
1745ccd979bdSMark Fasheh 		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1746ccd979bdSMark Fasheh 
1747ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, group_bh,
1748ccd979bdSMark Fasheh 				      journal_type);
1749ccd979bdSMark Fasheh 	if (status < 0) {
1750ccd979bdSMark Fasheh 		mlog_errno(status);
1751ccd979bdSMark Fasheh 		goto bail;
1752ccd979bdSMark Fasheh 	}
1753ccd979bdSMark Fasheh 
1754ccd979bdSMark Fasheh 	if (ocfs2_is_cluster_bitmap(alloc_inode))
1755ccd979bdSMark Fasheh 		undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
1756ccd979bdSMark Fasheh 
1757ccd979bdSMark Fasheh 	tmp = num_bits;
1758ccd979bdSMark Fasheh 	while(tmp--) {
1759ccd979bdSMark Fasheh 		ocfs2_clear_bit((bit_off + tmp),
1760ccd979bdSMark Fasheh 				(unsigned long *) bg->bg_bitmap);
1761ccd979bdSMark Fasheh 		if (ocfs2_is_cluster_bitmap(alloc_inode))
1762ccd979bdSMark Fasheh 			ocfs2_set_bit(bit_off + tmp,
1763ccd979bdSMark Fasheh 				      (unsigned long *) undo_bg->bg_bitmap);
1764ccd979bdSMark Fasheh 	}
1765ccd979bdSMark Fasheh 	le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1766ccd979bdSMark Fasheh 
1767ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, group_bh);
1768ccd979bdSMark Fasheh 	if (status < 0)
1769ccd979bdSMark Fasheh 		mlog_errno(status);
1770ccd979bdSMark Fasheh bail:
1771ccd979bdSMark Fasheh 	return status;
1772ccd979bdSMark Fasheh }
1773ccd979bdSMark Fasheh 
1774ccd979bdSMark Fasheh /*
1775ccd979bdSMark Fasheh  * expects the suballoc inode to already be locked.
1776ccd979bdSMark Fasheh  */
17772b604351SMark Fasheh int ocfs2_free_suballoc_bits(handle_t *handle,
1778ccd979bdSMark Fasheh 			     struct inode *alloc_inode,
1779ccd979bdSMark Fasheh 			     struct buffer_head *alloc_bh,
1780ccd979bdSMark Fasheh 			     unsigned int start_bit,
1781ccd979bdSMark Fasheh 			     u64 bg_blkno,
1782ccd979bdSMark Fasheh 			     unsigned int count)
1783ccd979bdSMark Fasheh {
1784ccd979bdSMark Fasheh 	int status = 0;
1785ccd979bdSMark Fasheh 	u32 tmp_used;
1786ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
1787ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1788ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1789ccd979bdSMark Fasheh 	struct buffer_head *group_bh = NULL;
1790ccd979bdSMark Fasheh 	struct ocfs2_group_desc *group;
1791ccd979bdSMark Fasheh 
1792ccd979bdSMark Fasheh 	mlog_entry_void();
1793ccd979bdSMark Fasheh 
1794ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
1795ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
1796ccd979bdSMark Fasheh 		status = -EIO;
1797ccd979bdSMark Fasheh 		goto bail;
1798ccd979bdSMark Fasheh 	}
1799ccd979bdSMark Fasheh 	BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1800ccd979bdSMark Fasheh 
1801b0697053SMark Fasheh 	mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1802b0697053SMark Fasheh 	     (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1803b0697053SMark Fasheh 	     (unsigned long long)bg_blkno, start_bit);
1804ccd979bdSMark Fasheh 
1805ccd979bdSMark Fasheh 	status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED,
1806ccd979bdSMark Fasheh 				  alloc_inode);
1807ccd979bdSMark Fasheh 	if (status < 0) {
1808ccd979bdSMark Fasheh 		mlog_errno(status);
1809ccd979bdSMark Fasheh 		goto bail;
1810ccd979bdSMark Fasheh 	}
1811ccd979bdSMark Fasheh 
1812ccd979bdSMark Fasheh 	group = (struct ocfs2_group_desc *) group_bh->b_data;
18137bf72edeSMark Fasheh 	status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, group);
18147bf72edeSMark Fasheh 	if (status) {
18157bf72edeSMark Fasheh 		mlog_errno(status);
1816ccd979bdSMark Fasheh 		goto bail;
1817ccd979bdSMark Fasheh 	}
1818ccd979bdSMark Fasheh 	BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1819ccd979bdSMark Fasheh 
1820ccd979bdSMark Fasheh 	status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1821ccd979bdSMark Fasheh 					      group, group_bh,
1822ccd979bdSMark Fasheh 					      start_bit, count);
1823ccd979bdSMark Fasheh 	if (status < 0) {
1824ccd979bdSMark Fasheh 		mlog_errno(status);
1825ccd979bdSMark Fasheh 		goto bail;
1826ccd979bdSMark Fasheh 	}
1827ccd979bdSMark Fasheh 
1828ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
1829ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
1830ccd979bdSMark Fasheh 	if (status < 0) {
1831ccd979bdSMark Fasheh 		mlog_errno(status);
1832ccd979bdSMark Fasheh 		goto bail;
1833ccd979bdSMark Fasheh 	}
1834ccd979bdSMark Fasheh 
1835ccd979bdSMark Fasheh 	le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
1836ccd979bdSMark Fasheh 		     count);
1837ccd979bdSMark Fasheh 	tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1838ccd979bdSMark Fasheh 	fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
1839ccd979bdSMark Fasheh 
1840ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, alloc_bh);
1841ccd979bdSMark Fasheh 	if (status < 0) {
1842ccd979bdSMark Fasheh 		mlog_errno(status);
1843ccd979bdSMark Fasheh 		goto bail;
1844ccd979bdSMark Fasheh 	}
1845ccd979bdSMark Fasheh 
1846ccd979bdSMark Fasheh bail:
1847ccd979bdSMark Fasheh 	if (group_bh)
1848ccd979bdSMark Fasheh 		brelse(group_bh);
1849ccd979bdSMark Fasheh 
1850ccd979bdSMark Fasheh 	mlog_exit(status);
1851ccd979bdSMark Fasheh 	return status;
1852ccd979bdSMark Fasheh }
1853ccd979bdSMark Fasheh 
18541fabe148SMark Fasheh int ocfs2_free_dinode(handle_t *handle,
1855ccd979bdSMark Fasheh 		      struct inode *inode_alloc_inode,
1856ccd979bdSMark Fasheh 		      struct buffer_head *inode_alloc_bh,
1857ccd979bdSMark Fasheh 		      struct ocfs2_dinode *di)
1858ccd979bdSMark Fasheh {
1859ccd979bdSMark Fasheh 	u64 blk = le64_to_cpu(di->i_blkno);
1860ccd979bdSMark Fasheh 	u16 bit = le16_to_cpu(di->i_suballoc_bit);
1861ccd979bdSMark Fasheh 	u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1862ccd979bdSMark Fasheh 
1863ccd979bdSMark Fasheh 	return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
1864ccd979bdSMark Fasheh 					inode_alloc_bh, bit, bg_blkno, 1);
1865ccd979bdSMark Fasheh }
1866ccd979bdSMark Fasheh 
18671fabe148SMark Fasheh int ocfs2_free_clusters(handle_t *handle,
1868ccd979bdSMark Fasheh 		       struct inode *bitmap_inode,
1869ccd979bdSMark Fasheh 		       struct buffer_head *bitmap_bh,
1870ccd979bdSMark Fasheh 		       u64 start_blk,
1871ccd979bdSMark Fasheh 		       unsigned int num_clusters)
1872ccd979bdSMark Fasheh {
1873ccd979bdSMark Fasheh 	int status;
1874ccd979bdSMark Fasheh 	u16 bg_start_bit;
1875ccd979bdSMark Fasheh 	u64 bg_blkno;
1876ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
1877ccd979bdSMark Fasheh 
1878ccd979bdSMark Fasheh 	/* You can't ever have a contiguous set of clusters
1879ccd979bdSMark Fasheh 	 * bigger than a block group bitmap so we never have to worry
1880ccd979bdSMark Fasheh 	 * about looping on them. */
1881ccd979bdSMark Fasheh 
1882ccd979bdSMark Fasheh 	mlog_entry_void();
1883ccd979bdSMark Fasheh 
1884ccd979bdSMark Fasheh 	/* This is expensive. We can safely remove once this stuff has
1885ccd979bdSMark Fasheh 	 * gotten tested really well. */
1886ccd979bdSMark Fasheh 	BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
1887ccd979bdSMark Fasheh 
1888ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
1889ccd979bdSMark Fasheh 
1890ccd979bdSMark Fasheh 	ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
1891ccd979bdSMark Fasheh 				     &bg_start_bit);
1892ccd979bdSMark Fasheh 
1893b0697053SMark Fasheh 	mlog(0, "want to free %u clusters starting at block %llu\n",
1894b0697053SMark Fasheh 	     num_clusters, (unsigned long long)start_blk);
1895b0697053SMark Fasheh 	mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1896b0697053SMark Fasheh 	     (unsigned long long)bg_blkno, bg_start_bit);
1897ccd979bdSMark Fasheh 
1898ccd979bdSMark Fasheh 	status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
1899ccd979bdSMark Fasheh 					  bg_start_bit, bg_blkno,
1900ccd979bdSMark Fasheh 					  num_clusters);
19019c7af40bSMark Fasheh 	if (status < 0) {
1902ccd979bdSMark Fasheh 		mlog_errno(status);
19039c7af40bSMark Fasheh 		goto out;
19049c7af40bSMark Fasheh 	}
1905ccd979bdSMark Fasheh 
19069c7af40bSMark Fasheh 	ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
19079c7af40bSMark Fasheh 					 num_clusters);
19089c7af40bSMark Fasheh 
19099c7af40bSMark Fasheh out:
1910ccd979bdSMark Fasheh 	mlog_exit(status);
1911ccd979bdSMark Fasheh 	return status;
1912ccd979bdSMark Fasheh }
1913ccd979bdSMark Fasheh 
1914ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
1915ccd979bdSMark Fasheh {
1916ccd979bdSMark Fasheh 	printk("Block Group:\n");
1917ccd979bdSMark Fasheh 	printk("bg_signature:       %s\n", bg->bg_signature);
1918ccd979bdSMark Fasheh 	printk("bg_size:            %u\n", bg->bg_size);
1919ccd979bdSMark Fasheh 	printk("bg_bits:            %u\n", bg->bg_bits);
1920ccd979bdSMark Fasheh 	printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
1921ccd979bdSMark Fasheh 	printk("bg_chain:           %u\n", bg->bg_chain);
1922ccd979bdSMark Fasheh 	printk("bg_generation:      %u\n", le32_to_cpu(bg->bg_generation));
1923b0697053SMark Fasheh 	printk("bg_next_group:      %llu\n",
1924b0697053SMark Fasheh 	       (unsigned long long)bg->bg_next_group);
1925b0697053SMark Fasheh 	printk("bg_parent_dinode:   %llu\n",
1926b0697053SMark Fasheh 	       (unsigned long long)bg->bg_parent_dinode);
1927b0697053SMark Fasheh 	printk("bg_blkno:           %llu\n",
1928b0697053SMark Fasheh 	       (unsigned long long)bg->bg_blkno);
1929ccd979bdSMark Fasheh }
1930ccd979bdSMark Fasheh 
1931ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
1932ccd979bdSMark Fasheh {
1933ccd979bdSMark Fasheh 	int i;
1934ccd979bdSMark Fasheh 
1935b0697053SMark Fasheh 	printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
1936ccd979bdSMark Fasheh 	printk("i_signature:                  %s\n", fe->i_signature);
1937b0697053SMark Fasheh 	printk("i_size:                       %llu\n",
1938b0697053SMark Fasheh 	       (unsigned long long)fe->i_size);
1939ccd979bdSMark Fasheh 	printk("i_clusters:                   %u\n", fe->i_clusters);
1940ccd979bdSMark Fasheh 	printk("i_generation:                 %u\n",
1941ccd979bdSMark Fasheh 	       le32_to_cpu(fe->i_generation));
1942ccd979bdSMark Fasheh 	printk("id1.bitmap1.i_used:           %u\n",
1943ccd979bdSMark Fasheh 	       le32_to_cpu(fe->id1.bitmap1.i_used));
1944ccd979bdSMark Fasheh 	printk("id1.bitmap1.i_total:          %u\n",
1945ccd979bdSMark Fasheh 	       le32_to_cpu(fe->id1.bitmap1.i_total));
1946ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_cpg:           %u\n", fe->id2.i_chain.cl_cpg);
1947ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_bpc:           %u\n", fe->id2.i_chain.cl_bpc);
1948ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_count:         %u\n", fe->id2.i_chain.cl_count);
1949ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_next_free_rec: %u\n",
1950ccd979bdSMark Fasheh 	       fe->id2.i_chain.cl_next_free_rec);
1951ccd979bdSMark Fasheh 	for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
1952ccd979bdSMark Fasheh 		printk("fe->id2.i_chain.cl_recs[%d].c_free:  %u\n", i,
1953ccd979bdSMark Fasheh 		       fe->id2.i_chain.cl_recs[i].c_free);
1954ccd979bdSMark Fasheh 		printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
1955ccd979bdSMark Fasheh 		       fe->id2.i_chain.cl_recs[i].c_total);
1956b0697053SMark Fasheh 		printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
1957b0697053SMark Fasheh 		       (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
1958ccd979bdSMark Fasheh 	}
1959ccd979bdSMark Fasheh }
1960e7d4cb6bSTao Ma 
1961e7d4cb6bSTao Ma /*
1962e7d4cb6bSTao Ma  * For a given allocation, determine which allocators will need to be
1963e7d4cb6bSTao Ma  * accessed, and lock them, reserving the appropriate number of bits.
1964e7d4cb6bSTao Ma  *
1965e7d4cb6bSTao Ma  * Sparse file systems call this from ocfs2_write_begin_nolock()
1966e7d4cb6bSTao Ma  * and ocfs2_allocate_unwritten_extents().
1967e7d4cb6bSTao Ma  *
1968e7d4cb6bSTao Ma  * File systems which don't support holes call this from
1969e7d4cb6bSTao Ma  * ocfs2_extend_allocation().
1970e7d4cb6bSTao Ma  */
1971f99b9b7cSJoel Becker int ocfs2_lock_allocators(struct inode *inode,
1972f99b9b7cSJoel Becker 			  struct ocfs2_extent_tree *et,
1973e7d4cb6bSTao Ma 			  u32 clusters_to_add, u32 extents_to_split,
1974e7d4cb6bSTao Ma 			  struct ocfs2_alloc_context **data_ac,
1975f99b9b7cSJoel Becker 			  struct ocfs2_alloc_context **meta_ac)
1976e7d4cb6bSTao Ma {
1977e7d4cb6bSTao Ma 	int ret = 0, num_free_extents;
1978e7d4cb6bSTao Ma 	unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
1979e7d4cb6bSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1980e7d4cb6bSTao Ma 
1981e7d4cb6bSTao Ma 	*meta_ac = NULL;
1982e7d4cb6bSTao Ma 	if (data_ac)
1983e7d4cb6bSTao Ma 		*data_ac = NULL;
1984e7d4cb6bSTao Ma 
1985e7d4cb6bSTao Ma 	BUG_ON(clusters_to_add != 0 && data_ac == NULL);
1986e7d4cb6bSTao Ma 
1987f99b9b7cSJoel Becker 	num_free_extents = ocfs2_num_free_extents(osb, inode, et);
1988e7d4cb6bSTao Ma 	if (num_free_extents < 0) {
1989e7d4cb6bSTao Ma 		ret = num_free_extents;
1990e7d4cb6bSTao Ma 		mlog_errno(ret);
1991e7d4cb6bSTao Ma 		goto out;
1992e7d4cb6bSTao Ma 	}
1993e7d4cb6bSTao Ma 
1994e7d4cb6bSTao Ma 	/*
1995e7d4cb6bSTao Ma 	 * Sparse allocation file systems need to be more conservative
1996e7d4cb6bSTao Ma 	 * with reserving room for expansion - the actual allocation
1997e7d4cb6bSTao Ma 	 * happens while we've got a journal handle open so re-taking
1998e7d4cb6bSTao Ma 	 * a cluster lock (because we ran out of room for another
1999e7d4cb6bSTao Ma 	 * extent) will violate ordering rules.
2000e7d4cb6bSTao Ma 	 *
2001e7d4cb6bSTao Ma 	 * Most of the time we'll only be seeing this 1 cluster at a time
2002e7d4cb6bSTao Ma 	 * anyway.
2003e7d4cb6bSTao Ma 	 *
2004e7d4cb6bSTao Ma 	 * Always lock for any unwritten extents - we might want to
2005e7d4cb6bSTao Ma 	 * add blocks during a split.
2006e7d4cb6bSTao Ma 	 */
2007e7d4cb6bSTao Ma 	if (!num_free_extents ||
2008e7d4cb6bSTao Ma 	    (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
2009f99b9b7cSJoel Becker 		ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
2010e7d4cb6bSTao Ma 		if (ret < 0) {
2011e7d4cb6bSTao Ma 			if (ret != -ENOSPC)
2012e7d4cb6bSTao Ma 				mlog_errno(ret);
2013e7d4cb6bSTao Ma 			goto out;
2014e7d4cb6bSTao Ma 		}
2015e7d4cb6bSTao Ma 	}
2016e7d4cb6bSTao Ma 
2017e7d4cb6bSTao Ma 	if (clusters_to_add == 0)
2018e7d4cb6bSTao Ma 		goto out;
2019e7d4cb6bSTao Ma 
2020e7d4cb6bSTao Ma 	ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2021e7d4cb6bSTao Ma 	if (ret < 0) {
2022e7d4cb6bSTao Ma 		if (ret != -ENOSPC)
2023e7d4cb6bSTao Ma 			mlog_errno(ret);
2024e7d4cb6bSTao Ma 		goto out;
2025e7d4cb6bSTao Ma 	}
2026e7d4cb6bSTao Ma 
2027e7d4cb6bSTao Ma out:
2028e7d4cb6bSTao Ma 	if (ret) {
2029e7d4cb6bSTao Ma 		if (*meta_ac) {
2030e7d4cb6bSTao Ma 			ocfs2_free_alloc_context(*meta_ac);
2031e7d4cb6bSTao Ma 			*meta_ac = NULL;
2032e7d4cb6bSTao Ma 		}
2033e7d4cb6bSTao Ma 
2034e7d4cb6bSTao Ma 		/*
2035e7d4cb6bSTao Ma 		 * We cannot have an error and a non null *data_ac.
2036e7d4cb6bSTao Ma 		 */
2037e7d4cb6bSTao Ma 	}
2038e7d4cb6bSTao Ma 
2039e7d4cb6bSTao Ma 	return ret;
2040e7d4cb6bSTao Ma }
2041