xref: /openbmc/linux/fs/ocfs2/suballoc.c (revision 8a517c514d5893602cf85c1b4c47afbbc04d2198)
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * suballoc.c
5  *
6  * metadata alloc and free
7  * Inspired by ext3 block groups.
8  *
9  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public
22  * License along with this program; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 021110-1307, USA.
25  */
26 
27 #include <linux/fs.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
31 
32 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33 #include <cluster/masklog.h>
34 
35 #include "ocfs2.h"
36 
37 #include "alloc.h"
38 #include "blockcheck.h"
39 #include "dlmglue.h"
40 #include "inode.h"
41 #include "journal.h"
42 #include "localalloc.h"
43 #include "suballoc.h"
44 #include "super.h"
45 #include "sysfile.h"
46 #include "uptodate.h"
47 
48 #include "buffer_head_io.h"
49 
50 #define NOT_ALLOC_NEW_GROUP		0
51 #define ALLOC_NEW_GROUP			0x1
52 #define ALLOC_GROUPS_FROM_GLOBAL	0x2
53 
54 #define OCFS2_MAX_INODES_TO_STEAL	1024
55 
56 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
57 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
58 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
59 static int ocfs2_block_group_fill(handle_t *handle,
60 				  struct inode *alloc_inode,
61 				  struct buffer_head *bg_bh,
62 				  u64 group_blkno,
63 				  u16 my_chain,
64 				  struct ocfs2_chain_list *cl);
65 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
66 				   struct inode *alloc_inode,
67 				   struct buffer_head *bh,
68 				   u64 max_block,
69 				   u64 *last_alloc_group,
70 				   int flags);
71 
72 static int ocfs2_cluster_group_search(struct inode *inode,
73 				      struct buffer_head *group_bh,
74 				      u32 bits_wanted, u32 min_bits,
75 				      u64 max_block,
76 				      u16 *bit_off, u16 *bits_found);
77 static int ocfs2_block_group_search(struct inode *inode,
78 				    struct buffer_head *group_bh,
79 				    u32 bits_wanted, u32 min_bits,
80 				    u64 max_block,
81 				    u16 *bit_off, u16 *bits_found);
82 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
83 				     struct ocfs2_alloc_context *ac,
84 				     handle_t *handle,
85 				     u32 bits_wanted,
86 				     u32 min_bits,
87 				     u16 *bit_off,
88 				     unsigned int *num_bits,
89 				     u64 *bg_blkno);
90 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
91 					 int nr);
92 static inline int ocfs2_block_group_set_bits(handle_t *handle,
93 					     struct inode *alloc_inode,
94 					     struct ocfs2_group_desc *bg,
95 					     struct buffer_head *group_bh,
96 					     unsigned int bit_off,
97 					     unsigned int num_bits);
98 static inline int ocfs2_block_group_clear_bits(handle_t *handle,
99 					       struct inode *alloc_inode,
100 					       struct ocfs2_group_desc *bg,
101 					       struct buffer_head *group_bh,
102 					       unsigned int bit_off,
103 					       unsigned int num_bits);
104 
105 static int ocfs2_relink_block_group(handle_t *handle,
106 				    struct inode *alloc_inode,
107 				    struct buffer_head *fe_bh,
108 				    struct buffer_head *bg_bh,
109 				    struct buffer_head *prev_bg_bh,
110 				    u16 chain);
111 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
112 						     u32 wanted);
113 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
114 						   u64 bg_blkno,
115 						   u16 bg_bit_off);
116 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
117 						u64 data_blkno,
118 						u64 *bg_blkno,
119 						u16 *bg_bit_off);
120 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
121 					     u32 bits_wanted, u64 max_block,
122 					     int flags,
123 					     struct ocfs2_alloc_context **ac);
124 
125 void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
126 {
127 	struct inode *inode = ac->ac_inode;
128 
129 	if (inode) {
130 		if (ac->ac_which != OCFS2_AC_USE_LOCAL)
131 			ocfs2_inode_unlock(inode, 1);
132 
133 		mutex_unlock(&inode->i_mutex);
134 
135 		iput(inode);
136 		ac->ac_inode = NULL;
137 	}
138 	brelse(ac->ac_bh);
139 	ac->ac_bh = NULL;
140 }
141 
142 void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
143 {
144 	ocfs2_free_ac_resource(ac);
145 	kfree(ac);
146 }
147 
148 static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
149 {
150 	return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
151 }
152 
153 #define do_error(fmt, ...)						\
154 	do{								\
155 		if (clean_error)					\
156 			mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__);	\
157 		else							\
158 			ocfs2_error(sb, fmt, ##__VA_ARGS__);		\
159 	} while (0)
160 
161 static int ocfs2_validate_gd_self(struct super_block *sb,
162 				  struct buffer_head *bh,
163 				  int clean_error)
164 {
165 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
166 
167 	if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
168 		do_error("Group descriptor #%llu has bad signature %.*s",
169 			 (unsigned long long)bh->b_blocknr, 7,
170 			 gd->bg_signature);
171 		return -EINVAL;
172 	}
173 
174 	if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
175 		do_error("Group descriptor #%llu has an invalid bg_blkno "
176 			 "of %llu",
177 			 (unsigned long long)bh->b_blocknr,
178 			 (unsigned long long)le64_to_cpu(gd->bg_blkno));
179 		return -EINVAL;
180 	}
181 
182 	if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
183 		do_error("Group descriptor #%llu has an invalid "
184 			 "fs_generation of #%u",
185 			 (unsigned long long)bh->b_blocknr,
186 			 le32_to_cpu(gd->bg_generation));
187 		return -EINVAL;
188 	}
189 
190 	if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
191 		do_error("Group descriptor #%llu has bit count %u but "
192 			 "claims that %u are free",
193 			 (unsigned long long)bh->b_blocknr,
194 			 le16_to_cpu(gd->bg_bits),
195 			 le16_to_cpu(gd->bg_free_bits_count));
196 		return -EINVAL;
197 	}
198 
199 	if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
200 		do_error("Group descriptor #%llu has bit count %u but "
201 			 "max bitmap bits of %u",
202 			 (unsigned long long)bh->b_blocknr,
203 			 le16_to_cpu(gd->bg_bits),
204 			 8 * le16_to_cpu(gd->bg_size));
205 		return -EINVAL;
206 	}
207 
208 	return 0;
209 }
210 
211 static int ocfs2_validate_gd_parent(struct super_block *sb,
212 				    struct ocfs2_dinode *di,
213 				    struct buffer_head *bh,
214 				    int clean_error)
215 {
216 	unsigned int max_bits;
217 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
218 
219 	if (di->i_blkno != gd->bg_parent_dinode) {
220 		do_error("Group descriptor #%llu has bad parent "
221 			 "pointer (%llu, expected %llu)",
222 			 (unsigned long long)bh->b_blocknr,
223 			 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
224 			 (unsigned long long)le64_to_cpu(di->i_blkno));
225 		return -EINVAL;
226 	}
227 
228 	max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
229 	if (le16_to_cpu(gd->bg_bits) > max_bits) {
230 		do_error("Group descriptor #%llu has bit count of %u",
231 			 (unsigned long long)bh->b_blocknr,
232 			 le16_to_cpu(gd->bg_bits));
233 		return -EINVAL;
234 	}
235 
236 	if (le16_to_cpu(gd->bg_chain) >=
237 	    le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
238 		do_error("Group descriptor #%llu has bad chain %u",
239 			 (unsigned long long)bh->b_blocknr,
240 			 le16_to_cpu(gd->bg_chain));
241 		return -EINVAL;
242 	}
243 
244 	return 0;
245 }
246 
247 #undef do_error
248 
249 /*
250  * This version only prints errors.  It does not fail the filesystem, and
251  * exists only for resize.
252  */
253 int ocfs2_check_group_descriptor(struct super_block *sb,
254 				 struct ocfs2_dinode *di,
255 				 struct buffer_head *bh)
256 {
257 	int rc;
258 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
259 
260 	BUG_ON(!buffer_uptodate(bh));
261 
262 	/*
263 	 * If the ecc fails, we return the error but otherwise
264 	 * leave the filesystem running.  We know any error is
265 	 * local to this block.
266 	 */
267 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
268 	if (rc) {
269 		mlog(ML_ERROR,
270 		     "Checksum failed for group descriptor %llu\n",
271 		     (unsigned long long)bh->b_blocknr);
272 	} else
273 		rc = ocfs2_validate_gd_self(sb, bh, 1);
274 	if (!rc)
275 		rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
276 
277 	return rc;
278 }
279 
280 static int ocfs2_validate_group_descriptor(struct super_block *sb,
281 					   struct buffer_head *bh)
282 {
283 	int rc;
284 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
285 
286 	mlog(0, "Validating group descriptor %llu\n",
287 	     (unsigned long long)bh->b_blocknr);
288 
289 	BUG_ON(!buffer_uptodate(bh));
290 
291 	/*
292 	 * If the ecc fails, we return the error but otherwise
293 	 * leave the filesystem running.  We know any error is
294 	 * local to this block.
295 	 */
296 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
297 	if (rc)
298 		return rc;
299 
300 	/*
301 	 * Errors after here are fatal.
302 	 */
303 
304 	return ocfs2_validate_gd_self(sb, bh, 0);
305 }
306 
307 int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
308 				u64 gd_blkno, struct buffer_head **bh)
309 {
310 	int rc;
311 	struct buffer_head *tmp = *bh;
312 
313 	rc = ocfs2_read_block(inode, gd_blkno, &tmp,
314 			      ocfs2_validate_group_descriptor);
315 	if (rc)
316 		goto out;
317 
318 	rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
319 	if (rc) {
320 		brelse(tmp);
321 		goto out;
322 	}
323 
324 	/* If ocfs2_read_block() got us a new bh, pass it up. */
325 	if (!*bh)
326 		*bh = tmp;
327 
328 out:
329 	return rc;
330 }
331 
332 static int ocfs2_block_group_fill(handle_t *handle,
333 				  struct inode *alloc_inode,
334 				  struct buffer_head *bg_bh,
335 				  u64 group_blkno,
336 				  u16 my_chain,
337 				  struct ocfs2_chain_list *cl)
338 {
339 	int status = 0;
340 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
341 	struct super_block * sb = alloc_inode->i_sb;
342 
343 	mlog_entry_void();
344 
345 	if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
346 		ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
347 			    "b_blocknr (%llu)",
348 			    (unsigned long long)group_blkno,
349 			    (unsigned long long) bg_bh->b_blocknr);
350 		status = -EIO;
351 		goto bail;
352 	}
353 
354 	status = ocfs2_journal_access_gd(handle,
355 					 alloc_inode,
356 					 bg_bh,
357 					 OCFS2_JOURNAL_ACCESS_CREATE);
358 	if (status < 0) {
359 		mlog_errno(status);
360 		goto bail;
361 	}
362 
363 	memset(bg, 0, sb->s_blocksize);
364 	strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
365 	bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
366 	bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
367 	bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
368 	bg->bg_chain = cpu_to_le16(my_chain);
369 	bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
370 	bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
371 	bg->bg_blkno = cpu_to_le64(group_blkno);
372 	/* set the 1st bit in the bitmap to account for the descriptor block */
373 	ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
374 	bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
375 
376 	status = ocfs2_journal_dirty(handle, bg_bh);
377 	if (status < 0)
378 		mlog_errno(status);
379 
380 	/* There is no need to zero out or otherwise initialize the
381 	 * other blocks in a group - All valid FS metadata in a block
382 	 * group stores the superblock fs_generation value at
383 	 * allocation time. */
384 
385 bail:
386 	mlog_exit(status);
387 	return status;
388 }
389 
390 static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
391 {
392 	u16 curr, best;
393 
394 	best = curr = 0;
395 	while (curr < le16_to_cpu(cl->cl_count)) {
396 		if (le32_to_cpu(cl->cl_recs[best].c_total) >
397 		    le32_to_cpu(cl->cl_recs[curr].c_total))
398 			best = curr;
399 		curr++;
400 	}
401 	return best;
402 }
403 
404 /*
405  * We expect the block group allocator to already be locked.
406  */
407 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
408 				   struct inode *alloc_inode,
409 				   struct buffer_head *bh,
410 				   u64 max_block,
411 				   u64 *last_alloc_group,
412 				   int flags)
413 {
414 	int status, credits;
415 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
416 	struct ocfs2_chain_list *cl;
417 	struct ocfs2_alloc_context *ac = NULL;
418 	handle_t *handle = NULL;
419 	u32 bit_off, num_bits;
420 	u16 alloc_rec;
421 	u64 bg_blkno;
422 	struct buffer_head *bg_bh = NULL;
423 	struct ocfs2_group_desc *bg;
424 
425 	BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
426 
427 	mlog_entry_void();
428 
429 	cl = &fe->id2.i_chain;
430 	status = ocfs2_reserve_clusters_with_limit(osb,
431 						   le16_to_cpu(cl->cl_cpg),
432 						   max_block, flags, &ac);
433 	if (status < 0) {
434 		if (status != -ENOSPC)
435 			mlog_errno(status);
436 		goto bail;
437 	}
438 
439 	credits = ocfs2_calc_group_alloc_credits(osb->sb,
440 						 le16_to_cpu(cl->cl_cpg));
441 	handle = ocfs2_start_trans(osb, credits);
442 	if (IS_ERR(handle)) {
443 		status = PTR_ERR(handle);
444 		handle = NULL;
445 		mlog_errno(status);
446 		goto bail;
447 	}
448 
449 	if (last_alloc_group && *last_alloc_group != 0) {
450 		mlog(0, "use old allocation group %llu for block group alloc\n",
451 		     (unsigned long long)*last_alloc_group);
452 		ac->ac_last_group = *last_alloc_group;
453 	}
454 	status = ocfs2_claim_clusters(osb,
455 				      handle,
456 				      ac,
457 				      le16_to_cpu(cl->cl_cpg),
458 				      &bit_off,
459 				      &num_bits);
460 	if (status < 0) {
461 		if (status != -ENOSPC)
462 			mlog_errno(status);
463 		goto bail;
464 	}
465 
466 	alloc_rec = ocfs2_find_smallest_chain(cl);
467 
468 	/* setup the group */
469 	bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
470 	mlog(0, "new descriptor, record %u, at block %llu\n",
471 	     alloc_rec, (unsigned long long)bg_blkno);
472 
473 	bg_bh = sb_getblk(osb->sb, bg_blkno);
474 	if (!bg_bh) {
475 		status = -EIO;
476 		mlog_errno(status);
477 		goto bail;
478 	}
479 	ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
480 
481 	status = ocfs2_block_group_fill(handle,
482 					alloc_inode,
483 					bg_bh,
484 					bg_blkno,
485 					alloc_rec,
486 					cl);
487 	if (status < 0) {
488 		mlog_errno(status);
489 		goto bail;
490 	}
491 
492 	bg = (struct ocfs2_group_desc *) bg_bh->b_data;
493 
494 	status = ocfs2_journal_access_di(handle, alloc_inode,
495 					 bh, OCFS2_JOURNAL_ACCESS_WRITE);
496 	if (status < 0) {
497 		mlog_errno(status);
498 		goto bail;
499 	}
500 
501 	le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
502 		     le16_to_cpu(bg->bg_free_bits_count));
503 	le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
504 	cl->cl_recs[alloc_rec].c_blkno  = cpu_to_le64(bg_blkno);
505 	if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
506 		le16_add_cpu(&cl->cl_next_free_rec, 1);
507 
508 	le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
509 					le16_to_cpu(bg->bg_free_bits_count));
510 	le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
511 	le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
512 
513 	status = ocfs2_journal_dirty(handle, bh);
514 	if (status < 0) {
515 		mlog_errno(status);
516 		goto bail;
517 	}
518 
519 	spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
520 	OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
521 	fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
522 					     le32_to_cpu(fe->i_clusters)));
523 	spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
524 	i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
525 	alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
526 
527 	status = 0;
528 
529 	/* save the new last alloc group so that the caller can cache it. */
530 	if (last_alloc_group)
531 		*last_alloc_group = ac->ac_last_group;
532 
533 bail:
534 	if (handle)
535 		ocfs2_commit_trans(osb, handle);
536 
537 	if (ac)
538 		ocfs2_free_alloc_context(ac);
539 
540 	brelse(bg_bh);
541 
542 	mlog_exit(status);
543 	return status;
544 }
545 
546 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
547 				       struct ocfs2_alloc_context *ac,
548 				       int type,
549 				       u32 slot,
550 				       u64 *last_alloc_group,
551 				       int flags)
552 {
553 	int status;
554 	u32 bits_wanted = ac->ac_bits_wanted;
555 	struct inode *alloc_inode;
556 	struct buffer_head *bh = NULL;
557 	struct ocfs2_dinode *fe;
558 	u32 free_bits;
559 
560 	mlog_entry_void();
561 
562 	alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
563 	if (!alloc_inode) {
564 		mlog_errno(-EINVAL);
565 		return -EINVAL;
566 	}
567 
568 	mutex_lock(&alloc_inode->i_mutex);
569 
570 	status = ocfs2_inode_lock(alloc_inode, &bh, 1);
571 	if (status < 0) {
572 		mutex_unlock(&alloc_inode->i_mutex);
573 		iput(alloc_inode);
574 
575 		mlog_errno(status);
576 		return status;
577 	}
578 
579 	ac->ac_inode = alloc_inode;
580 	ac->ac_alloc_slot = slot;
581 
582 	fe = (struct ocfs2_dinode *) bh->b_data;
583 
584 	/* The bh was validated by the inode read inside
585 	 * ocfs2_inode_lock().  Any corruption is a code bug. */
586 	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
587 
588 	if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
589 		ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
590 			    (unsigned long long)le64_to_cpu(fe->i_blkno));
591 		status = -EIO;
592 		goto bail;
593 	}
594 
595 	free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
596 		le32_to_cpu(fe->id1.bitmap1.i_used);
597 
598 	if (bits_wanted > free_bits) {
599 		/* cluster bitmap never grows */
600 		if (ocfs2_is_cluster_bitmap(alloc_inode)) {
601 			mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
602 			     bits_wanted, free_bits);
603 			status = -ENOSPC;
604 			goto bail;
605 		}
606 
607 		if (!(flags & ALLOC_NEW_GROUP)) {
608 			mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
609 			     "and we don't alloc a new group for it.\n",
610 			     slot, bits_wanted, free_bits);
611 			status = -ENOSPC;
612 			goto bail;
613 		}
614 
615 		status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
616 						 ac->ac_max_block,
617 						 last_alloc_group, flags);
618 		if (status < 0) {
619 			if (status != -ENOSPC)
620 				mlog_errno(status);
621 			goto bail;
622 		}
623 		atomic_inc(&osb->alloc_stats.bg_extends);
624 
625 		/* You should never ask for this much metadata */
626 		BUG_ON(bits_wanted >
627 		       (le32_to_cpu(fe->id1.bitmap1.i_total)
628 			- le32_to_cpu(fe->id1.bitmap1.i_used)));
629 	}
630 
631 	get_bh(bh);
632 	ac->ac_bh = bh;
633 bail:
634 	brelse(bh);
635 
636 	mlog_exit(status);
637 	return status;
638 }
639 
640 int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
641 				      int blocks,
642 				      struct ocfs2_alloc_context **ac)
643 {
644 	int status;
645 	u32 slot;
646 
647 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
648 	if (!(*ac)) {
649 		status = -ENOMEM;
650 		mlog_errno(status);
651 		goto bail;
652 	}
653 
654 	(*ac)->ac_bits_wanted = blocks;
655 	(*ac)->ac_which = OCFS2_AC_USE_META;
656 	slot = osb->slot_num;
657 	(*ac)->ac_group_search = ocfs2_block_group_search;
658 
659 	status = ocfs2_reserve_suballoc_bits(osb, (*ac),
660 					     EXTENT_ALLOC_SYSTEM_INODE,
661 					     slot, NULL, ALLOC_NEW_GROUP);
662 	if (status < 0) {
663 		if (status != -ENOSPC)
664 			mlog_errno(status);
665 		goto bail;
666 	}
667 
668 	status = 0;
669 bail:
670 	if ((status < 0) && *ac) {
671 		ocfs2_free_alloc_context(*ac);
672 		*ac = NULL;
673 	}
674 
675 	mlog_exit(status);
676 	return status;
677 }
678 
679 int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
680 			       struct ocfs2_extent_list *root_el,
681 			       struct ocfs2_alloc_context **ac)
682 {
683 	return ocfs2_reserve_new_metadata_blocks(osb,
684 					ocfs2_extend_meta_needed(root_el),
685 					ac);
686 }
687 
688 static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
689 					      struct ocfs2_alloc_context *ac)
690 {
691 	int i, status = -ENOSPC;
692 	s16 slot = ocfs2_get_inode_steal_slot(osb);
693 
694 	/* Start to steal inodes from the first slot after ours. */
695 	if (slot == OCFS2_INVALID_SLOT)
696 		slot = osb->slot_num + 1;
697 
698 	for (i = 0; i < osb->max_slots; i++, slot++) {
699 		if (slot == osb->max_slots)
700 			slot = 0;
701 
702 		if (slot == osb->slot_num)
703 			continue;
704 
705 		status = ocfs2_reserve_suballoc_bits(osb, ac,
706 						     INODE_ALLOC_SYSTEM_INODE,
707 						     slot, NULL,
708 						     NOT_ALLOC_NEW_GROUP);
709 		if (status >= 0) {
710 			ocfs2_set_inode_steal_slot(osb, slot);
711 			break;
712 		}
713 
714 		ocfs2_free_ac_resource(ac);
715 	}
716 
717 	return status;
718 }
719 
720 int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
721 			    struct ocfs2_alloc_context **ac)
722 {
723 	int status;
724 	s16 slot = ocfs2_get_inode_steal_slot(osb);
725 	u64 alloc_group;
726 
727 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
728 	if (!(*ac)) {
729 		status = -ENOMEM;
730 		mlog_errno(status);
731 		goto bail;
732 	}
733 
734 	(*ac)->ac_bits_wanted = 1;
735 	(*ac)->ac_which = OCFS2_AC_USE_INODE;
736 
737 	(*ac)->ac_group_search = ocfs2_block_group_search;
738 
739 	/*
740 	 * stat(2) can't handle i_ino > 32bits, so we tell the
741 	 * lower levels not to allocate us a block group past that
742 	 * limit.  The 'inode64' mount option avoids this behavior.
743 	 */
744 	if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
745 		(*ac)->ac_max_block = (u32)~0U;
746 
747 	/*
748 	 * slot is set when we successfully steal inode from other nodes.
749 	 * It is reset in 3 places:
750 	 * 1. when we flush the truncate log
751 	 * 2. when we complete local alloc recovery.
752 	 * 3. when we successfully allocate from our own slot.
753 	 * After it is set, we will go on stealing inodes until we find the
754 	 * need to check our slots to see whether there is some space for us.
755 	 */
756 	if (slot != OCFS2_INVALID_SLOT &&
757 	    atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_INODES_TO_STEAL)
758 		goto inode_steal;
759 
760 	atomic_set(&osb->s_num_inodes_stolen, 0);
761 	alloc_group = osb->osb_inode_alloc_group;
762 	status = ocfs2_reserve_suballoc_bits(osb, *ac,
763 					     INODE_ALLOC_SYSTEM_INODE,
764 					     osb->slot_num,
765 					     &alloc_group,
766 					     ALLOC_NEW_GROUP |
767 					     ALLOC_GROUPS_FROM_GLOBAL);
768 	if (status >= 0) {
769 		status = 0;
770 
771 		spin_lock(&osb->osb_lock);
772 		osb->osb_inode_alloc_group = alloc_group;
773 		spin_unlock(&osb->osb_lock);
774 		mlog(0, "after reservation, new allocation group is "
775 		     "%llu\n", (unsigned long long)alloc_group);
776 
777 		/*
778 		 * Some inodes must be freed by us, so try to allocate
779 		 * from our own next time.
780 		 */
781 		if (slot != OCFS2_INVALID_SLOT)
782 			ocfs2_init_inode_steal_slot(osb);
783 		goto bail;
784 	} else if (status < 0 && status != -ENOSPC) {
785 		mlog_errno(status);
786 		goto bail;
787 	}
788 
789 	ocfs2_free_ac_resource(*ac);
790 
791 inode_steal:
792 	status = ocfs2_steal_inode_from_other_nodes(osb, *ac);
793 	atomic_inc(&osb->s_num_inodes_stolen);
794 	if (status < 0) {
795 		if (status != -ENOSPC)
796 			mlog_errno(status);
797 		goto bail;
798 	}
799 
800 	status = 0;
801 bail:
802 	if ((status < 0) && *ac) {
803 		ocfs2_free_alloc_context(*ac);
804 		*ac = NULL;
805 	}
806 
807 	mlog_exit(status);
808 	return status;
809 }
810 
811 /* local alloc code has to do the same thing, so rather than do this
812  * twice.. */
813 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
814 				      struct ocfs2_alloc_context *ac)
815 {
816 	int status;
817 
818 	ac->ac_which = OCFS2_AC_USE_MAIN;
819 	ac->ac_group_search = ocfs2_cluster_group_search;
820 
821 	status = ocfs2_reserve_suballoc_bits(osb, ac,
822 					     GLOBAL_BITMAP_SYSTEM_INODE,
823 					     OCFS2_INVALID_SLOT, NULL,
824 					     ALLOC_NEW_GROUP);
825 	if (status < 0 && status != -ENOSPC) {
826 		mlog_errno(status);
827 		goto bail;
828 	}
829 
830 bail:
831 	return status;
832 }
833 
834 /* Callers don't need to care which bitmap (local alloc or main) to
835  * use so we figure it out for them, but unfortunately this clutters
836  * things a bit. */
837 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
838 					     u32 bits_wanted, u64 max_block,
839 					     int flags,
840 					     struct ocfs2_alloc_context **ac)
841 {
842 	int status;
843 
844 	mlog_entry_void();
845 
846 	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
847 	if (!(*ac)) {
848 		status = -ENOMEM;
849 		mlog_errno(status);
850 		goto bail;
851 	}
852 
853 	(*ac)->ac_bits_wanted = bits_wanted;
854 	(*ac)->ac_max_block = max_block;
855 
856 	status = -ENOSPC;
857 	if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
858 	    ocfs2_alloc_should_use_local(osb, bits_wanted)) {
859 		status = ocfs2_reserve_local_alloc_bits(osb,
860 							bits_wanted,
861 							*ac);
862 		if (status == -EFBIG) {
863 			/* The local alloc window is outside ac_max_block.
864 			 * use the main bitmap. */
865 			status = -ENOSPC;
866 		} else if ((status < 0) && (status != -ENOSPC)) {
867 			mlog_errno(status);
868 			goto bail;
869 		}
870 	}
871 
872 	if (status == -ENOSPC) {
873 		status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
874 		if (status < 0) {
875 			if (status != -ENOSPC)
876 				mlog_errno(status);
877 			goto bail;
878 		}
879 	}
880 
881 	status = 0;
882 bail:
883 	if ((status < 0) && *ac) {
884 		ocfs2_free_alloc_context(*ac);
885 		*ac = NULL;
886 	}
887 
888 	mlog_exit(status);
889 	return status;
890 }
891 
892 int ocfs2_reserve_clusters(struct ocfs2_super *osb,
893 			   u32 bits_wanted,
894 			   struct ocfs2_alloc_context **ac)
895 {
896 	return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
897 						 ALLOC_NEW_GROUP, ac);
898 }
899 
900 /*
901  * More or less lifted from ext3. I'll leave their description below:
902  *
903  * "For ext3 allocations, we must not reuse any blocks which are
904  * allocated in the bitmap buffer's "last committed data" copy.  This
905  * prevents deletes from freeing up the page for reuse until we have
906  * committed the delete transaction.
907  *
908  * If we didn't do this, then deleting something and reallocating it as
909  * data would allow the old block to be overwritten before the
910  * transaction committed (because we force data to disk before commit).
911  * This would lead to corruption if we crashed between overwriting the
912  * data and committing the delete.
913  *
914  * @@@ We may want to make this allocation behaviour conditional on
915  * data-writes at some point, and disable it for metadata allocations or
916  * sync-data inodes."
917  *
918  * Note: OCFS2 already does this differently for metadata vs data
919  * allocations, as those bitmaps are separate and undo access is never
920  * called on a metadata group descriptor.
921  */
922 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
923 					 int nr)
924 {
925 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
926 	int ret;
927 
928 	if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
929 		return 0;
930 
931 	if (!buffer_jbd(bg_bh))
932 		return 1;
933 
934 	jbd_lock_bh_state(bg_bh);
935 	bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
936 	if (bg)
937 		ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
938 	else
939 		ret = 1;
940 	jbd_unlock_bh_state(bg_bh);
941 
942 	return ret;
943 }
944 
945 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
946 					     struct buffer_head *bg_bh,
947 					     unsigned int bits_wanted,
948 					     unsigned int total_bits,
949 					     u16 *bit_off,
950 					     u16 *bits_found)
951 {
952 	void *bitmap;
953 	u16 best_offset, best_size;
954 	int offset, start, found, status = 0;
955 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
956 
957 	/* Callers got this descriptor from
958 	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
959 	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
960 
961 	found = start = best_offset = best_size = 0;
962 	bitmap = bg->bg_bitmap;
963 
964 	while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
965 		if (offset == total_bits)
966 			break;
967 
968 		if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
969 			/* We found a zero, but we can't use it as it
970 			 * hasn't been put to disk yet! */
971 			found = 0;
972 			start = offset + 1;
973 		} else if (offset == start) {
974 			/* we found a zero */
975 			found++;
976 			/* move start to the next bit to test */
977 			start++;
978 		} else {
979 			/* got a zero after some ones */
980 			found = 1;
981 			start = offset + 1;
982 		}
983 		if (found > best_size) {
984 			best_size = found;
985 			best_offset = start - found;
986 		}
987 		/* we got everything we needed */
988 		if (found == bits_wanted) {
989 			/* mlog(0, "Found it all!\n"); */
990 			break;
991 		}
992 	}
993 
994 	/* XXX: I think the first clause is equivalent to the second
995 	 * 	- jlbec */
996 	if (found == bits_wanted) {
997 		*bit_off = start - found;
998 		*bits_found = found;
999 	} else if (best_size) {
1000 		*bit_off = best_offset;
1001 		*bits_found = best_size;
1002 	} else {
1003 		status = -ENOSPC;
1004 		/* No error log here -- see the comment above
1005 		 * ocfs2_test_bg_bit_allocatable */
1006 	}
1007 
1008 	return status;
1009 }
1010 
1011 static inline int ocfs2_block_group_set_bits(handle_t *handle,
1012 					     struct inode *alloc_inode,
1013 					     struct ocfs2_group_desc *bg,
1014 					     struct buffer_head *group_bh,
1015 					     unsigned int bit_off,
1016 					     unsigned int num_bits)
1017 {
1018 	int status;
1019 	void *bitmap = bg->bg_bitmap;
1020 	int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1021 
1022 	mlog_entry_void();
1023 
1024 	/* All callers get the descriptor via
1025 	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
1026 	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1027 	BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
1028 
1029 	mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
1030 	     num_bits);
1031 
1032 	if (ocfs2_is_cluster_bitmap(alloc_inode))
1033 		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1034 
1035 	status = ocfs2_journal_access_gd(handle,
1036 					 alloc_inode,
1037 					 group_bh,
1038 					 journal_type);
1039 	if (status < 0) {
1040 		mlog_errno(status);
1041 		goto bail;
1042 	}
1043 
1044 	le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
1045 
1046 	while(num_bits--)
1047 		ocfs2_set_bit(bit_off++, bitmap);
1048 
1049 	status = ocfs2_journal_dirty(handle,
1050 				     group_bh);
1051 	if (status < 0) {
1052 		mlog_errno(status);
1053 		goto bail;
1054 	}
1055 
1056 bail:
1057 	mlog_exit(status);
1058 	return status;
1059 }
1060 
1061 /* find the one with the most empty bits */
1062 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1063 {
1064 	u16 curr, best;
1065 
1066 	BUG_ON(!cl->cl_next_free_rec);
1067 
1068 	best = curr = 0;
1069 	while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1070 		if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1071 		    le32_to_cpu(cl->cl_recs[best].c_free))
1072 			best = curr;
1073 		curr++;
1074 	}
1075 
1076 	BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1077 	return best;
1078 }
1079 
1080 static int ocfs2_relink_block_group(handle_t *handle,
1081 				    struct inode *alloc_inode,
1082 				    struct buffer_head *fe_bh,
1083 				    struct buffer_head *bg_bh,
1084 				    struct buffer_head *prev_bg_bh,
1085 				    u16 chain)
1086 {
1087 	int status;
1088 	/* there is a really tiny chance the journal calls could fail,
1089 	 * but we wouldn't want inconsistent blocks in *any* case. */
1090 	u64 fe_ptr, bg_ptr, prev_bg_ptr;
1091 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1092 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1093 	struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1094 
1095 	/* The caller got these descriptors from
1096 	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
1097 	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1098 	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
1099 
1100 	mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
1101 	     (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1102 	     (unsigned long long)le64_to_cpu(bg->bg_blkno),
1103 	     (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
1104 
1105 	fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
1106 	bg_ptr = le64_to_cpu(bg->bg_next_group);
1107 	prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1108 
1109 	status = ocfs2_journal_access_gd(handle, alloc_inode, prev_bg_bh,
1110 					 OCFS2_JOURNAL_ACCESS_WRITE);
1111 	if (status < 0) {
1112 		mlog_errno(status);
1113 		goto out_rollback;
1114 	}
1115 
1116 	prev_bg->bg_next_group = bg->bg_next_group;
1117 
1118 	status = ocfs2_journal_dirty(handle, prev_bg_bh);
1119 	if (status < 0) {
1120 		mlog_errno(status);
1121 		goto out_rollback;
1122 	}
1123 
1124 	status = ocfs2_journal_access_gd(handle, alloc_inode, bg_bh,
1125 					 OCFS2_JOURNAL_ACCESS_WRITE);
1126 	if (status < 0) {
1127 		mlog_errno(status);
1128 		goto out_rollback;
1129 	}
1130 
1131 	bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
1132 
1133 	status = ocfs2_journal_dirty(handle, bg_bh);
1134 	if (status < 0) {
1135 		mlog_errno(status);
1136 		goto out_rollback;
1137 	}
1138 
1139 	status = ocfs2_journal_access_di(handle, alloc_inode, fe_bh,
1140 					 OCFS2_JOURNAL_ACCESS_WRITE);
1141 	if (status < 0) {
1142 		mlog_errno(status);
1143 		goto out_rollback;
1144 	}
1145 
1146 	fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
1147 
1148 	status = ocfs2_journal_dirty(handle, fe_bh);
1149 	if (status < 0) {
1150 		mlog_errno(status);
1151 		goto out_rollback;
1152 	}
1153 
1154 	status = 0;
1155 out_rollback:
1156 	if (status < 0) {
1157 		fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1158 		bg->bg_next_group = cpu_to_le64(bg_ptr);
1159 		prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1160 	}
1161 
1162 	mlog_exit(status);
1163 	return status;
1164 }
1165 
1166 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1167 						     u32 wanted)
1168 {
1169 	return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1170 }
1171 
1172 /* return 0 on success, -ENOSPC to keep searching and any other < 0
1173  * value on error. */
1174 static int ocfs2_cluster_group_search(struct inode *inode,
1175 				      struct buffer_head *group_bh,
1176 				      u32 bits_wanted, u32 min_bits,
1177 				      u64 max_block,
1178 				      u16 *bit_off, u16 *bits_found)
1179 {
1180 	int search = -ENOSPC;
1181 	int ret;
1182 	u64 blkoff;
1183 	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
1184 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1185 	u16 tmp_off, tmp_found;
1186 	unsigned int max_bits, gd_cluster_off;
1187 
1188 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1189 
1190 	if (gd->bg_free_bits_count) {
1191 		max_bits = le16_to_cpu(gd->bg_bits);
1192 
1193 		/* Tail groups in cluster bitmaps which aren't cpg
1194 		 * aligned are prone to partial extention by a failed
1195 		 * fs resize. If the file system resize never got to
1196 		 * update the dinode cluster count, then we don't want
1197 		 * to trust any clusters past it, regardless of what
1198 		 * the group descriptor says. */
1199 		gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1200 							  le64_to_cpu(gd->bg_blkno));
1201 		if ((gd_cluster_off + max_bits) >
1202 		    OCFS2_I(inode)->ip_clusters) {
1203 			max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1204 			mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1205 			     (unsigned long long)le64_to_cpu(gd->bg_blkno),
1206 			     le16_to_cpu(gd->bg_bits),
1207 			     OCFS2_I(inode)->ip_clusters, max_bits);
1208 		}
1209 
1210 		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1211 							group_bh, bits_wanted,
1212 							max_bits,
1213 							&tmp_off, &tmp_found);
1214 		if (ret)
1215 			return ret;
1216 
1217 		if (max_block) {
1218 			blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1219 							  gd_cluster_off +
1220 							  tmp_off + tmp_found);
1221 			mlog(0, "Checking %llu against %llu\n",
1222 			     (unsigned long long)blkoff,
1223 			     (unsigned long long)max_block);
1224 			if (blkoff > max_block)
1225 				return -ENOSPC;
1226 		}
1227 
1228 		/* ocfs2_block_group_find_clear_bits() might
1229 		 * return success, but we still want to return
1230 		 * -ENOSPC unless it found the minimum number
1231 		 * of bits. */
1232 		if (min_bits <= tmp_found) {
1233 			*bit_off = tmp_off;
1234 			*bits_found = tmp_found;
1235 			search = 0; /* success */
1236 		} else if (tmp_found) {
1237 			/*
1238 			 * Don't show bits which we'll be returning
1239 			 * for allocation to the local alloc bitmap.
1240 			 */
1241 			ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
1242 		}
1243 	}
1244 
1245 	return search;
1246 }
1247 
1248 static int ocfs2_block_group_search(struct inode *inode,
1249 				    struct buffer_head *group_bh,
1250 				    u32 bits_wanted, u32 min_bits,
1251 				    u64 max_block,
1252 				    u16 *bit_off, u16 *bits_found)
1253 {
1254 	int ret = -ENOSPC;
1255 	u64 blkoff;
1256 	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1257 
1258 	BUG_ON(min_bits != 1);
1259 	BUG_ON(ocfs2_is_cluster_bitmap(inode));
1260 
1261 	if (bg->bg_free_bits_count) {
1262 		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1263 							group_bh, bits_wanted,
1264 							le16_to_cpu(bg->bg_bits),
1265 							bit_off, bits_found);
1266 		if (!ret && max_block) {
1267 			blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
1268 				*bits_found;
1269 			mlog(0, "Checking %llu against %llu\n",
1270 			     (unsigned long long)blkoff,
1271 			     (unsigned long long)max_block);
1272 			if (blkoff > max_block)
1273 				ret = -ENOSPC;
1274 		}
1275 	}
1276 
1277 	return ret;
1278 }
1279 
1280 static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
1281 				       handle_t *handle,
1282 				       struct buffer_head *di_bh,
1283 				       u32 num_bits,
1284 				       u16 chain)
1285 {
1286 	int ret;
1287 	u32 tmp_used;
1288 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1289 	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1290 
1291 	ret = ocfs2_journal_access_di(handle, inode, di_bh,
1292 				      OCFS2_JOURNAL_ACCESS_WRITE);
1293 	if (ret < 0) {
1294 		mlog_errno(ret);
1295 		goto out;
1296 	}
1297 
1298 	tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1299 	di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1300 	le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1301 
1302 	ret = ocfs2_journal_dirty(handle, di_bh);
1303 	if (ret < 0)
1304 		mlog_errno(ret);
1305 
1306 out:
1307 	return ret;
1308 }
1309 
1310 static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1311 				  handle_t *handle,
1312 				  u32 bits_wanted,
1313 				  u32 min_bits,
1314 				  u16 *bit_off,
1315 				  unsigned int *num_bits,
1316 				  u64 gd_blkno,
1317 				  u16 *bits_left)
1318 {
1319 	int ret;
1320 	u16 found;
1321 	struct buffer_head *group_bh = NULL;
1322 	struct ocfs2_group_desc *gd;
1323 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1324 	struct inode *alloc_inode = ac->ac_inode;
1325 
1326 	ret = ocfs2_read_group_descriptor(alloc_inode, di, gd_blkno,
1327 					  &group_bh);
1328 	if (ret < 0) {
1329 		mlog_errno(ret);
1330 		return ret;
1331 	}
1332 
1333 	gd = (struct ocfs2_group_desc *) group_bh->b_data;
1334 	ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1335 				  ac->ac_max_block, bit_off, &found);
1336 	if (ret < 0) {
1337 		if (ret != -ENOSPC)
1338 			mlog_errno(ret);
1339 		goto out;
1340 	}
1341 
1342 	*num_bits = found;
1343 
1344 	ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1345 					       *num_bits,
1346 					       le16_to_cpu(gd->bg_chain));
1347 	if (ret < 0) {
1348 		mlog_errno(ret);
1349 		goto out;
1350 	}
1351 
1352 	ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1353 					 *bit_off, *num_bits);
1354 	if (ret < 0)
1355 		mlog_errno(ret);
1356 
1357 	*bits_left = le16_to_cpu(gd->bg_free_bits_count);
1358 
1359 out:
1360 	brelse(group_bh);
1361 
1362 	return ret;
1363 }
1364 
1365 static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1366 			      handle_t *handle,
1367 			      u32 bits_wanted,
1368 			      u32 min_bits,
1369 			      u16 *bit_off,
1370 			      unsigned int *num_bits,
1371 			      u64 *bg_blkno,
1372 			      u16 *bits_left)
1373 {
1374 	int status;
1375 	u16 chain, tmp_bits;
1376 	u32 tmp_used;
1377 	u64 next_group;
1378 	struct inode *alloc_inode = ac->ac_inode;
1379 	struct buffer_head *group_bh = NULL;
1380 	struct buffer_head *prev_group_bh = NULL;
1381 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1382 	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1383 	struct ocfs2_group_desc *bg;
1384 
1385 	chain = ac->ac_chain;
1386 	mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1387 	     bits_wanted, chain,
1388 	     (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
1389 
1390 	status = ocfs2_read_group_descriptor(alloc_inode, fe,
1391 					     le64_to_cpu(cl->cl_recs[chain].c_blkno),
1392 					     &group_bh);
1393 	if (status < 0) {
1394 		mlog_errno(status);
1395 		goto bail;
1396 	}
1397 	bg = (struct ocfs2_group_desc *) group_bh->b_data;
1398 
1399 	status = -ENOSPC;
1400 	/* for now, the chain search is a bit simplistic. We just use
1401 	 * the 1st group with any empty bits. */
1402 	while ((status = ac->ac_group_search(alloc_inode, group_bh,
1403 					     bits_wanted, min_bits,
1404 					     ac->ac_max_block, bit_off,
1405 					     &tmp_bits)) == -ENOSPC) {
1406 		if (!bg->bg_next_group)
1407 			break;
1408 
1409 		brelse(prev_group_bh);
1410 		prev_group_bh = NULL;
1411 
1412 		next_group = le64_to_cpu(bg->bg_next_group);
1413 		prev_group_bh = group_bh;
1414 		group_bh = NULL;
1415 		status = ocfs2_read_group_descriptor(alloc_inode, fe,
1416 						     next_group, &group_bh);
1417 		if (status < 0) {
1418 			mlog_errno(status);
1419 			goto bail;
1420 		}
1421 		bg = (struct ocfs2_group_desc *) group_bh->b_data;
1422 	}
1423 	if (status < 0) {
1424 		if (status != -ENOSPC)
1425 			mlog_errno(status);
1426 		goto bail;
1427 	}
1428 
1429 	mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1430 	     tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
1431 
1432 	*num_bits = tmp_bits;
1433 
1434 	BUG_ON(*num_bits == 0);
1435 
1436 	/*
1437 	 * Keep track of previous block descriptor read. When
1438 	 * we find a target, if we have read more than X
1439 	 * number of descriptors, and the target is reasonably
1440 	 * empty, relink him to top of his chain.
1441 	 *
1442 	 * We've read 0 extra blocks and only send one more to
1443 	 * the transaction, yet the next guy to search has a
1444 	 * much easier time.
1445 	 *
1446 	 * Do this *after* figuring out how many bits we're taking out
1447 	 * of our target group.
1448 	 */
1449 	if (ac->ac_allow_chain_relink &&
1450 	    (prev_group_bh) &&
1451 	    (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1452 		status = ocfs2_relink_block_group(handle, alloc_inode,
1453 						  ac->ac_bh, group_bh,
1454 						  prev_group_bh, chain);
1455 		if (status < 0) {
1456 			mlog_errno(status);
1457 			goto bail;
1458 		}
1459 	}
1460 
1461 	/* Ok, claim our bits now: set the info on dinode, chainlist
1462 	 * and then the group */
1463 	status = ocfs2_journal_access_di(handle,
1464 					 alloc_inode,
1465 					 ac->ac_bh,
1466 					 OCFS2_JOURNAL_ACCESS_WRITE);
1467 	if (status < 0) {
1468 		mlog_errno(status);
1469 		goto bail;
1470 	}
1471 
1472 	tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1473 	fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1474 	le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1475 
1476 	status = ocfs2_journal_dirty(handle,
1477 				     ac->ac_bh);
1478 	if (status < 0) {
1479 		mlog_errno(status);
1480 		goto bail;
1481 	}
1482 
1483 	status = ocfs2_block_group_set_bits(handle,
1484 					    alloc_inode,
1485 					    bg,
1486 					    group_bh,
1487 					    *bit_off,
1488 					    *num_bits);
1489 	if (status < 0) {
1490 		mlog_errno(status);
1491 		goto bail;
1492 	}
1493 
1494 	mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
1495 	     (unsigned long long)le64_to_cpu(fe->i_blkno));
1496 
1497 	*bg_blkno = le64_to_cpu(bg->bg_blkno);
1498 	*bits_left = le16_to_cpu(bg->bg_free_bits_count);
1499 bail:
1500 	brelse(group_bh);
1501 	brelse(prev_group_bh);
1502 
1503 	mlog_exit(status);
1504 	return status;
1505 }
1506 
1507 /* will give out up to bits_wanted contiguous bits. */
1508 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1509 				     struct ocfs2_alloc_context *ac,
1510 				     handle_t *handle,
1511 				     u32 bits_wanted,
1512 				     u32 min_bits,
1513 				     u16 *bit_off,
1514 				     unsigned int *num_bits,
1515 				     u64 *bg_blkno)
1516 {
1517 	int status;
1518 	u16 victim, i;
1519 	u16 bits_left = 0;
1520 	u64 hint_blkno = ac->ac_last_group;
1521 	struct ocfs2_chain_list *cl;
1522 	struct ocfs2_dinode *fe;
1523 
1524 	mlog_entry_void();
1525 
1526 	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1527 	BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1528 	BUG_ON(!ac->ac_bh);
1529 
1530 	fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1531 
1532 	/* The bh was validated by the inode read during
1533 	 * ocfs2_reserve_suballoc_bits().  Any corruption is a code bug. */
1534 	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1535 
1536 	if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1537 	    le32_to_cpu(fe->id1.bitmap1.i_total)) {
1538 		ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1539 			    "bits but only %u total.",
1540 			    (unsigned long long)le64_to_cpu(fe->i_blkno),
1541 			    le32_to_cpu(fe->id1.bitmap1.i_used),
1542 			    le32_to_cpu(fe->id1.bitmap1.i_total));
1543 		status = -EIO;
1544 		goto bail;
1545 	}
1546 
1547 	if (hint_blkno) {
1548 		/* Attempt to short-circuit the usual search mechanism
1549 		 * by jumping straight to the most recently used
1550 		 * allocation group. This helps us mantain some
1551 		 * contiguousness across allocations. */
1552 		status = ocfs2_search_one_group(ac, handle, bits_wanted,
1553 						min_bits, bit_off, num_bits,
1554 						hint_blkno, &bits_left);
1555 		if (!status) {
1556 			/* Be careful to update *bg_blkno here as the
1557 			 * caller is expecting it to be filled in, and
1558 			 * ocfs2_search_one_group() won't do that for
1559 			 * us. */
1560 			*bg_blkno = hint_blkno;
1561 			goto set_hint;
1562 		}
1563 		if (status < 0 && status != -ENOSPC) {
1564 			mlog_errno(status);
1565 			goto bail;
1566 		}
1567 	}
1568 
1569 	cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1570 
1571 	victim = ocfs2_find_victim_chain(cl);
1572 	ac->ac_chain = victim;
1573 	ac->ac_allow_chain_relink = 1;
1574 
1575 	status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
1576 				    num_bits, bg_blkno, &bits_left);
1577 	if (!status)
1578 		goto set_hint;
1579 	if (status < 0 && status != -ENOSPC) {
1580 		mlog_errno(status);
1581 		goto bail;
1582 	}
1583 
1584 	mlog(0, "Search of victim chain %u came up with nothing, "
1585 	     "trying all chains now.\n", victim);
1586 
1587 	/* If we didn't pick a good victim, then just default to
1588 	 * searching each chain in order. Don't allow chain relinking
1589 	 * because we only calculate enough journal credits for one
1590 	 * relink per alloc. */
1591 	ac->ac_allow_chain_relink = 0;
1592 	for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1593 		if (i == victim)
1594 			continue;
1595 		if (!cl->cl_recs[i].c_free)
1596 			continue;
1597 
1598 		ac->ac_chain = i;
1599 		status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1600 					    bit_off, num_bits, bg_blkno,
1601 					    &bits_left);
1602 		if (!status)
1603 			break;
1604 		if (status < 0 && status != -ENOSPC) {
1605 			mlog_errno(status);
1606 			goto bail;
1607 		}
1608 	}
1609 
1610 set_hint:
1611 	if (status != -ENOSPC) {
1612 		/* If the next search of this group is not likely to
1613 		 * yield a suitable extent, then we reset the last
1614 		 * group hint so as to not waste a disk read */
1615 		if (bits_left < min_bits)
1616 			ac->ac_last_group = 0;
1617 		else
1618 			ac->ac_last_group = *bg_blkno;
1619 	}
1620 
1621 bail:
1622 	mlog_exit(status);
1623 	return status;
1624 }
1625 
1626 int ocfs2_claim_metadata(struct ocfs2_super *osb,
1627 			 handle_t *handle,
1628 			 struct ocfs2_alloc_context *ac,
1629 			 u32 bits_wanted,
1630 			 u16 *suballoc_bit_start,
1631 			 unsigned int *num_bits,
1632 			 u64 *blkno_start)
1633 {
1634 	int status;
1635 	u64 bg_blkno;
1636 
1637 	BUG_ON(!ac);
1638 	BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1639 	BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
1640 
1641 	status = ocfs2_claim_suballoc_bits(osb,
1642 					   ac,
1643 					   handle,
1644 					   bits_wanted,
1645 					   1,
1646 					   suballoc_bit_start,
1647 					   num_bits,
1648 					   &bg_blkno);
1649 	if (status < 0) {
1650 		mlog_errno(status);
1651 		goto bail;
1652 	}
1653 	atomic_inc(&osb->alloc_stats.bg_allocs);
1654 
1655 	*blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1656 	ac->ac_bits_given += (*num_bits);
1657 	status = 0;
1658 bail:
1659 	mlog_exit(status);
1660 	return status;
1661 }
1662 
1663 static void ocfs2_init_inode_ac_group(struct inode *dir,
1664 				      struct buffer_head *parent_fe_bh,
1665 				      struct ocfs2_alloc_context *ac)
1666 {
1667 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1668 	/*
1669 	 * Try to allocate inodes from some specific group.
1670 	 *
1671 	 * If the parent dir has recorded the last group used in allocation,
1672 	 * cool, use it. Otherwise if we try to allocate new inode from the
1673 	 * same slot the parent dir belongs to, use the same chunk.
1674 	 *
1675 	 * We are very careful here to avoid the mistake of setting
1676 	 * ac_last_group to a group descriptor from a different (unlocked) slot.
1677 	 */
1678 	if (OCFS2_I(dir)->ip_last_used_group &&
1679 	    OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
1680 		ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
1681 	else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
1682 		ac->ac_last_group = ocfs2_which_suballoc_group(
1683 					le64_to_cpu(fe->i_blkno),
1684 					le16_to_cpu(fe->i_suballoc_bit));
1685 }
1686 
1687 static inline void ocfs2_save_inode_ac_group(struct inode *dir,
1688 					     struct ocfs2_alloc_context *ac)
1689 {
1690 	OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
1691 	OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
1692 }
1693 
1694 int ocfs2_claim_new_inode(struct ocfs2_super *osb,
1695 			  handle_t *handle,
1696 			  struct inode *dir,
1697 			  struct buffer_head *parent_fe_bh,
1698 			  struct ocfs2_alloc_context *ac,
1699 			  u16 *suballoc_bit,
1700 			  u64 *fe_blkno)
1701 {
1702 	int status;
1703 	unsigned int num_bits;
1704 	u64 bg_blkno;
1705 
1706 	mlog_entry_void();
1707 
1708 	BUG_ON(!ac);
1709 	BUG_ON(ac->ac_bits_given != 0);
1710 	BUG_ON(ac->ac_bits_wanted != 1);
1711 	BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
1712 
1713 	ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
1714 
1715 	status = ocfs2_claim_suballoc_bits(osb,
1716 					   ac,
1717 					   handle,
1718 					   1,
1719 					   1,
1720 					   suballoc_bit,
1721 					   &num_bits,
1722 					   &bg_blkno);
1723 	if (status < 0) {
1724 		mlog_errno(status);
1725 		goto bail;
1726 	}
1727 	atomic_inc(&osb->alloc_stats.bg_allocs);
1728 
1729 	BUG_ON(num_bits != 1);
1730 
1731 	*fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1732 	ac->ac_bits_given++;
1733 	ocfs2_save_inode_ac_group(dir, ac);
1734 	status = 0;
1735 bail:
1736 	mlog_exit(status);
1737 	return status;
1738 }
1739 
1740 /* translate a group desc. blkno and it's bitmap offset into
1741  * disk cluster offset. */
1742 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1743 						   u64 bg_blkno,
1744 						   u16 bg_bit_off)
1745 {
1746 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1747 	u32 cluster = 0;
1748 
1749 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1750 
1751 	if (bg_blkno != osb->first_cluster_group_blkno)
1752 		cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1753 	cluster += (u32) bg_bit_off;
1754 	return cluster;
1755 }
1756 
1757 /* given a cluster offset, calculate which block group it belongs to
1758  * and return that block offset. */
1759 u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
1760 {
1761 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1762 	u32 group_no;
1763 
1764 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1765 
1766 	group_no = cluster / osb->bitmap_cpg;
1767 	if (!group_no)
1768 		return osb->first_cluster_group_blkno;
1769 	return ocfs2_clusters_to_blocks(inode->i_sb,
1770 					group_no * osb->bitmap_cpg);
1771 }
1772 
1773 /* given the block number of a cluster start, calculate which cluster
1774  * group and descriptor bitmap offset that corresponds to. */
1775 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1776 						u64 data_blkno,
1777 						u64 *bg_blkno,
1778 						u16 *bg_bit_off)
1779 {
1780 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1781 	u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1782 
1783 	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1784 
1785 	*bg_blkno = ocfs2_which_cluster_group(inode,
1786 					      data_cluster);
1787 
1788 	if (*bg_blkno == osb->first_cluster_group_blkno)
1789 		*bg_bit_off = (u16) data_cluster;
1790 	else
1791 		*bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1792 							     data_blkno - *bg_blkno);
1793 }
1794 
1795 /*
1796  * min_bits - minimum contiguous chunk from this total allocation we
1797  * can handle. set to what we asked for originally for a full
1798  * contig. allocation, set to '1' to indicate we can deal with extents
1799  * of any size.
1800  */
1801 int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1802 			   handle_t *handle,
1803 			   struct ocfs2_alloc_context *ac,
1804 			   u32 min_clusters,
1805 			   u32 max_clusters,
1806 			   u32 *cluster_start,
1807 			   u32 *num_clusters)
1808 {
1809 	int status;
1810 	unsigned int bits_wanted = max_clusters;
1811 	u64 bg_blkno = 0;
1812 	u16 bg_bit_off;
1813 
1814 	mlog_entry_void();
1815 
1816 	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1817 
1818 	BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1819 	       && ac->ac_which != OCFS2_AC_USE_MAIN);
1820 
1821 	if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1822 		status = ocfs2_claim_local_alloc_bits(osb,
1823 						      handle,
1824 						      ac,
1825 						      bits_wanted,
1826 						      cluster_start,
1827 						      num_clusters);
1828 		if (!status)
1829 			atomic_inc(&osb->alloc_stats.local_data);
1830 	} else {
1831 		if (min_clusters > (osb->bitmap_cpg - 1)) {
1832 			/* The only paths asking for contiguousness
1833 			 * should know about this already. */
1834 			mlog(ML_ERROR, "minimum allocation requested %u exceeds "
1835 			     "group bitmap size %u!\n", min_clusters,
1836 			     osb->bitmap_cpg);
1837 			status = -ENOSPC;
1838 			goto bail;
1839 		}
1840 		/* clamp the current request down to a realistic size. */
1841 		if (bits_wanted > (osb->bitmap_cpg - 1))
1842 			bits_wanted = osb->bitmap_cpg - 1;
1843 
1844 		status = ocfs2_claim_suballoc_bits(osb,
1845 						   ac,
1846 						   handle,
1847 						   bits_wanted,
1848 						   min_clusters,
1849 						   &bg_bit_off,
1850 						   num_clusters,
1851 						   &bg_blkno);
1852 		if (!status) {
1853 			*cluster_start =
1854 				ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1855 								 bg_blkno,
1856 								 bg_bit_off);
1857 			atomic_inc(&osb->alloc_stats.bitmap_data);
1858 		}
1859 	}
1860 	if (status < 0) {
1861 		if (status != -ENOSPC)
1862 			mlog_errno(status);
1863 		goto bail;
1864 	}
1865 
1866 	ac->ac_bits_given += *num_clusters;
1867 
1868 bail:
1869 	mlog_exit(status);
1870 	return status;
1871 }
1872 
1873 int ocfs2_claim_clusters(struct ocfs2_super *osb,
1874 			 handle_t *handle,
1875 			 struct ocfs2_alloc_context *ac,
1876 			 u32 min_clusters,
1877 			 u32 *cluster_start,
1878 			 u32 *num_clusters)
1879 {
1880 	unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1881 
1882 	return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1883 				      bits_wanted, cluster_start, num_clusters);
1884 }
1885 
1886 static inline int ocfs2_block_group_clear_bits(handle_t *handle,
1887 					       struct inode *alloc_inode,
1888 					       struct ocfs2_group_desc *bg,
1889 					       struct buffer_head *group_bh,
1890 					       unsigned int bit_off,
1891 					       unsigned int num_bits)
1892 {
1893 	int status;
1894 	unsigned int tmp;
1895 	int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1896 	struct ocfs2_group_desc *undo_bg = NULL;
1897 	int cluster_bitmap = 0;
1898 
1899 	mlog_entry_void();
1900 
1901 	/* The caller got this descriptor from
1902 	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
1903 	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1904 
1905 	mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1906 
1907 	if (ocfs2_is_cluster_bitmap(alloc_inode))
1908 		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1909 
1910 	status = ocfs2_journal_access_gd(handle, alloc_inode, group_bh,
1911 					 journal_type);
1912 	if (status < 0) {
1913 		mlog_errno(status);
1914 		goto bail;
1915 	}
1916 
1917 	if (ocfs2_is_cluster_bitmap(alloc_inode))
1918 		cluster_bitmap = 1;
1919 
1920 	if (cluster_bitmap) {
1921 		jbd_lock_bh_state(group_bh);
1922 		undo_bg = (struct ocfs2_group_desc *)
1923 					bh2jh(group_bh)->b_committed_data;
1924 		BUG_ON(!undo_bg);
1925 	}
1926 
1927 	tmp = num_bits;
1928 	while(tmp--) {
1929 		ocfs2_clear_bit((bit_off + tmp),
1930 				(unsigned long *) bg->bg_bitmap);
1931 		if (cluster_bitmap)
1932 			ocfs2_set_bit(bit_off + tmp,
1933 				      (unsigned long *) undo_bg->bg_bitmap);
1934 	}
1935 	le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1936 
1937 	if (cluster_bitmap)
1938 		jbd_unlock_bh_state(group_bh);
1939 
1940 	status = ocfs2_journal_dirty(handle, group_bh);
1941 	if (status < 0)
1942 		mlog_errno(status);
1943 bail:
1944 	return status;
1945 }
1946 
1947 /*
1948  * expects the suballoc inode to already be locked.
1949  */
1950 int ocfs2_free_suballoc_bits(handle_t *handle,
1951 			     struct inode *alloc_inode,
1952 			     struct buffer_head *alloc_bh,
1953 			     unsigned int start_bit,
1954 			     u64 bg_blkno,
1955 			     unsigned int count)
1956 {
1957 	int status = 0;
1958 	u32 tmp_used;
1959 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1960 	struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1961 	struct buffer_head *group_bh = NULL;
1962 	struct ocfs2_group_desc *group;
1963 
1964 	mlog_entry_void();
1965 
1966 	/* The alloc_bh comes from ocfs2_free_dinode() or
1967 	 * ocfs2_free_clusters().  The callers have all locked the
1968 	 * allocator and gotten alloc_bh from the lock call.  This
1969 	 * validates the dinode buffer.  Any corruption that has happended
1970 	 * is a code bug. */
1971 	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1972 	BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1973 
1974 	mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1975 	     (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1976 	     (unsigned long long)bg_blkno, start_bit);
1977 
1978 	status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
1979 					     &group_bh);
1980 	if (status < 0) {
1981 		mlog_errno(status);
1982 		goto bail;
1983 	}
1984 	group = (struct ocfs2_group_desc *) group_bh->b_data;
1985 
1986 	BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1987 
1988 	status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1989 					      group, group_bh,
1990 					      start_bit, count);
1991 	if (status < 0) {
1992 		mlog_errno(status);
1993 		goto bail;
1994 	}
1995 
1996 	status = ocfs2_journal_access_di(handle, alloc_inode, alloc_bh,
1997 					 OCFS2_JOURNAL_ACCESS_WRITE);
1998 	if (status < 0) {
1999 		mlog_errno(status);
2000 		goto bail;
2001 	}
2002 
2003 	le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2004 		     count);
2005 	tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2006 	fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
2007 
2008 	status = ocfs2_journal_dirty(handle, alloc_bh);
2009 	if (status < 0) {
2010 		mlog_errno(status);
2011 		goto bail;
2012 	}
2013 
2014 bail:
2015 	brelse(group_bh);
2016 
2017 	mlog_exit(status);
2018 	return status;
2019 }
2020 
2021 int ocfs2_free_dinode(handle_t *handle,
2022 		      struct inode *inode_alloc_inode,
2023 		      struct buffer_head *inode_alloc_bh,
2024 		      struct ocfs2_dinode *di)
2025 {
2026 	u64 blk = le64_to_cpu(di->i_blkno);
2027 	u16 bit = le16_to_cpu(di->i_suballoc_bit);
2028 	u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2029 
2030 	return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2031 					inode_alloc_bh, bit, bg_blkno, 1);
2032 }
2033 
2034 int ocfs2_free_clusters(handle_t *handle,
2035 		       struct inode *bitmap_inode,
2036 		       struct buffer_head *bitmap_bh,
2037 		       u64 start_blk,
2038 		       unsigned int num_clusters)
2039 {
2040 	int status;
2041 	u16 bg_start_bit;
2042 	u64 bg_blkno;
2043 	struct ocfs2_dinode *fe;
2044 
2045 	/* You can't ever have a contiguous set of clusters
2046 	 * bigger than a block group bitmap so we never have to worry
2047 	 * about looping on them. */
2048 
2049 	mlog_entry_void();
2050 
2051 	/* This is expensive. We can safely remove once this stuff has
2052 	 * gotten tested really well. */
2053 	BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2054 
2055 	fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2056 
2057 	ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2058 				     &bg_start_bit);
2059 
2060 	mlog(0, "want to free %u clusters starting at block %llu\n",
2061 	     num_clusters, (unsigned long long)start_blk);
2062 	mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2063 	     (unsigned long long)bg_blkno, bg_start_bit);
2064 
2065 	status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2066 					  bg_start_bit, bg_blkno,
2067 					  num_clusters);
2068 	if (status < 0) {
2069 		mlog_errno(status);
2070 		goto out;
2071 	}
2072 
2073 	ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2074 					 num_clusters);
2075 
2076 out:
2077 	mlog_exit(status);
2078 	return status;
2079 }
2080 
2081 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2082 {
2083 	printk("Block Group:\n");
2084 	printk("bg_signature:       %s\n", bg->bg_signature);
2085 	printk("bg_size:            %u\n", bg->bg_size);
2086 	printk("bg_bits:            %u\n", bg->bg_bits);
2087 	printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2088 	printk("bg_chain:           %u\n", bg->bg_chain);
2089 	printk("bg_generation:      %u\n", le32_to_cpu(bg->bg_generation));
2090 	printk("bg_next_group:      %llu\n",
2091 	       (unsigned long long)bg->bg_next_group);
2092 	printk("bg_parent_dinode:   %llu\n",
2093 	       (unsigned long long)bg->bg_parent_dinode);
2094 	printk("bg_blkno:           %llu\n",
2095 	       (unsigned long long)bg->bg_blkno);
2096 }
2097 
2098 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2099 {
2100 	int i;
2101 
2102 	printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
2103 	printk("i_signature:                  %s\n", fe->i_signature);
2104 	printk("i_size:                       %llu\n",
2105 	       (unsigned long long)fe->i_size);
2106 	printk("i_clusters:                   %u\n", fe->i_clusters);
2107 	printk("i_generation:                 %u\n",
2108 	       le32_to_cpu(fe->i_generation));
2109 	printk("id1.bitmap1.i_used:           %u\n",
2110 	       le32_to_cpu(fe->id1.bitmap1.i_used));
2111 	printk("id1.bitmap1.i_total:          %u\n",
2112 	       le32_to_cpu(fe->id1.bitmap1.i_total));
2113 	printk("id2.i_chain.cl_cpg:           %u\n", fe->id2.i_chain.cl_cpg);
2114 	printk("id2.i_chain.cl_bpc:           %u\n", fe->id2.i_chain.cl_bpc);
2115 	printk("id2.i_chain.cl_count:         %u\n", fe->id2.i_chain.cl_count);
2116 	printk("id2.i_chain.cl_next_free_rec: %u\n",
2117 	       fe->id2.i_chain.cl_next_free_rec);
2118 	for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2119 		printk("fe->id2.i_chain.cl_recs[%d].c_free:  %u\n", i,
2120 		       fe->id2.i_chain.cl_recs[i].c_free);
2121 		printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2122 		       fe->id2.i_chain.cl_recs[i].c_total);
2123 		printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2124 		       (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
2125 	}
2126 }
2127 
2128 /*
2129  * For a given allocation, determine which allocators will need to be
2130  * accessed, and lock them, reserving the appropriate number of bits.
2131  *
2132  * Sparse file systems call this from ocfs2_write_begin_nolock()
2133  * and ocfs2_allocate_unwritten_extents().
2134  *
2135  * File systems which don't support holes call this from
2136  * ocfs2_extend_allocation().
2137  */
2138 int ocfs2_lock_allocators(struct inode *inode,
2139 			  struct ocfs2_extent_tree *et,
2140 			  u32 clusters_to_add, u32 extents_to_split,
2141 			  struct ocfs2_alloc_context **data_ac,
2142 			  struct ocfs2_alloc_context **meta_ac)
2143 {
2144 	int ret = 0, num_free_extents;
2145 	unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2146 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2147 
2148 	*meta_ac = NULL;
2149 	if (data_ac)
2150 		*data_ac = NULL;
2151 
2152 	BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2153 
2154 	num_free_extents = ocfs2_num_free_extents(osb, inode, et);
2155 	if (num_free_extents < 0) {
2156 		ret = num_free_extents;
2157 		mlog_errno(ret);
2158 		goto out;
2159 	}
2160 
2161 	/*
2162 	 * Sparse allocation file systems need to be more conservative
2163 	 * with reserving room for expansion - the actual allocation
2164 	 * happens while we've got a journal handle open so re-taking
2165 	 * a cluster lock (because we ran out of room for another
2166 	 * extent) will violate ordering rules.
2167 	 *
2168 	 * Most of the time we'll only be seeing this 1 cluster at a time
2169 	 * anyway.
2170 	 *
2171 	 * Always lock for any unwritten extents - we might want to
2172 	 * add blocks during a split.
2173 	 */
2174 	if (!num_free_extents ||
2175 	    (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
2176 		ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
2177 		if (ret < 0) {
2178 			if (ret != -ENOSPC)
2179 				mlog_errno(ret);
2180 			goto out;
2181 		}
2182 	}
2183 
2184 	if (clusters_to_add == 0)
2185 		goto out;
2186 
2187 	ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2188 	if (ret < 0) {
2189 		if (ret != -ENOSPC)
2190 			mlog_errno(ret);
2191 		goto out;
2192 	}
2193 
2194 out:
2195 	if (ret) {
2196 		if (*meta_ac) {
2197 			ocfs2_free_alloc_context(*meta_ac);
2198 			*meta_ac = NULL;
2199 		}
2200 
2201 		/*
2202 		 * We cannot have an error and a non null *data_ac.
2203 		 */
2204 	}
2205 
2206 	return ret;
2207 }
2208 
2209 /*
2210  * Read the inode specified by blkno to get suballoc_slot and
2211  * suballoc_bit.
2212  */
2213 static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2214 				       u16 *suballoc_slot, u16 *suballoc_bit)
2215 {
2216 	int status;
2217 	struct buffer_head *inode_bh = NULL;
2218 	struct ocfs2_dinode *inode_fe;
2219 
2220 	mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
2221 
2222 	/* dirty read disk */
2223 	status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2224 	if (status < 0) {
2225 		mlog(ML_ERROR, "read block %llu failed %d\n",
2226 		     (unsigned long long)blkno, status);
2227 		goto bail;
2228 	}
2229 
2230 	inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2231 	if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
2232 		mlog(ML_ERROR, "invalid inode %llu requested\n",
2233 		     (unsigned long long)blkno);
2234 		status = -EINVAL;
2235 		goto bail;
2236 	}
2237 
2238 	if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
2239 	    (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2240 		mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
2241 		     (unsigned long long)blkno,
2242 		     (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
2243 		status = -EINVAL;
2244 		goto bail;
2245 	}
2246 
2247 	if (suballoc_slot)
2248 		*suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2249 	if (suballoc_bit)
2250 		*suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2251 
2252 bail:
2253 	brelse(inode_bh);
2254 
2255 	mlog_exit(status);
2256 	return status;
2257 }
2258 
2259 /*
2260  * test whether bit is SET in allocator bitmap or not.  on success, 0
2261  * is returned and *res is 1 for SET; 0 otherwise.  when fails, errno
2262  * is returned and *res is meaningless.  Call this after you have
2263  * cluster locked against suballoc, or you may get a result based on
2264  * non-up2date contents
2265  */
2266 static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2267 				   struct inode *suballoc,
2268 				   struct buffer_head *alloc_bh, u64 blkno,
2269 				   u16 bit, int *res)
2270 {
2271 	struct ocfs2_dinode *alloc_fe;
2272 	struct ocfs2_group_desc *group;
2273 	struct buffer_head *group_bh = NULL;
2274 	u64 bg_blkno;
2275 	int status;
2276 
2277 	mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2278 		   (unsigned int)bit);
2279 
2280 	alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
2281 	if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
2282 		mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2283 		     (unsigned int)bit,
2284 		     ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
2285 		status = -EINVAL;
2286 		goto bail;
2287 	}
2288 
2289 	bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
2290 	status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
2291 					     &group_bh);
2292 	if (status < 0) {
2293 		mlog(ML_ERROR, "read group %llu failed %d\n",
2294 		     (unsigned long long)bg_blkno, status);
2295 		goto bail;
2296 	}
2297 
2298 	group = (struct ocfs2_group_desc *) group_bh->b_data;
2299 	*res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2300 
2301 bail:
2302 	brelse(group_bh);
2303 
2304 	mlog_exit(status);
2305 	return status;
2306 }
2307 
2308 /*
2309  * Test if the bit representing this inode (blkno) is set in the
2310  * suballocator.
2311  *
2312  * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2313  *
2314  * In the event of failure, a negative value is returned and *res is
2315  * meaningless.
2316  *
2317  * Callers must make sure to hold nfs_sync_lock to prevent
2318  * ocfs2_delete_inode() on another node from accessing the same
2319  * suballocator concurrently.
2320  */
2321 int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2322 {
2323 	int status;
2324 	u16 suballoc_bit = 0, suballoc_slot = 0;
2325 	struct inode *inode_alloc_inode;
2326 	struct buffer_head *alloc_bh = NULL;
2327 
2328 	mlog_entry("blkno: %llu", (unsigned long long)blkno);
2329 
2330 	status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2331 					     &suballoc_bit);
2332 	if (status < 0) {
2333 		mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2334 		goto bail;
2335 	}
2336 
2337 	inode_alloc_inode =
2338 		ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2339 					    suballoc_slot);
2340 	if (!inode_alloc_inode) {
2341 		/* the error code could be inaccurate, but we are not able to
2342 		 * get the correct one. */
2343 		status = -EINVAL;
2344 		mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2345 		     (u32)suballoc_slot);
2346 		goto bail;
2347 	}
2348 
2349 	mutex_lock(&inode_alloc_inode->i_mutex);
2350 	status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2351 	if (status < 0) {
2352 		mutex_unlock(&inode_alloc_inode->i_mutex);
2353 		mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2354 		     (u32)suballoc_slot, status);
2355 		goto bail;
2356 	}
2357 
2358 	status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2359 					 blkno, suballoc_bit, res);
2360 	if (status < 0)
2361 		mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2362 
2363 	ocfs2_inode_unlock(inode_alloc_inode, 0);
2364 	mutex_unlock(&inode_alloc_inode->i_mutex);
2365 
2366 	iput(inode_alloc_inode);
2367 	brelse(alloc_bh);
2368 bail:
2369 	mlog_exit(status);
2370 	return status;
2371 }
2372