1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * metadata alloc and free
7 * Inspired by ext3 block groups.
9 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
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.
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.
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.
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
32 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33 #include <cluster/masklog.h>
41 #include "localalloc.h"
47 #include "buffer_head_io.h"
49 #define NOT_ALLOC_NEW_GROUP 0
50 #define ALLOC_NEW_GROUP 1
52 #define OCFS2_MAX_INODES_TO_STEAL 1024
54 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
55 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
56 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
57 static int ocfs2_block_group_fill(handle_t *handle,
58 struct inode *alloc_inode,
59 struct buffer_head *bg_bh,
62 struct ocfs2_chain_list *cl);
63 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
64 struct inode *alloc_inode,
65 struct buffer_head *bh);
67 static int ocfs2_cluster_group_search(struct inode *inode,
68 struct buffer_head *group_bh,
69 u32 bits_wanted, u32 min_bits,
70 u16 *bit_off, u16 *bits_found);
71 static int ocfs2_block_group_search(struct inode *inode,
72 struct buffer_head *group_bh,
73 u32 bits_wanted, u32 min_bits,
74 u16 *bit_off, u16 *bits_found);
75 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
76 struct ocfs2_alloc_context *ac,
81 unsigned int *num_bits,
83 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
85 static inline int ocfs2_block_group_set_bits(handle_t *handle,
86 struct inode *alloc_inode,
87 struct ocfs2_group_desc *bg,
88 struct buffer_head *group_bh,
90 unsigned int num_bits);
91 static inline int ocfs2_block_group_clear_bits(handle_t *handle,
92 struct inode *alloc_inode,
93 struct ocfs2_group_desc *bg,
94 struct buffer_head *group_bh,
96 unsigned int num_bits);
98 static int ocfs2_relink_block_group(handle_t *handle,
99 struct inode *alloc_inode,
100 struct buffer_head *fe_bh,
101 struct buffer_head *bg_bh,
102 struct buffer_head *prev_bg_bh,
104 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
106 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
109 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
114 static void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
116 struct inode *inode = ac->ac_inode;
119 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
120 ocfs2_inode_unlock(inode, 1);
122 mutex_unlock(&inode->i_mutex);
133 void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
135 ocfs2_free_ac_resource(ac);
139 static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
141 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
144 /* somewhat more expensive than our other checks, so use sparingly. */
145 int ocfs2_check_group_descriptor(struct super_block *sb,
146 struct ocfs2_dinode *di,
147 struct ocfs2_group_desc *gd)
149 unsigned int max_bits;
151 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
152 OCFS2_RO_ON_INVALID_GROUP_DESC(sb, gd);
156 if (di->i_blkno != gd->bg_parent_dinode) {
157 ocfs2_error(sb, "Group descriptor # %llu has bad parent "
158 "pointer (%llu, expected %llu)",
159 (unsigned long long)le64_to_cpu(gd->bg_blkno),
160 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
161 (unsigned long long)le64_to_cpu(di->i_blkno));
165 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
166 if (le16_to_cpu(gd->bg_bits) > max_bits) {
167 ocfs2_error(sb, "Group descriptor # %llu has bit count of %u",
168 (unsigned long long)le64_to_cpu(gd->bg_blkno),
169 le16_to_cpu(gd->bg_bits));
173 if (le16_to_cpu(gd->bg_chain) >=
174 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
175 ocfs2_error(sb, "Group descriptor # %llu has bad chain %u",
176 (unsigned long long)le64_to_cpu(gd->bg_blkno),
177 le16_to_cpu(gd->bg_chain));
181 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
182 ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
183 "claims that %u are free",
184 (unsigned long long)le64_to_cpu(gd->bg_blkno),
185 le16_to_cpu(gd->bg_bits),
186 le16_to_cpu(gd->bg_free_bits_count));
190 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
191 ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
192 "max bitmap bits of %u",
193 (unsigned long long)le64_to_cpu(gd->bg_blkno),
194 le16_to_cpu(gd->bg_bits),
195 8 * le16_to_cpu(gd->bg_size));
202 static int ocfs2_block_group_fill(handle_t *handle,
203 struct inode *alloc_inode,
204 struct buffer_head *bg_bh,
207 struct ocfs2_chain_list *cl)
210 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
211 struct super_block * sb = alloc_inode->i_sb;
215 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
216 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
218 (unsigned long long)group_blkno,
219 (unsigned long long) bg_bh->b_blocknr);
224 status = ocfs2_journal_access(handle,
227 OCFS2_JOURNAL_ACCESS_CREATE);
233 memset(bg, 0, sb->s_blocksize);
234 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
235 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
236 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
237 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
238 bg->bg_chain = cpu_to_le16(my_chain);
239 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
240 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
241 bg->bg_blkno = cpu_to_le64(group_blkno);
242 /* set the 1st bit in the bitmap to account for the descriptor block */
243 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
244 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
246 status = ocfs2_journal_dirty(handle, bg_bh);
250 /* There is no need to zero out or otherwise initialize the
251 * other blocks in a group - All valid FS metadata in a block
252 * group stores the superblock fs_generation value at
253 * allocation time. */
260 static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
265 while (curr < le16_to_cpu(cl->cl_count)) {
266 if (le32_to_cpu(cl->cl_recs[best].c_total) >
267 le32_to_cpu(cl->cl_recs[curr].c_total))
275 * We expect the block group allocator to already be locked.
277 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
278 struct inode *alloc_inode,
279 struct buffer_head *bh)
282 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
283 struct ocfs2_chain_list *cl;
284 struct ocfs2_alloc_context *ac = NULL;
285 handle_t *handle = NULL;
286 u32 bit_off, num_bits;
289 struct buffer_head *bg_bh = NULL;
290 struct ocfs2_group_desc *bg;
292 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
296 cl = &fe->id2.i_chain;
297 status = ocfs2_reserve_clusters(osb,
298 le16_to_cpu(cl->cl_cpg),
301 if (status != -ENOSPC)
306 credits = ocfs2_calc_group_alloc_credits(osb->sb,
307 le16_to_cpu(cl->cl_cpg));
308 handle = ocfs2_start_trans(osb, credits);
309 if (IS_ERR(handle)) {
310 status = PTR_ERR(handle);
316 status = ocfs2_claim_clusters(osb,
319 le16_to_cpu(cl->cl_cpg),
323 if (status != -ENOSPC)
328 alloc_rec = ocfs2_find_smallest_chain(cl);
330 /* setup the group */
331 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
332 mlog(0, "new descriptor, record %u, at block %llu\n",
333 alloc_rec, (unsigned long long)bg_blkno);
335 bg_bh = sb_getblk(osb->sb, bg_blkno);
341 ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
343 status = ocfs2_block_group_fill(handle,
354 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
356 status = ocfs2_journal_access(handle, alloc_inode,
357 bh, OCFS2_JOURNAL_ACCESS_WRITE);
363 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
364 le16_to_cpu(bg->bg_free_bits_count));
365 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
366 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno);
367 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
368 le16_add_cpu(&cl->cl_next_free_rec, 1);
370 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
371 le16_to_cpu(bg->bg_free_bits_count));
372 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
373 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
375 status = ocfs2_journal_dirty(handle, bh);
381 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
382 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
383 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
384 le32_to_cpu(fe->i_clusters)));
385 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
386 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
387 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
392 ocfs2_commit_trans(osb, handle);
395 ocfs2_free_alloc_context(ac);
404 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
405 struct ocfs2_alloc_context *ac,
411 u32 bits_wanted = ac->ac_bits_wanted;
412 struct inode *alloc_inode;
413 struct buffer_head *bh = NULL;
414 struct ocfs2_dinode *fe;
419 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
425 mutex_lock(&alloc_inode->i_mutex);
427 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
429 mutex_unlock(&alloc_inode->i_mutex);
436 ac->ac_inode = alloc_inode;
437 ac->ac_alloc_slot = slot;
439 fe = (struct ocfs2_dinode *) bh->b_data;
440 if (!OCFS2_IS_VALID_DINODE(fe)) {
441 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
445 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
446 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
447 (unsigned long long)le64_to_cpu(fe->i_blkno));
452 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
453 le32_to_cpu(fe->id1.bitmap1.i_used);
455 if (bits_wanted > free_bits) {
456 /* cluster bitmap never grows */
457 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
458 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
459 bits_wanted, free_bits);
464 if (alloc_new_group != ALLOC_NEW_GROUP) {
465 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
466 "and we don't alloc a new group for it.\n",
467 slot, bits_wanted, free_bits);
472 status = ocfs2_block_group_alloc(osb, alloc_inode, bh);
474 if (status != -ENOSPC)
478 atomic_inc(&osb->alloc_stats.bg_extends);
480 /* You should never ask for this much metadata */
482 (le32_to_cpu(fe->id1.bitmap1.i_total)
483 - le32_to_cpu(fe->id1.bitmap1.i_used)));
496 int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
497 struct ocfs2_dinode *fe,
498 struct ocfs2_alloc_context **ac)
503 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
510 (*ac)->ac_bits_wanted = ocfs2_extend_meta_needed(fe);
511 (*ac)->ac_which = OCFS2_AC_USE_META;
512 slot = osb->slot_num;
513 (*ac)->ac_group_search = ocfs2_block_group_search;
515 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
516 EXTENT_ALLOC_SYSTEM_INODE,
517 slot, ALLOC_NEW_GROUP);
519 if (status != -ENOSPC)
526 if ((status < 0) && *ac) {
527 ocfs2_free_alloc_context(*ac);
535 static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
536 struct ocfs2_alloc_context *ac)
538 int i, status = -ENOSPC;
539 s16 slot = ocfs2_get_inode_steal_slot(osb);
541 /* Start to steal inodes from the first slot after ours. */
542 if (slot == OCFS2_INVALID_SLOT)
543 slot = osb->slot_num + 1;
545 for (i = 0; i < osb->max_slots; i++, slot++) {
546 if (slot == osb->max_slots)
549 if (slot == osb->slot_num)
552 status = ocfs2_reserve_suballoc_bits(osb, ac,
553 INODE_ALLOC_SYSTEM_INODE,
554 slot, NOT_ALLOC_NEW_GROUP);
556 ocfs2_set_inode_steal_slot(osb, slot);
560 ocfs2_free_ac_resource(ac);
566 int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
567 struct ocfs2_alloc_context **ac)
570 s16 slot = ocfs2_get_inode_steal_slot(osb);
572 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
579 (*ac)->ac_bits_wanted = 1;
580 (*ac)->ac_which = OCFS2_AC_USE_INODE;
582 (*ac)->ac_group_search = ocfs2_block_group_search;
585 * slot is set when we successfully steal inode from other nodes.
586 * It is reset in 3 places:
587 * 1. when we flush the truncate log
588 * 2. when we complete local alloc recovery.
589 * 3. when we successfully allocate from our own slot.
590 * After it is set, we will go on stealing inodes until we find the
591 * need to check our slots to see whether there is some space for us.
593 if (slot != OCFS2_INVALID_SLOT &&
594 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_INODES_TO_STEAL)
597 atomic_set(&osb->s_num_inodes_stolen, 0);
598 status = ocfs2_reserve_suballoc_bits(osb, *ac,
599 INODE_ALLOC_SYSTEM_INODE,
600 osb->slot_num, ALLOC_NEW_GROUP);
605 * Some inodes must be freed by us, so try to allocate
606 * from our own next time.
608 if (slot != OCFS2_INVALID_SLOT)
609 ocfs2_init_inode_steal_slot(osb);
611 } else if (status < 0 && status != -ENOSPC) {
616 ocfs2_free_ac_resource(*ac);
619 status = ocfs2_steal_inode_from_other_nodes(osb, *ac);
620 atomic_inc(&osb->s_num_inodes_stolen);
622 if (status != -ENOSPC)
629 if ((status < 0) && *ac) {
630 ocfs2_free_alloc_context(*ac);
638 /* local alloc code has to do the same thing, so rather than do this
640 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
641 struct ocfs2_alloc_context *ac)
645 ac->ac_which = OCFS2_AC_USE_MAIN;
646 ac->ac_group_search = ocfs2_cluster_group_search;
648 status = ocfs2_reserve_suballoc_bits(osb, ac,
649 GLOBAL_BITMAP_SYSTEM_INODE,
652 if (status < 0 && status != -ENOSPC) {
661 /* Callers don't need to care which bitmap (local alloc or main) to
662 * use so we figure it out for them, but unfortunately this clutters
664 int ocfs2_reserve_clusters(struct ocfs2_super *osb,
666 struct ocfs2_alloc_context **ac)
672 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
679 (*ac)->ac_bits_wanted = bits_wanted;
682 if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
683 status = ocfs2_reserve_local_alloc_bits(osb,
686 if ((status < 0) && (status != -ENOSPC)) {
689 } else if (status == -ENOSPC) {
690 /* reserve_local_bits will return enospc with
691 * the local alloc inode still locked, so we
692 * can change this safely here. */
693 mlog(0, "Disabling local alloc\n");
694 /* We set to OCFS2_LA_DISABLED so that umount
695 * can clean up what's left of the local
697 osb->local_alloc_state = OCFS2_LA_DISABLED;
701 if (status == -ENOSPC) {
702 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
704 if (status != -ENOSPC)
712 if ((status < 0) && *ac) {
713 ocfs2_free_alloc_context(*ac);
722 * More or less lifted from ext3. I'll leave their description below:
724 * "For ext3 allocations, we must not reuse any blocks which are
725 * allocated in the bitmap buffer's "last committed data" copy. This
726 * prevents deletes from freeing up the page for reuse until we have
727 * committed the delete transaction.
729 * If we didn't do this, then deleting something and reallocating it as
730 * data would allow the old block to be overwritten before the
731 * transaction committed (because we force data to disk before commit).
732 * This would lead to corruption if we crashed between overwriting the
733 * data and committing the delete.
735 * @@@ We may want to make this allocation behaviour conditional on
736 * data-writes at some point, and disable it for metadata allocations or
739 * Note: OCFS2 already does this differently for metadata vs data
740 * allocations, as those bitmaps are separate and undo access is never
741 * called on a metadata group descriptor.
743 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
746 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
748 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
750 if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
753 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
754 return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
757 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
758 struct buffer_head *bg_bh,
759 unsigned int bits_wanted,
760 unsigned int total_bits,
765 u16 best_offset, best_size;
766 int offset, start, found, status = 0;
767 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
769 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
770 OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg);
774 found = start = best_offset = best_size = 0;
775 bitmap = bg->bg_bitmap;
777 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
778 if (offset == total_bits)
781 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
782 /* We found a zero, but we can't use it as it
783 * hasn't been put to disk yet! */
786 } else if (offset == start) {
787 /* we found a zero */
789 /* move start to the next bit to test */
792 /* got a zero after some ones */
796 if (found > best_size) {
798 best_offset = start - found;
800 /* we got everything we needed */
801 if (found == bits_wanted) {
802 /* mlog(0, "Found it all!\n"); */
807 /* XXX: I think the first clause is equivalent to the second
809 if (found == bits_wanted) {
810 *bit_off = start - found;
812 } else if (best_size) {
813 *bit_off = best_offset;
814 *bits_found = best_size;
817 /* No error log here -- see the comment above
818 * ocfs2_test_bg_bit_allocatable */
824 static inline int ocfs2_block_group_set_bits(handle_t *handle,
825 struct inode *alloc_inode,
826 struct ocfs2_group_desc *bg,
827 struct buffer_head *group_bh,
828 unsigned int bit_off,
829 unsigned int num_bits)
832 void *bitmap = bg->bg_bitmap;
833 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
837 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
838 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
842 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
844 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
847 if (ocfs2_is_cluster_bitmap(alloc_inode))
848 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
850 status = ocfs2_journal_access(handle,
859 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
862 ocfs2_set_bit(bit_off++, bitmap);
864 status = ocfs2_journal_dirty(handle,
876 /* find the one with the most empty bits */
877 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
881 BUG_ON(!cl->cl_next_free_rec);
884 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
885 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
886 le32_to_cpu(cl->cl_recs[best].c_free))
891 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
895 static int ocfs2_relink_block_group(handle_t *handle,
896 struct inode *alloc_inode,
897 struct buffer_head *fe_bh,
898 struct buffer_head *bg_bh,
899 struct buffer_head *prev_bg_bh,
903 /* there is a really tiny chance the journal calls could fail,
904 * but we wouldn't want inconsistent blocks in *any* case. */
905 u64 fe_ptr, bg_ptr, prev_bg_ptr;
906 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
907 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
908 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
910 if (!OCFS2_IS_VALID_DINODE(fe)) {
911 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
915 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
916 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
920 if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) {
921 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg);
926 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
927 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
928 (unsigned long long)le64_to_cpu(bg->bg_blkno),
929 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
931 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
932 bg_ptr = le64_to_cpu(bg->bg_next_group);
933 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
935 status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh,
936 OCFS2_JOURNAL_ACCESS_WRITE);
942 prev_bg->bg_next_group = bg->bg_next_group;
944 status = ocfs2_journal_dirty(handle, prev_bg_bh);
950 status = ocfs2_journal_access(handle, alloc_inode, bg_bh,
951 OCFS2_JOURNAL_ACCESS_WRITE);
957 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
959 status = ocfs2_journal_dirty(handle, bg_bh);
965 status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
966 OCFS2_JOURNAL_ACCESS_WRITE);
972 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
974 status = ocfs2_journal_dirty(handle, fe_bh);
983 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
984 bg->bg_next_group = cpu_to_le64(bg_ptr);
985 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
992 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
995 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
998 /* return 0 on success, -ENOSPC to keep searching and any other < 0
1000 static int ocfs2_cluster_group_search(struct inode *inode,
1001 struct buffer_head *group_bh,
1002 u32 bits_wanted, u32 min_bits,
1003 u16 *bit_off, u16 *bits_found)
1005 int search = -ENOSPC;
1007 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
1008 u16 tmp_off, tmp_found;
1009 unsigned int max_bits, gd_cluster_off;
1011 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1013 if (gd->bg_free_bits_count) {
1014 max_bits = le16_to_cpu(gd->bg_bits);
1016 /* Tail groups in cluster bitmaps which aren't cpg
1017 * aligned are prone to partial extention by a failed
1018 * fs resize. If the file system resize never got to
1019 * update the dinode cluster count, then we don't want
1020 * to trust any clusters past it, regardless of what
1021 * the group descriptor says. */
1022 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1023 le64_to_cpu(gd->bg_blkno));
1024 if ((gd_cluster_off + max_bits) >
1025 OCFS2_I(inode)->ip_clusters) {
1026 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1027 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1028 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1029 le16_to_cpu(gd->bg_bits),
1030 OCFS2_I(inode)->ip_clusters, max_bits);
1033 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1034 group_bh, bits_wanted,
1036 &tmp_off, &tmp_found);
1040 /* ocfs2_block_group_find_clear_bits() might
1041 * return success, but we still want to return
1042 * -ENOSPC unless it found the minimum number
1044 if (min_bits <= tmp_found) {
1046 *bits_found = tmp_found;
1047 search = 0; /* success */
1054 static int ocfs2_block_group_search(struct inode *inode,
1055 struct buffer_head *group_bh,
1056 u32 bits_wanted, u32 min_bits,
1057 u16 *bit_off, u16 *bits_found)
1060 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1062 BUG_ON(min_bits != 1);
1063 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1065 if (bg->bg_free_bits_count)
1066 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1067 group_bh, bits_wanted,
1068 le16_to_cpu(bg->bg_bits),
1069 bit_off, bits_found);
1074 static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
1076 struct buffer_head *di_bh,
1082 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1083 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1085 ret = ocfs2_journal_access(handle, inode, di_bh,
1086 OCFS2_JOURNAL_ACCESS_WRITE);
1092 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1093 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1094 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1096 ret = ocfs2_journal_dirty(handle, di_bh);
1104 static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1109 unsigned int *num_bits,
1115 struct buffer_head *group_bh = NULL;
1116 struct ocfs2_group_desc *gd;
1117 struct inode *alloc_inode = ac->ac_inode;
1119 ret = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), gd_blkno,
1120 &group_bh, OCFS2_BH_CACHED, alloc_inode);
1126 gd = (struct ocfs2_group_desc *) group_bh->b_data;
1127 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1128 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, gd);
1133 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1143 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1145 le16_to_cpu(gd->bg_chain));
1151 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1152 *bit_off, *num_bits);
1156 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1164 static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1169 unsigned int *num_bits,
1174 u16 chain, tmp_bits;
1177 struct inode *alloc_inode = ac->ac_inode;
1178 struct buffer_head *group_bh = NULL;
1179 struct buffer_head *prev_group_bh = NULL;
1180 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1181 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1182 struct ocfs2_group_desc *bg;
1184 chain = ac->ac_chain;
1185 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1187 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
1189 status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1190 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1191 &group_bh, OCFS2_BH_CACHED, alloc_inode);
1196 bg = (struct ocfs2_group_desc *) group_bh->b_data;
1197 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
1204 /* for now, the chain search is a bit simplistic. We just use
1205 * the 1st group with any empty bits. */
1206 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1207 bits_wanted, min_bits, bit_off,
1208 &tmp_bits)) == -ENOSPC) {
1209 if (!bg->bg_next_group)
1212 if (prev_group_bh) {
1213 brelse(prev_group_bh);
1214 prev_group_bh = NULL;
1216 next_group = le64_to_cpu(bg->bg_next_group);
1217 prev_group_bh = group_bh;
1219 status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1220 next_group, &group_bh,
1221 OCFS2_BH_CACHED, alloc_inode);
1226 bg = (struct ocfs2_group_desc *) group_bh->b_data;
1227 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
1234 if (status != -ENOSPC)
1239 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1240 tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
1242 *num_bits = tmp_bits;
1244 BUG_ON(*num_bits == 0);
1247 * Keep track of previous block descriptor read. When
1248 * we find a target, if we have read more than X
1249 * number of descriptors, and the target is reasonably
1250 * empty, relink him to top of his chain.
1252 * We've read 0 extra blocks and only send one more to
1253 * the transaction, yet the next guy to search has a
1256 * Do this *after* figuring out how many bits we're taking out
1257 * of our target group.
1259 if (ac->ac_allow_chain_relink &&
1261 (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1262 status = ocfs2_relink_block_group(handle, alloc_inode,
1263 ac->ac_bh, group_bh,
1264 prev_group_bh, chain);
1271 /* Ok, claim our bits now: set the info on dinode, chainlist
1272 * and then the group */
1273 status = ocfs2_journal_access(handle,
1276 OCFS2_JOURNAL_ACCESS_WRITE);
1282 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1283 fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1284 le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1286 status = ocfs2_journal_dirty(handle,
1293 status = ocfs2_block_group_set_bits(handle,
1304 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
1305 (unsigned long long)le64_to_cpu(fe->i_blkno));
1307 *bg_blkno = le64_to_cpu(bg->bg_blkno);
1308 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
1313 brelse(prev_group_bh);
1319 /* will give out up to bits_wanted contiguous bits. */
1320 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1321 struct ocfs2_alloc_context *ac,
1326 unsigned int *num_bits,
1332 u64 hint_blkno = ac->ac_last_group;
1333 struct ocfs2_chain_list *cl;
1334 struct ocfs2_dinode *fe;
1338 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1339 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1342 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1343 if (!OCFS2_IS_VALID_DINODE(fe)) {
1344 OCFS2_RO_ON_INVALID_DINODE(osb->sb, fe);
1348 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1349 le32_to_cpu(fe->id1.bitmap1.i_total)) {
1350 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1351 "bits but only %u total.",
1352 (unsigned long long)le64_to_cpu(fe->i_blkno),
1353 le32_to_cpu(fe->id1.bitmap1.i_used),
1354 le32_to_cpu(fe->id1.bitmap1.i_total));
1360 /* Attempt to short-circuit the usual search mechanism
1361 * by jumping straight to the most recently used
1362 * allocation group. This helps us mantain some
1363 * contiguousness across allocations. */
1364 status = ocfs2_search_one_group(ac, handle, bits_wanted,
1365 min_bits, bit_off, num_bits,
1366 hint_blkno, &bits_left);
1368 /* Be careful to update *bg_blkno here as the
1369 * caller is expecting it to be filled in, and
1370 * ocfs2_search_one_group() won't do that for
1372 *bg_blkno = hint_blkno;
1375 if (status < 0 && status != -ENOSPC) {
1381 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1383 victim = ocfs2_find_victim_chain(cl);
1384 ac->ac_chain = victim;
1385 ac->ac_allow_chain_relink = 1;
1387 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
1388 num_bits, bg_blkno, &bits_left);
1391 if (status < 0 && status != -ENOSPC) {
1396 mlog(0, "Search of victim chain %u came up with nothing, "
1397 "trying all chains now.\n", victim);
1399 /* If we didn't pick a good victim, then just default to
1400 * searching each chain in order. Don't allow chain relinking
1401 * because we only calculate enough journal credits for one
1402 * relink per alloc. */
1403 ac->ac_allow_chain_relink = 0;
1404 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1407 if (!cl->cl_recs[i].c_free)
1411 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1412 bit_off, num_bits, bg_blkno,
1416 if (status < 0 && status != -ENOSPC) {
1423 if (status != -ENOSPC) {
1424 /* If the next search of this group is not likely to
1425 * yield a suitable extent, then we reset the last
1426 * group hint so as to not waste a disk read */
1427 if (bits_left < min_bits)
1428 ac->ac_last_group = 0;
1430 ac->ac_last_group = *bg_blkno;
1438 int ocfs2_claim_metadata(struct ocfs2_super *osb,
1440 struct ocfs2_alloc_context *ac,
1442 u16 *suballoc_bit_start,
1443 unsigned int *num_bits,
1450 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1451 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
1453 status = ocfs2_claim_suballoc_bits(osb,
1465 atomic_inc(&osb->alloc_stats.bg_allocs);
1467 *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1468 ac->ac_bits_given += (*num_bits);
1475 int ocfs2_claim_new_inode(struct ocfs2_super *osb,
1477 struct ocfs2_alloc_context *ac,
1482 unsigned int num_bits;
1488 BUG_ON(ac->ac_bits_given != 0);
1489 BUG_ON(ac->ac_bits_wanted != 1);
1490 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
1492 status = ocfs2_claim_suballoc_bits(osb,
1504 atomic_inc(&osb->alloc_stats.bg_allocs);
1506 BUG_ON(num_bits != 1);
1508 *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1509 ac->ac_bits_given++;
1516 /* translate a group desc. blkno and it's bitmap offset into
1517 * disk cluster offset. */
1518 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1522 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1525 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1527 if (bg_blkno != osb->first_cluster_group_blkno)
1528 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1529 cluster += (u32) bg_bit_off;
1533 /* given a cluster offset, calculate which block group it belongs to
1534 * and return that block offset. */
1535 u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
1537 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1540 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1542 group_no = cluster / osb->bitmap_cpg;
1544 return osb->first_cluster_group_blkno;
1545 return ocfs2_clusters_to_blocks(inode->i_sb,
1546 group_no * osb->bitmap_cpg);
1549 /* given the block number of a cluster start, calculate which cluster
1550 * group and descriptor bitmap offset that corresponds to. */
1551 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1556 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1557 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1559 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1561 *bg_blkno = ocfs2_which_cluster_group(inode,
1564 if (*bg_blkno == osb->first_cluster_group_blkno)
1565 *bg_bit_off = (u16) data_cluster;
1567 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1568 data_blkno - *bg_blkno);
1572 * min_bits - minimum contiguous chunk from this total allocation we
1573 * can handle. set to what we asked for originally for a full
1574 * contig. allocation, set to '1' to indicate we can deal with extents
1577 int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1579 struct ocfs2_alloc_context *ac,
1586 unsigned int bits_wanted = max_clusters;
1592 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1594 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1595 && ac->ac_which != OCFS2_AC_USE_MAIN);
1597 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1598 status = ocfs2_claim_local_alloc_bits(osb,
1605 atomic_inc(&osb->alloc_stats.local_data);
1607 if (min_clusters > (osb->bitmap_cpg - 1)) {
1608 /* The only paths asking for contiguousness
1609 * should know about this already. */
1610 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
1611 "group bitmap size %u!\n", min_clusters,
1616 /* clamp the current request down to a realistic size. */
1617 if (bits_wanted > (osb->bitmap_cpg - 1))
1618 bits_wanted = osb->bitmap_cpg - 1;
1620 status = ocfs2_claim_suballoc_bits(osb,
1630 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1633 atomic_inc(&osb->alloc_stats.bitmap_data);
1637 if (status != -ENOSPC)
1642 ac->ac_bits_given += *num_clusters;
1649 int ocfs2_claim_clusters(struct ocfs2_super *osb,
1651 struct ocfs2_alloc_context *ac,
1656 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1658 return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1659 bits_wanted, cluster_start, num_clusters);
1662 static inline int ocfs2_block_group_clear_bits(handle_t *handle,
1663 struct inode *alloc_inode,
1664 struct ocfs2_group_desc *bg,
1665 struct buffer_head *group_bh,
1666 unsigned int bit_off,
1667 unsigned int num_bits)
1671 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1672 struct ocfs2_group_desc *undo_bg = NULL;
1676 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
1677 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
1682 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1684 if (ocfs2_is_cluster_bitmap(alloc_inode))
1685 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1687 status = ocfs2_journal_access(handle, alloc_inode, group_bh,
1694 if (ocfs2_is_cluster_bitmap(alloc_inode))
1695 undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
1699 ocfs2_clear_bit((bit_off + tmp),
1700 (unsigned long *) bg->bg_bitmap);
1701 if (ocfs2_is_cluster_bitmap(alloc_inode))
1702 ocfs2_set_bit(bit_off + tmp,
1703 (unsigned long *) undo_bg->bg_bitmap);
1705 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1707 status = ocfs2_journal_dirty(handle, group_bh);
1715 * expects the suballoc inode to already be locked.
1717 int ocfs2_free_suballoc_bits(handle_t *handle,
1718 struct inode *alloc_inode,
1719 struct buffer_head *alloc_bh,
1720 unsigned int start_bit,
1726 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
1727 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1728 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1729 struct buffer_head *group_bh = NULL;
1730 struct ocfs2_group_desc *group;
1734 if (!OCFS2_IS_VALID_DINODE(fe)) {
1735 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
1739 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1741 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1742 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1743 (unsigned long long)bg_blkno, start_bit);
1745 status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED,
1752 group = (struct ocfs2_group_desc *) group_bh->b_data;
1753 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, group);
1758 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1760 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1768 status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
1769 OCFS2_JOURNAL_ACCESS_WRITE);
1775 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
1777 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1778 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
1780 status = ocfs2_journal_dirty(handle, alloc_bh);
1794 int ocfs2_free_dinode(handle_t *handle,
1795 struct inode *inode_alloc_inode,
1796 struct buffer_head *inode_alloc_bh,
1797 struct ocfs2_dinode *di)
1799 u64 blk = le64_to_cpu(di->i_blkno);
1800 u16 bit = le16_to_cpu(di->i_suballoc_bit);
1801 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1803 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
1804 inode_alloc_bh, bit, bg_blkno, 1);
1807 int ocfs2_free_clusters(handle_t *handle,
1808 struct inode *bitmap_inode,
1809 struct buffer_head *bitmap_bh,
1811 unsigned int num_clusters)
1816 struct ocfs2_dinode *fe;
1818 /* You can't ever have a contiguous set of clusters
1819 * bigger than a block group bitmap so we never have to worry
1820 * about looping on them. */
1824 /* This is expensive. We can safely remove once this stuff has
1825 * gotten tested really well. */
1826 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
1828 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
1830 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
1833 mlog(0, "want to free %u clusters starting at block %llu\n",
1834 num_clusters, (unsigned long long)start_blk);
1835 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1836 (unsigned long long)bg_blkno, bg_start_bit);
1838 status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
1839 bg_start_bit, bg_blkno,
1848 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
1850 printk("Block Group:\n");
1851 printk("bg_signature: %s\n", bg->bg_signature);
1852 printk("bg_size: %u\n", bg->bg_size);
1853 printk("bg_bits: %u\n", bg->bg_bits);
1854 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
1855 printk("bg_chain: %u\n", bg->bg_chain);
1856 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
1857 printk("bg_next_group: %llu\n",
1858 (unsigned long long)bg->bg_next_group);
1859 printk("bg_parent_dinode: %llu\n",
1860 (unsigned long long)bg->bg_parent_dinode);
1861 printk("bg_blkno: %llu\n",
1862 (unsigned long long)bg->bg_blkno);
1865 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
1869 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
1870 printk("i_signature: %s\n", fe->i_signature);
1871 printk("i_size: %llu\n",
1872 (unsigned long long)fe->i_size);
1873 printk("i_clusters: %u\n", fe->i_clusters);
1874 printk("i_generation: %u\n",
1875 le32_to_cpu(fe->i_generation));
1876 printk("id1.bitmap1.i_used: %u\n",
1877 le32_to_cpu(fe->id1.bitmap1.i_used));
1878 printk("id1.bitmap1.i_total: %u\n",
1879 le32_to_cpu(fe->id1.bitmap1.i_total));
1880 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
1881 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
1882 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
1883 printk("id2.i_chain.cl_next_free_rec: %u\n",
1884 fe->id2.i_chain.cl_next_free_rec);
1885 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
1886 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
1887 fe->id2.i_chain.cl_recs[i].c_free);
1888 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
1889 fe->id2.i_chain.cl_recs[i].c_total);
1890 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
1891 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);