xref: /openbmc/linux/fs/ocfs2/suballoc.c (revision e7d4cb6bc19658646357eeff134645cd9bc3479f)
1ccd979bdSMark Fasheh /* -*- mode: c; c-basic-offset: 8; -*-
2ccd979bdSMark Fasheh  * vim: noexpandtab sw=8 ts=8 sts=0:
3ccd979bdSMark Fasheh  *
4ccd979bdSMark Fasheh  * suballoc.c
5ccd979bdSMark Fasheh  *
6ccd979bdSMark Fasheh  * metadata alloc and free
7ccd979bdSMark Fasheh  * Inspired by ext3 block groups.
8ccd979bdSMark Fasheh  *
9ccd979bdSMark Fasheh  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
10ccd979bdSMark Fasheh  *
11ccd979bdSMark Fasheh  * This program is free software; you can redistribute it and/or
12ccd979bdSMark Fasheh  * modify it under the terms of the GNU General Public
13ccd979bdSMark Fasheh  * License as published by the Free Software Foundation; either
14ccd979bdSMark Fasheh  * version 2 of the License, or (at your option) any later version.
15ccd979bdSMark Fasheh  *
16ccd979bdSMark Fasheh  * This program is distributed in the hope that it will be useful,
17ccd979bdSMark Fasheh  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18ccd979bdSMark Fasheh  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19ccd979bdSMark Fasheh  * General Public License for more details.
20ccd979bdSMark Fasheh  *
21ccd979bdSMark Fasheh  * You should have received a copy of the GNU General Public
22ccd979bdSMark Fasheh  * License along with this program; if not, write to the
23ccd979bdSMark Fasheh  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24ccd979bdSMark Fasheh  * Boston, MA 021110-1307, USA.
25ccd979bdSMark Fasheh  */
26ccd979bdSMark Fasheh 
27ccd979bdSMark Fasheh #include <linux/fs.h>
28ccd979bdSMark Fasheh #include <linux/types.h>
29ccd979bdSMark Fasheh #include <linux/slab.h>
30ccd979bdSMark Fasheh #include <linux/highmem.h>
31ccd979bdSMark Fasheh 
32ccd979bdSMark Fasheh #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33ccd979bdSMark Fasheh #include <cluster/masklog.h>
34ccd979bdSMark Fasheh 
35ccd979bdSMark Fasheh #include "ocfs2.h"
36ccd979bdSMark Fasheh 
37ccd979bdSMark Fasheh #include "alloc.h"
38ccd979bdSMark Fasheh #include "dlmglue.h"
39ccd979bdSMark Fasheh #include "inode.h"
40ccd979bdSMark Fasheh #include "journal.h"
41ccd979bdSMark Fasheh #include "localalloc.h"
42ccd979bdSMark Fasheh #include "suballoc.h"
43ccd979bdSMark Fasheh #include "super.h"
44ccd979bdSMark Fasheh #include "sysfile.h"
45ccd979bdSMark Fasheh #include "uptodate.h"
46ccd979bdSMark Fasheh 
47ccd979bdSMark Fasheh #include "buffer_head_io.h"
48ccd979bdSMark Fasheh 
49ffda89a3STao Ma #define NOT_ALLOC_NEW_GROUP		0
50ffda89a3STao Ma #define ALLOC_NEW_GROUP			1
51ffda89a3STao Ma 
524d0ddb2cSTao Ma #define OCFS2_MAX_INODES_TO_STEAL	1024
534d0ddb2cSTao Ma 
54ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
55ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
56ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
571fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle,
58ccd979bdSMark Fasheh 				  struct inode *alloc_inode,
59ccd979bdSMark Fasheh 				  struct buffer_head *bg_bh,
60ccd979bdSMark Fasheh 				  u64 group_blkno,
61ccd979bdSMark Fasheh 				  u16 my_chain,
62ccd979bdSMark Fasheh 				  struct ocfs2_chain_list *cl);
63ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
64ccd979bdSMark Fasheh 				   struct inode *alloc_inode,
65ccd979bdSMark Fasheh 				   struct buffer_head *bh);
66ccd979bdSMark Fasheh 
67ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode,
68ccd979bdSMark Fasheh 				      struct buffer_head *group_bh,
69ccd979bdSMark Fasheh 				      u32 bits_wanted, u32 min_bits,
70ccd979bdSMark Fasheh 				      u16 *bit_off, u16 *bits_found);
71ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode,
72ccd979bdSMark Fasheh 				    struct buffer_head *group_bh,
73ccd979bdSMark Fasheh 				    u32 bits_wanted, u32 min_bits,
74ccd979bdSMark Fasheh 				    u16 *bit_off, u16 *bits_found);
75ccd979bdSMark Fasheh static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
76ccd979bdSMark Fasheh 				     struct ocfs2_alloc_context *ac,
771fabe148SMark Fasheh 				     handle_t *handle,
78ccd979bdSMark Fasheh 				     u32 bits_wanted,
79ccd979bdSMark Fasheh 				     u32 min_bits,
80ccd979bdSMark Fasheh 				     u16 *bit_off,
81ccd979bdSMark Fasheh 				     unsigned int *num_bits,
82ccd979bdSMark Fasheh 				     u64 *bg_blkno);
83ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
84ccd979bdSMark Fasheh 					 int nr);
851fabe148SMark Fasheh static inline int ocfs2_block_group_set_bits(handle_t *handle,
86ccd979bdSMark Fasheh 					     struct inode *alloc_inode,
87ccd979bdSMark Fasheh 					     struct ocfs2_group_desc *bg,
88ccd979bdSMark Fasheh 					     struct buffer_head *group_bh,
89ccd979bdSMark Fasheh 					     unsigned int bit_off,
90ccd979bdSMark Fasheh 					     unsigned int num_bits);
911fabe148SMark Fasheh static inline int ocfs2_block_group_clear_bits(handle_t *handle,
92ccd979bdSMark Fasheh 					       struct inode *alloc_inode,
93ccd979bdSMark Fasheh 					       struct ocfs2_group_desc *bg,
94ccd979bdSMark Fasheh 					       struct buffer_head *group_bh,
95ccd979bdSMark Fasheh 					       unsigned int bit_off,
96ccd979bdSMark Fasheh 					       unsigned int num_bits);
97ccd979bdSMark Fasheh 
981fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle,
99ccd979bdSMark Fasheh 				    struct inode *alloc_inode,
100ccd979bdSMark Fasheh 				    struct buffer_head *fe_bh,
101ccd979bdSMark Fasheh 				    struct buffer_head *bg_bh,
102ccd979bdSMark Fasheh 				    struct buffer_head *prev_bg_bh,
103ccd979bdSMark Fasheh 				    u16 chain);
104ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
105ccd979bdSMark Fasheh 						     u32 wanted);
106ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
107ccd979bdSMark Fasheh 						   u64 bg_blkno,
108ccd979bdSMark Fasheh 						   u16 bg_bit_off);
109ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode,
110ccd979bdSMark Fasheh 						u64 data_blkno,
111ccd979bdSMark Fasheh 						u64 *bg_blkno,
112ccd979bdSMark Fasheh 						u16 *bg_bit_off);
113ccd979bdSMark Fasheh 
1149c7af40bSMark Fasheh void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
115ccd979bdSMark Fasheh {
116da5cbf2fSMark Fasheh 	struct inode *inode = ac->ac_inode;
117da5cbf2fSMark Fasheh 
118da5cbf2fSMark Fasheh 	if (inode) {
119da5cbf2fSMark Fasheh 		if (ac->ac_which != OCFS2_AC_USE_LOCAL)
120e63aecb6SMark Fasheh 			ocfs2_inode_unlock(inode, 1);
121da5cbf2fSMark Fasheh 
122da5cbf2fSMark Fasheh 		mutex_unlock(&inode->i_mutex);
123da5cbf2fSMark Fasheh 
124da5cbf2fSMark Fasheh 		iput(inode);
1254d0ddb2cSTao Ma 		ac->ac_inode = NULL;
126da5cbf2fSMark Fasheh 	}
1274d0ddb2cSTao Ma 	if (ac->ac_bh) {
128ccd979bdSMark Fasheh 		brelse(ac->ac_bh);
1294d0ddb2cSTao Ma 		ac->ac_bh = NULL;
1304d0ddb2cSTao Ma 	}
1314d0ddb2cSTao Ma }
1324d0ddb2cSTao Ma 
1334d0ddb2cSTao Ma void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
1344d0ddb2cSTao Ma {
1354d0ddb2cSTao Ma 	ocfs2_free_ac_resource(ac);
136ccd979bdSMark Fasheh 	kfree(ac);
137ccd979bdSMark Fasheh }
138ccd979bdSMark Fasheh 
139ccd979bdSMark Fasheh static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
140ccd979bdSMark Fasheh {
141ccd979bdSMark Fasheh 	return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
142ccd979bdSMark Fasheh }
143ccd979bdSMark Fasheh 
1447bf72edeSMark Fasheh /* somewhat more expensive than our other checks, so use sparingly. */
145d659072fSTao Ma int ocfs2_check_group_descriptor(struct super_block *sb,
1467bf72edeSMark Fasheh 				 struct ocfs2_dinode *di,
1477bf72edeSMark Fasheh 				 struct ocfs2_group_desc *gd)
1487bf72edeSMark Fasheh {
1497bf72edeSMark Fasheh 	unsigned int max_bits;
1507bf72edeSMark Fasheh 
1517bf72edeSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1527bf72edeSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(sb, gd);
1537bf72edeSMark Fasheh 		return -EIO;
1547bf72edeSMark Fasheh 	}
1557bf72edeSMark Fasheh 
1567bf72edeSMark Fasheh 	if (di->i_blkno != gd->bg_parent_dinode) {
1577bf72edeSMark Fasheh 		ocfs2_error(sb, "Group descriptor # %llu has bad parent "
1587bf72edeSMark Fasheh 			    "pointer (%llu, expected %llu)",
1597bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_blkno),
1607bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
1617bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(di->i_blkno));
1627bf72edeSMark Fasheh 		return -EIO;
1637bf72edeSMark Fasheh 	}
1647bf72edeSMark Fasheh 
1657bf72edeSMark Fasheh 	max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
1667bf72edeSMark Fasheh 	if (le16_to_cpu(gd->bg_bits) > max_bits) {
1677bf72edeSMark Fasheh 		ocfs2_error(sb, "Group descriptor # %llu has bit count of %u",
1687bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_blkno),
1697bf72edeSMark Fasheh 			    le16_to_cpu(gd->bg_bits));
1707bf72edeSMark Fasheh 		return -EIO;
1717bf72edeSMark Fasheh 	}
1727bf72edeSMark Fasheh 
1737bf72edeSMark Fasheh 	if (le16_to_cpu(gd->bg_chain) >=
1747bf72edeSMark Fasheh 	    le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
1757bf72edeSMark Fasheh 		ocfs2_error(sb, "Group descriptor # %llu has bad chain %u",
1767bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_blkno),
1777bf72edeSMark Fasheh 			    le16_to_cpu(gd->bg_chain));
1787bf72edeSMark Fasheh 		return -EIO;
1797bf72edeSMark Fasheh 	}
1807bf72edeSMark Fasheh 
1817bf72edeSMark Fasheh 	if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
1827bf72edeSMark Fasheh 		ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
1837bf72edeSMark Fasheh 			    "claims that %u are free",
1847bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_blkno),
1857bf72edeSMark Fasheh 			    le16_to_cpu(gd->bg_bits),
1867bf72edeSMark Fasheh 			    le16_to_cpu(gd->bg_free_bits_count));
1877bf72edeSMark Fasheh 		return -EIO;
1887bf72edeSMark Fasheh 	}
1897bf72edeSMark Fasheh 
1907bf72edeSMark Fasheh 	if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
1917bf72edeSMark Fasheh 		ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
1927bf72edeSMark Fasheh 			    "max bitmap bits of %u",
1937bf72edeSMark Fasheh 			    (unsigned long long)le64_to_cpu(gd->bg_blkno),
1947bf72edeSMark Fasheh 			    le16_to_cpu(gd->bg_bits),
1957bf72edeSMark Fasheh 			    8 * le16_to_cpu(gd->bg_size));
1967bf72edeSMark Fasheh 		return -EIO;
1977bf72edeSMark Fasheh 	}
1987bf72edeSMark Fasheh 
1997bf72edeSMark Fasheh 	return 0;
2007bf72edeSMark Fasheh }
2017bf72edeSMark Fasheh 
2021fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle,
203ccd979bdSMark Fasheh 				  struct inode *alloc_inode,
204ccd979bdSMark Fasheh 				  struct buffer_head *bg_bh,
205ccd979bdSMark Fasheh 				  u64 group_blkno,
206ccd979bdSMark Fasheh 				  u16 my_chain,
207ccd979bdSMark Fasheh 				  struct ocfs2_chain_list *cl)
208ccd979bdSMark Fasheh {
209ccd979bdSMark Fasheh 	int status = 0;
210ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
211ccd979bdSMark Fasheh 	struct super_block * sb = alloc_inode->i_sb;
212ccd979bdSMark Fasheh 
213ccd979bdSMark Fasheh 	mlog_entry_void();
214ccd979bdSMark Fasheh 
215ccd979bdSMark Fasheh 	if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
216b0697053SMark Fasheh 		ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
217b0697053SMark Fasheh 			    "b_blocknr (%llu)",
218b0697053SMark Fasheh 			    (unsigned long long)group_blkno,
219ccd979bdSMark Fasheh 			    (unsigned long long) bg_bh->b_blocknr);
220ccd979bdSMark Fasheh 		status = -EIO;
221ccd979bdSMark Fasheh 		goto bail;
222ccd979bdSMark Fasheh 	}
223ccd979bdSMark Fasheh 
224ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle,
225ccd979bdSMark Fasheh 				      alloc_inode,
226ccd979bdSMark Fasheh 				      bg_bh,
227ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_CREATE);
228ccd979bdSMark Fasheh 	if (status < 0) {
229ccd979bdSMark Fasheh 		mlog_errno(status);
230ccd979bdSMark Fasheh 		goto bail;
231ccd979bdSMark Fasheh 	}
232ccd979bdSMark Fasheh 
233ccd979bdSMark Fasheh 	memset(bg, 0, sb->s_blocksize);
234ccd979bdSMark Fasheh 	strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
235ccd979bdSMark Fasheh 	bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
236ccd979bdSMark Fasheh 	bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
237ccd979bdSMark Fasheh 	bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
238ccd979bdSMark Fasheh 	bg->bg_chain = cpu_to_le16(my_chain);
239ccd979bdSMark Fasheh 	bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
240ccd979bdSMark Fasheh 	bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
241ccd979bdSMark Fasheh 	bg->bg_blkno = cpu_to_le64(group_blkno);
242ccd979bdSMark Fasheh 	/* set the 1st bit in the bitmap to account for the descriptor block */
243ccd979bdSMark Fasheh 	ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
244ccd979bdSMark Fasheh 	bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
245ccd979bdSMark Fasheh 
246ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, bg_bh);
247ccd979bdSMark Fasheh 	if (status < 0)
248ccd979bdSMark Fasheh 		mlog_errno(status);
249ccd979bdSMark Fasheh 
250ccd979bdSMark Fasheh 	/* There is no need to zero out or otherwise initialize the
251ccd979bdSMark Fasheh 	 * other blocks in a group - All valid FS metadata in a block
252ccd979bdSMark Fasheh 	 * group stores the superblock fs_generation value at
253ccd979bdSMark Fasheh 	 * allocation time. */
254ccd979bdSMark Fasheh 
255ccd979bdSMark Fasheh bail:
256ccd979bdSMark Fasheh 	mlog_exit(status);
257ccd979bdSMark Fasheh 	return status;
258ccd979bdSMark Fasheh }
259ccd979bdSMark Fasheh 
260ccd979bdSMark Fasheh static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
261ccd979bdSMark Fasheh {
262ccd979bdSMark Fasheh 	u16 curr, best;
263ccd979bdSMark Fasheh 
264ccd979bdSMark Fasheh 	best = curr = 0;
265ccd979bdSMark Fasheh 	while (curr < le16_to_cpu(cl->cl_count)) {
266ccd979bdSMark Fasheh 		if (le32_to_cpu(cl->cl_recs[best].c_total) >
267ccd979bdSMark Fasheh 		    le32_to_cpu(cl->cl_recs[curr].c_total))
268ccd979bdSMark Fasheh 			best = curr;
269ccd979bdSMark Fasheh 		curr++;
270ccd979bdSMark Fasheh 	}
271ccd979bdSMark Fasheh 	return best;
272ccd979bdSMark Fasheh }
273ccd979bdSMark Fasheh 
274ccd979bdSMark Fasheh /*
275ccd979bdSMark Fasheh  * We expect the block group allocator to already be locked.
276ccd979bdSMark Fasheh  */
277ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
278ccd979bdSMark Fasheh 				   struct inode *alloc_inode,
279ccd979bdSMark Fasheh 				   struct buffer_head *bh)
280ccd979bdSMark Fasheh {
281ccd979bdSMark Fasheh 	int status, credits;
282ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
283ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl;
284ccd979bdSMark Fasheh 	struct ocfs2_alloc_context *ac = NULL;
2851fabe148SMark Fasheh 	handle_t *handle = NULL;
286ccd979bdSMark Fasheh 	u32 bit_off, num_bits;
287ccd979bdSMark Fasheh 	u16 alloc_rec;
288ccd979bdSMark Fasheh 	u64 bg_blkno;
289ccd979bdSMark Fasheh 	struct buffer_head *bg_bh = NULL;
290ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg;
291ccd979bdSMark Fasheh 
292ccd979bdSMark Fasheh 	BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
293ccd979bdSMark Fasheh 
294ccd979bdSMark Fasheh 	mlog_entry_void();
295ccd979bdSMark Fasheh 
296ccd979bdSMark Fasheh 	cl = &fe->id2.i_chain;
297ccd979bdSMark Fasheh 	status = ocfs2_reserve_clusters(osb,
298ccd979bdSMark Fasheh 					le16_to_cpu(cl->cl_cpg),
299ccd979bdSMark Fasheh 					&ac);
300ccd979bdSMark Fasheh 	if (status < 0) {
301ccd979bdSMark Fasheh 		if (status != -ENOSPC)
302ccd979bdSMark Fasheh 			mlog_errno(status);
303ccd979bdSMark Fasheh 		goto bail;
304ccd979bdSMark Fasheh 	}
305ccd979bdSMark Fasheh 
306ccd979bdSMark Fasheh 	credits = ocfs2_calc_group_alloc_credits(osb->sb,
307ccd979bdSMark Fasheh 						 le16_to_cpu(cl->cl_cpg));
30865eff9ccSMark Fasheh 	handle = ocfs2_start_trans(osb, credits);
309ccd979bdSMark Fasheh 	if (IS_ERR(handle)) {
310ccd979bdSMark Fasheh 		status = PTR_ERR(handle);
311ccd979bdSMark Fasheh 		handle = NULL;
312ccd979bdSMark Fasheh 		mlog_errno(status);
313ccd979bdSMark Fasheh 		goto bail;
314ccd979bdSMark Fasheh 	}
315ccd979bdSMark Fasheh 
316ccd979bdSMark Fasheh 	status = ocfs2_claim_clusters(osb,
317ccd979bdSMark Fasheh 				      handle,
318ccd979bdSMark Fasheh 				      ac,
319ccd979bdSMark Fasheh 				      le16_to_cpu(cl->cl_cpg),
320ccd979bdSMark Fasheh 				      &bit_off,
321ccd979bdSMark Fasheh 				      &num_bits);
322ccd979bdSMark Fasheh 	if (status < 0) {
323ccd979bdSMark Fasheh 		if (status != -ENOSPC)
324ccd979bdSMark Fasheh 			mlog_errno(status);
325ccd979bdSMark Fasheh 		goto bail;
326ccd979bdSMark Fasheh 	}
327ccd979bdSMark Fasheh 
328ccd979bdSMark Fasheh 	alloc_rec = ocfs2_find_smallest_chain(cl);
329ccd979bdSMark Fasheh 
330ccd979bdSMark Fasheh 	/* setup the group */
331ccd979bdSMark Fasheh 	bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
332b0697053SMark Fasheh 	mlog(0, "new descriptor, record %u, at block %llu\n",
333b0697053SMark Fasheh 	     alloc_rec, (unsigned long long)bg_blkno);
334ccd979bdSMark Fasheh 
335ccd979bdSMark Fasheh 	bg_bh = sb_getblk(osb->sb, bg_blkno);
336ccd979bdSMark Fasheh 	if (!bg_bh) {
337ccd979bdSMark Fasheh 		status = -EIO;
338ccd979bdSMark Fasheh 		mlog_errno(status);
339ccd979bdSMark Fasheh 		goto bail;
340ccd979bdSMark Fasheh 	}
341ccd979bdSMark Fasheh 	ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
342ccd979bdSMark Fasheh 
343ccd979bdSMark Fasheh 	status = ocfs2_block_group_fill(handle,
344ccd979bdSMark Fasheh 					alloc_inode,
345ccd979bdSMark Fasheh 					bg_bh,
346ccd979bdSMark Fasheh 					bg_blkno,
347ccd979bdSMark Fasheh 					alloc_rec,
348ccd979bdSMark Fasheh 					cl);
349ccd979bdSMark Fasheh 	if (status < 0) {
350ccd979bdSMark Fasheh 		mlog_errno(status);
351ccd979bdSMark Fasheh 		goto bail;
352ccd979bdSMark Fasheh 	}
353ccd979bdSMark Fasheh 
354ccd979bdSMark Fasheh 	bg = (struct ocfs2_group_desc *) bg_bh->b_data;
355ccd979bdSMark Fasheh 
356ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode,
357ccd979bdSMark Fasheh 				      bh, OCFS2_JOURNAL_ACCESS_WRITE);
358ccd979bdSMark Fasheh 	if (status < 0) {
359ccd979bdSMark Fasheh 		mlog_errno(status);
360ccd979bdSMark Fasheh 		goto bail;
361ccd979bdSMark Fasheh 	}
362ccd979bdSMark Fasheh 
363ccd979bdSMark Fasheh 	le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
364ccd979bdSMark Fasheh 		     le16_to_cpu(bg->bg_free_bits_count));
365ccd979bdSMark Fasheh 	le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
366ccd979bdSMark Fasheh 	cl->cl_recs[alloc_rec].c_blkno  = cpu_to_le64(bg_blkno);
367ccd979bdSMark Fasheh 	if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
368ccd979bdSMark Fasheh 		le16_add_cpu(&cl->cl_next_free_rec, 1);
369ccd979bdSMark Fasheh 
370ccd979bdSMark Fasheh 	le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
371ccd979bdSMark Fasheh 					le16_to_cpu(bg->bg_free_bits_count));
372ccd979bdSMark Fasheh 	le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
373ccd979bdSMark Fasheh 	le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
374ccd979bdSMark Fasheh 
375ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, bh);
376ccd979bdSMark Fasheh 	if (status < 0) {
377ccd979bdSMark Fasheh 		mlog_errno(status);
378ccd979bdSMark Fasheh 		goto bail;
379ccd979bdSMark Fasheh 	}
380ccd979bdSMark Fasheh 
381ccd979bdSMark Fasheh 	spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
382ccd979bdSMark Fasheh 	OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
383ccd979bdSMark Fasheh 	fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
384ccd979bdSMark Fasheh 					     le32_to_cpu(fe->i_clusters)));
385ccd979bdSMark Fasheh 	spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
386ccd979bdSMark Fasheh 	i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
3878110b073SMark Fasheh 	alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
388ccd979bdSMark Fasheh 
389ccd979bdSMark Fasheh 	status = 0;
390ccd979bdSMark Fasheh bail:
391ccd979bdSMark Fasheh 	if (handle)
39202dc1af4SMark Fasheh 		ocfs2_commit_trans(osb, handle);
393ccd979bdSMark Fasheh 
394ccd979bdSMark Fasheh 	if (ac)
395ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(ac);
396ccd979bdSMark Fasheh 
397ccd979bdSMark Fasheh 	if (bg_bh)
398ccd979bdSMark Fasheh 		brelse(bg_bh);
399ccd979bdSMark Fasheh 
400ccd979bdSMark Fasheh 	mlog_exit(status);
401ccd979bdSMark Fasheh 	return status;
402ccd979bdSMark Fasheh }
403ccd979bdSMark Fasheh 
404ccd979bdSMark Fasheh static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
405da5cbf2fSMark Fasheh 				       struct ocfs2_alloc_context *ac,
406da5cbf2fSMark Fasheh 				       int type,
407ffda89a3STao Ma 				       u32 slot,
408ffda89a3STao Ma 				       int alloc_new_group)
409ccd979bdSMark Fasheh {
410ccd979bdSMark Fasheh 	int status;
411ccd979bdSMark Fasheh 	u32 bits_wanted = ac->ac_bits_wanted;
412da5cbf2fSMark Fasheh 	struct inode *alloc_inode;
413ccd979bdSMark Fasheh 	struct buffer_head *bh = NULL;
414ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
415ccd979bdSMark Fasheh 	u32 free_bits;
416ccd979bdSMark Fasheh 
417ccd979bdSMark Fasheh 	mlog_entry_void();
418ccd979bdSMark Fasheh 
419da5cbf2fSMark Fasheh 	alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
420da5cbf2fSMark Fasheh 	if (!alloc_inode) {
421da5cbf2fSMark Fasheh 		mlog_errno(-EINVAL);
422da5cbf2fSMark Fasheh 		return -EINVAL;
423ccd979bdSMark Fasheh 	}
424ccd979bdSMark Fasheh 
425da5cbf2fSMark Fasheh 	mutex_lock(&alloc_inode->i_mutex);
426da5cbf2fSMark Fasheh 
427e63aecb6SMark Fasheh 	status = ocfs2_inode_lock(alloc_inode, &bh, 1);
428da5cbf2fSMark Fasheh 	if (status < 0) {
429da5cbf2fSMark Fasheh 		mutex_unlock(&alloc_inode->i_mutex);
430da5cbf2fSMark Fasheh 		iput(alloc_inode);
431da5cbf2fSMark Fasheh 
432da5cbf2fSMark Fasheh 		mlog_errno(status);
433da5cbf2fSMark Fasheh 		return status;
434da5cbf2fSMark Fasheh 	}
435da5cbf2fSMark Fasheh 
436da5cbf2fSMark Fasheh 	ac->ac_inode = alloc_inode;
437a4a48911STao Ma 	ac->ac_alloc_slot = slot;
438da5cbf2fSMark Fasheh 
439ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) bh->b_data;
440ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
441ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
442ccd979bdSMark Fasheh 		status = -EIO;
443ccd979bdSMark Fasheh 		goto bail;
444ccd979bdSMark Fasheh 	}
445ccd979bdSMark Fasheh 	if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
446b0697053SMark Fasheh 		ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
447b0697053SMark Fasheh 			    (unsigned long long)le64_to_cpu(fe->i_blkno));
448ccd979bdSMark Fasheh 		status = -EIO;
449ccd979bdSMark Fasheh 		goto bail;
450ccd979bdSMark Fasheh 	}
451ccd979bdSMark Fasheh 
452ccd979bdSMark Fasheh 	free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
453ccd979bdSMark Fasheh 		le32_to_cpu(fe->id1.bitmap1.i_used);
454ccd979bdSMark Fasheh 
455ccd979bdSMark Fasheh 	if (bits_wanted > free_bits) {
456ccd979bdSMark Fasheh 		/* cluster bitmap never grows */
457ccd979bdSMark Fasheh 		if (ocfs2_is_cluster_bitmap(alloc_inode)) {
458ccd979bdSMark Fasheh 			mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
459ccd979bdSMark Fasheh 			     bits_wanted, free_bits);
460ccd979bdSMark Fasheh 			status = -ENOSPC;
461ccd979bdSMark Fasheh 			goto bail;
462ccd979bdSMark Fasheh 		}
463ccd979bdSMark Fasheh 
464ffda89a3STao Ma 		if (alloc_new_group != ALLOC_NEW_GROUP) {
465ffda89a3STao Ma 			mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
466ffda89a3STao Ma 			     "and we don't alloc a new group for it.\n",
467ffda89a3STao Ma 			     slot, bits_wanted, free_bits);
468ffda89a3STao Ma 			status = -ENOSPC;
469ffda89a3STao Ma 			goto bail;
470ffda89a3STao Ma 		}
471ffda89a3STao Ma 
472ccd979bdSMark Fasheh 		status = ocfs2_block_group_alloc(osb, alloc_inode, bh);
473ccd979bdSMark Fasheh 		if (status < 0) {
474ccd979bdSMark Fasheh 			if (status != -ENOSPC)
475ccd979bdSMark Fasheh 				mlog_errno(status);
476ccd979bdSMark Fasheh 			goto bail;
477ccd979bdSMark Fasheh 		}
478ccd979bdSMark Fasheh 		atomic_inc(&osb->alloc_stats.bg_extends);
479ccd979bdSMark Fasheh 
480ccd979bdSMark Fasheh 		/* You should never ask for this much metadata */
481ccd979bdSMark Fasheh 		BUG_ON(bits_wanted >
482ccd979bdSMark Fasheh 		       (le32_to_cpu(fe->id1.bitmap1.i_total)
483ccd979bdSMark Fasheh 			- le32_to_cpu(fe->id1.bitmap1.i_used)));
484ccd979bdSMark Fasheh 	}
485ccd979bdSMark Fasheh 
486ccd979bdSMark Fasheh 	get_bh(bh);
487ccd979bdSMark Fasheh 	ac->ac_bh = bh;
488ccd979bdSMark Fasheh bail:
489ccd979bdSMark Fasheh 	if (bh)
490ccd979bdSMark Fasheh 		brelse(bh);
491ccd979bdSMark Fasheh 
492ccd979bdSMark Fasheh 	mlog_exit(status);
493ccd979bdSMark Fasheh 	return status;
494ccd979bdSMark Fasheh }
495ccd979bdSMark Fasheh 
496ccd979bdSMark Fasheh int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
497811f933dSTao Ma 			       struct ocfs2_extent_list *root_el,
498ccd979bdSMark Fasheh 			       struct ocfs2_alloc_context **ac)
499ccd979bdSMark Fasheh {
500ccd979bdSMark Fasheh 	int status;
501da5cbf2fSMark Fasheh 	u32 slot;
502ccd979bdSMark Fasheh 
503cd861280SRobert P. J. Day 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
504ccd979bdSMark Fasheh 	if (!(*ac)) {
505ccd979bdSMark Fasheh 		status = -ENOMEM;
506ccd979bdSMark Fasheh 		mlog_errno(status);
507ccd979bdSMark Fasheh 		goto bail;
508ccd979bdSMark Fasheh 	}
509ccd979bdSMark Fasheh 
510811f933dSTao Ma 	(*ac)->ac_bits_wanted = ocfs2_extend_meta_needed(root_el);
511ccd979bdSMark Fasheh 	(*ac)->ac_which = OCFS2_AC_USE_META;
512da5cbf2fSMark Fasheh 	slot = osb->slot_num;
513ccd979bdSMark Fasheh 	(*ac)->ac_group_search = ocfs2_block_group_search;
514ccd979bdSMark Fasheh 
515da5cbf2fSMark Fasheh 	status = ocfs2_reserve_suballoc_bits(osb, (*ac),
516ffda89a3STao Ma 					     EXTENT_ALLOC_SYSTEM_INODE,
517ffda89a3STao Ma 					     slot, ALLOC_NEW_GROUP);
518ccd979bdSMark Fasheh 	if (status < 0) {
519ccd979bdSMark Fasheh 		if (status != -ENOSPC)
520ccd979bdSMark Fasheh 			mlog_errno(status);
521ccd979bdSMark Fasheh 		goto bail;
522ccd979bdSMark Fasheh 	}
523ccd979bdSMark Fasheh 
524ccd979bdSMark Fasheh 	status = 0;
525ccd979bdSMark Fasheh bail:
526ccd979bdSMark Fasheh 	if ((status < 0) && *ac) {
527ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(*ac);
528ccd979bdSMark Fasheh 		*ac = NULL;
529ccd979bdSMark Fasheh 	}
530ccd979bdSMark Fasheh 
531ccd979bdSMark Fasheh 	mlog_exit(status);
532ccd979bdSMark Fasheh 	return status;
533ccd979bdSMark Fasheh }
534ccd979bdSMark Fasheh 
5354d0ddb2cSTao Ma static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
5364d0ddb2cSTao Ma 					      struct ocfs2_alloc_context *ac)
5374d0ddb2cSTao Ma {
5384d0ddb2cSTao Ma 	int i, status = -ENOSPC;
5394d0ddb2cSTao Ma 	s16 slot = ocfs2_get_inode_steal_slot(osb);
5404d0ddb2cSTao Ma 
5414d0ddb2cSTao Ma 	/* Start to steal inodes from the first slot after ours. */
5424d0ddb2cSTao Ma 	if (slot == OCFS2_INVALID_SLOT)
5434d0ddb2cSTao Ma 		slot = osb->slot_num + 1;
5444d0ddb2cSTao Ma 
5454d0ddb2cSTao Ma 	for (i = 0; i < osb->max_slots; i++, slot++) {
5464d0ddb2cSTao Ma 		if (slot == osb->max_slots)
5474d0ddb2cSTao Ma 			slot = 0;
5484d0ddb2cSTao Ma 
5494d0ddb2cSTao Ma 		if (slot == osb->slot_num)
5504d0ddb2cSTao Ma 			continue;
5514d0ddb2cSTao Ma 
5524d0ddb2cSTao Ma 		status = ocfs2_reserve_suballoc_bits(osb, ac,
5534d0ddb2cSTao Ma 						     INODE_ALLOC_SYSTEM_INODE,
5544d0ddb2cSTao Ma 						     slot, NOT_ALLOC_NEW_GROUP);
5554d0ddb2cSTao Ma 		if (status >= 0) {
5564d0ddb2cSTao Ma 			ocfs2_set_inode_steal_slot(osb, slot);
5574d0ddb2cSTao Ma 			break;
5584d0ddb2cSTao Ma 		}
5594d0ddb2cSTao Ma 
5604d0ddb2cSTao Ma 		ocfs2_free_ac_resource(ac);
5614d0ddb2cSTao Ma 	}
5624d0ddb2cSTao Ma 
5634d0ddb2cSTao Ma 	return status;
5644d0ddb2cSTao Ma }
5654d0ddb2cSTao Ma 
566ccd979bdSMark Fasheh int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
567ccd979bdSMark Fasheh 			    struct ocfs2_alloc_context **ac)
568ccd979bdSMark Fasheh {
569ccd979bdSMark Fasheh 	int status;
5704d0ddb2cSTao Ma 	s16 slot = ocfs2_get_inode_steal_slot(osb);
571ccd979bdSMark Fasheh 
572cd861280SRobert P. J. Day 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
573ccd979bdSMark Fasheh 	if (!(*ac)) {
574ccd979bdSMark Fasheh 		status = -ENOMEM;
575ccd979bdSMark Fasheh 		mlog_errno(status);
576ccd979bdSMark Fasheh 		goto bail;
577ccd979bdSMark Fasheh 	}
578ccd979bdSMark Fasheh 
579ccd979bdSMark Fasheh 	(*ac)->ac_bits_wanted = 1;
580ccd979bdSMark Fasheh 	(*ac)->ac_which = OCFS2_AC_USE_INODE;
581ccd979bdSMark Fasheh 
582ccd979bdSMark Fasheh 	(*ac)->ac_group_search = ocfs2_block_group_search;
583ccd979bdSMark Fasheh 
5844d0ddb2cSTao Ma 	/*
5854d0ddb2cSTao Ma 	 * slot is set when we successfully steal inode from other nodes.
5864d0ddb2cSTao Ma 	 * It is reset in 3 places:
5874d0ddb2cSTao Ma 	 * 1. when we flush the truncate log
5884d0ddb2cSTao Ma 	 * 2. when we complete local alloc recovery.
5894d0ddb2cSTao Ma 	 * 3. when we successfully allocate from our own slot.
5904d0ddb2cSTao Ma 	 * After it is set, we will go on stealing inodes until we find the
5914d0ddb2cSTao Ma 	 * need to check our slots to see whether there is some space for us.
5924d0ddb2cSTao Ma 	 */
5934d0ddb2cSTao Ma 	if (slot != OCFS2_INVALID_SLOT &&
5944d0ddb2cSTao Ma 	    atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_INODES_TO_STEAL)
5954d0ddb2cSTao Ma 		goto inode_steal;
5964d0ddb2cSTao Ma 
5974d0ddb2cSTao Ma 	atomic_set(&osb->s_num_inodes_stolen, 0);
598da5cbf2fSMark Fasheh 	status = ocfs2_reserve_suballoc_bits(osb, *ac,
599da5cbf2fSMark Fasheh 					     INODE_ALLOC_SYSTEM_INODE,
600ffda89a3STao Ma 					     osb->slot_num, ALLOC_NEW_GROUP);
6014d0ddb2cSTao Ma 	if (status >= 0) {
6024d0ddb2cSTao Ma 		status = 0;
6034d0ddb2cSTao Ma 
6044d0ddb2cSTao Ma 		/*
6054d0ddb2cSTao Ma 		 * Some inodes must be freed by us, so try to allocate
6064d0ddb2cSTao Ma 		 * from our own next time.
6074d0ddb2cSTao Ma 		 */
6084d0ddb2cSTao Ma 		if (slot != OCFS2_INVALID_SLOT)
6094d0ddb2cSTao Ma 			ocfs2_init_inode_steal_slot(osb);
6104d0ddb2cSTao Ma 		goto bail;
6114d0ddb2cSTao Ma 	} else if (status < 0 && status != -ENOSPC) {
6124d0ddb2cSTao Ma 		mlog_errno(status);
6134d0ddb2cSTao Ma 		goto bail;
6144d0ddb2cSTao Ma 	}
6154d0ddb2cSTao Ma 
6164d0ddb2cSTao Ma 	ocfs2_free_ac_resource(*ac);
6174d0ddb2cSTao Ma 
6184d0ddb2cSTao Ma inode_steal:
6194d0ddb2cSTao Ma 	status = ocfs2_steal_inode_from_other_nodes(osb, *ac);
6204d0ddb2cSTao Ma 	atomic_inc(&osb->s_num_inodes_stolen);
621ccd979bdSMark Fasheh 	if (status < 0) {
622ccd979bdSMark Fasheh 		if (status != -ENOSPC)
623ccd979bdSMark Fasheh 			mlog_errno(status);
624ccd979bdSMark Fasheh 		goto bail;
625ccd979bdSMark Fasheh 	}
626ccd979bdSMark Fasheh 
627ccd979bdSMark Fasheh 	status = 0;
628ccd979bdSMark Fasheh bail:
629ccd979bdSMark Fasheh 	if ((status < 0) && *ac) {
630ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(*ac);
631ccd979bdSMark Fasheh 		*ac = NULL;
632ccd979bdSMark Fasheh 	}
633ccd979bdSMark Fasheh 
634ccd979bdSMark Fasheh 	mlog_exit(status);
635ccd979bdSMark Fasheh 	return status;
636ccd979bdSMark Fasheh }
637ccd979bdSMark Fasheh 
638ccd979bdSMark Fasheh /* local alloc code has to do the same thing, so rather than do this
639ccd979bdSMark Fasheh  * twice.. */
640ccd979bdSMark Fasheh int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
641ccd979bdSMark Fasheh 				      struct ocfs2_alloc_context *ac)
642ccd979bdSMark Fasheh {
643ccd979bdSMark Fasheh 	int status;
644ccd979bdSMark Fasheh 
645ccd979bdSMark Fasheh 	ac->ac_which = OCFS2_AC_USE_MAIN;
646ccd979bdSMark Fasheh 	ac->ac_group_search = ocfs2_cluster_group_search;
647ccd979bdSMark Fasheh 
648da5cbf2fSMark Fasheh 	status = ocfs2_reserve_suballoc_bits(osb, ac,
649da5cbf2fSMark Fasheh 					     GLOBAL_BITMAP_SYSTEM_INODE,
650ffda89a3STao Ma 					     OCFS2_INVALID_SLOT,
651ffda89a3STao Ma 					     ALLOC_NEW_GROUP);
652da5cbf2fSMark Fasheh 	if (status < 0 && status != -ENOSPC) {
653ccd979bdSMark Fasheh 		mlog_errno(status);
654da5cbf2fSMark Fasheh 		goto bail;
655da5cbf2fSMark Fasheh 	}
656da5cbf2fSMark Fasheh 
657ccd979bdSMark Fasheh bail:
658ccd979bdSMark Fasheh 	return status;
659ccd979bdSMark Fasheh }
660ccd979bdSMark Fasheh 
661ccd979bdSMark Fasheh /* Callers don't need to care which bitmap (local alloc or main) to
662ccd979bdSMark Fasheh  * use so we figure it out for them, but unfortunately this clutters
663ccd979bdSMark Fasheh  * things a bit. */
664ccd979bdSMark Fasheh int ocfs2_reserve_clusters(struct ocfs2_super *osb,
665ccd979bdSMark Fasheh 			   u32 bits_wanted,
666ccd979bdSMark Fasheh 			   struct ocfs2_alloc_context **ac)
667ccd979bdSMark Fasheh {
668ccd979bdSMark Fasheh 	int status;
669ccd979bdSMark Fasheh 
670ccd979bdSMark Fasheh 	mlog_entry_void();
671ccd979bdSMark Fasheh 
672cd861280SRobert P. J. Day 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
673ccd979bdSMark Fasheh 	if (!(*ac)) {
674ccd979bdSMark Fasheh 		status = -ENOMEM;
675ccd979bdSMark Fasheh 		mlog_errno(status);
676ccd979bdSMark Fasheh 		goto bail;
677ccd979bdSMark Fasheh 	}
678ccd979bdSMark Fasheh 
679ccd979bdSMark Fasheh 	(*ac)->ac_bits_wanted = bits_wanted;
680ccd979bdSMark Fasheh 
681ccd979bdSMark Fasheh 	status = -ENOSPC;
682ccd979bdSMark Fasheh 	if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
683ccd979bdSMark Fasheh 		status = ocfs2_reserve_local_alloc_bits(osb,
684ccd979bdSMark Fasheh 							bits_wanted,
685ccd979bdSMark Fasheh 							*ac);
686ccd979bdSMark Fasheh 		if ((status < 0) && (status != -ENOSPC)) {
687ccd979bdSMark Fasheh 			mlog_errno(status);
688ccd979bdSMark Fasheh 			goto bail;
689ccd979bdSMark Fasheh 		}
690ccd979bdSMark Fasheh 	}
691ccd979bdSMark Fasheh 
692ccd979bdSMark Fasheh 	if (status == -ENOSPC) {
693ccd979bdSMark Fasheh 		status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
694ccd979bdSMark Fasheh 		if (status < 0) {
695ccd979bdSMark Fasheh 			if (status != -ENOSPC)
696ccd979bdSMark Fasheh 				mlog_errno(status);
697ccd979bdSMark Fasheh 			goto bail;
698ccd979bdSMark Fasheh 		}
699ccd979bdSMark Fasheh 	}
700ccd979bdSMark Fasheh 
701ccd979bdSMark Fasheh 	status = 0;
702ccd979bdSMark Fasheh bail:
703ccd979bdSMark Fasheh 	if ((status < 0) && *ac) {
704ccd979bdSMark Fasheh 		ocfs2_free_alloc_context(*ac);
705ccd979bdSMark Fasheh 		*ac = NULL;
706ccd979bdSMark Fasheh 	}
707ccd979bdSMark Fasheh 
708ccd979bdSMark Fasheh 	mlog_exit(status);
709ccd979bdSMark Fasheh 	return status;
710ccd979bdSMark Fasheh }
711ccd979bdSMark Fasheh 
712ccd979bdSMark Fasheh /*
713ccd979bdSMark Fasheh  * More or less lifted from ext3. I'll leave their description below:
714ccd979bdSMark Fasheh  *
715ccd979bdSMark Fasheh  * "For ext3 allocations, we must not reuse any blocks which are
716ccd979bdSMark Fasheh  * allocated in the bitmap buffer's "last committed data" copy.  This
717ccd979bdSMark Fasheh  * prevents deletes from freeing up the page for reuse until we have
718ccd979bdSMark Fasheh  * committed the delete transaction.
719ccd979bdSMark Fasheh  *
720ccd979bdSMark Fasheh  * If we didn't do this, then deleting something and reallocating it as
721ccd979bdSMark Fasheh  * data would allow the old block to be overwritten before the
722ccd979bdSMark Fasheh  * transaction committed (because we force data to disk before commit).
723ccd979bdSMark Fasheh  * This would lead to corruption if we crashed between overwriting the
724ccd979bdSMark Fasheh  * data and committing the delete.
725ccd979bdSMark Fasheh  *
726ccd979bdSMark Fasheh  * @@@ We may want to make this allocation behaviour conditional on
727ccd979bdSMark Fasheh  * data-writes at some point, and disable it for metadata allocations or
728ccd979bdSMark Fasheh  * sync-data inodes."
729ccd979bdSMark Fasheh  *
730ccd979bdSMark Fasheh  * Note: OCFS2 already does this differently for metadata vs data
731c78bad11SJoe Perches  * allocations, as those bitmaps are separate and undo access is never
732ccd979bdSMark Fasheh  * called on a metadata group descriptor.
733ccd979bdSMark Fasheh  */
734ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
735ccd979bdSMark Fasheh 					 int nr)
736ccd979bdSMark Fasheh {
737ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
738ccd979bdSMark Fasheh 
739ccd979bdSMark Fasheh 	if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
740ccd979bdSMark Fasheh 		return 0;
741ccd979bdSMark Fasheh 	if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
742ccd979bdSMark Fasheh 		return 1;
743ccd979bdSMark Fasheh 
744ccd979bdSMark Fasheh 	bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
745ccd979bdSMark Fasheh 	return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
746ccd979bdSMark Fasheh }
747ccd979bdSMark Fasheh 
748ccd979bdSMark Fasheh static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
749ccd979bdSMark Fasheh 					     struct buffer_head *bg_bh,
750ccd979bdSMark Fasheh 					     unsigned int bits_wanted,
7517bf72edeSMark Fasheh 					     unsigned int total_bits,
752ccd979bdSMark Fasheh 					     u16 *bit_off,
753ccd979bdSMark Fasheh 					     u16 *bits_found)
754ccd979bdSMark Fasheh {
755ccd979bdSMark Fasheh 	void *bitmap;
756ccd979bdSMark Fasheh 	u16 best_offset, best_size;
757ccd979bdSMark Fasheh 	int offset, start, found, status = 0;
758ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
759ccd979bdSMark Fasheh 
760ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
761ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg);
762ccd979bdSMark Fasheh 		return -EIO;
763ccd979bdSMark Fasheh 	}
764ccd979bdSMark Fasheh 
765ccd979bdSMark Fasheh 	found = start = best_offset = best_size = 0;
766ccd979bdSMark Fasheh 	bitmap = bg->bg_bitmap;
767ccd979bdSMark Fasheh 
7687bf72edeSMark Fasheh 	while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
7697bf72edeSMark Fasheh 		if (offset == total_bits)
770ccd979bdSMark Fasheh 			break;
771ccd979bdSMark Fasheh 
772ccd979bdSMark Fasheh 		if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
773ccd979bdSMark Fasheh 			/* We found a zero, but we can't use it as it
774ccd979bdSMark Fasheh 			 * hasn't been put to disk yet! */
775ccd979bdSMark Fasheh 			found = 0;
776ccd979bdSMark Fasheh 			start = offset + 1;
777ccd979bdSMark Fasheh 		} else if (offset == start) {
778ccd979bdSMark Fasheh 			/* we found a zero */
779ccd979bdSMark Fasheh 			found++;
780ccd979bdSMark Fasheh 			/* move start to the next bit to test */
781ccd979bdSMark Fasheh 			start++;
782ccd979bdSMark Fasheh 		} else {
783ccd979bdSMark Fasheh 			/* got a zero after some ones */
784ccd979bdSMark Fasheh 			found = 1;
785ccd979bdSMark Fasheh 			start = offset + 1;
786ccd979bdSMark Fasheh 		}
787ccd979bdSMark Fasheh 		if (found > best_size) {
788ccd979bdSMark Fasheh 			best_size = found;
789ccd979bdSMark Fasheh 			best_offset = start - found;
790ccd979bdSMark Fasheh 		}
791ccd979bdSMark Fasheh 		/* we got everything we needed */
792ccd979bdSMark Fasheh 		if (found == bits_wanted) {
793ccd979bdSMark Fasheh 			/* mlog(0, "Found it all!\n"); */
794ccd979bdSMark Fasheh 			break;
795ccd979bdSMark Fasheh 		}
796ccd979bdSMark Fasheh 	}
797ccd979bdSMark Fasheh 
798ccd979bdSMark Fasheh 	/* XXX: I think the first clause is equivalent to the second
799ccd979bdSMark Fasheh 	 * 	- jlbec */
800ccd979bdSMark Fasheh 	if (found == bits_wanted) {
801ccd979bdSMark Fasheh 		*bit_off = start - found;
802ccd979bdSMark Fasheh 		*bits_found = found;
803ccd979bdSMark Fasheh 	} else if (best_size) {
804ccd979bdSMark Fasheh 		*bit_off = best_offset;
805ccd979bdSMark Fasheh 		*bits_found = best_size;
806ccd979bdSMark Fasheh 	} else {
807ccd979bdSMark Fasheh 		status = -ENOSPC;
808ccd979bdSMark Fasheh 		/* No error log here -- see the comment above
809ccd979bdSMark Fasheh 		 * ocfs2_test_bg_bit_allocatable */
810ccd979bdSMark Fasheh 	}
811ccd979bdSMark Fasheh 
812ccd979bdSMark Fasheh 	return status;
813ccd979bdSMark Fasheh }
814ccd979bdSMark Fasheh 
8151fabe148SMark Fasheh static inline int ocfs2_block_group_set_bits(handle_t *handle,
816ccd979bdSMark Fasheh 					     struct inode *alloc_inode,
817ccd979bdSMark Fasheh 					     struct ocfs2_group_desc *bg,
818ccd979bdSMark Fasheh 					     struct buffer_head *group_bh,
819ccd979bdSMark Fasheh 					     unsigned int bit_off,
820ccd979bdSMark Fasheh 					     unsigned int num_bits)
821ccd979bdSMark Fasheh {
822ccd979bdSMark Fasheh 	int status;
823ccd979bdSMark Fasheh 	void *bitmap = bg->bg_bitmap;
824ccd979bdSMark Fasheh 	int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
825ccd979bdSMark Fasheh 
826ccd979bdSMark Fasheh 	mlog_entry_void();
827ccd979bdSMark Fasheh 
828ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
829ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
830ccd979bdSMark Fasheh 		status = -EIO;
831ccd979bdSMark Fasheh 		goto bail;
832ccd979bdSMark Fasheh 	}
833ccd979bdSMark Fasheh 	BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
834ccd979bdSMark Fasheh 
835ccd979bdSMark Fasheh 	mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
836ccd979bdSMark Fasheh 	     num_bits);
837ccd979bdSMark Fasheh 
838ccd979bdSMark Fasheh 	if (ocfs2_is_cluster_bitmap(alloc_inode))
839ccd979bdSMark Fasheh 		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
840ccd979bdSMark Fasheh 
841ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle,
842ccd979bdSMark Fasheh 				      alloc_inode,
843ccd979bdSMark Fasheh 				      group_bh,
844ccd979bdSMark Fasheh 				      journal_type);
845ccd979bdSMark Fasheh 	if (status < 0) {
846ccd979bdSMark Fasheh 		mlog_errno(status);
847ccd979bdSMark Fasheh 		goto bail;
848ccd979bdSMark Fasheh 	}
849ccd979bdSMark Fasheh 
850ccd979bdSMark Fasheh 	le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
851ccd979bdSMark Fasheh 
852ccd979bdSMark Fasheh 	while(num_bits--)
853ccd979bdSMark Fasheh 		ocfs2_set_bit(bit_off++, bitmap);
854ccd979bdSMark Fasheh 
855ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle,
856ccd979bdSMark Fasheh 				     group_bh);
857ccd979bdSMark Fasheh 	if (status < 0) {
858ccd979bdSMark Fasheh 		mlog_errno(status);
859ccd979bdSMark Fasheh 		goto bail;
860ccd979bdSMark Fasheh 	}
861ccd979bdSMark Fasheh 
862ccd979bdSMark Fasheh bail:
863ccd979bdSMark Fasheh 	mlog_exit(status);
864ccd979bdSMark Fasheh 	return status;
865ccd979bdSMark Fasheh }
866ccd979bdSMark Fasheh 
867ccd979bdSMark Fasheh /* find the one with the most empty bits */
868ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
869ccd979bdSMark Fasheh {
870ccd979bdSMark Fasheh 	u16 curr, best;
871ccd979bdSMark Fasheh 
872ccd979bdSMark Fasheh 	BUG_ON(!cl->cl_next_free_rec);
873ccd979bdSMark Fasheh 
874ccd979bdSMark Fasheh 	best = curr = 0;
875ccd979bdSMark Fasheh 	while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
876ccd979bdSMark Fasheh 		if (le32_to_cpu(cl->cl_recs[curr].c_free) >
877ccd979bdSMark Fasheh 		    le32_to_cpu(cl->cl_recs[best].c_free))
878ccd979bdSMark Fasheh 			best = curr;
879ccd979bdSMark Fasheh 		curr++;
880ccd979bdSMark Fasheh 	}
881ccd979bdSMark Fasheh 
882ccd979bdSMark Fasheh 	BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
883ccd979bdSMark Fasheh 	return best;
884ccd979bdSMark Fasheh }
885ccd979bdSMark Fasheh 
8861fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle,
887ccd979bdSMark Fasheh 				    struct inode *alloc_inode,
888ccd979bdSMark Fasheh 				    struct buffer_head *fe_bh,
889ccd979bdSMark Fasheh 				    struct buffer_head *bg_bh,
890ccd979bdSMark Fasheh 				    struct buffer_head *prev_bg_bh,
891ccd979bdSMark Fasheh 				    u16 chain)
892ccd979bdSMark Fasheh {
893ccd979bdSMark Fasheh 	int status;
894ccd979bdSMark Fasheh 	/* there is a really tiny chance the journal calls could fail,
895ccd979bdSMark Fasheh 	 * but we wouldn't want inconsistent blocks in *any* case. */
896ccd979bdSMark Fasheh 	u64 fe_ptr, bg_ptr, prev_bg_ptr;
897ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
898ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
899ccd979bdSMark Fasheh 	struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
900ccd979bdSMark Fasheh 
901ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
902ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
903ccd979bdSMark Fasheh 		status = -EIO;
904ccd979bdSMark Fasheh 		goto out;
905ccd979bdSMark Fasheh 	}
906ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
907ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
908ccd979bdSMark Fasheh 		status = -EIO;
909ccd979bdSMark Fasheh 		goto out;
910ccd979bdSMark Fasheh 	}
911ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) {
912ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg);
913ccd979bdSMark Fasheh 		status = -EIO;
914ccd979bdSMark Fasheh 		goto out;
915ccd979bdSMark Fasheh 	}
916ccd979bdSMark Fasheh 
917b0697053SMark Fasheh 	mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
9181ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
9191ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(bg->bg_blkno),
9201ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
921ccd979bdSMark Fasheh 
922ccd979bdSMark Fasheh 	fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
923ccd979bdSMark Fasheh 	bg_ptr = le64_to_cpu(bg->bg_next_group);
924ccd979bdSMark Fasheh 	prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
925ccd979bdSMark Fasheh 
926ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh,
927ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
928ccd979bdSMark Fasheh 	if (status < 0) {
929ccd979bdSMark Fasheh 		mlog_errno(status);
930ccd979bdSMark Fasheh 		goto out_rollback;
931ccd979bdSMark Fasheh 	}
932ccd979bdSMark Fasheh 
933ccd979bdSMark Fasheh 	prev_bg->bg_next_group = bg->bg_next_group;
934ccd979bdSMark Fasheh 
935ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, prev_bg_bh);
936ccd979bdSMark Fasheh 	if (status < 0) {
937ccd979bdSMark Fasheh 		mlog_errno(status);
938ccd979bdSMark Fasheh 		goto out_rollback;
939ccd979bdSMark Fasheh 	}
940ccd979bdSMark Fasheh 
941ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, bg_bh,
942ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
943ccd979bdSMark Fasheh 	if (status < 0) {
944ccd979bdSMark Fasheh 		mlog_errno(status);
945ccd979bdSMark Fasheh 		goto out_rollback;
946ccd979bdSMark Fasheh 	}
947ccd979bdSMark Fasheh 
948ccd979bdSMark Fasheh 	bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
949ccd979bdSMark Fasheh 
950ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, bg_bh);
951ccd979bdSMark Fasheh 	if (status < 0) {
952ccd979bdSMark Fasheh 		mlog_errno(status);
953ccd979bdSMark Fasheh 		goto out_rollback;
954ccd979bdSMark Fasheh 	}
955ccd979bdSMark Fasheh 
956ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
957ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
958ccd979bdSMark Fasheh 	if (status < 0) {
959ccd979bdSMark Fasheh 		mlog_errno(status);
960ccd979bdSMark Fasheh 		goto out_rollback;
961ccd979bdSMark Fasheh 	}
962ccd979bdSMark Fasheh 
963ccd979bdSMark Fasheh 	fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
964ccd979bdSMark Fasheh 
965ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, fe_bh);
966ccd979bdSMark Fasheh 	if (status < 0) {
967ccd979bdSMark Fasheh 		mlog_errno(status);
968ccd979bdSMark Fasheh 		goto out_rollback;
969ccd979bdSMark Fasheh 	}
970ccd979bdSMark Fasheh 
971ccd979bdSMark Fasheh 	status = 0;
972ccd979bdSMark Fasheh out_rollback:
973ccd979bdSMark Fasheh 	if (status < 0) {
974ccd979bdSMark Fasheh 		fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
975ccd979bdSMark Fasheh 		bg->bg_next_group = cpu_to_le64(bg_ptr);
976ccd979bdSMark Fasheh 		prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
977ccd979bdSMark Fasheh 	}
978ccd979bdSMark Fasheh out:
979ccd979bdSMark Fasheh 	mlog_exit(status);
980ccd979bdSMark Fasheh 	return status;
981ccd979bdSMark Fasheh }
982ccd979bdSMark Fasheh 
983ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
984ccd979bdSMark Fasheh 						     u32 wanted)
985ccd979bdSMark Fasheh {
986ccd979bdSMark Fasheh 	return le16_to_cpu(bg->bg_free_bits_count) > wanted;
987ccd979bdSMark Fasheh }
988ccd979bdSMark Fasheh 
989ccd979bdSMark Fasheh /* return 0 on success, -ENOSPC to keep searching and any other < 0
990ccd979bdSMark Fasheh  * value on error. */
991ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode,
992ccd979bdSMark Fasheh 				      struct buffer_head *group_bh,
993ccd979bdSMark Fasheh 				      u32 bits_wanted, u32 min_bits,
994ccd979bdSMark Fasheh 				      u16 *bit_off, u16 *bits_found)
995ccd979bdSMark Fasheh {
996ccd979bdSMark Fasheh 	int search = -ENOSPC;
997ccd979bdSMark Fasheh 	int ret;
9987bf72edeSMark Fasheh 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
9999c7af40bSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1000ccd979bdSMark Fasheh 	u16 tmp_off, tmp_found;
10017bf72edeSMark Fasheh 	unsigned int max_bits, gd_cluster_off;
1002ccd979bdSMark Fasheh 
1003ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1004ccd979bdSMark Fasheh 
10057bf72edeSMark Fasheh 	if (gd->bg_free_bits_count) {
10067bf72edeSMark Fasheh 		max_bits = le16_to_cpu(gd->bg_bits);
10077bf72edeSMark Fasheh 
10087bf72edeSMark Fasheh 		/* Tail groups in cluster bitmaps which aren't cpg
10097bf72edeSMark Fasheh 		 * aligned are prone to partial extention by a failed
10107bf72edeSMark Fasheh 		 * fs resize. If the file system resize never got to
10117bf72edeSMark Fasheh 		 * update the dinode cluster count, then we don't want
10127bf72edeSMark Fasheh 		 * to trust any clusters past it, regardless of what
10137bf72edeSMark Fasheh 		 * the group descriptor says. */
10147bf72edeSMark Fasheh 		gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
10157bf72edeSMark Fasheh 							  le64_to_cpu(gd->bg_blkno));
10167bf72edeSMark Fasheh 		if ((gd_cluster_off + max_bits) >
10177bf72edeSMark Fasheh 		    OCFS2_I(inode)->ip_clusters) {
10187bf72edeSMark Fasheh 			max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
10197bf72edeSMark Fasheh 			mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
10207bf72edeSMark Fasheh 			     (unsigned long long)le64_to_cpu(gd->bg_blkno),
10217bf72edeSMark Fasheh 			     le16_to_cpu(gd->bg_bits),
10227bf72edeSMark Fasheh 			     OCFS2_I(inode)->ip_clusters, max_bits);
10237bf72edeSMark Fasheh 		}
10247bf72edeSMark Fasheh 
1025ccd979bdSMark Fasheh 		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1026ccd979bdSMark Fasheh 							group_bh, bits_wanted,
10277bf72edeSMark Fasheh 							max_bits,
1028ccd979bdSMark Fasheh 							&tmp_off, &tmp_found);
1029ccd979bdSMark Fasheh 		if (ret)
1030ccd979bdSMark Fasheh 			return ret;
1031ccd979bdSMark Fasheh 
1032ccd979bdSMark Fasheh 		/* ocfs2_block_group_find_clear_bits() might
1033ccd979bdSMark Fasheh 		 * return success, but we still want to return
1034ccd979bdSMark Fasheh 		 * -ENOSPC unless it found the minimum number
1035ccd979bdSMark Fasheh 		 * of bits. */
1036ccd979bdSMark Fasheh 		if (min_bits <= tmp_found) {
1037ccd979bdSMark Fasheh 			*bit_off = tmp_off;
1038ccd979bdSMark Fasheh 			*bits_found = tmp_found;
1039ccd979bdSMark Fasheh 			search = 0; /* success */
10409c7af40bSMark Fasheh 		} else if (tmp_found) {
10419c7af40bSMark Fasheh 			/*
10429c7af40bSMark Fasheh 			 * Don't show bits which we'll be returning
10439c7af40bSMark Fasheh 			 * for allocation to the local alloc bitmap.
10449c7af40bSMark Fasheh 			 */
10459c7af40bSMark Fasheh 			ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
1046ccd979bdSMark Fasheh 		}
1047ccd979bdSMark Fasheh 	}
1048ccd979bdSMark Fasheh 
1049ccd979bdSMark Fasheh 	return search;
1050ccd979bdSMark Fasheh }
1051ccd979bdSMark Fasheh 
1052ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode,
1053ccd979bdSMark Fasheh 				    struct buffer_head *group_bh,
1054ccd979bdSMark Fasheh 				    u32 bits_wanted, u32 min_bits,
1055ccd979bdSMark Fasheh 				    u16 *bit_off, u16 *bits_found)
1056ccd979bdSMark Fasheh {
1057ccd979bdSMark Fasheh 	int ret = -ENOSPC;
1058ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1059ccd979bdSMark Fasheh 
1060ccd979bdSMark Fasheh 	BUG_ON(min_bits != 1);
1061ccd979bdSMark Fasheh 	BUG_ON(ocfs2_is_cluster_bitmap(inode));
1062ccd979bdSMark Fasheh 
1063ccd979bdSMark Fasheh 	if (bg->bg_free_bits_count)
1064ccd979bdSMark Fasheh 		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1065ccd979bdSMark Fasheh 							group_bh, bits_wanted,
10667bf72edeSMark Fasheh 							le16_to_cpu(bg->bg_bits),
1067ccd979bdSMark Fasheh 							bit_off, bits_found);
1068ccd979bdSMark Fasheh 
1069ccd979bdSMark Fasheh 	return ret;
1070ccd979bdSMark Fasheh }
1071ccd979bdSMark Fasheh 
1072883d4caeSMark Fasheh static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
10731fabe148SMark Fasheh 				       handle_t *handle,
1074883d4caeSMark Fasheh 				       struct buffer_head *di_bh,
1075883d4caeSMark Fasheh 				       u32 num_bits,
1076883d4caeSMark Fasheh 				       u16 chain)
1077883d4caeSMark Fasheh {
1078883d4caeSMark Fasheh 	int ret;
1079883d4caeSMark Fasheh 	u32 tmp_used;
1080883d4caeSMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1081883d4caeSMark Fasheh 	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1082883d4caeSMark Fasheh 
1083883d4caeSMark Fasheh 	ret = ocfs2_journal_access(handle, inode, di_bh,
1084883d4caeSMark Fasheh 				   OCFS2_JOURNAL_ACCESS_WRITE);
1085883d4caeSMark Fasheh 	if (ret < 0) {
1086883d4caeSMark Fasheh 		mlog_errno(ret);
1087883d4caeSMark Fasheh 		goto out;
1088883d4caeSMark Fasheh 	}
1089883d4caeSMark Fasheh 
1090883d4caeSMark Fasheh 	tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1091883d4caeSMark Fasheh 	di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1092883d4caeSMark Fasheh 	le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1093883d4caeSMark Fasheh 
1094883d4caeSMark Fasheh 	ret = ocfs2_journal_dirty(handle, di_bh);
1095883d4caeSMark Fasheh 	if (ret < 0)
1096883d4caeSMark Fasheh 		mlog_errno(ret);
1097883d4caeSMark Fasheh 
1098883d4caeSMark Fasheh out:
1099883d4caeSMark Fasheh 	return ret;
1100883d4caeSMark Fasheh }
1101883d4caeSMark Fasheh 
1102883d4caeSMark Fasheh static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
11031fabe148SMark Fasheh 				  handle_t *handle,
1104883d4caeSMark Fasheh 				  u32 bits_wanted,
1105883d4caeSMark Fasheh 				  u32 min_bits,
1106883d4caeSMark Fasheh 				  u16 *bit_off,
1107883d4caeSMark Fasheh 				  unsigned int *num_bits,
1108883d4caeSMark Fasheh 				  u64 gd_blkno,
1109883d4caeSMark Fasheh 				  u16 *bits_left)
1110883d4caeSMark Fasheh {
1111883d4caeSMark Fasheh 	int ret;
1112883d4caeSMark Fasheh 	u16 found;
1113883d4caeSMark Fasheh 	struct buffer_head *group_bh = NULL;
1114883d4caeSMark Fasheh 	struct ocfs2_group_desc *gd;
1115883d4caeSMark Fasheh 	struct inode *alloc_inode = ac->ac_inode;
1116883d4caeSMark Fasheh 
1117883d4caeSMark Fasheh 	ret = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), gd_blkno,
1118883d4caeSMark Fasheh 			       &group_bh, OCFS2_BH_CACHED, alloc_inode);
1119883d4caeSMark Fasheh 	if (ret < 0) {
1120883d4caeSMark Fasheh 		mlog_errno(ret);
1121883d4caeSMark Fasheh 		return ret;
1122883d4caeSMark Fasheh 	}
1123883d4caeSMark Fasheh 
1124883d4caeSMark Fasheh 	gd = (struct ocfs2_group_desc *) group_bh->b_data;
1125883d4caeSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1126883d4caeSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, gd);
1127883d4caeSMark Fasheh 		ret = -EIO;
1128883d4caeSMark Fasheh 		goto out;
1129883d4caeSMark Fasheh 	}
1130883d4caeSMark Fasheh 
1131883d4caeSMark Fasheh 	ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1132883d4caeSMark Fasheh 				  bit_off, &found);
1133883d4caeSMark Fasheh 	if (ret < 0) {
1134883d4caeSMark Fasheh 		if (ret != -ENOSPC)
1135883d4caeSMark Fasheh 			mlog_errno(ret);
1136883d4caeSMark Fasheh 		goto out;
1137883d4caeSMark Fasheh 	}
1138883d4caeSMark Fasheh 
1139883d4caeSMark Fasheh 	*num_bits = found;
1140883d4caeSMark Fasheh 
1141883d4caeSMark Fasheh 	ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1142883d4caeSMark Fasheh 					       *num_bits,
1143883d4caeSMark Fasheh 					       le16_to_cpu(gd->bg_chain));
1144883d4caeSMark Fasheh 	if (ret < 0) {
1145883d4caeSMark Fasheh 		mlog_errno(ret);
1146883d4caeSMark Fasheh 		goto out;
1147883d4caeSMark Fasheh 	}
1148883d4caeSMark Fasheh 
1149883d4caeSMark Fasheh 	ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1150883d4caeSMark Fasheh 					 *bit_off, *num_bits);
1151883d4caeSMark Fasheh 	if (ret < 0)
1152883d4caeSMark Fasheh 		mlog_errno(ret);
1153883d4caeSMark Fasheh 
1154883d4caeSMark Fasheh 	*bits_left = le16_to_cpu(gd->bg_free_bits_count);
1155883d4caeSMark Fasheh 
1156883d4caeSMark Fasheh out:
1157883d4caeSMark Fasheh 	brelse(group_bh);
1158883d4caeSMark Fasheh 
1159883d4caeSMark Fasheh 	return ret;
1160883d4caeSMark Fasheh }
1161883d4caeSMark Fasheh 
1162ccd979bdSMark Fasheh static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
11631fabe148SMark Fasheh 			      handle_t *handle,
1164ccd979bdSMark Fasheh 			      u32 bits_wanted,
1165ccd979bdSMark Fasheh 			      u32 min_bits,
1166ccd979bdSMark Fasheh 			      u16 *bit_off,
1167ccd979bdSMark Fasheh 			      unsigned int *num_bits,
1168883d4caeSMark Fasheh 			      u64 *bg_blkno,
1169883d4caeSMark Fasheh 			      u16 *bits_left)
1170ccd979bdSMark Fasheh {
1171ccd979bdSMark Fasheh 	int status;
1172ccd979bdSMark Fasheh 	u16 chain, tmp_bits;
1173ccd979bdSMark Fasheh 	u32 tmp_used;
1174ccd979bdSMark Fasheh 	u64 next_group;
1175ccd979bdSMark Fasheh 	struct inode *alloc_inode = ac->ac_inode;
1176ccd979bdSMark Fasheh 	struct buffer_head *group_bh = NULL;
1177ccd979bdSMark Fasheh 	struct buffer_head *prev_group_bh = NULL;
1178ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1179ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1180ccd979bdSMark Fasheh 	struct ocfs2_group_desc *bg;
1181ccd979bdSMark Fasheh 
1182ccd979bdSMark Fasheh 	chain = ac->ac_chain;
1183b0697053SMark Fasheh 	mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1184b0697053SMark Fasheh 	     bits_wanted, chain,
1185b0697053SMark Fasheh 	     (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
1186ccd979bdSMark Fasheh 
1187ccd979bdSMark Fasheh 	status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1188ccd979bdSMark Fasheh 				  le64_to_cpu(cl->cl_recs[chain].c_blkno),
1189ccd979bdSMark Fasheh 				  &group_bh, OCFS2_BH_CACHED, alloc_inode);
1190ccd979bdSMark Fasheh 	if (status < 0) {
1191ccd979bdSMark Fasheh 		mlog_errno(status);
1192ccd979bdSMark Fasheh 		goto bail;
1193ccd979bdSMark Fasheh 	}
1194ccd979bdSMark Fasheh 	bg = (struct ocfs2_group_desc *) group_bh->b_data;
11957bf72edeSMark Fasheh 	status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
11967bf72edeSMark Fasheh 	if (status) {
11977bf72edeSMark Fasheh 		mlog_errno(status);
1198ccd979bdSMark Fasheh 		goto bail;
1199ccd979bdSMark Fasheh 	}
1200ccd979bdSMark Fasheh 
1201ccd979bdSMark Fasheh 	status = -ENOSPC;
1202ccd979bdSMark Fasheh 	/* for now, the chain search is a bit simplistic. We just use
1203ccd979bdSMark Fasheh 	 * the 1st group with any empty bits. */
12049c7af40bSMark Fasheh 	while ((status = ac->ac_group_search(alloc_inode, group_bh, bits_wanted,
12059c7af40bSMark Fasheh 					     min_bits, bit_off, &tmp_bits)) == -ENOSPC) {
1206ccd979bdSMark Fasheh 		if (!bg->bg_next_group)
1207ccd979bdSMark Fasheh 			break;
1208ccd979bdSMark Fasheh 
1209ccd979bdSMark Fasheh 		if (prev_group_bh) {
1210ccd979bdSMark Fasheh 			brelse(prev_group_bh);
1211ccd979bdSMark Fasheh 			prev_group_bh = NULL;
1212ccd979bdSMark Fasheh 		}
1213ccd979bdSMark Fasheh 		next_group = le64_to_cpu(bg->bg_next_group);
1214ccd979bdSMark Fasheh 		prev_group_bh = group_bh;
1215ccd979bdSMark Fasheh 		group_bh = NULL;
1216ccd979bdSMark Fasheh 		status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1217ccd979bdSMark Fasheh 					  next_group, &group_bh,
1218ccd979bdSMark Fasheh 					  OCFS2_BH_CACHED, alloc_inode);
1219ccd979bdSMark Fasheh 		if (status < 0) {
1220ccd979bdSMark Fasheh 			mlog_errno(status);
1221ccd979bdSMark Fasheh 			goto bail;
1222ccd979bdSMark Fasheh 		}
1223ccd979bdSMark Fasheh 		bg = (struct ocfs2_group_desc *) group_bh->b_data;
12247bf72edeSMark Fasheh 		status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
12257bf72edeSMark Fasheh 		if (status) {
12267bf72edeSMark Fasheh 			mlog_errno(status);
1227ccd979bdSMark Fasheh 			goto bail;
1228ccd979bdSMark Fasheh 		}
1229ccd979bdSMark Fasheh 	}
1230ccd979bdSMark Fasheh 	if (status < 0) {
1231ccd979bdSMark Fasheh 		if (status != -ENOSPC)
1232ccd979bdSMark Fasheh 			mlog_errno(status);
1233ccd979bdSMark Fasheh 		goto bail;
1234ccd979bdSMark Fasheh 	}
1235ccd979bdSMark Fasheh 
1236b0697053SMark Fasheh 	mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
12371ca1a111SMark Fasheh 	     tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
1238ccd979bdSMark Fasheh 
1239ccd979bdSMark Fasheh 	*num_bits = tmp_bits;
1240ccd979bdSMark Fasheh 
1241ccd979bdSMark Fasheh 	BUG_ON(*num_bits == 0);
1242ccd979bdSMark Fasheh 
1243ccd979bdSMark Fasheh 	/*
1244ccd979bdSMark Fasheh 	 * Keep track of previous block descriptor read. When
1245ccd979bdSMark Fasheh 	 * we find a target, if we have read more than X
1246ccd979bdSMark Fasheh 	 * number of descriptors, and the target is reasonably
1247ccd979bdSMark Fasheh 	 * empty, relink him to top of his chain.
1248ccd979bdSMark Fasheh 	 *
1249ccd979bdSMark Fasheh 	 * We've read 0 extra blocks and only send one more to
1250ccd979bdSMark Fasheh 	 * the transaction, yet the next guy to search has a
1251ccd979bdSMark Fasheh 	 * much easier time.
1252ccd979bdSMark Fasheh 	 *
1253ccd979bdSMark Fasheh 	 * Do this *after* figuring out how many bits we're taking out
1254ccd979bdSMark Fasheh 	 * of our target group.
1255ccd979bdSMark Fasheh 	 */
1256ccd979bdSMark Fasheh 	if (ac->ac_allow_chain_relink &&
1257ccd979bdSMark Fasheh 	    (prev_group_bh) &&
1258ccd979bdSMark Fasheh 	    (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1259ccd979bdSMark Fasheh 		status = ocfs2_relink_block_group(handle, alloc_inode,
1260ccd979bdSMark Fasheh 						  ac->ac_bh, group_bh,
1261ccd979bdSMark Fasheh 						  prev_group_bh, chain);
1262ccd979bdSMark Fasheh 		if (status < 0) {
1263ccd979bdSMark Fasheh 			mlog_errno(status);
1264ccd979bdSMark Fasheh 			goto bail;
1265ccd979bdSMark Fasheh 		}
1266ccd979bdSMark Fasheh 	}
1267ccd979bdSMark Fasheh 
1268ccd979bdSMark Fasheh 	/* Ok, claim our bits now: set the info on dinode, chainlist
1269ccd979bdSMark Fasheh 	 * and then the group */
1270ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle,
1271ccd979bdSMark Fasheh 				      alloc_inode,
1272ccd979bdSMark Fasheh 				      ac->ac_bh,
1273ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
1274ccd979bdSMark Fasheh 	if (status < 0) {
1275ccd979bdSMark Fasheh 		mlog_errno(status);
1276ccd979bdSMark Fasheh 		goto bail;
1277ccd979bdSMark Fasheh 	}
1278ccd979bdSMark Fasheh 
1279ccd979bdSMark Fasheh 	tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1280ccd979bdSMark Fasheh 	fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1281ccd979bdSMark Fasheh 	le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1282ccd979bdSMark Fasheh 
1283ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle,
1284ccd979bdSMark Fasheh 				     ac->ac_bh);
1285ccd979bdSMark Fasheh 	if (status < 0) {
1286ccd979bdSMark Fasheh 		mlog_errno(status);
1287ccd979bdSMark Fasheh 		goto bail;
1288ccd979bdSMark Fasheh 	}
1289ccd979bdSMark Fasheh 
1290ccd979bdSMark Fasheh 	status = ocfs2_block_group_set_bits(handle,
1291ccd979bdSMark Fasheh 					    alloc_inode,
1292ccd979bdSMark Fasheh 					    bg,
1293ccd979bdSMark Fasheh 					    group_bh,
1294ccd979bdSMark Fasheh 					    *bit_off,
1295ccd979bdSMark Fasheh 					    *num_bits);
1296ccd979bdSMark Fasheh 	if (status < 0) {
1297ccd979bdSMark Fasheh 		mlog_errno(status);
1298ccd979bdSMark Fasheh 		goto bail;
1299ccd979bdSMark Fasheh 	}
1300ccd979bdSMark Fasheh 
1301b0697053SMark Fasheh 	mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
13021ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(fe->i_blkno));
1303ccd979bdSMark Fasheh 
1304ccd979bdSMark Fasheh 	*bg_blkno = le64_to_cpu(bg->bg_blkno);
1305883d4caeSMark Fasheh 	*bits_left = le16_to_cpu(bg->bg_free_bits_count);
1306ccd979bdSMark Fasheh bail:
1307ccd979bdSMark Fasheh 	if (group_bh)
1308ccd979bdSMark Fasheh 		brelse(group_bh);
1309ccd979bdSMark Fasheh 	if (prev_group_bh)
1310ccd979bdSMark Fasheh 		brelse(prev_group_bh);
1311ccd979bdSMark Fasheh 
1312ccd979bdSMark Fasheh 	mlog_exit(status);
1313ccd979bdSMark Fasheh 	return status;
1314ccd979bdSMark Fasheh }
1315ccd979bdSMark Fasheh 
1316ccd979bdSMark Fasheh /* will give out up to bits_wanted contiguous bits. */
1317ccd979bdSMark Fasheh static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1318ccd979bdSMark Fasheh 				     struct ocfs2_alloc_context *ac,
13191fabe148SMark Fasheh 				     handle_t *handle,
1320ccd979bdSMark Fasheh 				     u32 bits_wanted,
1321ccd979bdSMark Fasheh 				     u32 min_bits,
1322ccd979bdSMark Fasheh 				     u16 *bit_off,
1323ccd979bdSMark Fasheh 				     unsigned int *num_bits,
1324ccd979bdSMark Fasheh 				     u64 *bg_blkno)
1325ccd979bdSMark Fasheh {
1326ccd979bdSMark Fasheh 	int status;
1327ccd979bdSMark Fasheh 	u16 victim, i;
1328883d4caeSMark Fasheh 	u16 bits_left = 0;
1329883d4caeSMark Fasheh 	u64 hint_blkno = ac->ac_last_group;
1330ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl;
1331ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
1332ccd979bdSMark Fasheh 
1333ccd979bdSMark Fasheh 	mlog_entry_void();
1334ccd979bdSMark Fasheh 
1335ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1336ccd979bdSMark Fasheh 	BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1337ccd979bdSMark Fasheh 	BUG_ON(!ac->ac_bh);
1338ccd979bdSMark Fasheh 
1339ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1340ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
1341ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(osb->sb, fe);
1342ccd979bdSMark Fasheh 		status = -EIO;
1343ccd979bdSMark Fasheh 		goto bail;
1344ccd979bdSMark Fasheh 	}
1345ccd979bdSMark Fasheh 	if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1346ccd979bdSMark Fasheh 	    le32_to_cpu(fe->id1.bitmap1.i_total)) {
1347b0697053SMark Fasheh 		ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1348b0697053SMark Fasheh 			    "bits but only %u total.",
1349b0697053SMark Fasheh 			    (unsigned long long)le64_to_cpu(fe->i_blkno),
1350ccd979bdSMark Fasheh 			    le32_to_cpu(fe->id1.bitmap1.i_used),
1351ccd979bdSMark Fasheh 			    le32_to_cpu(fe->id1.bitmap1.i_total));
1352ccd979bdSMark Fasheh 		status = -EIO;
1353ccd979bdSMark Fasheh 		goto bail;
1354ccd979bdSMark Fasheh 	}
1355ccd979bdSMark Fasheh 
1356883d4caeSMark Fasheh 	if (hint_blkno) {
1357883d4caeSMark Fasheh 		/* Attempt to short-circuit the usual search mechanism
1358883d4caeSMark Fasheh 		 * by jumping straight to the most recently used
1359883d4caeSMark Fasheh 		 * allocation group. This helps us mantain some
1360883d4caeSMark Fasheh 		 * contiguousness across allocations. */
1361da5cbf2fSMark Fasheh 		status = ocfs2_search_one_group(ac, handle, bits_wanted,
1362da5cbf2fSMark Fasheh 						min_bits, bit_off, num_bits,
1363883d4caeSMark Fasheh 						hint_blkno, &bits_left);
1364883d4caeSMark Fasheh 		if (!status) {
1365883d4caeSMark Fasheh 			/* Be careful to update *bg_blkno here as the
1366883d4caeSMark Fasheh 			 * caller is expecting it to be filled in, and
1367883d4caeSMark Fasheh 			 * ocfs2_search_one_group() won't do that for
1368883d4caeSMark Fasheh 			 * us. */
1369883d4caeSMark Fasheh 			*bg_blkno = hint_blkno;
1370883d4caeSMark Fasheh 			goto set_hint;
1371883d4caeSMark Fasheh 		}
1372883d4caeSMark Fasheh 		if (status < 0 && status != -ENOSPC) {
1373883d4caeSMark Fasheh 			mlog_errno(status);
1374883d4caeSMark Fasheh 			goto bail;
1375883d4caeSMark Fasheh 		}
1376883d4caeSMark Fasheh 	}
1377883d4caeSMark Fasheh 
1378ccd979bdSMark Fasheh 	cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1379ccd979bdSMark Fasheh 
1380ccd979bdSMark Fasheh 	victim = ocfs2_find_victim_chain(cl);
1381ccd979bdSMark Fasheh 	ac->ac_chain = victim;
1382ccd979bdSMark Fasheh 	ac->ac_allow_chain_relink = 1;
1383ccd979bdSMark Fasheh 
1384da5cbf2fSMark Fasheh 	status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
1385883d4caeSMark Fasheh 				    num_bits, bg_blkno, &bits_left);
1386ccd979bdSMark Fasheh 	if (!status)
1387883d4caeSMark Fasheh 		goto set_hint;
1388ccd979bdSMark Fasheh 	if (status < 0 && status != -ENOSPC) {
1389ccd979bdSMark Fasheh 		mlog_errno(status);
1390ccd979bdSMark Fasheh 		goto bail;
1391ccd979bdSMark Fasheh 	}
1392ccd979bdSMark Fasheh 
1393ccd979bdSMark Fasheh 	mlog(0, "Search of victim chain %u came up with nothing, "
1394ccd979bdSMark Fasheh 	     "trying all chains now.\n", victim);
1395ccd979bdSMark Fasheh 
1396ccd979bdSMark Fasheh 	/* If we didn't pick a good victim, then just default to
1397ccd979bdSMark Fasheh 	 * searching each chain in order. Don't allow chain relinking
1398ccd979bdSMark Fasheh 	 * because we only calculate enough journal credits for one
1399ccd979bdSMark Fasheh 	 * relink per alloc. */
1400ccd979bdSMark Fasheh 	ac->ac_allow_chain_relink = 0;
1401ccd979bdSMark Fasheh 	for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1402ccd979bdSMark Fasheh 		if (i == victim)
1403ccd979bdSMark Fasheh 			continue;
1404ccd979bdSMark Fasheh 		if (!cl->cl_recs[i].c_free)
1405ccd979bdSMark Fasheh 			continue;
1406ccd979bdSMark Fasheh 
1407ccd979bdSMark Fasheh 		ac->ac_chain = i;
1408da5cbf2fSMark Fasheh 		status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1409883d4caeSMark Fasheh 					    bit_off, num_bits, bg_blkno,
1410883d4caeSMark Fasheh 					    &bits_left);
1411ccd979bdSMark Fasheh 		if (!status)
1412ccd979bdSMark Fasheh 			break;
1413ccd979bdSMark Fasheh 		if (status < 0 && status != -ENOSPC) {
1414ccd979bdSMark Fasheh 			mlog_errno(status);
1415ccd979bdSMark Fasheh 			goto bail;
1416ccd979bdSMark Fasheh 		}
1417ccd979bdSMark Fasheh 	}
1418ccd979bdSMark Fasheh 
1419883d4caeSMark Fasheh set_hint:
1420883d4caeSMark Fasheh 	if (status != -ENOSPC) {
1421883d4caeSMark Fasheh 		/* If the next search of this group is not likely to
1422883d4caeSMark Fasheh 		 * yield a suitable extent, then we reset the last
1423883d4caeSMark Fasheh 		 * group hint so as to not waste a disk read */
1424883d4caeSMark Fasheh 		if (bits_left < min_bits)
1425883d4caeSMark Fasheh 			ac->ac_last_group = 0;
1426883d4caeSMark Fasheh 		else
1427883d4caeSMark Fasheh 			ac->ac_last_group = *bg_blkno;
1428883d4caeSMark Fasheh 	}
1429883d4caeSMark Fasheh 
1430883d4caeSMark Fasheh bail:
1431ccd979bdSMark Fasheh 	mlog_exit(status);
1432ccd979bdSMark Fasheh 	return status;
1433ccd979bdSMark Fasheh }
1434ccd979bdSMark Fasheh 
1435ccd979bdSMark Fasheh int ocfs2_claim_metadata(struct ocfs2_super *osb,
14361fabe148SMark Fasheh 			 handle_t *handle,
1437ccd979bdSMark Fasheh 			 struct ocfs2_alloc_context *ac,
1438ccd979bdSMark Fasheh 			 u32 bits_wanted,
1439ccd979bdSMark Fasheh 			 u16 *suballoc_bit_start,
1440ccd979bdSMark Fasheh 			 unsigned int *num_bits,
1441ccd979bdSMark Fasheh 			 u64 *blkno_start)
1442ccd979bdSMark Fasheh {
1443ccd979bdSMark Fasheh 	int status;
1444ccd979bdSMark Fasheh 	u64 bg_blkno;
1445ccd979bdSMark Fasheh 
1446ccd979bdSMark Fasheh 	BUG_ON(!ac);
1447ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1448ccd979bdSMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
1449ccd979bdSMark Fasheh 
1450ccd979bdSMark Fasheh 	status = ocfs2_claim_suballoc_bits(osb,
1451ccd979bdSMark Fasheh 					   ac,
1452da5cbf2fSMark Fasheh 					   handle,
1453ccd979bdSMark Fasheh 					   bits_wanted,
1454ccd979bdSMark Fasheh 					   1,
1455ccd979bdSMark Fasheh 					   suballoc_bit_start,
1456ccd979bdSMark Fasheh 					   num_bits,
1457ccd979bdSMark Fasheh 					   &bg_blkno);
1458ccd979bdSMark Fasheh 	if (status < 0) {
1459ccd979bdSMark Fasheh 		mlog_errno(status);
1460ccd979bdSMark Fasheh 		goto bail;
1461ccd979bdSMark Fasheh 	}
1462ccd979bdSMark Fasheh 	atomic_inc(&osb->alloc_stats.bg_allocs);
1463ccd979bdSMark Fasheh 
1464ccd979bdSMark Fasheh 	*blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1465ccd979bdSMark Fasheh 	ac->ac_bits_given += (*num_bits);
1466ccd979bdSMark Fasheh 	status = 0;
1467ccd979bdSMark Fasheh bail:
1468ccd979bdSMark Fasheh 	mlog_exit(status);
1469ccd979bdSMark Fasheh 	return status;
1470ccd979bdSMark Fasheh }
1471ccd979bdSMark Fasheh 
1472ccd979bdSMark Fasheh int ocfs2_claim_new_inode(struct ocfs2_super *osb,
14731fabe148SMark Fasheh 			  handle_t *handle,
1474ccd979bdSMark Fasheh 			  struct ocfs2_alloc_context *ac,
1475ccd979bdSMark Fasheh 			  u16 *suballoc_bit,
1476ccd979bdSMark Fasheh 			  u64 *fe_blkno)
1477ccd979bdSMark Fasheh {
1478ccd979bdSMark Fasheh 	int status;
1479ccd979bdSMark Fasheh 	unsigned int num_bits;
1480ccd979bdSMark Fasheh 	u64 bg_blkno;
1481ccd979bdSMark Fasheh 
1482ccd979bdSMark Fasheh 	mlog_entry_void();
1483ccd979bdSMark Fasheh 
1484ccd979bdSMark Fasheh 	BUG_ON(!ac);
1485ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_given != 0);
1486ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_wanted != 1);
1487ccd979bdSMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
1488ccd979bdSMark Fasheh 
1489ccd979bdSMark Fasheh 	status = ocfs2_claim_suballoc_bits(osb,
1490ccd979bdSMark Fasheh 					   ac,
1491da5cbf2fSMark Fasheh 					   handle,
1492ccd979bdSMark Fasheh 					   1,
1493ccd979bdSMark Fasheh 					   1,
1494ccd979bdSMark Fasheh 					   suballoc_bit,
1495ccd979bdSMark Fasheh 					   &num_bits,
1496ccd979bdSMark Fasheh 					   &bg_blkno);
1497ccd979bdSMark Fasheh 	if (status < 0) {
1498ccd979bdSMark Fasheh 		mlog_errno(status);
1499ccd979bdSMark Fasheh 		goto bail;
1500ccd979bdSMark Fasheh 	}
1501ccd979bdSMark Fasheh 	atomic_inc(&osb->alloc_stats.bg_allocs);
1502ccd979bdSMark Fasheh 
1503ccd979bdSMark Fasheh 	BUG_ON(num_bits != 1);
1504ccd979bdSMark Fasheh 
1505ccd979bdSMark Fasheh 	*fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1506ccd979bdSMark Fasheh 	ac->ac_bits_given++;
1507ccd979bdSMark Fasheh 	status = 0;
1508ccd979bdSMark Fasheh bail:
1509ccd979bdSMark Fasheh 	mlog_exit(status);
1510ccd979bdSMark Fasheh 	return status;
1511ccd979bdSMark Fasheh }
1512ccd979bdSMark Fasheh 
1513ccd979bdSMark Fasheh /* translate a group desc. blkno and it's bitmap offset into
1514ccd979bdSMark Fasheh  * disk cluster offset. */
1515ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1516ccd979bdSMark Fasheh 						   u64 bg_blkno,
1517ccd979bdSMark Fasheh 						   u16 bg_bit_off)
1518ccd979bdSMark Fasheh {
1519ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1520ccd979bdSMark Fasheh 	u32 cluster = 0;
1521ccd979bdSMark Fasheh 
1522ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1523ccd979bdSMark Fasheh 
1524ccd979bdSMark Fasheh 	if (bg_blkno != osb->first_cluster_group_blkno)
1525ccd979bdSMark Fasheh 		cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1526ccd979bdSMark Fasheh 	cluster += (u32) bg_bit_off;
1527ccd979bdSMark Fasheh 	return cluster;
1528ccd979bdSMark Fasheh }
1529ccd979bdSMark Fasheh 
1530ccd979bdSMark Fasheh /* given a cluster offset, calculate which block group it belongs to
1531ccd979bdSMark Fasheh  * and return that block offset. */
1532d659072fSTao Ma u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
1533ccd979bdSMark Fasheh {
1534ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1535ccd979bdSMark Fasheh 	u32 group_no;
1536ccd979bdSMark Fasheh 
1537ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1538ccd979bdSMark Fasheh 
1539ccd979bdSMark Fasheh 	group_no = cluster / osb->bitmap_cpg;
1540ccd979bdSMark Fasheh 	if (!group_no)
1541ccd979bdSMark Fasheh 		return osb->first_cluster_group_blkno;
1542ccd979bdSMark Fasheh 	return ocfs2_clusters_to_blocks(inode->i_sb,
1543ccd979bdSMark Fasheh 					group_no * osb->bitmap_cpg);
1544ccd979bdSMark Fasheh }
1545ccd979bdSMark Fasheh 
1546ccd979bdSMark Fasheh /* given the block number of a cluster start, calculate which cluster
1547ccd979bdSMark Fasheh  * group and descriptor bitmap offset that corresponds to. */
1548ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1549ccd979bdSMark Fasheh 						u64 data_blkno,
1550ccd979bdSMark Fasheh 						u64 *bg_blkno,
1551ccd979bdSMark Fasheh 						u16 *bg_bit_off)
1552ccd979bdSMark Fasheh {
1553ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1554ccd979bdSMark Fasheh 	u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1555ccd979bdSMark Fasheh 
1556ccd979bdSMark Fasheh 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1557ccd979bdSMark Fasheh 
1558ccd979bdSMark Fasheh 	*bg_blkno = ocfs2_which_cluster_group(inode,
1559ccd979bdSMark Fasheh 					      data_cluster);
1560ccd979bdSMark Fasheh 
1561ccd979bdSMark Fasheh 	if (*bg_blkno == osb->first_cluster_group_blkno)
1562ccd979bdSMark Fasheh 		*bg_bit_off = (u16) data_cluster;
1563ccd979bdSMark Fasheh 	else
1564ccd979bdSMark Fasheh 		*bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1565ccd979bdSMark Fasheh 							     data_blkno - *bg_blkno);
1566ccd979bdSMark Fasheh }
1567ccd979bdSMark Fasheh 
1568ccd979bdSMark Fasheh /*
1569ccd979bdSMark Fasheh  * min_bits - minimum contiguous chunk from this total allocation we
1570ccd979bdSMark Fasheh  * can handle. set to what we asked for originally for a full
1571ccd979bdSMark Fasheh  * contig. allocation, set to '1' to indicate we can deal with extents
1572ccd979bdSMark Fasheh  * of any size.
1573ccd979bdSMark Fasheh  */
1574415cb800SMark Fasheh int __ocfs2_claim_clusters(struct ocfs2_super *osb,
15751fabe148SMark Fasheh 			   handle_t *handle,
1576ccd979bdSMark Fasheh 			   struct ocfs2_alloc_context *ac,
1577ccd979bdSMark Fasheh 			   u32 min_clusters,
1578415cb800SMark Fasheh 			   u32 max_clusters,
1579ccd979bdSMark Fasheh 			   u32 *cluster_start,
1580ccd979bdSMark Fasheh 			   u32 *num_clusters)
1581ccd979bdSMark Fasheh {
1582ccd979bdSMark Fasheh 	int status;
1583415cb800SMark Fasheh 	unsigned int bits_wanted = max_clusters;
1584883d4caeSMark Fasheh 	u64 bg_blkno = 0;
1585ccd979bdSMark Fasheh 	u16 bg_bit_off;
1586ccd979bdSMark Fasheh 
1587ccd979bdSMark Fasheh 	mlog_entry_void();
1588ccd979bdSMark Fasheh 
1589ccd979bdSMark Fasheh 	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1590ccd979bdSMark Fasheh 
1591ccd979bdSMark Fasheh 	BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1592ccd979bdSMark Fasheh 	       && ac->ac_which != OCFS2_AC_USE_MAIN);
1593ccd979bdSMark Fasheh 
1594ccd979bdSMark Fasheh 	if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1595ccd979bdSMark Fasheh 		status = ocfs2_claim_local_alloc_bits(osb,
1596ccd979bdSMark Fasheh 						      handle,
1597ccd979bdSMark Fasheh 						      ac,
1598ccd979bdSMark Fasheh 						      bits_wanted,
1599ccd979bdSMark Fasheh 						      cluster_start,
1600ccd979bdSMark Fasheh 						      num_clusters);
1601ccd979bdSMark Fasheh 		if (!status)
1602ccd979bdSMark Fasheh 			atomic_inc(&osb->alloc_stats.local_data);
1603ccd979bdSMark Fasheh 	} else {
1604ccd979bdSMark Fasheh 		if (min_clusters > (osb->bitmap_cpg - 1)) {
1605ccd979bdSMark Fasheh 			/* The only paths asking for contiguousness
1606ccd979bdSMark Fasheh 			 * should know about this already. */
16072fbe8d1eSSunil Mushran 			mlog(ML_ERROR, "minimum allocation requested %u exceeds "
16082fbe8d1eSSunil Mushran 			     "group bitmap size %u!\n", min_clusters,
16092fbe8d1eSSunil Mushran 			     osb->bitmap_cpg);
1610ccd979bdSMark Fasheh 			status = -ENOSPC;
1611ccd979bdSMark Fasheh 			goto bail;
1612ccd979bdSMark Fasheh 		}
1613ccd979bdSMark Fasheh 		/* clamp the current request down to a realistic size. */
1614ccd979bdSMark Fasheh 		if (bits_wanted > (osb->bitmap_cpg - 1))
1615ccd979bdSMark Fasheh 			bits_wanted = osb->bitmap_cpg - 1;
1616ccd979bdSMark Fasheh 
1617ccd979bdSMark Fasheh 		status = ocfs2_claim_suballoc_bits(osb,
1618ccd979bdSMark Fasheh 						   ac,
1619da5cbf2fSMark Fasheh 						   handle,
1620ccd979bdSMark Fasheh 						   bits_wanted,
1621ccd979bdSMark Fasheh 						   min_clusters,
1622ccd979bdSMark Fasheh 						   &bg_bit_off,
1623ccd979bdSMark Fasheh 						   num_clusters,
1624ccd979bdSMark Fasheh 						   &bg_blkno);
1625ccd979bdSMark Fasheh 		if (!status) {
1626ccd979bdSMark Fasheh 			*cluster_start =
1627ccd979bdSMark Fasheh 				ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1628ccd979bdSMark Fasheh 								 bg_blkno,
1629ccd979bdSMark Fasheh 								 bg_bit_off);
1630ccd979bdSMark Fasheh 			atomic_inc(&osb->alloc_stats.bitmap_data);
1631ccd979bdSMark Fasheh 		}
1632ccd979bdSMark Fasheh 	}
1633ccd979bdSMark Fasheh 	if (status < 0) {
1634ccd979bdSMark Fasheh 		if (status != -ENOSPC)
1635ccd979bdSMark Fasheh 			mlog_errno(status);
1636ccd979bdSMark Fasheh 		goto bail;
1637ccd979bdSMark Fasheh 	}
1638ccd979bdSMark Fasheh 
1639ccd979bdSMark Fasheh 	ac->ac_bits_given += *num_clusters;
1640ccd979bdSMark Fasheh 
1641ccd979bdSMark Fasheh bail:
1642ccd979bdSMark Fasheh 	mlog_exit(status);
1643ccd979bdSMark Fasheh 	return status;
1644ccd979bdSMark Fasheh }
1645ccd979bdSMark Fasheh 
1646415cb800SMark Fasheh int ocfs2_claim_clusters(struct ocfs2_super *osb,
1647415cb800SMark Fasheh 			 handle_t *handle,
1648415cb800SMark Fasheh 			 struct ocfs2_alloc_context *ac,
1649415cb800SMark Fasheh 			 u32 min_clusters,
1650415cb800SMark Fasheh 			 u32 *cluster_start,
1651415cb800SMark Fasheh 			 u32 *num_clusters)
1652415cb800SMark Fasheh {
1653415cb800SMark Fasheh 	unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1654415cb800SMark Fasheh 
1655415cb800SMark Fasheh 	return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1656415cb800SMark Fasheh 				      bits_wanted, cluster_start, num_clusters);
1657415cb800SMark Fasheh }
1658415cb800SMark Fasheh 
16591fabe148SMark Fasheh static inline int ocfs2_block_group_clear_bits(handle_t *handle,
1660ccd979bdSMark Fasheh 					       struct inode *alloc_inode,
1661ccd979bdSMark Fasheh 					       struct ocfs2_group_desc *bg,
1662ccd979bdSMark Fasheh 					       struct buffer_head *group_bh,
1663ccd979bdSMark Fasheh 					       unsigned int bit_off,
1664ccd979bdSMark Fasheh 					       unsigned int num_bits)
1665ccd979bdSMark Fasheh {
1666ccd979bdSMark Fasheh 	int status;
1667ccd979bdSMark Fasheh 	unsigned int tmp;
1668ccd979bdSMark Fasheh 	int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1669ccd979bdSMark Fasheh 	struct ocfs2_group_desc *undo_bg = NULL;
1670ccd979bdSMark Fasheh 
1671ccd979bdSMark Fasheh 	mlog_entry_void();
1672ccd979bdSMark Fasheh 
1673ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
1674ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
1675ccd979bdSMark Fasheh 		status = -EIO;
1676ccd979bdSMark Fasheh 		goto bail;
1677ccd979bdSMark Fasheh 	}
1678ccd979bdSMark Fasheh 
1679ccd979bdSMark Fasheh 	mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1680ccd979bdSMark Fasheh 
1681ccd979bdSMark Fasheh 	if (ocfs2_is_cluster_bitmap(alloc_inode))
1682ccd979bdSMark Fasheh 		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1683ccd979bdSMark Fasheh 
1684ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, group_bh,
1685ccd979bdSMark Fasheh 				      journal_type);
1686ccd979bdSMark Fasheh 	if (status < 0) {
1687ccd979bdSMark Fasheh 		mlog_errno(status);
1688ccd979bdSMark Fasheh 		goto bail;
1689ccd979bdSMark Fasheh 	}
1690ccd979bdSMark Fasheh 
1691ccd979bdSMark Fasheh 	if (ocfs2_is_cluster_bitmap(alloc_inode))
1692ccd979bdSMark Fasheh 		undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
1693ccd979bdSMark Fasheh 
1694ccd979bdSMark Fasheh 	tmp = num_bits;
1695ccd979bdSMark Fasheh 	while(tmp--) {
1696ccd979bdSMark Fasheh 		ocfs2_clear_bit((bit_off + tmp),
1697ccd979bdSMark Fasheh 				(unsigned long *) bg->bg_bitmap);
1698ccd979bdSMark Fasheh 		if (ocfs2_is_cluster_bitmap(alloc_inode))
1699ccd979bdSMark Fasheh 			ocfs2_set_bit(bit_off + tmp,
1700ccd979bdSMark Fasheh 				      (unsigned long *) undo_bg->bg_bitmap);
1701ccd979bdSMark Fasheh 	}
1702ccd979bdSMark Fasheh 	le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1703ccd979bdSMark Fasheh 
1704ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, group_bh);
1705ccd979bdSMark Fasheh 	if (status < 0)
1706ccd979bdSMark Fasheh 		mlog_errno(status);
1707ccd979bdSMark Fasheh bail:
1708ccd979bdSMark Fasheh 	return status;
1709ccd979bdSMark Fasheh }
1710ccd979bdSMark Fasheh 
1711ccd979bdSMark Fasheh /*
1712ccd979bdSMark Fasheh  * expects the suballoc inode to already be locked.
1713ccd979bdSMark Fasheh  */
17142b604351SMark Fasheh int ocfs2_free_suballoc_bits(handle_t *handle,
1715ccd979bdSMark Fasheh 			     struct inode *alloc_inode,
1716ccd979bdSMark Fasheh 			     struct buffer_head *alloc_bh,
1717ccd979bdSMark Fasheh 			     unsigned int start_bit,
1718ccd979bdSMark Fasheh 			     u64 bg_blkno,
1719ccd979bdSMark Fasheh 			     unsigned int count)
1720ccd979bdSMark Fasheh {
1721ccd979bdSMark Fasheh 	int status = 0;
1722ccd979bdSMark Fasheh 	u32 tmp_used;
1723ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
1724ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1725ccd979bdSMark Fasheh 	struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1726ccd979bdSMark Fasheh 	struct buffer_head *group_bh = NULL;
1727ccd979bdSMark Fasheh 	struct ocfs2_group_desc *group;
1728ccd979bdSMark Fasheh 
1729ccd979bdSMark Fasheh 	mlog_entry_void();
1730ccd979bdSMark Fasheh 
1731ccd979bdSMark Fasheh 	if (!OCFS2_IS_VALID_DINODE(fe)) {
1732ccd979bdSMark Fasheh 		OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
1733ccd979bdSMark Fasheh 		status = -EIO;
1734ccd979bdSMark Fasheh 		goto bail;
1735ccd979bdSMark Fasheh 	}
1736ccd979bdSMark Fasheh 	BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1737ccd979bdSMark Fasheh 
1738b0697053SMark Fasheh 	mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1739b0697053SMark Fasheh 	     (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1740b0697053SMark Fasheh 	     (unsigned long long)bg_blkno, start_bit);
1741ccd979bdSMark Fasheh 
1742ccd979bdSMark Fasheh 	status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED,
1743ccd979bdSMark Fasheh 				  alloc_inode);
1744ccd979bdSMark Fasheh 	if (status < 0) {
1745ccd979bdSMark Fasheh 		mlog_errno(status);
1746ccd979bdSMark Fasheh 		goto bail;
1747ccd979bdSMark Fasheh 	}
1748ccd979bdSMark Fasheh 
1749ccd979bdSMark Fasheh 	group = (struct ocfs2_group_desc *) group_bh->b_data;
17507bf72edeSMark Fasheh 	status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, group);
17517bf72edeSMark Fasheh 	if (status) {
17527bf72edeSMark Fasheh 		mlog_errno(status);
1753ccd979bdSMark Fasheh 		goto bail;
1754ccd979bdSMark Fasheh 	}
1755ccd979bdSMark Fasheh 	BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1756ccd979bdSMark Fasheh 
1757ccd979bdSMark Fasheh 	status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1758ccd979bdSMark Fasheh 					      group, group_bh,
1759ccd979bdSMark Fasheh 					      start_bit, count);
1760ccd979bdSMark Fasheh 	if (status < 0) {
1761ccd979bdSMark Fasheh 		mlog_errno(status);
1762ccd979bdSMark Fasheh 		goto bail;
1763ccd979bdSMark Fasheh 	}
1764ccd979bdSMark Fasheh 
1765ccd979bdSMark Fasheh 	status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
1766ccd979bdSMark Fasheh 				      OCFS2_JOURNAL_ACCESS_WRITE);
1767ccd979bdSMark Fasheh 	if (status < 0) {
1768ccd979bdSMark Fasheh 		mlog_errno(status);
1769ccd979bdSMark Fasheh 		goto bail;
1770ccd979bdSMark Fasheh 	}
1771ccd979bdSMark Fasheh 
1772ccd979bdSMark Fasheh 	le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
1773ccd979bdSMark Fasheh 		     count);
1774ccd979bdSMark Fasheh 	tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1775ccd979bdSMark Fasheh 	fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
1776ccd979bdSMark Fasheh 
1777ccd979bdSMark Fasheh 	status = ocfs2_journal_dirty(handle, alloc_bh);
1778ccd979bdSMark Fasheh 	if (status < 0) {
1779ccd979bdSMark Fasheh 		mlog_errno(status);
1780ccd979bdSMark Fasheh 		goto bail;
1781ccd979bdSMark Fasheh 	}
1782ccd979bdSMark Fasheh 
1783ccd979bdSMark Fasheh bail:
1784ccd979bdSMark Fasheh 	if (group_bh)
1785ccd979bdSMark Fasheh 		brelse(group_bh);
1786ccd979bdSMark Fasheh 
1787ccd979bdSMark Fasheh 	mlog_exit(status);
1788ccd979bdSMark Fasheh 	return status;
1789ccd979bdSMark Fasheh }
1790ccd979bdSMark Fasheh 
17911fabe148SMark Fasheh int ocfs2_free_dinode(handle_t *handle,
1792ccd979bdSMark Fasheh 		      struct inode *inode_alloc_inode,
1793ccd979bdSMark Fasheh 		      struct buffer_head *inode_alloc_bh,
1794ccd979bdSMark Fasheh 		      struct ocfs2_dinode *di)
1795ccd979bdSMark Fasheh {
1796ccd979bdSMark Fasheh 	u64 blk = le64_to_cpu(di->i_blkno);
1797ccd979bdSMark Fasheh 	u16 bit = le16_to_cpu(di->i_suballoc_bit);
1798ccd979bdSMark Fasheh 	u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1799ccd979bdSMark Fasheh 
1800ccd979bdSMark Fasheh 	return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
1801ccd979bdSMark Fasheh 					inode_alloc_bh, bit, bg_blkno, 1);
1802ccd979bdSMark Fasheh }
1803ccd979bdSMark Fasheh 
18041fabe148SMark Fasheh int ocfs2_free_clusters(handle_t *handle,
1805ccd979bdSMark Fasheh 		       struct inode *bitmap_inode,
1806ccd979bdSMark Fasheh 		       struct buffer_head *bitmap_bh,
1807ccd979bdSMark Fasheh 		       u64 start_blk,
1808ccd979bdSMark Fasheh 		       unsigned int num_clusters)
1809ccd979bdSMark Fasheh {
1810ccd979bdSMark Fasheh 	int status;
1811ccd979bdSMark Fasheh 	u16 bg_start_bit;
1812ccd979bdSMark Fasheh 	u64 bg_blkno;
1813ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
1814ccd979bdSMark Fasheh 
1815ccd979bdSMark Fasheh 	/* You can't ever have a contiguous set of clusters
1816ccd979bdSMark Fasheh 	 * bigger than a block group bitmap so we never have to worry
1817ccd979bdSMark Fasheh 	 * about looping on them. */
1818ccd979bdSMark Fasheh 
1819ccd979bdSMark Fasheh 	mlog_entry_void();
1820ccd979bdSMark Fasheh 
1821ccd979bdSMark Fasheh 	/* This is expensive. We can safely remove once this stuff has
1822ccd979bdSMark Fasheh 	 * gotten tested really well. */
1823ccd979bdSMark Fasheh 	BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
1824ccd979bdSMark Fasheh 
1825ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
1826ccd979bdSMark Fasheh 
1827ccd979bdSMark Fasheh 	ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
1828ccd979bdSMark Fasheh 				     &bg_start_bit);
1829ccd979bdSMark Fasheh 
1830b0697053SMark Fasheh 	mlog(0, "want to free %u clusters starting at block %llu\n",
1831b0697053SMark Fasheh 	     num_clusters, (unsigned long long)start_blk);
1832b0697053SMark Fasheh 	mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1833b0697053SMark Fasheh 	     (unsigned long long)bg_blkno, bg_start_bit);
1834ccd979bdSMark Fasheh 
1835ccd979bdSMark Fasheh 	status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
1836ccd979bdSMark Fasheh 					  bg_start_bit, bg_blkno,
1837ccd979bdSMark Fasheh 					  num_clusters);
18389c7af40bSMark Fasheh 	if (status < 0) {
1839ccd979bdSMark Fasheh 		mlog_errno(status);
18409c7af40bSMark Fasheh 		goto out;
18419c7af40bSMark Fasheh 	}
1842ccd979bdSMark Fasheh 
18439c7af40bSMark Fasheh 	ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
18449c7af40bSMark Fasheh 					 num_clusters);
18459c7af40bSMark Fasheh 
18469c7af40bSMark Fasheh out:
1847ccd979bdSMark Fasheh 	mlog_exit(status);
1848ccd979bdSMark Fasheh 	return status;
1849ccd979bdSMark Fasheh }
1850ccd979bdSMark Fasheh 
1851ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
1852ccd979bdSMark Fasheh {
1853ccd979bdSMark Fasheh 	printk("Block Group:\n");
1854ccd979bdSMark Fasheh 	printk("bg_signature:       %s\n", bg->bg_signature);
1855ccd979bdSMark Fasheh 	printk("bg_size:            %u\n", bg->bg_size);
1856ccd979bdSMark Fasheh 	printk("bg_bits:            %u\n", bg->bg_bits);
1857ccd979bdSMark Fasheh 	printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
1858ccd979bdSMark Fasheh 	printk("bg_chain:           %u\n", bg->bg_chain);
1859ccd979bdSMark Fasheh 	printk("bg_generation:      %u\n", le32_to_cpu(bg->bg_generation));
1860b0697053SMark Fasheh 	printk("bg_next_group:      %llu\n",
1861b0697053SMark Fasheh 	       (unsigned long long)bg->bg_next_group);
1862b0697053SMark Fasheh 	printk("bg_parent_dinode:   %llu\n",
1863b0697053SMark Fasheh 	       (unsigned long long)bg->bg_parent_dinode);
1864b0697053SMark Fasheh 	printk("bg_blkno:           %llu\n",
1865b0697053SMark Fasheh 	       (unsigned long long)bg->bg_blkno);
1866ccd979bdSMark Fasheh }
1867ccd979bdSMark Fasheh 
1868ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
1869ccd979bdSMark Fasheh {
1870ccd979bdSMark Fasheh 	int i;
1871ccd979bdSMark Fasheh 
1872b0697053SMark Fasheh 	printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
1873ccd979bdSMark Fasheh 	printk("i_signature:                  %s\n", fe->i_signature);
1874b0697053SMark Fasheh 	printk("i_size:                       %llu\n",
1875b0697053SMark Fasheh 	       (unsigned long long)fe->i_size);
1876ccd979bdSMark Fasheh 	printk("i_clusters:                   %u\n", fe->i_clusters);
1877ccd979bdSMark Fasheh 	printk("i_generation:                 %u\n",
1878ccd979bdSMark Fasheh 	       le32_to_cpu(fe->i_generation));
1879ccd979bdSMark Fasheh 	printk("id1.bitmap1.i_used:           %u\n",
1880ccd979bdSMark Fasheh 	       le32_to_cpu(fe->id1.bitmap1.i_used));
1881ccd979bdSMark Fasheh 	printk("id1.bitmap1.i_total:          %u\n",
1882ccd979bdSMark Fasheh 	       le32_to_cpu(fe->id1.bitmap1.i_total));
1883ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_cpg:           %u\n", fe->id2.i_chain.cl_cpg);
1884ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_bpc:           %u\n", fe->id2.i_chain.cl_bpc);
1885ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_count:         %u\n", fe->id2.i_chain.cl_count);
1886ccd979bdSMark Fasheh 	printk("id2.i_chain.cl_next_free_rec: %u\n",
1887ccd979bdSMark Fasheh 	       fe->id2.i_chain.cl_next_free_rec);
1888ccd979bdSMark Fasheh 	for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
1889ccd979bdSMark Fasheh 		printk("fe->id2.i_chain.cl_recs[%d].c_free:  %u\n", i,
1890ccd979bdSMark Fasheh 		       fe->id2.i_chain.cl_recs[i].c_free);
1891ccd979bdSMark Fasheh 		printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
1892ccd979bdSMark Fasheh 		       fe->id2.i_chain.cl_recs[i].c_total);
1893b0697053SMark Fasheh 		printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
1894b0697053SMark Fasheh 		       (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
1895ccd979bdSMark Fasheh 	}
1896ccd979bdSMark Fasheh }
1897*e7d4cb6bSTao Ma 
1898*e7d4cb6bSTao Ma /*
1899*e7d4cb6bSTao Ma  * For a given allocation, determine which allocators will need to be
1900*e7d4cb6bSTao Ma  * accessed, and lock them, reserving the appropriate number of bits.
1901*e7d4cb6bSTao Ma  *
1902*e7d4cb6bSTao Ma  * Sparse file systems call this from ocfs2_write_begin_nolock()
1903*e7d4cb6bSTao Ma  * and ocfs2_allocate_unwritten_extents().
1904*e7d4cb6bSTao Ma  *
1905*e7d4cb6bSTao Ma  * File systems which don't support holes call this from
1906*e7d4cb6bSTao Ma  * ocfs2_extend_allocation().
1907*e7d4cb6bSTao Ma  */
1908*e7d4cb6bSTao Ma int ocfs2_lock_allocators(struct inode *inode, struct buffer_head *root_bh,
1909*e7d4cb6bSTao Ma 			  struct ocfs2_extent_list *root_el,
1910*e7d4cb6bSTao Ma 			  u32 clusters_to_add, u32 extents_to_split,
1911*e7d4cb6bSTao Ma 			  struct ocfs2_alloc_context **data_ac,
1912*e7d4cb6bSTao Ma 			  struct ocfs2_alloc_context **meta_ac)
1913*e7d4cb6bSTao Ma {
1914*e7d4cb6bSTao Ma 	int ret = 0, num_free_extents;
1915*e7d4cb6bSTao Ma 	unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
1916*e7d4cb6bSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1917*e7d4cb6bSTao Ma 
1918*e7d4cb6bSTao Ma 	*meta_ac = NULL;
1919*e7d4cb6bSTao Ma 	if (data_ac)
1920*e7d4cb6bSTao Ma 		*data_ac = NULL;
1921*e7d4cb6bSTao Ma 
1922*e7d4cb6bSTao Ma 	BUG_ON(clusters_to_add != 0 && data_ac == NULL);
1923*e7d4cb6bSTao Ma 
1924*e7d4cb6bSTao Ma 	num_free_extents = ocfs2_num_free_extents(osb, inode, root_bh,
1925*e7d4cb6bSTao Ma 						  OCFS2_DINODE_EXTENT);
1926*e7d4cb6bSTao Ma 	if (num_free_extents < 0) {
1927*e7d4cb6bSTao Ma 		ret = num_free_extents;
1928*e7d4cb6bSTao Ma 		mlog_errno(ret);
1929*e7d4cb6bSTao Ma 		goto out;
1930*e7d4cb6bSTao Ma 	}
1931*e7d4cb6bSTao Ma 
1932*e7d4cb6bSTao Ma 	/*
1933*e7d4cb6bSTao Ma 	 * Sparse allocation file systems need to be more conservative
1934*e7d4cb6bSTao Ma 	 * with reserving room for expansion - the actual allocation
1935*e7d4cb6bSTao Ma 	 * happens while we've got a journal handle open so re-taking
1936*e7d4cb6bSTao Ma 	 * a cluster lock (because we ran out of room for another
1937*e7d4cb6bSTao Ma 	 * extent) will violate ordering rules.
1938*e7d4cb6bSTao Ma 	 *
1939*e7d4cb6bSTao Ma 	 * Most of the time we'll only be seeing this 1 cluster at a time
1940*e7d4cb6bSTao Ma 	 * anyway.
1941*e7d4cb6bSTao Ma 	 *
1942*e7d4cb6bSTao Ma 	 * Always lock for any unwritten extents - we might want to
1943*e7d4cb6bSTao Ma 	 * add blocks during a split.
1944*e7d4cb6bSTao Ma 	 */
1945*e7d4cb6bSTao Ma 	if (!num_free_extents ||
1946*e7d4cb6bSTao Ma 	    (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
1947*e7d4cb6bSTao Ma 		ret = ocfs2_reserve_new_metadata(osb, root_el, meta_ac);
1948*e7d4cb6bSTao Ma 		if (ret < 0) {
1949*e7d4cb6bSTao Ma 			if (ret != -ENOSPC)
1950*e7d4cb6bSTao Ma 				mlog_errno(ret);
1951*e7d4cb6bSTao Ma 			goto out;
1952*e7d4cb6bSTao Ma 		}
1953*e7d4cb6bSTao Ma 	}
1954*e7d4cb6bSTao Ma 
1955*e7d4cb6bSTao Ma 	if (clusters_to_add == 0)
1956*e7d4cb6bSTao Ma 		goto out;
1957*e7d4cb6bSTao Ma 
1958*e7d4cb6bSTao Ma 	ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
1959*e7d4cb6bSTao Ma 	if (ret < 0) {
1960*e7d4cb6bSTao Ma 		if (ret != -ENOSPC)
1961*e7d4cb6bSTao Ma 			mlog_errno(ret);
1962*e7d4cb6bSTao Ma 		goto out;
1963*e7d4cb6bSTao Ma 	}
1964*e7d4cb6bSTao Ma 
1965*e7d4cb6bSTao Ma out:
1966*e7d4cb6bSTao Ma 	if (ret) {
1967*e7d4cb6bSTao Ma 		if (*meta_ac) {
1968*e7d4cb6bSTao Ma 			ocfs2_free_alloc_context(*meta_ac);
1969*e7d4cb6bSTao Ma 			*meta_ac = NULL;
1970*e7d4cb6bSTao Ma 		}
1971*e7d4cb6bSTao Ma 
1972*e7d4cb6bSTao Ma 		/*
1973*e7d4cb6bSTao Ma 		 * We cannot have an error and a non null *data_ac.
1974*e7d4cb6bSTao Ma 		 */
1975*e7d4cb6bSTao Ma 	}
1976*e7d4cb6bSTao Ma 
1977*e7d4cb6bSTao Ma 	return ret;
1978*e7d4cb6bSTao Ma }
1979