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
);
139 void ocfs2_free_alloc_context(struct ocfs2_alloc_context
*ac
)
141 ocfs2_free_ac_resource(ac
);
145 static u32
ocfs2_bits_per_group(struct ocfs2_chain_list
*cl
)
147 return (u32
)le16_to_cpu(cl
->cl_cpg
) * (u32
)le16_to_cpu(cl
->cl_bpc
);
150 /* somewhat more expensive than our other checks, so use sparingly. */
151 int ocfs2_check_group_descriptor(struct super_block
*sb
,
152 struct ocfs2_dinode
*di
,
153 struct ocfs2_group_desc
*gd
)
155 unsigned int max_bits
;
157 if (!OCFS2_IS_VALID_GROUP_DESC(gd
)) {
158 OCFS2_RO_ON_INVALID_GROUP_DESC(sb
, gd
);
162 if (di
->i_blkno
!= gd
->bg_parent_dinode
) {
163 ocfs2_error(sb
, "Group descriptor # %llu has bad parent "
164 "pointer (%llu, expected %llu)",
165 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
166 (unsigned long long)le64_to_cpu(gd
->bg_parent_dinode
),
167 (unsigned long long)le64_to_cpu(di
->i_blkno
));
171 max_bits
= le16_to_cpu(di
->id2
.i_chain
.cl_cpg
) * le16_to_cpu(di
->id2
.i_chain
.cl_bpc
);
172 if (le16_to_cpu(gd
->bg_bits
) > max_bits
) {
173 ocfs2_error(sb
, "Group descriptor # %llu has bit count of %u",
174 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
175 le16_to_cpu(gd
->bg_bits
));
179 if (le16_to_cpu(gd
->bg_chain
) >=
180 le16_to_cpu(di
->id2
.i_chain
.cl_next_free_rec
)) {
181 ocfs2_error(sb
, "Group descriptor # %llu has bad chain %u",
182 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
183 le16_to_cpu(gd
->bg_chain
));
187 if (le16_to_cpu(gd
->bg_free_bits_count
) > le16_to_cpu(gd
->bg_bits
)) {
188 ocfs2_error(sb
, "Group descriptor # %llu has bit count %u but "
189 "claims that %u are free",
190 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
191 le16_to_cpu(gd
->bg_bits
),
192 le16_to_cpu(gd
->bg_free_bits_count
));
196 if (le16_to_cpu(gd
->bg_bits
) > (8 * le16_to_cpu(gd
->bg_size
))) {
197 ocfs2_error(sb
, "Group descriptor # %llu has bit count %u but "
198 "max bitmap bits of %u",
199 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
200 le16_to_cpu(gd
->bg_bits
),
201 8 * le16_to_cpu(gd
->bg_size
));
208 static int ocfs2_block_group_fill(handle_t
*handle
,
209 struct inode
*alloc_inode
,
210 struct buffer_head
*bg_bh
,
213 struct ocfs2_chain_list
*cl
)
216 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
217 struct super_block
* sb
= alloc_inode
->i_sb
;
221 if (((unsigned long long) bg_bh
->b_blocknr
) != group_blkno
) {
222 ocfs2_error(alloc_inode
->i_sb
, "group block (%llu) != "
224 (unsigned long long)group_blkno
,
225 (unsigned long long) bg_bh
->b_blocknr
);
230 status
= ocfs2_journal_access(handle
,
233 OCFS2_JOURNAL_ACCESS_CREATE
);
239 memset(bg
, 0, sb
->s_blocksize
);
240 strcpy(bg
->bg_signature
, OCFS2_GROUP_DESC_SIGNATURE
);
241 bg
->bg_generation
= cpu_to_le32(OCFS2_SB(sb
)->fs_generation
);
242 bg
->bg_size
= cpu_to_le16(ocfs2_group_bitmap_size(sb
));
243 bg
->bg_bits
= cpu_to_le16(ocfs2_bits_per_group(cl
));
244 bg
->bg_chain
= cpu_to_le16(my_chain
);
245 bg
->bg_next_group
= cl
->cl_recs
[my_chain
].c_blkno
;
246 bg
->bg_parent_dinode
= cpu_to_le64(OCFS2_I(alloc_inode
)->ip_blkno
);
247 bg
->bg_blkno
= cpu_to_le64(group_blkno
);
248 /* set the 1st bit in the bitmap to account for the descriptor block */
249 ocfs2_set_bit(0, (unsigned long *)bg
->bg_bitmap
);
250 bg
->bg_free_bits_count
= cpu_to_le16(le16_to_cpu(bg
->bg_bits
) - 1);
252 status
= ocfs2_journal_dirty(handle
, bg_bh
);
256 /* There is no need to zero out or otherwise initialize the
257 * other blocks in a group - All valid FS metadata in a block
258 * group stores the superblock fs_generation value at
259 * allocation time. */
266 static inline u16
ocfs2_find_smallest_chain(struct ocfs2_chain_list
*cl
)
271 while (curr
< le16_to_cpu(cl
->cl_count
)) {
272 if (le32_to_cpu(cl
->cl_recs
[best
].c_total
) >
273 le32_to_cpu(cl
->cl_recs
[curr
].c_total
))
281 * We expect the block group allocator to already be locked.
283 static int ocfs2_block_group_alloc(struct ocfs2_super
*osb
,
284 struct inode
*alloc_inode
,
285 struct buffer_head
*bh
,
289 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) bh
->b_data
;
290 struct ocfs2_chain_list
*cl
;
291 struct ocfs2_alloc_context
*ac
= NULL
;
292 handle_t
*handle
= NULL
;
293 u32 bit_off
, num_bits
;
296 struct buffer_head
*bg_bh
= NULL
;
297 struct ocfs2_group_desc
*bg
;
299 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode
));
303 cl
= &fe
->id2
.i_chain
;
304 status
= ocfs2_reserve_clusters_with_limit(osb
,
305 le16_to_cpu(cl
->cl_cpg
),
308 if (status
!= -ENOSPC
)
313 credits
= ocfs2_calc_group_alloc_credits(osb
->sb
,
314 le16_to_cpu(cl
->cl_cpg
));
315 handle
= ocfs2_start_trans(osb
, credits
);
316 if (IS_ERR(handle
)) {
317 status
= PTR_ERR(handle
);
323 status
= ocfs2_claim_clusters(osb
,
326 le16_to_cpu(cl
->cl_cpg
),
330 if (status
!= -ENOSPC
)
335 alloc_rec
= ocfs2_find_smallest_chain(cl
);
337 /* setup the group */
338 bg_blkno
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
339 mlog(0, "new descriptor, record %u, at block %llu\n",
340 alloc_rec
, (unsigned long long)bg_blkno
);
342 bg_bh
= sb_getblk(osb
->sb
, bg_blkno
);
348 ocfs2_set_new_buffer_uptodate(alloc_inode
, bg_bh
);
350 status
= ocfs2_block_group_fill(handle
,
361 bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
363 status
= ocfs2_journal_access(handle
, alloc_inode
,
364 bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
370 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_free
,
371 le16_to_cpu(bg
->bg_free_bits_count
));
372 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_total
, le16_to_cpu(bg
->bg_bits
));
373 cl
->cl_recs
[alloc_rec
].c_blkno
= cpu_to_le64(bg_blkno
);
374 if (le16_to_cpu(cl
->cl_next_free_rec
) < le16_to_cpu(cl
->cl_count
))
375 le16_add_cpu(&cl
->cl_next_free_rec
, 1);
377 le32_add_cpu(&fe
->id1
.bitmap1
.i_used
, le16_to_cpu(bg
->bg_bits
) -
378 le16_to_cpu(bg
->bg_free_bits_count
));
379 le32_add_cpu(&fe
->id1
.bitmap1
.i_total
, le16_to_cpu(bg
->bg_bits
));
380 le32_add_cpu(&fe
->i_clusters
, le16_to_cpu(cl
->cl_cpg
));
382 status
= ocfs2_journal_dirty(handle
, bh
);
388 spin_lock(&OCFS2_I(alloc_inode
)->ip_lock
);
389 OCFS2_I(alloc_inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
);
390 fe
->i_size
= cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode
->i_sb
,
391 le32_to_cpu(fe
->i_clusters
)));
392 spin_unlock(&OCFS2_I(alloc_inode
)->ip_lock
);
393 i_size_write(alloc_inode
, le64_to_cpu(fe
->i_size
));
394 alloc_inode
->i_blocks
= ocfs2_inode_sector_count(alloc_inode
);
399 ocfs2_commit_trans(osb
, handle
);
402 ocfs2_free_alloc_context(ac
);
411 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super
*osb
,
412 struct ocfs2_alloc_context
*ac
,
418 u32 bits_wanted
= ac
->ac_bits_wanted
;
419 struct inode
*alloc_inode
;
420 struct buffer_head
*bh
= NULL
;
421 struct ocfs2_dinode
*fe
;
426 alloc_inode
= ocfs2_get_system_file_inode(osb
, type
, slot
);
432 mutex_lock(&alloc_inode
->i_mutex
);
434 status
= ocfs2_inode_lock(alloc_inode
, &bh
, 1);
436 mutex_unlock(&alloc_inode
->i_mutex
);
443 ac
->ac_inode
= alloc_inode
;
444 ac
->ac_alloc_slot
= slot
;
446 fe
= (struct ocfs2_dinode
*) bh
->b_data
;
447 if (!OCFS2_IS_VALID_DINODE(fe
)) {
448 OCFS2_RO_ON_INVALID_DINODE(alloc_inode
->i_sb
, fe
);
452 if (!(fe
->i_flags
& cpu_to_le32(OCFS2_CHAIN_FL
))) {
453 ocfs2_error(alloc_inode
->i_sb
, "Invalid chain allocator %llu",
454 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
459 free_bits
= le32_to_cpu(fe
->id1
.bitmap1
.i_total
) -
460 le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
462 if (bits_wanted
> free_bits
) {
463 /* cluster bitmap never grows */
464 if (ocfs2_is_cluster_bitmap(alloc_inode
)) {
465 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
466 bits_wanted
, free_bits
);
471 if (alloc_new_group
!= ALLOC_NEW_GROUP
) {
472 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
473 "and we don't alloc a new group for it.\n",
474 slot
, bits_wanted
, free_bits
);
479 status
= ocfs2_block_group_alloc(osb
, alloc_inode
, bh
,
482 if (status
!= -ENOSPC
)
486 atomic_inc(&osb
->alloc_stats
.bg_extends
);
488 /* You should never ask for this much metadata */
490 (le32_to_cpu(fe
->id1
.bitmap1
.i_total
)
491 - le32_to_cpu(fe
->id1
.bitmap1
.i_used
)));
504 int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super
*osb
,
506 struct ocfs2_alloc_context
**ac
)
511 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
518 (*ac
)->ac_bits_wanted
= blocks
;
519 (*ac
)->ac_which
= OCFS2_AC_USE_META
;
520 slot
= osb
->slot_num
;
521 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
523 status
= ocfs2_reserve_suballoc_bits(osb
, (*ac
),
524 EXTENT_ALLOC_SYSTEM_INODE
,
525 slot
, ALLOC_NEW_GROUP
);
527 if (status
!= -ENOSPC
)
534 if ((status
< 0) && *ac
) {
535 ocfs2_free_alloc_context(*ac
);
543 int ocfs2_reserve_new_metadata(struct ocfs2_super
*osb
,
544 struct ocfs2_extent_list
*root_el
,
545 struct ocfs2_alloc_context
**ac
)
547 return ocfs2_reserve_new_metadata_blocks(osb
,
548 ocfs2_extend_meta_needed(root_el
),
552 static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super
*osb
,
553 struct ocfs2_alloc_context
*ac
)
555 int i
, status
= -ENOSPC
;
556 s16 slot
= ocfs2_get_inode_steal_slot(osb
);
558 /* Start to steal inodes from the first slot after ours. */
559 if (slot
== OCFS2_INVALID_SLOT
)
560 slot
= osb
->slot_num
+ 1;
562 for (i
= 0; i
< osb
->max_slots
; i
++, slot
++) {
563 if (slot
== osb
->max_slots
)
566 if (slot
== osb
->slot_num
)
569 status
= ocfs2_reserve_suballoc_bits(osb
, ac
,
570 INODE_ALLOC_SYSTEM_INODE
,
571 slot
, NOT_ALLOC_NEW_GROUP
);
573 ocfs2_set_inode_steal_slot(osb
, slot
);
577 ocfs2_free_ac_resource(ac
);
583 int ocfs2_reserve_new_inode(struct ocfs2_super
*osb
,
584 struct ocfs2_alloc_context
**ac
)
587 s16 slot
= ocfs2_get_inode_steal_slot(osb
);
589 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
596 (*ac
)->ac_bits_wanted
= 1;
597 (*ac
)->ac_which
= OCFS2_AC_USE_INODE
;
599 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
602 * stat(2) can't handle i_ino > 32bits, so we tell the
603 * lower levels not to allocate us a block group past that
604 * limit. The 'inode64' mount option avoids this behavior.
606 if (!(osb
->s_mount_opt
& OCFS2_MOUNT_INODE64
))
607 (*ac
)->ac_max_block
= (u32
)~0U;
610 * slot is set when we successfully steal inode from other nodes.
611 * It is reset in 3 places:
612 * 1. when we flush the truncate log
613 * 2. when we complete local alloc recovery.
614 * 3. when we successfully allocate from our own slot.
615 * After it is set, we will go on stealing inodes until we find the
616 * need to check our slots to see whether there is some space for us.
618 if (slot
!= OCFS2_INVALID_SLOT
&&
619 atomic_read(&osb
->s_num_inodes_stolen
) < OCFS2_MAX_INODES_TO_STEAL
)
622 atomic_set(&osb
->s_num_inodes_stolen
, 0);
623 status
= ocfs2_reserve_suballoc_bits(osb
, *ac
,
624 INODE_ALLOC_SYSTEM_INODE
,
625 osb
->slot_num
, ALLOC_NEW_GROUP
);
630 * Some inodes must be freed by us, so try to allocate
631 * from our own next time.
633 if (slot
!= OCFS2_INVALID_SLOT
)
634 ocfs2_init_inode_steal_slot(osb
);
636 } else if (status
< 0 && status
!= -ENOSPC
) {
641 ocfs2_free_ac_resource(*ac
);
644 status
= ocfs2_steal_inode_from_other_nodes(osb
, *ac
);
645 atomic_inc(&osb
->s_num_inodes_stolen
);
647 if (status
!= -ENOSPC
)
654 if ((status
< 0) && *ac
) {
655 ocfs2_free_alloc_context(*ac
);
663 /* local alloc code has to do the same thing, so rather than do this
665 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super
*osb
,
666 struct ocfs2_alloc_context
*ac
)
670 ac
->ac_which
= OCFS2_AC_USE_MAIN
;
671 ac
->ac_group_search
= ocfs2_cluster_group_search
;
673 status
= ocfs2_reserve_suballoc_bits(osb
, ac
,
674 GLOBAL_BITMAP_SYSTEM_INODE
,
677 if (status
< 0 && status
!= -ENOSPC
) {
686 /* Callers don't need to care which bitmap (local alloc or main) to
687 * use so we figure it out for them, but unfortunately this clutters
689 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super
*osb
,
690 u32 bits_wanted
, u64 max_block
,
691 struct ocfs2_alloc_context
**ac
)
697 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
704 (*ac
)->ac_bits_wanted
= bits_wanted
;
705 (*ac
)->ac_max_block
= max_block
;
708 if (ocfs2_alloc_should_use_local(osb
, bits_wanted
)) {
709 status
= ocfs2_reserve_local_alloc_bits(osb
,
712 if (status
== -EFBIG
) {
713 /* The local alloc window is outside ac_max_block.
714 * use the main bitmap. */
716 } else if ((status
< 0) && (status
!= -ENOSPC
)) {
722 if (status
== -ENOSPC
) {
723 status
= ocfs2_reserve_cluster_bitmap_bits(osb
, *ac
);
725 if (status
!= -ENOSPC
)
733 if ((status
< 0) && *ac
) {
734 ocfs2_free_alloc_context(*ac
);
742 int ocfs2_reserve_clusters(struct ocfs2_super
*osb
,
744 struct ocfs2_alloc_context
**ac
)
746 return ocfs2_reserve_clusters_with_limit(osb
, bits_wanted
, 0, ac
);
750 * More or less lifted from ext3. I'll leave their description below:
752 * "For ext3 allocations, we must not reuse any blocks which are
753 * allocated in the bitmap buffer's "last committed data" copy. This
754 * prevents deletes from freeing up the page for reuse until we have
755 * committed the delete transaction.
757 * If we didn't do this, then deleting something and reallocating it as
758 * data would allow the old block to be overwritten before the
759 * transaction committed (because we force data to disk before commit).
760 * This would lead to corruption if we crashed between overwriting the
761 * data and committing the delete.
763 * @@@ We may want to make this allocation behaviour conditional on
764 * data-writes at some point, and disable it for metadata allocations or
767 * Note: OCFS2 already does this differently for metadata vs data
768 * allocations, as those bitmaps are separate and undo access is never
769 * called on a metadata group descriptor.
771 static int ocfs2_test_bg_bit_allocatable(struct buffer_head
*bg_bh
,
774 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
776 if (ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
))
778 if (!buffer_jbd(bg_bh
) || !bh2jh(bg_bh
)->b_committed_data
)
781 bg
= (struct ocfs2_group_desc
*) bh2jh(bg_bh
)->b_committed_data
;
782 return !ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
);
785 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super
*osb
,
786 struct buffer_head
*bg_bh
,
787 unsigned int bits_wanted
,
788 unsigned int total_bits
,
793 u16 best_offset
, best_size
;
794 int offset
, start
, found
, status
= 0;
795 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
797 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
798 OCFS2_RO_ON_INVALID_GROUP_DESC(osb
->sb
, bg
);
802 found
= start
= best_offset
= best_size
= 0;
803 bitmap
= bg
->bg_bitmap
;
805 while((offset
= ocfs2_find_next_zero_bit(bitmap
, total_bits
, start
)) != -1) {
806 if (offset
== total_bits
)
809 if (!ocfs2_test_bg_bit_allocatable(bg_bh
, offset
)) {
810 /* We found a zero, but we can't use it as it
811 * hasn't been put to disk yet! */
814 } else if (offset
== start
) {
815 /* we found a zero */
817 /* move start to the next bit to test */
820 /* got a zero after some ones */
824 if (found
> best_size
) {
826 best_offset
= start
- found
;
828 /* we got everything we needed */
829 if (found
== bits_wanted
) {
830 /* mlog(0, "Found it all!\n"); */
835 /* XXX: I think the first clause is equivalent to the second
837 if (found
== bits_wanted
) {
838 *bit_off
= start
- found
;
840 } else if (best_size
) {
841 *bit_off
= best_offset
;
842 *bits_found
= best_size
;
845 /* No error log here -- see the comment above
846 * ocfs2_test_bg_bit_allocatable */
852 static inline int ocfs2_block_group_set_bits(handle_t
*handle
,
853 struct inode
*alloc_inode
,
854 struct ocfs2_group_desc
*bg
,
855 struct buffer_head
*group_bh
,
856 unsigned int bit_off
,
857 unsigned int num_bits
)
860 void *bitmap
= bg
->bg_bitmap
;
861 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
865 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
866 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
870 BUG_ON(le16_to_cpu(bg
->bg_free_bits_count
) < num_bits
);
872 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off
,
875 if (ocfs2_is_cluster_bitmap(alloc_inode
))
876 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
878 status
= ocfs2_journal_access(handle
,
887 le16_add_cpu(&bg
->bg_free_bits_count
, -num_bits
);
890 ocfs2_set_bit(bit_off
++, bitmap
);
892 status
= ocfs2_journal_dirty(handle
,
904 /* find the one with the most empty bits */
905 static inline u16
ocfs2_find_victim_chain(struct ocfs2_chain_list
*cl
)
909 BUG_ON(!cl
->cl_next_free_rec
);
912 while (curr
< le16_to_cpu(cl
->cl_next_free_rec
)) {
913 if (le32_to_cpu(cl
->cl_recs
[curr
].c_free
) >
914 le32_to_cpu(cl
->cl_recs
[best
].c_free
))
919 BUG_ON(best
>= le16_to_cpu(cl
->cl_next_free_rec
));
923 static int ocfs2_relink_block_group(handle_t
*handle
,
924 struct inode
*alloc_inode
,
925 struct buffer_head
*fe_bh
,
926 struct buffer_head
*bg_bh
,
927 struct buffer_head
*prev_bg_bh
,
931 /* there is a really tiny chance the journal calls could fail,
932 * but we wouldn't want inconsistent blocks in *any* case. */
933 u64 fe_ptr
, bg_ptr
, prev_bg_ptr
;
934 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
935 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
936 struct ocfs2_group_desc
*prev_bg
= (struct ocfs2_group_desc
*) prev_bg_bh
->b_data
;
938 if (!OCFS2_IS_VALID_DINODE(fe
)) {
939 OCFS2_RO_ON_INVALID_DINODE(alloc_inode
->i_sb
, fe
);
943 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
944 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
948 if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg
)) {
949 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, prev_bg
);
954 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
955 (unsigned long long)le64_to_cpu(fe
->i_blkno
), chain
,
956 (unsigned long long)le64_to_cpu(bg
->bg_blkno
),
957 (unsigned long long)le64_to_cpu(prev_bg
->bg_blkno
));
959 fe_ptr
= le64_to_cpu(fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
);
960 bg_ptr
= le64_to_cpu(bg
->bg_next_group
);
961 prev_bg_ptr
= le64_to_cpu(prev_bg
->bg_next_group
);
963 status
= ocfs2_journal_access(handle
, alloc_inode
, prev_bg_bh
,
964 OCFS2_JOURNAL_ACCESS_WRITE
);
970 prev_bg
->bg_next_group
= bg
->bg_next_group
;
972 status
= ocfs2_journal_dirty(handle
, prev_bg_bh
);
978 status
= ocfs2_journal_access(handle
, alloc_inode
, bg_bh
,
979 OCFS2_JOURNAL_ACCESS_WRITE
);
985 bg
->bg_next_group
= fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
;
987 status
= ocfs2_journal_dirty(handle
, bg_bh
);
993 status
= ocfs2_journal_access(handle
, alloc_inode
, fe_bh
,
994 OCFS2_JOURNAL_ACCESS_WRITE
);
1000 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= bg
->bg_blkno
;
1002 status
= ocfs2_journal_dirty(handle
, fe_bh
);
1011 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= cpu_to_le64(fe_ptr
);
1012 bg
->bg_next_group
= cpu_to_le64(bg_ptr
);
1013 prev_bg
->bg_next_group
= cpu_to_le64(prev_bg_ptr
);
1020 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc
*bg
,
1023 return le16_to_cpu(bg
->bg_free_bits_count
) > wanted
;
1026 /* return 0 on success, -ENOSPC to keep searching and any other < 0
1027 * value on error. */
1028 static int ocfs2_cluster_group_search(struct inode
*inode
,
1029 struct buffer_head
*group_bh
,
1030 u32 bits_wanted
, u32 min_bits
,
1032 u16
*bit_off
, u16
*bits_found
)
1034 int search
= -ENOSPC
;
1037 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1038 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1039 u16 tmp_off
, tmp_found
;
1040 unsigned int max_bits
, gd_cluster_off
;
1042 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1044 if (gd
->bg_free_bits_count
) {
1045 max_bits
= le16_to_cpu(gd
->bg_bits
);
1047 /* Tail groups in cluster bitmaps which aren't cpg
1048 * aligned are prone to partial extention by a failed
1049 * fs resize. If the file system resize never got to
1050 * update the dinode cluster count, then we don't want
1051 * to trust any clusters past it, regardless of what
1052 * the group descriptor says. */
1053 gd_cluster_off
= ocfs2_blocks_to_clusters(inode
->i_sb
,
1054 le64_to_cpu(gd
->bg_blkno
));
1055 if ((gd_cluster_off
+ max_bits
) >
1056 OCFS2_I(inode
)->ip_clusters
) {
1057 max_bits
= OCFS2_I(inode
)->ip_clusters
- gd_cluster_off
;
1058 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1059 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
1060 le16_to_cpu(gd
->bg_bits
),
1061 OCFS2_I(inode
)->ip_clusters
, max_bits
);
1064 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1065 group_bh
, bits_wanted
,
1067 &tmp_off
, &tmp_found
);
1072 blkoff
= ocfs2_clusters_to_blocks(inode
->i_sb
,
1074 tmp_off
+ tmp_found
);
1075 mlog(0, "Checking %llu against %llu\n",
1076 (unsigned long long)blkoff
,
1077 (unsigned long long)max_block
);
1078 if (blkoff
> max_block
)
1082 /* ocfs2_block_group_find_clear_bits() might
1083 * return success, but we still want to return
1084 * -ENOSPC unless it found the minimum number
1086 if (min_bits
<= tmp_found
) {
1088 *bits_found
= tmp_found
;
1089 search
= 0; /* success */
1090 } else if (tmp_found
) {
1092 * Don't show bits which we'll be returning
1093 * for allocation to the local alloc bitmap.
1095 ocfs2_local_alloc_seen_free_bits(osb
, tmp_found
);
1102 static int ocfs2_block_group_search(struct inode
*inode
,
1103 struct buffer_head
*group_bh
,
1104 u32 bits_wanted
, u32 min_bits
,
1106 u16
*bit_off
, u16
*bits_found
)
1110 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1112 BUG_ON(min_bits
!= 1);
1113 BUG_ON(ocfs2_is_cluster_bitmap(inode
));
1115 if (bg
->bg_free_bits_count
) {
1116 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1117 group_bh
, bits_wanted
,
1118 le16_to_cpu(bg
->bg_bits
),
1119 bit_off
, bits_found
);
1120 if (!ret
&& max_block
) {
1121 blkoff
= le64_to_cpu(bg
->bg_blkno
) + *bit_off
+
1123 mlog(0, "Checking %llu against %llu\n",
1124 (unsigned long long)blkoff
,
1125 (unsigned long long)max_block
);
1126 if (blkoff
> max_block
)
1134 static int ocfs2_alloc_dinode_update_counts(struct inode
*inode
,
1136 struct buffer_head
*di_bh
,
1142 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*) di_bh
->b_data
;
1143 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &di
->id2
.i_chain
;
1145 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
1146 OCFS2_JOURNAL_ACCESS_WRITE
);
1152 tmp_used
= le32_to_cpu(di
->id1
.bitmap1
.i_used
);
1153 di
->id1
.bitmap1
.i_used
= cpu_to_le32(num_bits
+ tmp_used
);
1154 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -num_bits
);
1156 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1164 static int ocfs2_search_one_group(struct ocfs2_alloc_context
*ac
,
1169 unsigned int *num_bits
,
1175 struct buffer_head
*group_bh
= NULL
;
1176 struct ocfs2_group_desc
*gd
;
1177 struct inode
*alloc_inode
= ac
->ac_inode
;
1179 ret
= ocfs2_read_block(OCFS2_SB(alloc_inode
->i_sb
), gd_blkno
,
1180 &group_bh
, OCFS2_BH_CACHED
, alloc_inode
);
1186 gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1187 if (!OCFS2_IS_VALID_GROUP_DESC(gd
)) {
1188 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, gd
);
1193 ret
= ac
->ac_group_search(alloc_inode
, group_bh
, bits_wanted
, min_bits
,
1194 ac
->ac_max_block
, bit_off
, &found
);
1203 ret
= ocfs2_alloc_dinode_update_counts(alloc_inode
, handle
, ac
->ac_bh
,
1205 le16_to_cpu(gd
->bg_chain
));
1211 ret
= ocfs2_block_group_set_bits(handle
, alloc_inode
, gd
, group_bh
,
1212 *bit_off
, *num_bits
);
1216 *bits_left
= le16_to_cpu(gd
->bg_free_bits_count
);
1224 static int ocfs2_search_chain(struct ocfs2_alloc_context
*ac
,
1229 unsigned int *num_bits
,
1234 u16 chain
, tmp_bits
;
1237 struct inode
*alloc_inode
= ac
->ac_inode
;
1238 struct buffer_head
*group_bh
= NULL
;
1239 struct buffer_head
*prev_group_bh
= NULL
;
1240 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1241 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1242 struct ocfs2_group_desc
*bg
;
1244 chain
= ac
->ac_chain
;
1245 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1247 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
);
1249 status
= ocfs2_read_block(OCFS2_SB(alloc_inode
->i_sb
),
1250 le64_to_cpu(cl
->cl_recs
[chain
].c_blkno
),
1251 &group_bh
, OCFS2_BH_CACHED
, alloc_inode
);
1256 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1257 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, bg
);
1264 /* for now, the chain search is a bit simplistic. We just use
1265 * the 1st group with any empty bits. */
1266 while ((status
= ac
->ac_group_search(alloc_inode
, group_bh
,
1267 bits_wanted
, min_bits
,
1268 ac
->ac_max_block
, bit_off
,
1269 &tmp_bits
)) == -ENOSPC
) {
1270 if (!bg
->bg_next_group
)
1272 if (prev_group_bh
) {
1273 brelse(prev_group_bh
);
1274 prev_group_bh
= NULL
;
1276 next_group
= le64_to_cpu(bg
->bg_next_group
);
1277 prev_group_bh
= group_bh
;
1279 status
= ocfs2_read_block(OCFS2_SB(alloc_inode
->i_sb
),
1280 next_group
, &group_bh
,
1281 OCFS2_BH_CACHED
, alloc_inode
);
1286 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1287 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, bg
);
1294 if (status
!= -ENOSPC
)
1299 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1300 tmp_bits
, (unsigned long long)le64_to_cpu(bg
->bg_blkno
));
1302 *num_bits
= tmp_bits
;
1304 BUG_ON(*num_bits
== 0);
1307 * Keep track of previous block descriptor read. When
1308 * we find a target, if we have read more than X
1309 * number of descriptors, and the target is reasonably
1310 * empty, relink him to top of his chain.
1312 * We've read 0 extra blocks and only send one more to
1313 * the transaction, yet the next guy to search has a
1316 * Do this *after* figuring out how many bits we're taking out
1317 * of our target group.
1319 if (ac
->ac_allow_chain_relink
&&
1321 (ocfs2_block_group_reasonably_empty(bg
, *num_bits
))) {
1322 status
= ocfs2_relink_block_group(handle
, alloc_inode
,
1323 ac
->ac_bh
, group_bh
,
1324 prev_group_bh
, chain
);
1331 /* Ok, claim our bits now: set the info on dinode, chainlist
1332 * and then the group */
1333 status
= ocfs2_journal_access(handle
,
1336 OCFS2_JOURNAL_ACCESS_WRITE
);
1342 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1343 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(*num_bits
+ tmp_used
);
1344 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -(*num_bits
));
1346 status
= ocfs2_journal_dirty(handle
,
1353 status
= ocfs2_block_group_set_bits(handle
,
1364 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits
,
1365 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
1367 *bg_blkno
= le64_to_cpu(bg
->bg_blkno
);
1368 *bits_left
= le16_to_cpu(bg
->bg_free_bits_count
);
1373 brelse(prev_group_bh
);
1379 /* will give out up to bits_wanted contiguous bits. */
1380 static int ocfs2_claim_suballoc_bits(struct ocfs2_super
*osb
,
1381 struct ocfs2_alloc_context
*ac
,
1386 unsigned int *num_bits
,
1392 u64 hint_blkno
= ac
->ac_last_group
;
1393 struct ocfs2_chain_list
*cl
;
1394 struct ocfs2_dinode
*fe
;
1398 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1399 BUG_ON(bits_wanted
> (ac
->ac_bits_wanted
- ac
->ac_bits_given
));
1402 fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1403 if (!OCFS2_IS_VALID_DINODE(fe
)) {
1404 OCFS2_RO_ON_INVALID_DINODE(osb
->sb
, fe
);
1408 if (le32_to_cpu(fe
->id1
.bitmap1
.i_used
) >=
1409 le32_to_cpu(fe
->id1
.bitmap1
.i_total
)) {
1410 ocfs2_error(osb
->sb
, "Chain allocator dinode %llu has %u used "
1411 "bits but only %u total.",
1412 (unsigned long long)le64_to_cpu(fe
->i_blkno
),
1413 le32_to_cpu(fe
->id1
.bitmap1
.i_used
),
1414 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1420 /* Attempt to short-circuit the usual search mechanism
1421 * by jumping straight to the most recently used
1422 * allocation group. This helps us mantain some
1423 * contiguousness across allocations. */
1424 status
= ocfs2_search_one_group(ac
, handle
, bits_wanted
,
1425 min_bits
, bit_off
, num_bits
,
1426 hint_blkno
, &bits_left
);
1428 /* Be careful to update *bg_blkno here as the
1429 * caller is expecting it to be filled in, and
1430 * ocfs2_search_one_group() won't do that for
1432 *bg_blkno
= hint_blkno
;
1435 if (status
< 0 && status
!= -ENOSPC
) {
1441 cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1443 victim
= ocfs2_find_victim_chain(cl
);
1444 ac
->ac_chain
= victim
;
1445 ac
->ac_allow_chain_relink
= 1;
1447 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
, bit_off
,
1448 num_bits
, bg_blkno
, &bits_left
);
1451 if (status
< 0 && status
!= -ENOSPC
) {
1456 mlog(0, "Search of victim chain %u came up with nothing, "
1457 "trying all chains now.\n", victim
);
1459 /* If we didn't pick a good victim, then just default to
1460 * searching each chain in order. Don't allow chain relinking
1461 * because we only calculate enough journal credits for one
1462 * relink per alloc. */
1463 ac
->ac_allow_chain_relink
= 0;
1464 for (i
= 0; i
< le16_to_cpu(cl
->cl_next_free_rec
); i
++) {
1467 if (!cl
->cl_recs
[i
].c_free
)
1471 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
,
1472 bit_off
, num_bits
, bg_blkno
,
1476 if (status
< 0 && status
!= -ENOSPC
) {
1483 if (status
!= -ENOSPC
) {
1484 /* If the next search of this group is not likely to
1485 * yield a suitable extent, then we reset the last
1486 * group hint so as to not waste a disk read */
1487 if (bits_left
< min_bits
)
1488 ac
->ac_last_group
= 0;
1490 ac
->ac_last_group
= *bg_blkno
;
1498 int ocfs2_claim_metadata(struct ocfs2_super
*osb
,
1500 struct ocfs2_alloc_context
*ac
,
1502 u16
*suballoc_bit_start
,
1503 unsigned int *num_bits
,
1510 BUG_ON(ac
->ac_bits_wanted
< (ac
->ac_bits_given
+ bits_wanted
));
1511 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_META
);
1513 status
= ocfs2_claim_suballoc_bits(osb
,
1525 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1527 *blkno_start
= bg_blkno
+ (u64
) *suballoc_bit_start
;
1528 ac
->ac_bits_given
+= (*num_bits
);
1535 int ocfs2_claim_new_inode(struct ocfs2_super
*osb
,
1537 struct ocfs2_alloc_context
*ac
,
1542 unsigned int num_bits
;
1548 BUG_ON(ac
->ac_bits_given
!= 0);
1549 BUG_ON(ac
->ac_bits_wanted
!= 1);
1550 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_INODE
);
1552 status
= ocfs2_claim_suballoc_bits(osb
,
1564 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1566 BUG_ON(num_bits
!= 1);
1568 *fe_blkno
= bg_blkno
+ (u64
) (*suballoc_bit
);
1569 ac
->ac_bits_given
++;
1576 /* translate a group desc. blkno and it's bitmap offset into
1577 * disk cluster offset. */
1578 static inline u32
ocfs2_desc_bitmap_to_cluster_off(struct inode
*inode
,
1582 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1585 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1587 if (bg_blkno
!= osb
->first_cluster_group_blkno
)
1588 cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, bg_blkno
);
1589 cluster
+= (u32
) bg_bit_off
;
1593 /* given a cluster offset, calculate which block group it belongs to
1594 * and return that block offset. */
1595 u64
ocfs2_which_cluster_group(struct inode
*inode
, u32 cluster
)
1597 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1600 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1602 group_no
= cluster
/ osb
->bitmap_cpg
;
1604 return osb
->first_cluster_group_blkno
;
1605 return ocfs2_clusters_to_blocks(inode
->i_sb
,
1606 group_no
* osb
->bitmap_cpg
);
1609 /* given the block number of a cluster start, calculate which cluster
1610 * group and descriptor bitmap offset that corresponds to. */
1611 static inline void ocfs2_block_to_cluster_group(struct inode
*inode
,
1616 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1617 u32 data_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, data_blkno
);
1619 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1621 *bg_blkno
= ocfs2_which_cluster_group(inode
,
1624 if (*bg_blkno
== osb
->first_cluster_group_blkno
)
1625 *bg_bit_off
= (u16
) data_cluster
;
1627 *bg_bit_off
= (u16
) ocfs2_blocks_to_clusters(osb
->sb
,
1628 data_blkno
- *bg_blkno
);
1632 * min_bits - minimum contiguous chunk from this total allocation we
1633 * can handle. set to what we asked for originally for a full
1634 * contig. allocation, set to '1' to indicate we can deal with extents
1637 int __ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1639 struct ocfs2_alloc_context
*ac
,
1646 unsigned int bits_wanted
= max_clusters
;
1652 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1654 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_LOCAL
1655 && ac
->ac_which
!= OCFS2_AC_USE_MAIN
);
1657 if (ac
->ac_which
== OCFS2_AC_USE_LOCAL
) {
1658 status
= ocfs2_claim_local_alloc_bits(osb
,
1665 atomic_inc(&osb
->alloc_stats
.local_data
);
1667 if (min_clusters
> (osb
->bitmap_cpg
- 1)) {
1668 /* The only paths asking for contiguousness
1669 * should know about this already. */
1670 mlog(ML_ERROR
, "minimum allocation requested %u exceeds "
1671 "group bitmap size %u!\n", min_clusters
,
1676 /* clamp the current request down to a realistic size. */
1677 if (bits_wanted
> (osb
->bitmap_cpg
- 1))
1678 bits_wanted
= osb
->bitmap_cpg
- 1;
1680 status
= ocfs2_claim_suballoc_bits(osb
,
1690 ocfs2_desc_bitmap_to_cluster_off(ac
->ac_inode
,
1693 atomic_inc(&osb
->alloc_stats
.bitmap_data
);
1697 if (status
!= -ENOSPC
)
1702 ac
->ac_bits_given
+= *num_clusters
;
1709 int ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1711 struct ocfs2_alloc_context
*ac
,
1716 unsigned int bits_wanted
= ac
->ac_bits_wanted
- ac
->ac_bits_given
;
1718 return __ocfs2_claim_clusters(osb
, handle
, ac
, min_clusters
,
1719 bits_wanted
, cluster_start
, num_clusters
);
1722 static inline int ocfs2_block_group_clear_bits(handle_t
*handle
,
1723 struct inode
*alloc_inode
,
1724 struct ocfs2_group_desc
*bg
,
1725 struct buffer_head
*group_bh
,
1726 unsigned int bit_off
,
1727 unsigned int num_bits
)
1731 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
1732 struct ocfs2_group_desc
*undo_bg
= NULL
;
1736 if (!OCFS2_IS_VALID_GROUP_DESC(bg
)) {
1737 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode
->i_sb
, bg
);
1742 mlog(0, "off = %u, num = %u\n", bit_off
, num_bits
);
1744 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1745 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
1747 status
= ocfs2_journal_access(handle
, alloc_inode
, group_bh
,
1754 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1755 undo_bg
= (struct ocfs2_group_desc
*) bh2jh(group_bh
)->b_committed_data
;
1759 ocfs2_clear_bit((bit_off
+ tmp
),
1760 (unsigned long *) bg
->bg_bitmap
);
1761 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1762 ocfs2_set_bit(bit_off
+ tmp
,
1763 (unsigned long *) undo_bg
->bg_bitmap
);
1765 le16_add_cpu(&bg
->bg_free_bits_count
, num_bits
);
1767 status
= ocfs2_journal_dirty(handle
, group_bh
);
1775 * expects the suballoc inode to already be locked.
1777 int ocfs2_free_suballoc_bits(handle_t
*handle
,
1778 struct inode
*alloc_inode
,
1779 struct buffer_head
*alloc_bh
,
1780 unsigned int start_bit
,
1786 struct ocfs2_super
*osb
= OCFS2_SB(alloc_inode
->i_sb
);
1787 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) alloc_bh
->b_data
;
1788 struct ocfs2_chain_list
*cl
= &fe
->id2
.i_chain
;
1789 struct buffer_head
*group_bh
= NULL
;
1790 struct ocfs2_group_desc
*group
;
1794 if (!OCFS2_IS_VALID_DINODE(fe
)) {
1795 OCFS2_RO_ON_INVALID_DINODE(alloc_inode
->i_sb
, fe
);
1799 BUG_ON((count
+ start_bit
) > ocfs2_bits_per_group(cl
));
1801 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1802 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
, count
,
1803 (unsigned long long)bg_blkno
, start_bit
);
1805 status
= ocfs2_read_block(osb
, bg_blkno
, &group_bh
, OCFS2_BH_CACHED
,
1812 group
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1813 status
= ocfs2_check_group_descriptor(alloc_inode
->i_sb
, fe
, group
);
1818 BUG_ON((count
+ start_bit
) > le16_to_cpu(group
->bg_bits
));
1820 status
= ocfs2_block_group_clear_bits(handle
, alloc_inode
,
1828 status
= ocfs2_journal_access(handle
, alloc_inode
, alloc_bh
,
1829 OCFS2_JOURNAL_ACCESS_WRITE
);
1835 le32_add_cpu(&cl
->cl_recs
[le16_to_cpu(group
->bg_chain
)].c_free
,
1837 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1838 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(tmp_used
- count
);
1840 status
= ocfs2_journal_dirty(handle
, alloc_bh
);
1854 int ocfs2_free_dinode(handle_t
*handle
,
1855 struct inode
*inode_alloc_inode
,
1856 struct buffer_head
*inode_alloc_bh
,
1857 struct ocfs2_dinode
*di
)
1859 u64 blk
= le64_to_cpu(di
->i_blkno
);
1860 u16 bit
= le16_to_cpu(di
->i_suballoc_bit
);
1861 u64 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
1863 return ocfs2_free_suballoc_bits(handle
, inode_alloc_inode
,
1864 inode_alloc_bh
, bit
, bg_blkno
, 1);
1867 int ocfs2_free_clusters(handle_t
*handle
,
1868 struct inode
*bitmap_inode
,
1869 struct buffer_head
*bitmap_bh
,
1871 unsigned int num_clusters
)
1876 struct ocfs2_dinode
*fe
;
1878 /* You can't ever have a contiguous set of clusters
1879 * bigger than a block group bitmap so we never have to worry
1880 * about looping on them. */
1884 /* This is expensive. We can safely remove once this stuff has
1885 * gotten tested really well. */
1886 BUG_ON(start_blk
!= ocfs2_clusters_to_blocks(bitmap_inode
->i_sb
, ocfs2_blocks_to_clusters(bitmap_inode
->i_sb
, start_blk
)));
1888 fe
= (struct ocfs2_dinode
*) bitmap_bh
->b_data
;
1890 ocfs2_block_to_cluster_group(bitmap_inode
, start_blk
, &bg_blkno
,
1893 mlog(0, "want to free %u clusters starting at block %llu\n",
1894 num_clusters
, (unsigned long long)start_blk
);
1895 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1896 (unsigned long long)bg_blkno
, bg_start_bit
);
1898 status
= ocfs2_free_suballoc_bits(handle
, bitmap_inode
, bitmap_bh
,
1899 bg_start_bit
, bg_blkno
,
1906 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode
->i_sb
),
1914 static inline void ocfs2_debug_bg(struct ocfs2_group_desc
*bg
)
1916 printk("Block Group:\n");
1917 printk("bg_signature: %s\n", bg
->bg_signature
);
1918 printk("bg_size: %u\n", bg
->bg_size
);
1919 printk("bg_bits: %u\n", bg
->bg_bits
);
1920 printk("bg_free_bits_count: %u\n", bg
->bg_free_bits_count
);
1921 printk("bg_chain: %u\n", bg
->bg_chain
);
1922 printk("bg_generation: %u\n", le32_to_cpu(bg
->bg_generation
));
1923 printk("bg_next_group: %llu\n",
1924 (unsigned long long)bg
->bg_next_group
);
1925 printk("bg_parent_dinode: %llu\n",
1926 (unsigned long long)bg
->bg_parent_dinode
);
1927 printk("bg_blkno: %llu\n",
1928 (unsigned long long)bg
->bg_blkno
);
1931 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode
*fe
)
1935 printk("Suballoc Inode %llu:\n", (unsigned long long)fe
->i_blkno
);
1936 printk("i_signature: %s\n", fe
->i_signature
);
1937 printk("i_size: %llu\n",
1938 (unsigned long long)fe
->i_size
);
1939 printk("i_clusters: %u\n", fe
->i_clusters
);
1940 printk("i_generation: %u\n",
1941 le32_to_cpu(fe
->i_generation
));
1942 printk("id1.bitmap1.i_used: %u\n",
1943 le32_to_cpu(fe
->id1
.bitmap1
.i_used
));
1944 printk("id1.bitmap1.i_total: %u\n",
1945 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1946 printk("id2.i_chain.cl_cpg: %u\n", fe
->id2
.i_chain
.cl_cpg
);
1947 printk("id2.i_chain.cl_bpc: %u\n", fe
->id2
.i_chain
.cl_bpc
);
1948 printk("id2.i_chain.cl_count: %u\n", fe
->id2
.i_chain
.cl_count
);
1949 printk("id2.i_chain.cl_next_free_rec: %u\n",
1950 fe
->id2
.i_chain
.cl_next_free_rec
);
1951 for(i
= 0; i
< fe
->id2
.i_chain
.cl_next_free_rec
; i
++) {
1952 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i
,
1953 fe
->id2
.i_chain
.cl_recs
[i
].c_free
);
1954 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i
,
1955 fe
->id2
.i_chain
.cl_recs
[i
].c_total
);
1956 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i
,
1957 (unsigned long long)fe
->id2
.i_chain
.cl_recs
[i
].c_blkno
);
1962 * For a given allocation, determine which allocators will need to be
1963 * accessed, and lock them, reserving the appropriate number of bits.
1965 * Sparse file systems call this from ocfs2_write_begin_nolock()
1966 * and ocfs2_allocate_unwritten_extents().
1968 * File systems which don't support holes call this from
1969 * ocfs2_extend_allocation().
1971 int ocfs2_lock_allocators(struct inode
*inode
,
1972 struct ocfs2_extent_tree
*et
,
1973 u32 clusters_to_add
, u32 extents_to_split
,
1974 struct ocfs2_alloc_context
**data_ac
,
1975 struct ocfs2_alloc_context
**meta_ac
)
1977 int ret
= 0, num_free_extents
;
1978 unsigned int max_recs_needed
= clusters_to_add
+ 2 * extents_to_split
;
1979 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1985 BUG_ON(clusters_to_add
!= 0 && data_ac
== NULL
);
1987 num_free_extents
= ocfs2_num_free_extents(osb
, inode
, et
);
1988 if (num_free_extents
< 0) {
1989 ret
= num_free_extents
;
1995 * Sparse allocation file systems need to be more conservative
1996 * with reserving room for expansion - the actual allocation
1997 * happens while we've got a journal handle open so re-taking
1998 * a cluster lock (because we ran out of room for another
1999 * extent) will violate ordering rules.
2001 * Most of the time we'll only be seeing this 1 cluster at a time
2004 * Always lock for any unwritten extents - we might want to
2005 * add blocks during a split.
2007 if (!num_free_extents
||
2008 (ocfs2_sparse_alloc(osb
) && num_free_extents
< max_recs_needed
)) {
2009 ret
= ocfs2_reserve_new_metadata(osb
, et
->et_root_el
, meta_ac
);
2017 if (clusters_to_add
== 0)
2020 ret
= ocfs2_reserve_clusters(osb
, clusters_to_add
, data_ac
);
2030 ocfs2_free_alloc_context(*meta_ac
);
2035 * We cannot have an error and a non null *data_ac.