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 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
)) {
692 if (status
== -ENOSPC
) {
693 status
= ocfs2_reserve_cluster_bitmap_bits(osb
, *ac
);
695 if (status
!= -ENOSPC
)
703 if ((status
< 0) && *ac
) {
704 ocfs2_free_alloc_context(*ac
);
713 * More or less lifted from ext3. I'll leave their description below:
715 * "For ext3 allocations, we must not reuse any blocks which are
716 * allocated in the bitmap buffer's "last committed data" copy. This
717 * prevents deletes from freeing up the page for reuse until we have
718 * committed the delete transaction.
720 * If we didn't do this, then deleting something and reallocating it as
721 * data would allow the old block to be overwritten before the
722 * transaction committed (because we force data to disk before commit).
723 * This would lead to corruption if we crashed between overwriting the
724 * data and committing the delete.
726 * @@@ We may want to make this allocation behaviour conditional on
727 * data-writes at some point, and disable it for metadata allocations or
730 * Note: OCFS2 already does this differently for metadata vs data
731 * allocations, as those bitmaps are separate and undo access is never
732 * called on a metadata group descriptor.
734 static int ocfs2_test_bg_bit_allocatable(struct buffer_head
*bg_bh
,
737 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
739 if (ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
))
741 if (!buffer_jbd(bg_bh
) || !bh2jh(bg_bh
)->b_committed_data
)
744 bg
= (struct ocfs2_group_desc
*) bh2jh(bg_bh
)->b_committed_data
;
745 return !ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
);
748 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super
*osb
,
749 struct buffer_head
*bg_bh
,
750 unsigned int bits_wanted
,
751 unsigned int total_bits
,
756 u16 best_offset
, best_size
;
757 int offset
, start
, found
, status
= 0;
758 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
760 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
761 OCFS2_RO_ON_INVALID_GROUP_DESC(osb
->sb
, bg
);
765 found
= start
= best_offset
= best_size
= 0;
766 bitmap
= bg
->bg_bitmap
;
768 while((offset
= ocfs2_find_next_zero_bit(bitmap
, total_bits
, start
)) != -1) {
769 if (offset
== total_bits
)
772 if (!ocfs2_test_bg_bit_allocatable(bg_bh
, offset
)) {
773 /* We found a zero, but we can't use it as it
774 * hasn't been put to disk yet! */
777 } else if (offset
== start
) {
778 /* we found a zero */
780 /* move start to the next bit to test */
783 /* got a zero after some ones */
787 if (found
> best_size
) {
789 best_offset
= start
- found
;
791 /* we got everything we needed */
792 if (found
== bits_wanted
) {
793 /* mlog(0, "Found it all!\n"); */
798 /* XXX: I think the first clause is equivalent to the second
800 if (found
== bits_wanted
) {
801 *bit_off
= start
- found
;
803 } else if (best_size
) {
804 *bit_off
= best_offset
;
805 *bits_found
= best_size
;
808 /* No error log here -- see the comment above
809 * ocfs2_test_bg_bit_allocatable */
815 static inline int ocfs2_block_group_set_bits(handle_t
*handle
,
816 struct inode
*alloc_inode
,
817 struct ocfs2_group_desc
*bg
,
818 struct buffer_head
*group_bh
,
819 unsigned int bit_off
,
820 unsigned int num_bits
)
823 void *bitmap
= bg
->bg_bitmap
;
824 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
828 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
829 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
833 BUG_ON(le16_to_cpu(bg
->bg_free_bits_count
) < num_bits
);
835 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off
,
838 if (ocfs2_is_cluster_bitmap(alloc_inode
))
839 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
841 status
= ocfs2_journal_access(handle
,
850 le16_add_cpu(&bg
->bg_free_bits_count
, -num_bits
);
853 ocfs2_set_bit(bit_off
++, bitmap
);
855 status
= ocfs2_journal_dirty(handle
,
867 /* find the one with the most empty bits */
868 static inline u16
ocfs2_find_victim_chain(struct ocfs2_chain_list
*cl
)
872 BUG_ON(!cl
->cl_next_free_rec
);
875 while (curr
< le16_to_cpu(cl
->cl_next_free_rec
)) {
876 if (le32_to_cpu(cl
->cl_recs
[curr
].c_free
) >
877 le32_to_cpu(cl
->cl_recs
[best
].c_free
))
882 BUG_ON(best
>= le16_to_cpu(cl
->cl_next_free_rec
));
886 static int ocfs2_relink_block_group(handle_t
*handle
,
887 struct inode
*alloc_inode
,
888 struct buffer_head
*fe_bh
,
889 struct buffer_head
*bg_bh
,
890 struct buffer_head
*prev_bg_bh
,
894 /* there is a really tiny chance the journal calls could fail,
895 * but we wouldn't want inconsistent blocks in *any* case. */
896 u64 fe_ptr
, bg_ptr
, prev_bg_ptr
;
897 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
898 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
899 struct ocfs2_group_desc
*prev_bg
= (struct ocfs2_group_desc
*) prev_bg_bh
->b_data
;
901 if (!OCFS2_IS_VALID_DINODE(fe
)) {
902 OCFS2_RO_ON_INVALID_DINODE(alloc_inode
->i_sb
, fe
);
906 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
907 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
911 if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg
)) {
912 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, prev_bg
);
917 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
918 (unsigned long long)le64_to_cpu(fe
->i_blkno
), chain
,
919 (unsigned long long)le64_to_cpu(bg
->bg_blkno
),
920 (unsigned long long)le64_to_cpu(prev_bg
->bg_blkno
));
922 fe_ptr
= le64_to_cpu(fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
);
923 bg_ptr
= le64_to_cpu(bg
->bg_next_group
);
924 prev_bg_ptr
= le64_to_cpu(prev_bg
->bg_next_group
);
926 status
= ocfs2_journal_access(handle
, alloc_inode
, prev_bg_bh
,
927 OCFS2_JOURNAL_ACCESS_WRITE
);
933 prev_bg
->bg_next_group
= bg
->bg_next_group
;
935 status
= ocfs2_journal_dirty(handle
, prev_bg_bh
);
941 status
= ocfs2_journal_access(handle
, alloc_inode
, bg_bh
,
942 OCFS2_JOURNAL_ACCESS_WRITE
);
948 bg
->bg_next_group
= fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
;
950 status
= ocfs2_journal_dirty(handle
, bg_bh
);
956 status
= ocfs2_journal_access(handle
, alloc_inode
, fe_bh
,
957 OCFS2_JOURNAL_ACCESS_WRITE
);
963 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= bg
->bg_blkno
;
965 status
= ocfs2_journal_dirty(handle
, fe_bh
);
974 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= cpu_to_le64(fe_ptr
);
975 bg
->bg_next_group
= cpu_to_le64(bg_ptr
);
976 prev_bg
->bg_next_group
= cpu_to_le64(prev_bg_ptr
);
983 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc
*bg
,
986 return le16_to_cpu(bg
->bg_free_bits_count
) > wanted
;
989 /* return 0 on success, -ENOSPC to keep searching and any other < 0
991 static int ocfs2_cluster_group_search(struct inode
*inode
,
992 struct buffer_head
*group_bh
,
993 u32 bits_wanted
, u32 min_bits
,
994 u16
*bit_off
, u16
*bits_found
)
996 int search
= -ENOSPC
;
998 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
999 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1000 u16 tmp_off
, tmp_found
;
1001 unsigned int max_bits
, gd_cluster_off
;
1003 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1005 if (gd
->bg_free_bits_count
) {
1006 max_bits
= le16_to_cpu(gd
->bg_bits
);
1008 /* Tail groups in cluster bitmaps which aren't cpg
1009 * aligned are prone to partial extention by a failed
1010 * fs resize. If the file system resize never got to
1011 * update the dinode cluster count, then we don't want
1012 * to trust any clusters past it, regardless of what
1013 * the group descriptor says. */
1014 gd_cluster_off
= ocfs2_blocks_to_clusters(inode
->i_sb
,
1015 le64_to_cpu(gd
->bg_blkno
));
1016 if ((gd_cluster_off
+ max_bits
) >
1017 OCFS2_I(inode
)->ip_clusters
) {
1018 max_bits
= OCFS2_I(inode
)->ip_clusters
- gd_cluster_off
;
1019 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1020 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
1021 le16_to_cpu(gd
->bg_bits
),
1022 OCFS2_I(inode
)->ip_clusters
, max_bits
);
1025 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1026 group_bh
, bits_wanted
,
1028 &tmp_off
, &tmp_found
);
1032 /* ocfs2_block_group_find_clear_bits() might
1033 * return success, but we still want to return
1034 * -ENOSPC unless it found the minimum number
1036 if (min_bits
<= tmp_found
) {
1038 *bits_found
= tmp_found
;
1039 search
= 0; /* success */
1040 } else if (tmp_found
) {
1042 * Don't show bits which we'll be returning
1043 * for allocation to the local alloc bitmap.
1045 ocfs2_local_alloc_seen_free_bits(osb
, tmp_found
);
1052 static int ocfs2_block_group_search(struct inode
*inode
,
1053 struct buffer_head
*group_bh
,
1054 u32 bits_wanted
, u32 min_bits
,
1055 u16
*bit_off
, u16
*bits_found
)
1058 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1060 BUG_ON(min_bits
!= 1);
1061 BUG_ON(ocfs2_is_cluster_bitmap(inode
));
1063 if (bg
->bg_free_bits_count
)
1064 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1065 group_bh
, bits_wanted
,
1066 le16_to_cpu(bg
->bg_bits
),
1067 bit_off
, bits_found
);
1072 static int ocfs2_alloc_dinode_update_counts(struct inode
*inode
,
1074 struct buffer_head
*di_bh
,
1080 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*) di_bh
->b_data
;
1081 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &di
->id2
.i_chain
;
1083 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
1084 OCFS2_JOURNAL_ACCESS_WRITE
);
1090 tmp_used
= le32_to_cpu(di
->id1
.bitmap1
.i_used
);
1091 di
->id1
.bitmap1
.i_used
= cpu_to_le32(num_bits
+ tmp_used
);
1092 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -num_bits
);
1094 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1102 static int ocfs2_search_one_group(struct ocfs2_alloc_context
*ac
,
1107 unsigned int *num_bits
,
1113 struct buffer_head
*group_bh
= NULL
;
1114 struct ocfs2_group_desc
*gd
;
1115 struct inode
*alloc_inode
= ac
->ac_inode
;
1117 ret
= ocfs2_read_block(OCFS2_SB(alloc_inode
->i_sb
), gd_blkno
,
1118 &group_bh
, OCFS2_BH_CACHED
, alloc_inode
);
1124 gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1125 if (!OCFS2_IS_VALID_GROUP_DESC(gd
)) {
1126 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, gd
);
1131 ret
= ac
->ac_group_search(alloc_inode
, group_bh
, bits_wanted
, min_bits
,
1141 ret
= ocfs2_alloc_dinode_update_counts(alloc_inode
, handle
, ac
->ac_bh
,
1143 le16_to_cpu(gd
->bg_chain
));
1149 ret
= ocfs2_block_group_set_bits(handle
, alloc_inode
, gd
, group_bh
,
1150 *bit_off
, *num_bits
);
1154 *bits_left
= le16_to_cpu(gd
->bg_free_bits_count
);
1162 static int ocfs2_search_chain(struct ocfs2_alloc_context
*ac
,
1167 unsigned int *num_bits
,
1172 u16 chain
, tmp_bits
;
1175 struct inode
*alloc_inode
= ac
->ac_inode
;
1176 struct buffer_head
*group_bh
= NULL
;
1177 struct buffer_head
*prev_group_bh
= NULL
;
1178 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1179 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1180 struct ocfs2_group_desc
*bg
;
1182 chain
= ac
->ac_chain
;
1183 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1185 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
);
1187 status
= ocfs2_read_block(OCFS2_SB(alloc_inode
->i_sb
),
1188 le64_to_cpu(cl
->cl_recs
[chain
].c_blkno
),
1189 &group_bh
, OCFS2_BH_CACHED
, alloc_inode
);
1194 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1195 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, bg
);
1202 /* for now, the chain search is a bit simplistic. We just use
1203 * the 1st group with any empty bits. */
1204 while ((status
= ac
->ac_group_search(alloc_inode
, group_bh
, bits_wanted
,
1205 min_bits
, bit_off
, &tmp_bits
)) == -ENOSPC
) {
1206 if (!bg
->bg_next_group
)
1209 if (prev_group_bh
) {
1210 brelse(prev_group_bh
);
1211 prev_group_bh
= NULL
;
1213 next_group
= le64_to_cpu(bg
->bg_next_group
);
1214 prev_group_bh
= group_bh
;
1216 status
= ocfs2_read_block(OCFS2_SB(alloc_inode
->i_sb
),
1217 next_group
, &group_bh
,
1218 OCFS2_BH_CACHED
, alloc_inode
);
1223 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1224 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, bg
);
1231 if (status
!= -ENOSPC
)
1236 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1237 tmp_bits
, (unsigned long long)le64_to_cpu(bg
->bg_blkno
));
1239 *num_bits
= tmp_bits
;
1241 BUG_ON(*num_bits
== 0);
1244 * Keep track of previous block descriptor read. When
1245 * we find a target, if we have read more than X
1246 * number of descriptors, and the target is reasonably
1247 * empty, relink him to top of his chain.
1249 * We've read 0 extra blocks and only send one more to
1250 * the transaction, yet the next guy to search has a
1253 * Do this *after* figuring out how many bits we're taking out
1254 * of our target group.
1256 if (ac
->ac_allow_chain_relink
&&
1258 (ocfs2_block_group_reasonably_empty(bg
, *num_bits
))) {
1259 status
= ocfs2_relink_block_group(handle
, alloc_inode
,
1260 ac
->ac_bh
, group_bh
,
1261 prev_group_bh
, chain
);
1268 /* Ok, claim our bits now: set the info on dinode, chainlist
1269 * and then the group */
1270 status
= ocfs2_journal_access(handle
,
1273 OCFS2_JOURNAL_ACCESS_WRITE
);
1279 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1280 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(*num_bits
+ tmp_used
);
1281 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -(*num_bits
));
1283 status
= ocfs2_journal_dirty(handle
,
1290 status
= ocfs2_block_group_set_bits(handle
,
1301 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits
,
1302 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
1304 *bg_blkno
= le64_to_cpu(bg
->bg_blkno
);
1305 *bits_left
= le16_to_cpu(bg
->bg_free_bits_count
);
1310 brelse(prev_group_bh
);
1316 /* will give out up to bits_wanted contiguous bits. */
1317 static int ocfs2_claim_suballoc_bits(struct ocfs2_super
*osb
,
1318 struct ocfs2_alloc_context
*ac
,
1323 unsigned int *num_bits
,
1329 u64 hint_blkno
= ac
->ac_last_group
;
1330 struct ocfs2_chain_list
*cl
;
1331 struct ocfs2_dinode
*fe
;
1335 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1336 BUG_ON(bits_wanted
> (ac
->ac_bits_wanted
- ac
->ac_bits_given
));
1339 fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1340 if (!OCFS2_IS_VALID_DINODE(fe
)) {
1341 OCFS2_RO_ON_INVALID_DINODE(osb
->sb
, fe
);
1345 if (le32_to_cpu(fe
->id1
.bitmap1
.i_used
) >=
1346 le32_to_cpu(fe
->id1
.bitmap1
.i_total
)) {
1347 ocfs2_error(osb
->sb
, "Chain allocator dinode %llu has %u used "
1348 "bits but only %u total.",
1349 (unsigned long long)le64_to_cpu(fe
->i_blkno
),
1350 le32_to_cpu(fe
->id1
.bitmap1
.i_used
),
1351 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1357 /* Attempt to short-circuit the usual search mechanism
1358 * by jumping straight to the most recently used
1359 * allocation group. This helps us mantain some
1360 * contiguousness across allocations. */
1361 status
= ocfs2_search_one_group(ac
, handle
, bits_wanted
,
1362 min_bits
, bit_off
, num_bits
,
1363 hint_blkno
, &bits_left
);
1365 /* Be careful to update *bg_blkno here as the
1366 * caller is expecting it to be filled in, and
1367 * ocfs2_search_one_group() won't do that for
1369 *bg_blkno
= hint_blkno
;
1372 if (status
< 0 && status
!= -ENOSPC
) {
1378 cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1380 victim
= ocfs2_find_victim_chain(cl
);
1381 ac
->ac_chain
= victim
;
1382 ac
->ac_allow_chain_relink
= 1;
1384 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
, bit_off
,
1385 num_bits
, bg_blkno
, &bits_left
);
1388 if (status
< 0 && status
!= -ENOSPC
) {
1393 mlog(0, "Search of victim chain %u came up with nothing, "
1394 "trying all chains now.\n", victim
);
1396 /* If we didn't pick a good victim, then just default to
1397 * searching each chain in order. Don't allow chain relinking
1398 * because we only calculate enough journal credits for one
1399 * relink per alloc. */
1400 ac
->ac_allow_chain_relink
= 0;
1401 for (i
= 0; i
< le16_to_cpu(cl
->cl_next_free_rec
); i
++) {
1404 if (!cl
->cl_recs
[i
].c_free
)
1408 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
,
1409 bit_off
, num_bits
, bg_blkno
,
1413 if (status
< 0 && status
!= -ENOSPC
) {
1420 if (status
!= -ENOSPC
) {
1421 /* If the next search of this group is not likely to
1422 * yield a suitable extent, then we reset the last
1423 * group hint so as to not waste a disk read */
1424 if (bits_left
< min_bits
)
1425 ac
->ac_last_group
= 0;
1427 ac
->ac_last_group
= *bg_blkno
;
1435 int ocfs2_claim_metadata(struct ocfs2_super
*osb
,
1437 struct ocfs2_alloc_context
*ac
,
1439 u16
*suballoc_bit_start
,
1440 unsigned int *num_bits
,
1447 BUG_ON(ac
->ac_bits_wanted
< (ac
->ac_bits_given
+ bits_wanted
));
1448 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_META
);
1450 status
= ocfs2_claim_suballoc_bits(osb
,
1462 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1464 *blkno_start
= bg_blkno
+ (u64
) *suballoc_bit_start
;
1465 ac
->ac_bits_given
+= (*num_bits
);
1472 int ocfs2_claim_new_inode(struct ocfs2_super
*osb
,
1474 struct ocfs2_alloc_context
*ac
,
1479 unsigned int num_bits
;
1485 BUG_ON(ac
->ac_bits_given
!= 0);
1486 BUG_ON(ac
->ac_bits_wanted
!= 1);
1487 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_INODE
);
1489 status
= ocfs2_claim_suballoc_bits(osb
,
1501 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1503 BUG_ON(num_bits
!= 1);
1505 *fe_blkno
= bg_blkno
+ (u64
) (*suballoc_bit
);
1506 ac
->ac_bits_given
++;
1513 /* translate a group desc. blkno and it's bitmap offset into
1514 * disk cluster offset. */
1515 static inline u32
ocfs2_desc_bitmap_to_cluster_off(struct inode
*inode
,
1519 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1522 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1524 if (bg_blkno
!= osb
->first_cluster_group_blkno
)
1525 cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, bg_blkno
);
1526 cluster
+= (u32
) bg_bit_off
;
1530 /* given a cluster offset, calculate which block group it belongs to
1531 * and return that block offset. */
1532 u64
ocfs2_which_cluster_group(struct inode
*inode
, u32 cluster
)
1534 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1537 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1539 group_no
= cluster
/ osb
->bitmap_cpg
;
1541 return osb
->first_cluster_group_blkno
;
1542 return ocfs2_clusters_to_blocks(inode
->i_sb
,
1543 group_no
* osb
->bitmap_cpg
);
1546 /* given the block number of a cluster start, calculate which cluster
1547 * group and descriptor bitmap offset that corresponds to. */
1548 static inline void ocfs2_block_to_cluster_group(struct inode
*inode
,
1553 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1554 u32 data_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, data_blkno
);
1556 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1558 *bg_blkno
= ocfs2_which_cluster_group(inode
,
1561 if (*bg_blkno
== osb
->first_cluster_group_blkno
)
1562 *bg_bit_off
= (u16
) data_cluster
;
1564 *bg_bit_off
= (u16
) ocfs2_blocks_to_clusters(osb
->sb
,
1565 data_blkno
- *bg_blkno
);
1569 * min_bits - minimum contiguous chunk from this total allocation we
1570 * can handle. set to what we asked for originally for a full
1571 * contig. allocation, set to '1' to indicate we can deal with extents
1574 int __ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1576 struct ocfs2_alloc_context
*ac
,
1583 unsigned int bits_wanted
= max_clusters
;
1589 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1591 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_LOCAL
1592 && ac
->ac_which
!= OCFS2_AC_USE_MAIN
);
1594 if (ac
->ac_which
== OCFS2_AC_USE_LOCAL
) {
1595 status
= ocfs2_claim_local_alloc_bits(osb
,
1602 atomic_inc(&osb
->alloc_stats
.local_data
);
1604 if (min_clusters
> (osb
->bitmap_cpg
- 1)) {
1605 /* The only paths asking for contiguousness
1606 * should know about this already. */
1607 mlog(ML_ERROR
, "minimum allocation requested %u exceeds "
1608 "group bitmap size %u!\n", min_clusters
,
1613 /* clamp the current request down to a realistic size. */
1614 if (bits_wanted
> (osb
->bitmap_cpg
- 1))
1615 bits_wanted
= osb
->bitmap_cpg
- 1;
1617 status
= ocfs2_claim_suballoc_bits(osb
,
1627 ocfs2_desc_bitmap_to_cluster_off(ac
->ac_inode
,
1630 atomic_inc(&osb
->alloc_stats
.bitmap_data
);
1634 if (status
!= -ENOSPC
)
1639 ac
->ac_bits_given
+= *num_clusters
;
1646 int ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1648 struct ocfs2_alloc_context
*ac
,
1653 unsigned int bits_wanted
= ac
->ac_bits_wanted
- ac
->ac_bits_given
;
1655 return __ocfs2_claim_clusters(osb
, handle
, ac
, min_clusters
,
1656 bits_wanted
, cluster_start
, num_clusters
);
1659 static inline int ocfs2_block_group_clear_bits(handle_t
*handle
,
1660 struct inode
*alloc_inode
,
1661 struct ocfs2_group_desc
*bg
,
1662 struct buffer_head
*group_bh
,
1663 unsigned int bit_off
,
1664 unsigned int num_bits
)
1668 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
1669 struct ocfs2_group_desc
*undo_bg
= NULL
;
1673 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
1674 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
1679 mlog(0, "off = %u, num = %u\n", bit_off
, num_bits
);
1681 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1682 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
1684 status
= ocfs2_journal_access(handle
, alloc_inode
, group_bh
,
1691 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1692 undo_bg
= (struct ocfs2_group_desc
*) bh2jh(group_bh
)->b_committed_data
;
1696 ocfs2_clear_bit((bit_off
+ tmp
),
1697 (unsigned long *) bg
->bg_bitmap
);
1698 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1699 ocfs2_set_bit(bit_off
+ tmp
,
1700 (unsigned long *) undo_bg
->bg_bitmap
);
1702 le16_add_cpu(&bg
->bg_free_bits_count
, num_bits
);
1704 status
= ocfs2_journal_dirty(handle
, group_bh
);
1712 * expects the suballoc inode to already be locked.
1714 int ocfs2_free_suballoc_bits(handle_t
*handle
,
1715 struct inode
*alloc_inode
,
1716 struct buffer_head
*alloc_bh
,
1717 unsigned int start_bit
,
1723 struct ocfs2_super
*osb
= OCFS2_SB(alloc_inode
->i_sb
);
1724 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) alloc_bh
->b_data
;
1725 struct ocfs2_chain_list
*cl
= &fe
->id2
.i_chain
;
1726 struct buffer_head
*group_bh
= NULL
;
1727 struct ocfs2_group_desc
*group
;
1731 if (!OCFS2_IS_VALID_DINODE(fe
)) {
1732 OCFS2_RO_ON_INVALID_DINODE(alloc_inode
->i_sb
, fe
);
1736 BUG_ON((count
+ start_bit
) > ocfs2_bits_per_group(cl
));
1738 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1739 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
, count
,
1740 (unsigned long long)bg_blkno
, start_bit
);
1742 status
= ocfs2_read_block(osb
, bg_blkno
, &group_bh
, OCFS2_BH_CACHED
,
1749 group
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1750 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, group
);
1755 BUG_ON((count
+ start_bit
) > le16_to_cpu(group
->bg_bits
));
1757 status
= ocfs2_block_group_clear_bits(handle
, alloc_inode
,
1765 status
= ocfs2_journal_access(handle
, alloc_inode
, alloc_bh
,
1766 OCFS2_JOURNAL_ACCESS_WRITE
);
1772 le32_add_cpu(&cl
->cl_recs
[le16_to_cpu(group
->bg_chain
)].c_free
,
1774 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1775 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(tmp_used
- count
);
1777 status
= ocfs2_journal_dirty(handle
, alloc_bh
);
1791 int ocfs2_free_dinode(handle_t
*handle
,
1792 struct inode
*inode_alloc_inode
,
1793 struct buffer_head
*inode_alloc_bh
,
1794 struct ocfs2_dinode
*di
)
1796 u64 blk
= le64_to_cpu(di
->i_blkno
);
1797 u16 bit
= le16_to_cpu(di
->i_suballoc_bit
);
1798 u64 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
1800 return ocfs2_free_suballoc_bits(handle
, inode_alloc_inode
,
1801 inode_alloc_bh
, bit
, bg_blkno
, 1);
1804 int ocfs2_free_clusters(handle_t
*handle
,
1805 struct inode
*bitmap_inode
,
1806 struct buffer_head
*bitmap_bh
,
1808 unsigned int num_clusters
)
1813 struct ocfs2_dinode
*fe
;
1815 /* You can't ever have a contiguous set of clusters
1816 * bigger than a block group bitmap so we never have to worry
1817 * about looping on them. */
1821 /* This is expensive. We can safely remove once this stuff has
1822 * gotten tested really well. */
1823 BUG_ON(start_blk
!= ocfs2_clusters_to_blocks(bitmap_inode
->i_sb
, ocfs2_blocks_to_clusters(bitmap_inode
->i_sb
, start_blk
)));
1825 fe
= (struct ocfs2_dinode
*) bitmap_bh
->b_data
;
1827 ocfs2_block_to_cluster_group(bitmap_inode
, start_blk
, &bg_blkno
,
1830 mlog(0, "want to free %u clusters starting at block %llu\n",
1831 num_clusters
, (unsigned long long)start_blk
);
1832 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1833 (unsigned long long)bg_blkno
, bg_start_bit
);
1835 status
= ocfs2_free_suballoc_bits(handle
, bitmap_inode
, bitmap_bh
,
1836 bg_start_bit
, bg_blkno
,
1843 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode
->i_sb
),
1851 static inline void ocfs2_debug_bg(struct ocfs2_group_desc
*bg
)
1853 printk("Block Group:\n");
1854 printk("bg_signature: %s\n", bg
->bg_signature
);
1855 printk("bg_size: %u\n", bg
->bg_size
);
1856 printk("bg_bits: %u\n", bg
->bg_bits
);
1857 printk("bg_free_bits_count: %u\n", bg
->bg_free_bits_count
);
1858 printk("bg_chain: %u\n", bg
->bg_chain
);
1859 printk("bg_generation: %u\n", le32_to_cpu(bg
->bg_generation
));
1860 printk("bg_next_group: %llu\n",
1861 (unsigned long long)bg
->bg_next_group
);
1862 printk("bg_parent_dinode: %llu\n",
1863 (unsigned long long)bg
->bg_parent_dinode
);
1864 printk("bg_blkno: %llu\n",
1865 (unsigned long long)bg
->bg_blkno
);
1868 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode
*fe
)
1872 printk("Suballoc Inode %llu:\n", (unsigned long long)fe
->i_blkno
);
1873 printk("i_signature: %s\n", fe
->i_signature
);
1874 printk("i_size: %llu\n",
1875 (unsigned long long)fe
->i_size
);
1876 printk("i_clusters: %u\n", fe
->i_clusters
);
1877 printk("i_generation: %u\n",
1878 le32_to_cpu(fe
->i_generation
));
1879 printk("id1.bitmap1.i_used: %u\n",
1880 le32_to_cpu(fe
->id1
.bitmap1
.i_used
));
1881 printk("id1.bitmap1.i_total: %u\n",
1882 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1883 printk("id2.i_chain.cl_cpg: %u\n", fe
->id2
.i_chain
.cl_cpg
);
1884 printk("id2.i_chain.cl_bpc: %u\n", fe
->id2
.i_chain
.cl_bpc
);
1885 printk("id2.i_chain.cl_count: %u\n", fe
->id2
.i_chain
.cl_count
);
1886 printk("id2.i_chain.cl_next_free_rec: %u\n",
1887 fe
->id2
.i_chain
.cl_next_free_rec
);
1888 for(i
= 0; i
< fe
->id2
.i_chain
.cl_next_free_rec
; i
++) {
1889 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i
,
1890 fe
->id2
.i_chain
.cl_recs
[i
].c_free
);
1891 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i
,
1892 fe
->id2
.i_chain
.cl_recs
[i
].c_total
);
1893 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i
,
1894 (unsigned long long)fe
->id2
.i_chain
.cl_recs
[i
].c_blkno
);