xref: /openbmc/linux/fs/ocfs2/suballoc.c (revision a4a4891164d4f6f383cc17e7c90828a7ca6a1146)
1ccd979bdSMark Fasheh /* -*- mode: c; c-basic-offset: 8; -*-
2ccd979bdSMark Fasheh  * vim: noexpandtab sw=8 ts=8 sts=0:
3ccd979bdSMark Fasheh  *
4ccd979bdSMark Fasheh  * suballoc.c
5ccd979bdSMark Fasheh  *
6ccd979bdSMark Fasheh  * metadata alloc and free
7ccd979bdSMark Fasheh  * Inspired by ext3 block groups.
8ccd979bdSMark Fasheh  *
9ccd979bdSMark Fasheh  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
10ccd979bdSMark Fasheh  *
11ccd979bdSMark Fasheh  * This program is free software; you can redistribute it and/or
12ccd979bdSMark Fasheh  * modify it under the terms of the GNU General Public
13ccd979bdSMark Fasheh  * License as published by the Free Software Foundation; either
14ccd979bdSMark Fasheh  * version 2 of the License, or (at your option) any later version.
15ccd979bdSMark Fasheh  *
16ccd979bdSMark Fasheh  * This program is distributed in the hope that it will be useful,
17ccd979bdSMark Fasheh  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18ccd979bdSMark Fasheh  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19ccd979bdSMark Fasheh  * General Public License for more details.
20ccd979bdSMark Fasheh  *
21ccd979bdSMark Fasheh  * You should have received a copy of the GNU General Public
22ccd979bdSMark Fasheh  * License along with this program; if not, write to the
23ccd979bdSMark Fasheh  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24ccd979bdSMark Fasheh  * Boston, MA 021110-1307, USA.
25ccd979bdSMark Fasheh  */
26ccd979bdSMark Fasheh 
27ccd979bdSMark Fasheh #include <linux/fs.h>
28ccd979bdSMark Fasheh #include <linux/types.h>
29ccd979bdSMark Fasheh #include <linux/slab.h>
30ccd979bdSMark Fasheh #include <linux/highmem.h>
31ccd979bdSMark Fasheh 
32ccd979bdSMark Fasheh #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33ccd979bdSMark Fasheh #include <cluster/masklog.h>
34ccd979bdSMark Fasheh 
35ccd979bdSMark Fasheh #include "ocfs2.h"
36ccd979bdSMark Fasheh 
37ccd979bdSMark Fasheh #include "alloc.h"
38ccd979bdSMark Fasheh #include "dlmglue.h"
39ccd979bdSMark Fasheh #include "inode.h"
40ccd979bdSMark Fasheh #include "journal.h"
41ccd979bdSMark Fasheh #include "localalloc.h"
42ccd979bdSMark Fasheh #include "suballoc.h"
43ccd979bdSMark Fasheh #include "super.h"
44ccd979bdSMark Fasheh #include "sysfile.h"
45ccd979bdSMark Fasheh #include "uptodate.h"
46ccd979bdSMark Fasheh 
47ccd979bdSMark Fasheh #include "buffer_head_io.h"
48ccd979bdSMark Fasheh 
49ffda89a3STao Ma #define NOT_ALLOC_NEW_GROUP		0
50ffda89a3STao Ma #define ALLOC_NEW_GROUP			1
51ffda89a3STao Ma 
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,
397ffda89a3STao Ma 				       u32 slot,
398ffda89a3STao 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;
427*a4a48911STao Ma 	ac->ac_alloc_slot = slot;
428da5cbf2fSMark Fasheh 
429ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) bh->b_data;
430ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
431ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
432ccd979bdSMark Fasheh 		status = -EIO;
433ccd979bdSMark Fasheh 		goto bail;
434ccd979bdSMark Fasheh 	}
435ccd979bdSMark Fasheh 	if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
436b0697053SMark Fasheh 		ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
437b0697053SMark Fasheh 			    (unsigned long long)le64_to_cpu(fe->i_blkno));
438ccd979bdSMark Fasheh 		status = -EIO;
439ccd979bdSMark Fasheh 		goto bail;
440ccd979bdSMark Fasheh 	}
441ccd979bdSMark Fasheh 
442ccd979bdSMark Fasheh 	free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
443ccd979bdSMark Fasheh 		le32_to_cpu(fe->id1.bitmap1.i_used);
444ccd979bdSMark Fasheh 
445ccd979bdSMark Fasheh 	if (bits_wanted > free_bits) {
446ccd979bdSMark Fasheh 		/* cluster bitmap never grows */
447ccd979bdSMark Fasheh 		if (ocfs2_is_cluster_bitmap(alloc_inode)) {
448ccd979bdSMark Fasheh 			mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
449ccd979bdSMark Fasheh 			     bits_wanted, free_bits);
450ccd979bdSMark Fasheh 			status = -ENOSPC;
451ccd979bdSMark Fasheh 			goto bail;
452ccd979bdSMark Fasheh 		}
453ccd979bdSMark Fasheh 
454ffda89a3STao Ma 		if (alloc_new_group != ALLOC_NEW_GROUP) {
455ffda89a3STao Ma 			mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
456ffda89a3STao Ma 			     "and we don't alloc a new group for it.\n",
457ffda89a3STao Ma 			     slot, bits_wanted, free_bits);
458ffda89a3STao Ma 			status = -ENOSPC;
459ffda89a3STao Ma 			goto bail;
460ffda89a3STao Ma 		}
461ffda89a3STao Ma 
462ccd979bdSMark Fasheh 		status = ocfs2_block_group_alloc(osb, alloc_inode, bh);
463ccd979bdSMark Fasheh 		if (status < 0) {
464ccd979bdSMark Fasheh 			if (status != -ENOSPC)
465ccd979bdSMark Fasheh 				mlog_errno(status);
466ccd979bdSMark Fasheh 			goto bail;
467ccd979bdSMark Fasheh 		}
468ccd979bdSMark Fasheh 		atomic_inc(&osb->alloc_stats.bg_extends);
469ccd979bdSMark Fasheh 
470ccd979bdSMark Fasheh 		/* You should never ask for this much metadata */
471ccd979bdSMark Fasheh 		BUG_ON(bits_wanted >
472ccd979bdSMark Fasheh 		       (le32_to_cpu(fe->id1.bitmap1.i_total)
473ccd979bdSMark Fasheh 			- le32_to_cpu(fe->id1.bitmap1.i_used)));
474ccd979bdSMark Fasheh 	}
475ccd979bdSMark Fasheh 
476ccd979bdSMark Fasheh 	get_bh(bh);
477ccd979bdSMark Fasheh 	ac->ac_bh = bh;
478ccd979bdSMark Fasheh bail:
479ccd979bdSMark Fasheh 	if (bh)
480ccd979bdSMark Fasheh 		brelse(bh);
481ccd979bdSMark Fasheh 
482ccd979bdSMark Fasheh 	mlog_exit(status);
483ccd979bdSMark Fasheh 	return status;
484ccd979bdSMark Fasheh }
485ccd979bdSMark Fasheh 
486ccd979bdSMark Fasheh int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
487ccd979bdSMark Fasheh 			       struct ocfs2_dinode *fe,
488ccd979bdSMark Fasheh 			       struct ocfs2_alloc_context **ac)
489ccd979bdSMark Fasheh {
490ccd979bdSMark Fasheh 	int status;
491da5cbf2fSMark Fasheh 	u32 slot;
492ccd979bdSMark Fasheh 
493cd861280SRobert P. J. Day 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
494ccd979bdSMark Fasheh 	if (!(*ac)) {
495ccd979bdSMark Fasheh 		status = -ENOMEM;
496ccd979bdSMark Fasheh 		mlog_errno(status);
497ccd979bdSMark Fasheh 		goto bail;
498ccd979bdSMark Fasheh 	}
499ccd979bdSMark Fasheh 
500ccd979bdSMark Fasheh 	(*ac)->ac_bits_wanted = ocfs2_extend_meta_needed(fe);
501ccd979bdSMark Fasheh 	(*ac)->ac_which = OCFS2_AC_USE_META;
502da5cbf2fSMark Fasheh 	slot = osb->slot_num;
503ccd979bdSMark Fasheh 	(*ac)->ac_group_search = ocfs2_block_group_search;
504ccd979bdSMark Fasheh 
505da5cbf2fSMark Fasheh 	status = ocfs2_reserve_suballoc_bits(osb, (*ac),
506ffda89a3STao Ma 					     EXTENT_ALLOC_SYSTEM_INODE,
507ffda89a3STao Ma 					     slot, ALLOC_NEW_GROUP);
508ccd979bdSMark Fasheh 	if (status < 0) {
509ccd979bdSMark Fasheh 		if (status != -ENOSPC)
510ccd979bdSMark Fasheh 			mlog_errno(status);
511ccd979bdSMark Fasheh 		goto bail;
512ccd979bdSMark Fasheh 	}
513ccd979bdSMark Fasheh 
514ccd979bdSMark Fasheh 	status = 0;
515ccd979bdSMark Fasheh bail:
516ccd979bdSMark Fasheh 	if ((status < 0) && *ac) {
517ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(*ac);
518ccd979bdSMark Fasheh 		*ac = NULL;
519ccd979bdSMark Fasheh 	}
520ccd979bdSMark Fasheh 
521ccd979bdSMark Fasheh 	mlog_exit(status);
522ccd979bdSMark Fasheh 	return status;
523ccd979bdSMark Fasheh }
524ccd979bdSMark Fasheh 
525ccd979bdSMark Fasheh int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
526ccd979bdSMark Fasheh 			    struct ocfs2_alloc_context **ac)
527ccd979bdSMark Fasheh {
528ccd979bdSMark Fasheh 	int status;
529ccd979bdSMark Fasheh 
530cd861280SRobert P. J. Day 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
531ccd979bdSMark Fasheh 	if (!(*ac)) {
532ccd979bdSMark Fasheh 		status = -ENOMEM;
533ccd979bdSMark Fasheh 		mlog_errno(status);
534ccd979bdSMark Fasheh 		goto bail;
535ccd979bdSMark Fasheh 	}
536ccd979bdSMark Fasheh 
537ccd979bdSMark Fasheh 	(*ac)->ac_bits_wanted = 1;
538ccd979bdSMark Fasheh 	(*ac)->ac_which = OCFS2_AC_USE_INODE;
539ccd979bdSMark Fasheh 
540ccd979bdSMark Fasheh 	(*ac)->ac_group_search = ocfs2_block_group_search;
541ccd979bdSMark Fasheh 
542da5cbf2fSMark Fasheh 	status = ocfs2_reserve_suballoc_bits(osb, *ac,
543da5cbf2fSMark Fasheh 					     INODE_ALLOC_SYSTEM_INODE,
544ffda89a3STao Ma 					     osb->slot_num, ALLOC_NEW_GROUP);
545ccd979bdSMark Fasheh 	if (status < 0) {
546ccd979bdSMark Fasheh 		if (status != -ENOSPC)
547ccd979bdSMark Fasheh 			mlog_errno(status);
548ccd979bdSMark Fasheh 		goto bail;
549ccd979bdSMark Fasheh 	}
550ccd979bdSMark Fasheh 
551ccd979bdSMark Fasheh 	status = 0;
552ccd979bdSMark Fasheh bail:
553ccd979bdSMark Fasheh 	if ((status < 0) && *ac) {
554ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(*ac);
555ccd979bdSMark Fasheh 		*ac = NULL;
556ccd979bdSMark Fasheh 	}
557ccd979bdSMark Fasheh 
558ccd979bdSMark Fasheh 	mlog_exit(status);
559ccd979bdSMark Fasheh 	return status;
560ccd979bdSMark Fasheh }
561ccd979bdSMark Fasheh 
562ccd979bdSMark Fasheh /* local alloc code has to do the same thing, so rather than do this
563ccd979bdSMark Fasheh  * twice.. */
564ccd979bdSMark Fasheh int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
565ccd979bdSMark Fasheh 				      struct ocfs2_alloc_context *ac)
566ccd979bdSMark Fasheh {
567ccd979bdSMark Fasheh 	int status;
568ccd979bdSMark Fasheh 
569ccd979bdSMark Fasheh 	ac->ac_which = OCFS2_AC_USE_MAIN;
570ccd979bdSMark Fasheh 	ac->ac_group_search = ocfs2_cluster_group_search;
571ccd979bdSMark Fasheh 
572da5cbf2fSMark Fasheh 	status = ocfs2_reserve_suballoc_bits(osb, ac,
573da5cbf2fSMark Fasheh 					     GLOBAL_BITMAP_SYSTEM_INODE,
574ffda89a3STao Ma 					     OCFS2_INVALID_SLOT,
575ffda89a3STao Ma 					     ALLOC_NEW_GROUP);
576da5cbf2fSMark Fasheh 	if (status < 0 && status != -ENOSPC) {
577ccd979bdSMark Fasheh 		mlog_errno(status);
578da5cbf2fSMark Fasheh 		goto bail;
579da5cbf2fSMark Fasheh 	}
580da5cbf2fSMark Fasheh 
581ccd979bdSMark Fasheh bail:
582ccd979bdSMark Fasheh 	return status;
583ccd979bdSMark Fasheh }
584ccd979bdSMark Fasheh 
585ccd979bdSMark Fasheh /* Callers don't need to care which bitmap (local alloc or main) to
586ccd979bdSMark Fasheh  * use so we figure it out for them, but unfortunately this clutters
587ccd979bdSMark Fasheh  * things a bit. */
588ccd979bdSMark Fasheh int ocfs2_reserve_clusters(struct ocfs2_super *osb,
589ccd979bdSMark Fasheh 			   u32 bits_wanted,
590ccd979bdSMark Fasheh 			   struct ocfs2_alloc_context **ac)
591ccd979bdSMark Fasheh {
592ccd979bdSMark Fasheh 	int status;
593ccd979bdSMark Fasheh 
594ccd979bdSMark Fasheh 	mlog_entry_void();
595ccd979bdSMark Fasheh 
596cd861280SRobert P. J. Day 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
597ccd979bdSMark Fasheh 	if (!(*ac)) {
598ccd979bdSMark Fasheh 		status = -ENOMEM;
599ccd979bdSMark Fasheh 		mlog_errno(status);
600ccd979bdSMark Fasheh 		goto bail;
601ccd979bdSMark Fasheh 	}
602ccd979bdSMark Fasheh 
603ccd979bdSMark Fasheh 	(*ac)->ac_bits_wanted = bits_wanted;
604ccd979bdSMark Fasheh 
605ccd979bdSMark Fasheh 	status = -ENOSPC;
606ccd979bdSMark Fasheh 	if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
607ccd979bdSMark Fasheh 		status = ocfs2_reserve_local_alloc_bits(osb,
608ccd979bdSMark Fasheh 							bits_wanted,
609ccd979bdSMark Fasheh 							*ac);
610ccd979bdSMark Fasheh 		if ((status < 0) && (status != -ENOSPC)) {
611ccd979bdSMark Fasheh 			mlog_errno(status);
612ccd979bdSMark Fasheh 			goto bail;
613ccd979bdSMark Fasheh 		} else if (status == -ENOSPC) {
614ccd979bdSMark Fasheh 			/* reserve_local_bits will return enospc with
615ccd979bdSMark Fasheh 			 * the local alloc inode still locked, so we
616ccd979bdSMark Fasheh 			 * can change this safely here. */
617ccd979bdSMark Fasheh 			mlog(0, "Disabling local alloc\n");
618ccd979bdSMark Fasheh 			/* We set to OCFS2_LA_DISABLED so that umount
619ccd979bdSMark Fasheh 			 * can clean up what's left of the local
620ccd979bdSMark Fasheh 			 * allocation */
621ccd979bdSMark Fasheh 			osb->local_alloc_state = OCFS2_LA_DISABLED;
622ccd979bdSMark Fasheh 		}
623ccd979bdSMark Fasheh 	}
624ccd979bdSMark Fasheh 
625ccd979bdSMark Fasheh 	if (status == -ENOSPC) {
626ccd979bdSMark Fasheh 		status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
627ccd979bdSMark Fasheh 		if (status < 0) {
628ccd979bdSMark Fasheh 			if (status != -ENOSPC)
629ccd979bdSMark Fasheh 				mlog_errno(status);
630ccd979bdSMark Fasheh 			goto bail;
631ccd979bdSMark Fasheh 		}
632ccd979bdSMark Fasheh 	}
633ccd979bdSMark Fasheh 
634ccd979bdSMark Fasheh 	status = 0;
635ccd979bdSMark Fasheh bail:
636ccd979bdSMark Fasheh 	if ((status < 0) && *ac) {
637ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(*ac);
638ccd979bdSMark Fasheh 		*ac = NULL;
639ccd979bdSMark Fasheh 	}
640ccd979bdSMark Fasheh 
641ccd979bdSMark Fasheh 	mlog_exit(status);
642ccd979bdSMark Fasheh 	return status;
643ccd979bdSMark Fasheh }
644ccd979bdSMark Fasheh 
645ccd979bdSMark Fasheh /*
646ccd979bdSMark Fasheh  * More or less lifted from ext3. I'll leave their description below:
647ccd979bdSMark Fasheh  *
648ccd979bdSMark Fasheh  * "For ext3 allocations, we must not reuse any blocks which are
649ccd979bdSMark Fasheh  * allocated in the bitmap buffer's "last committed data" copy.  This
650ccd979bdSMark Fasheh  * prevents deletes from freeing up the page for reuse until we have
651ccd979bdSMark Fasheh  * committed the delete transaction.
652ccd979bdSMark Fasheh  *
653ccd979bdSMark Fasheh  * If we didn't do this, then deleting something and reallocating it as
654ccd979bdSMark Fasheh  * data would allow the old block to be overwritten before the
655ccd979bdSMark Fasheh  * transaction committed (because we force data to disk before commit).
656ccd979bdSMark Fasheh  * This would lead to corruption if we crashed between overwriting the
657ccd979bdSMark Fasheh  * data and committing the delete.
658ccd979bdSMark Fasheh  *
659ccd979bdSMark Fasheh  * @@@ We may want to make this allocation behaviour conditional on
660ccd979bdSMark Fasheh  * data-writes at some point, and disable it for metadata allocations or
661ccd979bdSMark Fasheh  * sync-data inodes."
662ccd979bdSMark Fasheh  *
663ccd979bdSMark Fasheh  * Note: OCFS2 already does this differently for metadata vs data
664c78bad11SJoe Perches  * allocations, as those bitmaps are separate and undo access is never
665ccd979bdSMark Fasheh  * called on a metadata group descriptor.
666ccd979bdSMark Fasheh  */
667ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
668ccd979bdSMark Fasheh 					 int nr)
669ccd979bdSMark Fasheh {
670ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
671ccd979bdSMark Fasheh 
672ccd979bdSMark Fasheh 	if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
673ccd979bdSMark Fasheh 		return 0;
674ccd979bdSMark Fasheh 	if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
675ccd979bdSMark Fasheh 		return 1;
676ccd979bdSMark Fasheh 
677ccd979bdSMark Fasheh 	bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
678ccd979bdSMark Fasheh 	return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
679ccd979bdSMark Fasheh }
680ccd979bdSMark Fasheh 
681ccd979bdSMark Fasheh static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
682ccd979bdSMark Fasheh 					     struct buffer_head *bg_bh,
683ccd979bdSMark Fasheh 					     unsigned int bits_wanted,
6847bf72edeSMark Fasheh 					     unsigned int total_bits,
685ccd979bdSMark Fasheh 					     u16 *bit_off,
686ccd979bdSMark Fasheh 					     u16 *bits_found)
687ccd979bdSMark Fasheh {
688ccd979bdSMark Fasheh 	void *bitmap;
689ccd979bdSMark Fasheh 	u16 best_offset, best_size;
690ccd979bdSMark Fasheh 	int offset, start, found, status = 0;
691ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
692ccd979bdSMark Fasheh 
693ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
694ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg);
695ccd979bdSMark Fasheh 		return -EIO;
696ccd979bdSMark Fasheh 	}
697ccd979bdSMark Fasheh 
698ccd979bdSMark Fasheh 	found = start = best_offset = best_size = 0;
699ccd979bdSMark Fasheh 	bitmap = bg->bg_bitmap;
700ccd979bdSMark Fasheh 
7017bf72edeSMark Fasheh 	while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
7027bf72edeSMark Fasheh 		if (offset == total_bits)
703ccd979bdSMark Fasheh 			break;
704ccd979bdSMark Fasheh 
705ccd979bdSMark Fasheh 		if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
706ccd979bdSMark Fasheh 			/* We found a zero, but we can't use it as it
707ccd979bdSMark Fasheh 			 * hasn't been put to disk yet! */
708ccd979bdSMark Fasheh 			found = 0;
709ccd979bdSMark Fasheh 			start = offset + 1;
710ccd979bdSMark Fasheh 		} else if (offset == start) {
711ccd979bdSMark Fasheh 			/* we found a zero */
712ccd979bdSMark Fasheh 			found++;
713ccd979bdSMark Fasheh 			/* move start to the next bit to test */
714ccd979bdSMark Fasheh 			start++;
715ccd979bdSMark Fasheh 		} else {
716ccd979bdSMark Fasheh 			/* got a zero after some ones */
717ccd979bdSMark Fasheh 			found = 1;
718ccd979bdSMark Fasheh 			start = offset + 1;
719ccd979bdSMark Fasheh 		}
720ccd979bdSMark Fasheh 		if (found > best_size) {
721ccd979bdSMark Fasheh 			best_size = found;
722ccd979bdSMark Fasheh 			best_offset = start - found;
723ccd979bdSMark Fasheh 		}
724ccd979bdSMark Fasheh 		/* we got everything we needed */
725ccd979bdSMark Fasheh 		if (found == bits_wanted) {
726ccd979bdSMark Fasheh 			/* mlog(0, "Found it all!\n"); */
727ccd979bdSMark Fasheh 			break;
728ccd979bdSMark Fasheh 		}
729ccd979bdSMark Fasheh 	}
730ccd979bdSMark Fasheh 
731ccd979bdSMark Fasheh 	/* XXX: I think the first clause is equivalent to the second
732ccd979bdSMark Fasheh 	 * 	- jlbec */
733ccd979bdSMark Fasheh 	if (found == bits_wanted) {
734ccd979bdSMark Fasheh 		*bit_off = start - found;
735ccd979bdSMark Fasheh 		*bits_found = found;
736ccd979bdSMark Fasheh 	} else if (best_size) {
737ccd979bdSMark Fasheh 		*bit_off = best_offset;
738ccd979bdSMark Fasheh 		*bits_found = best_size;
739ccd979bdSMark Fasheh 	} else {
740ccd979bdSMark Fasheh 		status = -ENOSPC;
741ccd979bdSMark Fasheh 		/* No error log here -- see the comment above
742ccd979bdSMark Fasheh 		 * ocfs2_test_bg_bit_allocatable */
743ccd979bdSMark Fasheh 	}
744ccd979bdSMark Fasheh 
745ccd979bdSMark Fasheh 	return status;
746ccd979bdSMark Fasheh }
747ccd979bdSMark Fasheh 
7481fabe148SMark Fasheh static inline int ocfs2_block_group_set_bits(handle_t *handle,
749ccd979bdSMark Fasheh 					     struct inode *alloc_inode,
750ccd979bdSMark Fasheh 					     struct ocfs2_group_desc *bg,
751ccd979bdSMark Fasheh 					     struct buffer_head *group_bh,
752ccd979bdSMark Fasheh 					     unsigned int bit_off,
753ccd979bdSMark Fasheh 					     unsigned int num_bits)
754ccd979bdSMark Fasheh {
755ccd979bdSMark Fasheh 	int status;
756ccd979bdSMark Fasheh 	void *bitmap = bg->bg_bitmap;
757ccd979bdSMark Fasheh 	int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
758ccd979bdSMark Fasheh 
759ccd979bdSMark Fasheh 	mlog_entry_void();
760ccd979bdSMark Fasheh 
761ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
762ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
763ccd979bdSMark Fasheh 		status = -EIO;
764ccd979bdSMark Fasheh 		goto bail;
765ccd979bdSMark Fasheh 	}
766ccd979bdSMark Fasheh 	BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
767ccd979bdSMark Fasheh 
768ccd979bdSMark Fasheh 	mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
769ccd979bdSMark Fasheh 	     num_bits);
770ccd979bdSMark Fasheh 
771ccd979bdSMark Fasheh 	if (ocfs2_is_cluster_bitmap(alloc_inode))
772ccd979bdSMark Fasheh 		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
773ccd979bdSMark Fasheh 
774ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle,
775ccd979bdSMark Fasheh 				      alloc_inode,
776ccd979bdSMark Fasheh 				      group_bh,
777ccd979bdSMark Fasheh 				      journal_type);
778ccd979bdSMark Fasheh 	if (status < 0) {
779ccd979bdSMark Fasheh 		mlog_errno(status);
780ccd979bdSMark Fasheh 		goto bail;
781ccd979bdSMark Fasheh 	}
782ccd979bdSMark Fasheh 
783ccd979bdSMark Fasheh 	le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
784ccd979bdSMark Fasheh 
785ccd979bdSMark Fasheh 	while(num_bits--)
786ccd979bdSMark Fasheh 		ocfs2_set_bit(bit_off++, bitmap);
787ccd979bdSMark Fasheh 
788ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle,
789ccd979bdSMark Fasheh 				     group_bh);
790ccd979bdSMark Fasheh 	if (status < 0) {
791ccd979bdSMark Fasheh 		mlog_errno(status);
792ccd979bdSMark Fasheh 		goto bail;
793ccd979bdSMark Fasheh 	}
794ccd979bdSMark Fasheh 
795ccd979bdSMark Fasheh bail:
796ccd979bdSMark Fasheh 	mlog_exit(status);
797ccd979bdSMark Fasheh 	return status;
798ccd979bdSMark Fasheh }
799ccd979bdSMark Fasheh 
800ccd979bdSMark Fasheh /* find the one with the most empty bits */
801ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
802ccd979bdSMark Fasheh {
803ccd979bdSMark Fasheh 	u16 curr, best;
804ccd979bdSMark Fasheh 
805ccd979bdSMark Fasheh 	BUG_ON(!cl->cl_next_free_rec);
806ccd979bdSMark Fasheh 
807ccd979bdSMark Fasheh 	best = curr = 0;
808ccd979bdSMark Fasheh 	while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
809ccd979bdSMark Fasheh 		if (le32_to_cpu(cl->cl_recs[curr].c_free) >
810ccd979bdSMark Fasheh 		    le32_to_cpu(cl->cl_recs[best].c_free))
811ccd979bdSMark Fasheh 			best = curr;
812ccd979bdSMark Fasheh 		curr++;
813ccd979bdSMark Fasheh 	}
814ccd979bdSMark Fasheh 
815ccd979bdSMark Fasheh 	BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
816ccd979bdSMark Fasheh 	return best;
817ccd979bdSMark Fasheh }
818ccd979bdSMark Fasheh 
8191fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle,
820ccd979bdSMark Fasheh 				    struct inode *alloc_inode,
821ccd979bdSMark Fasheh 				    struct buffer_head *fe_bh,
822ccd979bdSMark Fasheh 				    struct buffer_head *bg_bh,
823ccd979bdSMark Fasheh 				    struct buffer_head *prev_bg_bh,
824ccd979bdSMark Fasheh 				    u16 chain)
825ccd979bdSMark Fasheh {
826ccd979bdSMark Fasheh 	int status;
827ccd979bdSMark Fasheh 	/* there is a really tiny chance the journal calls could fail,
828ccd979bdSMark Fasheh 	 * but we wouldn't want inconsistent blocks in *any* case. */
829ccd979bdSMark Fasheh 	u64 fe_ptr, bg_ptr, prev_bg_ptr;
830ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
831ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
832ccd979bdSMark Fasheh 	struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
833ccd979bdSMark Fasheh 
834ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
835ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
836ccd979bdSMark Fasheh 		status = -EIO;
837ccd979bdSMark Fasheh 		goto out;
838ccd979bdSMark Fasheh 	}
839ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
840ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
841ccd979bdSMark Fasheh 		status = -EIO;
842ccd979bdSMark Fasheh 		goto out;
843ccd979bdSMark Fasheh 	}
844ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) {
845ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg);
846ccd979bdSMark Fasheh 		status = -EIO;
847ccd979bdSMark Fasheh 		goto out;
848ccd979bdSMark Fasheh 	}
849ccd979bdSMark Fasheh 
850b0697053SMark Fasheh 	mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
8511ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
8521ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(bg->bg_blkno),
8531ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
854ccd979bdSMark Fasheh 
855ccd979bdSMark Fasheh 	fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
856ccd979bdSMark Fasheh 	bg_ptr = le64_to_cpu(bg->bg_next_group);
857ccd979bdSMark Fasheh 	prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
858ccd979bdSMark Fasheh 
859ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, prev_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 	prev_bg->bg_next_group = bg->bg_next_group;
867ccd979bdSMark Fasheh 
868ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, prev_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, bg_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 	bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
882ccd979bdSMark Fasheh 
883ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, bg_bh);
884ccd979bdSMark Fasheh 	if (status < 0) {
885ccd979bdSMark Fasheh 		mlog_errno(status);
886ccd979bdSMark Fasheh 		goto out_rollback;
887ccd979bdSMark Fasheh 	}
888ccd979bdSMark Fasheh 
889ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
890ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
891ccd979bdSMark Fasheh 	if (status < 0) {
892ccd979bdSMark Fasheh 		mlog_errno(status);
893ccd979bdSMark Fasheh 		goto out_rollback;
894ccd979bdSMark Fasheh 	}
895ccd979bdSMark Fasheh 
896ccd979bdSMark Fasheh 	fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
897ccd979bdSMark Fasheh 
898ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, fe_bh);
899ccd979bdSMark Fasheh 	if (status < 0) {
900ccd979bdSMark Fasheh 		mlog_errno(status);
901ccd979bdSMark Fasheh 		goto out_rollback;
902ccd979bdSMark Fasheh 	}
903ccd979bdSMark Fasheh 
904ccd979bdSMark Fasheh 	status = 0;
905ccd979bdSMark Fasheh out_rollback:
906ccd979bdSMark Fasheh 	if (status < 0) {
907ccd979bdSMark Fasheh 		fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
908ccd979bdSMark Fasheh 		bg->bg_next_group = cpu_to_le64(bg_ptr);
909ccd979bdSMark Fasheh 		prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
910ccd979bdSMark Fasheh 	}
911ccd979bdSMark Fasheh out:
912ccd979bdSMark Fasheh 	mlog_exit(status);
913ccd979bdSMark Fasheh 	return status;
914ccd979bdSMark Fasheh }
915ccd979bdSMark Fasheh 
916ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
917ccd979bdSMark Fasheh 						     u32 wanted)
918ccd979bdSMark Fasheh {
919ccd979bdSMark Fasheh 	return le16_to_cpu(bg->bg_free_bits_count) > wanted;
920ccd979bdSMark Fasheh }
921ccd979bdSMark Fasheh 
922ccd979bdSMark Fasheh /* return 0 on success, -ENOSPC to keep searching and any other < 0
923ccd979bdSMark Fasheh  * value on error. */
924ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode,
925ccd979bdSMark Fasheh 				      struct buffer_head *group_bh,
926ccd979bdSMark Fasheh 				      u32 bits_wanted, u32 min_bits,
927ccd979bdSMark Fasheh 				      u16 *bit_off, u16 *bits_found)
928ccd979bdSMark Fasheh {
929ccd979bdSMark Fasheh 	int search = -ENOSPC;
930ccd979bdSMark Fasheh 	int ret;
9317bf72edeSMark Fasheh 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
932ccd979bdSMark Fasheh 	u16 tmp_off, tmp_found;
9337bf72edeSMark Fasheh 	unsigned int max_bits, gd_cluster_off;
934ccd979bdSMark Fasheh 
935ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
936ccd979bdSMark Fasheh 
9377bf72edeSMark Fasheh 	if (gd->bg_free_bits_count) {
9387bf72edeSMark Fasheh 		max_bits = le16_to_cpu(gd->bg_bits);
9397bf72edeSMark Fasheh 
9407bf72edeSMark Fasheh 		/* Tail groups in cluster bitmaps which aren't cpg
9417bf72edeSMark Fasheh 		 * aligned are prone to partial extention by a failed
9427bf72edeSMark Fasheh 		 * fs resize. If the file system resize never got to
9437bf72edeSMark Fasheh 		 * update the dinode cluster count, then we don't want
9447bf72edeSMark Fasheh 		 * to trust any clusters past it, regardless of what
9457bf72edeSMark Fasheh 		 * the group descriptor says. */
9467bf72edeSMark Fasheh 		gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
9477bf72edeSMark Fasheh 							  le64_to_cpu(gd->bg_blkno));
9487bf72edeSMark Fasheh 		if ((gd_cluster_off + max_bits) >
9497bf72edeSMark Fasheh 		    OCFS2_I(inode)->ip_clusters) {
9507bf72edeSMark Fasheh 			max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
9517bf72edeSMark Fasheh 			mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
9527bf72edeSMark Fasheh 			     (unsigned long long)le64_to_cpu(gd->bg_blkno),
9537bf72edeSMark Fasheh 			     le16_to_cpu(gd->bg_bits),
9547bf72edeSMark Fasheh 			     OCFS2_I(inode)->ip_clusters, max_bits);
9557bf72edeSMark Fasheh 		}
9567bf72edeSMark Fasheh 
957ccd979bdSMark Fasheh 		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
958ccd979bdSMark Fasheh 							group_bh, bits_wanted,
9597bf72edeSMark Fasheh 							max_bits,
960ccd979bdSMark Fasheh 							&tmp_off, &tmp_found);
961ccd979bdSMark Fasheh 		if (ret)
962ccd979bdSMark Fasheh 			return ret;
963ccd979bdSMark Fasheh 
964ccd979bdSMark Fasheh 		/* ocfs2_block_group_find_clear_bits() might
965ccd979bdSMark Fasheh 		 * return success, but we still want to return
966ccd979bdSMark Fasheh 		 * -ENOSPC unless it found the minimum number
967ccd979bdSMark Fasheh 		 * of bits. */
968ccd979bdSMark Fasheh 		if (min_bits <= tmp_found) {
969ccd979bdSMark Fasheh 			*bit_off = tmp_off;
970ccd979bdSMark Fasheh 			*bits_found = tmp_found;
971ccd979bdSMark Fasheh 			search = 0; /* success */
972ccd979bdSMark Fasheh 		}
973ccd979bdSMark Fasheh 	}
974ccd979bdSMark Fasheh 
975ccd979bdSMark Fasheh 	return search;
976ccd979bdSMark Fasheh }
977ccd979bdSMark Fasheh 
978ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode,
979ccd979bdSMark Fasheh 				    struct buffer_head *group_bh,
980ccd979bdSMark Fasheh 				    u32 bits_wanted, u32 min_bits,
981ccd979bdSMark Fasheh 				    u16 *bit_off, u16 *bits_found)
982ccd979bdSMark Fasheh {
983ccd979bdSMark Fasheh 	int ret = -ENOSPC;
984ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
985ccd979bdSMark Fasheh 
986ccd979bdSMark Fasheh 	BUG_ON(min_bits != 1);
987ccd979bdSMark Fasheh 	BUG_ON(ocfs2_is_cluster_bitmap(inode));
988ccd979bdSMark Fasheh 
989ccd979bdSMark Fasheh 	if (bg->bg_free_bits_count)
990ccd979bdSMark Fasheh 		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
991ccd979bdSMark Fasheh 							group_bh, bits_wanted,
9927bf72edeSMark Fasheh 							le16_to_cpu(bg->bg_bits),
993ccd979bdSMark Fasheh 							bit_off, bits_found);
994ccd979bdSMark Fasheh 
995ccd979bdSMark Fasheh 	return ret;
996ccd979bdSMark Fasheh }
997ccd979bdSMark Fasheh 
998883d4caeSMark Fasheh static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
9991fabe148SMark Fasheh 				       handle_t *handle,
1000883d4caeSMark Fasheh 				       struct buffer_head *di_bh,
1001883d4caeSMark Fasheh 				       u32 num_bits,
1002883d4caeSMark Fasheh 				       u16 chain)
1003883d4caeSMark Fasheh {
1004883d4caeSMark Fasheh 	int ret;
1005883d4caeSMark Fasheh 	u32 tmp_used;
1006883d4caeSMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1007883d4caeSMark Fasheh 	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1008883d4caeSMark Fasheh 
1009883d4caeSMark Fasheh 	ret = ocfs2_journal_access(handle, inode, di_bh,
1010883d4caeSMark Fasheh 				   OCFS2_JOURNAL_ACCESS_WRITE);
1011883d4caeSMark Fasheh 	if (ret < 0) {
1012883d4caeSMark Fasheh 		mlog_errno(ret);
1013883d4caeSMark Fasheh 		goto out;
1014883d4caeSMark Fasheh 	}
1015883d4caeSMark Fasheh 
1016883d4caeSMark Fasheh 	tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1017883d4caeSMark Fasheh 	di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1018883d4caeSMark Fasheh 	le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1019883d4caeSMark Fasheh 
1020883d4caeSMark Fasheh 	ret = ocfs2_journal_dirty(handle, di_bh);
1021883d4caeSMark Fasheh 	if (ret < 0)
1022883d4caeSMark Fasheh 		mlog_errno(ret);
1023883d4caeSMark Fasheh 
1024883d4caeSMark Fasheh out:
1025883d4caeSMark Fasheh 	return ret;
1026883d4caeSMark Fasheh }
1027883d4caeSMark Fasheh 
1028883d4caeSMark Fasheh static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
10291fabe148SMark Fasheh 				  handle_t *handle,
1030883d4caeSMark Fasheh 				  u32 bits_wanted,
1031883d4caeSMark Fasheh 				  u32 min_bits,
1032883d4caeSMark Fasheh 				  u16 *bit_off,
1033883d4caeSMark Fasheh 				  unsigned int *num_bits,
1034883d4caeSMark Fasheh 				  u64 gd_blkno,
1035883d4caeSMark Fasheh 				  u16 *bits_left)
1036883d4caeSMark Fasheh {
1037883d4caeSMark Fasheh 	int ret;
1038883d4caeSMark Fasheh 	u16 found;
1039883d4caeSMark Fasheh 	struct buffer_head *group_bh = NULL;
1040883d4caeSMark Fasheh 	struct ocfs2_group_desc *gd;
1041883d4caeSMark Fasheh 	struct inode *alloc_inode = ac->ac_inode;
1042883d4caeSMark Fasheh 
1043883d4caeSMark Fasheh 	ret = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), gd_blkno,
1044883d4caeSMark Fasheh 			       &group_bh, OCFS2_BH_CACHED, alloc_inode);
1045883d4caeSMark Fasheh 	if (ret < 0) {
1046883d4caeSMark Fasheh 		mlog_errno(ret);
1047883d4caeSMark Fasheh 		return ret;
1048883d4caeSMark Fasheh 	}
1049883d4caeSMark Fasheh 
1050883d4caeSMark Fasheh 	gd = (struct ocfs2_group_desc *) group_bh->b_data;
1051883d4caeSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1052883d4caeSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, gd);
1053883d4caeSMark Fasheh 		ret = -EIO;
1054883d4caeSMark Fasheh 		goto out;
1055883d4caeSMark Fasheh 	}
1056883d4caeSMark Fasheh 
1057883d4caeSMark Fasheh 	ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1058883d4caeSMark Fasheh 				  bit_off, &found);
1059883d4caeSMark Fasheh 	if (ret < 0) {
1060883d4caeSMark Fasheh 		if (ret != -ENOSPC)
1061883d4caeSMark Fasheh 			mlog_errno(ret);
1062883d4caeSMark Fasheh 		goto out;
1063883d4caeSMark Fasheh 	}
1064883d4caeSMark Fasheh 
1065883d4caeSMark Fasheh 	*num_bits = found;
1066883d4caeSMark Fasheh 
1067883d4caeSMark Fasheh 	ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1068883d4caeSMark Fasheh 					       *num_bits,
1069883d4caeSMark Fasheh 					       le16_to_cpu(gd->bg_chain));
1070883d4caeSMark Fasheh 	if (ret < 0) {
1071883d4caeSMark Fasheh 		mlog_errno(ret);
1072883d4caeSMark Fasheh 		goto out;
1073883d4caeSMark Fasheh 	}
1074883d4caeSMark Fasheh 
1075883d4caeSMark Fasheh 	ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1076883d4caeSMark Fasheh 					 *bit_off, *num_bits);
1077883d4caeSMark Fasheh 	if (ret < 0)
1078883d4caeSMark Fasheh 		mlog_errno(ret);
1079883d4caeSMark Fasheh 
1080883d4caeSMark Fasheh 	*bits_left = le16_to_cpu(gd->bg_free_bits_count);
1081883d4caeSMark Fasheh 
1082883d4caeSMark Fasheh out:
1083883d4caeSMark Fasheh 	brelse(group_bh);
1084883d4caeSMark Fasheh 
1085883d4caeSMark Fasheh 	return ret;
1086883d4caeSMark Fasheh }
1087883d4caeSMark Fasheh 
1088ccd979bdSMark Fasheh static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
10891fabe148SMark Fasheh 			      handle_t *handle,
1090ccd979bdSMark Fasheh 			      u32 bits_wanted,
1091ccd979bdSMark Fasheh 			      u32 min_bits,
1092ccd979bdSMark Fasheh 			      u16 *bit_off,
1093ccd979bdSMark Fasheh 			      unsigned int *num_bits,
1094883d4caeSMark Fasheh 			      u64 *bg_blkno,
1095883d4caeSMark Fasheh 			      u16 *bits_left)
1096ccd979bdSMark Fasheh {
1097ccd979bdSMark Fasheh 	int status;
1098ccd979bdSMark Fasheh 	u16 chain, tmp_bits;
1099ccd979bdSMark Fasheh 	u32 tmp_used;
1100ccd979bdSMark Fasheh 	u64 next_group;
1101ccd979bdSMark Fasheh 	struct inode *alloc_inode = ac->ac_inode;
1102ccd979bdSMark Fasheh 	struct buffer_head *group_bh = NULL;
1103ccd979bdSMark Fasheh 	struct buffer_head *prev_group_bh = NULL;
1104ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1105ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1106ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg;
1107ccd979bdSMark Fasheh 
1108ccd979bdSMark Fasheh 	chain = ac->ac_chain;
1109b0697053SMark Fasheh 	mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1110b0697053SMark Fasheh 	     bits_wanted, chain,
1111b0697053SMark Fasheh 	     (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
1112ccd979bdSMark Fasheh 
1113ccd979bdSMark Fasheh 	status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1114ccd979bdSMark Fasheh 				  le64_to_cpu(cl->cl_recs[chain].c_blkno),
1115ccd979bdSMark Fasheh 				  &group_bh, OCFS2_BH_CACHED, alloc_inode);
1116ccd979bdSMark Fasheh 	if (status < 0) {
1117ccd979bdSMark Fasheh 		mlog_errno(status);
1118ccd979bdSMark Fasheh 		goto bail;
1119ccd979bdSMark Fasheh 	}
1120ccd979bdSMark Fasheh 	bg = (struct ocfs2_group_desc *) group_bh->b_data;
11217bf72edeSMark Fasheh 	status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
11227bf72edeSMark Fasheh 	if (status) {
11237bf72edeSMark Fasheh 		mlog_errno(status);
1124ccd979bdSMark Fasheh 		goto bail;
1125ccd979bdSMark Fasheh 	}
1126ccd979bdSMark Fasheh 
1127ccd979bdSMark Fasheh 	status = -ENOSPC;
1128ccd979bdSMark Fasheh 	/* for now, the chain search is a bit simplistic. We just use
1129ccd979bdSMark Fasheh 	 * the 1st group with any empty bits. */
1130ccd979bdSMark Fasheh 	while ((status = ac->ac_group_search(alloc_inode, group_bh,
1131ccd979bdSMark Fasheh 					     bits_wanted, min_bits, bit_off,
1132ccd979bdSMark Fasheh 					     &tmp_bits)) == -ENOSPC) {
1133ccd979bdSMark Fasheh 		if (!bg->bg_next_group)
1134ccd979bdSMark Fasheh 			break;
1135ccd979bdSMark Fasheh 
1136ccd979bdSMark Fasheh 		if (prev_group_bh) {
1137ccd979bdSMark Fasheh 			brelse(prev_group_bh);
1138ccd979bdSMark Fasheh 			prev_group_bh = NULL;
1139ccd979bdSMark Fasheh 		}
1140ccd979bdSMark Fasheh 		next_group = le64_to_cpu(bg->bg_next_group);
1141ccd979bdSMark Fasheh 		prev_group_bh = group_bh;
1142ccd979bdSMark Fasheh 		group_bh = NULL;
1143ccd979bdSMark Fasheh 		status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1144ccd979bdSMark Fasheh 					  next_group, &group_bh,
1145ccd979bdSMark Fasheh 					  OCFS2_BH_CACHED, alloc_inode);
1146ccd979bdSMark Fasheh 		if (status < 0) {
1147ccd979bdSMark Fasheh 			mlog_errno(status);
1148ccd979bdSMark Fasheh 			goto bail;
1149ccd979bdSMark Fasheh 		}
1150ccd979bdSMark Fasheh 		bg = (struct ocfs2_group_desc *) group_bh->b_data;
11517bf72edeSMark Fasheh 		status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
11527bf72edeSMark Fasheh 		if (status) {
11537bf72edeSMark Fasheh 			mlog_errno(status);
1154ccd979bdSMark Fasheh 			goto bail;
1155ccd979bdSMark Fasheh 		}
1156ccd979bdSMark Fasheh 	}
1157ccd979bdSMark Fasheh 	if (status < 0) {
1158ccd979bdSMark Fasheh 		if (status != -ENOSPC)
1159ccd979bdSMark Fasheh 			mlog_errno(status);
1160ccd979bdSMark Fasheh 		goto bail;
1161ccd979bdSMark Fasheh 	}
1162ccd979bdSMark Fasheh 
1163b0697053SMark Fasheh 	mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
11641ca1a111SMark Fasheh 	     tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
1165ccd979bdSMark Fasheh 
1166ccd979bdSMark Fasheh 	*num_bits = tmp_bits;
1167ccd979bdSMark Fasheh 
1168ccd979bdSMark Fasheh 	BUG_ON(*num_bits == 0);
1169ccd979bdSMark Fasheh 
1170ccd979bdSMark Fasheh 	/*
1171ccd979bdSMark Fasheh 	 * Keep track of previous block descriptor read. When
1172ccd979bdSMark Fasheh 	 * we find a target, if we have read more than X
1173ccd979bdSMark Fasheh 	 * number of descriptors, and the target is reasonably
1174ccd979bdSMark Fasheh 	 * empty, relink him to top of his chain.
1175ccd979bdSMark Fasheh 	 *
1176ccd979bdSMark Fasheh 	 * We've read 0 extra blocks and only send one more to
1177ccd979bdSMark Fasheh 	 * the transaction, yet the next guy to search has a
1178ccd979bdSMark Fasheh 	 * much easier time.
1179ccd979bdSMark Fasheh 	 *
1180ccd979bdSMark Fasheh 	 * Do this *after* figuring out how many bits we're taking out
1181ccd979bdSMark Fasheh 	 * of our target group.
1182ccd979bdSMark Fasheh 	 */
1183ccd979bdSMark Fasheh 	if (ac->ac_allow_chain_relink &&
1184ccd979bdSMark Fasheh 	    (prev_group_bh) &&
1185ccd979bdSMark Fasheh 	    (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1186ccd979bdSMark Fasheh 		status = ocfs2_relink_block_group(handle, alloc_inode,
1187ccd979bdSMark Fasheh 						  ac->ac_bh, group_bh,
1188ccd979bdSMark Fasheh 						  prev_group_bh, chain);
1189ccd979bdSMark Fasheh 		if (status < 0) {
1190ccd979bdSMark Fasheh 			mlog_errno(status);
1191ccd979bdSMark Fasheh 			goto bail;
1192ccd979bdSMark Fasheh 		}
1193ccd979bdSMark Fasheh 	}
1194ccd979bdSMark Fasheh 
1195ccd979bdSMark Fasheh 	/* Ok, claim our bits now: set the info on dinode, chainlist
1196ccd979bdSMark Fasheh 	 * and then the group */
1197ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle,
1198ccd979bdSMark Fasheh 				      alloc_inode,
1199ccd979bdSMark Fasheh 				      ac->ac_bh,
1200ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
1201ccd979bdSMark Fasheh 	if (status < 0) {
1202ccd979bdSMark Fasheh 		mlog_errno(status);
1203ccd979bdSMark Fasheh 		goto bail;
1204ccd979bdSMark Fasheh 	}
1205ccd979bdSMark Fasheh 
1206ccd979bdSMark Fasheh 	tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1207ccd979bdSMark Fasheh 	fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1208ccd979bdSMark Fasheh 	le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1209ccd979bdSMark Fasheh 
1210ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle,
1211ccd979bdSMark Fasheh 				     ac->ac_bh);
1212ccd979bdSMark Fasheh 	if (status < 0) {
1213ccd979bdSMark Fasheh 		mlog_errno(status);
1214ccd979bdSMark Fasheh 		goto bail;
1215ccd979bdSMark Fasheh 	}
1216ccd979bdSMark Fasheh 
1217ccd979bdSMark Fasheh 	status = ocfs2_block_group_set_bits(handle,
1218ccd979bdSMark Fasheh 					    alloc_inode,
1219ccd979bdSMark Fasheh 					    bg,
1220ccd979bdSMark Fasheh 					    group_bh,
1221ccd979bdSMark Fasheh 					    *bit_off,
1222ccd979bdSMark Fasheh 					    *num_bits);
1223ccd979bdSMark Fasheh 	if (status < 0) {
1224ccd979bdSMark Fasheh 		mlog_errno(status);
1225ccd979bdSMark Fasheh 		goto bail;
1226ccd979bdSMark Fasheh 	}
1227ccd979bdSMark Fasheh 
1228b0697053SMark Fasheh 	mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
12291ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(fe->i_blkno));
1230ccd979bdSMark Fasheh 
1231ccd979bdSMark Fasheh 	*bg_blkno = le64_to_cpu(bg->bg_blkno);
1232883d4caeSMark Fasheh 	*bits_left = le16_to_cpu(bg->bg_free_bits_count);
1233ccd979bdSMark Fasheh bail:
1234ccd979bdSMark Fasheh 	if (group_bh)
1235ccd979bdSMark Fasheh 		brelse(group_bh);
1236ccd979bdSMark Fasheh 	if (prev_group_bh)
1237ccd979bdSMark Fasheh 		brelse(prev_group_bh);
1238ccd979bdSMark Fasheh 
1239ccd979bdSMark Fasheh 	mlog_exit(status);
1240ccd979bdSMark Fasheh 	return status;
1241ccd979bdSMark Fasheh }
1242ccd979bdSMark Fasheh 
1243ccd979bdSMark Fasheh /* will give out up to bits_wanted contiguous bits. */
1244ccd979bdSMark Fasheh static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1245ccd979bdSMark Fasheh 				     struct ocfs2_alloc_context *ac,
12461fabe148SMark Fasheh 				     handle_t *handle,
1247ccd979bdSMark Fasheh 				     u32 bits_wanted,
1248ccd979bdSMark Fasheh 				     u32 min_bits,
1249ccd979bdSMark Fasheh 				     u16 *bit_off,
1250ccd979bdSMark Fasheh 				     unsigned int *num_bits,
1251ccd979bdSMark Fasheh 				     u64 *bg_blkno)
1252ccd979bdSMark Fasheh {
1253ccd979bdSMark Fasheh 	int status;
1254ccd979bdSMark Fasheh 	u16 victim, i;
1255883d4caeSMark Fasheh 	u16 bits_left = 0;
1256883d4caeSMark Fasheh 	u64 hint_blkno = ac->ac_last_group;
1257ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl;
1258ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
1259ccd979bdSMark Fasheh 
1260ccd979bdSMark Fasheh 	mlog_entry_void();
1261ccd979bdSMark Fasheh 
1262ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1263ccd979bdSMark Fasheh 	BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1264ccd979bdSMark Fasheh 	BUG_ON(!ac->ac_bh);
1265ccd979bdSMark Fasheh 
1266ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1267ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
1268ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(osb->sb, fe);
1269ccd979bdSMark Fasheh 		status = -EIO;
1270ccd979bdSMark Fasheh 		goto bail;
1271ccd979bdSMark Fasheh 	}
1272ccd979bdSMark Fasheh 	if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1273ccd979bdSMark Fasheh 	    le32_to_cpu(fe->id1.bitmap1.i_total)) {
1274b0697053SMark Fasheh 		ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1275b0697053SMark Fasheh 			    "bits but only %u total.",
1276b0697053SMark Fasheh 			    (unsigned long long)le64_to_cpu(fe->i_blkno),
1277ccd979bdSMark Fasheh 			    le32_to_cpu(fe->id1.bitmap1.i_used),
1278ccd979bdSMark Fasheh 			    le32_to_cpu(fe->id1.bitmap1.i_total));
1279ccd979bdSMark Fasheh 		status = -EIO;
1280ccd979bdSMark Fasheh 		goto bail;
1281ccd979bdSMark Fasheh 	}
1282ccd979bdSMark Fasheh 
1283883d4caeSMark Fasheh 	if (hint_blkno) {
1284883d4caeSMark Fasheh 		/* Attempt to short-circuit the usual search mechanism
1285883d4caeSMark Fasheh 		 * by jumping straight to the most recently used
1286883d4caeSMark Fasheh 		 * allocation group. This helps us mantain some
1287883d4caeSMark Fasheh 		 * contiguousness across allocations. */
1288da5cbf2fSMark Fasheh 		status = ocfs2_search_one_group(ac, handle, bits_wanted,
1289da5cbf2fSMark Fasheh 						min_bits, bit_off, num_bits,
1290883d4caeSMark Fasheh 						hint_blkno, &bits_left);
1291883d4caeSMark Fasheh 		if (!status) {
1292883d4caeSMark Fasheh 			/* Be careful to update *bg_blkno here as the
1293883d4caeSMark Fasheh 			 * caller is expecting it to be filled in, and
1294883d4caeSMark Fasheh 			 * ocfs2_search_one_group() won't do that for
1295883d4caeSMark Fasheh 			 * us. */
1296883d4caeSMark Fasheh 			*bg_blkno = hint_blkno;
1297883d4caeSMark Fasheh 			goto set_hint;
1298883d4caeSMark Fasheh 		}
1299883d4caeSMark Fasheh 		if (status < 0 && status != -ENOSPC) {
1300883d4caeSMark Fasheh 			mlog_errno(status);
1301883d4caeSMark Fasheh 			goto bail;
1302883d4caeSMark Fasheh 		}
1303883d4caeSMark Fasheh 	}
1304883d4caeSMark Fasheh 
1305ccd979bdSMark Fasheh 	cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1306ccd979bdSMark Fasheh 
1307ccd979bdSMark Fasheh 	victim = ocfs2_find_victim_chain(cl);
1308ccd979bdSMark Fasheh 	ac->ac_chain = victim;
1309ccd979bdSMark Fasheh 	ac->ac_allow_chain_relink = 1;
1310ccd979bdSMark Fasheh 
1311da5cbf2fSMark Fasheh 	status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
1312883d4caeSMark Fasheh 				    num_bits, bg_blkno, &bits_left);
1313ccd979bdSMark Fasheh 	if (!status)
1314883d4caeSMark Fasheh 		goto set_hint;
1315ccd979bdSMark Fasheh 	if (status < 0 && status != -ENOSPC) {
1316ccd979bdSMark Fasheh 		mlog_errno(status);
1317ccd979bdSMark Fasheh 		goto bail;
1318ccd979bdSMark Fasheh 	}
1319ccd979bdSMark Fasheh 
1320ccd979bdSMark Fasheh 	mlog(0, "Search of victim chain %u came up with nothing, "
1321ccd979bdSMark Fasheh 	     "trying all chains now.\n", victim);
1322ccd979bdSMark Fasheh 
1323ccd979bdSMark Fasheh 	/* If we didn't pick a good victim, then just default to
1324ccd979bdSMark Fasheh 	 * searching each chain in order. Don't allow chain relinking
1325ccd979bdSMark Fasheh 	 * because we only calculate enough journal credits for one
1326ccd979bdSMark Fasheh 	 * relink per alloc. */
1327ccd979bdSMark Fasheh 	ac->ac_allow_chain_relink = 0;
1328ccd979bdSMark Fasheh 	for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1329ccd979bdSMark Fasheh 		if (i == victim)
1330ccd979bdSMark Fasheh 			continue;
1331ccd979bdSMark Fasheh 		if (!cl->cl_recs[i].c_free)
1332ccd979bdSMark Fasheh 			continue;
1333ccd979bdSMark Fasheh 
1334ccd979bdSMark Fasheh 		ac->ac_chain = i;
1335da5cbf2fSMark Fasheh 		status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1336883d4caeSMark Fasheh 					    bit_off, num_bits, bg_blkno,
1337883d4caeSMark Fasheh 					    &bits_left);
1338ccd979bdSMark Fasheh 		if (!status)
1339ccd979bdSMark Fasheh 			break;
1340ccd979bdSMark Fasheh 		if (status < 0 && status != -ENOSPC) {
1341ccd979bdSMark Fasheh 			mlog_errno(status);
1342ccd979bdSMark Fasheh 			goto bail;
1343ccd979bdSMark Fasheh 		}
1344ccd979bdSMark Fasheh 	}
1345ccd979bdSMark Fasheh 
1346883d4caeSMark Fasheh set_hint:
1347883d4caeSMark Fasheh 	if (status != -ENOSPC) {
1348883d4caeSMark Fasheh 		/* If the next search of this group is not likely to
1349883d4caeSMark Fasheh 		 * yield a suitable extent, then we reset the last
1350883d4caeSMark Fasheh 		 * group hint so as to not waste a disk read */
1351883d4caeSMark Fasheh 		if (bits_left < min_bits)
1352883d4caeSMark Fasheh 			ac->ac_last_group = 0;
1353883d4caeSMark Fasheh 		else
1354883d4caeSMark Fasheh 			ac->ac_last_group = *bg_blkno;
1355883d4caeSMark Fasheh 	}
1356883d4caeSMark Fasheh 
1357883d4caeSMark Fasheh bail:
1358ccd979bdSMark Fasheh 	mlog_exit(status);
1359ccd979bdSMark Fasheh 	return status;
1360ccd979bdSMark Fasheh }
1361ccd979bdSMark Fasheh 
1362ccd979bdSMark Fasheh int ocfs2_claim_metadata(struct ocfs2_super *osb,
13631fabe148SMark Fasheh 			 handle_t *handle,
1364ccd979bdSMark Fasheh 			 struct ocfs2_alloc_context *ac,
1365ccd979bdSMark Fasheh 			 u32 bits_wanted,
1366ccd979bdSMark Fasheh 			 u16 *suballoc_bit_start,
1367ccd979bdSMark Fasheh 			 unsigned int *num_bits,
1368ccd979bdSMark Fasheh 			 u64 *blkno_start)
1369ccd979bdSMark Fasheh {
1370ccd979bdSMark Fasheh 	int status;
1371ccd979bdSMark Fasheh 	u64 bg_blkno;
1372ccd979bdSMark Fasheh 
1373ccd979bdSMark Fasheh 	BUG_ON(!ac);
1374ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1375ccd979bdSMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
1376ccd979bdSMark Fasheh 
1377ccd979bdSMark Fasheh 	status = ocfs2_claim_suballoc_bits(osb,
1378ccd979bdSMark Fasheh 					   ac,
1379da5cbf2fSMark Fasheh 					   handle,
1380ccd979bdSMark Fasheh 					   bits_wanted,
1381ccd979bdSMark Fasheh 					   1,
1382ccd979bdSMark Fasheh 					   suballoc_bit_start,
1383ccd979bdSMark Fasheh 					   num_bits,
1384ccd979bdSMark Fasheh 					   &bg_blkno);
1385ccd979bdSMark Fasheh 	if (status < 0) {
1386ccd979bdSMark Fasheh 		mlog_errno(status);
1387ccd979bdSMark Fasheh 		goto bail;
1388ccd979bdSMark Fasheh 	}
1389ccd979bdSMark Fasheh 	atomic_inc(&osb->alloc_stats.bg_allocs);
1390ccd979bdSMark Fasheh 
1391ccd979bdSMark Fasheh 	*blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1392ccd979bdSMark Fasheh 	ac->ac_bits_given += (*num_bits);
1393ccd979bdSMark Fasheh 	status = 0;
1394ccd979bdSMark Fasheh bail:
1395ccd979bdSMark Fasheh 	mlog_exit(status);
1396ccd979bdSMark Fasheh 	return status;
1397ccd979bdSMark Fasheh }
1398ccd979bdSMark Fasheh 
1399ccd979bdSMark Fasheh int ocfs2_claim_new_inode(struct ocfs2_super *osb,
14001fabe148SMark Fasheh 			  handle_t *handle,
1401ccd979bdSMark Fasheh 			  struct ocfs2_alloc_context *ac,
1402ccd979bdSMark Fasheh 			  u16 *suballoc_bit,
1403ccd979bdSMark Fasheh 			  u64 *fe_blkno)
1404ccd979bdSMark Fasheh {
1405ccd979bdSMark Fasheh 	int status;
1406ccd979bdSMark Fasheh 	unsigned int num_bits;
1407ccd979bdSMark Fasheh 	u64 bg_blkno;
1408ccd979bdSMark Fasheh 
1409ccd979bdSMark Fasheh 	mlog_entry_void();
1410ccd979bdSMark Fasheh 
1411ccd979bdSMark Fasheh 	BUG_ON(!ac);
1412ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_given != 0);
1413ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_wanted != 1);
1414ccd979bdSMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
1415ccd979bdSMark Fasheh 
1416ccd979bdSMark Fasheh 	status = ocfs2_claim_suballoc_bits(osb,
1417ccd979bdSMark Fasheh 					   ac,
1418da5cbf2fSMark Fasheh 					   handle,
1419ccd979bdSMark Fasheh 					   1,
1420ccd979bdSMark Fasheh 					   1,
1421ccd979bdSMark Fasheh 					   suballoc_bit,
1422ccd979bdSMark Fasheh 					   &num_bits,
1423ccd979bdSMark Fasheh 					   &bg_blkno);
1424ccd979bdSMark Fasheh 	if (status < 0) {
1425ccd979bdSMark Fasheh 		mlog_errno(status);
1426ccd979bdSMark Fasheh 		goto bail;
1427ccd979bdSMark Fasheh 	}
1428ccd979bdSMark Fasheh 	atomic_inc(&osb->alloc_stats.bg_allocs);
1429ccd979bdSMark Fasheh 
1430ccd979bdSMark Fasheh 	BUG_ON(num_bits != 1);
1431ccd979bdSMark Fasheh 
1432ccd979bdSMark Fasheh 	*fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1433ccd979bdSMark Fasheh 	ac->ac_bits_given++;
1434ccd979bdSMark Fasheh 	status = 0;
1435ccd979bdSMark Fasheh bail:
1436ccd979bdSMark Fasheh 	mlog_exit(status);
1437ccd979bdSMark Fasheh 	return status;
1438ccd979bdSMark Fasheh }
1439ccd979bdSMark Fasheh 
1440ccd979bdSMark Fasheh /* translate a group desc. blkno and it's bitmap offset into
1441ccd979bdSMark Fasheh  * disk cluster offset. */
1442ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1443ccd979bdSMark Fasheh 						   u64 bg_blkno,
1444ccd979bdSMark Fasheh 						   u16 bg_bit_off)
1445ccd979bdSMark Fasheh {
1446ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1447ccd979bdSMark Fasheh 	u32 cluster = 0;
1448ccd979bdSMark Fasheh 
1449ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1450ccd979bdSMark Fasheh 
1451ccd979bdSMark Fasheh 	if (bg_blkno != osb->first_cluster_group_blkno)
1452ccd979bdSMark Fasheh 		cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1453ccd979bdSMark Fasheh 	cluster += (u32) bg_bit_off;
1454ccd979bdSMark Fasheh 	return cluster;
1455ccd979bdSMark Fasheh }
1456ccd979bdSMark Fasheh 
1457ccd979bdSMark Fasheh /* given a cluster offset, calculate which block group it belongs to
1458ccd979bdSMark Fasheh  * and return that block offset. */
1459d659072fSTao Ma u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
1460ccd979bdSMark Fasheh {
1461ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1462ccd979bdSMark Fasheh 	u32 group_no;
1463ccd979bdSMark Fasheh 
1464ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1465ccd979bdSMark Fasheh 
1466ccd979bdSMark Fasheh 	group_no = cluster / osb->bitmap_cpg;
1467ccd979bdSMark Fasheh 	if (!group_no)
1468ccd979bdSMark Fasheh 		return osb->first_cluster_group_blkno;
1469ccd979bdSMark Fasheh 	return ocfs2_clusters_to_blocks(inode->i_sb,
1470ccd979bdSMark Fasheh 					group_no * osb->bitmap_cpg);
1471ccd979bdSMark Fasheh }
1472ccd979bdSMark Fasheh 
1473ccd979bdSMark Fasheh /* given the block number of a cluster start, calculate which cluster
1474ccd979bdSMark Fasheh  * group and descriptor bitmap offset that corresponds to. */
1475ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1476ccd979bdSMark Fasheh 						u64 data_blkno,
1477ccd979bdSMark Fasheh 						u64 *bg_blkno,
1478ccd979bdSMark Fasheh 						u16 *bg_bit_off)
1479ccd979bdSMark Fasheh {
1480ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1481ccd979bdSMark Fasheh 	u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1482ccd979bdSMark Fasheh 
1483ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1484ccd979bdSMark Fasheh 
1485ccd979bdSMark Fasheh 	*bg_blkno = ocfs2_which_cluster_group(inode,
1486ccd979bdSMark Fasheh 					      data_cluster);
1487ccd979bdSMark Fasheh 
1488ccd979bdSMark Fasheh 	if (*bg_blkno == osb->first_cluster_group_blkno)
1489ccd979bdSMark Fasheh 		*bg_bit_off = (u16) data_cluster;
1490ccd979bdSMark Fasheh 	else
1491ccd979bdSMark Fasheh 		*bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1492ccd979bdSMark Fasheh 							     data_blkno - *bg_blkno);
1493ccd979bdSMark Fasheh }
1494ccd979bdSMark Fasheh 
1495ccd979bdSMark Fasheh /*
1496ccd979bdSMark Fasheh  * min_bits - minimum contiguous chunk from this total allocation we
1497ccd979bdSMark Fasheh  * can handle. set to what we asked for originally for a full
1498ccd979bdSMark Fasheh  * contig. allocation, set to '1' to indicate we can deal with extents
1499ccd979bdSMark Fasheh  * of any size.
1500ccd979bdSMark Fasheh  */
1501415cb800SMark Fasheh int __ocfs2_claim_clusters(struct ocfs2_super *osb,
15021fabe148SMark Fasheh 			   handle_t *handle,
1503ccd979bdSMark Fasheh 			   struct ocfs2_alloc_context *ac,
1504ccd979bdSMark Fasheh 			   u32 min_clusters,
1505415cb800SMark Fasheh 			   u32 max_clusters,
1506ccd979bdSMark Fasheh 			   u32 *cluster_start,
1507ccd979bdSMark Fasheh 			   u32 *num_clusters)
1508ccd979bdSMark Fasheh {
1509ccd979bdSMark Fasheh 	int status;
1510415cb800SMark Fasheh 	unsigned int bits_wanted = max_clusters;
1511883d4caeSMark Fasheh 	u64 bg_blkno = 0;
1512ccd979bdSMark Fasheh 	u16 bg_bit_off;
1513ccd979bdSMark Fasheh 
1514ccd979bdSMark Fasheh 	mlog_entry_void();
1515ccd979bdSMark Fasheh 
1516ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1517ccd979bdSMark Fasheh 
1518ccd979bdSMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1519ccd979bdSMark Fasheh 	       && ac->ac_which != OCFS2_AC_USE_MAIN);
1520ccd979bdSMark Fasheh 
1521ccd979bdSMark Fasheh 	if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1522ccd979bdSMark Fasheh 		status = ocfs2_claim_local_alloc_bits(osb,
1523ccd979bdSMark Fasheh 						      handle,
1524ccd979bdSMark Fasheh 						      ac,
1525ccd979bdSMark Fasheh 						      bits_wanted,
1526ccd979bdSMark Fasheh 						      cluster_start,
1527ccd979bdSMark Fasheh 						      num_clusters);
1528ccd979bdSMark Fasheh 		if (!status)
1529ccd979bdSMark Fasheh 			atomic_inc(&osb->alloc_stats.local_data);
1530ccd979bdSMark Fasheh 	} else {
1531ccd979bdSMark Fasheh 		if (min_clusters > (osb->bitmap_cpg - 1)) {
1532ccd979bdSMark Fasheh 			/* The only paths asking for contiguousness
1533ccd979bdSMark Fasheh 			 * should know about this already. */
15342fbe8d1eSSunil Mushran 			mlog(ML_ERROR, "minimum allocation requested %u exceeds "
15352fbe8d1eSSunil Mushran 			     "group bitmap size %u!\n", min_clusters,
15362fbe8d1eSSunil Mushran 			     osb->bitmap_cpg);
1537ccd979bdSMark Fasheh 			status = -ENOSPC;
1538ccd979bdSMark Fasheh 			goto bail;
1539ccd979bdSMark Fasheh 		}
1540ccd979bdSMark Fasheh 		/* clamp the current request down to a realistic size. */
1541ccd979bdSMark Fasheh 		if (bits_wanted > (osb->bitmap_cpg - 1))
1542ccd979bdSMark Fasheh 			bits_wanted = osb->bitmap_cpg - 1;
1543ccd979bdSMark Fasheh 
1544ccd979bdSMark Fasheh 		status = ocfs2_claim_suballoc_bits(osb,
1545ccd979bdSMark Fasheh 						   ac,
1546da5cbf2fSMark Fasheh 						   handle,
1547ccd979bdSMark Fasheh 						   bits_wanted,
1548ccd979bdSMark Fasheh 						   min_clusters,
1549ccd979bdSMark Fasheh 						   &bg_bit_off,
1550ccd979bdSMark Fasheh 						   num_clusters,
1551ccd979bdSMark Fasheh 						   &bg_blkno);
1552ccd979bdSMark Fasheh 		if (!status) {
1553ccd979bdSMark Fasheh 			*cluster_start =
1554ccd979bdSMark Fasheh 				ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1555ccd979bdSMark Fasheh 								 bg_blkno,
1556ccd979bdSMark Fasheh 								 bg_bit_off);
1557ccd979bdSMark Fasheh 			atomic_inc(&osb->alloc_stats.bitmap_data);
1558ccd979bdSMark Fasheh 		}
1559ccd979bdSMark Fasheh 	}
1560ccd979bdSMark Fasheh 	if (status < 0) {
1561ccd979bdSMark Fasheh 		if (status != -ENOSPC)
1562ccd979bdSMark Fasheh 			mlog_errno(status);
1563ccd979bdSMark Fasheh 		goto bail;
1564ccd979bdSMark Fasheh 	}
1565ccd979bdSMark Fasheh 
1566ccd979bdSMark Fasheh 	ac->ac_bits_given += *num_clusters;
1567ccd979bdSMark Fasheh 
1568ccd979bdSMark Fasheh bail:
1569ccd979bdSMark Fasheh 	mlog_exit(status);
1570ccd979bdSMark Fasheh 	return status;
1571ccd979bdSMark Fasheh }
1572ccd979bdSMark Fasheh 
1573415cb800SMark Fasheh int ocfs2_claim_clusters(struct ocfs2_super *osb,
1574415cb800SMark Fasheh 			 handle_t *handle,
1575415cb800SMark Fasheh 			 struct ocfs2_alloc_context *ac,
1576415cb800SMark Fasheh 			 u32 min_clusters,
1577415cb800SMark Fasheh 			 u32 *cluster_start,
1578415cb800SMark Fasheh 			 u32 *num_clusters)
1579415cb800SMark Fasheh {
1580415cb800SMark Fasheh 	unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1581415cb800SMark Fasheh 
1582415cb800SMark Fasheh 	return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1583415cb800SMark Fasheh 				      bits_wanted, cluster_start, num_clusters);
1584415cb800SMark Fasheh }
1585415cb800SMark Fasheh 
15861fabe148SMark Fasheh static inline int ocfs2_block_group_clear_bits(handle_t *handle,
1587ccd979bdSMark Fasheh 					       struct inode *alloc_inode,
1588ccd979bdSMark Fasheh 					       struct ocfs2_group_desc *bg,
1589ccd979bdSMark Fasheh 					       struct buffer_head *group_bh,
1590ccd979bdSMark Fasheh 					       unsigned int bit_off,
1591ccd979bdSMark Fasheh 					       unsigned int num_bits)
1592ccd979bdSMark Fasheh {
1593ccd979bdSMark Fasheh 	int status;
1594ccd979bdSMark Fasheh 	unsigned int tmp;
1595ccd979bdSMark Fasheh 	int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1596ccd979bdSMark Fasheh 	struct ocfs2_group_desc *undo_bg = NULL;
1597ccd979bdSMark Fasheh 
1598ccd979bdSMark Fasheh 	mlog_entry_void();
1599ccd979bdSMark Fasheh 
1600ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
1601ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
1602ccd979bdSMark Fasheh 		status = -EIO;
1603ccd979bdSMark Fasheh 		goto bail;
1604ccd979bdSMark Fasheh 	}
1605ccd979bdSMark Fasheh 
1606ccd979bdSMark Fasheh 	mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1607ccd979bdSMark Fasheh 
1608ccd979bdSMark Fasheh 	if (ocfs2_is_cluster_bitmap(alloc_inode))
1609ccd979bdSMark Fasheh 		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1610ccd979bdSMark Fasheh 
1611ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, group_bh,
1612ccd979bdSMark Fasheh 				      journal_type);
1613ccd979bdSMark Fasheh 	if (status < 0) {
1614ccd979bdSMark Fasheh 		mlog_errno(status);
1615ccd979bdSMark Fasheh 		goto bail;
1616ccd979bdSMark Fasheh 	}
1617ccd979bdSMark Fasheh 
1618ccd979bdSMark Fasheh 	if (ocfs2_is_cluster_bitmap(alloc_inode))
1619ccd979bdSMark Fasheh 		undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
1620ccd979bdSMark Fasheh 
1621ccd979bdSMark Fasheh 	tmp = num_bits;
1622ccd979bdSMark Fasheh 	while(tmp--) {
1623ccd979bdSMark Fasheh 		ocfs2_clear_bit((bit_off + tmp),
1624ccd979bdSMark Fasheh 				(unsigned long *) bg->bg_bitmap);
1625ccd979bdSMark Fasheh 		if (ocfs2_is_cluster_bitmap(alloc_inode))
1626ccd979bdSMark Fasheh 			ocfs2_set_bit(bit_off + tmp,
1627ccd979bdSMark Fasheh 				      (unsigned long *) undo_bg->bg_bitmap);
1628ccd979bdSMark Fasheh 	}
1629ccd979bdSMark Fasheh 	le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1630ccd979bdSMark Fasheh 
1631ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, group_bh);
1632ccd979bdSMark Fasheh 	if (status < 0)
1633ccd979bdSMark Fasheh 		mlog_errno(status);
1634ccd979bdSMark Fasheh bail:
1635ccd979bdSMark Fasheh 	return status;
1636ccd979bdSMark Fasheh }
1637ccd979bdSMark Fasheh 
1638ccd979bdSMark Fasheh /*
1639ccd979bdSMark Fasheh  * expects the suballoc inode to already be locked.
1640ccd979bdSMark Fasheh  */
16412b604351SMark Fasheh int ocfs2_free_suballoc_bits(handle_t *handle,
1642ccd979bdSMark Fasheh 			     struct inode *alloc_inode,
1643ccd979bdSMark Fasheh 			     struct buffer_head *alloc_bh,
1644ccd979bdSMark Fasheh 			     unsigned int start_bit,
1645ccd979bdSMark Fasheh 			     u64 bg_blkno,
1646ccd979bdSMark Fasheh 			     unsigned int count)
1647ccd979bdSMark Fasheh {
1648ccd979bdSMark Fasheh 	int status = 0;
1649ccd979bdSMark Fasheh 	u32 tmp_used;
1650ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
1651ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1652ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1653ccd979bdSMark Fasheh 	struct buffer_head *group_bh = NULL;
1654ccd979bdSMark Fasheh 	struct ocfs2_group_desc *group;
1655ccd979bdSMark Fasheh 
1656ccd979bdSMark Fasheh 	mlog_entry_void();
1657ccd979bdSMark Fasheh 
1658ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
1659ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
1660ccd979bdSMark Fasheh 		status = -EIO;
1661ccd979bdSMark Fasheh 		goto bail;
1662ccd979bdSMark Fasheh 	}
1663ccd979bdSMark Fasheh 	BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1664ccd979bdSMark Fasheh 
1665b0697053SMark Fasheh 	mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1666b0697053SMark Fasheh 	     (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1667b0697053SMark Fasheh 	     (unsigned long long)bg_blkno, start_bit);
1668ccd979bdSMark Fasheh 
1669ccd979bdSMark Fasheh 	status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED,
1670ccd979bdSMark Fasheh 				  alloc_inode);
1671ccd979bdSMark Fasheh 	if (status < 0) {
1672ccd979bdSMark Fasheh 		mlog_errno(status);
1673ccd979bdSMark Fasheh 		goto bail;
1674ccd979bdSMark Fasheh 	}
1675ccd979bdSMark Fasheh 
1676ccd979bdSMark Fasheh 	group = (struct ocfs2_group_desc *) group_bh->b_data;
16777bf72edeSMark Fasheh 	status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, group);
16787bf72edeSMark Fasheh 	if (status) {
16797bf72edeSMark Fasheh 		mlog_errno(status);
1680ccd979bdSMark Fasheh 		goto bail;
1681ccd979bdSMark Fasheh 	}
1682ccd979bdSMark Fasheh 	BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1683ccd979bdSMark Fasheh 
1684ccd979bdSMark Fasheh 	status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1685ccd979bdSMark Fasheh 					      group, group_bh,
1686ccd979bdSMark Fasheh 					      start_bit, count);
1687ccd979bdSMark Fasheh 	if (status < 0) {
1688ccd979bdSMark Fasheh 		mlog_errno(status);
1689ccd979bdSMark Fasheh 		goto bail;
1690ccd979bdSMark Fasheh 	}
1691ccd979bdSMark Fasheh 
1692ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
1693ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
1694ccd979bdSMark Fasheh 	if (status < 0) {
1695ccd979bdSMark Fasheh 		mlog_errno(status);
1696ccd979bdSMark Fasheh 		goto bail;
1697ccd979bdSMark Fasheh 	}
1698ccd979bdSMark Fasheh 
1699ccd979bdSMark Fasheh 	le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
1700ccd979bdSMark Fasheh 		     count);
1701ccd979bdSMark Fasheh 	tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1702ccd979bdSMark Fasheh 	fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
1703ccd979bdSMark Fasheh 
1704ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, alloc_bh);
1705ccd979bdSMark Fasheh 	if (status < 0) {
1706ccd979bdSMark Fasheh 		mlog_errno(status);
1707ccd979bdSMark Fasheh 		goto bail;
1708ccd979bdSMark Fasheh 	}
1709ccd979bdSMark Fasheh 
1710ccd979bdSMark Fasheh bail:
1711ccd979bdSMark Fasheh 	if (group_bh)
1712ccd979bdSMark Fasheh 		brelse(group_bh);
1713ccd979bdSMark Fasheh 
1714ccd979bdSMark Fasheh 	mlog_exit(status);
1715ccd979bdSMark Fasheh 	return status;
1716ccd979bdSMark Fasheh }
1717ccd979bdSMark Fasheh 
17181fabe148SMark Fasheh int ocfs2_free_dinode(handle_t *handle,
1719ccd979bdSMark Fasheh 		      struct inode *inode_alloc_inode,
1720ccd979bdSMark Fasheh 		      struct buffer_head *inode_alloc_bh,
1721ccd979bdSMark Fasheh 		      struct ocfs2_dinode *di)
1722ccd979bdSMark Fasheh {
1723ccd979bdSMark Fasheh 	u64 blk = le64_to_cpu(di->i_blkno);
1724ccd979bdSMark Fasheh 	u16 bit = le16_to_cpu(di->i_suballoc_bit);
1725ccd979bdSMark Fasheh 	u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1726ccd979bdSMark Fasheh 
1727ccd979bdSMark Fasheh 	return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
1728ccd979bdSMark Fasheh 					inode_alloc_bh, bit, bg_blkno, 1);
1729ccd979bdSMark Fasheh }
1730ccd979bdSMark Fasheh 
17311fabe148SMark Fasheh int ocfs2_free_clusters(handle_t *handle,
1732ccd979bdSMark Fasheh 		       struct inode *bitmap_inode,
1733ccd979bdSMark Fasheh 		       struct buffer_head *bitmap_bh,
1734ccd979bdSMark Fasheh 		       u64 start_blk,
1735ccd979bdSMark Fasheh 		       unsigned int num_clusters)
1736ccd979bdSMark Fasheh {
1737ccd979bdSMark Fasheh 	int status;
1738ccd979bdSMark Fasheh 	u16 bg_start_bit;
1739ccd979bdSMark Fasheh 	u64 bg_blkno;
1740ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
1741ccd979bdSMark Fasheh 
1742ccd979bdSMark Fasheh 	/* You can't ever have a contiguous set of clusters
1743ccd979bdSMark Fasheh 	 * bigger than a block group bitmap so we never have to worry
1744ccd979bdSMark Fasheh 	 * about looping on them. */
1745ccd979bdSMark Fasheh 
1746ccd979bdSMark Fasheh 	mlog_entry_void();
1747ccd979bdSMark Fasheh 
1748ccd979bdSMark Fasheh 	/* This is expensive. We can safely remove once this stuff has
1749ccd979bdSMark Fasheh 	 * gotten tested really well. */
1750ccd979bdSMark Fasheh 	BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
1751ccd979bdSMark Fasheh 
1752ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
1753ccd979bdSMark Fasheh 
1754ccd979bdSMark Fasheh 	ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
1755ccd979bdSMark Fasheh 				     &bg_start_bit);
1756ccd979bdSMark Fasheh 
1757b0697053SMark Fasheh 	mlog(0, "want to free %u clusters starting at block %llu\n",
1758b0697053SMark Fasheh 	     num_clusters, (unsigned long long)start_blk);
1759b0697053SMark Fasheh 	mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1760b0697053SMark Fasheh 	     (unsigned long long)bg_blkno, bg_start_bit);
1761ccd979bdSMark Fasheh 
1762ccd979bdSMark Fasheh 	status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
1763ccd979bdSMark Fasheh 					  bg_start_bit, bg_blkno,
1764ccd979bdSMark Fasheh 					  num_clusters);
1765ccd979bdSMark Fasheh 	if (status < 0)
1766ccd979bdSMark Fasheh 		mlog_errno(status);
1767ccd979bdSMark Fasheh 
1768ccd979bdSMark Fasheh 	mlog_exit(status);
1769ccd979bdSMark Fasheh 	return status;
1770ccd979bdSMark Fasheh }
1771ccd979bdSMark Fasheh 
1772ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
1773ccd979bdSMark Fasheh {
1774ccd979bdSMark Fasheh 	printk("Block Group:\n");
1775ccd979bdSMark Fasheh 	printk("bg_signature:       %s\n", bg->bg_signature);
1776ccd979bdSMark Fasheh 	printk("bg_size:            %u\n", bg->bg_size);
1777ccd979bdSMark Fasheh 	printk("bg_bits:            %u\n", bg->bg_bits);
1778ccd979bdSMark Fasheh 	printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
1779ccd979bdSMark Fasheh 	printk("bg_chain:           %u\n", bg->bg_chain);
1780ccd979bdSMark Fasheh 	printk("bg_generation:      %u\n", le32_to_cpu(bg->bg_generation));
1781b0697053SMark Fasheh 	printk("bg_next_group:      %llu\n",
1782b0697053SMark Fasheh 	       (unsigned long long)bg->bg_next_group);
1783b0697053SMark Fasheh 	printk("bg_parent_dinode:   %llu\n",
1784b0697053SMark Fasheh 	       (unsigned long long)bg->bg_parent_dinode);
1785b0697053SMark Fasheh 	printk("bg_blkno:           %llu\n",
1786b0697053SMark Fasheh 	       (unsigned long long)bg->bg_blkno);
1787ccd979bdSMark Fasheh }
1788ccd979bdSMark Fasheh 
1789ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
1790ccd979bdSMark Fasheh {
1791ccd979bdSMark Fasheh 	int i;
1792ccd979bdSMark Fasheh 
1793b0697053SMark Fasheh 	printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
1794ccd979bdSMark Fasheh 	printk("i_signature:                  %s\n", fe->i_signature);
1795b0697053SMark Fasheh 	printk("i_size:                       %llu\n",
1796b0697053SMark Fasheh 	       (unsigned long long)fe->i_size);
1797ccd979bdSMark Fasheh 	printk("i_clusters:                   %u\n", fe->i_clusters);
1798ccd979bdSMark Fasheh 	printk("i_generation:                 %u\n",
1799ccd979bdSMark Fasheh 	       le32_to_cpu(fe->i_generation));
1800ccd979bdSMark Fasheh 	printk("id1.bitmap1.i_used:           %u\n",
1801ccd979bdSMark Fasheh 	       le32_to_cpu(fe->id1.bitmap1.i_used));
1802ccd979bdSMark Fasheh 	printk("id1.bitmap1.i_total:          %u\n",
1803ccd979bdSMark Fasheh 	       le32_to_cpu(fe->id1.bitmap1.i_total));
1804ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_cpg:           %u\n", fe->id2.i_chain.cl_cpg);
1805ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_bpc:           %u\n", fe->id2.i_chain.cl_bpc);
1806ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_count:         %u\n", fe->id2.i_chain.cl_count);
1807ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_next_free_rec: %u\n",
1808ccd979bdSMark Fasheh 	       fe->id2.i_chain.cl_next_free_rec);
1809ccd979bdSMark Fasheh 	for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
1810ccd979bdSMark Fasheh 		printk("fe->id2.i_chain.cl_recs[%d].c_free:  %u\n", i,
1811ccd979bdSMark Fasheh 		       fe->id2.i_chain.cl_recs[i].c_free);
1812ccd979bdSMark Fasheh 		printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
1813ccd979bdSMark Fasheh 		       fe->id2.i_chain.cl_recs[i].c_total);
1814b0697053SMark Fasheh 		printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
1815b0697053SMark Fasheh 		       (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
1816ccd979bdSMark Fasheh 	}
1817ccd979bdSMark Fasheh }
1818