xref: /openbmc/linux/fs/ocfs2/suballoc.c (revision ffda89a3bf3b968bdc268584c6bc1da5c173cf12)
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 
49*ffda89a3STao Ma #define NOT_ALLOC_NEW_GROUP		0
50*ffda89a3STao Ma #define ALLOC_NEW_GROUP			1
51*ffda89a3STao Ma 
52ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
53ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
54ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
551fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle,
56ccd979bdSMark Fasheh 				  struct inode *alloc_inode,
57ccd979bdSMark Fasheh 				  struct buffer_head *bg_bh,
58ccd979bdSMark Fasheh 				  u64 group_blkno,
59ccd979bdSMark Fasheh 				  u16 my_chain,
60ccd979bdSMark Fasheh 				  struct ocfs2_chain_list *cl);
61ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
62ccd979bdSMark Fasheh 				   struct inode *alloc_inode,
63ccd979bdSMark Fasheh 				   struct buffer_head *bh);
64ccd979bdSMark Fasheh 
65ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode,
66ccd979bdSMark Fasheh 				      struct buffer_head *group_bh,
67ccd979bdSMark Fasheh 				      u32 bits_wanted, u32 min_bits,
68ccd979bdSMark Fasheh 				      u16 *bit_off, u16 *bits_found);
69ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode,
70ccd979bdSMark Fasheh 				    struct buffer_head *group_bh,
71ccd979bdSMark Fasheh 				    u32 bits_wanted, u32 min_bits,
72ccd979bdSMark Fasheh 				    u16 *bit_off, u16 *bits_found);
73ccd979bdSMark Fasheh static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
74ccd979bdSMark Fasheh 				     struct ocfs2_alloc_context *ac,
751fabe148SMark Fasheh 				     handle_t *handle,
76ccd979bdSMark Fasheh 				     u32 bits_wanted,
77ccd979bdSMark Fasheh 				     u32 min_bits,
78ccd979bdSMark Fasheh 				     u16 *bit_off,
79ccd979bdSMark Fasheh 				     unsigned int *num_bits,
80ccd979bdSMark Fasheh 				     u64 *bg_blkno);
81ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
82ccd979bdSMark Fasheh 					 int nr);
831fabe148SMark Fasheh static inline int ocfs2_block_group_set_bits(handle_t *handle,
84ccd979bdSMark Fasheh 					     struct inode *alloc_inode,
85ccd979bdSMark Fasheh 					     struct ocfs2_group_desc *bg,
86ccd979bdSMark Fasheh 					     struct buffer_head *group_bh,
87ccd979bdSMark Fasheh 					     unsigned int bit_off,
88ccd979bdSMark Fasheh 					     unsigned int num_bits);
891fabe148SMark Fasheh static inline int ocfs2_block_group_clear_bits(handle_t *handle,
90ccd979bdSMark Fasheh 					       struct inode *alloc_inode,
91ccd979bdSMark Fasheh 					       struct ocfs2_group_desc *bg,
92ccd979bdSMark Fasheh 					       struct buffer_head *group_bh,
93ccd979bdSMark Fasheh 					       unsigned int bit_off,
94ccd979bdSMark Fasheh 					       unsigned int num_bits);
95ccd979bdSMark Fasheh 
961fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle,
97ccd979bdSMark Fasheh 				    struct inode *alloc_inode,
98ccd979bdSMark Fasheh 				    struct buffer_head *fe_bh,
99ccd979bdSMark Fasheh 				    struct buffer_head *bg_bh,
100ccd979bdSMark Fasheh 				    struct buffer_head *prev_bg_bh,
101ccd979bdSMark Fasheh 				    u16 chain);
102ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
103ccd979bdSMark Fasheh 						     u32 wanted);
104ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
105ccd979bdSMark Fasheh 						   u64 bg_blkno,
106ccd979bdSMark Fasheh 						   u16 bg_bit_off);
107ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode,
108ccd979bdSMark Fasheh 						u64 data_blkno,
109ccd979bdSMark Fasheh 						u64 *bg_blkno,
110ccd979bdSMark Fasheh 						u16 *bg_bit_off);
111ccd979bdSMark Fasheh 
112ccd979bdSMark Fasheh void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
113ccd979bdSMark Fasheh {
114da5cbf2fSMark Fasheh 	struct inode *inode = ac->ac_inode;
115da5cbf2fSMark Fasheh 
116da5cbf2fSMark Fasheh 	if (inode) {
117da5cbf2fSMark Fasheh 		if (ac->ac_which != OCFS2_AC_USE_LOCAL)
118e63aecb6SMark Fasheh 			ocfs2_inode_unlock(inode, 1);
119da5cbf2fSMark Fasheh 
120da5cbf2fSMark Fasheh 		mutex_unlock(&inode->i_mutex);
121da5cbf2fSMark Fasheh 
122da5cbf2fSMark Fasheh 		iput(inode);
123da5cbf2fSMark Fasheh 	}
124ccd979bdSMark Fasheh 	if (ac->ac_bh)
125ccd979bdSMark Fasheh 		brelse(ac->ac_bh);
126ccd979bdSMark Fasheh 	kfree(ac);
127ccd979bdSMark Fasheh }
128ccd979bdSMark Fasheh 
129ccd979bdSMark Fasheh static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
130ccd979bdSMark Fasheh {
131ccd979bdSMark Fasheh 	return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
132ccd979bdSMark Fasheh }
133ccd979bdSMark Fasheh 
1347bf72edeSMark Fasheh /* somewhat more expensive than our other checks, so use sparingly. */
135d659072fSTao Ma int ocfs2_check_group_descriptor(struct super_block *sb,
1367bf72edeSMark Fasheh 				 struct ocfs2_dinode *di,
1377bf72edeSMark Fasheh 				 struct ocfs2_group_desc *gd)
1387bf72edeSMark Fasheh {
1397bf72edeSMark Fasheh 	unsigned int max_bits;
1407bf72edeSMark Fasheh 
1417bf72edeSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1427bf72edeSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(sb, gd);
1437bf72edeSMark Fasheh 		return -EIO;
1447bf72edeSMark Fasheh 	}
1457bf72edeSMark Fasheh 
1467bf72edeSMark Fasheh 	if (di->i_blkno != gd->bg_parent_dinode) {
1477bf72edeSMark Fasheh 		ocfs2_error(sb, "Group descriptor # %llu has bad parent "
1487bf72edeSMark Fasheh 			    "pointer (%llu, expected %llu)",
1497bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_blkno),
1507bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
1517bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(di->i_blkno));
1527bf72edeSMark Fasheh 		return -EIO;
1537bf72edeSMark Fasheh 	}
1547bf72edeSMark Fasheh 
1557bf72edeSMark Fasheh 	max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
1567bf72edeSMark Fasheh 	if (le16_to_cpu(gd->bg_bits) > max_bits) {
1577bf72edeSMark Fasheh 		ocfs2_error(sb, "Group descriptor # %llu has bit count of %u",
1587bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_blkno),
1597bf72edeSMark Fasheh 			    le16_to_cpu(gd->bg_bits));
1607bf72edeSMark Fasheh 		return -EIO;
1617bf72edeSMark Fasheh 	}
1627bf72edeSMark Fasheh 
1637bf72edeSMark Fasheh 	if (le16_to_cpu(gd->bg_chain) >=
1647bf72edeSMark Fasheh 	    le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
1657bf72edeSMark Fasheh 		ocfs2_error(sb, "Group descriptor # %llu has bad chain %u",
1667bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_blkno),
1677bf72edeSMark Fasheh 			    le16_to_cpu(gd->bg_chain));
1687bf72edeSMark Fasheh 		return -EIO;
1697bf72edeSMark Fasheh 	}
1707bf72edeSMark Fasheh 
1717bf72edeSMark Fasheh 	if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
1727bf72edeSMark Fasheh 		ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
1737bf72edeSMark Fasheh 			    "claims that %u are free",
1747bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_blkno),
1757bf72edeSMark Fasheh 			    le16_to_cpu(gd->bg_bits),
1767bf72edeSMark Fasheh 			    le16_to_cpu(gd->bg_free_bits_count));
1777bf72edeSMark Fasheh 		return -EIO;
1787bf72edeSMark Fasheh 	}
1797bf72edeSMark Fasheh 
1807bf72edeSMark Fasheh 	if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
1817bf72edeSMark Fasheh 		ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
1827bf72edeSMark Fasheh 			    "max bitmap bits of %u",
1837bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_blkno),
1847bf72edeSMark Fasheh 			    le16_to_cpu(gd->bg_bits),
1857bf72edeSMark Fasheh 			    8 * le16_to_cpu(gd->bg_size));
1867bf72edeSMark Fasheh 		return -EIO;
1877bf72edeSMark Fasheh 	}
1887bf72edeSMark Fasheh 
1897bf72edeSMark Fasheh 	return 0;
1907bf72edeSMark Fasheh }
1917bf72edeSMark Fasheh 
1921fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle,
193ccd979bdSMark Fasheh 				  struct inode *alloc_inode,
194ccd979bdSMark Fasheh 				  struct buffer_head *bg_bh,
195ccd979bdSMark Fasheh 				  u64 group_blkno,
196ccd979bdSMark Fasheh 				  u16 my_chain,
197ccd979bdSMark Fasheh 				  struct ocfs2_chain_list *cl)
198ccd979bdSMark Fasheh {
199ccd979bdSMark Fasheh 	int status = 0;
200ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
201ccd979bdSMark Fasheh 	struct super_block * sb = alloc_inode->i_sb;
202ccd979bdSMark Fasheh 
203ccd979bdSMark Fasheh 	mlog_entry_void();
204ccd979bdSMark Fasheh 
205ccd979bdSMark Fasheh 	if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
206b0697053SMark Fasheh 		ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
207b0697053SMark Fasheh 			    "b_blocknr (%llu)",
208b0697053SMark Fasheh 			    (unsigned long long)group_blkno,
209ccd979bdSMark Fasheh 			    (unsigned long long) bg_bh->b_blocknr);
210ccd979bdSMark Fasheh 		status = -EIO;
211ccd979bdSMark Fasheh 		goto bail;
212ccd979bdSMark Fasheh 	}
213ccd979bdSMark Fasheh 
214ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle,
215ccd979bdSMark Fasheh 				      alloc_inode,
216ccd979bdSMark Fasheh 				      bg_bh,
217ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_CREATE);
218ccd979bdSMark Fasheh 	if (status < 0) {
219ccd979bdSMark Fasheh 		mlog_errno(status);
220ccd979bdSMark Fasheh 		goto bail;
221ccd979bdSMark Fasheh 	}
222ccd979bdSMark Fasheh 
223ccd979bdSMark Fasheh 	memset(bg, 0, sb->s_blocksize);
224ccd979bdSMark Fasheh 	strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
225ccd979bdSMark Fasheh 	bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
226ccd979bdSMark Fasheh 	bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
227ccd979bdSMark Fasheh 	bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
228ccd979bdSMark Fasheh 	bg->bg_chain = cpu_to_le16(my_chain);
229ccd979bdSMark Fasheh 	bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
230ccd979bdSMark Fasheh 	bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
231ccd979bdSMark Fasheh 	bg->bg_blkno = cpu_to_le64(group_blkno);
232ccd979bdSMark Fasheh 	/* set the 1st bit in the bitmap to account for the descriptor block */
233ccd979bdSMark Fasheh 	ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
234ccd979bdSMark Fasheh 	bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
235ccd979bdSMark Fasheh 
236ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, bg_bh);
237ccd979bdSMark Fasheh 	if (status < 0)
238ccd979bdSMark Fasheh 		mlog_errno(status);
239ccd979bdSMark Fasheh 
240ccd979bdSMark Fasheh 	/* There is no need to zero out or otherwise initialize the
241ccd979bdSMark Fasheh 	 * other blocks in a group - All valid FS metadata in a block
242ccd979bdSMark Fasheh 	 * group stores the superblock fs_generation value at
243ccd979bdSMark Fasheh 	 * allocation time. */
244ccd979bdSMark Fasheh 
245ccd979bdSMark Fasheh bail:
246ccd979bdSMark Fasheh 	mlog_exit(status);
247ccd979bdSMark Fasheh 	return status;
248ccd979bdSMark Fasheh }
249ccd979bdSMark Fasheh 
250ccd979bdSMark Fasheh static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
251ccd979bdSMark Fasheh {
252ccd979bdSMark Fasheh 	u16 curr, best;
253ccd979bdSMark Fasheh 
254ccd979bdSMark Fasheh 	best = curr = 0;
255ccd979bdSMark Fasheh 	while (curr < le16_to_cpu(cl->cl_count)) {
256ccd979bdSMark Fasheh 		if (le32_to_cpu(cl->cl_recs[best].c_total) >
257ccd979bdSMark Fasheh 		    le32_to_cpu(cl->cl_recs[curr].c_total))
258ccd979bdSMark Fasheh 			best = curr;
259ccd979bdSMark Fasheh 		curr++;
260ccd979bdSMark Fasheh 	}
261ccd979bdSMark Fasheh 	return best;
262ccd979bdSMark Fasheh }
263ccd979bdSMark Fasheh 
264ccd979bdSMark Fasheh /*
265ccd979bdSMark Fasheh  * We expect the block group allocator to already be locked.
266ccd979bdSMark Fasheh  */
267ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
268ccd979bdSMark Fasheh 				   struct inode *alloc_inode,
269ccd979bdSMark Fasheh 				   struct buffer_head *bh)
270ccd979bdSMark Fasheh {
271ccd979bdSMark Fasheh 	int status, credits;
272ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
273ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl;
274ccd979bdSMark Fasheh 	struct ocfs2_alloc_context *ac = NULL;
2751fabe148SMark Fasheh 	handle_t *handle = NULL;
276ccd979bdSMark Fasheh 	u32 bit_off, num_bits;
277ccd979bdSMark Fasheh 	u16 alloc_rec;
278ccd979bdSMark Fasheh 	u64 bg_blkno;
279ccd979bdSMark Fasheh 	struct buffer_head *bg_bh = NULL;
280ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg;
281ccd979bdSMark Fasheh 
282ccd979bdSMark Fasheh 	BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
283ccd979bdSMark Fasheh 
284ccd979bdSMark Fasheh 	mlog_entry_void();
285ccd979bdSMark Fasheh 
286ccd979bdSMark Fasheh 	cl = &fe->id2.i_chain;
287ccd979bdSMark Fasheh 	status = ocfs2_reserve_clusters(osb,
288ccd979bdSMark Fasheh 					le16_to_cpu(cl->cl_cpg),
289ccd979bdSMark Fasheh 					&ac);
290ccd979bdSMark Fasheh 	if (status < 0) {
291ccd979bdSMark Fasheh 		if (status != -ENOSPC)
292ccd979bdSMark Fasheh 			mlog_errno(status);
293ccd979bdSMark Fasheh 		goto bail;
294ccd979bdSMark Fasheh 	}
295ccd979bdSMark Fasheh 
296ccd979bdSMark Fasheh 	credits = ocfs2_calc_group_alloc_credits(osb->sb,
297ccd979bdSMark Fasheh 						 le16_to_cpu(cl->cl_cpg));
29865eff9ccSMark Fasheh 	handle = ocfs2_start_trans(osb, credits);
299ccd979bdSMark Fasheh 	if (IS_ERR(handle)) {
300ccd979bdSMark Fasheh 		status = PTR_ERR(handle);
301ccd979bdSMark Fasheh 		handle = NULL;
302ccd979bdSMark Fasheh 		mlog_errno(status);
303ccd979bdSMark Fasheh 		goto bail;
304ccd979bdSMark Fasheh 	}
305ccd979bdSMark Fasheh 
306ccd979bdSMark Fasheh 	status = ocfs2_claim_clusters(osb,
307ccd979bdSMark Fasheh 				      handle,
308ccd979bdSMark Fasheh 				      ac,
309ccd979bdSMark Fasheh 				      le16_to_cpu(cl->cl_cpg),
310ccd979bdSMark Fasheh 				      &bit_off,
311ccd979bdSMark Fasheh 				      &num_bits);
312ccd979bdSMark Fasheh 	if (status < 0) {
313ccd979bdSMark Fasheh 		if (status != -ENOSPC)
314ccd979bdSMark Fasheh 			mlog_errno(status);
315ccd979bdSMark Fasheh 		goto bail;
316ccd979bdSMark Fasheh 	}
317ccd979bdSMark Fasheh 
318ccd979bdSMark Fasheh 	alloc_rec = ocfs2_find_smallest_chain(cl);
319ccd979bdSMark Fasheh 
320ccd979bdSMark Fasheh 	/* setup the group */
321ccd979bdSMark Fasheh 	bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
322b0697053SMark Fasheh 	mlog(0, "new descriptor, record %u, at block %llu\n",
323b0697053SMark Fasheh 	     alloc_rec, (unsigned long long)bg_blkno);
324ccd979bdSMark Fasheh 
325ccd979bdSMark Fasheh 	bg_bh = sb_getblk(osb->sb, bg_blkno);
326ccd979bdSMark Fasheh 	if (!bg_bh) {
327ccd979bdSMark Fasheh 		status = -EIO;
328ccd979bdSMark Fasheh 		mlog_errno(status);
329ccd979bdSMark Fasheh 		goto bail;
330ccd979bdSMark Fasheh 	}
331ccd979bdSMark Fasheh 	ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
332ccd979bdSMark Fasheh 
333ccd979bdSMark Fasheh 	status = ocfs2_block_group_fill(handle,
334ccd979bdSMark Fasheh 					alloc_inode,
335ccd979bdSMark Fasheh 					bg_bh,
336ccd979bdSMark Fasheh 					bg_blkno,
337ccd979bdSMark Fasheh 					alloc_rec,
338ccd979bdSMark Fasheh 					cl);
339ccd979bdSMark Fasheh 	if (status < 0) {
340ccd979bdSMark Fasheh 		mlog_errno(status);
341ccd979bdSMark Fasheh 		goto bail;
342ccd979bdSMark Fasheh 	}
343ccd979bdSMark Fasheh 
344ccd979bdSMark Fasheh 	bg = (struct ocfs2_group_desc *) bg_bh->b_data;
345ccd979bdSMark Fasheh 
346ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode,
347ccd979bdSMark Fasheh 				      bh, OCFS2_JOURNAL_ACCESS_WRITE);
348ccd979bdSMark Fasheh 	if (status < 0) {
349ccd979bdSMark Fasheh 		mlog_errno(status);
350ccd979bdSMark Fasheh 		goto bail;
351ccd979bdSMark Fasheh 	}
352ccd979bdSMark Fasheh 
353ccd979bdSMark Fasheh 	le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
354ccd979bdSMark Fasheh 		     le16_to_cpu(bg->bg_free_bits_count));
355ccd979bdSMark Fasheh 	le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
356ccd979bdSMark Fasheh 	cl->cl_recs[alloc_rec].c_blkno  = cpu_to_le64(bg_blkno);
357ccd979bdSMark Fasheh 	if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
358ccd979bdSMark Fasheh 		le16_add_cpu(&cl->cl_next_free_rec, 1);
359ccd979bdSMark Fasheh 
360ccd979bdSMark Fasheh 	le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
361ccd979bdSMark Fasheh 					le16_to_cpu(bg->bg_free_bits_count));
362ccd979bdSMark Fasheh 	le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
363ccd979bdSMark Fasheh 	le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
364ccd979bdSMark Fasheh 
365ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, bh);
366ccd979bdSMark Fasheh 	if (status < 0) {
367ccd979bdSMark Fasheh 		mlog_errno(status);
368ccd979bdSMark Fasheh 		goto bail;
369ccd979bdSMark Fasheh 	}
370ccd979bdSMark Fasheh 
371ccd979bdSMark Fasheh 	spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
372ccd979bdSMark Fasheh 	OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
373ccd979bdSMark Fasheh 	fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
374ccd979bdSMark Fasheh 					     le32_to_cpu(fe->i_clusters)));
375ccd979bdSMark Fasheh 	spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
376ccd979bdSMark Fasheh 	i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
3778110b073SMark Fasheh 	alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
378ccd979bdSMark Fasheh 
379ccd979bdSMark Fasheh 	status = 0;
380ccd979bdSMark Fasheh bail:
381ccd979bdSMark Fasheh 	if (handle)
38202dc1af4SMark Fasheh 		ocfs2_commit_trans(osb, handle);
383ccd979bdSMark Fasheh 
384ccd979bdSMark Fasheh 	if (ac)
385ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(ac);
386ccd979bdSMark Fasheh 
387ccd979bdSMark Fasheh 	if (bg_bh)
388ccd979bdSMark Fasheh 		brelse(bg_bh);
389ccd979bdSMark Fasheh 
390ccd979bdSMark Fasheh 	mlog_exit(status);
391ccd979bdSMark Fasheh 	return status;
392ccd979bdSMark Fasheh }
393ccd979bdSMark Fasheh 
394ccd979bdSMark Fasheh static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
395da5cbf2fSMark Fasheh 				       struct ocfs2_alloc_context *ac,
396da5cbf2fSMark Fasheh 				       int type,
397*ffda89a3STao Ma 				       u32 slot,
398*ffda89a3STao Ma 				       int alloc_new_group)
399ccd979bdSMark Fasheh {
400ccd979bdSMark Fasheh 	int status;
401ccd979bdSMark Fasheh 	u32 bits_wanted = ac->ac_bits_wanted;
402da5cbf2fSMark Fasheh 	struct inode *alloc_inode;
403ccd979bdSMark Fasheh 	struct buffer_head *bh = NULL;
404ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
405ccd979bdSMark Fasheh 	u32 free_bits;
406ccd979bdSMark Fasheh 
407ccd979bdSMark Fasheh 	mlog_entry_void();
408ccd979bdSMark Fasheh 
409da5cbf2fSMark Fasheh 	alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
410da5cbf2fSMark Fasheh 	if (!alloc_inode) {
411da5cbf2fSMark Fasheh 		mlog_errno(-EINVAL);
412da5cbf2fSMark Fasheh 		return -EINVAL;
413ccd979bdSMark Fasheh 	}
414ccd979bdSMark Fasheh 
415da5cbf2fSMark Fasheh 	mutex_lock(&alloc_inode->i_mutex);
416da5cbf2fSMark Fasheh 
417e63aecb6SMark Fasheh 	status = ocfs2_inode_lock(alloc_inode, &bh, 1);
418da5cbf2fSMark Fasheh 	if (status < 0) {
419da5cbf2fSMark Fasheh 		mutex_unlock(&alloc_inode->i_mutex);
420da5cbf2fSMark Fasheh 		iput(alloc_inode);
421da5cbf2fSMark Fasheh 
422da5cbf2fSMark Fasheh 		mlog_errno(status);
423da5cbf2fSMark Fasheh 		return status;
424da5cbf2fSMark Fasheh 	}
425da5cbf2fSMark Fasheh 
426da5cbf2fSMark Fasheh 	ac->ac_inode = alloc_inode;
427da5cbf2fSMark Fasheh 
428ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) bh->b_data;
429ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
430ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
431ccd979bdSMark Fasheh 		status = -EIO;
432ccd979bdSMark Fasheh 		goto bail;
433ccd979bdSMark Fasheh 	}
434ccd979bdSMark Fasheh 	if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
435b0697053SMark Fasheh 		ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
436b0697053SMark Fasheh 			    (unsigned long long)le64_to_cpu(fe->i_blkno));
437ccd979bdSMark Fasheh 		status = -EIO;
438ccd979bdSMark Fasheh 		goto bail;
439ccd979bdSMark Fasheh 	}
440ccd979bdSMark Fasheh 
441ccd979bdSMark Fasheh 	free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
442ccd979bdSMark Fasheh 		le32_to_cpu(fe->id1.bitmap1.i_used);
443ccd979bdSMark Fasheh 
444ccd979bdSMark Fasheh 	if (bits_wanted > free_bits) {
445ccd979bdSMark Fasheh 		/* cluster bitmap never grows */
446ccd979bdSMark Fasheh 		if (ocfs2_is_cluster_bitmap(alloc_inode)) {
447ccd979bdSMark Fasheh 			mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
448ccd979bdSMark Fasheh 			     bits_wanted, free_bits);
449ccd979bdSMark Fasheh 			status = -ENOSPC;
450ccd979bdSMark Fasheh 			goto bail;
451ccd979bdSMark Fasheh 		}
452ccd979bdSMark Fasheh 
453*ffda89a3STao Ma 		if (alloc_new_group != ALLOC_NEW_GROUP) {
454*ffda89a3STao Ma 			mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
455*ffda89a3STao Ma 			     "and we don't alloc a new group for it.\n",
456*ffda89a3STao Ma 			     slot, bits_wanted, free_bits);
457*ffda89a3STao Ma 			status = -ENOSPC;
458*ffda89a3STao Ma 			goto bail;
459*ffda89a3STao Ma 		}
460*ffda89a3STao Ma 
461ccd979bdSMark Fasheh 		status = ocfs2_block_group_alloc(osb, alloc_inode, bh);
462ccd979bdSMark Fasheh 		if (status < 0) {
463ccd979bdSMark Fasheh 			if (status != -ENOSPC)
464ccd979bdSMark Fasheh 				mlog_errno(status);
465ccd979bdSMark Fasheh 			goto bail;
466ccd979bdSMark Fasheh 		}
467ccd979bdSMark Fasheh 		atomic_inc(&osb->alloc_stats.bg_extends);
468ccd979bdSMark Fasheh 
469ccd979bdSMark Fasheh 		/* You should never ask for this much metadata */
470ccd979bdSMark Fasheh 		BUG_ON(bits_wanted >
471ccd979bdSMark Fasheh 		       (le32_to_cpu(fe->id1.bitmap1.i_total)
472ccd979bdSMark Fasheh 			- le32_to_cpu(fe->id1.bitmap1.i_used)));
473ccd979bdSMark Fasheh 	}
474ccd979bdSMark Fasheh 
475ccd979bdSMark Fasheh 	get_bh(bh);
476ccd979bdSMark Fasheh 	ac->ac_bh = bh;
477ccd979bdSMark Fasheh bail:
478ccd979bdSMark Fasheh 	if (bh)
479ccd979bdSMark Fasheh 		brelse(bh);
480ccd979bdSMark Fasheh 
481ccd979bdSMark Fasheh 	mlog_exit(status);
482ccd979bdSMark Fasheh 	return status;
483ccd979bdSMark Fasheh }
484ccd979bdSMark Fasheh 
485ccd979bdSMark Fasheh int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
486ccd979bdSMark Fasheh 			       struct ocfs2_dinode *fe,
487ccd979bdSMark Fasheh 			       struct ocfs2_alloc_context **ac)
488ccd979bdSMark Fasheh {
489ccd979bdSMark Fasheh 	int status;
490da5cbf2fSMark Fasheh 	u32 slot;
491ccd979bdSMark Fasheh 
492cd861280SRobert P. J. Day 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
493ccd979bdSMark Fasheh 	if (!(*ac)) {
494ccd979bdSMark Fasheh 		status = -ENOMEM;
495ccd979bdSMark Fasheh 		mlog_errno(status);
496ccd979bdSMark Fasheh 		goto bail;
497ccd979bdSMark Fasheh 	}
498ccd979bdSMark Fasheh 
499ccd979bdSMark Fasheh 	(*ac)->ac_bits_wanted = ocfs2_extend_meta_needed(fe);
500ccd979bdSMark Fasheh 	(*ac)->ac_which = OCFS2_AC_USE_META;
501da5cbf2fSMark Fasheh 	slot = osb->slot_num;
502ccd979bdSMark Fasheh 	(*ac)->ac_group_search = ocfs2_block_group_search;
503ccd979bdSMark Fasheh 
504da5cbf2fSMark Fasheh 	status = ocfs2_reserve_suballoc_bits(osb, (*ac),
505*ffda89a3STao Ma 					     EXTENT_ALLOC_SYSTEM_INODE,
506*ffda89a3STao Ma 					     slot, ALLOC_NEW_GROUP);
507ccd979bdSMark Fasheh 	if (status < 0) {
508ccd979bdSMark Fasheh 		if (status != -ENOSPC)
509ccd979bdSMark Fasheh 			mlog_errno(status);
510ccd979bdSMark Fasheh 		goto bail;
511ccd979bdSMark Fasheh 	}
512ccd979bdSMark Fasheh 
513ccd979bdSMark Fasheh 	status = 0;
514ccd979bdSMark Fasheh bail:
515ccd979bdSMark Fasheh 	if ((status < 0) && *ac) {
516ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(*ac);
517ccd979bdSMark Fasheh 		*ac = NULL;
518ccd979bdSMark Fasheh 	}
519ccd979bdSMark Fasheh 
520ccd979bdSMark Fasheh 	mlog_exit(status);
521ccd979bdSMark Fasheh 	return status;
522ccd979bdSMark Fasheh }
523ccd979bdSMark Fasheh 
524ccd979bdSMark Fasheh int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
525ccd979bdSMark Fasheh 			    struct ocfs2_alloc_context **ac)
526ccd979bdSMark Fasheh {
527ccd979bdSMark Fasheh 	int status;
528ccd979bdSMark Fasheh 
529cd861280SRobert P. J. Day 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
530ccd979bdSMark Fasheh 	if (!(*ac)) {
531ccd979bdSMark Fasheh 		status = -ENOMEM;
532ccd979bdSMark Fasheh 		mlog_errno(status);
533ccd979bdSMark Fasheh 		goto bail;
534ccd979bdSMark Fasheh 	}
535ccd979bdSMark Fasheh 
536ccd979bdSMark Fasheh 	(*ac)->ac_bits_wanted = 1;
537ccd979bdSMark Fasheh 	(*ac)->ac_which = OCFS2_AC_USE_INODE;
538ccd979bdSMark Fasheh 
539ccd979bdSMark Fasheh 	(*ac)->ac_group_search = ocfs2_block_group_search;
540ccd979bdSMark Fasheh 
541da5cbf2fSMark Fasheh 	status = ocfs2_reserve_suballoc_bits(osb, *ac,
542da5cbf2fSMark Fasheh 					     INODE_ALLOC_SYSTEM_INODE,
543*ffda89a3STao Ma 					     osb->slot_num, ALLOC_NEW_GROUP);
544ccd979bdSMark Fasheh 	if (status < 0) {
545ccd979bdSMark Fasheh 		if (status != -ENOSPC)
546ccd979bdSMark Fasheh 			mlog_errno(status);
547ccd979bdSMark Fasheh 		goto bail;
548ccd979bdSMark Fasheh 	}
549ccd979bdSMark Fasheh 
550ccd979bdSMark Fasheh 	status = 0;
551ccd979bdSMark Fasheh bail:
552ccd979bdSMark Fasheh 	if ((status < 0) && *ac) {
553ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(*ac);
554ccd979bdSMark Fasheh 		*ac = NULL;
555ccd979bdSMark Fasheh 	}
556ccd979bdSMark Fasheh 
557ccd979bdSMark Fasheh 	mlog_exit(status);
558ccd979bdSMark Fasheh 	return status;
559ccd979bdSMark Fasheh }
560ccd979bdSMark Fasheh 
561ccd979bdSMark Fasheh /* local alloc code has to do the same thing, so rather than do this
562ccd979bdSMark Fasheh  * twice.. */
563ccd979bdSMark Fasheh int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
564ccd979bdSMark Fasheh 				      struct ocfs2_alloc_context *ac)
565ccd979bdSMark Fasheh {
566ccd979bdSMark Fasheh 	int status;
567ccd979bdSMark Fasheh 
568ccd979bdSMark Fasheh 	ac->ac_which = OCFS2_AC_USE_MAIN;
569ccd979bdSMark Fasheh 	ac->ac_group_search = ocfs2_cluster_group_search;
570ccd979bdSMark Fasheh 
571da5cbf2fSMark Fasheh 	status = ocfs2_reserve_suballoc_bits(osb, ac,
572da5cbf2fSMark Fasheh 					     GLOBAL_BITMAP_SYSTEM_INODE,
573*ffda89a3STao Ma 					     OCFS2_INVALID_SLOT,
574*ffda89a3STao Ma 					     ALLOC_NEW_GROUP);
575da5cbf2fSMark Fasheh 	if (status < 0 && status != -ENOSPC) {
576ccd979bdSMark Fasheh 		mlog_errno(status);
577da5cbf2fSMark Fasheh 		goto bail;
578da5cbf2fSMark Fasheh 	}
579da5cbf2fSMark Fasheh 
580ccd979bdSMark Fasheh bail:
581ccd979bdSMark Fasheh 	return status;
582ccd979bdSMark Fasheh }
583ccd979bdSMark Fasheh 
584ccd979bdSMark Fasheh /* Callers don't need to care which bitmap (local alloc or main) to
585ccd979bdSMark Fasheh  * use so we figure it out for them, but unfortunately this clutters
586ccd979bdSMark Fasheh  * things a bit. */
587ccd979bdSMark Fasheh int ocfs2_reserve_clusters(struct ocfs2_super *osb,
588ccd979bdSMark Fasheh 			   u32 bits_wanted,
589ccd979bdSMark Fasheh 			   struct ocfs2_alloc_context **ac)
590ccd979bdSMark Fasheh {
591ccd979bdSMark Fasheh 	int status;
592ccd979bdSMark Fasheh 
593ccd979bdSMark Fasheh 	mlog_entry_void();
594ccd979bdSMark Fasheh 
595cd861280SRobert P. J. Day 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
596ccd979bdSMark Fasheh 	if (!(*ac)) {
597ccd979bdSMark Fasheh 		status = -ENOMEM;
598ccd979bdSMark Fasheh 		mlog_errno(status);
599ccd979bdSMark Fasheh 		goto bail;
600ccd979bdSMark Fasheh 	}
601ccd979bdSMark Fasheh 
602ccd979bdSMark Fasheh 	(*ac)->ac_bits_wanted = bits_wanted;
603ccd979bdSMark Fasheh 
604ccd979bdSMark Fasheh 	status = -ENOSPC;
605ccd979bdSMark Fasheh 	if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
606ccd979bdSMark Fasheh 		status = ocfs2_reserve_local_alloc_bits(osb,
607ccd979bdSMark Fasheh 							bits_wanted,
608ccd979bdSMark Fasheh 							*ac);
609ccd979bdSMark Fasheh 		if ((status < 0) && (status != -ENOSPC)) {
610ccd979bdSMark Fasheh 			mlog_errno(status);
611ccd979bdSMark Fasheh 			goto bail;
612ccd979bdSMark Fasheh 		} else if (status == -ENOSPC) {
613ccd979bdSMark Fasheh 			/* reserve_local_bits will return enospc with
614ccd979bdSMark Fasheh 			 * the local alloc inode still locked, so we
615ccd979bdSMark Fasheh 			 * can change this safely here. */
616ccd979bdSMark Fasheh 			mlog(0, "Disabling local alloc\n");
617ccd979bdSMark Fasheh 			/* We set to OCFS2_LA_DISABLED so that umount
618ccd979bdSMark Fasheh 			 * can clean up what's left of the local
619ccd979bdSMark Fasheh 			 * allocation */
620ccd979bdSMark Fasheh 			osb->local_alloc_state = OCFS2_LA_DISABLED;
621ccd979bdSMark Fasheh 		}
622ccd979bdSMark Fasheh 	}
623ccd979bdSMark Fasheh 
624ccd979bdSMark Fasheh 	if (status == -ENOSPC) {
625ccd979bdSMark Fasheh 		status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
626ccd979bdSMark Fasheh 		if (status < 0) {
627ccd979bdSMark Fasheh 			if (status != -ENOSPC)
628ccd979bdSMark Fasheh 				mlog_errno(status);
629ccd979bdSMark Fasheh 			goto bail;
630ccd979bdSMark Fasheh 		}
631ccd979bdSMark Fasheh 	}
632ccd979bdSMark Fasheh 
633ccd979bdSMark Fasheh 	status = 0;
634ccd979bdSMark Fasheh bail:
635ccd979bdSMark Fasheh 	if ((status < 0) && *ac) {
636ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(*ac);
637ccd979bdSMark Fasheh 		*ac = NULL;
638ccd979bdSMark Fasheh 	}
639ccd979bdSMark Fasheh 
640ccd979bdSMark Fasheh 	mlog_exit(status);
641ccd979bdSMark Fasheh 	return status;
642ccd979bdSMark Fasheh }
643ccd979bdSMark Fasheh 
644ccd979bdSMark Fasheh /*
645ccd979bdSMark Fasheh  * More or less lifted from ext3. I'll leave their description below:
646ccd979bdSMark Fasheh  *
647ccd979bdSMark Fasheh  * "For ext3 allocations, we must not reuse any blocks which are
648ccd979bdSMark Fasheh  * allocated in the bitmap buffer's "last committed data" copy.  This
649ccd979bdSMark Fasheh  * prevents deletes from freeing up the page for reuse until we have
650ccd979bdSMark Fasheh  * committed the delete transaction.
651ccd979bdSMark Fasheh  *
652ccd979bdSMark Fasheh  * If we didn't do this, then deleting something and reallocating it as
653ccd979bdSMark Fasheh  * data would allow the old block to be overwritten before the
654ccd979bdSMark Fasheh  * transaction committed (because we force data to disk before commit).
655ccd979bdSMark Fasheh  * This would lead to corruption if we crashed between overwriting the
656ccd979bdSMark Fasheh  * data and committing the delete.
657ccd979bdSMark Fasheh  *
658ccd979bdSMark Fasheh  * @@@ We may want to make this allocation behaviour conditional on
659ccd979bdSMark Fasheh  * data-writes at some point, and disable it for metadata allocations or
660ccd979bdSMark Fasheh  * sync-data inodes."
661ccd979bdSMark Fasheh  *
662ccd979bdSMark Fasheh  * Note: OCFS2 already does this differently for metadata vs data
663c78bad11SJoe Perches  * allocations, as those bitmaps are separate and undo access is never
664ccd979bdSMark Fasheh  * called on a metadata group descriptor.
665ccd979bdSMark Fasheh  */
666ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
667ccd979bdSMark Fasheh 					 int nr)
668ccd979bdSMark Fasheh {
669ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
670ccd979bdSMark Fasheh 
671ccd979bdSMark Fasheh 	if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
672ccd979bdSMark Fasheh 		return 0;
673ccd979bdSMark Fasheh 	if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
674ccd979bdSMark Fasheh 		return 1;
675ccd979bdSMark Fasheh 
676ccd979bdSMark Fasheh 	bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
677ccd979bdSMark Fasheh 	return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
678ccd979bdSMark Fasheh }
679ccd979bdSMark Fasheh 
680ccd979bdSMark Fasheh static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
681ccd979bdSMark Fasheh 					     struct buffer_head *bg_bh,
682ccd979bdSMark Fasheh 					     unsigned int bits_wanted,
6837bf72edeSMark Fasheh 					     unsigned int total_bits,
684ccd979bdSMark Fasheh 					     u16 *bit_off,
685ccd979bdSMark Fasheh 					     u16 *bits_found)
686ccd979bdSMark Fasheh {
687ccd979bdSMark Fasheh 	void *bitmap;
688ccd979bdSMark Fasheh 	u16 best_offset, best_size;
689ccd979bdSMark Fasheh 	int offset, start, found, status = 0;
690ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
691ccd979bdSMark Fasheh 
692ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
693ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg);
694ccd979bdSMark Fasheh 		return -EIO;
695ccd979bdSMark Fasheh 	}
696ccd979bdSMark Fasheh 
697ccd979bdSMark Fasheh 	found = start = best_offset = best_size = 0;
698ccd979bdSMark Fasheh 	bitmap = bg->bg_bitmap;
699ccd979bdSMark Fasheh 
7007bf72edeSMark Fasheh 	while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
7017bf72edeSMark Fasheh 		if (offset == total_bits)
702ccd979bdSMark Fasheh 			break;
703ccd979bdSMark Fasheh 
704ccd979bdSMark Fasheh 		if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
705ccd979bdSMark Fasheh 			/* We found a zero, but we can't use it as it
706ccd979bdSMark Fasheh 			 * hasn't been put to disk yet! */
707ccd979bdSMark Fasheh 			found = 0;
708ccd979bdSMark Fasheh 			start = offset + 1;
709ccd979bdSMark Fasheh 		} else if (offset == start) {
710ccd979bdSMark Fasheh 			/* we found a zero */
711ccd979bdSMark Fasheh 			found++;
712ccd979bdSMark Fasheh 			/* move start to the next bit to test */
713ccd979bdSMark Fasheh 			start++;
714ccd979bdSMark Fasheh 		} else {
715ccd979bdSMark Fasheh 			/* got a zero after some ones */
716ccd979bdSMark Fasheh 			found = 1;
717ccd979bdSMark Fasheh 			start = offset + 1;
718ccd979bdSMark Fasheh 		}
719ccd979bdSMark Fasheh 		if (found > best_size) {
720ccd979bdSMark Fasheh 			best_size = found;
721ccd979bdSMark Fasheh 			best_offset = start - found;
722ccd979bdSMark Fasheh 		}
723ccd979bdSMark Fasheh 		/* we got everything we needed */
724ccd979bdSMark Fasheh 		if (found == bits_wanted) {
725ccd979bdSMark Fasheh 			/* mlog(0, "Found it all!\n"); */
726ccd979bdSMark Fasheh 			break;
727ccd979bdSMark Fasheh 		}
728ccd979bdSMark Fasheh 	}
729ccd979bdSMark Fasheh 
730ccd979bdSMark Fasheh 	/* XXX: I think the first clause is equivalent to the second
731ccd979bdSMark Fasheh 	 * 	- jlbec */
732ccd979bdSMark Fasheh 	if (found == bits_wanted) {
733ccd979bdSMark Fasheh 		*bit_off = start - found;
734ccd979bdSMark Fasheh 		*bits_found = found;
735ccd979bdSMark Fasheh 	} else if (best_size) {
736ccd979bdSMark Fasheh 		*bit_off = best_offset;
737ccd979bdSMark Fasheh 		*bits_found = best_size;
738ccd979bdSMark Fasheh 	} else {
739ccd979bdSMark Fasheh 		status = -ENOSPC;
740ccd979bdSMark Fasheh 		/* No error log here -- see the comment above
741ccd979bdSMark Fasheh 		 * ocfs2_test_bg_bit_allocatable */
742ccd979bdSMark Fasheh 	}
743ccd979bdSMark Fasheh 
744ccd979bdSMark Fasheh 	return status;
745ccd979bdSMark Fasheh }
746ccd979bdSMark Fasheh 
7471fabe148SMark Fasheh static inline int ocfs2_block_group_set_bits(handle_t *handle,
748ccd979bdSMark Fasheh 					     struct inode *alloc_inode,
749ccd979bdSMark Fasheh 					     struct ocfs2_group_desc *bg,
750ccd979bdSMark Fasheh 					     struct buffer_head *group_bh,
751ccd979bdSMark Fasheh 					     unsigned int bit_off,
752ccd979bdSMark Fasheh 					     unsigned int num_bits)
753ccd979bdSMark Fasheh {
754ccd979bdSMark Fasheh 	int status;
755ccd979bdSMark Fasheh 	void *bitmap = bg->bg_bitmap;
756ccd979bdSMark Fasheh 	int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
757ccd979bdSMark Fasheh 
758ccd979bdSMark Fasheh 	mlog_entry_void();
759ccd979bdSMark Fasheh 
760ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
761ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
762ccd979bdSMark Fasheh 		status = -EIO;
763ccd979bdSMark Fasheh 		goto bail;
764ccd979bdSMark Fasheh 	}
765ccd979bdSMark Fasheh 	BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
766ccd979bdSMark Fasheh 
767ccd979bdSMark Fasheh 	mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
768ccd979bdSMark Fasheh 	     num_bits);
769ccd979bdSMark Fasheh 
770ccd979bdSMark Fasheh 	if (ocfs2_is_cluster_bitmap(alloc_inode))
771ccd979bdSMark Fasheh 		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
772ccd979bdSMark Fasheh 
773ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle,
774ccd979bdSMark Fasheh 				      alloc_inode,
775ccd979bdSMark Fasheh 				      group_bh,
776ccd979bdSMark Fasheh 				      journal_type);
777ccd979bdSMark Fasheh 	if (status < 0) {
778ccd979bdSMark Fasheh 		mlog_errno(status);
779ccd979bdSMark Fasheh 		goto bail;
780ccd979bdSMark Fasheh 	}
781ccd979bdSMark Fasheh 
782ccd979bdSMark Fasheh 	le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
783ccd979bdSMark Fasheh 
784ccd979bdSMark Fasheh 	while(num_bits--)
785ccd979bdSMark Fasheh 		ocfs2_set_bit(bit_off++, bitmap);
786ccd979bdSMark Fasheh 
787ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle,
788ccd979bdSMark Fasheh 				     group_bh);
789ccd979bdSMark Fasheh 	if (status < 0) {
790ccd979bdSMark Fasheh 		mlog_errno(status);
791ccd979bdSMark Fasheh 		goto bail;
792ccd979bdSMark Fasheh 	}
793ccd979bdSMark Fasheh 
794ccd979bdSMark Fasheh bail:
795ccd979bdSMark Fasheh 	mlog_exit(status);
796ccd979bdSMark Fasheh 	return status;
797ccd979bdSMark Fasheh }
798ccd979bdSMark Fasheh 
799ccd979bdSMark Fasheh /* find the one with the most empty bits */
800ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
801ccd979bdSMark Fasheh {
802ccd979bdSMark Fasheh 	u16 curr, best;
803ccd979bdSMark Fasheh 
804ccd979bdSMark Fasheh 	BUG_ON(!cl->cl_next_free_rec);
805ccd979bdSMark Fasheh 
806ccd979bdSMark Fasheh 	best = curr = 0;
807ccd979bdSMark Fasheh 	while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
808ccd979bdSMark Fasheh 		if (le32_to_cpu(cl->cl_recs[curr].c_free) >
809ccd979bdSMark Fasheh 		    le32_to_cpu(cl->cl_recs[best].c_free))
810ccd979bdSMark Fasheh 			best = curr;
811ccd979bdSMark Fasheh 		curr++;
812ccd979bdSMark Fasheh 	}
813ccd979bdSMark Fasheh 
814ccd979bdSMark Fasheh 	BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
815ccd979bdSMark Fasheh 	return best;
816ccd979bdSMark Fasheh }
817ccd979bdSMark Fasheh 
8181fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle,
819ccd979bdSMark Fasheh 				    struct inode *alloc_inode,
820ccd979bdSMark Fasheh 				    struct buffer_head *fe_bh,
821ccd979bdSMark Fasheh 				    struct buffer_head *bg_bh,
822ccd979bdSMark Fasheh 				    struct buffer_head *prev_bg_bh,
823ccd979bdSMark Fasheh 				    u16 chain)
824ccd979bdSMark Fasheh {
825ccd979bdSMark Fasheh 	int status;
826ccd979bdSMark Fasheh 	/* there is a really tiny chance the journal calls could fail,
827ccd979bdSMark Fasheh 	 * but we wouldn't want inconsistent blocks in *any* case. */
828ccd979bdSMark Fasheh 	u64 fe_ptr, bg_ptr, prev_bg_ptr;
829ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
830ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
831ccd979bdSMark Fasheh 	struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
832ccd979bdSMark Fasheh 
833ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
834ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
835ccd979bdSMark Fasheh 		status = -EIO;
836ccd979bdSMark Fasheh 		goto out;
837ccd979bdSMark Fasheh 	}
838ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
839ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
840ccd979bdSMark Fasheh 		status = -EIO;
841ccd979bdSMark Fasheh 		goto out;
842ccd979bdSMark Fasheh 	}
843ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) {
844ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg);
845ccd979bdSMark Fasheh 		status = -EIO;
846ccd979bdSMark Fasheh 		goto out;
847ccd979bdSMark Fasheh 	}
848ccd979bdSMark Fasheh 
849b0697053SMark Fasheh 	mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
8501ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
8511ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(bg->bg_blkno),
8521ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
853ccd979bdSMark Fasheh 
854ccd979bdSMark Fasheh 	fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
855ccd979bdSMark Fasheh 	bg_ptr = le64_to_cpu(bg->bg_next_group);
856ccd979bdSMark Fasheh 	prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
857ccd979bdSMark Fasheh 
858ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh,
859ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
860ccd979bdSMark Fasheh 	if (status < 0) {
861ccd979bdSMark Fasheh 		mlog_errno(status);
862ccd979bdSMark Fasheh 		goto out_rollback;
863ccd979bdSMark Fasheh 	}
864ccd979bdSMark Fasheh 
865ccd979bdSMark Fasheh 	prev_bg->bg_next_group = bg->bg_next_group;
866ccd979bdSMark Fasheh 
867ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, prev_bg_bh);
868ccd979bdSMark Fasheh 	if (status < 0) {
869ccd979bdSMark Fasheh 		mlog_errno(status);
870ccd979bdSMark Fasheh 		goto out_rollback;
871ccd979bdSMark Fasheh 	}
872ccd979bdSMark Fasheh 
873ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, bg_bh,
874ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
875ccd979bdSMark Fasheh 	if (status < 0) {
876ccd979bdSMark Fasheh 		mlog_errno(status);
877ccd979bdSMark Fasheh 		goto out_rollback;
878ccd979bdSMark Fasheh 	}
879ccd979bdSMark Fasheh 
880ccd979bdSMark Fasheh 	bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
881ccd979bdSMark Fasheh 
882ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, bg_bh);
883ccd979bdSMark Fasheh 	if (status < 0) {
884ccd979bdSMark Fasheh 		mlog_errno(status);
885ccd979bdSMark Fasheh 		goto out_rollback;
886ccd979bdSMark Fasheh 	}
887ccd979bdSMark Fasheh 
888ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
889ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
890ccd979bdSMark Fasheh 	if (status < 0) {
891ccd979bdSMark Fasheh 		mlog_errno(status);
892ccd979bdSMark Fasheh 		goto out_rollback;
893ccd979bdSMark Fasheh 	}
894ccd979bdSMark Fasheh 
895ccd979bdSMark Fasheh 	fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
896ccd979bdSMark Fasheh 
897ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, fe_bh);
898ccd979bdSMark Fasheh 	if (status < 0) {
899ccd979bdSMark Fasheh 		mlog_errno(status);
900ccd979bdSMark Fasheh 		goto out_rollback;
901ccd979bdSMark Fasheh 	}
902ccd979bdSMark Fasheh 
903ccd979bdSMark Fasheh 	status = 0;
904ccd979bdSMark Fasheh out_rollback:
905ccd979bdSMark Fasheh 	if (status < 0) {
906ccd979bdSMark Fasheh 		fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
907ccd979bdSMark Fasheh 		bg->bg_next_group = cpu_to_le64(bg_ptr);
908ccd979bdSMark Fasheh 		prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
909ccd979bdSMark Fasheh 	}
910ccd979bdSMark Fasheh out:
911ccd979bdSMark Fasheh 	mlog_exit(status);
912ccd979bdSMark Fasheh 	return status;
913ccd979bdSMark Fasheh }
914ccd979bdSMark Fasheh 
915ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
916ccd979bdSMark Fasheh 						     u32 wanted)
917ccd979bdSMark Fasheh {
918ccd979bdSMark Fasheh 	return le16_to_cpu(bg->bg_free_bits_count) > wanted;
919ccd979bdSMark Fasheh }
920ccd979bdSMark Fasheh 
921ccd979bdSMark Fasheh /* return 0 on success, -ENOSPC to keep searching and any other < 0
922ccd979bdSMark Fasheh  * value on error. */
923ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode,
924ccd979bdSMark Fasheh 				      struct buffer_head *group_bh,
925ccd979bdSMark Fasheh 				      u32 bits_wanted, u32 min_bits,
926ccd979bdSMark Fasheh 				      u16 *bit_off, u16 *bits_found)
927ccd979bdSMark Fasheh {
928ccd979bdSMark Fasheh 	int search = -ENOSPC;
929ccd979bdSMark Fasheh 	int ret;
9307bf72edeSMark Fasheh 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
931ccd979bdSMark Fasheh 	u16 tmp_off, tmp_found;
9327bf72edeSMark Fasheh 	unsigned int max_bits, gd_cluster_off;
933ccd979bdSMark Fasheh 
934ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
935ccd979bdSMark Fasheh 
9367bf72edeSMark Fasheh 	if (gd->bg_free_bits_count) {
9377bf72edeSMark Fasheh 		max_bits = le16_to_cpu(gd->bg_bits);
9387bf72edeSMark Fasheh 
9397bf72edeSMark Fasheh 		/* Tail groups in cluster bitmaps which aren't cpg
9407bf72edeSMark Fasheh 		 * aligned are prone to partial extention by a failed
9417bf72edeSMark Fasheh 		 * fs resize. If the file system resize never got to
9427bf72edeSMark Fasheh 		 * update the dinode cluster count, then we don't want
9437bf72edeSMark Fasheh 		 * to trust any clusters past it, regardless of what
9447bf72edeSMark Fasheh 		 * the group descriptor says. */
9457bf72edeSMark Fasheh 		gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
9467bf72edeSMark Fasheh 							  le64_to_cpu(gd->bg_blkno));
9477bf72edeSMark Fasheh 		if ((gd_cluster_off + max_bits) >
9487bf72edeSMark Fasheh 		    OCFS2_I(inode)->ip_clusters) {
9497bf72edeSMark Fasheh 			max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
9507bf72edeSMark Fasheh 			mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
9517bf72edeSMark Fasheh 			     (unsigned long long)le64_to_cpu(gd->bg_blkno),
9527bf72edeSMark Fasheh 			     le16_to_cpu(gd->bg_bits),
9537bf72edeSMark Fasheh 			     OCFS2_I(inode)->ip_clusters, max_bits);
9547bf72edeSMark Fasheh 		}
9557bf72edeSMark Fasheh 
956ccd979bdSMark Fasheh 		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
957ccd979bdSMark Fasheh 							group_bh, bits_wanted,
9587bf72edeSMark Fasheh 							max_bits,
959ccd979bdSMark Fasheh 							&tmp_off, &tmp_found);
960ccd979bdSMark Fasheh 		if (ret)
961ccd979bdSMark Fasheh 			return ret;
962ccd979bdSMark Fasheh 
963ccd979bdSMark Fasheh 		/* ocfs2_block_group_find_clear_bits() might
964ccd979bdSMark Fasheh 		 * return success, but we still want to return
965ccd979bdSMark Fasheh 		 * -ENOSPC unless it found the minimum number
966ccd979bdSMark Fasheh 		 * of bits. */
967ccd979bdSMark Fasheh 		if (min_bits <= tmp_found) {
968ccd979bdSMark Fasheh 			*bit_off = tmp_off;
969ccd979bdSMark Fasheh 			*bits_found = tmp_found;
970ccd979bdSMark Fasheh 			search = 0; /* success */
971ccd979bdSMark Fasheh 		}
972ccd979bdSMark Fasheh 	}
973ccd979bdSMark Fasheh 
974ccd979bdSMark Fasheh 	return search;
975ccd979bdSMark Fasheh }
976ccd979bdSMark Fasheh 
977ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode,
978ccd979bdSMark Fasheh 				    struct buffer_head *group_bh,
979ccd979bdSMark Fasheh 				    u32 bits_wanted, u32 min_bits,
980ccd979bdSMark Fasheh 				    u16 *bit_off, u16 *bits_found)
981ccd979bdSMark Fasheh {
982ccd979bdSMark Fasheh 	int ret = -ENOSPC;
983ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
984ccd979bdSMark Fasheh 
985ccd979bdSMark Fasheh 	BUG_ON(min_bits != 1);
986ccd979bdSMark Fasheh 	BUG_ON(ocfs2_is_cluster_bitmap(inode));
987ccd979bdSMark Fasheh 
988ccd979bdSMark Fasheh 	if (bg->bg_free_bits_count)
989ccd979bdSMark Fasheh 		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
990ccd979bdSMark Fasheh 							group_bh, bits_wanted,
9917bf72edeSMark Fasheh 							le16_to_cpu(bg->bg_bits),
992ccd979bdSMark Fasheh 							bit_off, bits_found);
993ccd979bdSMark Fasheh 
994ccd979bdSMark Fasheh 	return ret;
995ccd979bdSMark Fasheh }
996ccd979bdSMark Fasheh 
997883d4caeSMark Fasheh static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
9981fabe148SMark Fasheh 				       handle_t *handle,
999883d4caeSMark Fasheh 				       struct buffer_head *di_bh,
1000883d4caeSMark Fasheh 				       u32 num_bits,
1001883d4caeSMark Fasheh 				       u16 chain)
1002883d4caeSMark Fasheh {
1003883d4caeSMark Fasheh 	int ret;
1004883d4caeSMark Fasheh 	u32 tmp_used;
1005883d4caeSMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1006883d4caeSMark Fasheh 	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1007883d4caeSMark Fasheh 
1008883d4caeSMark Fasheh 	ret = ocfs2_journal_access(handle, inode, di_bh,
1009883d4caeSMark Fasheh 				   OCFS2_JOURNAL_ACCESS_WRITE);
1010883d4caeSMark Fasheh 	if (ret < 0) {
1011883d4caeSMark Fasheh 		mlog_errno(ret);
1012883d4caeSMark Fasheh 		goto out;
1013883d4caeSMark Fasheh 	}
1014883d4caeSMark Fasheh 
1015883d4caeSMark Fasheh 	tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1016883d4caeSMark Fasheh 	di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1017883d4caeSMark Fasheh 	le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1018883d4caeSMark Fasheh 
1019883d4caeSMark Fasheh 	ret = ocfs2_journal_dirty(handle, di_bh);
1020883d4caeSMark Fasheh 	if (ret < 0)
1021883d4caeSMark Fasheh 		mlog_errno(ret);
1022883d4caeSMark Fasheh 
1023883d4caeSMark Fasheh out:
1024883d4caeSMark Fasheh 	return ret;
1025883d4caeSMark Fasheh }
1026883d4caeSMark Fasheh 
1027883d4caeSMark Fasheh static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
10281fabe148SMark Fasheh 				  handle_t *handle,
1029883d4caeSMark Fasheh 				  u32 bits_wanted,
1030883d4caeSMark Fasheh 				  u32 min_bits,
1031883d4caeSMark Fasheh 				  u16 *bit_off,
1032883d4caeSMark Fasheh 				  unsigned int *num_bits,
1033883d4caeSMark Fasheh 				  u64 gd_blkno,
1034883d4caeSMark Fasheh 				  u16 *bits_left)
1035883d4caeSMark Fasheh {
1036883d4caeSMark Fasheh 	int ret;
1037883d4caeSMark Fasheh 	u16 found;
1038883d4caeSMark Fasheh 	struct buffer_head *group_bh = NULL;
1039883d4caeSMark Fasheh 	struct ocfs2_group_desc *gd;
1040883d4caeSMark Fasheh 	struct inode *alloc_inode = ac->ac_inode;
1041883d4caeSMark Fasheh 
1042883d4caeSMark Fasheh 	ret = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), gd_blkno,
1043883d4caeSMark Fasheh 			       &group_bh, OCFS2_BH_CACHED, alloc_inode);
1044883d4caeSMark Fasheh 	if (ret < 0) {
1045883d4caeSMark Fasheh 		mlog_errno(ret);
1046883d4caeSMark Fasheh 		return ret;
1047883d4caeSMark Fasheh 	}
1048883d4caeSMark Fasheh 
1049883d4caeSMark Fasheh 	gd = (struct ocfs2_group_desc *) group_bh->b_data;
1050883d4caeSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1051883d4caeSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, gd);
1052883d4caeSMark Fasheh 		ret = -EIO;
1053883d4caeSMark Fasheh 		goto out;
1054883d4caeSMark Fasheh 	}
1055883d4caeSMark Fasheh 
1056883d4caeSMark Fasheh 	ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1057883d4caeSMark Fasheh 				  bit_off, &found);
1058883d4caeSMark Fasheh 	if (ret < 0) {
1059883d4caeSMark Fasheh 		if (ret != -ENOSPC)
1060883d4caeSMark Fasheh 			mlog_errno(ret);
1061883d4caeSMark Fasheh 		goto out;
1062883d4caeSMark Fasheh 	}
1063883d4caeSMark Fasheh 
1064883d4caeSMark Fasheh 	*num_bits = found;
1065883d4caeSMark Fasheh 
1066883d4caeSMark Fasheh 	ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1067883d4caeSMark Fasheh 					       *num_bits,
1068883d4caeSMark Fasheh 					       le16_to_cpu(gd->bg_chain));
1069883d4caeSMark Fasheh 	if (ret < 0) {
1070883d4caeSMark Fasheh 		mlog_errno(ret);
1071883d4caeSMark Fasheh 		goto out;
1072883d4caeSMark Fasheh 	}
1073883d4caeSMark Fasheh 
1074883d4caeSMark Fasheh 	ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1075883d4caeSMark Fasheh 					 *bit_off, *num_bits);
1076883d4caeSMark Fasheh 	if (ret < 0)
1077883d4caeSMark Fasheh 		mlog_errno(ret);
1078883d4caeSMark Fasheh 
1079883d4caeSMark Fasheh 	*bits_left = le16_to_cpu(gd->bg_free_bits_count);
1080883d4caeSMark Fasheh 
1081883d4caeSMark Fasheh out:
1082883d4caeSMark Fasheh 	brelse(group_bh);
1083883d4caeSMark Fasheh 
1084883d4caeSMark Fasheh 	return ret;
1085883d4caeSMark Fasheh }
1086883d4caeSMark Fasheh 
1087ccd979bdSMark Fasheh static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
10881fabe148SMark Fasheh 			      handle_t *handle,
1089ccd979bdSMark Fasheh 			      u32 bits_wanted,
1090ccd979bdSMark Fasheh 			      u32 min_bits,
1091ccd979bdSMark Fasheh 			      u16 *bit_off,
1092ccd979bdSMark Fasheh 			      unsigned int *num_bits,
1093883d4caeSMark Fasheh 			      u64 *bg_blkno,
1094883d4caeSMark Fasheh 			      u16 *bits_left)
1095ccd979bdSMark Fasheh {
1096ccd979bdSMark Fasheh 	int status;
1097ccd979bdSMark Fasheh 	u16 chain, tmp_bits;
1098ccd979bdSMark Fasheh 	u32 tmp_used;
1099ccd979bdSMark Fasheh 	u64 next_group;
1100ccd979bdSMark Fasheh 	struct inode *alloc_inode = ac->ac_inode;
1101ccd979bdSMark Fasheh 	struct buffer_head *group_bh = NULL;
1102ccd979bdSMark Fasheh 	struct buffer_head *prev_group_bh = NULL;
1103ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1104ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1105ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg;
1106ccd979bdSMark Fasheh 
1107ccd979bdSMark Fasheh 	chain = ac->ac_chain;
1108b0697053SMark Fasheh 	mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1109b0697053SMark Fasheh 	     bits_wanted, chain,
1110b0697053SMark Fasheh 	     (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
1111ccd979bdSMark Fasheh 
1112ccd979bdSMark Fasheh 	status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1113ccd979bdSMark Fasheh 				  le64_to_cpu(cl->cl_recs[chain].c_blkno),
1114ccd979bdSMark Fasheh 				  &group_bh, OCFS2_BH_CACHED, alloc_inode);
1115ccd979bdSMark Fasheh 	if (status < 0) {
1116ccd979bdSMark Fasheh 		mlog_errno(status);
1117ccd979bdSMark Fasheh 		goto bail;
1118ccd979bdSMark Fasheh 	}
1119ccd979bdSMark Fasheh 	bg = (struct ocfs2_group_desc *) group_bh->b_data;
11207bf72edeSMark Fasheh 	status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
11217bf72edeSMark Fasheh 	if (status) {
11227bf72edeSMark Fasheh 		mlog_errno(status);
1123ccd979bdSMark Fasheh 		goto bail;
1124ccd979bdSMark Fasheh 	}
1125ccd979bdSMark Fasheh 
1126ccd979bdSMark Fasheh 	status = -ENOSPC;
1127ccd979bdSMark Fasheh 	/* for now, the chain search is a bit simplistic. We just use
1128ccd979bdSMark Fasheh 	 * the 1st group with any empty bits. */
1129ccd979bdSMark Fasheh 	while ((status = ac->ac_group_search(alloc_inode, group_bh,
1130ccd979bdSMark Fasheh 					     bits_wanted, min_bits, bit_off,
1131ccd979bdSMark Fasheh 					     &tmp_bits)) == -ENOSPC) {
1132ccd979bdSMark Fasheh 		if (!bg->bg_next_group)
1133ccd979bdSMark Fasheh 			break;
1134ccd979bdSMark Fasheh 
1135ccd979bdSMark Fasheh 		if (prev_group_bh) {
1136ccd979bdSMark Fasheh 			brelse(prev_group_bh);
1137ccd979bdSMark Fasheh 			prev_group_bh = NULL;
1138ccd979bdSMark Fasheh 		}
1139ccd979bdSMark Fasheh 		next_group = le64_to_cpu(bg->bg_next_group);
1140ccd979bdSMark Fasheh 		prev_group_bh = group_bh;
1141ccd979bdSMark Fasheh 		group_bh = NULL;
1142ccd979bdSMark Fasheh 		status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1143ccd979bdSMark Fasheh 					  next_group, &group_bh,
1144ccd979bdSMark Fasheh 					  OCFS2_BH_CACHED, alloc_inode);
1145ccd979bdSMark Fasheh 		if (status < 0) {
1146ccd979bdSMark Fasheh 			mlog_errno(status);
1147ccd979bdSMark Fasheh 			goto bail;
1148ccd979bdSMark Fasheh 		}
1149ccd979bdSMark Fasheh 		bg = (struct ocfs2_group_desc *) group_bh->b_data;
11507bf72edeSMark Fasheh 		status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
11517bf72edeSMark Fasheh 		if (status) {
11527bf72edeSMark Fasheh 			mlog_errno(status);
1153ccd979bdSMark Fasheh 			goto bail;
1154ccd979bdSMark Fasheh 		}
1155ccd979bdSMark Fasheh 	}
1156ccd979bdSMark Fasheh 	if (status < 0) {
1157ccd979bdSMark Fasheh 		if (status != -ENOSPC)
1158ccd979bdSMark Fasheh 			mlog_errno(status);
1159ccd979bdSMark Fasheh 		goto bail;
1160ccd979bdSMark Fasheh 	}
1161ccd979bdSMark Fasheh 
1162b0697053SMark Fasheh 	mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
11631ca1a111SMark Fasheh 	     tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
1164ccd979bdSMark Fasheh 
1165ccd979bdSMark Fasheh 	*num_bits = tmp_bits;
1166ccd979bdSMark Fasheh 
1167ccd979bdSMark Fasheh 	BUG_ON(*num_bits == 0);
1168ccd979bdSMark Fasheh 
1169ccd979bdSMark Fasheh 	/*
1170ccd979bdSMark Fasheh 	 * Keep track of previous block descriptor read. When
1171ccd979bdSMark Fasheh 	 * we find a target, if we have read more than X
1172ccd979bdSMark Fasheh 	 * number of descriptors, and the target is reasonably
1173ccd979bdSMark Fasheh 	 * empty, relink him to top of his chain.
1174ccd979bdSMark Fasheh 	 *
1175ccd979bdSMark Fasheh 	 * We've read 0 extra blocks and only send one more to
1176ccd979bdSMark Fasheh 	 * the transaction, yet the next guy to search has a
1177ccd979bdSMark Fasheh 	 * much easier time.
1178ccd979bdSMark Fasheh 	 *
1179ccd979bdSMark Fasheh 	 * Do this *after* figuring out how many bits we're taking out
1180ccd979bdSMark Fasheh 	 * of our target group.
1181ccd979bdSMark Fasheh 	 */
1182ccd979bdSMark Fasheh 	if (ac->ac_allow_chain_relink &&
1183ccd979bdSMark Fasheh 	    (prev_group_bh) &&
1184ccd979bdSMark Fasheh 	    (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1185ccd979bdSMark Fasheh 		status = ocfs2_relink_block_group(handle, alloc_inode,
1186ccd979bdSMark Fasheh 						  ac->ac_bh, group_bh,
1187ccd979bdSMark Fasheh 						  prev_group_bh, chain);
1188ccd979bdSMark Fasheh 		if (status < 0) {
1189ccd979bdSMark Fasheh 			mlog_errno(status);
1190ccd979bdSMark Fasheh 			goto bail;
1191ccd979bdSMark Fasheh 		}
1192ccd979bdSMark Fasheh 	}
1193ccd979bdSMark Fasheh 
1194ccd979bdSMark Fasheh 	/* Ok, claim our bits now: set the info on dinode, chainlist
1195ccd979bdSMark Fasheh 	 * and then the group */
1196ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle,
1197ccd979bdSMark Fasheh 				      alloc_inode,
1198ccd979bdSMark Fasheh 				      ac->ac_bh,
1199ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
1200ccd979bdSMark Fasheh 	if (status < 0) {
1201ccd979bdSMark Fasheh 		mlog_errno(status);
1202ccd979bdSMark Fasheh 		goto bail;
1203ccd979bdSMark Fasheh 	}
1204ccd979bdSMark Fasheh 
1205ccd979bdSMark Fasheh 	tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1206ccd979bdSMark Fasheh 	fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1207ccd979bdSMark Fasheh 	le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1208ccd979bdSMark Fasheh 
1209ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle,
1210ccd979bdSMark Fasheh 				     ac->ac_bh);
1211ccd979bdSMark Fasheh 	if (status < 0) {
1212ccd979bdSMark Fasheh 		mlog_errno(status);
1213ccd979bdSMark Fasheh 		goto bail;
1214ccd979bdSMark Fasheh 	}
1215ccd979bdSMark Fasheh 
1216ccd979bdSMark Fasheh 	status = ocfs2_block_group_set_bits(handle,
1217ccd979bdSMark Fasheh 					    alloc_inode,
1218ccd979bdSMark Fasheh 					    bg,
1219ccd979bdSMark Fasheh 					    group_bh,
1220ccd979bdSMark Fasheh 					    *bit_off,
1221ccd979bdSMark Fasheh 					    *num_bits);
1222ccd979bdSMark Fasheh 	if (status < 0) {
1223ccd979bdSMark Fasheh 		mlog_errno(status);
1224ccd979bdSMark Fasheh 		goto bail;
1225ccd979bdSMark Fasheh 	}
1226ccd979bdSMark Fasheh 
1227b0697053SMark Fasheh 	mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
12281ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(fe->i_blkno));
1229ccd979bdSMark Fasheh 
1230ccd979bdSMark Fasheh 	*bg_blkno = le64_to_cpu(bg->bg_blkno);
1231883d4caeSMark Fasheh 	*bits_left = le16_to_cpu(bg->bg_free_bits_count);
1232ccd979bdSMark Fasheh bail:
1233ccd979bdSMark Fasheh 	if (group_bh)
1234ccd979bdSMark Fasheh 		brelse(group_bh);
1235ccd979bdSMark Fasheh 	if (prev_group_bh)
1236ccd979bdSMark Fasheh 		brelse(prev_group_bh);
1237ccd979bdSMark Fasheh 
1238ccd979bdSMark Fasheh 	mlog_exit(status);
1239ccd979bdSMark Fasheh 	return status;
1240ccd979bdSMark Fasheh }
1241ccd979bdSMark Fasheh 
1242ccd979bdSMark Fasheh /* will give out up to bits_wanted contiguous bits. */
1243ccd979bdSMark Fasheh static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1244ccd979bdSMark Fasheh 				     struct ocfs2_alloc_context *ac,
12451fabe148SMark Fasheh 				     handle_t *handle,
1246ccd979bdSMark Fasheh 				     u32 bits_wanted,
1247ccd979bdSMark Fasheh 				     u32 min_bits,
1248ccd979bdSMark Fasheh 				     u16 *bit_off,
1249ccd979bdSMark Fasheh 				     unsigned int *num_bits,
1250ccd979bdSMark Fasheh 				     u64 *bg_blkno)
1251ccd979bdSMark Fasheh {
1252ccd979bdSMark Fasheh 	int status;
1253ccd979bdSMark Fasheh 	u16 victim, i;
1254883d4caeSMark Fasheh 	u16 bits_left = 0;
1255883d4caeSMark Fasheh 	u64 hint_blkno = ac->ac_last_group;
1256ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl;
1257ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
1258ccd979bdSMark Fasheh 
1259ccd979bdSMark Fasheh 	mlog_entry_void();
1260ccd979bdSMark Fasheh 
1261ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1262ccd979bdSMark Fasheh 	BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1263ccd979bdSMark Fasheh 	BUG_ON(!ac->ac_bh);
1264ccd979bdSMark Fasheh 
1265ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1266ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
1267ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(osb->sb, fe);
1268ccd979bdSMark Fasheh 		status = -EIO;
1269ccd979bdSMark Fasheh 		goto bail;
1270ccd979bdSMark Fasheh 	}
1271ccd979bdSMark Fasheh 	if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1272ccd979bdSMark Fasheh 	    le32_to_cpu(fe->id1.bitmap1.i_total)) {
1273b0697053SMark Fasheh 		ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1274b0697053SMark Fasheh 			    "bits but only %u total.",
1275b0697053SMark Fasheh 			    (unsigned long long)le64_to_cpu(fe->i_blkno),
1276ccd979bdSMark Fasheh 			    le32_to_cpu(fe->id1.bitmap1.i_used),
1277ccd979bdSMark Fasheh 			    le32_to_cpu(fe->id1.bitmap1.i_total));
1278ccd979bdSMark Fasheh 		status = -EIO;
1279ccd979bdSMark Fasheh 		goto bail;
1280ccd979bdSMark Fasheh 	}
1281ccd979bdSMark Fasheh 
1282883d4caeSMark Fasheh 	if (hint_blkno) {
1283883d4caeSMark Fasheh 		/* Attempt to short-circuit the usual search mechanism
1284883d4caeSMark Fasheh 		 * by jumping straight to the most recently used
1285883d4caeSMark Fasheh 		 * allocation group. This helps us mantain some
1286883d4caeSMark Fasheh 		 * contiguousness across allocations. */
1287da5cbf2fSMark Fasheh 		status = ocfs2_search_one_group(ac, handle, bits_wanted,
1288da5cbf2fSMark Fasheh 						min_bits, bit_off, num_bits,
1289883d4caeSMark Fasheh 						hint_blkno, &bits_left);
1290883d4caeSMark Fasheh 		if (!status) {
1291883d4caeSMark Fasheh 			/* Be careful to update *bg_blkno here as the
1292883d4caeSMark Fasheh 			 * caller is expecting it to be filled in, and
1293883d4caeSMark Fasheh 			 * ocfs2_search_one_group() won't do that for
1294883d4caeSMark Fasheh 			 * us. */
1295883d4caeSMark Fasheh 			*bg_blkno = hint_blkno;
1296883d4caeSMark Fasheh 			goto set_hint;
1297883d4caeSMark Fasheh 		}
1298883d4caeSMark Fasheh 		if (status < 0 && status != -ENOSPC) {
1299883d4caeSMark Fasheh 			mlog_errno(status);
1300883d4caeSMark Fasheh 			goto bail;
1301883d4caeSMark Fasheh 		}
1302883d4caeSMark Fasheh 	}
1303883d4caeSMark Fasheh 
1304ccd979bdSMark Fasheh 	cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1305ccd979bdSMark Fasheh 
1306ccd979bdSMark Fasheh 	victim = ocfs2_find_victim_chain(cl);
1307ccd979bdSMark Fasheh 	ac->ac_chain = victim;
1308ccd979bdSMark Fasheh 	ac->ac_allow_chain_relink = 1;
1309ccd979bdSMark Fasheh 
1310da5cbf2fSMark Fasheh 	status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
1311883d4caeSMark Fasheh 				    num_bits, bg_blkno, &bits_left);
1312ccd979bdSMark Fasheh 	if (!status)
1313883d4caeSMark Fasheh 		goto set_hint;
1314ccd979bdSMark Fasheh 	if (status < 0 && status != -ENOSPC) {
1315ccd979bdSMark Fasheh 		mlog_errno(status);
1316ccd979bdSMark Fasheh 		goto bail;
1317ccd979bdSMark Fasheh 	}
1318ccd979bdSMark Fasheh 
1319ccd979bdSMark Fasheh 	mlog(0, "Search of victim chain %u came up with nothing, "
1320ccd979bdSMark Fasheh 	     "trying all chains now.\n", victim);
1321ccd979bdSMark Fasheh 
1322ccd979bdSMark Fasheh 	/* If we didn't pick a good victim, then just default to
1323ccd979bdSMark Fasheh 	 * searching each chain in order. Don't allow chain relinking
1324ccd979bdSMark Fasheh 	 * because we only calculate enough journal credits for one
1325ccd979bdSMark Fasheh 	 * relink per alloc. */
1326ccd979bdSMark Fasheh 	ac->ac_allow_chain_relink = 0;
1327ccd979bdSMark Fasheh 	for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1328ccd979bdSMark Fasheh 		if (i == victim)
1329ccd979bdSMark Fasheh 			continue;
1330ccd979bdSMark Fasheh 		if (!cl->cl_recs[i].c_free)
1331ccd979bdSMark Fasheh 			continue;
1332ccd979bdSMark Fasheh 
1333ccd979bdSMark Fasheh 		ac->ac_chain = i;
1334da5cbf2fSMark Fasheh 		status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1335883d4caeSMark Fasheh 					    bit_off, num_bits, bg_blkno,
1336883d4caeSMark Fasheh 					    &bits_left);
1337ccd979bdSMark Fasheh 		if (!status)
1338ccd979bdSMark Fasheh 			break;
1339ccd979bdSMark Fasheh 		if (status < 0 && status != -ENOSPC) {
1340ccd979bdSMark Fasheh 			mlog_errno(status);
1341ccd979bdSMark Fasheh 			goto bail;
1342ccd979bdSMark Fasheh 		}
1343ccd979bdSMark Fasheh 	}
1344ccd979bdSMark Fasheh 
1345883d4caeSMark Fasheh set_hint:
1346883d4caeSMark Fasheh 	if (status != -ENOSPC) {
1347883d4caeSMark Fasheh 		/* If the next search of this group is not likely to
1348883d4caeSMark Fasheh 		 * yield a suitable extent, then we reset the last
1349883d4caeSMark Fasheh 		 * group hint so as to not waste a disk read */
1350883d4caeSMark Fasheh 		if (bits_left < min_bits)
1351883d4caeSMark Fasheh 			ac->ac_last_group = 0;
1352883d4caeSMark Fasheh 		else
1353883d4caeSMark Fasheh 			ac->ac_last_group = *bg_blkno;
1354883d4caeSMark Fasheh 	}
1355883d4caeSMark Fasheh 
1356883d4caeSMark Fasheh bail:
1357ccd979bdSMark Fasheh 	mlog_exit(status);
1358ccd979bdSMark Fasheh 	return status;
1359ccd979bdSMark Fasheh }
1360ccd979bdSMark Fasheh 
1361ccd979bdSMark Fasheh int ocfs2_claim_metadata(struct ocfs2_super *osb,
13621fabe148SMark Fasheh 			 handle_t *handle,
1363ccd979bdSMark Fasheh 			 struct ocfs2_alloc_context *ac,
1364ccd979bdSMark Fasheh 			 u32 bits_wanted,
1365ccd979bdSMark Fasheh 			 u16 *suballoc_bit_start,
1366ccd979bdSMark Fasheh 			 unsigned int *num_bits,
1367ccd979bdSMark Fasheh 			 u64 *blkno_start)
1368ccd979bdSMark Fasheh {
1369ccd979bdSMark Fasheh 	int status;
1370ccd979bdSMark Fasheh 	u64 bg_blkno;
1371ccd979bdSMark Fasheh 
1372ccd979bdSMark Fasheh 	BUG_ON(!ac);
1373ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1374ccd979bdSMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
1375ccd979bdSMark Fasheh 
1376ccd979bdSMark Fasheh 	status = ocfs2_claim_suballoc_bits(osb,
1377ccd979bdSMark Fasheh 					   ac,
1378da5cbf2fSMark Fasheh 					   handle,
1379ccd979bdSMark Fasheh 					   bits_wanted,
1380ccd979bdSMark Fasheh 					   1,
1381ccd979bdSMark Fasheh 					   suballoc_bit_start,
1382ccd979bdSMark Fasheh 					   num_bits,
1383ccd979bdSMark Fasheh 					   &bg_blkno);
1384ccd979bdSMark Fasheh 	if (status < 0) {
1385ccd979bdSMark Fasheh 		mlog_errno(status);
1386ccd979bdSMark Fasheh 		goto bail;
1387ccd979bdSMark Fasheh 	}
1388ccd979bdSMark Fasheh 	atomic_inc(&osb->alloc_stats.bg_allocs);
1389ccd979bdSMark Fasheh 
1390ccd979bdSMark Fasheh 	*blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1391ccd979bdSMark Fasheh 	ac->ac_bits_given += (*num_bits);
1392ccd979bdSMark Fasheh 	status = 0;
1393ccd979bdSMark Fasheh bail:
1394ccd979bdSMark Fasheh 	mlog_exit(status);
1395ccd979bdSMark Fasheh 	return status;
1396ccd979bdSMark Fasheh }
1397ccd979bdSMark Fasheh 
1398ccd979bdSMark Fasheh int ocfs2_claim_new_inode(struct ocfs2_super *osb,
13991fabe148SMark Fasheh 			  handle_t *handle,
1400ccd979bdSMark Fasheh 			  struct ocfs2_alloc_context *ac,
1401ccd979bdSMark Fasheh 			  u16 *suballoc_bit,
1402ccd979bdSMark Fasheh 			  u64 *fe_blkno)
1403ccd979bdSMark Fasheh {
1404ccd979bdSMark Fasheh 	int status;
1405ccd979bdSMark Fasheh 	unsigned int num_bits;
1406ccd979bdSMark Fasheh 	u64 bg_blkno;
1407ccd979bdSMark Fasheh 
1408ccd979bdSMark Fasheh 	mlog_entry_void();
1409ccd979bdSMark Fasheh 
1410ccd979bdSMark Fasheh 	BUG_ON(!ac);
1411ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_given != 0);
1412ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_wanted != 1);
1413ccd979bdSMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
1414ccd979bdSMark Fasheh 
1415ccd979bdSMark Fasheh 	status = ocfs2_claim_suballoc_bits(osb,
1416ccd979bdSMark Fasheh 					   ac,
1417da5cbf2fSMark Fasheh 					   handle,
1418ccd979bdSMark Fasheh 					   1,
1419ccd979bdSMark Fasheh 					   1,
1420ccd979bdSMark Fasheh 					   suballoc_bit,
1421ccd979bdSMark Fasheh 					   &num_bits,
1422ccd979bdSMark Fasheh 					   &bg_blkno);
1423ccd979bdSMark Fasheh 	if (status < 0) {
1424ccd979bdSMark Fasheh 		mlog_errno(status);
1425ccd979bdSMark Fasheh 		goto bail;
1426ccd979bdSMark Fasheh 	}
1427ccd979bdSMark Fasheh 	atomic_inc(&osb->alloc_stats.bg_allocs);
1428ccd979bdSMark Fasheh 
1429ccd979bdSMark Fasheh 	BUG_ON(num_bits != 1);
1430ccd979bdSMark Fasheh 
1431ccd979bdSMark Fasheh 	*fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1432ccd979bdSMark Fasheh 	ac->ac_bits_given++;
1433ccd979bdSMark Fasheh 	status = 0;
1434ccd979bdSMark Fasheh bail:
1435ccd979bdSMark Fasheh 	mlog_exit(status);
1436ccd979bdSMark Fasheh 	return status;
1437ccd979bdSMark Fasheh }
1438ccd979bdSMark Fasheh 
1439ccd979bdSMark Fasheh /* translate a group desc. blkno and it's bitmap offset into
1440ccd979bdSMark Fasheh  * disk cluster offset. */
1441ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1442ccd979bdSMark Fasheh 						   u64 bg_blkno,
1443ccd979bdSMark Fasheh 						   u16 bg_bit_off)
1444ccd979bdSMark Fasheh {
1445ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1446ccd979bdSMark Fasheh 	u32 cluster = 0;
1447ccd979bdSMark Fasheh 
1448ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1449ccd979bdSMark Fasheh 
1450ccd979bdSMark Fasheh 	if (bg_blkno != osb->first_cluster_group_blkno)
1451ccd979bdSMark Fasheh 		cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1452ccd979bdSMark Fasheh 	cluster += (u32) bg_bit_off;
1453ccd979bdSMark Fasheh 	return cluster;
1454ccd979bdSMark Fasheh }
1455ccd979bdSMark Fasheh 
1456ccd979bdSMark Fasheh /* given a cluster offset, calculate which block group it belongs to
1457ccd979bdSMark Fasheh  * and return that block offset. */
1458d659072fSTao Ma u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
1459ccd979bdSMark Fasheh {
1460ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1461ccd979bdSMark Fasheh 	u32 group_no;
1462ccd979bdSMark Fasheh 
1463ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1464ccd979bdSMark Fasheh 
1465ccd979bdSMark Fasheh 	group_no = cluster / osb->bitmap_cpg;
1466ccd979bdSMark Fasheh 	if (!group_no)
1467ccd979bdSMark Fasheh 		return osb->first_cluster_group_blkno;
1468ccd979bdSMark Fasheh 	return ocfs2_clusters_to_blocks(inode->i_sb,
1469ccd979bdSMark Fasheh 					group_no * osb->bitmap_cpg);
1470ccd979bdSMark Fasheh }
1471ccd979bdSMark Fasheh 
1472ccd979bdSMark Fasheh /* given the block number of a cluster start, calculate which cluster
1473ccd979bdSMark Fasheh  * group and descriptor bitmap offset that corresponds to. */
1474ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1475ccd979bdSMark Fasheh 						u64 data_blkno,
1476ccd979bdSMark Fasheh 						u64 *bg_blkno,
1477ccd979bdSMark Fasheh 						u16 *bg_bit_off)
1478ccd979bdSMark Fasheh {
1479ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1480ccd979bdSMark Fasheh 	u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1481ccd979bdSMark Fasheh 
1482ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1483ccd979bdSMark Fasheh 
1484ccd979bdSMark Fasheh 	*bg_blkno = ocfs2_which_cluster_group(inode,
1485ccd979bdSMark Fasheh 					      data_cluster);
1486ccd979bdSMark Fasheh 
1487ccd979bdSMark Fasheh 	if (*bg_blkno == osb->first_cluster_group_blkno)
1488ccd979bdSMark Fasheh 		*bg_bit_off = (u16) data_cluster;
1489ccd979bdSMark Fasheh 	else
1490ccd979bdSMark Fasheh 		*bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1491ccd979bdSMark Fasheh 							     data_blkno - *bg_blkno);
1492ccd979bdSMark Fasheh }
1493ccd979bdSMark Fasheh 
1494ccd979bdSMark Fasheh /*
1495ccd979bdSMark Fasheh  * min_bits - minimum contiguous chunk from this total allocation we
1496ccd979bdSMark Fasheh  * can handle. set to what we asked for originally for a full
1497ccd979bdSMark Fasheh  * contig. allocation, set to '1' to indicate we can deal with extents
1498ccd979bdSMark Fasheh  * of any size.
1499ccd979bdSMark Fasheh  */
1500415cb800SMark Fasheh int __ocfs2_claim_clusters(struct ocfs2_super *osb,
15011fabe148SMark Fasheh 			   handle_t *handle,
1502ccd979bdSMark Fasheh 			   struct ocfs2_alloc_context *ac,
1503ccd979bdSMark Fasheh 			   u32 min_clusters,
1504415cb800SMark Fasheh 			   u32 max_clusters,
1505ccd979bdSMark Fasheh 			   u32 *cluster_start,
1506ccd979bdSMark Fasheh 			   u32 *num_clusters)
1507ccd979bdSMark Fasheh {
1508ccd979bdSMark Fasheh 	int status;
1509415cb800SMark Fasheh 	unsigned int bits_wanted = max_clusters;
1510883d4caeSMark Fasheh 	u64 bg_blkno = 0;
1511ccd979bdSMark Fasheh 	u16 bg_bit_off;
1512ccd979bdSMark Fasheh 
1513ccd979bdSMark Fasheh 	mlog_entry_void();
1514ccd979bdSMark Fasheh 
1515ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1516ccd979bdSMark Fasheh 
1517ccd979bdSMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1518ccd979bdSMark Fasheh 	       && ac->ac_which != OCFS2_AC_USE_MAIN);
1519ccd979bdSMark Fasheh 
1520ccd979bdSMark Fasheh 	if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1521ccd979bdSMark Fasheh 		status = ocfs2_claim_local_alloc_bits(osb,
1522ccd979bdSMark Fasheh 						      handle,
1523ccd979bdSMark Fasheh 						      ac,
1524ccd979bdSMark Fasheh 						      bits_wanted,
1525ccd979bdSMark Fasheh 						      cluster_start,
1526ccd979bdSMark Fasheh 						      num_clusters);
1527ccd979bdSMark Fasheh 		if (!status)
1528ccd979bdSMark Fasheh 			atomic_inc(&osb->alloc_stats.local_data);
1529ccd979bdSMark Fasheh 	} else {
1530ccd979bdSMark Fasheh 		if (min_clusters > (osb->bitmap_cpg - 1)) {
1531ccd979bdSMark Fasheh 			/* The only paths asking for contiguousness
1532ccd979bdSMark Fasheh 			 * should know about this already. */
15332fbe8d1eSSunil Mushran 			mlog(ML_ERROR, "minimum allocation requested %u exceeds "
15342fbe8d1eSSunil Mushran 			     "group bitmap size %u!\n", min_clusters,
15352fbe8d1eSSunil Mushran 			     osb->bitmap_cpg);
1536ccd979bdSMark Fasheh 			status = -ENOSPC;
1537ccd979bdSMark Fasheh 			goto bail;
1538ccd979bdSMark Fasheh 		}
1539ccd979bdSMark Fasheh 		/* clamp the current request down to a realistic size. */
1540ccd979bdSMark Fasheh 		if (bits_wanted > (osb->bitmap_cpg - 1))
1541ccd979bdSMark Fasheh 			bits_wanted = osb->bitmap_cpg - 1;
1542ccd979bdSMark Fasheh 
1543ccd979bdSMark Fasheh 		status = ocfs2_claim_suballoc_bits(osb,
1544ccd979bdSMark Fasheh 						   ac,
1545da5cbf2fSMark Fasheh 						   handle,
1546ccd979bdSMark Fasheh 						   bits_wanted,
1547ccd979bdSMark Fasheh 						   min_clusters,
1548ccd979bdSMark Fasheh 						   &bg_bit_off,
1549ccd979bdSMark Fasheh 						   num_clusters,
1550ccd979bdSMark Fasheh 						   &bg_blkno);
1551ccd979bdSMark Fasheh 		if (!status) {
1552ccd979bdSMark Fasheh 			*cluster_start =
1553ccd979bdSMark Fasheh 				ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1554ccd979bdSMark Fasheh 								 bg_blkno,
1555ccd979bdSMark Fasheh 								 bg_bit_off);
1556ccd979bdSMark Fasheh 			atomic_inc(&osb->alloc_stats.bitmap_data);
1557ccd979bdSMark Fasheh 		}
1558ccd979bdSMark Fasheh 	}
1559ccd979bdSMark Fasheh 	if (status < 0) {
1560ccd979bdSMark Fasheh 		if (status != -ENOSPC)
1561ccd979bdSMark Fasheh 			mlog_errno(status);
1562ccd979bdSMark Fasheh 		goto bail;
1563ccd979bdSMark Fasheh 	}
1564ccd979bdSMark Fasheh 
1565ccd979bdSMark Fasheh 	ac->ac_bits_given += *num_clusters;
1566ccd979bdSMark Fasheh 
1567ccd979bdSMark Fasheh bail:
1568ccd979bdSMark Fasheh 	mlog_exit(status);
1569ccd979bdSMark Fasheh 	return status;
1570ccd979bdSMark Fasheh }
1571ccd979bdSMark Fasheh 
1572415cb800SMark Fasheh int ocfs2_claim_clusters(struct ocfs2_super *osb,
1573415cb800SMark Fasheh 			 handle_t *handle,
1574415cb800SMark Fasheh 			 struct ocfs2_alloc_context *ac,
1575415cb800SMark Fasheh 			 u32 min_clusters,
1576415cb800SMark Fasheh 			 u32 *cluster_start,
1577415cb800SMark Fasheh 			 u32 *num_clusters)
1578415cb800SMark Fasheh {
1579415cb800SMark Fasheh 	unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1580415cb800SMark Fasheh 
1581415cb800SMark Fasheh 	return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1582415cb800SMark Fasheh 				      bits_wanted, cluster_start, num_clusters);
1583415cb800SMark Fasheh }
1584415cb800SMark Fasheh 
15851fabe148SMark Fasheh static inline int ocfs2_block_group_clear_bits(handle_t *handle,
1586ccd979bdSMark Fasheh 					       struct inode *alloc_inode,
1587ccd979bdSMark Fasheh 					       struct ocfs2_group_desc *bg,
1588ccd979bdSMark Fasheh 					       struct buffer_head *group_bh,
1589ccd979bdSMark Fasheh 					       unsigned int bit_off,
1590ccd979bdSMark Fasheh 					       unsigned int num_bits)
1591ccd979bdSMark Fasheh {
1592ccd979bdSMark Fasheh 	int status;
1593ccd979bdSMark Fasheh 	unsigned int tmp;
1594ccd979bdSMark Fasheh 	int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1595ccd979bdSMark Fasheh 	struct ocfs2_group_desc *undo_bg = NULL;
1596ccd979bdSMark Fasheh 
1597ccd979bdSMark Fasheh 	mlog_entry_void();
1598ccd979bdSMark Fasheh 
1599ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
1600ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
1601ccd979bdSMark Fasheh 		status = -EIO;
1602ccd979bdSMark Fasheh 		goto bail;
1603ccd979bdSMark Fasheh 	}
1604ccd979bdSMark Fasheh 
1605ccd979bdSMark Fasheh 	mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1606ccd979bdSMark Fasheh 
1607ccd979bdSMark Fasheh 	if (ocfs2_is_cluster_bitmap(alloc_inode))
1608ccd979bdSMark Fasheh 		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1609ccd979bdSMark Fasheh 
1610ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, group_bh,
1611ccd979bdSMark Fasheh 				      journal_type);
1612ccd979bdSMark Fasheh 	if (status < 0) {
1613ccd979bdSMark Fasheh 		mlog_errno(status);
1614ccd979bdSMark Fasheh 		goto bail;
1615ccd979bdSMark Fasheh 	}
1616ccd979bdSMark Fasheh 
1617ccd979bdSMark Fasheh 	if (ocfs2_is_cluster_bitmap(alloc_inode))
1618ccd979bdSMark Fasheh 		undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
1619ccd979bdSMark Fasheh 
1620ccd979bdSMark Fasheh 	tmp = num_bits;
1621ccd979bdSMark Fasheh 	while(tmp--) {
1622ccd979bdSMark Fasheh 		ocfs2_clear_bit((bit_off + tmp),
1623ccd979bdSMark Fasheh 				(unsigned long *) bg->bg_bitmap);
1624ccd979bdSMark Fasheh 		if (ocfs2_is_cluster_bitmap(alloc_inode))
1625ccd979bdSMark Fasheh 			ocfs2_set_bit(bit_off + tmp,
1626ccd979bdSMark Fasheh 				      (unsigned long *) undo_bg->bg_bitmap);
1627ccd979bdSMark Fasheh 	}
1628ccd979bdSMark Fasheh 	le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1629ccd979bdSMark Fasheh 
1630ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, group_bh);
1631ccd979bdSMark Fasheh 	if (status < 0)
1632ccd979bdSMark Fasheh 		mlog_errno(status);
1633ccd979bdSMark Fasheh bail:
1634ccd979bdSMark Fasheh 	return status;
1635ccd979bdSMark Fasheh }
1636ccd979bdSMark Fasheh 
1637ccd979bdSMark Fasheh /*
1638ccd979bdSMark Fasheh  * expects the suballoc inode to already be locked.
1639ccd979bdSMark Fasheh  */
16402b604351SMark Fasheh int ocfs2_free_suballoc_bits(handle_t *handle,
1641ccd979bdSMark Fasheh 			     struct inode *alloc_inode,
1642ccd979bdSMark Fasheh 			     struct buffer_head *alloc_bh,
1643ccd979bdSMark Fasheh 			     unsigned int start_bit,
1644ccd979bdSMark Fasheh 			     u64 bg_blkno,
1645ccd979bdSMark Fasheh 			     unsigned int count)
1646ccd979bdSMark Fasheh {
1647ccd979bdSMark Fasheh 	int status = 0;
1648ccd979bdSMark Fasheh 	u32 tmp_used;
1649ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
1650ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1651ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1652ccd979bdSMark Fasheh 	struct buffer_head *group_bh = NULL;
1653ccd979bdSMark Fasheh 	struct ocfs2_group_desc *group;
1654ccd979bdSMark Fasheh 
1655ccd979bdSMark Fasheh 	mlog_entry_void();
1656ccd979bdSMark Fasheh 
1657ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
1658ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
1659ccd979bdSMark Fasheh 		status = -EIO;
1660ccd979bdSMark Fasheh 		goto bail;
1661ccd979bdSMark Fasheh 	}
1662ccd979bdSMark Fasheh 	BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1663ccd979bdSMark Fasheh 
1664b0697053SMark Fasheh 	mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1665b0697053SMark Fasheh 	     (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1666b0697053SMark Fasheh 	     (unsigned long long)bg_blkno, start_bit);
1667ccd979bdSMark Fasheh 
1668ccd979bdSMark Fasheh 	status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED,
1669ccd979bdSMark Fasheh 				  alloc_inode);
1670ccd979bdSMark Fasheh 	if (status < 0) {
1671ccd979bdSMark Fasheh 		mlog_errno(status);
1672ccd979bdSMark Fasheh 		goto bail;
1673ccd979bdSMark Fasheh 	}
1674ccd979bdSMark Fasheh 
1675ccd979bdSMark Fasheh 	group = (struct ocfs2_group_desc *) group_bh->b_data;
16767bf72edeSMark Fasheh 	status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, group);
16777bf72edeSMark Fasheh 	if (status) {
16787bf72edeSMark Fasheh 		mlog_errno(status);
1679ccd979bdSMark Fasheh 		goto bail;
1680ccd979bdSMark Fasheh 	}
1681ccd979bdSMark Fasheh 	BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1682ccd979bdSMark Fasheh 
1683ccd979bdSMark Fasheh 	status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1684ccd979bdSMark Fasheh 					      group, group_bh,
1685ccd979bdSMark Fasheh 					      start_bit, count);
1686ccd979bdSMark Fasheh 	if (status < 0) {
1687ccd979bdSMark Fasheh 		mlog_errno(status);
1688ccd979bdSMark Fasheh 		goto bail;
1689ccd979bdSMark Fasheh 	}
1690ccd979bdSMark Fasheh 
1691ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
1692ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
1693ccd979bdSMark Fasheh 	if (status < 0) {
1694ccd979bdSMark Fasheh 		mlog_errno(status);
1695ccd979bdSMark Fasheh 		goto bail;
1696ccd979bdSMark Fasheh 	}
1697ccd979bdSMark Fasheh 
1698ccd979bdSMark Fasheh 	le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
1699ccd979bdSMark Fasheh 		     count);
1700ccd979bdSMark Fasheh 	tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1701ccd979bdSMark Fasheh 	fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
1702ccd979bdSMark Fasheh 
1703ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, alloc_bh);
1704ccd979bdSMark Fasheh 	if (status < 0) {
1705ccd979bdSMark Fasheh 		mlog_errno(status);
1706ccd979bdSMark Fasheh 		goto bail;
1707ccd979bdSMark Fasheh 	}
1708ccd979bdSMark Fasheh 
1709ccd979bdSMark Fasheh bail:
1710ccd979bdSMark Fasheh 	if (group_bh)
1711ccd979bdSMark Fasheh 		brelse(group_bh);
1712ccd979bdSMark Fasheh 
1713ccd979bdSMark Fasheh 	mlog_exit(status);
1714ccd979bdSMark Fasheh 	return status;
1715ccd979bdSMark Fasheh }
1716ccd979bdSMark Fasheh 
17171fabe148SMark Fasheh int ocfs2_free_dinode(handle_t *handle,
1718ccd979bdSMark Fasheh 		      struct inode *inode_alloc_inode,
1719ccd979bdSMark Fasheh 		      struct buffer_head *inode_alloc_bh,
1720ccd979bdSMark Fasheh 		      struct ocfs2_dinode *di)
1721ccd979bdSMark Fasheh {
1722ccd979bdSMark Fasheh 	u64 blk = le64_to_cpu(di->i_blkno);
1723ccd979bdSMark Fasheh 	u16 bit = le16_to_cpu(di->i_suballoc_bit);
1724ccd979bdSMark Fasheh 	u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1725ccd979bdSMark Fasheh 
1726ccd979bdSMark Fasheh 	return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
1727ccd979bdSMark Fasheh 					inode_alloc_bh, bit, bg_blkno, 1);
1728ccd979bdSMark Fasheh }
1729ccd979bdSMark Fasheh 
17301fabe148SMark Fasheh int ocfs2_free_clusters(handle_t *handle,
1731ccd979bdSMark Fasheh 		       struct inode *bitmap_inode,
1732ccd979bdSMark Fasheh 		       struct buffer_head *bitmap_bh,
1733ccd979bdSMark Fasheh 		       u64 start_blk,
1734ccd979bdSMark Fasheh 		       unsigned int num_clusters)
1735ccd979bdSMark Fasheh {
1736ccd979bdSMark Fasheh 	int status;
1737ccd979bdSMark Fasheh 	u16 bg_start_bit;
1738ccd979bdSMark Fasheh 	u64 bg_blkno;
1739ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
1740ccd979bdSMark Fasheh 
1741ccd979bdSMark Fasheh 	/* You can't ever have a contiguous set of clusters
1742ccd979bdSMark Fasheh 	 * bigger than a block group bitmap so we never have to worry
1743ccd979bdSMark Fasheh 	 * about looping on them. */
1744ccd979bdSMark Fasheh 
1745ccd979bdSMark Fasheh 	mlog_entry_void();
1746ccd979bdSMark Fasheh 
1747ccd979bdSMark Fasheh 	/* This is expensive. We can safely remove once this stuff has
1748ccd979bdSMark Fasheh 	 * gotten tested really well. */
1749ccd979bdSMark Fasheh 	BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
1750ccd979bdSMark Fasheh 
1751ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
1752ccd979bdSMark Fasheh 
1753ccd979bdSMark Fasheh 	ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
1754ccd979bdSMark Fasheh 				     &bg_start_bit);
1755ccd979bdSMark Fasheh 
1756b0697053SMark Fasheh 	mlog(0, "want to free %u clusters starting at block %llu\n",
1757b0697053SMark Fasheh 	     num_clusters, (unsigned long long)start_blk);
1758b0697053SMark Fasheh 	mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1759b0697053SMark Fasheh 	     (unsigned long long)bg_blkno, bg_start_bit);
1760ccd979bdSMark Fasheh 
1761ccd979bdSMark Fasheh 	status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
1762ccd979bdSMark Fasheh 					  bg_start_bit, bg_blkno,
1763ccd979bdSMark Fasheh 					  num_clusters);
1764ccd979bdSMark Fasheh 	if (status < 0)
1765ccd979bdSMark Fasheh 		mlog_errno(status);
1766ccd979bdSMark Fasheh 
1767ccd979bdSMark Fasheh 	mlog_exit(status);
1768ccd979bdSMark Fasheh 	return status;
1769ccd979bdSMark Fasheh }
1770ccd979bdSMark Fasheh 
1771ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
1772ccd979bdSMark Fasheh {
1773ccd979bdSMark Fasheh 	printk("Block Group:\n");
1774ccd979bdSMark Fasheh 	printk("bg_signature:       %s\n", bg->bg_signature);
1775ccd979bdSMark Fasheh 	printk("bg_size:            %u\n", bg->bg_size);
1776ccd979bdSMark Fasheh 	printk("bg_bits:            %u\n", bg->bg_bits);
1777ccd979bdSMark Fasheh 	printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
1778ccd979bdSMark Fasheh 	printk("bg_chain:           %u\n", bg->bg_chain);
1779ccd979bdSMark Fasheh 	printk("bg_generation:      %u\n", le32_to_cpu(bg->bg_generation));
1780b0697053SMark Fasheh 	printk("bg_next_group:      %llu\n",
1781b0697053SMark Fasheh 	       (unsigned long long)bg->bg_next_group);
1782b0697053SMark Fasheh 	printk("bg_parent_dinode:   %llu\n",
1783b0697053SMark Fasheh 	       (unsigned long long)bg->bg_parent_dinode);
1784b0697053SMark Fasheh 	printk("bg_blkno:           %llu\n",
1785b0697053SMark Fasheh 	       (unsigned long long)bg->bg_blkno);
1786ccd979bdSMark Fasheh }
1787ccd979bdSMark Fasheh 
1788ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
1789ccd979bdSMark Fasheh {
1790ccd979bdSMark Fasheh 	int i;
1791ccd979bdSMark Fasheh 
1792b0697053SMark Fasheh 	printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
1793ccd979bdSMark Fasheh 	printk("i_signature:                  %s\n", fe->i_signature);
1794b0697053SMark Fasheh 	printk("i_size:                       %llu\n",
1795b0697053SMark Fasheh 	       (unsigned long long)fe->i_size);
1796ccd979bdSMark Fasheh 	printk("i_clusters:                   %u\n", fe->i_clusters);
1797ccd979bdSMark Fasheh 	printk("i_generation:                 %u\n",
1798ccd979bdSMark Fasheh 	       le32_to_cpu(fe->i_generation));
1799ccd979bdSMark Fasheh 	printk("id1.bitmap1.i_used:           %u\n",
1800ccd979bdSMark Fasheh 	       le32_to_cpu(fe->id1.bitmap1.i_used));
1801ccd979bdSMark Fasheh 	printk("id1.bitmap1.i_total:          %u\n",
1802ccd979bdSMark Fasheh 	       le32_to_cpu(fe->id1.bitmap1.i_total));
1803ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_cpg:           %u\n", fe->id2.i_chain.cl_cpg);
1804ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_bpc:           %u\n", fe->id2.i_chain.cl_bpc);
1805ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_count:         %u\n", fe->id2.i_chain.cl_count);
1806ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_next_free_rec: %u\n",
1807ccd979bdSMark Fasheh 	       fe->id2.i_chain.cl_next_free_rec);
1808ccd979bdSMark Fasheh 	for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
1809ccd979bdSMark Fasheh 		printk("fe->id2.i_chain.cl_recs[%d].c_free:  %u\n", i,
1810ccd979bdSMark Fasheh 		       fe->id2.i_chain.cl_recs[i].c_free);
1811ccd979bdSMark Fasheh 		printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
1812ccd979bdSMark Fasheh 		       fe->id2.i_chain.cl_recs[i].c_total);
1813b0697053SMark Fasheh 		printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
1814b0697053SMark Fasheh 		       (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
1815ccd979bdSMark Fasheh 	}
1816ccd979bdSMark Fasheh }
1817