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 static inline void ocfs2_debug_bg(struct ocfs2_group_desc
*bg
);
50 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode
*fe
);
51 static inline u16
ocfs2_find_victim_chain(struct ocfs2_chain_list
*cl
);
52 static int ocfs2_block_group_fill(struct ocfs2_journal_handle
*handle
,
53 struct inode
*alloc_inode
,
54 struct buffer_head
*bg_bh
,
57 struct ocfs2_chain_list
*cl
);
58 static int ocfs2_block_group_alloc(struct ocfs2_super
*osb
,
59 struct inode
*alloc_inode
,
60 struct buffer_head
*bh
);
62 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super
*osb
,
63 struct ocfs2_alloc_context
*ac
);
65 static int ocfs2_cluster_group_search(struct inode
*inode
,
66 struct buffer_head
*group_bh
,
67 u32 bits_wanted
, u32 min_bits
,
68 u16
*bit_off
, u16
*bits_found
);
69 static int ocfs2_block_group_search(struct inode
*inode
,
70 struct buffer_head
*group_bh
,
71 u32 bits_wanted
, u32 min_bits
,
72 u16
*bit_off
, u16
*bits_found
);
73 static int ocfs2_search_chain(struct ocfs2_alloc_context
*ac
,
77 unsigned int *num_bits
,
79 static int ocfs2_claim_suballoc_bits(struct ocfs2_super
*osb
,
80 struct ocfs2_alloc_context
*ac
,
84 unsigned int *num_bits
,
86 static int ocfs2_test_bg_bit_allocatable(struct buffer_head
*bg_bh
,
88 static inline int ocfs2_block_group_set_bits(struct ocfs2_journal_handle
*handle
,
89 struct inode
*alloc_inode
,
90 struct ocfs2_group_desc
*bg
,
91 struct buffer_head
*group_bh
,
93 unsigned int num_bits
);
94 static inline int ocfs2_block_group_clear_bits(struct ocfs2_journal_handle
*handle
,
95 struct inode
*alloc_inode
,
96 struct ocfs2_group_desc
*bg
,
97 struct buffer_head
*group_bh
,
99 unsigned int num_bits
);
101 static int ocfs2_relink_block_group(struct ocfs2_journal_handle
*handle
,
102 struct inode
*alloc_inode
,
103 struct buffer_head
*fe_bh
,
104 struct buffer_head
*bg_bh
,
105 struct buffer_head
*prev_bg_bh
,
107 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc
*bg
,
109 static int ocfs2_free_suballoc_bits(struct ocfs2_journal_handle
*handle
,
110 struct inode
*alloc_inode
,
111 struct buffer_head
*alloc_bh
,
112 unsigned int start_bit
,
115 static inline u64
ocfs2_which_suballoc_group(u64 block
,
117 static inline u32
ocfs2_desc_bitmap_to_cluster_off(struct inode
*inode
,
120 static inline u64
ocfs2_which_cluster_group(struct inode
*inode
,
122 static inline void ocfs2_block_to_cluster_group(struct inode
*inode
,
127 void ocfs2_free_alloc_context(struct ocfs2_alloc_context
*ac
)
136 static u32
ocfs2_bits_per_group(struct ocfs2_chain_list
*cl
)
138 return (u32
)le16_to_cpu(cl
->cl_cpg
) * (u32
)le16_to_cpu(cl
->cl_bpc
);
141 /* somewhat more expensive than our other checks, so use sparingly. */
142 static int ocfs2_check_group_descriptor(struct super_block
*sb
,
143 struct ocfs2_dinode
*di
,
144 struct ocfs2_group_desc
*gd
)
146 unsigned int max_bits
;
148 if (!OCFS2_IS_VALID_GROUP_DESC(gd
)) {
149 OCFS2_RO_ON_INVALID_GROUP_DESC(sb
, gd
);
153 if (di
->i_blkno
!= gd
->bg_parent_dinode
) {
154 ocfs2_error(sb
, "Group descriptor # %llu has bad parent "
155 "pointer (%llu, expected %llu)",
156 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
157 (unsigned long long)le64_to_cpu(gd
->bg_parent_dinode
),
158 (unsigned long long)le64_to_cpu(di
->i_blkno
));
162 max_bits
= le16_to_cpu(di
->id2
.i_chain
.cl_cpg
) * le16_to_cpu(di
->id2
.i_chain
.cl_bpc
);
163 if (le16_to_cpu(gd
->bg_bits
) > max_bits
) {
164 ocfs2_error(sb
, "Group descriptor # %llu has bit count of %u",
165 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
166 le16_to_cpu(gd
->bg_bits
));
170 if (le16_to_cpu(gd
->bg_chain
) >=
171 le16_to_cpu(di
->id2
.i_chain
.cl_next_free_rec
)) {
172 ocfs2_error(sb
, "Group descriptor # %llu has bad chain %u",
173 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
174 le16_to_cpu(gd
->bg_chain
));
178 if (le16_to_cpu(gd
->bg_free_bits_count
) > le16_to_cpu(gd
->bg_bits
)) {
179 ocfs2_error(sb
, "Group descriptor # %llu has bit count %u but "
180 "claims that %u are free",
181 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
182 le16_to_cpu(gd
->bg_bits
),
183 le16_to_cpu(gd
->bg_free_bits_count
));
187 if (le16_to_cpu(gd
->bg_bits
) > (8 * le16_to_cpu(gd
->bg_size
))) {
188 ocfs2_error(sb
, "Group descriptor # %llu has bit count %u but "
189 "max bitmap bits of %u",
190 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
191 le16_to_cpu(gd
->bg_bits
),
192 8 * le16_to_cpu(gd
->bg_size
));
199 static int ocfs2_block_group_fill(struct ocfs2_journal_handle
*handle
,
200 struct inode
*alloc_inode
,
201 struct buffer_head
*bg_bh
,
204 struct ocfs2_chain_list
*cl
)
207 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
208 struct super_block
* sb
= alloc_inode
->i_sb
;
212 if (((unsigned long long) bg_bh
->b_blocknr
) != group_blkno
) {
213 ocfs2_error(alloc_inode
->i_sb
, "group block (%llu) != "
215 (unsigned long long)group_blkno
,
216 (unsigned long long) bg_bh
->b_blocknr
);
221 status
= ocfs2_journal_access(handle
,
224 OCFS2_JOURNAL_ACCESS_CREATE
);
230 memset(bg
, 0, sb
->s_blocksize
);
231 strcpy(bg
->bg_signature
, OCFS2_GROUP_DESC_SIGNATURE
);
232 bg
->bg_generation
= cpu_to_le32(OCFS2_SB(sb
)->fs_generation
);
233 bg
->bg_size
= cpu_to_le16(ocfs2_group_bitmap_size(sb
));
234 bg
->bg_bits
= cpu_to_le16(ocfs2_bits_per_group(cl
));
235 bg
->bg_chain
= cpu_to_le16(my_chain
);
236 bg
->bg_next_group
= cl
->cl_recs
[my_chain
].c_blkno
;
237 bg
->bg_parent_dinode
= cpu_to_le64(OCFS2_I(alloc_inode
)->ip_blkno
);
238 bg
->bg_blkno
= cpu_to_le64(group_blkno
);
239 /* set the 1st bit in the bitmap to account for the descriptor block */
240 ocfs2_set_bit(0, (unsigned long *)bg
->bg_bitmap
);
241 bg
->bg_free_bits_count
= cpu_to_le16(le16_to_cpu(bg
->bg_bits
) - 1);
243 status
= ocfs2_journal_dirty(handle
, bg_bh
);
247 /* There is no need to zero out or otherwise initialize the
248 * other blocks in a group - All valid FS metadata in a block
249 * group stores the superblock fs_generation value at
250 * allocation time. */
257 static inline u16
ocfs2_find_smallest_chain(struct ocfs2_chain_list
*cl
)
262 while (curr
< le16_to_cpu(cl
->cl_count
)) {
263 if (le32_to_cpu(cl
->cl_recs
[best
].c_total
) >
264 le32_to_cpu(cl
->cl_recs
[curr
].c_total
))
272 * We expect the block group allocator to already be locked.
274 static int ocfs2_block_group_alloc(struct ocfs2_super
*osb
,
275 struct inode
*alloc_inode
,
276 struct buffer_head
*bh
)
279 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) bh
->b_data
;
280 struct ocfs2_chain_list
*cl
;
281 struct ocfs2_alloc_context
*ac
= NULL
;
282 struct ocfs2_journal_handle
*handle
= NULL
;
283 u32 bit_off
, num_bits
;
286 struct buffer_head
*bg_bh
= NULL
;
287 struct ocfs2_group_desc
*bg
;
289 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode
));
293 handle
= ocfs2_alloc_handle(osb
);
300 cl
= &fe
->id2
.i_chain
;
301 status
= ocfs2_reserve_clusters(osb
,
303 le16_to_cpu(cl
->cl_cpg
),
306 if (status
!= -ENOSPC
)
311 credits
= ocfs2_calc_group_alloc_credits(osb
->sb
,
312 le16_to_cpu(cl
->cl_cpg
));
313 handle
= ocfs2_start_trans(osb
, handle
, credits
);
314 if (IS_ERR(handle
)) {
315 status
= PTR_ERR(handle
);
321 status
= ocfs2_claim_clusters(osb
,
324 le16_to_cpu(cl
->cl_cpg
),
328 if (status
!= -ENOSPC
)
333 alloc_rec
= ocfs2_find_smallest_chain(cl
);
335 /* setup the group */
336 bg_blkno
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
337 mlog(0, "new descriptor, record %u, at block %llu\n",
338 alloc_rec
, (unsigned long long)bg_blkno
);
340 bg_bh
= sb_getblk(osb
->sb
, bg_blkno
);
346 ocfs2_set_new_buffer_uptodate(alloc_inode
, bg_bh
);
348 status
= ocfs2_block_group_fill(handle
,
359 bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
361 status
= ocfs2_journal_access(handle
, alloc_inode
,
362 bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
368 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_free
,
369 le16_to_cpu(bg
->bg_free_bits_count
));
370 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_total
, le16_to_cpu(bg
->bg_bits
));
371 cl
->cl_recs
[alloc_rec
].c_blkno
= cpu_to_le64(bg_blkno
);
372 if (le16_to_cpu(cl
->cl_next_free_rec
) < le16_to_cpu(cl
->cl_count
))
373 le16_add_cpu(&cl
->cl_next_free_rec
, 1);
375 le32_add_cpu(&fe
->id1
.bitmap1
.i_used
, le16_to_cpu(bg
->bg_bits
) -
376 le16_to_cpu(bg
->bg_free_bits_count
));
377 le32_add_cpu(&fe
->id1
.bitmap1
.i_total
, le16_to_cpu(bg
->bg_bits
));
378 le32_add_cpu(&fe
->i_clusters
, le16_to_cpu(cl
->cl_cpg
));
380 status
= ocfs2_journal_dirty(handle
, bh
);
386 spin_lock(&OCFS2_I(alloc_inode
)->ip_lock
);
387 OCFS2_I(alloc_inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
);
388 fe
->i_size
= cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode
->i_sb
,
389 le32_to_cpu(fe
->i_clusters
)));
390 spin_unlock(&OCFS2_I(alloc_inode
)->ip_lock
);
391 i_size_write(alloc_inode
, le64_to_cpu(fe
->i_size
));
392 alloc_inode
->i_blocks
=
393 ocfs2_align_bytes_to_sectors(i_size_read(alloc_inode
));
398 ocfs2_commit_trans(handle
);
401 ocfs2_free_alloc_context(ac
);
410 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super
*osb
,
411 struct ocfs2_alloc_context
*ac
)
414 u32 bits_wanted
= ac
->ac_bits_wanted
;
415 struct inode
*alloc_inode
= ac
->ac_inode
;
416 struct buffer_head
*bh
= NULL
;
417 struct ocfs2_journal_handle
*handle
= ac
->ac_handle
;
418 struct ocfs2_dinode
*fe
;
423 BUG_ON(handle
->flags
& OCFS2_HANDLE_STARTED
);
425 ocfs2_handle_add_inode(handle
, alloc_inode
);
426 status
= ocfs2_meta_lock(alloc_inode
, handle
, &bh
, 1);
432 fe
= (struct ocfs2_dinode
*) bh
->b_data
;
433 if (!OCFS2_IS_VALID_DINODE(fe
)) {
434 OCFS2_RO_ON_INVALID_DINODE(alloc_inode
->i_sb
, fe
);
438 if (!(fe
->i_flags
& cpu_to_le32(OCFS2_CHAIN_FL
))) {
439 ocfs2_error(alloc_inode
->i_sb
, "Invalid chain allocator %llu",
440 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
445 free_bits
= le32_to_cpu(fe
->id1
.bitmap1
.i_total
) -
446 le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
448 if (bits_wanted
> free_bits
) {
449 /* cluster bitmap never grows */
450 if (ocfs2_is_cluster_bitmap(alloc_inode
)) {
451 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
452 bits_wanted
, free_bits
);
457 status
= ocfs2_block_group_alloc(osb
, alloc_inode
, bh
);
459 if (status
!= -ENOSPC
)
463 atomic_inc(&osb
->alloc_stats
.bg_extends
);
465 /* You should never ask for this much metadata */
467 (le32_to_cpu(fe
->id1
.bitmap1
.i_total
)
468 - le32_to_cpu(fe
->id1
.bitmap1
.i_used
)));
481 int ocfs2_reserve_new_metadata(struct ocfs2_super
*osb
,
482 struct ocfs2_journal_handle
*handle
,
483 struct ocfs2_dinode
*fe
,
484 struct ocfs2_alloc_context
**ac
)
487 struct inode
*alloc_inode
= NULL
;
489 *ac
= kcalloc(1, sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
496 (*ac
)->ac_bits_wanted
= ocfs2_extend_meta_needed(fe
);
497 (*ac
)->ac_handle
= handle
;
498 (*ac
)->ac_which
= OCFS2_AC_USE_META
;
500 #ifndef OCFS2_USE_ALL_METADATA_SUBALLOCATORS
501 alloc_inode
= ocfs2_get_system_file_inode(osb
,
502 EXTENT_ALLOC_SYSTEM_INODE
,
505 alloc_inode
= ocfs2_get_system_file_inode(osb
,
506 EXTENT_ALLOC_SYSTEM_INODE
,
515 (*ac
)->ac_inode
= igrab(alloc_inode
);
516 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
518 status
= ocfs2_reserve_suballoc_bits(osb
, (*ac
));
520 if (status
!= -ENOSPC
)
527 if ((status
< 0) && *ac
) {
528 ocfs2_free_alloc_context(*ac
);
539 int ocfs2_reserve_new_inode(struct ocfs2_super
*osb
,
540 struct ocfs2_journal_handle
*handle
,
541 struct ocfs2_alloc_context
**ac
)
544 struct inode
*alloc_inode
= NULL
;
546 *ac
= kcalloc(1, sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
553 (*ac
)->ac_bits_wanted
= 1;
554 (*ac
)->ac_handle
= handle
;
555 (*ac
)->ac_which
= OCFS2_AC_USE_INODE
;
557 alloc_inode
= ocfs2_get_system_file_inode(osb
,
558 INODE_ALLOC_SYSTEM_INODE
,
566 (*ac
)->ac_inode
= igrab(alloc_inode
);
567 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
569 status
= ocfs2_reserve_suballoc_bits(osb
, *ac
);
571 if (status
!= -ENOSPC
)
578 if ((status
< 0) && *ac
) {
579 ocfs2_free_alloc_context(*ac
);
590 /* local alloc code has to do the same thing, so rather than do this
592 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super
*osb
,
593 struct ocfs2_alloc_context
*ac
)
597 ac
->ac_inode
= ocfs2_get_system_file_inode(osb
,
598 GLOBAL_BITMAP_SYSTEM_INODE
,
602 mlog(ML_ERROR
, "Could not get bitmap inode!\n");
605 ac
->ac_which
= OCFS2_AC_USE_MAIN
;
606 ac
->ac_group_search
= ocfs2_cluster_group_search
;
608 status
= ocfs2_reserve_suballoc_bits(osb
, ac
);
609 if (status
< 0 && status
!= -ENOSPC
)
615 /* Callers don't need to care which bitmap (local alloc or main) to
616 * use so we figure it out for them, but unfortunately this clutters
618 int ocfs2_reserve_clusters(struct ocfs2_super
*osb
,
619 struct ocfs2_journal_handle
*handle
,
621 struct ocfs2_alloc_context
**ac
)
629 *ac
= kcalloc(1, sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
636 (*ac
)->ac_bits_wanted
= bits_wanted
;
637 (*ac
)->ac_handle
= handle
;
640 if (ocfs2_alloc_should_use_local(osb
, bits_wanted
)) {
641 status
= ocfs2_reserve_local_alloc_bits(osb
,
645 if ((status
< 0) && (status
!= -ENOSPC
)) {
648 } else if (status
== -ENOSPC
) {
649 /* reserve_local_bits will return enospc with
650 * the local alloc inode still locked, so we
651 * can change this safely here. */
652 mlog(0, "Disabling local alloc\n");
653 /* We set to OCFS2_LA_DISABLED so that umount
654 * can clean up what's left of the local
656 osb
->local_alloc_state
= OCFS2_LA_DISABLED
;
660 if (status
== -ENOSPC
) {
661 status
= ocfs2_reserve_cluster_bitmap_bits(osb
, *ac
);
663 if (status
!= -ENOSPC
)
671 if ((status
< 0) && *ac
) {
672 ocfs2_free_alloc_context(*ac
);
681 * More or less lifted from ext3. I'll leave their description below:
683 * "For ext3 allocations, we must not reuse any blocks which are
684 * allocated in the bitmap buffer's "last committed data" copy. This
685 * prevents deletes from freeing up the page for reuse until we have
686 * committed the delete transaction.
688 * If we didn't do this, then deleting something and reallocating it as
689 * data would allow the old block to be overwritten before the
690 * transaction committed (because we force data to disk before commit).
691 * This would lead to corruption if we crashed between overwriting the
692 * data and committing the delete.
694 * @@@ We may want to make this allocation behaviour conditional on
695 * data-writes at some point, and disable it for metadata allocations or
698 * Note: OCFS2 already does this differently for metadata vs data
699 * allocations, as those bitmaps are seperate and undo access is never
700 * called on a metadata group descriptor.
702 static int ocfs2_test_bg_bit_allocatable(struct buffer_head
*bg_bh
,
705 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
707 if (ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
))
709 if (!buffer_jbd(bg_bh
) || !bh2jh(bg_bh
)->b_committed_data
)
712 bg
= (struct ocfs2_group_desc
*) bh2jh(bg_bh
)->b_committed_data
;
713 return !ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
);
716 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super
*osb
,
717 struct buffer_head
*bg_bh
,
718 unsigned int bits_wanted
,
719 unsigned int total_bits
,
724 u16 best_offset
, best_size
;
725 int offset
, start
, found
, status
= 0;
726 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
728 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
729 OCFS2_RO_ON_INVALID_GROUP_DESC(osb
->sb
, bg
);
733 found
= start
= best_offset
= best_size
= 0;
734 bitmap
= bg
->bg_bitmap
;
736 while((offset
= ocfs2_find_next_zero_bit(bitmap
, total_bits
, start
)) != -1) {
737 if (offset
== total_bits
)
740 if (!ocfs2_test_bg_bit_allocatable(bg_bh
, offset
)) {
741 /* We found a zero, but we can't use it as it
742 * hasn't been put to disk yet! */
745 } else if (offset
== start
) {
746 /* we found a zero */
748 /* move start to the next bit to test */
751 /* got a zero after some ones */
755 if (found
> best_size
) {
757 best_offset
= start
- found
;
759 /* we got everything we needed */
760 if (found
== bits_wanted
) {
761 /* mlog(0, "Found it all!\n"); */
766 /* XXX: I think the first clause is equivalent to the second
768 if (found
== bits_wanted
) {
769 *bit_off
= start
- found
;
771 } else if (best_size
) {
772 *bit_off
= best_offset
;
773 *bits_found
= best_size
;
776 /* No error log here -- see the comment above
777 * ocfs2_test_bg_bit_allocatable */
783 static inline int ocfs2_block_group_set_bits(struct ocfs2_journal_handle
*handle
,
784 struct inode
*alloc_inode
,
785 struct ocfs2_group_desc
*bg
,
786 struct buffer_head
*group_bh
,
787 unsigned int bit_off
,
788 unsigned int num_bits
)
791 void *bitmap
= bg
->bg_bitmap
;
792 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
796 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
797 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
801 BUG_ON(le16_to_cpu(bg
->bg_free_bits_count
) < num_bits
);
803 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off
,
806 if (ocfs2_is_cluster_bitmap(alloc_inode
))
807 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
809 status
= ocfs2_journal_access(handle
,
818 le16_add_cpu(&bg
->bg_free_bits_count
, -num_bits
);
821 ocfs2_set_bit(bit_off
++, bitmap
);
823 status
= ocfs2_journal_dirty(handle
,
835 /* find the one with the most empty bits */
836 static inline u16
ocfs2_find_victim_chain(struct ocfs2_chain_list
*cl
)
840 BUG_ON(!cl
->cl_next_free_rec
);
843 while (curr
< le16_to_cpu(cl
->cl_next_free_rec
)) {
844 if (le32_to_cpu(cl
->cl_recs
[curr
].c_free
) >
845 le32_to_cpu(cl
->cl_recs
[best
].c_free
))
850 BUG_ON(best
>= le16_to_cpu(cl
->cl_next_free_rec
));
854 static int ocfs2_relink_block_group(struct ocfs2_journal_handle
*handle
,
855 struct inode
*alloc_inode
,
856 struct buffer_head
*fe_bh
,
857 struct buffer_head
*bg_bh
,
858 struct buffer_head
*prev_bg_bh
,
862 /* there is a really tiny chance the journal calls could fail,
863 * but we wouldn't want inconsistent blocks in *any* case. */
864 u64 fe_ptr
, bg_ptr
, prev_bg_ptr
;
865 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
866 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
867 struct ocfs2_group_desc
*prev_bg
= (struct ocfs2_group_desc
*) prev_bg_bh
->b_data
;
869 if (!OCFS2_IS_VALID_DINODE(fe
)) {
870 OCFS2_RO_ON_INVALID_DINODE(alloc_inode
->i_sb
, fe
);
874 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
875 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
879 if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg
)) {
880 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, prev_bg
);
885 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
886 (unsigned long long)fe
->i_blkno
, chain
,
887 (unsigned long long)bg
->bg_blkno
,
888 (unsigned long long)prev_bg
->bg_blkno
);
890 fe_ptr
= le64_to_cpu(fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
);
891 bg_ptr
= le64_to_cpu(bg
->bg_next_group
);
892 prev_bg_ptr
= le64_to_cpu(prev_bg
->bg_next_group
);
894 status
= ocfs2_journal_access(handle
, alloc_inode
, prev_bg_bh
,
895 OCFS2_JOURNAL_ACCESS_WRITE
);
901 prev_bg
->bg_next_group
= bg
->bg_next_group
;
903 status
= ocfs2_journal_dirty(handle
, prev_bg_bh
);
909 status
= ocfs2_journal_access(handle
, alloc_inode
, bg_bh
,
910 OCFS2_JOURNAL_ACCESS_WRITE
);
916 bg
->bg_next_group
= fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
;
918 status
= ocfs2_journal_dirty(handle
, bg_bh
);
924 status
= ocfs2_journal_access(handle
, alloc_inode
, fe_bh
,
925 OCFS2_JOURNAL_ACCESS_WRITE
);
931 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= bg
->bg_blkno
;
933 status
= ocfs2_journal_dirty(handle
, fe_bh
);
942 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= cpu_to_le64(fe_ptr
);
943 bg
->bg_next_group
= cpu_to_le64(bg_ptr
);
944 prev_bg
->bg_next_group
= cpu_to_le64(prev_bg_ptr
);
951 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc
*bg
,
954 return le16_to_cpu(bg
->bg_free_bits_count
) > wanted
;
957 /* return 0 on success, -ENOSPC to keep searching and any other < 0
959 static int ocfs2_cluster_group_search(struct inode
*inode
,
960 struct buffer_head
*group_bh
,
961 u32 bits_wanted
, u32 min_bits
,
962 u16
*bit_off
, u16
*bits_found
)
964 int search
= -ENOSPC
;
966 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
967 u16 tmp_off
, tmp_found
;
968 unsigned int max_bits
, gd_cluster_off
;
970 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
972 if (gd
->bg_free_bits_count
) {
973 max_bits
= le16_to_cpu(gd
->bg_bits
);
975 /* Tail groups in cluster bitmaps which aren't cpg
976 * aligned are prone to partial extention by a failed
977 * fs resize. If the file system resize never got to
978 * update the dinode cluster count, then we don't want
979 * to trust any clusters past it, regardless of what
980 * the group descriptor says. */
981 gd_cluster_off
= ocfs2_blocks_to_clusters(inode
->i_sb
,
982 le64_to_cpu(gd
->bg_blkno
));
983 if ((gd_cluster_off
+ max_bits
) >
984 OCFS2_I(inode
)->ip_clusters
) {
985 max_bits
= OCFS2_I(inode
)->ip_clusters
- gd_cluster_off
;
986 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
987 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
988 le16_to_cpu(gd
->bg_bits
),
989 OCFS2_I(inode
)->ip_clusters
, max_bits
);
992 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
993 group_bh
, bits_wanted
,
995 &tmp_off
, &tmp_found
);
999 /* ocfs2_block_group_find_clear_bits() might
1000 * return success, but we still want to return
1001 * -ENOSPC unless it found the minimum number
1003 if (min_bits
<= tmp_found
) {
1005 *bits_found
= tmp_found
;
1006 search
= 0; /* success */
1013 static int ocfs2_block_group_search(struct inode
*inode
,
1014 struct buffer_head
*group_bh
,
1015 u32 bits_wanted
, u32 min_bits
,
1016 u16
*bit_off
, u16
*bits_found
)
1019 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1021 BUG_ON(min_bits
!= 1);
1022 BUG_ON(ocfs2_is_cluster_bitmap(inode
));
1024 if (bg
->bg_free_bits_count
)
1025 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1026 group_bh
, bits_wanted
,
1027 le16_to_cpu(bg
->bg_bits
),
1028 bit_off
, bits_found
);
1033 static int ocfs2_search_chain(struct ocfs2_alloc_context
*ac
,
1037 unsigned int *num_bits
,
1041 u16 chain
, tmp_bits
;
1044 struct ocfs2_journal_handle
*handle
= ac
->ac_handle
;
1045 struct inode
*alloc_inode
= ac
->ac_inode
;
1046 struct buffer_head
*group_bh
= NULL
;
1047 struct buffer_head
*prev_group_bh
= NULL
;
1048 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1049 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1050 struct ocfs2_group_desc
*bg
;
1052 chain
= ac
->ac_chain
;
1053 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1055 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
);
1057 status
= ocfs2_read_block(OCFS2_SB(alloc_inode
->i_sb
),
1058 le64_to_cpu(cl
->cl_recs
[chain
].c_blkno
),
1059 &group_bh
, OCFS2_BH_CACHED
, alloc_inode
);
1064 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1065 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, bg
);
1072 /* for now, the chain search is a bit simplistic. We just use
1073 * the 1st group with any empty bits. */
1074 while ((status
= ac
->ac_group_search(alloc_inode
, group_bh
,
1075 bits_wanted
, min_bits
, bit_off
,
1076 &tmp_bits
)) == -ENOSPC
) {
1077 if (!bg
->bg_next_group
)
1080 if (prev_group_bh
) {
1081 brelse(prev_group_bh
);
1082 prev_group_bh
= NULL
;
1084 next_group
= le64_to_cpu(bg
->bg_next_group
);
1085 prev_group_bh
= group_bh
;
1087 status
= ocfs2_read_block(OCFS2_SB(alloc_inode
->i_sb
),
1088 next_group
, &group_bh
,
1089 OCFS2_BH_CACHED
, alloc_inode
);
1094 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1095 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, bg
);
1102 if (status
!= -ENOSPC
)
1107 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1108 tmp_bits
, (unsigned long long)bg
->bg_blkno
);
1110 *num_bits
= tmp_bits
;
1112 BUG_ON(*num_bits
== 0);
1115 * Keep track of previous block descriptor read. When
1116 * we find a target, if we have read more than X
1117 * number of descriptors, and the target is reasonably
1118 * empty, relink him to top of his chain.
1120 * We've read 0 extra blocks and only send one more to
1121 * the transaction, yet the next guy to search has a
1124 * Do this *after* figuring out how many bits we're taking out
1125 * of our target group.
1127 if (ac
->ac_allow_chain_relink
&&
1129 (ocfs2_block_group_reasonably_empty(bg
, *num_bits
))) {
1130 status
= ocfs2_relink_block_group(handle
, alloc_inode
,
1131 ac
->ac_bh
, group_bh
,
1132 prev_group_bh
, chain
);
1139 /* Ok, claim our bits now: set the info on dinode, chainlist
1140 * and then the group */
1141 status
= ocfs2_journal_access(handle
,
1144 OCFS2_JOURNAL_ACCESS_WRITE
);
1150 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1151 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(*num_bits
+ tmp_used
);
1152 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -(*num_bits
));
1154 status
= ocfs2_journal_dirty(handle
,
1161 status
= ocfs2_block_group_set_bits(handle
,
1172 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits
,
1173 (unsigned long long)fe
->i_blkno
);
1175 *bg_blkno
= le64_to_cpu(bg
->bg_blkno
);
1180 brelse(prev_group_bh
);
1186 /* will give out up to bits_wanted contiguous bits. */
1187 static int ocfs2_claim_suballoc_bits(struct ocfs2_super
*osb
,
1188 struct ocfs2_alloc_context
*ac
,
1192 unsigned int *num_bits
,
1197 struct ocfs2_chain_list
*cl
;
1198 struct ocfs2_dinode
*fe
;
1202 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1203 BUG_ON(bits_wanted
> (ac
->ac_bits_wanted
- ac
->ac_bits_given
));
1206 fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1207 if (!OCFS2_IS_VALID_DINODE(fe
)) {
1208 OCFS2_RO_ON_INVALID_DINODE(osb
->sb
, fe
);
1212 if (le32_to_cpu(fe
->id1
.bitmap1
.i_used
) >=
1213 le32_to_cpu(fe
->id1
.bitmap1
.i_total
)) {
1214 ocfs2_error(osb
->sb
, "Chain allocator dinode %llu has %u used "
1215 "bits but only %u total.",
1216 (unsigned long long)le64_to_cpu(fe
->i_blkno
),
1217 le32_to_cpu(fe
->id1
.bitmap1
.i_used
),
1218 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1223 cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1225 victim
= ocfs2_find_victim_chain(cl
);
1226 ac
->ac_chain
= victim
;
1227 ac
->ac_allow_chain_relink
= 1;
1229 status
= ocfs2_search_chain(ac
, bits_wanted
, min_bits
, bit_off
,
1230 num_bits
, bg_blkno
);
1233 if (status
< 0 && status
!= -ENOSPC
) {
1238 mlog(0, "Search of victim chain %u came up with nothing, "
1239 "trying all chains now.\n", victim
);
1241 /* If we didn't pick a good victim, then just default to
1242 * searching each chain in order. Don't allow chain relinking
1243 * because we only calculate enough journal credits for one
1244 * relink per alloc. */
1245 ac
->ac_allow_chain_relink
= 0;
1246 for (i
= 0; i
< le16_to_cpu(cl
->cl_next_free_rec
); i
++) {
1249 if (!cl
->cl_recs
[i
].c_free
)
1253 status
= ocfs2_search_chain(ac
, bits_wanted
, min_bits
,
1258 if (status
< 0 && status
!= -ENOSPC
) {
1269 int ocfs2_claim_metadata(struct ocfs2_super
*osb
,
1270 struct ocfs2_journal_handle
*handle
,
1271 struct ocfs2_alloc_context
*ac
,
1273 u16
*suballoc_bit_start
,
1274 unsigned int *num_bits
,
1281 BUG_ON(ac
->ac_bits_wanted
< (ac
->ac_bits_given
+ bits_wanted
));
1282 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_META
);
1283 BUG_ON(ac
->ac_handle
!= handle
);
1285 status
= ocfs2_claim_suballoc_bits(osb
,
1296 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1298 *blkno_start
= bg_blkno
+ (u64
) *suballoc_bit_start
;
1299 ac
->ac_bits_given
+= (*num_bits
);
1306 int ocfs2_claim_new_inode(struct ocfs2_super
*osb
,
1307 struct ocfs2_journal_handle
*handle
,
1308 struct ocfs2_alloc_context
*ac
,
1313 unsigned int num_bits
;
1319 BUG_ON(ac
->ac_bits_given
!= 0);
1320 BUG_ON(ac
->ac_bits_wanted
!= 1);
1321 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_INODE
);
1322 BUG_ON(ac
->ac_handle
!= handle
);
1324 status
= ocfs2_claim_suballoc_bits(osb
,
1335 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1337 BUG_ON(num_bits
!= 1);
1339 *fe_blkno
= bg_blkno
+ (u64
) (*suballoc_bit
);
1340 ac
->ac_bits_given
++;
1347 /* translate a group desc. blkno and it's bitmap offset into
1348 * disk cluster offset. */
1349 static inline u32
ocfs2_desc_bitmap_to_cluster_off(struct inode
*inode
,
1353 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1356 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1358 if (bg_blkno
!= osb
->first_cluster_group_blkno
)
1359 cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, bg_blkno
);
1360 cluster
+= (u32
) bg_bit_off
;
1364 /* given a cluster offset, calculate which block group it belongs to
1365 * and return that block offset. */
1366 static inline u64
ocfs2_which_cluster_group(struct inode
*inode
,
1369 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1372 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1374 group_no
= cluster
/ osb
->bitmap_cpg
;
1376 return osb
->first_cluster_group_blkno
;
1377 return ocfs2_clusters_to_blocks(inode
->i_sb
,
1378 group_no
* osb
->bitmap_cpg
);
1381 /* given the block number of a cluster start, calculate which cluster
1382 * group and descriptor bitmap offset that corresponds to. */
1383 static inline void ocfs2_block_to_cluster_group(struct inode
*inode
,
1388 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1389 u32 data_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, data_blkno
);
1391 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1393 *bg_blkno
= ocfs2_which_cluster_group(inode
,
1396 if (*bg_blkno
== osb
->first_cluster_group_blkno
)
1397 *bg_bit_off
= (u16
) data_cluster
;
1399 *bg_bit_off
= (u16
) ocfs2_blocks_to_clusters(osb
->sb
,
1400 data_blkno
- *bg_blkno
);
1404 * min_bits - minimum contiguous chunk from this total allocation we
1405 * can handle. set to what we asked for originally for a full
1406 * contig. allocation, set to '1' to indicate we can deal with extents
1409 int ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1410 struct ocfs2_journal_handle
*handle
,
1411 struct ocfs2_alloc_context
*ac
,
1417 unsigned int bits_wanted
= ac
->ac_bits_wanted
- ac
->ac_bits_given
;
1424 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1426 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_LOCAL
1427 && ac
->ac_which
!= OCFS2_AC_USE_MAIN
);
1428 BUG_ON(ac
->ac_handle
!= handle
);
1430 if (ac
->ac_which
== OCFS2_AC_USE_LOCAL
) {
1431 status
= ocfs2_claim_local_alloc_bits(osb
,
1438 atomic_inc(&osb
->alloc_stats
.local_data
);
1440 if (min_clusters
> (osb
->bitmap_cpg
- 1)) {
1441 /* The only paths asking for contiguousness
1442 * should know about this already. */
1443 mlog(ML_ERROR
, "minimum allocation requested exceeds "
1444 "group bitmap size!");
1448 /* clamp the current request down to a realistic size. */
1449 if (bits_wanted
> (osb
->bitmap_cpg
- 1))
1450 bits_wanted
= osb
->bitmap_cpg
- 1;
1452 status
= ocfs2_claim_suballoc_bits(osb
,
1461 ocfs2_desc_bitmap_to_cluster_off(ac
->ac_inode
,
1464 atomic_inc(&osb
->alloc_stats
.bitmap_data
);
1468 if (status
!= -ENOSPC
)
1473 ac
->ac_bits_given
+= *num_clusters
;
1480 static inline int ocfs2_block_group_clear_bits(struct ocfs2_journal_handle
*handle
,
1481 struct inode
*alloc_inode
,
1482 struct ocfs2_group_desc
*bg
,
1483 struct buffer_head
*group_bh
,
1484 unsigned int bit_off
,
1485 unsigned int num_bits
)
1489 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
1490 struct ocfs2_group_desc
*undo_bg
= NULL
;
1494 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
1495 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
1500 mlog(0, "off = %u, num = %u\n", bit_off
, num_bits
);
1502 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1503 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
1505 status
= ocfs2_journal_access(handle
, alloc_inode
, group_bh
,
1512 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1513 undo_bg
= (struct ocfs2_group_desc
*) bh2jh(group_bh
)->b_committed_data
;
1517 ocfs2_clear_bit((bit_off
+ tmp
),
1518 (unsigned long *) bg
->bg_bitmap
);
1519 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1520 ocfs2_set_bit(bit_off
+ tmp
,
1521 (unsigned long *) undo_bg
->bg_bitmap
);
1523 le16_add_cpu(&bg
->bg_free_bits_count
, num_bits
);
1525 status
= ocfs2_journal_dirty(handle
, group_bh
);
1533 * expects the suballoc inode to already be locked.
1535 static int ocfs2_free_suballoc_bits(struct ocfs2_journal_handle
*handle
,
1536 struct inode
*alloc_inode
,
1537 struct buffer_head
*alloc_bh
,
1538 unsigned int start_bit
,
1544 struct ocfs2_super
*osb
= OCFS2_SB(alloc_inode
->i_sb
);
1545 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) alloc_bh
->b_data
;
1546 struct ocfs2_chain_list
*cl
= &fe
->id2
.i_chain
;
1547 struct buffer_head
*group_bh
= NULL
;
1548 struct ocfs2_group_desc
*group
;
1552 if (!OCFS2_IS_VALID_DINODE(fe
)) {
1553 OCFS2_RO_ON_INVALID_DINODE(alloc_inode
->i_sb
, fe
);
1557 BUG_ON((count
+ start_bit
) > ocfs2_bits_per_group(cl
));
1559 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1560 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
, count
,
1561 (unsigned long long)bg_blkno
, start_bit
);
1563 status
= ocfs2_read_block(osb
, bg_blkno
, &group_bh
, OCFS2_BH_CACHED
,
1570 group
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1571 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, group
);
1576 BUG_ON((count
+ start_bit
) > le16_to_cpu(group
->bg_bits
));
1578 status
= ocfs2_block_group_clear_bits(handle
, alloc_inode
,
1586 status
= ocfs2_journal_access(handle
, alloc_inode
, alloc_bh
,
1587 OCFS2_JOURNAL_ACCESS_WRITE
);
1593 le32_add_cpu(&cl
->cl_recs
[le16_to_cpu(group
->bg_chain
)].c_free
,
1595 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1596 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(tmp_used
- count
);
1598 status
= ocfs2_journal_dirty(handle
, alloc_bh
);
1612 static inline u64
ocfs2_which_suballoc_group(u64 block
, unsigned int bit
)
1614 u64 group
= block
- (u64
) bit
;
1619 int ocfs2_free_dinode(struct ocfs2_journal_handle
*handle
,
1620 struct inode
*inode_alloc_inode
,
1621 struct buffer_head
*inode_alloc_bh
,
1622 struct ocfs2_dinode
*di
)
1624 u64 blk
= le64_to_cpu(di
->i_blkno
);
1625 u16 bit
= le16_to_cpu(di
->i_suballoc_bit
);
1626 u64 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
1628 return ocfs2_free_suballoc_bits(handle
, inode_alloc_inode
,
1629 inode_alloc_bh
, bit
, bg_blkno
, 1);
1632 int ocfs2_free_extent_block(struct ocfs2_journal_handle
*handle
,
1633 struct inode
*eb_alloc_inode
,
1634 struct buffer_head
*eb_alloc_bh
,
1635 struct ocfs2_extent_block
*eb
)
1637 u64 blk
= le64_to_cpu(eb
->h_blkno
);
1638 u16 bit
= le16_to_cpu(eb
->h_suballoc_bit
);
1639 u64 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
1641 return ocfs2_free_suballoc_bits(handle
, eb_alloc_inode
, eb_alloc_bh
,
1645 int ocfs2_free_clusters(struct ocfs2_journal_handle
*handle
,
1646 struct inode
*bitmap_inode
,
1647 struct buffer_head
*bitmap_bh
,
1649 unsigned int num_clusters
)
1654 struct ocfs2_dinode
*fe
;
1656 /* You can't ever have a contiguous set of clusters
1657 * bigger than a block group bitmap so we never have to worry
1658 * about looping on them. */
1662 /* This is expensive. We can safely remove once this stuff has
1663 * gotten tested really well. */
1664 BUG_ON(start_blk
!= ocfs2_clusters_to_blocks(bitmap_inode
->i_sb
, ocfs2_blocks_to_clusters(bitmap_inode
->i_sb
, start_blk
)));
1666 fe
= (struct ocfs2_dinode
*) bitmap_bh
->b_data
;
1668 ocfs2_block_to_cluster_group(bitmap_inode
, start_blk
, &bg_blkno
,
1671 mlog(0, "want to free %u clusters starting at block %llu\n",
1672 num_clusters
, (unsigned long long)start_blk
);
1673 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1674 (unsigned long long)bg_blkno
, bg_start_bit
);
1676 status
= ocfs2_free_suballoc_bits(handle
, bitmap_inode
, bitmap_bh
,
1677 bg_start_bit
, bg_blkno
,
1686 static inline void ocfs2_debug_bg(struct ocfs2_group_desc
*bg
)
1688 printk("Block Group:\n");
1689 printk("bg_signature: %s\n", bg
->bg_signature
);
1690 printk("bg_size: %u\n", bg
->bg_size
);
1691 printk("bg_bits: %u\n", bg
->bg_bits
);
1692 printk("bg_free_bits_count: %u\n", bg
->bg_free_bits_count
);
1693 printk("bg_chain: %u\n", bg
->bg_chain
);
1694 printk("bg_generation: %u\n", le32_to_cpu(bg
->bg_generation
));
1695 printk("bg_next_group: %llu\n",
1696 (unsigned long long)bg
->bg_next_group
);
1697 printk("bg_parent_dinode: %llu\n",
1698 (unsigned long long)bg
->bg_parent_dinode
);
1699 printk("bg_blkno: %llu\n",
1700 (unsigned long long)bg
->bg_blkno
);
1703 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode
*fe
)
1707 printk("Suballoc Inode %llu:\n", (unsigned long long)fe
->i_blkno
);
1708 printk("i_signature: %s\n", fe
->i_signature
);
1709 printk("i_size: %llu\n",
1710 (unsigned long long)fe
->i_size
);
1711 printk("i_clusters: %u\n", fe
->i_clusters
);
1712 printk("i_generation: %u\n",
1713 le32_to_cpu(fe
->i_generation
));
1714 printk("id1.bitmap1.i_used: %u\n",
1715 le32_to_cpu(fe
->id1
.bitmap1
.i_used
));
1716 printk("id1.bitmap1.i_total: %u\n",
1717 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1718 printk("id2.i_chain.cl_cpg: %u\n", fe
->id2
.i_chain
.cl_cpg
);
1719 printk("id2.i_chain.cl_bpc: %u\n", fe
->id2
.i_chain
.cl_bpc
);
1720 printk("id2.i_chain.cl_count: %u\n", fe
->id2
.i_chain
.cl_count
);
1721 printk("id2.i_chain.cl_next_free_rec: %u\n",
1722 fe
->id2
.i_chain
.cl_next_free_rec
);
1723 for(i
= 0; i
< fe
->id2
.i_chain
.cl_next_free_rec
; i
++) {
1724 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i
,
1725 fe
->id2
.i_chain
.cl_recs
[i
].c_free
);
1726 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i
,
1727 fe
->id2
.i_chain
.cl_recs
[i
].c_total
);
1728 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i
,
1729 (unsigned long long)fe
->id2
.i_chain
.cl_recs
[i
].c_blkno
);