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