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
,
68 static int ocfs2_cluster_group_search(struct inode
*inode
,
69 struct buffer_head
*group_bh
,
70 u32 bits_wanted
, u32 min_bits
,
72 u16
*bit_off
, u16
*bits_found
);
73 static int ocfs2_block_group_search(struct inode
*inode
,
74 struct buffer_head
*group_bh
,
75 u32 bits_wanted
, u32 min_bits
,
77 u16
*bit_off
, u16
*bits_found
);
78 static int ocfs2_claim_suballoc_bits(struct ocfs2_super
*osb
,
79 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(handle_t
*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(handle_t
*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(handle_t
*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 inline u32
ocfs2_desc_bitmap_to_cluster_off(struct inode
*inode
,
112 static inline void ocfs2_block_to_cluster_group(struct inode
*inode
,
116 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super
*osb
,
117 u32 bits_wanted
, u64 max_block
,
118 struct ocfs2_alloc_context
**ac
);
120 void ocfs2_free_ac_resource(struct ocfs2_alloc_context
*ac
)
122 struct inode
*inode
= ac
->ac_inode
;
125 if (ac
->ac_which
!= OCFS2_AC_USE_LOCAL
)
126 ocfs2_inode_unlock(inode
, 1);
128 mutex_unlock(&inode
->i_mutex
);
137 void ocfs2_free_alloc_context(struct ocfs2_alloc_context
*ac
)
139 ocfs2_free_ac_resource(ac
);
143 static u32
ocfs2_bits_per_group(struct ocfs2_chain_list
*cl
)
145 return (u32
)le16_to_cpu(cl
->cl_cpg
) * (u32
)le16_to_cpu(cl
->cl_bpc
);
148 /* somewhat more expensive than our other checks, so use sparingly. */
149 int ocfs2_validate_group_descriptor(struct super_block
*sb
,
150 struct ocfs2_dinode
*di
,
151 struct ocfs2_group_desc
*gd
,
154 unsigned int max_bits
;
156 #define do_error(fmt, ...) \
159 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
161 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
164 if (!OCFS2_IS_VALID_GROUP_DESC(gd
)) {
165 do_error("Group Descriptor #%llu has bad signature %.*s",
166 (unsigned long long)le64_to_cpu(gd
->bg_blkno
), 7,
171 if (di
->i_blkno
!= gd
->bg_parent_dinode
) {
172 do_error("Group descriptor # %llu has bad parent "
173 "pointer (%llu, expected %llu)",
174 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
175 (unsigned long long)le64_to_cpu(gd
->bg_parent_dinode
),
176 (unsigned long long)le64_to_cpu(di
->i_blkno
));
180 max_bits
= le16_to_cpu(di
->id2
.i_chain
.cl_cpg
) * le16_to_cpu(di
->id2
.i_chain
.cl_bpc
);
181 if (le16_to_cpu(gd
->bg_bits
) > max_bits
) {
182 do_error("Group descriptor # %llu has bit count of %u",
183 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
184 le16_to_cpu(gd
->bg_bits
));
188 if (le16_to_cpu(gd
->bg_chain
) >=
189 le16_to_cpu(di
->id2
.i_chain
.cl_next_free_rec
)) {
190 do_error("Group descriptor # %llu has bad chain %u",
191 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
192 le16_to_cpu(gd
->bg_chain
));
196 if (le16_to_cpu(gd
->bg_free_bits_count
) > le16_to_cpu(gd
->bg_bits
)) {
197 do_error("Group descriptor # %llu has bit count %u but "
198 "claims that %u are free",
199 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
200 le16_to_cpu(gd
->bg_bits
),
201 le16_to_cpu(gd
->bg_free_bits_count
));
205 if (le16_to_cpu(gd
->bg_bits
) > (8 * le16_to_cpu(gd
->bg_size
))) {
206 do_error("Group descriptor # %llu has bit count %u but "
207 "max bitmap bits of %u",
208 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
209 le16_to_cpu(gd
->bg_bits
),
210 8 * le16_to_cpu(gd
->bg_size
));
218 static int ocfs2_block_group_fill(handle_t
*handle
,
219 struct inode
*alloc_inode
,
220 struct buffer_head
*bg_bh
,
223 struct ocfs2_chain_list
*cl
)
226 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
227 struct super_block
* sb
= alloc_inode
->i_sb
;
231 if (((unsigned long long) bg_bh
->b_blocknr
) != group_blkno
) {
232 ocfs2_error(alloc_inode
->i_sb
, "group block (%llu) != "
234 (unsigned long long)group_blkno
,
235 (unsigned long long) bg_bh
->b_blocknr
);
240 status
= ocfs2_journal_access(handle
,
243 OCFS2_JOURNAL_ACCESS_CREATE
);
249 memset(bg
, 0, sb
->s_blocksize
);
250 strcpy(bg
->bg_signature
, OCFS2_GROUP_DESC_SIGNATURE
);
251 bg
->bg_generation
= cpu_to_le32(OCFS2_SB(sb
)->fs_generation
);
252 bg
->bg_size
= cpu_to_le16(ocfs2_group_bitmap_size(sb
));
253 bg
->bg_bits
= cpu_to_le16(ocfs2_bits_per_group(cl
));
254 bg
->bg_chain
= cpu_to_le16(my_chain
);
255 bg
->bg_next_group
= cl
->cl_recs
[my_chain
].c_blkno
;
256 bg
->bg_parent_dinode
= cpu_to_le64(OCFS2_I(alloc_inode
)->ip_blkno
);
257 bg
->bg_blkno
= cpu_to_le64(group_blkno
);
258 /* set the 1st bit in the bitmap to account for the descriptor block */
259 ocfs2_set_bit(0, (unsigned long *)bg
->bg_bitmap
);
260 bg
->bg_free_bits_count
= cpu_to_le16(le16_to_cpu(bg
->bg_bits
) - 1);
262 status
= ocfs2_journal_dirty(handle
, bg_bh
);
266 /* There is no need to zero out or otherwise initialize the
267 * other blocks in a group - All valid FS metadata in a block
268 * group stores the superblock fs_generation value at
269 * allocation time. */
276 static inline u16
ocfs2_find_smallest_chain(struct ocfs2_chain_list
*cl
)
281 while (curr
< le16_to_cpu(cl
->cl_count
)) {
282 if (le32_to_cpu(cl
->cl_recs
[best
].c_total
) >
283 le32_to_cpu(cl
->cl_recs
[curr
].c_total
))
291 * We expect the block group allocator to already be locked.
293 static int ocfs2_block_group_alloc(struct ocfs2_super
*osb
,
294 struct inode
*alloc_inode
,
295 struct buffer_head
*bh
,
299 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) bh
->b_data
;
300 struct ocfs2_chain_list
*cl
;
301 struct ocfs2_alloc_context
*ac
= NULL
;
302 handle_t
*handle
= NULL
;
303 u32 bit_off
, num_bits
;
306 struct buffer_head
*bg_bh
= NULL
;
307 struct ocfs2_group_desc
*bg
;
309 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode
));
313 cl
= &fe
->id2
.i_chain
;
314 status
= ocfs2_reserve_clusters_with_limit(osb
,
315 le16_to_cpu(cl
->cl_cpg
),
318 if (status
!= -ENOSPC
)
323 credits
= ocfs2_calc_group_alloc_credits(osb
->sb
,
324 le16_to_cpu(cl
->cl_cpg
));
325 handle
= ocfs2_start_trans(osb
, credits
);
326 if (IS_ERR(handle
)) {
327 status
= PTR_ERR(handle
);
333 status
= ocfs2_claim_clusters(osb
,
336 le16_to_cpu(cl
->cl_cpg
),
340 if (status
!= -ENOSPC
)
345 alloc_rec
= ocfs2_find_smallest_chain(cl
);
347 /* setup the group */
348 bg_blkno
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
349 mlog(0, "new descriptor, record %u, at block %llu\n",
350 alloc_rec
, (unsigned long long)bg_blkno
);
352 bg_bh
= sb_getblk(osb
->sb
, bg_blkno
);
358 ocfs2_set_new_buffer_uptodate(alloc_inode
, bg_bh
);
360 status
= ocfs2_block_group_fill(handle
,
371 bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
373 status
= ocfs2_journal_access(handle
, alloc_inode
,
374 bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
380 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_free
,
381 le16_to_cpu(bg
->bg_free_bits_count
));
382 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_total
, le16_to_cpu(bg
->bg_bits
));
383 cl
->cl_recs
[alloc_rec
].c_blkno
= cpu_to_le64(bg_blkno
);
384 if (le16_to_cpu(cl
->cl_next_free_rec
) < le16_to_cpu(cl
->cl_count
))
385 le16_add_cpu(&cl
->cl_next_free_rec
, 1);
387 le32_add_cpu(&fe
->id1
.bitmap1
.i_used
, le16_to_cpu(bg
->bg_bits
) -
388 le16_to_cpu(bg
->bg_free_bits_count
));
389 le32_add_cpu(&fe
->id1
.bitmap1
.i_total
, le16_to_cpu(bg
->bg_bits
));
390 le32_add_cpu(&fe
->i_clusters
, le16_to_cpu(cl
->cl_cpg
));
392 status
= ocfs2_journal_dirty(handle
, bh
);
398 spin_lock(&OCFS2_I(alloc_inode
)->ip_lock
);
399 OCFS2_I(alloc_inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
);
400 fe
->i_size
= cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode
->i_sb
,
401 le32_to_cpu(fe
->i_clusters
)));
402 spin_unlock(&OCFS2_I(alloc_inode
)->ip_lock
);
403 i_size_write(alloc_inode
, le64_to_cpu(fe
->i_size
));
404 alloc_inode
->i_blocks
= ocfs2_inode_sector_count(alloc_inode
);
409 ocfs2_commit_trans(osb
, handle
);
412 ocfs2_free_alloc_context(ac
);
420 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super
*osb
,
421 struct ocfs2_alloc_context
*ac
,
427 u32 bits_wanted
= ac
->ac_bits_wanted
;
428 struct inode
*alloc_inode
;
429 struct buffer_head
*bh
= NULL
;
430 struct ocfs2_dinode
*fe
;
435 alloc_inode
= ocfs2_get_system_file_inode(osb
, type
, slot
);
441 mutex_lock(&alloc_inode
->i_mutex
);
443 status
= ocfs2_inode_lock(alloc_inode
, &bh
, 1);
445 mutex_unlock(&alloc_inode
->i_mutex
);
452 ac
->ac_inode
= alloc_inode
;
453 ac
->ac_alloc_slot
= slot
;
455 fe
= (struct ocfs2_dinode
*) bh
->b_data
;
457 /* The bh was validated by the inode read inside
458 * ocfs2_inode_lock(). Any corruption is a code bug. */
459 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
461 if (!(fe
->i_flags
& cpu_to_le32(OCFS2_CHAIN_FL
))) {
462 ocfs2_error(alloc_inode
->i_sb
, "Invalid chain allocator %llu",
463 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
468 free_bits
= le32_to_cpu(fe
->id1
.bitmap1
.i_total
) -
469 le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
471 if (bits_wanted
> free_bits
) {
472 /* cluster bitmap never grows */
473 if (ocfs2_is_cluster_bitmap(alloc_inode
)) {
474 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
475 bits_wanted
, free_bits
);
480 if (alloc_new_group
!= ALLOC_NEW_GROUP
) {
481 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
482 "and we don't alloc a new group for it.\n",
483 slot
, bits_wanted
, free_bits
);
488 status
= ocfs2_block_group_alloc(osb
, alloc_inode
, bh
,
491 if (status
!= -ENOSPC
)
495 atomic_inc(&osb
->alloc_stats
.bg_extends
);
497 /* You should never ask for this much metadata */
499 (le32_to_cpu(fe
->id1
.bitmap1
.i_total
)
500 - le32_to_cpu(fe
->id1
.bitmap1
.i_used
)));
512 int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super
*osb
,
514 struct ocfs2_alloc_context
**ac
)
519 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
526 (*ac
)->ac_bits_wanted
= blocks
;
527 (*ac
)->ac_which
= OCFS2_AC_USE_META
;
528 slot
= osb
->slot_num
;
529 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
531 status
= ocfs2_reserve_suballoc_bits(osb
, (*ac
),
532 EXTENT_ALLOC_SYSTEM_INODE
,
533 slot
, ALLOC_NEW_GROUP
);
535 if (status
!= -ENOSPC
)
542 if ((status
< 0) && *ac
) {
543 ocfs2_free_alloc_context(*ac
);
551 int ocfs2_reserve_new_metadata(struct ocfs2_super
*osb
,
552 struct ocfs2_extent_list
*root_el
,
553 struct ocfs2_alloc_context
**ac
)
555 return ocfs2_reserve_new_metadata_blocks(osb
,
556 ocfs2_extend_meta_needed(root_el
),
560 static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super
*osb
,
561 struct ocfs2_alloc_context
*ac
)
563 int i
, status
= -ENOSPC
;
564 s16 slot
= ocfs2_get_inode_steal_slot(osb
);
566 /* Start to steal inodes from the first slot after ours. */
567 if (slot
== OCFS2_INVALID_SLOT
)
568 slot
= osb
->slot_num
+ 1;
570 for (i
= 0; i
< osb
->max_slots
; i
++, slot
++) {
571 if (slot
== osb
->max_slots
)
574 if (slot
== osb
->slot_num
)
577 status
= ocfs2_reserve_suballoc_bits(osb
, ac
,
578 INODE_ALLOC_SYSTEM_INODE
,
579 slot
, NOT_ALLOC_NEW_GROUP
);
581 ocfs2_set_inode_steal_slot(osb
, slot
);
585 ocfs2_free_ac_resource(ac
);
591 int ocfs2_reserve_new_inode(struct ocfs2_super
*osb
,
592 struct ocfs2_alloc_context
**ac
)
595 s16 slot
= ocfs2_get_inode_steal_slot(osb
);
597 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
604 (*ac
)->ac_bits_wanted
= 1;
605 (*ac
)->ac_which
= OCFS2_AC_USE_INODE
;
607 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
610 * stat(2) can't handle i_ino > 32bits, so we tell the
611 * lower levels not to allocate us a block group past that
612 * limit. The 'inode64' mount option avoids this behavior.
614 if (!(osb
->s_mount_opt
& OCFS2_MOUNT_INODE64
))
615 (*ac
)->ac_max_block
= (u32
)~0U;
618 * slot is set when we successfully steal inode from other nodes.
619 * It is reset in 3 places:
620 * 1. when we flush the truncate log
621 * 2. when we complete local alloc recovery.
622 * 3. when we successfully allocate from our own slot.
623 * After it is set, we will go on stealing inodes until we find the
624 * need to check our slots to see whether there is some space for us.
626 if (slot
!= OCFS2_INVALID_SLOT
&&
627 atomic_read(&osb
->s_num_inodes_stolen
) < OCFS2_MAX_INODES_TO_STEAL
)
630 atomic_set(&osb
->s_num_inodes_stolen
, 0);
631 status
= ocfs2_reserve_suballoc_bits(osb
, *ac
,
632 INODE_ALLOC_SYSTEM_INODE
,
633 osb
->slot_num
, ALLOC_NEW_GROUP
);
638 * Some inodes must be freed by us, so try to allocate
639 * from our own next time.
641 if (slot
!= OCFS2_INVALID_SLOT
)
642 ocfs2_init_inode_steal_slot(osb
);
644 } else if (status
< 0 && status
!= -ENOSPC
) {
649 ocfs2_free_ac_resource(*ac
);
652 status
= ocfs2_steal_inode_from_other_nodes(osb
, *ac
);
653 atomic_inc(&osb
->s_num_inodes_stolen
);
655 if (status
!= -ENOSPC
)
662 if ((status
< 0) && *ac
) {
663 ocfs2_free_alloc_context(*ac
);
671 /* local alloc code has to do the same thing, so rather than do this
673 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super
*osb
,
674 struct ocfs2_alloc_context
*ac
)
678 ac
->ac_which
= OCFS2_AC_USE_MAIN
;
679 ac
->ac_group_search
= ocfs2_cluster_group_search
;
681 status
= ocfs2_reserve_suballoc_bits(osb
, ac
,
682 GLOBAL_BITMAP_SYSTEM_INODE
,
685 if (status
< 0 && status
!= -ENOSPC
) {
694 /* Callers don't need to care which bitmap (local alloc or main) to
695 * use so we figure it out for them, but unfortunately this clutters
697 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super
*osb
,
698 u32 bits_wanted
, u64 max_block
,
699 struct ocfs2_alloc_context
**ac
)
705 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
712 (*ac
)->ac_bits_wanted
= bits_wanted
;
713 (*ac
)->ac_max_block
= max_block
;
716 if (ocfs2_alloc_should_use_local(osb
, bits_wanted
)) {
717 status
= ocfs2_reserve_local_alloc_bits(osb
,
720 if (status
== -EFBIG
) {
721 /* The local alloc window is outside ac_max_block.
722 * use the main bitmap. */
724 } else if ((status
< 0) && (status
!= -ENOSPC
)) {
730 if (status
== -ENOSPC
) {
731 status
= ocfs2_reserve_cluster_bitmap_bits(osb
, *ac
);
733 if (status
!= -ENOSPC
)
741 if ((status
< 0) && *ac
) {
742 ocfs2_free_alloc_context(*ac
);
750 int ocfs2_reserve_clusters(struct ocfs2_super
*osb
,
752 struct ocfs2_alloc_context
**ac
)
754 return ocfs2_reserve_clusters_with_limit(osb
, bits_wanted
, 0, ac
);
758 * More or less lifted from ext3. I'll leave their description below:
760 * "For ext3 allocations, we must not reuse any blocks which are
761 * allocated in the bitmap buffer's "last committed data" copy. This
762 * prevents deletes from freeing up the page for reuse until we have
763 * committed the delete transaction.
765 * If we didn't do this, then deleting something and reallocating it as
766 * data would allow the old block to be overwritten before the
767 * transaction committed (because we force data to disk before commit).
768 * This would lead to corruption if we crashed between overwriting the
769 * data and committing the delete.
771 * @@@ We may want to make this allocation behaviour conditional on
772 * data-writes at some point, and disable it for metadata allocations or
775 * Note: OCFS2 already does this differently for metadata vs data
776 * allocations, as those bitmaps are separate and undo access is never
777 * called on a metadata group descriptor.
779 static int ocfs2_test_bg_bit_allocatable(struct buffer_head
*bg_bh
,
782 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
784 if (ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
))
786 if (!buffer_jbd(bg_bh
) || !bh2jh(bg_bh
)->b_committed_data
)
789 bg
= (struct ocfs2_group_desc
*) bh2jh(bg_bh
)->b_committed_data
;
790 return !ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
);
793 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super
*osb
,
794 struct buffer_head
*bg_bh
,
795 unsigned int bits_wanted
,
796 unsigned int total_bits
,
801 u16 best_offset
, best_size
;
802 int offset
, start
, found
, status
= 0;
803 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
805 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
806 OCFS2_RO_ON_INVALID_GROUP_DESC(osb
->sb
, bg
);
810 found
= start
= best_offset
= best_size
= 0;
811 bitmap
= bg
->bg_bitmap
;
813 while((offset
= ocfs2_find_next_zero_bit(bitmap
, total_bits
, start
)) != -1) {
814 if (offset
== total_bits
)
817 if (!ocfs2_test_bg_bit_allocatable(bg_bh
, offset
)) {
818 /* We found a zero, but we can't use it as it
819 * hasn't been put to disk yet! */
822 } else if (offset
== start
) {
823 /* we found a zero */
825 /* move start to the next bit to test */
828 /* got a zero after some ones */
832 if (found
> best_size
) {
834 best_offset
= start
- found
;
836 /* we got everything we needed */
837 if (found
== bits_wanted
) {
838 /* mlog(0, "Found it all!\n"); */
843 /* XXX: I think the first clause is equivalent to the second
845 if (found
== bits_wanted
) {
846 *bit_off
= start
- found
;
848 } else if (best_size
) {
849 *bit_off
= best_offset
;
850 *bits_found
= best_size
;
853 /* No error log here -- see the comment above
854 * ocfs2_test_bg_bit_allocatable */
860 static inline int ocfs2_block_group_set_bits(handle_t
*handle
,
861 struct inode
*alloc_inode
,
862 struct ocfs2_group_desc
*bg
,
863 struct buffer_head
*group_bh
,
864 unsigned int bit_off
,
865 unsigned int num_bits
)
868 void *bitmap
= bg
->bg_bitmap
;
869 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
873 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
874 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
878 BUG_ON(le16_to_cpu(bg
->bg_free_bits_count
) < num_bits
);
880 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off
,
883 if (ocfs2_is_cluster_bitmap(alloc_inode
))
884 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
886 status
= ocfs2_journal_access(handle
,
895 le16_add_cpu(&bg
->bg_free_bits_count
, -num_bits
);
898 ocfs2_set_bit(bit_off
++, bitmap
);
900 status
= ocfs2_journal_dirty(handle
,
912 /* find the one with the most empty bits */
913 static inline u16
ocfs2_find_victim_chain(struct ocfs2_chain_list
*cl
)
917 BUG_ON(!cl
->cl_next_free_rec
);
920 while (curr
< le16_to_cpu(cl
->cl_next_free_rec
)) {
921 if (le32_to_cpu(cl
->cl_recs
[curr
].c_free
) >
922 le32_to_cpu(cl
->cl_recs
[best
].c_free
))
927 BUG_ON(best
>= le16_to_cpu(cl
->cl_next_free_rec
));
931 static int ocfs2_relink_block_group(handle_t
*handle
,
932 struct inode
*alloc_inode
,
933 struct buffer_head
*fe_bh
,
934 struct buffer_head
*bg_bh
,
935 struct buffer_head
*prev_bg_bh
,
939 /* there is a really tiny chance the journal calls could fail,
940 * but we wouldn't want inconsistent blocks in *any* case. */
941 u64 fe_ptr
, bg_ptr
, prev_bg_ptr
;
942 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
943 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
944 struct ocfs2_group_desc
*prev_bg
= (struct ocfs2_group_desc
*) prev_bg_bh
->b_data
;
946 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
947 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
951 if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg
)) {
952 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, prev_bg
);
957 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
958 (unsigned long long)le64_to_cpu(fe
->i_blkno
), chain
,
959 (unsigned long long)le64_to_cpu(bg
->bg_blkno
),
960 (unsigned long long)le64_to_cpu(prev_bg
->bg_blkno
));
962 fe_ptr
= le64_to_cpu(fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
);
963 bg_ptr
= le64_to_cpu(bg
->bg_next_group
);
964 prev_bg_ptr
= le64_to_cpu(prev_bg
->bg_next_group
);
966 status
= ocfs2_journal_access(handle
, alloc_inode
, prev_bg_bh
,
967 OCFS2_JOURNAL_ACCESS_WRITE
);
973 prev_bg
->bg_next_group
= bg
->bg_next_group
;
975 status
= ocfs2_journal_dirty(handle
, prev_bg_bh
);
981 status
= ocfs2_journal_access(handle
, alloc_inode
, bg_bh
,
982 OCFS2_JOURNAL_ACCESS_WRITE
);
988 bg
->bg_next_group
= fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
;
990 status
= ocfs2_journal_dirty(handle
, bg_bh
);
996 status
= ocfs2_journal_access(handle
, alloc_inode
, fe_bh
,
997 OCFS2_JOURNAL_ACCESS_WRITE
);
1003 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= bg
->bg_blkno
;
1005 status
= ocfs2_journal_dirty(handle
, fe_bh
);
1014 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= cpu_to_le64(fe_ptr
);
1015 bg
->bg_next_group
= cpu_to_le64(bg_ptr
);
1016 prev_bg
->bg_next_group
= cpu_to_le64(prev_bg_ptr
);
1023 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc
*bg
,
1026 return le16_to_cpu(bg
->bg_free_bits_count
) > wanted
;
1029 /* return 0 on success, -ENOSPC to keep searching and any other < 0
1030 * value on error. */
1031 static int ocfs2_cluster_group_search(struct inode
*inode
,
1032 struct buffer_head
*group_bh
,
1033 u32 bits_wanted
, u32 min_bits
,
1035 u16
*bit_off
, u16
*bits_found
)
1037 int search
= -ENOSPC
;
1040 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1041 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1042 u16 tmp_off
, tmp_found
;
1043 unsigned int max_bits
, gd_cluster_off
;
1045 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1047 if (gd
->bg_free_bits_count
) {
1048 max_bits
= le16_to_cpu(gd
->bg_bits
);
1050 /* Tail groups in cluster bitmaps which aren't cpg
1051 * aligned are prone to partial extention by a failed
1052 * fs resize. If the file system resize never got to
1053 * update the dinode cluster count, then we don't want
1054 * to trust any clusters past it, regardless of what
1055 * the group descriptor says. */
1056 gd_cluster_off
= ocfs2_blocks_to_clusters(inode
->i_sb
,
1057 le64_to_cpu(gd
->bg_blkno
));
1058 if ((gd_cluster_off
+ max_bits
) >
1059 OCFS2_I(inode
)->ip_clusters
) {
1060 max_bits
= OCFS2_I(inode
)->ip_clusters
- gd_cluster_off
;
1061 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1062 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
1063 le16_to_cpu(gd
->bg_bits
),
1064 OCFS2_I(inode
)->ip_clusters
, max_bits
);
1067 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1068 group_bh
, bits_wanted
,
1070 &tmp_off
, &tmp_found
);
1075 blkoff
= ocfs2_clusters_to_blocks(inode
->i_sb
,
1077 tmp_off
+ tmp_found
);
1078 mlog(0, "Checking %llu against %llu\n",
1079 (unsigned long long)blkoff
,
1080 (unsigned long long)max_block
);
1081 if (blkoff
> max_block
)
1085 /* ocfs2_block_group_find_clear_bits() might
1086 * return success, but we still want to return
1087 * -ENOSPC unless it found the minimum number
1089 if (min_bits
<= tmp_found
) {
1091 *bits_found
= tmp_found
;
1092 search
= 0; /* success */
1093 } else if (tmp_found
) {
1095 * Don't show bits which we'll be returning
1096 * for allocation to the local alloc bitmap.
1098 ocfs2_local_alloc_seen_free_bits(osb
, tmp_found
);
1105 static int ocfs2_block_group_search(struct inode
*inode
,
1106 struct buffer_head
*group_bh
,
1107 u32 bits_wanted
, u32 min_bits
,
1109 u16
*bit_off
, u16
*bits_found
)
1113 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1115 BUG_ON(min_bits
!= 1);
1116 BUG_ON(ocfs2_is_cluster_bitmap(inode
));
1118 if (bg
->bg_free_bits_count
) {
1119 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1120 group_bh
, bits_wanted
,
1121 le16_to_cpu(bg
->bg_bits
),
1122 bit_off
, bits_found
);
1123 if (!ret
&& max_block
) {
1124 blkoff
= le64_to_cpu(bg
->bg_blkno
) + *bit_off
+
1126 mlog(0, "Checking %llu against %llu\n",
1127 (unsigned long long)blkoff
,
1128 (unsigned long long)max_block
);
1129 if (blkoff
> max_block
)
1137 static int ocfs2_alloc_dinode_update_counts(struct inode
*inode
,
1139 struct buffer_head
*di_bh
,
1145 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*) di_bh
->b_data
;
1146 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &di
->id2
.i_chain
;
1148 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
1149 OCFS2_JOURNAL_ACCESS_WRITE
);
1155 tmp_used
= le32_to_cpu(di
->id1
.bitmap1
.i_used
);
1156 di
->id1
.bitmap1
.i_used
= cpu_to_le32(num_bits
+ tmp_used
);
1157 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -num_bits
);
1159 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1167 static int ocfs2_search_one_group(struct ocfs2_alloc_context
*ac
,
1172 unsigned int *num_bits
,
1178 struct buffer_head
*group_bh
= NULL
;
1179 struct ocfs2_group_desc
*gd
;
1180 struct inode
*alloc_inode
= ac
->ac_inode
;
1182 ret
= ocfs2_read_block(alloc_inode
, gd_blkno
, &group_bh
);
1188 gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1189 if (!OCFS2_IS_VALID_GROUP_DESC(gd
)) {
1190 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, gd
);
1195 ret
= ac
->ac_group_search(alloc_inode
, group_bh
, bits_wanted
, min_bits
,
1196 ac
->ac_max_block
, bit_off
, &found
);
1205 ret
= ocfs2_alloc_dinode_update_counts(alloc_inode
, handle
, ac
->ac_bh
,
1207 le16_to_cpu(gd
->bg_chain
));
1213 ret
= ocfs2_block_group_set_bits(handle
, alloc_inode
, gd
, group_bh
,
1214 *bit_off
, *num_bits
);
1218 *bits_left
= le16_to_cpu(gd
->bg_free_bits_count
);
1226 static int ocfs2_search_chain(struct ocfs2_alloc_context
*ac
,
1231 unsigned int *num_bits
,
1236 u16 chain
, tmp_bits
;
1239 struct inode
*alloc_inode
= ac
->ac_inode
;
1240 struct buffer_head
*group_bh
= NULL
;
1241 struct buffer_head
*prev_group_bh
= NULL
;
1242 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1243 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1244 struct ocfs2_group_desc
*bg
;
1246 chain
= ac
->ac_chain
;
1247 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1249 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
);
1251 status
= ocfs2_read_block(alloc_inode
,
1252 le64_to_cpu(cl
->cl_recs
[chain
].c_blkno
),
1258 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1259 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, bg
);
1266 /* for now, the chain search is a bit simplistic. We just use
1267 * the 1st group with any empty bits. */
1268 while ((status
= ac
->ac_group_search(alloc_inode
, group_bh
,
1269 bits_wanted
, min_bits
,
1270 ac
->ac_max_block
, bit_off
,
1271 &tmp_bits
)) == -ENOSPC
) {
1272 if (!bg
->bg_next_group
)
1275 brelse(prev_group_bh
);
1276 prev_group_bh
= NULL
;
1278 next_group
= le64_to_cpu(bg
->bg_next_group
);
1279 prev_group_bh
= group_bh
;
1281 status
= ocfs2_read_block(alloc_inode
,
1282 next_group
, &group_bh
);
1287 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1288 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, bg
);
1295 if (status
!= -ENOSPC
)
1300 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1301 tmp_bits
, (unsigned long long)le64_to_cpu(bg
->bg_blkno
));
1303 *num_bits
= tmp_bits
;
1305 BUG_ON(*num_bits
== 0);
1308 * Keep track of previous block descriptor read. When
1309 * we find a target, if we have read more than X
1310 * number of descriptors, and the target is reasonably
1311 * empty, relink him to top of his chain.
1313 * We've read 0 extra blocks and only send one more to
1314 * the transaction, yet the next guy to search has a
1317 * Do this *after* figuring out how many bits we're taking out
1318 * of our target group.
1320 if (ac
->ac_allow_chain_relink
&&
1322 (ocfs2_block_group_reasonably_empty(bg
, *num_bits
))) {
1323 status
= ocfs2_relink_block_group(handle
, alloc_inode
,
1324 ac
->ac_bh
, group_bh
,
1325 prev_group_bh
, chain
);
1332 /* Ok, claim our bits now: set the info on dinode, chainlist
1333 * and then the group */
1334 status
= ocfs2_journal_access(handle
,
1337 OCFS2_JOURNAL_ACCESS_WRITE
);
1343 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1344 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(*num_bits
+ tmp_used
);
1345 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -(*num_bits
));
1347 status
= ocfs2_journal_dirty(handle
,
1354 status
= ocfs2_block_group_set_bits(handle
,
1365 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits
,
1366 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
1368 *bg_blkno
= le64_to_cpu(bg
->bg_blkno
);
1369 *bits_left
= le16_to_cpu(bg
->bg_free_bits_count
);
1372 brelse(prev_group_bh
);
1378 /* will give out up to bits_wanted contiguous bits. */
1379 static int ocfs2_claim_suballoc_bits(struct ocfs2_super
*osb
,
1380 struct ocfs2_alloc_context
*ac
,
1385 unsigned int *num_bits
,
1391 u64 hint_blkno
= ac
->ac_last_group
;
1392 struct ocfs2_chain_list
*cl
;
1393 struct ocfs2_dinode
*fe
;
1397 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1398 BUG_ON(bits_wanted
> (ac
->ac_bits_wanted
- ac
->ac_bits_given
));
1401 fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1403 /* The bh was validated by the inode read during
1404 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1405 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
1407 if (le32_to_cpu(fe
->id1
.bitmap1
.i_used
) >=
1408 le32_to_cpu(fe
->id1
.bitmap1
.i_total
)) {
1409 ocfs2_error(osb
->sb
, "Chain allocator dinode %llu has %u used "
1410 "bits but only %u total.",
1411 (unsigned long long)le64_to_cpu(fe
->i_blkno
),
1412 le32_to_cpu(fe
->id1
.bitmap1
.i_used
),
1413 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1419 /* Attempt to short-circuit the usual search mechanism
1420 * by jumping straight to the most recently used
1421 * allocation group. This helps us mantain some
1422 * contiguousness across allocations. */
1423 status
= ocfs2_search_one_group(ac
, handle
, bits_wanted
,
1424 min_bits
, bit_off
, num_bits
,
1425 hint_blkno
, &bits_left
);
1427 /* Be careful to update *bg_blkno here as the
1428 * caller is expecting it to be filled in, and
1429 * ocfs2_search_one_group() won't do that for
1431 *bg_blkno
= hint_blkno
;
1434 if (status
< 0 && status
!= -ENOSPC
) {
1440 cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1442 victim
= ocfs2_find_victim_chain(cl
);
1443 ac
->ac_chain
= victim
;
1444 ac
->ac_allow_chain_relink
= 1;
1446 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
, bit_off
,
1447 num_bits
, bg_blkno
, &bits_left
);
1450 if (status
< 0 && status
!= -ENOSPC
) {
1455 mlog(0, "Search of victim chain %u came up with nothing, "
1456 "trying all chains now.\n", victim
);
1458 /* If we didn't pick a good victim, then just default to
1459 * searching each chain in order. Don't allow chain relinking
1460 * because we only calculate enough journal credits for one
1461 * relink per alloc. */
1462 ac
->ac_allow_chain_relink
= 0;
1463 for (i
= 0; i
< le16_to_cpu(cl
->cl_next_free_rec
); i
++) {
1466 if (!cl
->cl_recs
[i
].c_free
)
1470 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
,
1471 bit_off
, num_bits
, bg_blkno
,
1475 if (status
< 0 && status
!= -ENOSPC
) {
1482 if (status
!= -ENOSPC
) {
1483 /* If the next search of this group is not likely to
1484 * yield a suitable extent, then we reset the last
1485 * group hint so as to not waste a disk read */
1486 if (bits_left
< min_bits
)
1487 ac
->ac_last_group
= 0;
1489 ac
->ac_last_group
= *bg_blkno
;
1497 int ocfs2_claim_metadata(struct ocfs2_super
*osb
,
1499 struct ocfs2_alloc_context
*ac
,
1501 u16
*suballoc_bit_start
,
1502 unsigned int *num_bits
,
1509 BUG_ON(ac
->ac_bits_wanted
< (ac
->ac_bits_given
+ bits_wanted
));
1510 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_META
);
1512 status
= ocfs2_claim_suballoc_bits(osb
,
1524 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1526 *blkno_start
= bg_blkno
+ (u64
) *suballoc_bit_start
;
1527 ac
->ac_bits_given
+= (*num_bits
);
1534 int ocfs2_claim_new_inode(struct ocfs2_super
*osb
,
1536 struct ocfs2_alloc_context
*ac
,
1541 unsigned int num_bits
;
1547 BUG_ON(ac
->ac_bits_given
!= 0);
1548 BUG_ON(ac
->ac_bits_wanted
!= 1);
1549 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_INODE
);
1551 status
= ocfs2_claim_suballoc_bits(osb
,
1563 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1565 BUG_ON(num_bits
!= 1);
1567 *fe_blkno
= bg_blkno
+ (u64
) (*suballoc_bit
);
1568 ac
->ac_bits_given
++;
1575 /* translate a group desc. blkno and it's bitmap offset into
1576 * disk cluster offset. */
1577 static inline u32
ocfs2_desc_bitmap_to_cluster_off(struct inode
*inode
,
1581 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1584 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1586 if (bg_blkno
!= osb
->first_cluster_group_blkno
)
1587 cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, bg_blkno
);
1588 cluster
+= (u32
) bg_bit_off
;
1592 /* given a cluster offset, calculate which block group it belongs to
1593 * and return that block offset. */
1594 u64
ocfs2_which_cluster_group(struct inode
*inode
, u32 cluster
)
1596 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1599 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1601 group_no
= cluster
/ osb
->bitmap_cpg
;
1603 return osb
->first_cluster_group_blkno
;
1604 return ocfs2_clusters_to_blocks(inode
->i_sb
,
1605 group_no
* osb
->bitmap_cpg
);
1608 /* given the block number of a cluster start, calculate which cluster
1609 * group and descriptor bitmap offset that corresponds to. */
1610 static inline void ocfs2_block_to_cluster_group(struct inode
*inode
,
1615 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1616 u32 data_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, data_blkno
);
1618 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1620 *bg_blkno
= ocfs2_which_cluster_group(inode
,
1623 if (*bg_blkno
== osb
->first_cluster_group_blkno
)
1624 *bg_bit_off
= (u16
) data_cluster
;
1626 *bg_bit_off
= (u16
) ocfs2_blocks_to_clusters(osb
->sb
,
1627 data_blkno
- *bg_blkno
);
1631 * min_bits - minimum contiguous chunk from this total allocation we
1632 * can handle. set to what we asked for originally for a full
1633 * contig. allocation, set to '1' to indicate we can deal with extents
1636 int __ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1638 struct ocfs2_alloc_context
*ac
,
1645 unsigned int bits_wanted
= max_clusters
;
1651 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1653 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_LOCAL
1654 && ac
->ac_which
!= OCFS2_AC_USE_MAIN
);
1656 if (ac
->ac_which
== OCFS2_AC_USE_LOCAL
) {
1657 status
= ocfs2_claim_local_alloc_bits(osb
,
1664 atomic_inc(&osb
->alloc_stats
.local_data
);
1666 if (min_clusters
> (osb
->bitmap_cpg
- 1)) {
1667 /* The only paths asking for contiguousness
1668 * should know about this already. */
1669 mlog(ML_ERROR
, "minimum allocation requested %u exceeds "
1670 "group bitmap size %u!\n", min_clusters
,
1675 /* clamp the current request down to a realistic size. */
1676 if (bits_wanted
> (osb
->bitmap_cpg
- 1))
1677 bits_wanted
= osb
->bitmap_cpg
- 1;
1679 status
= ocfs2_claim_suballoc_bits(osb
,
1689 ocfs2_desc_bitmap_to_cluster_off(ac
->ac_inode
,
1692 atomic_inc(&osb
->alloc_stats
.bitmap_data
);
1696 if (status
!= -ENOSPC
)
1701 ac
->ac_bits_given
+= *num_clusters
;
1708 int ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1710 struct ocfs2_alloc_context
*ac
,
1715 unsigned int bits_wanted
= ac
->ac_bits_wanted
- ac
->ac_bits_given
;
1717 return __ocfs2_claim_clusters(osb
, handle
, ac
, min_clusters
,
1718 bits_wanted
, cluster_start
, num_clusters
);
1721 static inline int ocfs2_block_group_clear_bits(handle_t
*handle
,
1722 struct inode
*alloc_inode
,
1723 struct ocfs2_group_desc
*bg
,
1724 struct buffer_head
*group_bh
,
1725 unsigned int bit_off
,
1726 unsigned int num_bits
)
1730 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
1731 struct ocfs2_group_desc
*undo_bg
= NULL
;
1735 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
1736 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
1741 mlog(0, "off = %u, num = %u\n", bit_off
, num_bits
);
1743 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1744 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
1746 status
= ocfs2_journal_access(handle
, alloc_inode
, group_bh
,
1753 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1754 undo_bg
= (struct ocfs2_group_desc
*) bh2jh(group_bh
)->b_committed_data
;
1758 ocfs2_clear_bit((bit_off
+ tmp
),
1759 (unsigned long *) bg
->bg_bitmap
);
1760 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1761 ocfs2_set_bit(bit_off
+ tmp
,
1762 (unsigned long *) undo_bg
->bg_bitmap
);
1764 le16_add_cpu(&bg
->bg_free_bits_count
, num_bits
);
1766 status
= ocfs2_journal_dirty(handle
, group_bh
);
1774 * expects the suballoc inode to already be locked.
1776 int ocfs2_free_suballoc_bits(handle_t
*handle
,
1777 struct inode
*alloc_inode
,
1778 struct buffer_head
*alloc_bh
,
1779 unsigned int start_bit
,
1785 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) alloc_bh
->b_data
;
1786 struct ocfs2_chain_list
*cl
= &fe
->id2
.i_chain
;
1787 struct buffer_head
*group_bh
= NULL
;
1788 struct ocfs2_group_desc
*group
;
1792 /* The alloc_bh comes from ocfs2_free_dinode() or
1793 * ocfs2_free_clusters(). The callers have all locked the
1794 * allocator and gotten alloc_bh from the lock call. This
1795 * validates the dinode buffer. Any corruption that has happended
1797 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
1798 BUG_ON((count
+ start_bit
) > ocfs2_bits_per_group(cl
));
1800 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1801 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
, count
,
1802 (unsigned long long)bg_blkno
, start_bit
);
1804 status
= ocfs2_read_block(alloc_inode
, bg_blkno
, &group_bh
);
1810 group
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1811 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, group
);
1816 BUG_ON((count
+ start_bit
) > le16_to_cpu(group
->bg_bits
));
1818 status
= ocfs2_block_group_clear_bits(handle
, alloc_inode
,
1826 status
= ocfs2_journal_access(handle
, alloc_inode
, alloc_bh
,
1827 OCFS2_JOURNAL_ACCESS_WRITE
);
1833 le32_add_cpu(&cl
->cl_recs
[le16_to_cpu(group
->bg_chain
)].c_free
,
1835 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1836 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(tmp_used
- count
);
1838 status
= ocfs2_journal_dirty(handle
, alloc_bh
);
1851 int ocfs2_free_dinode(handle_t
*handle
,
1852 struct inode
*inode_alloc_inode
,
1853 struct buffer_head
*inode_alloc_bh
,
1854 struct ocfs2_dinode
*di
)
1856 u64 blk
= le64_to_cpu(di
->i_blkno
);
1857 u16 bit
= le16_to_cpu(di
->i_suballoc_bit
);
1858 u64 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
1860 return ocfs2_free_suballoc_bits(handle
, inode_alloc_inode
,
1861 inode_alloc_bh
, bit
, bg_blkno
, 1);
1864 int ocfs2_free_clusters(handle_t
*handle
,
1865 struct inode
*bitmap_inode
,
1866 struct buffer_head
*bitmap_bh
,
1868 unsigned int num_clusters
)
1873 struct ocfs2_dinode
*fe
;
1875 /* You can't ever have a contiguous set of clusters
1876 * bigger than a block group bitmap so we never have to worry
1877 * about looping on them. */
1881 /* This is expensive. We can safely remove once this stuff has
1882 * gotten tested really well. */
1883 BUG_ON(start_blk
!= ocfs2_clusters_to_blocks(bitmap_inode
->i_sb
, ocfs2_blocks_to_clusters(bitmap_inode
->i_sb
, start_blk
)));
1885 fe
= (struct ocfs2_dinode
*) bitmap_bh
->b_data
;
1887 ocfs2_block_to_cluster_group(bitmap_inode
, start_blk
, &bg_blkno
,
1890 mlog(0, "want to free %u clusters starting at block %llu\n",
1891 num_clusters
, (unsigned long long)start_blk
);
1892 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1893 (unsigned long long)bg_blkno
, bg_start_bit
);
1895 status
= ocfs2_free_suballoc_bits(handle
, bitmap_inode
, bitmap_bh
,
1896 bg_start_bit
, bg_blkno
,
1903 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode
->i_sb
),
1911 static inline void ocfs2_debug_bg(struct ocfs2_group_desc
*bg
)
1913 printk("Block Group:\n");
1914 printk("bg_signature: %s\n", bg
->bg_signature
);
1915 printk("bg_size: %u\n", bg
->bg_size
);
1916 printk("bg_bits: %u\n", bg
->bg_bits
);
1917 printk("bg_free_bits_count: %u\n", bg
->bg_free_bits_count
);
1918 printk("bg_chain: %u\n", bg
->bg_chain
);
1919 printk("bg_generation: %u\n", le32_to_cpu(bg
->bg_generation
));
1920 printk("bg_next_group: %llu\n",
1921 (unsigned long long)bg
->bg_next_group
);
1922 printk("bg_parent_dinode: %llu\n",
1923 (unsigned long long)bg
->bg_parent_dinode
);
1924 printk("bg_blkno: %llu\n",
1925 (unsigned long long)bg
->bg_blkno
);
1928 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode
*fe
)
1932 printk("Suballoc Inode %llu:\n", (unsigned long long)fe
->i_blkno
);
1933 printk("i_signature: %s\n", fe
->i_signature
);
1934 printk("i_size: %llu\n",
1935 (unsigned long long)fe
->i_size
);
1936 printk("i_clusters: %u\n", fe
->i_clusters
);
1937 printk("i_generation: %u\n",
1938 le32_to_cpu(fe
->i_generation
));
1939 printk("id1.bitmap1.i_used: %u\n",
1940 le32_to_cpu(fe
->id1
.bitmap1
.i_used
));
1941 printk("id1.bitmap1.i_total: %u\n",
1942 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1943 printk("id2.i_chain.cl_cpg: %u\n", fe
->id2
.i_chain
.cl_cpg
);
1944 printk("id2.i_chain.cl_bpc: %u\n", fe
->id2
.i_chain
.cl_bpc
);
1945 printk("id2.i_chain.cl_count: %u\n", fe
->id2
.i_chain
.cl_count
);
1946 printk("id2.i_chain.cl_next_free_rec: %u\n",
1947 fe
->id2
.i_chain
.cl_next_free_rec
);
1948 for(i
= 0; i
< fe
->id2
.i_chain
.cl_next_free_rec
; i
++) {
1949 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i
,
1950 fe
->id2
.i_chain
.cl_recs
[i
].c_free
);
1951 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i
,
1952 fe
->id2
.i_chain
.cl_recs
[i
].c_total
);
1953 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i
,
1954 (unsigned long long)fe
->id2
.i_chain
.cl_recs
[i
].c_blkno
);
1959 * For a given allocation, determine which allocators will need to be
1960 * accessed, and lock them, reserving the appropriate number of bits.
1962 * Sparse file systems call this from ocfs2_write_begin_nolock()
1963 * and ocfs2_allocate_unwritten_extents().
1965 * File systems which don't support holes call this from
1966 * ocfs2_extend_allocation().
1968 int ocfs2_lock_allocators(struct inode
*inode
,
1969 struct ocfs2_extent_tree
*et
,
1970 u32 clusters_to_add
, u32 extents_to_split
,
1971 struct ocfs2_alloc_context
**data_ac
,
1972 struct ocfs2_alloc_context
**meta_ac
)
1974 int ret
= 0, num_free_extents
;
1975 unsigned int max_recs_needed
= clusters_to_add
+ 2 * extents_to_split
;
1976 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1982 BUG_ON(clusters_to_add
!= 0 && data_ac
== NULL
);
1984 num_free_extents
= ocfs2_num_free_extents(osb
, inode
, et
);
1985 if (num_free_extents
< 0) {
1986 ret
= num_free_extents
;
1992 * Sparse allocation file systems need to be more conservative
1993 * with reserving room for expansion - the actual allocation
1994 * happens while we've got a journal handle open so re-taking
1995 * a cluster lock (because we ran out of room for another
1996 * extent) will violate ordering rules.
1998 * Most of the time we'll only be seeing this 1 cluster at a time
2001 * Always lock for any unwritten extents - we might want to
2002 * add blocks during a split.
2004 if (!num_free_extents
||
2005 (ocfs2_sparse_alloc(osb
) && num_free_extents
< max_recs_needed
)) {
2006 ret
= ocfs2_reserve_new_metadata(osb
, et
->et_root_el
, meta_ac
);
2014 if (clusters_to_add
== 0)
2017 ret
= ocfs2_reserve_clusters(osb
, clusters_to_add
, data_ac
);
2027 ocfs2_free_alloc_context(*meta_ac
);
2032 * We cannot have an error and a non null *data_ac.