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_claim_suballoc_bits(struct ocfs2_super
*osb
,
74 struct ocfs2_alloc_context
*ac
,
78 unsigned int *num_bits
,
80 static int ocfs2_test_bg_bit_allocatable(struct buffer_head
*bg_bh
,
82 static inline int ocfs2_block_group_set_bits(struct ocfs2_journal_handle
*handle
,
83 struct inode
*alloc_inode
,
84 struct ocfs2_group_desc
*bg
,
85 struct buffer_head
*group_bh
,
87 unsigned int num_bits
);
88 static inline int ocfs2_block_group_clear_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
);
95 static int ocfs2_relink_block_group(struct ocfs2_journal_handle
*handle
,
96 struct inode
*alloc_inode
,
97 struct buffer_head
*fe_bh
,
98 struct buffer_head
*bg_bh
,
99 struct buffer_head
*prev_bg_bh
,
101 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc
*bg
,
103 static int ocfs2_free_suballoc_bits(struct ocfs2_journal_handle
*handle
,
104 struct inode
*alloc_inode
,
105 struct buffer_head
*alloc_bh
,
106 unsigned int start_bit
,
109 static inline u64
ocfs2_which_suballoc_group(u64 block
,
111 static inline u32
ocfs2_desc_bitmap_to_cluster_off(struct inode
*inode
,
114 static inline u64
ocfs2_which_cluster_group(struct inode
*inode
,
116 static inline void ocfs2_block_to_cluster_group(struct inode
*inode
,
121 void ocfs2_free_alloc_context(struct ocfs2_alloc_context
*ac
)
130 static u32
ocfs2_bits_per_group(struct ocfs2_chain_list
*cl
)
132 return (u32
)le16_to_cpu(cl
->cl_cpg
) * (u32
)le16_to_cpu(cl
->cl_bpc
);
135 /* somewhat more expensive than our other checks, so use sparingly. */
136 static int ocfs2_check_group_descriptor(struct super_block
*sb
,
137 struct ocfs2_dinode
*di
,
138 struct ocfs2_group_desc
*gd
)
140 unsigned int max_bits
;
142 if (!OCFS2_IS_VALID_GROUP_DESC(gd
)) {
143 OCFS2_RO_ON_INVALID_GROUP_DESC(sb
, gd
);
147 if (di
->i_blkno
!= gd
->bg_parent_dinode
) {
148 ocfs2_error(sb
, "Group descriptor # %llu has bad parent "
149 "pointer (%llu, expected %llu)",
150 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
151 (unsigned long long)le64_to_cpu(gd
->bg_parent_dinode
),
152 (unsigned long long)le64_to_cpu(di
->i_blkno
));
156 max_bits
= le16_to_cpu(di
->id2
.i_chain
.cl_cpg
) * le16_to_cpu(di
->id2
.i_chain
.cl_bpc
);
157 if (le16_to_cpu(gd
->bg_bits
) > max_bits
) {
158 ocfs2_error(sb
, "Group descriptor # %llu has bit count of %u",
159 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
160 le16_to_cpu(gd
->bg_bits
));
164 if (le16_to_cpu(gd
->bg_chain
) >=
165 le16_to_cpu(di
->id2
.i_chain
.cl_next_free_rec
)) {
166 ocfs2_error(sb
, "Group descriptor # %llu has bad chain %u",
167 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
168 le16_to_cpu(gd
->bg_chain
));
172 if (le16_to_cpu(gd
->bg_free_bits_count
) > le16_to_cpu(gd
->bg_bits
)) {
173 ocfs2_error(sb
, "Group descriptor # %llu has bit count %u but "
174 "claims that %u are free",
175 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
176 le16_to_cpu(gd
->bg_bits
),
177 le16_to_cpu(gd
->bg_free_bits_count
));
181 if (le16_to_cpu(gd
->bg_bits
) > (8 * le16_to_cpu(gd
->bg_size
))) {
182 ocfs2_error(sb
, "Group descriptor # %llu has bit count %u but "
183 "max bitmap bits of %u",
184 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
185 le16_to_cpu(gd
->bg_bits
),
186 8 * le16_to_cpu(gd
->bg_size
));
193 static int ocfs2_block_group_fill(struct ocfs2_journal_handle
*handle
,
194 struct inode
*alloc_inode
,
195 struct buffer_head
*bg_bh
,
198 struct ocfs2_chain_list
*cl
)
201 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
202 struct super_block
* sb
= alloc_inode
->i_sb
;
206 if (((unsigned long long) bg_bh
->b_blocknr
) != group_blkno
) {
207 ocfs2_error(alloc_inode
->i_sb
, "group block (%llu) != "
209 (unsigned long long)group_blkno
,
210 (unsigned long long) bg_bh
->b_blocknr
);
215 status
= ocfs2_journal_access(handle
,
218 OCFS2_JOURNAL_ACCESS_CREATE
);
224 memset(bg
, 0, sb
->s_blocksize
);
225 strcpy(bg
->bg_signature
, OCFS2_GROUP_DESC_SIGNATURE
);
226 bg
->bg_generation
= cpu_to_le32(OCFS2_SB(sb
)->fs_generation
);
227 bg
->bg_size
= cpu_to_le16(ocfs2_group_bitmap_size(sb
));
228 bg
->bg_bits
= cpu_to_le16(ocfs2_bits_per_group(cl
));
229 bg
->bg_chain
= cpu_to_le16(my_chain
);
230 bg
->bg_next_group
= cl
->cl_recs
[my_chain
].c_blkno
;
231 bg
->bg_parent_dinode
= cpu_to_le64(OCFS2_I(alloc_inode
)->ip_blkno
);
232 bg
->bg_blkno
= cpu_to_le64(group_blkno
);
233 /* set the 1st bit in the bitmap to account for the descriptor block */
234 ocfs2_set_bit(0, (unsigned long *)bg
->bg_bitmap
);
235 bg
->bg_free_bits_count
= cpu_to_le16(le16_to_cpu(bg
->bg_bits
) - 1);
237 status
= ocfs2_journal_dirty(handle
, bg_bh
);
241 /* There is no need to zero out or otherwise initialize the
242 * other blocks in a group - All valid FS metadata in a block
243 * group stores the superblock fs_generation value at
244 * allocation time. */
251 static inline u16
ocfs2_find_smallest_chain(struct ocfs2_chain_list
*cl
)
256 while (curr
< le16_to_cpu(cl
->cl_count
)) {
257 if (le32_to_cpu(cl
->cl_recs
[best
].c_total
) >
258 le32_to_cpu(cl
->cl_recs
[curr
].c_total
))
266 * We expect the block group allocator to already be locked.
268 static int ocfs2_block_group_alloc(struct ocfs2_super
*osb
,
269 struct inode
*alloc_inode
,
270 struct buffer_head
*bh
)
273 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) bh
->b_data
;
274 struct ocfs2_chain_list
*cl
;
275 struct ocfs2_alloc_context
*ac
= NULL
;
276 struct ocfs2_journal_handle
*handle
= NULL
;
277 u32 bit_off
, num_bits
;
280 struct buffer_head
*bg_bh
= NULL
;
281 struct ocfs2_group_desc
*bg
;
283 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode
));
287 handle
= ocfs2_alloc_handle(osb
);
294 cl
= &fe
->id2
.i_chain
;
295 status
= ocfs2_reserve_clusters(osb
,
297 le16_to_cpu(cl
->cl_cpg
),
300 if (status
!= -ENOSPC
)
305 credits
= ocfs2_calc_group_alloc_credits(osb
->sb
,
306 le16_to_cpu(cl
->cl_cpg
));
307 handle
= ocfs2_start_trans(osb
, handle
, credits
);
308 if (IS_ERR(handle
)) {
309 status
= PTR_ERR(handle
);
315 status
= ocfs2_claim_clusters(osb
,
318 le16_to_cpu(cl
->cl_cpg
),
322 if (status
!= -ENOSPC
)
327 alloc_rec
= ocfs2_find_smallest_chain(cl
);
329 /* setup the group */
330 bg_blkno
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
331 mlog(0, "new descriptor, record %u, at block %llu\n",
332 alloc_rec
, (unsigned long long)bg_blkno
);
334 bg_bh
= sb_getblk(osb
->sb
, bg_blkno
);
340 ocfs2_set_new_buffer_uptodate(alloc_inode
, bg_bh
);
342 status
= ocfs2_block_group_fill(handle
,
353 bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
355 status
= ocfs2_journal_access(handle
, alloc_inode
,
356 bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
362 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_free
,
363 le16_to_cpu(bg
->bg_free_bits_count
));
364 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_total
, le16_to_cpu(bg
->bg_bits
));
365 cl
->cl_recs
[alloc_rec
].c_blkno
= cpu_to_le64(bg_blkno
);
366 if (le16_to_cpu(cl
->cl_next_free_rec
) < le16_to_cpu(cl
->cl_count
))
367 le16_add_cpu(&cl
->cl_next_free_rec
, 1);
369 le32_add_cpu(&fe
->id1
.bitmap1
.i_used
, le16_to_cpu(bg
->bg_bits
) -
370 le16_to_cpu(bg
->bg_free_bits_count
));
371 le32_add_cpu(&fe
->id1
.bitmap1
.i_total
, le16_to_cpu(bg
->bg_bits
));
372 le32_add_cpu(&fe
->i_clusters
, le16_to_cpu(cl
->cl_cpg
));
374 status
= ocfs2_journal_dirty(handle
, bh
);
380 spin_lock(&OCFS2_I(alloc_inode
)->ip_lock
);
381 OCFS2_I(alloc_inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
);
382 fe
->i_size
= cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode
->i_sb
,
383 le32_to_cpu(fe
->i_clusters
)));
384 spin_unlock(&OCFS2_I(alloc_inode
)->ip_lock
);
385 i_size_write(alloc_inode
, le64_to_cpu(fe
->i_size
));
386 alloc_inode
->i_blocks
=
387 ocfs2_align_bytes_to_sectors(i_size_read(alloc_inode
));
392 ocfs2_commit_trans(handle
);
395 ocfs2_free_alloc_context(ac
);
404 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super
*osb
,
405 struct ocfs2_alloc_context
*ac
)
408 u32 bits_wanted
= ac
->ac_bits_wanted
;
409 struct inode
*alloc_inode
= ac
->ac_inode
;
410 struct buffer_head
*bh
= NULL
;
411 struct ocfs2_journal_handle
*handle
= ac
->ac_handle
;
412 struct ocfs2_dinode
*fe
;
417 BUG_ON(handle
->flags
& OCFS2_HANDLE_STARTED
);
419 ocfs2_handle_add_inode(handle
, alloc_inode
);
420 status
= ocfs2_meta_lock(alloc_inode
, handle
, &bh
, 1);
426 fe
= (struct ocfs2_dinode
*) bh
->b_data
;
427 if (!OCFS2_IS_VALID_DINODE(fe
)) {
428 OCFS2_RO_ON_INVALID_DINODE(alloc_inode
->i_sb
, fe
);
432 if (!(fe
->i_flags
& cpu_to_le32(OCFS2_CHAIN_FL
))) {
433 ocfs2_error(alloc_inode
->i_sb
, "Invalid chain allocator %llu",
434 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
439 free_bits
= le32_to_cpu(fe
->id1
.bitmap1
.i_total
) -
440 le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
442 if (bits_wanted
> free_bits
) {
443 /* cluster bitmap never grows */
444 if (ocfs2_is_cluster_bitmap(alloc_inode
)) {
445 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
446 bits_wanted
, free_bits
);
451 status
= ocfs2_block_group_alloc(osb
, alloc_inode
, bh
);
453 if (status
!= -ENOSPC
)
457 atomic_inc(&osb
->alloc_stats
.bg_extends
);
459 /* You should never ask for this much metadata */
461 (le32_to_cpu(fe
->id1
.bitmap1
.i_total
)
462 - le32_to_cpu(fe
->id1
.bitmap1
.i_used
)));
475 int ocfs2_reserve_new_metadata(struct ocfs2_super
*osb
,
476 struct ocfs2_journal_handle
*handle
,
477 struct ocfs2_dinode
*fe
,
478 struct ocfs2_alloc_context
**ac
)
481 struct inode
*alloc_inode
= NULL
;
483 *ac
= kcalloc(1, sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
490 (*ac
)->ac_bits_wanted
= ocfs2_extend_meta_needed(fe
);
491 (*ac
)->ac_handle
= handle
;
492 (*ac
)->ac_which
= OCFS2_AC_USE_META
;
494 #ifndef OCFS2_USE_ALL_METADATA_SUBALLOCATORS
495 alloc_inode
= ocfs2_get_system_file_inode(osb
,
496 EXTENT_ALLOC_SYSTEM_INODE
,
499 alloc_inode
= ocfs2_get_system_file_inode(osb
,
500 EXTENT_ALLOC_SYSTEM_INODE
,
509 (*ac
)->ac_inode
= igrab(alloc_inode
);
510 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
512 status
= ocfs2_reserve_suballoc_bits(osb
, (*ac
));
514 if (status
!= -ENOSPC
)
521 if ((status
< 0) && *ac
) {
522 ocfs2_free_alloc_context(*ac
);
533 int ocfs2_reserve_new_inode(struct ocfs2_super
*osb
,
534 struct ocfs2_journal_handle
*handle
,
535 struct ocfs2_alloc_context
**ac
)
538 struct inode
*alloc_inode
= NULL
;
540 *ac
= kcalloc(1, sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
547 (*ac
)->ac_bits_wanted
= 1;
548 (*ac
)->ac_handle
= handle
;
549 (*ac
)->ac_which
= OCFS2_AC_USE_INODE
;
551 alloc_inode
= ocfs2_get_system_file_inode(osb
,
552 INODE_ALLOC_SYSTEM_INODE
,
560 (*ac
)->ac_inode
= igrab(alloc_inode
);
561 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
563 status
= ocfs2_reserve_suballoc_bits(osb
, *ac
);
565 if (status
!= -ENOSPC
)
572 if ((status
< 0) && *ac
) {
573 ocfs2_free_alloc_context(*ac
);
584 /* local alloc code has to do the same thing, so rather than do this
586 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super
*osb
,
587 struct ocfs2_alloc_context
*ac
)
591 ac
->ac_inode
= ocfs2_get_system_file_inode(osb
,
592 GLOBAL_BITMAP_SYSTEM_INODE
,
596 mlog(ML_ERROR
, "Could not get bitmap inode!\n");
599 ac
->ac_which
= OCFS2_AC_USE_MAIN
;
600 ac
->ac_group_search
= ocfs2_cluster_group_search
;
602 status
= ocfs2_reserve_suballoc_bits(osb
, ac
);
603 if (status
< 0 && status
!= -ENOSPC
)
609 /* Callers don't need to care which bitmap (local alloc or main) to
610 * use so we figure it out for them, but unfortunately this clutters
612 int ocfs2_reserve_clusters(struct ocfs2_super
*osb
,
613 struct ocfs2_journal_handle
*handle
,
615 struct ocfs2_alloc_context
**ac
)
623 *ac
= kcalloc(1, sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
630 (*ac
)->ac_bits_wanted
= bits_wanted
;
631 (*ac
)->ac_handle
= handle
;
634 if (ocfs2_alloc_should_use_local(osb
, bits_wanted
)) {
635 status
= ocfs2_reserve_local_alloc_bits(osb
,
639 if ((status
< 0) && (status
!= -ENOSPC
)) {
642 } else if (status
== -ENOSPC
) {
643 /* reserve_local_bits will return enospc with
644 * the local alloc inode still locked, so we
645 * can change this safely here. */
646 mlog(0, "Disabling local alloc\n");
647 /* We set to OCFS2_LA_DISABLED so that umount
648 * can clean up what's left of the local
650 osb
->local_alloc_state
= OCFS2_LA_DISABLED
;
654 if (status
== -ENOSPC
) {
655 status
= ocfs2_reserve_cluster_bitmap_bits(osb
, *ac
);
657 if (status
!= -ENOSPC
)
665 if ((status
< 0) && *ac
) {
666 ocfs2_free_alloc_context(*ac
);
675 * More or less lifted from ext3. I'll leave their description below:
677 * "For ext3 allocations, we must not reuse any blocks which are
678 * allocated in the bitmap buffer's "last committed data" copy. This
679 * prevents deletes from freeing up the page for reuse until we have
680 * committed the delete transaction.
682 * If we didn't do this, then deleting something and reallocating it as
683 * data would allow the old block to be overwritten before the
684 * transaction committed (because we force data to disk before commit).
685 * This would lead to corruption if we crashed between overwriting the
686 * data and committing the delete.
688 * @@@ We may want to make this allocation behaviour conditional on
689 * data-writes at some point, and disable it for metadata allocations or
692 * Note: OCFS2 already does this differently for metadata vs data
693 * allocations, as those bitmaps are seperate and undo access is never
694 * called on a metadata group descriptor.
696 static int ocfs2_test_bg_bit_allocatable(struct buffer_head
*bg_bh
,
699 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
701 if (ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
))
703 if (!buffer_jbd(bg_bh
) || !bh2jh(bg_bh
)->b_committed_data
)
706 bg
= (struct ocfs2_group_desc
*) bh2jh(bg_bh
)->b_committed_data
;
707 return !ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
);
710 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super
*osb
,
711 struct buffer_head
*bg_bh
,
712 unsigned int bits_wanted
,
713 unsigned int total_bits
,
718 u16 best_offset
, best_size
;
719 int offset
, start
, found
, status
= 0;
720 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
722 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
723 OCFS2_RO_ON_INVALID_GROUP_DESC(osb
->sb
, bg
);
727 found
= start
= best_offset
= best_size
= 0;
728 bitmap
= bg
->bg_bitmap
;
730 while((offset
= ocfs2_find_next_zero_bit(bitmap
, total_bits
, start
)) != -1) {
731 if (offset
== total_bits
)
734 if (!ocfs2_test_bg_bit_allocatable(bg_bh
, offset
)) {
735 /* We found a zero, but we can't use it as it
736 * hasn't been put to disk yet! */
739 } else if (offset
== start
) {
740 /* we found a zero */
742 /* move start to the next bit to test */
745 /* got a zero after some ones */
749 if (found
> best_size
) {
751 best_offset
= start
- found
;
753 /* we got everything we needed */
754 if (found
== bits_wanted
) {
755 /* mlog(0, "Found it all!\n"); */
760 /* XXX: I think the first clause is equivalent to the second
762 if (found
== bits_wanted
) {
763 *bit_off
= start
- found
;
765 } else if (best_size
) {
766 *bit_off
= best_offset
;
767 *bits_found
= best_size
;
770 /* No error log here -- see the comment above
771 * ocfs2_test_bg_bit_allocatable */
777 static inline int ocfs2_block_group_set_bits(struct ocfs2_journal_handle
*handle
,
778 struct inode
*alloc_inode
,
779 struct ocfs2_group_desc
*bg
,
780 struct buffer_head
*group_bh
,
781 unsigned int bit_off
,
782 unsigned int num_bits
)
785 void *bitmap
= bg
->bg_bitmap
;
786 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
790 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
791 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
795 BUG_ON(le16_to_cpu(bg
->bg_free_bits_count
) < num_bits
);
797 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off
,
800 if (ocfs2_is_cluster_bitmap(alloc_inode
))
801 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
803 status
= ocfs2_journal_access(handle
,
812 le16_add_cpu(&bg
->bg_free_bits_count
, -num_bits
);
815 ocfs2_set_bit(bit_off
++, bitmap
);
817 status
= ocfs2_journal_dirty(handle
,
829 /* find the one with the most empty bits */
830 static inline u16
ocfs2_find_victim_chain(struct ocfs2_chain_list
*cl
)
834 BUG_ON(!cl
->cl_next_free_rec
);
837 while (curr
< le16_to_cpu(cl
->cl_next_free_rec
)) {
838 if (le32_to_cpu(cl
->cl_recs
[curr
].c_free
) >
839 le32_to_cpu(cl
->cl_recs
[best
].c_free
))
844 BUG_ON(best
>= le16_to_cpu(cl
->cl_next_free_rec
));
848 static int ocfs2_relink_block_group(struct ocfs2_journal_handle
*handle
,
849 struct inode
*alloc_inode
,
850 struct buffer_head
*fe_bh
,
851 struct buffer_head
*bg_bh
,
852 struct buffer_head
*prev_bg_bh
,
856 /* there is a really tiny chance the journal calls could fail,
857 * but we wouldn't want inconsistent blocks in *any* case. */
858 u64 fe_ptr
, bg_ptr
, prev_bg_ptr
;
859 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
860 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
861 struct ocfs2_group_desc
*prev_bg
= (struct ocfs2_group_desc
*) prev_bg_bh
->b_data
;
863 if (!OCFS2_IS_VALID_DINODE(fe
)) {
864 OCFS2_RO_ON_INVALID_DINODE(alloc_inode
->i_sb
, fe
);
868 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
869 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
873 if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg
)) {
874 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, prev_bg
);
879 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
880 (unsigned long long)fe
->i_blkno
, chain
,
881 (unsigned long long)bg
->bg_blkno
,
882 (unsigned long long)prev_bg
->bg_blkno
);
884 fe_ptr
= le64_to_cpu(fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
);
885 bg_ptr
= le64_to_cpu(bg
->bg_next_group
);
886 prev_bg_ptr
= le64_to_cpu(prev_bg
->bg_next_group
);
888 status
= ocfs2_journal_access(handle
, alloc_inode
, prev_bg_bh
,
889 OCFS2_JOURNAL_ACCESS_WRITE
);
895 prev_bg
->bg_next_group
= bg
->bg_next_group
;
897 status
= ocfs2_journal_dirty(handle
, prev_bg_bh
);
903 status
= ocfs2_journal_access(handle
, alloc_inode
, bg_bh
,
904 OCFS2_JOURNAL_ACCESS_WRITE
);
910 bg
->bg_next_group
= fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
;
912 status
= ocfs2_journal_dirty(handle
, bg_bh
);
918 status
= ocfs2_journal_access(handle
, alloc_inode
, fe_bh
,
919 OCFS2_JOURNAL_ACCESS_WRITE
);
925 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= bg
->bg_blkno
;
927 status
= ocfs2_journal_dirty(handle
, fe_bh
);
936 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= cpu_to_le64(fe_ptr
);
937 bg
->bg_next_group
= cpu_to_le64(bg_ptr
);
938 prev_bg
->bg_next_group
= cpu_to_le64(prev_bg_ptr
);
945 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc
*bg
,
948 return le16_to_cpu(bg
->bg_free_bits_count
) > wanted
;
951 /* return 0 on success, -ENOSPC to keep searching and any other < 0
953 static int ocfs2_cluster_group_search(struct inode
*inode
,
954 struct buffer_head
*group_bh
,
955 u32 bits_wanted
, u32 min_bits
,
956 u16
*bit_off
, u16
*bits_found
)
958 int search
= -ENOSPC
;
960 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
961 u16 tmp_off
, tmp_found
;
962 unsigned int max_bits
, gd_cluster_off
;
964 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
966 if (gd
->bg_free_bits_count
) {
967 max_bits
= le16_to_cpu(gd
->bg_bits
);
969 /* Tail groups in cluster bitmaps which aren't cpg
970 * aligned are prone to partial extention by a failed
971 * fs resize. If the file system resize never got to
972 * update the dinode cluster count, then we don't want
973 * to trust any clusters past it, regardless of what
974 * the group descriptor says. */
975 gd_cluster_off
= ocfs2_blocks_to_clusters(inode
->i_sb
,
976 le64_to_cpu(gd
->bg_blkno
));
977 if ((gd_cluster_off
+ max_bits
) >
978 OCFS2_I(inode
)->ip_clusters
) {
979 max_bits
= OCFS2_I(inode
)->ip_clusters
- gd_cluster_off
;
980 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
981 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
982 le16_to_cpu(gd
->bg_bits
),
983 OCFS2_I(inode
)->ip_clusters
, max_bits
);
986 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
987 group_bh
, bits_wanted
,
989 &tmp_off
, &tmp_found
);
993 /* ocfs2_block_group_find_clear_bits() might
994 * return success, but we still want to return
995 * -ENOSPC unless it found the minimum number
997 if (min_bits
<= tmp_found
) {
999 *bits_found
= tmp_found
;
1000 search
= 0; /* success */
1007 static int ocfs2_block_group_search(struct inode
*inode
,
1008 struct buffer_head
*group_bh
,
1009 u32 bits_wanted
, u32 min_bits
,
1010 u16
*bit_off
, u16
*bits_found
)
1013 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1015 BUG_ON(min_bits
!= 1);
1016 BUG_ON(ocfs2_is_cluster_bitmap(inode
));
1018 if (bg
->bg_free_bits_count
)
1019 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1020 group_bh
, bits_wanted
,
1021 le16_to_cpu(bg
->bg_bits
),
1022 bit_off
, bits_found
);
1027 static int ocfs2_alloc_dinode_update_counts(struct inode
*inode
,
1028 struct ocfs2_journal_handle
*handle
,
1029 struct buffer_head
*di_bh
,
1035 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*) di_bh
->b_data
;
1036 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &di
->id2
.i_chain
;
1038 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
1039 OCFS2_JOURNAL_ACCESS_WRITE
);
1045 tmp_used
= le32_to_cpu(di
->id1
.bitmap1
.i_used
);
1046 di
->id1
.bitmap1
.i_used
= cpu_to_le32(num_bits
+ tmp_used
);
1047 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -num_bits
);
1049 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1057 static int ocfs2_search_one_group(struct ocfs2_alloc_context
*ac
,
1061 unsigned int *num_bits
,
1067 struct buffer_head
*group_bh
= NULL
;
1068 struct ocfs2_group_desc
*gd
;
1069 struct inode
*alloc_inode
= ac
->ac_inode
;
1070 struct ocfs2_journal_handle
*handle
= ac
->ac_handle
;
1072 ret
= ocfs2_read_block(OCFS2_SB(alloc_inode
->i_sb
), gd_blkno
,
1073 &group_bh
, OCFS2_BH_CACHED
, alloc_inode
);
1079 gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1080 if (!OCFS2_IS_VALID_GROUP_DESC(gd
)) {
1081 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, gd
);
1086 ret
= ac
->ac_group_search(alloc_inode
, group_bh
, bits_wanted
, min_bits
,
1096 ret
= ocfs2_alloc_dinode_update_counts(alloc_inode
, handle
, ac
->ac_bh
,
1098 le16_to_cpu(gd
->bg_chain
));
1104 ret
= ocfs2_block_group_set_bits(handle
, alloc_inode
, gd
, group_bh
,
1105 *bit_off
, *num_bits
);
1109 *bits_left
= le16_to_cpu(gd
->bg_free_bits_count
);
1117 static int ocfs2_search_chain(struct ocfs2_alloc_context
*ac
,
1121 unsigned int *num_bits
,
1126 u16 chain
, tmp_bits
;
1129 struct ocfs2_journal_handle
*handle
= ac
->ac_handle
;
1130 struct inode
*alloc_inode
= ac
->ac_inode
;
1131 struct buffer_head
*group_bh
= NULL
;
1132 struct buffer_head
*prev_group_bh
= NULL
;
1133 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1134 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1135 struct ocfs2_group_desc
*bg
;
1137 chain
= ac
->ac_chain
;
1138 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1140 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
);
1142 status
= ocfs2_read_block(OCFS2_SB(alloc_inode
->i_sb
),
1143 le64_to_cpu(cl
->cl_recs
[chain
].c_blkno
),
1144 &group_bh
, OCFS2_BH_CACHED
, alloc_inode
);
1149 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1150 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, bg
);
1157 /* for now, the chain search is a bit simplistic. We just use
1158 * the 1st group with any empty bits. */
1159 while ((status
= ac
->ac_group_search(alloc_inode
, group_bh
,
1160 bits_wanted
, min_bits
, bit_off
,
1161 &tmp_bits
)) == -ENOSPC
) {
1162 if (!bg
->bg_next_group
)
1165 if (prev_group_bh
) {
1166 brelse(prev_group_bh
);
1167 prev_group_bh
= NULL
;
1169 next_group
= le64_to_cpu(bg
->bg_next_group
);
1170 prev_group_bh
= group_bh
;
1172 status
= ocfs2_read_block(OCFS2_SB(alloc_inode
->i_sb
),
1173 next_group
, &group_bh
,
1174 OCFS2_BH_CACHED
, alloc_inode
);
1179 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1180 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, bg
);
1187 if (status
!= -ENOSPC
)
1192 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1193 tmp_bits
, (unsigned long long)bg
->bg_blkno
);
1195 *num_bits
= tmp_bits
;
1197 BUG_ON(*num_bits
== 0);
1200 * Keep track of previous block descriptor read. When
1201 * we find a target, if we have read more than X
1202 * number of descriptors, and the target is reasonably
1203 * empty, relink him to top of his chain.
1205 * We've read 0 extra blocks and only send one more to
1206 * the transaction, yet the next guy to search has a
1209 * Do this *after* figuring out how many bits we're taking out
1210 * of our target group.
1212 if (ac
->ac_allow_chain_relink
&&
1214 (ocfs2_block_group_reasonably_empty(bg
, *num_bits
))) {
1215 status
= ocfs2_relink_block_group(handle
, alloc_inode
,
1216 ac
->ac_bh
, group_bh
,
1217 prev_group_bh
, chain
);
1224 /* Ok, claim our bits now: set the info on dinode, chainlist
1225 * and then the group */
1226 status
= ocfs2_journal_access(handle
,
1229 OCFS2_JOURNAL_ACCESS_WRITE
);
1235 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1236 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(*num_bits
+ tmp_used
);
1237 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -(*num_bits
));
1239 status
= ocfs2_journal_dirty(handle
,
1246 status
= ocfs2_block_group_set_bits(handle
,
1257 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits
,
1258 (unsigned long long)fe
->i_blkno
);
1260 *bg_blkno
= le64_to_cpu(bg
->bg_blkno
);
1261 *bits_left
= le16_to_cpu(bg
->bg_free_bits_count
);
1266 brelse(prev_group_bh
);
1272 /* will give out up to bits_wanted contiguous bits. */
1273 static int ocfs2_claim_suballoc_bits(struct ocfs2_super
*osb
,
1274 struct ocfs2_alloc_context
*ac
,
1278 unsigned int *num_bits
,
1284 u64 hint_blkno
= ac
->ac_last_group
;
1285 struct ocfs2_chain_list
*cl
;
1286 struct ocfs2_dinode
*fe
;
1290 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1291 BUG_ON(bits_wanted
> (ac
->ac_bits_wanted
- ac
->ac_bits_given
));
1294 fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1295 if (!OCFS2_IS_VALID_DINODE(fe
)) {
1296 OCFS2_RO_ON_INVALID_DINODE(osb
->sb
, fe
);
1300 if (le32_to_cpu(fe
->id1
.bitmap1
.i_used
) >=
1301 le32_to_cpu(fe
->id1
.bitmap1
.i_total
)) {
1302 ocfs2_error(osb
->sb
, "Chain allocator dinode %llu has %u used "
1303 "bits but only %u total.",
1304 (unsigned long long)le64_to_cpu(fe
->i_blkno
),
1305 le32_to_cpu(fe
->id1
.bitmap1
.i_used
),
1306 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1312 /* Attempt to short-circuit the usual search mechanism
1313 * by jumping straight to the most recently used
1314 * allocation group. This helps us mantain some
1315 * contiguousness across allocations. */
1316 status
= ocfs2_search_one_group(ac
, bits_wanted
, min_bits
,
1318 hint_blkno
, &bits_left
);
1320 /* Be careful to update *bg_blkno here as the
1321 * caller is expecting it to be filled in, and
1322 * ocfs2_search_one_group() won't do that for
1324 *bg_blkno
= hint_blkno
;
1327 if (status
< 0 && status
!= -ENOSPC
) {
1333 cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1335 victim
= ocfs2_find_victim_chain(cl
);
1336 ac
->ac_chain
= victim
;
1337 ac
->ac_allow_chain_relink
= 1;
1339 status
= ocfs2_search_chain(ac
, bits_wanted
, min_bits
, bit_off
,
1340 num_bits
, bg_blkno
, &bits_left
);
1343 if (status
< 0 && status
!= -ENOSPC
) {
1348 mlog(0, "Search of victim chain %u came up with nothing, "
1349 "trying all chains now.\n", victim
);
1351 /* If we didn't pick a good victim, then just default to
1352 * searching each chain in order. Don't allow chain relinking
1353 * because we only calculate enough journal credits for one
1354 * relink per alloc. */
1355 ac
->ac_allow_chain_relink
= 0;
1356 for (i
= 0; i
< le16_to_cpu(cl
->cl_next_free_rec
); i
++) {
1359 if (!cl
->cl_recs
[i
].c_free
)
1363 status
= ocfs2_search_chain(ac
, bits_wanted
, min_bits
,
1364 bit_off
, num_bits
, bg_blkno
,
1368 if (status
< 0 && status
!= -ENOSPC
) {
1375 if (status
!= -ENOSPC
) {
1376 /* If the next search of this group is not likely to
1377 * yield a suitable extent, then we reset the last
1378 * group hint so as to not waste a disk read */
1379 if (bits_left
< min_bits
)
1380 ac
->ac_last_group
= 0;
1382 ac
->ac_last_group
= *bg_blkno
;
1390 int ocfs2_claim_metadata(struct ocfs2_super
*osb
,
1391 struct ocfs2_journal_handle
*handle
,
1392 struct ocfs2_alloc_context
*ac
,
1394 u16
*suballoc_bit_start
,
1395 unsigned int *num_bits
,
1402 BUG_ON(ac
->ac_bits_wanted
< (ac
->ac_bits_given
+ bits_wanted
));
1403 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_META
);
1404 BUG_ON(ac
->ac_handle
!= handle
);
1406 status
= ocfs2_claim_suballoc_bits(osb
,
1417 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1419 *blkno_start
= bg_blkno
+ (u64
) *suballoc_bit_start
;
1420 ac
->ac_bits_given
+= (*num_bits
);
1427 int ocfs2_claim_new_inode(struct ocfs2_super
*osb
,
1428 struct ocfs2_journal_handle
*handle
,
1429 struct ocfs2_alloc_context
*ac
,
1434 unsigned int num_bits
;
1440 BUG_ON(ac
->ac_bits_given
!= 0);
1441 BUG_ON(ac
->ac_bits_wanted
!= 1);
1442 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_INODE
);
1443 BUG_ON(ac
->ac_handle
!= handle
);
1445 status
= ocfs2_claim_suballoc_bits(osb
,
1456 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1458 BUG_ON(num_bits
!= 1);
1460 *fe_blkno
= bg_blkno
+ (u64
) (*suballoc_bit
);
1461 ac
->ac_bits_given
++;
1468 /* translate a group desc. blkno and it's bitmap offset into
1469 * disk cluster offset. */
1470 static inline u32
ocfs2_desc_bitmap_to_cluster_off(struct inode
*inode
,
1474 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1477 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1479 if (bg_blkno
!= osb
->first_cluster_group_blkno
)
1480 cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, bg_blkno
);
1481 cluster
+= (u32
) bg_bit_off
;
1485 /* given a cluster offset, calculate which block group it belongs to
1486 * and return that block offset. */
1487 static inline u64
ocfs2_which_cluster_group(struct inode
*inode
,
1490 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1493 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1495 group_no
= cluster
/ osb
->bitmap_cpg
;
1497 return osb
->first_cluster_group_blkno
;
1498 return ocfs2_clusters_to_blocks(inode
->i_sb
,
1499 group_no
* osb
->bitmap_cpg
);
1502 /* given the block number of a cluster start, calculate which cluster
1503 * group and descriptor bitmap offset that corresponds to. */
1504 static inline void ocfs2_block_to_cluster_group(struct inode
*inode
,
1509 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1510 u32 data_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, data_blkno
);
1512 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1514 *bg_blkno
= ocfs2_which_cluster_group(inode
,
1517 if (*bg_blkno
== osb
->first_cluster_group_blkno
)
1518 *bg_bit_off
= (u16
) data_cluster
;
1520 *bg_bit_off
= (u16
) ocfs2_blocks_to_clusters(osb
->sb
,
1521 data_blkno
- *bg_blkno
);
1525 * min_bits - minimum contiguous chunk from this total allocation we
1526 * can handle. set to what we asked for originally for a full
1527 * contig. allocation, set to '1' to indicate we can deal with extents
1530 int ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1531 struct ocfs2_journal_handle
*handle
,
1532 struct ocfs2_alloc_context
*ac
,
1538 unsigned int bits_wanted
= ac
->ac_bits_wanted
- ac
->ac_bits_given
;
1545 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1547 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_LOCAL
1548 && ac
->ac_which
!= OCFS2_AC_USE_MAIN
);
1549 BUG_ON(ac
->ac_handle
!= handle
);
1551 if (ac
->ac_which
== OCFS2_AC_USE_LOCAL
) {
1552 status
= ocfs2_claim_local_alloc_bits(osb
,
1559 atomic_inc(&osb
->alloc_stats
.local_data
);
1561 if (min_clusters
> (osb
->bitmap_cpg
- 1)) {
1562 /* The only paths asking for contiguousness
1563 * should know about this already. */
1564 mlog(ML_ERROR
, "minimum allocation requested exceeds "
1565 "group bitmap size!");
1569 /* clamp the current request down to a realistic size. */
1570 if (bits_wanted
> (osb
->bitmap_cpg
- 1))
1571 bits_wanted
= osb
->bitmap_cpg
- 1;
1573 status
= ocfs2_claim_suballoc_bits(osb
,
1582 ocfs2_desc_bitmap_to_cluster_off(ac
->ac_inode
,
1585 atomic_inc(&osb
->alloc_stats
.bitmap_data
);
1589 if (status
!= -ENOSPC
)
1594 ac
->ac_bits_given
+= *num_clusters
;
1601 static inline int ocfs2_block_group_clear_bits(struct ocfs2_journal_handle
*handle
,
1602 struct inode
*alloc_inode
,
1603 struct ocfs2_group_desc
*bg
,
1604 struct buffer_head
*group_bh
,
1605 unsigned int bit_off
,
1606 unsigned int num_bits
)
1610 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
1611 struct ocfs2_group_desc
*undo_bg
= NULL
;
1615 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
1616 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
1621 mlog(0, "off = %u, num = %u\n", bit_off
, num_bits
);
1623 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1624 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
1626 status
= ocfs2_journal_access(handle
, alloc_inode
, group_bh
,
1633 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1634 undo_bg
= (struct ocfs2_group_desc
*) bh2jh(group_bh
)->b_committed_data
;
1638 ocfs2_clear_bit((bit_off
+ tmp
),
1639 (unsigned long *) bg
->bg_bitmap
);
1640 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1641 ocfs2_set_bit(bit_off
+ tmp
,
1642 (unsigned long *) undo_bg
->bg_bitmap
);
1644 le16_add_cpu(&bg
->bg_free_bits_count
, num_bits
);
1646 status
= ocfs2_journal_dirty(handle
, group_bh
);
1654 * expects the suballoc inode to already be locked.
1656 static int ocfs2_free_suballoc_bits(struct ocfs2_journal_handle
*handle
,
1657 struct inode
*alloc_inode
,
1658 struct buffer_head
*alloc_bh
,
1659 unsigned int start_bit
,
1665 struct ocfs2_super
*osb
= OCFS2_SB(alloc_inode
->i_sb
);
1666 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) alloc_bh
->b_data
;
1667 struct ocfs2_chain_list
*cl
= &fe
->id2
.i_chain
;
1668 struct buffer_head
*group_bh
= NULL
;
1669 struct ocfs2_group_desc
*group
;
1673 if (!OCFS2_IS_VALID_DINODE(fe
)) {
1674 OCFS2_RO_ON_INVALID_DINODE(alloc_inode
->i_sb
, fe
);
1678 BUG_ON((count
+ start_bit
) > ocfs2_bits_per_group(cl
));
1680 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1681 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
, count
,
1682 (unsigned long long)bg_blkno
, start_bit
);
1684 status
= ocfs2_read_block(osb
, bg_blkno
, &group_bh
, OCFS2_BH_CACHED
,
1691 group
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1692 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, group
);
1697 BUG_ON((count
+ start_bit
) > le16_to_cpu(group
->bg_bits
));
1699 status
= ocfs2_block_group_clear_bits(handle
, alloc_inode
,
1707 status
= ocfs2_journal_access(handle
, alloc_inode
, alloc_bh
,
1708 OCFS2_JOURNAL_ACCESS_WRITE
);
1714 le32_add_cpu(&cl
->cl_recs
[le16_to_cpu(group
->bg_chain
)].c_free
,
1716 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1717 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(tmp_used
- count
);
1719 status
= ocfs2_journal_dirty(handle
, alloc_bh
);
1733 static inline u64
ocfs2_which_suballoc_group(u64 block
, unsigned int bit
)
1735 u64 group
= block
- (u64
) bit
;
1740 int ocfs2_free_dinode(struct ocfs2_journal_handle
*handle
,
1741 struct inode
*inode_alloc_inode
,
1742 struct buffer_head
*inode_alloc_bh
,
1743 struct ocfs2_dinode
*di
)
1745 u64 blk
= le64_to_cpu(di
->i_blkno
);
1746 u16 bit
= le16_to_cpu(di
->i_suballoc_bit
);
1747 u64 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
1749 return ocfs2_free_suballoc_bits(handle
, inode_alloc_inode
,
1750 inode_alloc_bh
, bit
, bg_blkno
, 1);
1753 int ocfs2_free_extent_block(struct ocfs2_journal_handle
*handle
,
1754 struct inode
*eb_alloc_inode
,
1755 struct buffer_head
*eb_alloc_bh
,
1756 struct ocfs2_extent_block
*eb
)
1758 u64 blk
= le64_to_cpu(eb
->h_blkno
);
1759 u16 bit
= le16_to_cpu(eb
->h_suballoc_bit
);
1760 u64 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
1762 return ocfs2_free_suballoc_bits(handle
, eb_alloc_inode
, eb_alloc_bh
,
1766 int ocfs2_free_clusters(struct ocfs2_journal_handle
*handle
,
1767 struct inode
*bitmap_inode
,
1768 struct buffer_head
*bitmap_bh
,
1770 unsigned int num_clusters
)
1775 struct ocfs2_dinode
*fe
;
1777 /* You can't ever have a contiguous set of clusters
1778 * bigger than a block group bitmap so we never have to worry
1779 * about looping on them. */
1783 /* This is expensive. We can safely remove once this stuff has
1784 * gotten tested really well. */
1785 BUG_ON(start_blk
!= ocfs2_clusters_to_blocks(bitmap_inode
->i_sb
, ocfs2_blocks_to_clusters(bitmap_inode
->i_sb
, start_blk
)));
1787 fe
= (struct ocfs2_dinode
*) bitmap_bh
->b_data
;
1789 ocfs2_block_to_cluster_group(bitmap_inode
, start_blk
, &bg_blkno
,
1792 mlog(0, "want to free %u clusters starting at block %llu\n",
1793 num_clusters
, (unsigned long long)start_blk
);
1794 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1795 (unsigned long long)bg_blkno
, bg_start_bit
);
1797 status
= ocfs2_free_suballoc_bits(handle
, bitmap_inode
, bitmap_bh
,
1798 bg_start_bit
, bg_blkno
,
1807 static inline void ocfs2_debug_bg(struct ocfs2_group_desc
*bg
)
1809 printk("Block Group:\n");
1810 printk("bg_signature: %s\n", bg
->bg_signature
);
1811 printk("bg_size: %u\n", bg
->bg_size
);
1812 printk("bg_bits: %u\n", bg
->bg_bits
);
1813 printk("bg_free_bits_count: %u\n", bg
->bg_free_bits_count
);
1814 printk("bg_chain: %u\n", bg
->bg_chain
);
1815 printk("bg_generation: %u\n", le32_to_cpu(bg
->bg_generation
));
1816 printk("bg_next_group: %llu\n",
1817 (unsigned long long)bg
->bg_next_group
);
1818 printk("bg_parent_dinode: %llu\n",
1819 (unsigned long long)bg
->bg_parent_dinode
);
1820 printk("bg_blkno: %llu\n",
1821 (unsigned long long)bg
->bg_blkno
);
1824 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode
*fe
)
1828 printk("Suballoc Inode %llu:\n", (unsigned long long)fe
->i_blkno
);
1829 printk("i_signature: %s\n", fe
->i_signature
);
1830 printk("i_size: %llu\n",
1831 (unsigned long long)fe
->i_size
);
1832 printk("i_clusters: %u\n", fe
->i_clusters
);
1833 printk("i_generation: %u\n",
1834 le32_to_cpu(fe
->i_generation
));
1835 printk("id1.bitmap1.i_used: %u\n",
1836 le32_to_cpu(fe
->id1
.bitmap1
.i_used
));
1837 printk("id1.bitmap1.i_total: %u\n",
1838 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1839 printk("id2.i_chain.cl_cpg: %u\n", fe
->id2
.i_chain
.cl_cpg
);
1840 printk("id2.i_chain.cl_bpc: %u\n", fe
->id2
.i_chain
.cl_bpc
);
1841 printk("id2.i_chain.cl_count: %u\n", fe
->id2
.i_chain
.cl_count
);
1842 printk("id2.i_chain.cl_next_free_rec: %u\n",
1843 fe
->id2
.i_chain
.cl_next_free_rec
);
1844 for(i
= 0; i
< fe
->id2
.i_chain
.cl_next_free_rec
; i
++) {
1845 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i
,
1846 fe
->id2
.i_chain
.cl_recs
[i
].c_free
);
1847 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i
,
1848 fe
->id2
.i_chain
.cl_recs
[i
].c_total
);
1849 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i
,
1850 (unsigned long long)fe
->id2
.i_chain
.cl_recs
[i
].c_blkno
);