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 #define do_error(fmt, ...) \
151 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
153 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
156 static int ocfs2_validate_gd_self(struct super_block
*sb
,
157 struct buffer_head
*bh
,
160 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*)bh
->b_data
;
162 if (!OCFS2_IS_VALID_GROUP_DESC(gd
)) {
163 do_error("Group descriptor #%llu has bad signature %.*s",
164 (unsigned long long)bh
->b_blocknr
, 7,
169 if (le64_to_cpu(gd
->bg_blkno
) != bh
->b_blocknr
) {
170 do_error("Group descriptor #%llu has an invalid bg_blkno "
172 (unsigned long long)bh
->b_blocknr
,
173 (unsigned long long)le64_to_cpu(gd
->bg_blkno
));
177 if (le32_to_cpu(gd
->bg_generation
) != OCFS2_SB(sb
)->fs_generation
) {
178 do_error("Group descriptor #%llu has an invalid "
179 "fs_generation of #%u",
180 (unsigned long long)bh
->b_blocknr
,
181 le32_to_cpu(gd
->bg_generation
));
185 if (le16_to_cpu(gd
->bg_free_bits_count
) > le16_to_cpu(gd
->bg_bits
)) {
186 do_error("Group descriptor #%llu has bit count %u but "
187 "claims that %u are free",
188 (unsigned long long)bh
->b_blocknr
,
189 le16_to_cpu(gd
->bg_bits
),
190 le16_to_cpu(gd
->bg_free_bits_count
));
194 if (le16_to_cpu(gd
->bg_bits
) > (8 * le16_to_cpu(gd
->bg_size
))) {
195 do_error("Group descriptor #%llu has bit count %u but "
196 "max bitmap bits of %u",
197 (unsigned long long)bh
->b_blocknr
,
198 le16_to_cpu(gd
->bg_bits
),
199 8 * le16_to_cpu(gd
->bg_size
));
206 static int ocfs2_validate_gd_parent(struct super_block
*sb
,
207 struct ocfs2_dinode
*di
,
208 struct buffer_head
*bh
,
211 unsigned int max_bits
;
212 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*)bh
->b_data
;
214 if (di
->i_blkno
!= gd
->bg_parent_dinode
) {
215 do_error("Group descriptor #%llu has bad parent "
216 "pointer (%llu, expected %llu)",
217 (unsigned long long)bh
->b_blocknr
,
218 (unsigned long long)le64_to_cpu(gd
->bg_parent_dinode
),
219 (unsigned long long)le64_to_cpu(di
->i_blkno
));
223 max_bits
= le16_to_cpu(di
->id2
.i_chain
.cl_cpg
) * le16_to_cpu(di
->id2
.i_chain
.cl_bpc
);
224 if (le16_to_cpu(gd
->bg_bits
) > max_bits
) {
225 do_error("Group descriptor #%llu has bit count of %u",
226 (unsigned long long)bh
->b_blocknr
,
227 le16_to_cpu(gd
->bg_bits
));
231 if (le16_to_cpu(gd
->bg_chain
) >=
232 le16_to_cpu(di
->id2
.i_chain
.cl_next_free_rec
)) {
233 do_error("Group descriptor #%llu has bad chain %u",
234 (unsigned long long)bh
->b_blocknr
,
235 le16_to_cpu(gd
->bg_chain
));
245 * This version only prints errors. It does not fail the filesystem, and
246 * exists only for resize.
248 int ocfs2_check_group_descriptor(struct super_block
*sb
,
249 struct ocfs2_dinode
*di
,
250 struct buffer_head
*bh
)
254 rc
= ocfs2_validate_gd_self(sb
, bh
, 1);
256 rc
= ocfs2_validate_gd_parent(sb
, di
, bh
, 1);
261 static int ocfs2_validate_group_descriptor(struct super_block
*sb
,
262 struct buffer_head
*bh
)
264 mlog(0, "Validating group descriptor %llu\n",
265 (unsigned long long)bh
->b_blocknr
);
267 return ocfs2_validate_gd_self(sb
, bh
, 0);
270 int ocfs2_read_group_descriptor(struct inode
*inode
, struct ocfs2_dinode
*di
,
271 u64 gd_blkno
, struct buffer_head
**bh
)
274 struct buffer_head
*tmp
= *bh
;
276 rc
= ocfs2_read_block(inode
, gd_blkno
, &tmp
,
277 ocfs2_validate_group_descriptor
);
281 rc
= ocfs2_validate_gd_parent(inode
->i_sb
, di
, tmp
, 0);
287 /* If ocfs2_read_block() got us a new bh, pass it up. */
295 static int ocfs2_block_group_fill(handle_t
*handle
,
296 struct inode
*alloc_inode
,
297 struct buffer_head
*bg_bh
,
300 struct ocfs2_chain_list
*cl
)
303 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
304 struct super_block
* sb
= alloc_inode
->i_sb
;
308 if (((unsigned long long) bg_bh
->b_blocknr
) != group_blkno
) {
309 ocfs2_error(alloc_inode
->i_sb
, "group block (%llu) != "
311 (unsigned long long)group_blkno
,
312 (unsigned long long) bg_bh
->b_blocknr
);
317 status
= ocfs2_journal_access(handle
,
320 OCFS2_JOURNAL_ACCESS_CREATE
);
326 memset(bg
, 0, sb
->s_blocksize
);
327 strcpy(bg
->bg_signature
, OCFS2_GROUP_DESC_SIGNATURE
);
328 bg
->bg_generation
= cpu_to_le32(OCFS2_SB(sb
)->fs_generation
);
329 bg
->bg_size
= cpu_to_le16(ocfs2_group_bitmap_size(sb
));
330 bg
->bg_bits
= cpu_to_le16(ocfs2_bits_per_group(cl
));
331 bg
->bg_chain
= cpu_to_le16(my_chain
);
332 bg
->bg_next_group
= cl
->cl_recs
[my_chain
].c_blkno
;
333 bg
->bg_parent_dinode
= cpu_to_le64(OCFS2_I(alloc_inode
)->ip_blkno
);
334 bg
->bg_blkno
= cpu_to_le64(group_blkno
);
335 /* set the 1st bit in the bitmap to account for the descriptor block */
336 ocfs2_set_bit(0, (unsigned long *)bg
->bg_bitmap
);
337 bg
->bg_free_bits_count
= cpu_to_le16(le16_to_cpu(bg
->bg_bits
) - 1);
339 status
= ocfs2_journal_dirty(handle
, bg_bh
);
343 /* There is no need to zero out or otherwise initialize the
344 * other blocks in a group - All valid FS metadata in a block
345 * group stores the superblock fs_generation value at
346 * allocation time. */
353 static inline u16
ocfs2_find_smallest_chain(struct ocfs2_chain_list
*cl
)
358 while (curr
< le16_to_cpu(cl
->cl_count
)) {
359 if (le32_to_cpu(cl
->cl_recs
[best
].c_total
) >
360 le32_to_cpu(cl
->cl_recs
[curr
].c_total
))
368 * We expect the block group allocator to already be locked.
370 static int ocfs2_block_group_alloc(struct ocfs2_super
*osb
,
371 struct inode
*alloc_inode
,
372 struct buffer_head
*bh
,
376 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) bh
->b_data
;
377 struct ocfs2_chain_list
*cl
;
378 struct ocfs2_alloc_context
*ac
= NULL
;
379 handle_t
*handle
= NULL
;
380 u32 bit_off
, num_bits
;
383 struct buffer_head
*bg_bh
= NULL
;
384 struct ocfs2_group_desc
*bg
;
386 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode
));
390 cl
= &fe
->id2
.i_chain
;
391 status
= ocfs2_reserve_clusters_with_limit(osb
,
392 le16_to_cpu(cl
->cl_cpg
),
395 if (status
!= -ENOSPC
)
400 credits
= ocfs2_calc_group_alloc_credits(osb
->sb
,
401 le16_to_cpu(cl
->cl_cpg
));
402 handle
= ocfs2_start_trans(osb
, credits
);
403 if (IS_ERR(handle
)) {
404 status
= PTR_ERR(handle
);
410 status
= ocfs2_claim_clusters(osb
,
413 le16_to_cpu(cl
->cl_cpg
),
417 if (status
!= -ENOSPC
)
422 alloc_rec
= ocfs2_find_smallest_chain(cl
);
424 /* setup the group */
425 bg_blkno
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
426 mlog(0, "new descriptor, record %u, at block %llu\n",
427 alloc_rec
, (unsigned long long)bg_blkno
);
429 bg_bh
= sb_getblk(osb
->sb
, bg_blkno
);
435 ocfs2_set_new_buffer_uptodate(alloc_inode
, bg_bh
);
437 status
= ocfs2_block_group_fill(handle
,
448 bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
450 status
= ocfs2_journal_access(handle
, alloc_inode
,
451 bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
457 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_free
,
458 le16_to_cpu(bg
->bg_free_bits_count
));
459 le32_add_cpu(&cl
->cl_recs
[alloc_rec
].c_total
, le16_to_cpu(bg
->bg_bits
));
460 cl
->cl_recs
[alloc_rec
].c_blkno
= cpu_to_le64(bg_blkno
);
461 if (le16_to_cpu(cl
->cl_next_free_rec
) < le16_to_cpu(cl
->cl_count
))
462 le16_add_cpu(&cl
->cl_next_free_rec
, 1);
464 le32_add_cpu(&fe
->id1
.bitmap1
.i_used
, le16_to_cpu(bg
->bg_bits
) -
465 le16_to_cpu(bg
->bg_free_bits_count
));
466 le32_add_cpu(&fe
->id1
.bitmap1
.i_total
, le16_to_cpu(bg
->bg_bits
));
467 le32_add_cpu(&fe
->i_clusters
, le16_to_cpu(cl
->cl_cpg
));
469 status
= ocfs2_journal_dirty(handle
, bh
);
475 spin_lock(&OCFS2_I(alloc_inode
)->ip_lock
);
476 OCFS2_I(alloc_inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
);
477 fe
->i_size
= cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode
->i_sb
,
478 le32_to_cpu(fe
->i_clusters
)));
479 spin_unlock(&OCFS2_I(alloc_inode
)->ip_lock
);
480 i_size_write(alloc_inode
, le64_to_cpu(fe
->i_size
));
481 alloc_inode
->i_blocks
= ocfs2_inode_sector_count(alloc_inode
);
486 ocfs2_commit_trans(osb
, handle
);
489 ocfs2_free_alloc_context(ac
);
497 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super
*osb
,
498 struct ocfs2_alloc_context
*ac
,
504 u32 bits_wanted
= ac
->ac_bits_wanted
;
505 struct inode
*alloc_inode
;
506 struct buffer_head
*bh
= NULL
;
507 struct ocfs2_dinode
*fe
;
512 alloc_inode
= ocfs2_get_system_file_inode(osb
, type
, slot
);
518 mutex_lock(&alloc_inode
->i_mutex
);
520 status
= ocfs2_inode_lock(alloc_inode
, &bh
, 1);
522 mutex_unlock(&alloc_inode
->i_mutex
);
529 ac
->ac_inode
= alloc_inode
;
530 ac
->ac_alloc_slot
= slot
;
532 fe
= (struct ocfs2_dinode
*) bh
->b_data
;
534 /* The bh was validated by the inode read inside
535 * ocfs2_inode_lock(). Any corruption is a code bug. */
536 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
538 if (!(fe
->i_flags
& cpu_to_le32(OCFS2_CHAIN_FL
))) {
539 ocfs2_error(alloc_inode
->i_sb
, "Invalid chain allocator %llu",
540 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
545 free_bits
= le32_to_cpu(fe
->id1
.bitmap1
.i_total
) -
546 le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
548 if (bits_wanted
> free_bits
) {
549 /* cluster bitmap never grows */
550 if (ocfs2_is_cluster_bitmap(alloc_inode
)) {
551 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
552 bits_wanted
, free_bits
);
557 if (alloc_new_group
!= ALLOC_NEW_GROUP
) {
558 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
559 "and we don't alloc a new group for it.\n",
560 slot
, bits_wanted
, free_bits
);
565 status
= ocfs2_block_group_alloc(osb
, alloc_inode
, bh
,
568 if (status
!= -ENOSPC
)
572 atomic_inc(&osb
->alloc_stats
.bg_extends
);
574 /* You should never ask for this much metadata */
576 (le32_to_cpu(fe
->id1
.bitmap1
.i_total
)
577 - le32_to_cpu(fe
->id1
.bitmap1
.i_used
)));
589 int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super
*osb
,
591 struct ocfs2_alloc_context
**ac
)
596 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
603 (*ac
)->ac_bits_wanted
= blocks
;
604 (*ac
)->ac_which
= OCFS2_AC_USE_META
;
605 slot
= osb
->slot_num
;
606 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
608 status
= ocfs2_reserve_suballoc_bits(osb
, (*ac
),
609 EXTENT_ALLOC_SYSTEM_INODE
,
610 slot
, ALLOC_NEW_GROUP
);
612 if (status
!= -ENOSPC
)
619 if ((status
< 0) && *ac
) {
620 ocfs2_free_alloc_context(*ac
);
628 int ocfs2_reserve_new_metadata(struct ocfs2_super
*osb
,
629 struct ocfs2_extent_list
*root_el
,
630 struct ocfs2_alloc_context
**ac
)
632 return ocfs2_reserve_new_metadata_blocks(osb
,
633 ocfs2_extend_meta_needed(root_el
),
637 static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super
*osb
,
638 struct ocfs2_alloc_context
*ac
)
640 int i
, status
= -ENOSPC
;
641 s16 slot
= ocfs2_get_inode_steal_slot(osb
);
643 /* Start to steal inodes from the first slot after ours. */
644 if (slot
== OCFS2_INVALID_SLOT
)
645 slot
= osb
->slot_num
+ 1;
647 for (i
= 0; i
< osb
->max_slots
; i
++, slot
++) {
648 if (slot
== osb
->max_slots
)
651 if (slot
== osb
->slot_num
)
654 status
= ocfs2_reserve_suballoc_bits(osb
, ac
,
655 INODE_ALLOC_SYSTEM_INODE
,
656 slot
, NOT_ALLOC_NEW_GROUP
);
658 ocfs2_set_inode_steal_slot(osb
, slot
);
662 ocfs2_free_ac_resource(ac
);
668 int ocfs2_reserve_new_inode(struct ocfs2_super
*osb
,
669 struct ocfs2_alloc_context
**ac
)
672 s16 slot
= ocfs2_get_inode_steal_slot(osb
);
674 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
681 (*ac
)->ac_bits_wanted
= 1;
682 (*ac
)->ac_which
= OCFS2_AC_USE_INODE
;
684 (*ac
)->ac_group_search
= ocfs2_block_group_search
;
687 * stat(2) can't handle i_ino > 32bits, so we tell the
688 * lower levels not to allocate us a block group past that
689 * limit. The 'inode64' mount option avoids this behavior.
691 if (!(osb
->s_mount_opt
& OCFS2_MOUNT_INODE64
))
692 (*ac
)->ac_max_block
= (u32
)~0U;
695 * slot is set when we successfully steal inode from other nodes.
696 * It is reset in 3 places:
697 * 1. when we flush the truncate log
698 * 2. when we complete local alloc recovery.
699 * 3. when we successfully allocate from our own slot.
700 * After it is set, we will go on stealing inodes until we find the
701 * need to check our slots to see whether there is some space for us.
703 if (slot
!= OCFS2_INVALID_SLOT
&&
704 atomic_read(&osb
->s_num_inodes_stolen
) < OCFS2_MAX_INODES_TO_STEAL
)
707 atomic_set(&osb
->s_num_inodes_stolen
, 0);
708 status
= ocfs2_reserve_suballoc_bits(osb
, *ac
,
709 INODE_ALLOC_SYSTEM_INODE
,
710 osb
->slot_num
, ALLOC_NEW_GROUP
);
715 * Some inodes must be freed by us, so try to allocate
716 * from our own next time.
718 if (slot
!= OCFS2_INVALID_SLOT
)
719 ocfs2_init_inode_steal_slot(osb
);
721 } else if (status
< 0 && status
!= -ENOSPC
) {
726 ocfs2_free_ac_resource(*ac
);
729 status
= ocfs2_steal_inode_from_other_nodes(osb
, *ac
);
730 atomic_inc(&osb
->s_num_inodes_stolen
);
732 if (status
!= -ENOSPC
)
739 if ((status
< 0) && *ac
) {
740 ocfs2_free_alloc_context(*ac
);
748 /* local alloc code has to do the same thing, so rather than do this
750 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super
*osb
,
751 struct ocfs2_alloc_context
*ac
)
755 ac
->ac_which
= OCFS2_AC_USE_MAIN
;
756 ac
->ac_group_search
= ocfs2_cluster_group_search
;
758 status
= ocfs2_reserve_suballoc_bits(osb
, ac
,
759 GLOBAL_BITMAP_SYSTEM_INODE
,
762 if (status
< 0 && status
!= -ENOSPC
) {
771 /* Callers don't need to care which bitmap (local alloc or main) to
772 * use so we figure it out for them, but unfortunately this clutters
774 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super
*osb
,
775 u32 bits_wanted
, u64 max_block
,
776 struct ocfs2_alloc_context
**ac
)
782 *ac
= kzalloc(sizeof(struct ocfs2_alloc_context
), GFP_KERNEL
);
789 (*ac
)->ac_bits_wanted
= bits_wanted
;
790 (*ac
)->ac_max_block
= max_block
;
793 if (ocfs2_alloc_should_use_local(osb
, bits_wanted
)) {
794 status
= ocfs2_reserve_local_alloc_bits(osb
,
797 if (status
== -EFBIG
) {
798 /* The local alloc window is outside ac_max_block.
799 * use the main bitmap. */
801 } else if ((status
< 0) && (status
!= -ENOSPC
)) {
807 if (status
== -ENOSPC
) {
808 status
= ocfs2_reserve_cluster_bitmap_bits(osb
, *ac
);
810 if (status
!= -ENOSPC
)
818 if ((status
< 0) && *ac
) {
819 ocfs2_free_alloc_context(*ac
);
827 int ocfs2_reserve_clusters(struct ocfs2_super
*osb
,
829 struct ocfs2_alloc_context
**ac
)
831 return ocfs2_reserve_clusters_with_limit(osb
, bits_wanted
, 0, ac
);
835 * More or less lifted from ext3. I'll leave their description below:
837 * "For ext3 allocations, we must not reuse any blocks which are
838 * allocated in the bitmap buffer's "last committed data" copy. This
839 * prevents deletes from freeing up the page for reuse until we have
840 * committed the delete transaction.
842 * If we didn't do this, then deleting something and reallocating it as
843 * data would allow the old block to be overwritten before the
844 * transaction committed (because we force data to disk before commit).
845 * This would lead to corruption if we crashed between overwriting the
846 * data and committing the delete.
848 * @@@ We may want to make this allocation behaviour conditional on
849 * data-writes at some point, and disable it for metadata allocations or
852 * Note: OCFS2 already does this differently for metadata vs data
853 * allocations, as those bitmaps are separate and undo access is never
854 * called on a metadata group descriptor.
856 static int ocfs2_test_bg_bit_allocatable(struct buffer_head
*bg_bh
,
859 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
861 if (ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
))
863 if (!buffer_jbd(bg_bh
) || !bh2jh(bg_bh
)->b_committed_data
)
866 bg
= (struct ocfs2_group_desc
*) bh2jh(bg_bh
)->b_committed_data
;
867 return !ocfs2_test_bit(nr
, (unsigned long *)bg
->bg_bitmap
);
870 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super
*osb
,
871 struct buffer_head
*bg_bh
,
872 unsigned int bits_wanted
,
873 unsigned int total_bits
,
878 u16 best_offset
, best_size
;
879 int offset
, start
, found
, status
= 0;
880 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
882 /* Callers got this descriptor from
883 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
884 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
886 found
= start
= best_offset
= best_size
= 0;
887 bitmap
= bg
->bg_bitmap
;
889 while((offset
= ocfs2_find_next_zero_bit(bitmap
, total_bits
, start
)) != -1) {
890 if (offset
== total_bits
)
893 if (!ocfs2_test_bg_bit_allocatable(bg_bh
, offset
)) {
894 /* We found a zero, but we can't use it as it
895 * hasn't been put to disk yet! */
898 } else if (offset
== start
) {
899 /* we found a zero */
901 /* move start to the next bit to test */
904 /* got a zero after some ones */
908 if (found
> best_size
) {
910 best_offset
= start
- found
;
912 /* we got everything we needed */
913 if (found
== bits_wanted
) {
914 /* mlog(0, "Found it all!\n"); */
919 /* XXX: I think the first clause is equivalent to the second
921 if (found
== bits_wanted
) {
922 *bit_off
= start
- found
;
924 } else if (best_size
) {
925 *bit_off
= best_offset
;
926 *bits_found
= best_size
;
929 /* No error log here -- see the comment above
930 * ocfs2_test_bg_bit_allocatable */
936 static inline int ocfs2_block_group_set_bits(handle_t
*handle
,
937 struct inode
*alloc_inode
,
938 struct ocfs2_group_desc
*bg
,
939 struct buffer_head
*group_bh
,
940 unsigned int bit_off
,
941 unsigned int num_bits
)
944 void *bitmap
= bg
->bg_bitmap
;
945 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
949 /* All callers get the descriptor via
950 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
951 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
952 BUG_ON(le16_to_cpu(bg
->bg_free_bits_count
) < num_bits
);
954 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off
,
957 if (ocfs2_is_cluster_bitmap(alloc_inode
))
958 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
960 status
= ocfs2_journal_access(handle
,
969 le16_add_cpu(&bg
->bg_free_bits_count
, -num_bits
);
972 ocfs2_set_bit(bit_off
++, bitmap
);
974 status
= ocfs2_journal_dirty(handle
,
986 /* find the one with the most empty bits */
987 static inline u16
ocfs2_find_victim_chain(struct ocfs2_chain_list
*cl
)
991 BUG_ON(!cl
->cl_next_free_rec
);
994 while (curr
< le16_to_cpu(cl
->cl_next_free_rec
)) {
995 if (le32_to_cpu(cl
->cl_recs
[curr
].c_free
) >
996 le32_to_cpu(cl
->cl_recs
[best
].c_free
))
1001 BUG_ON(best
>= le16_to_cpu(cl
->cl_next_free_rec
));
1005 static int ocfs2_relink_block_group(handle_t
*handle
,
1006 struct inode
*alloc_inode
,
1007 struct buffer_head
*fe_bh
,
1008 struct buffer_head
*bg_bh
,
1009 struct buffer_head
*prev_bg_bh
,
1013 /* there is a really tiny chance the journal calls could fail,
1014 * but we wouldn't want inconsistent blocks in *any* case. */
1015 u64 fe_ptr
, bg_ptr
, prev_bg_ptr
;
1016 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
1017 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) bg_bh
->b_data
;
1018 struct ocfs2_group_desc
*prev_bg
= (struct ocfs2_group_desc
*) prev_bg_bh
->b_data
;
1020 /* The caller got these descriptors from
1021 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1022 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
1023 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg
));
1025 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
1026 (unsigned long long)le64_to_cpu(fe
->i_blkno
), chain
,
1027 (unsigned long long)le64_to_cpu(bg
->bg_blkno
),
1028 (unsigned long long)le64_to_cpu(prev_bg
->bg_blkno
));
1030 fe_ptr
= le64_to_cpu(fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
);
1031 bg_ptr
= le64_to_cpu(bg
->bg_next_group
);
1032 prev_bg_ptr
= le64_to_cpu(prev_bg
->bg_next_group
);
1034 status
= ocfs2_journal_access(handle
, alloc_inode
, prev_bg_bh
,
1035 OCFS2_JOURNAL_ACCESS_WRITE
);
1041 prev_bg
->bg_next_group
= bg
->bg_next_group
;
1043 status
= ocfs2_journal_dirty(handle
, prev_bg_bh
);
1049 status
= ocfs2_journal_access(handle
, alloc_inode
, bg_bh
,
1050 OCFS2_JOURNAL_ACCESS_WRITE
);
1056 bg
->bg_next_group
= fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
;
1058 status
= ocfs2_journal_dirty(handle
, bg_bh
);
1064 status
= ocfs2_journal_access(handle
, alloc_inode
, fe_bh
,
1065 OCFS2_JOURNAL_ACCESS_WRITE
);
1071 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= bg
->bg_blkno
;
1073 status
= ocfs2_journal_dirty(handle
, fe_bh
);
1082 fe
->id2
.i_chain
.cl_recs
[chain
].c_blkno
= cpu_to_le64(fe_ptr
);
1083 bg
->bg_next_group
= cpu_to_le64(bg_ptr
);
1084 prev_bg
->bg_next_group
= cpu_to_le64(prev_bg_ptr
);
1091 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc
*bg
,
1094 return le16_to_cpu(bg
->bg_free_bits_count
) > wanted
;
1097 /* return 0 on success, -ENOSPC to keep searching and any other < 0
1098 * value on error. */
1099 static int ocfs2_cluster_group_search(struct inode
*inode
,
1100 struct buffer_head
*group_bh
,
1101 u32 bits_wanted
, u32 min_bits
,
1103 u16
*bit_off
, u16
*bits_found
)
1105 int search
= -ENOSPC
;
1108 struct ocfs2_group_desc
*gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1109 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1110 u16 tmp_off
, tmp_found
;
1111 unsigned int max_bits
, gd_cluster_off
;
1113 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1115 if (gd
->bg_free_bits_count
) {
1116 max_bits
= le16_to_cpu(gd
->bg_bits
);
1118 /* Tail groups in cluster bitmaps which aren't cpg
1119 * aligned are prone to partial extention by a failed
1120 * fs resize. If the file system resize never got to
1121 * update the dinode cluster count, then we don't want
1122 * to trust any clusters past it, regardless of what
1123 * the group descriptor says. */
1124 gd_cluster_off
= ocfs2_blocks_to_clusters(inode
->i_sb
,
1125 le64_to_cpu(gd
->bg_blkno
));
1126 if ((gd_cluster_off
+ max_bits
) >
1127 OCFS2_I(inode
)->ip_clusters
) {
1128 max_bits
= OCFS2_I(inode
)->ip_clusters
- gd_cluster_off
;
1129 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1130 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
1131 le16_to_cpu(gd
->bg_bits
),
1132 OCFS2_I(inode
)->ip_clusters
, max_bits
);
1135 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1136 group_bh
, bits_wanted
,
1138 &tmp_off
, &tmp_found
);
1143 blkoff
= ocfs2_clusters_to_blocks(inode
->i_sb
,
1145 tmp_off
+ tmp_found
);
1146 mlog(0, "Checking %llu against %llu\n",
1147 (unsigned long long)blkoff
,
1148 (unsigned long long)max_block
);
1149 if (blkoff
> max_block
)
1153 /* ocfs2_block_group_find_clear_bits() might
1154 * return success, but we still want to return
1155 * -ENOSPC unless it found the minimum number
1157 if (min_bits
<= tmp_found
) {
1159 *bits_found
= tmp_found
;
1160 search
= 0; /* success */
1161 } else if (tmp_found
) {
1163 * Don't show bits which we'll be returning
1164 * for allocation to the local alloc bitmap.
1166 ocfs2_local_alloc_seen_free_bits(osb
, tmp_found
);
1173 static int ocfs2_block_group_search(struct inode
*inode
,
1174 struct buffer_head
*group_bh
,
1175 u32 bits_wanted
, u32 min_bits
,
1177 u16
*bit_off
, u16
*bits_found
)
1181 struct ocfs2_group_desc
*bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1183 BUG_ON(min_bits
!= 1);
1184 BUG_ON(ocfs2_is_cluster_bitmap(inode
));
1186 if (bg
->bg_free_bits_count
) {
1187 ret
= ocfs2_block_group_find_clear_bits(OCFS2_SB(inode
->i_sb
),
1188 group_bh
, bits_wanted
,
1189 le16_to_cpu(bg
->bg_bits
),
1190 bit_off
, bits_found
);
1191 if (!ret
&& max_block
) {
1192 blkoff
= le64_to_cpu(bg
->bg_blkno
) + *bit_off
+
1194 mlog(0, "Checking %llu against %llu\n",
1195 (unsigned long long)blkoff
,
1196 (unsigned long long)max_block
);
1197 if (blkoff
> max_block
)
1205 static int ocfs2_alloc_dinode_update_counts(struct inode
*inode
,
1207 struct buffer_head
*di_bh
,
1213 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*) di_bh
->b_data
;
1214 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &di
->id2
.i_chain
;
1216 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
1217 OCFS2_JOURNAL_ACCESS_WRITE
);
1223 tmp_used
= le32_to_cpu(di
->id1
.bitmap1
.i_used
);
1224 di
->id1
.bitmap1
.i_used
= cpu_to_le32(num_bits
+ tmp_used
);
1225 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -num_bits
);
1227 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1235 static int ocfs2_search_one_group(struct ocfs2_alloc_context
*ac
,
1240 unsigned int *num_bits
,
1246 struct buffer_head
*group_bh
= NULL
;
1247 struct ocfs2_group_desc
*gd
;
1248 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)ac
->ac_bh
->b_data
;
1249 struct inode
*alloc_inode
= ac
->ac_inode
;
1251 ret
= ocfs2_read_group_descriptor(alloc_inode
, di
, gd_blkno
,
1258 gd
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1259 ret
= ac
->ac_group_search(alloc_inode
, group_bh
, bits_wanted
, min_bits
,
1260 ac
->ac_max_block
, bit_off
, &found
);
1269 ret
= ocfs2_alloc_dinode_update_counts(alloc_inode
, handle
, ac
->ac_bh
,
1271 le16_to_cpu(gd
->bg_chain
));
1277 ret
= ocfs2_block_group_set_bits(handle
, alloc_inode
, gd
, group_bh
,
1278 *bit_off
, *num_bits
);
1282 *bits_left
= le16_to_cpu(gd
->bg_free_bits_count
);
1290 static int ocfs2_search_chain(struct ocfs2_alloc_context
*ac
,
1295 unsigned int *num_bits
,
1300 u16 chain
, tmp_bits
;
1303 struct inode
*alloc_inode
= ac
->ac_inode
;
1304 struct buffer_head
*group_bh
= NULL
;
1305 struct buffer_head
*prev_group_bh
= NULL
;
1306 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1307 struct ocfs2_chain_list
*cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1308 struct ocfs2_group_desc
*bg
;
1310 chain
= ac
->ac_chain
;
1311 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1313 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
);
1315 status
= ocfs2_read_group_descriptor(alloc_inode
, fe
,
1316 le64_to_cpu(cl
->cl_recs
[chain
].c_blkno
),
1322 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1325 /* for now, the chain search is a bit simplistic. We just use
1326 * the 1st group with any empty bits. */
1327 while ((status
= ac
->ac_group_search(alloc_inode
, group_bh
,
1328 bits_wanted
, min_bits
,
1329 ac
->ac_max_block
, bit_off
,
1330 &tmp_bits
)) == -ENOSPC
) {
1331 if (!bg
->bg_next_group
)
1334 brelse(prev_group_bh
);
1335 prev_group_bh
= NULL
;
1337 next_group
= le64_to_cpu(bg
->bg_next_group
);
1338 prev_group_bh
= group_bh
;
1340 status
= ocfs2_read_group_descriptor(alloc_inode
, fe
,
1341 next_group
, &group_bh
);
1346 bg
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1349 if (status
!= -ENOSPC
)
1354 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1355 tmp_bits
, (unsigned long long)le64_to_cpu(bg
->bg_blkno
));
1357 *num_bits
= tmp_bits
;
1359 BUG_ON(*num_bits
== 0);
1362 * Keep track of previous block descriptor read. When
1363 * we find a target, if we have read more than X
1364 * number of descriptors, and the target is reasonably
1365 * empty, relink him to top of his chain.
1367 * We've read 0 extra blocks and only send one more to
1368 * the transaction, yet the next guy to search has a
1371 * Do this *after* figuring out how many bits we're taking out
1372 * of our target group.
1374 if (ac
->ac_allow_chain_relink
&&
1376 (ocfs2_block_group_reasonably_empty(bg
, *num_bits
))) {
1377 status
= ocfs2_relink_block_group(handle
, alloc_inode
,
1378 ac
->ac_bh
, group_bh
,
1379 prev_group_bh
, chain
);
1386 /* Ok, claim our bits now: set the info on dinode, chainlist
1387 * and then the group */
1388 status
= ocfs2_journal_access(handle
,
1391 OCFS2_JOURNAL_ACCESS_WRITE
);
1397 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1398 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(*num_bits
+ tmp_used
);
1399 le32_add_cpu(&cl
->cl_recs
[chain
].c_free
, -(*num_bits
));
1401 status
= ocfs2_journal_dirty(handle
,
1408 status
= ocfs2_block_group_set_bits(handle
,
1419 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits
,
1420 (unsigned long long)le64_to_cpu(fe
->i_blkno
));
1422 *bg_blkno
= le64_to_cpu(bg
->bg_blkno
);
1423 *bits_left
= le16_to_cpu(bg
->bg_free_bits_count
);
1426 brelse(prev_group_bh
);
1432 /* will give out up to bits_wanted contiguous bits. */
1433 static int ocfs2_claim_suballoc_bits(struct ocfs2_super
*osb
,
1434 struct ocfs2_alloc_context
*ac
,
1439 unsigned int *num_bits
,
1445 u64 hint_blkno
= ac
->ac_last_group
;
1446 struct ocfs2_chain_list
*cl
;
1447 struct ocfs2_dinode
*fe
;
1451 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1452 BUG_ON(bits_wanted
> (ac
->ac_bits_wanted
- ac
->ac_bits_given
));
1455 fe
= (struct ocfs2_dinode
*) ac
->ac_bh
->b_data
;
1457 /* The bh was validated by the inode read during
1458 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1459 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
1461 if (le32_to_cpu(fe
->id1
.bitmap1
.i_used
) >=
1462 le32_to_cpu(fe
->id1
.bitmap1
.i_total
)) {
1463 ocfs2_error(osb
->sb
, "Chain allocator dinode %llu has %u used "
1464 "bits but only %u total.",
1465 (unsigned long long)le64_to_cpu(fe
->i_blkno
),
1466 le32_to_cpu(fe
->id1
.bitmap1
.i_used
),
1467 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1473 /* Attempt to short-circuit the usual search mechanism
1474 * by jumping straight to the most recently used
1475 * allocation group. This helps us mantain some
1476 * contiguousness across allocations. */
1477 status
= ocfs2_search_one_group(ac
, handle
, bits_wanted
,
1478 min_bits
, bit_off
, num_bits
,
1479 hint_blkno
, &bits_left
);
1481 /* Be careful to update *bg_blkno here as the
1482 * caller is expecting it to be filled in, and
1483 * ocfs2_search_one_group() won't do that for
1485 *bg_blkno
= hint_blkno
;
1488 if (status
< 0 && status
!= -ENOSPC
) {
1494 cl
= (struct ocfs2_chain_list
*) &fe
->id2
.i_chain
;
1496 victim
= ocfs2_find_victim_chain(cl
);
1497 ac
->ac_chain
= victim
;
1498 ac
->ac_allow_chain_relink
= 1;
1500 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
, bit_off
,
1501 num_bits
, bg_blkno
, &bits_left
);
1504 if (status
< 0 && status
!= -ENOSPC
) {
1509 mlog(0, "Search of victim chain %u came up with nothing, "
1510 "trying all chains now.\n", victim
);
1512 /* If we didn't pick a good victim, then just default to
1513 * searching each chain in order. Don't allow chain relinking
1514 * because we only calculate enough journal credits for one
1515 * relink per alloc. */
1516 ac
->ac_allow_chain_relink
= 0;
1517 for (i
= 0; i
< le16_to_cpu(cl
->cl_next_free_rec
); i
++) {
1520 if (!cl
->cl_recs
[i
].c_free
)
1524 status
= ocfs2_search_chain(ac
, handle
, bits_wanted
, min_bits
,
1525 bit_off
, num_bits
, bg_blkno
,
1529 if (status
< 0 && status
!= -ENOSPC
) {
1536 if (status
!= -ENOSPC
) {
1537 /* If the next search of this group is not likely to
1538 * yield a suitable extent, then we reset the last
1539 * group hint so as to not waste a disk read */
1540 if (bits_left
< min_bits
)
1541 ac
->ac_last_group
= 0;
1543 ac
->ac_last_group
= *bg_blkno
;
1551 int ocfs2_claim_metadata(struct ocfs2_super
*osb
,
1553 struct ocfs2_alloc_context
*ac
,
1555 u16
*suballoc_bit_start
,
1556 unsigned int *num_bits
,
1563 BUG_ON(ac
->ac_bits_wanted
< (ac
->ac_bits_given
+ bits_wanted
));
1564 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_META
);
1566 status
= ocfs2_claim_suballoc_bits(osb
,
1578 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1580 *blkno_start
= bg_blkno
+ (u64
) *suballoc_bit_start
;
1581 ac
->ac_bits_given
+= (*num_bits
);
1588 int ocfs2_claim_new_inode(struct ocfs2_super
*osb
,
1590 struct ocfs2_alloc_context
*ac
,
1595 unsigned int num_bits
;
1601 BUG_ON(ac
->ac_bits_given
!= 0);
1602 BUG_ON(ac
->ac_bits_wanted
!= 1);
1603 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_INODE
);
1605 status
= ocfs2_claim_suballoc_bits(osb
,
1617 atomic_inc(&osb
->alloc_stats
.bg_allocs
);
1619 BUG_ON(num_bits
!= 1);
1621 *fe_blkno
= bg_blkno
+ (u64
) (*suballoc_bit
);
1622 ac
->ac_bits_given
++;
1629 /* translate a group desc. blkno and it's bitmap offset into
1630 * disk cluster offset. */
1631 static inline u32
ocfs2_desc_bitmap_to_cluster_off(struct inode
*inode
,
1635 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1638 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1640 if (bg_blkno
!= osb
->first_cluster_group_blkno
)
1641 cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, bg_blkno
);
1642 cluster
+= (u32
) bg_bit_off
;
1646 /* given a cluster offset, calculate which block group it belongs to
1647 * and return that block offset. */
1648 u64
ocfs2_which_cluster_group(struct inode
*inode
, u32 cluster
)
1650 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1653 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1655 group_no
= cluster
/ osb
->bitmap_cpg
;
1657 return osb
->first_cluster_group_blkno
;
1658 return ocfs2_clusters_to_blocks(inode
->i_sb
,
1659 group_no
* osb
->bitmap_cpg
);
1662 /* given the block number of a cluster start, calculate which cluster
1663 * group and descriptor bitmap offset that corresponds to. */
1664 static inline void ocfs2_block_to_cluster_group(struct inode
*inode
,
1669 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1670 u32 data_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, data_blkno
);
1672 BUG_ON(!ocfs2_is_cluster_bitmap(inode
));
1674 *bg_blkno
= ocfs2_which_cluster_group(inode
,
1677 if (*bg_blkno
== osb
->first_cluster_group_blkno
)
1678 *bg_bit_off
= (u16
) data_cluster
;
1680 *bg_bit_off
= (u16
) ocfs2_blocks_to_clusters(osb
->sb
,
1681 data_blkno
- *bg_blkno
);
1685 * min_bits - minimum contiguous chunk from this total allocation we
1686 * can handle. set to what we asked for originally for a full
1687 * contig. allocation, set to '1' to indicate we can deal with extents
1690 int __ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1692 struct ocfs2_alloc_context
*ac
,
1699 unsigned int bits_wanted
= max_clusters
;
1705 BUG_ON(ac
->ac_bits_given
>= ac
->ac_bits_wanted
);
1707 BUG_ON(ac
->ac_which
!= OCFS2_AC_USE_LOCAL
1708 && ac
->ac_which
!= OCFS2_AC_USE_MAIN
);
1710 if (ac
->ac_which
== OCFS2_AC_USE_LOCAL
) {
1711 status
= ocfs2_claim_local_alloc_bits(osb
,
1718 atomic_inc(&osb
->alloc_stats
.local_data
);
1720 if (min_clusters
> (osb
->bitmap_cpg
- 1)) {
1721 /* The only paths asking for contiguousness
1722 * should know about this already. */
1723 mlog(ML_ERROR
, "minimum allocation requested %u exceeds "
1724 "group bitmap size %u!\n", min_clusters
,
1729 /* clamp the current request down to a realistic size. */
1730 if (bits_wanted
> (osb
->bitmap_cpg
- 1))
1731 bits_wanted
= osb
->bitmap_cpg
- 1;
1733 status
= ocfs2_claim_suballoc_bits(osb
,
1743 ocfs2_desc_bitmap_to_cluster_off(ac
->ac_inode
,
1746 atomic_inc(&osb
->alloc_stats
.bitmap_data
);
1750 if (status
!= -ENOSPC
)
1755 ac
->ac_bits_given
+= *num_clusters
;
1762 int ocfs2_claim_clusters(struct ocfs2_super
*osb
,
1764 struct ocfs2_alloc_context
*ac
,
1769 unsigned int bits_wanted
= ac
->ac_bits_wanted
- ac
->ac_bits_given
;
1771 return __ocfs2_claim_clusters(osb
, handle
, ac
, min_clusters
,
1772 bits_wanted
, cluster_start
, num_clusters
);
1775 static inline int ocfs2_block_group_clear_bits(handle_t
*handle
,
1776 struct inode
*alloc_inode
,
1777 struct ocfs2_group_desc
*bg
,
1778 struct buffer_head
*group_bh
,
1779 unsigned int bit_off
,
1780 unsigned int num_bits
)
1784 int journal_type
= OCFS2_JOURNAL_ACCESS_WRITE
;
1785 struct ocfs2_group_desc
*undo_bg
= NULL
;
1789 /* The caller got this descriptor from
1790 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1791 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg
));
1793 mlog(0, "off = %u, num = %u\n", bit_off
, num_bits
);
1795 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1796 journal_type
= OCFS2_JOURNAL_ACCESS_UNDO
;
1798 status
= ocfs2_journal_access(handle
, alloc_inode
, group_bh
,
1805 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1806 undo_bg
= (struct ocfs2_group_desc
*) bh2jh(group_bh
)->b_committed_data
;
1810 ocfs2_clear_bit((bit_off
+ tmp
),
1811 (unsigned long *) bg
->bg_bitmap
);
1812 if (ocfs2_is_cluster_bitmap(alloc_inode
))
1813 ocfs2_set_bit(bit_off
+ tmp
,
1814 (unsigned long *) undo_bg
->bg_bitmap
);
1816 le16_add_cpu(&bg
->bg_free_bits_count
, num_bits
);
1818 status
= ocfs2_journal_dirty(handle
, group_bh
);
1826 * expects the suballoc inode to already be locked.
1828 int ocfs2_free_suballoc_bits(handle_t
*handle
,
1829 struct inode
*alloc_inode
,
1830 struct buffer_head
*alloc_bh
,
1831 unsigned int start_bit
,
1837 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) alloc_bh
->b_data
;
1838 struct ocfs2_chain_list
*cl
= &fe
->id2
.i_chain
;
1839 struct buffer_head
*group_bh
= NULL
;
1840 struct ocfs2_group_desc
*group
;
1844 /* The alloc_bh comes from ocfs2_free_dinode() or
1845 * ocfs2_free_clusters(). The callers have all locked the
1846 * allocator and gotten alloc_bh from the lock call. This
1847 * validates the dinode buffer. Any corruption that has happended
1849 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
1850 BUG_ON((count
+ start_bit
) > ocfs2_bits_per_group(cl
));
1852 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1853 (unsigned long long)OCFS2_I(alloc_inode
)->ip_blkno
, count
,
1854 (unsigned long long)bg_blkno
, start_bit
);
1856 status
= ocfs2_read_group_descriptor(alloc_inode
, fe
, bg_blkno
,
1862 group
= (struct ocfs2_group_desc
*) group_bh
->b_data
;
1864 BUG_ON((count
+ start_bit
) > le16_to_cpu(group
->bg_bits
));
1866 status
= ocfs2_block_group_clear_bits(handle
, alloc_inode
,
1874 status
= ocfs2_journal_access(handle
, alloc_inode
, alloc_bh
,
1875 OCFS2_JOURNAL_ACCESS_WRITE
);
1881 le32_add_cpu(&cl
->cl_recs
[le16_to_cpu(group
->bg_chain
)].c_free
,
1883 tmp_used
= le32_to_cpu(fe
->id1
.bitmap1
.i_used
);
1884 fe
->id1
.bitmap1
.i_used
= cpu_to_le32(tmp_used
- count
);
1886 status
= ocfs2_journal_dirty(handle
, alloc_bh
);
1899 int ocfs2_free_dinode(handle_t
*handle
,
1900 struct inode
*inode_alloc_inode
,
1901 struct buffer_head
*inode_alloc_bh
,
1902 struct ocfs2_dinode
*di
)
1904 u64 blk
= le64_to_cpu(di
->i_blkno
);
1905 u16 bit
= le16_to_cpu(di
->i_suballoc_bit
);
1906 u64 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
1908 return ocfs2_free_suballoc_bits(handle
, inode_alloc_inode
,
1909 inode_alloc_bh
, bit
, bg_blkno
, 1);
1912 int ocfs2_free_clusters(handle_t
*handle
,
1913 struct inode
*bitmap_inode
,
1914 struct buffer_head
*bitmap_bh
,
1916 unsigned int num_clusters
)
1921 struct ocfs2_dinode
*fe
;
1923 /* You can't ever have a contiguous set of clusters
1924 * bigger than a block group bitmap so we never have to worry
1925 * about looping on them. */
1929 /* This is expensive. We can safely remove once this stuff has
1930 * gotten tested really well. */
1931 BUG_ON(start_blk
!= ocfs2_clusters_to_blocks(bitmap_inode
->i_sb
, ocfs2_blocks_to_clusters(bitmap_inode
->i_sb
, start_blk
)));
1933 fe
= (struct ocfs2_dinode
*) bitmap_bh
->b_data
;
1935 ocfs2_block_to_cluster_group(bitmap_inode
, start_blk
, &bg_blkno
,
1938 mlog(0, "want to free %u clusters starting at block %llu\n",
1939 num_clusters
, (unsigned long long)start_blk
);
1940 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1941 (unsigned long long)bg_blkno
, bg_start_bit
);
1943 status
= ocfs2_free_suballoc_bits(handle
, bitmap_inode
, bitmap_bh
,
1944 bg_start_bit
, bg_blkno
,
1951 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode
->i_sb
),
1959 static inline void ocfs2_debug_bg(struct ocfs2_group_desc
*bg
)
1961 printk("Block Group:\n");
1962 printk("bg_signature: %s\n", bg
->bg_signature
);
1963 printk("bg_size: %u\n", bg
->bg_size
);
1964 printk("bg_bits: %u\n", bg
->bg_bits
);
1965 printk("bg_free_bits_count: %u\n", bg
->bg_free_bits_count
);
1966 printk("bg_chain: %u\n", bg
->bg_chain
);
1967 printk("bg_generation: %u\n", le32_to_cpu(bg
->bg_generation
));
1968 printk("bg_next_group: %llu\n",
1969 (unsigned long long)bg
->bg_next_group
);
1970 printk("bg_parent_dinode: %llu\n",
1971 (unsigned long long)bg
->bg_parent_dinode
);
1972 printk("bg_blkno: %llu\n",
1973 (unsigned long long)bg
->bg_blkno
);
1976 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode
*fe
)
1980 printk("Suballoc Inode %llu:\n", (unsigned long long)fe
->i_blkno
);
1981 printk("i_signature: %s\n", fe
->i_signature
);
1982 printk("i_size: %llu\n",
1983 (unsigned long long)fe
->i_size
);
1984 printk("i_clusters: %u\n", fe
->i_clusters
);
1985 printk("i_generation: %u\n",
1986 le32_to_cpu(fe
->i_generation
));
1987 printk("id1.bitmap1.i_used: %u\n",
1988 le32_to_cpu(fe
->id1
.bitmap1
.i_used
));
1989 printk("id1.bitmap1.i_total: %u\n",
1990 le32_to_cpu(fe
->id1
.bitmap1
.i_total
));
1991 printk("id2.i_chain.cl_cpg: %u\n", fe
->id2
.i_chain
.cl_cpg
);
1992 printk("id2.i_chain.cl_bpc: %u\n", fe
->id2
.i_chain
.cl_bpc
);
1993 printk("id2.i_chain.cl_count: %u\n", fe
->id2
.i_chain
.cl_count
);
1994 printk("id2.i_chain.cl_next_free_rec: %u\n",
1995 fe
->id2
.i_chain
.cl_next_free_rec
);
1996 for(i
= 0; i
< fe
->id2
.i_chain
.cl_next_free_rec
; i
++) {
1997 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i
,
1998 fe
->id2
.i_chain
.cl_recs
[i
].c_free
);
1999 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i
,
2000 fe
->id2
.i_chain
.cl_recs
[i
].c_total
);
2001 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i
,
2002 (unsigned long long)fe
->id2
.i_chain
.cl_recs
[i
].c_blkno
);
2007 * For a given allocation, determine which allocators will need to be
2008 * accessed, and lock them, reserving the appropriate number of bits.
2010 * Sparse file systems call this from ocfs2_write_begin_nolock()
2011 * and ocfs2_allocate_unwritten_extents().
2013 * File systems which don't support holes call this from
2014 * ocfs2_extend_allocation().
2016 int ocfs2_lock_allocators(struct inode
*inode
,
2017 struct ocfs2_extent_tree
*et
,
2018 u32 clusters_to_add
, u32 extents_to_split
,
2019 struct ocfs2_alloc_context
**data_ac
,
2020 struct ocfs2_alloc_context
**meta_ac
)
2022 int ret
= 0, num_free_extents
;
2023 unsigned int max_recs_needed
= clusters_to_add
+ 2 * extents_to_split
;
2024 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2030 BUG_ON(clusters_to_add
!= 0 && data_ac
== NULL
);
2032 num_free_extents
= ocfs2_num_free_extents(osb
, inode
, et
);
2033 if (num_free_extents
< 0) {
2034 ret
= num_free_extents
;
2040 * Sparse allocation file systems need to be more conservative
2041 * with reserving room for expansion - the actual allocation
2042 * happens while we've got a journal handle open so re-taking
2043 * a cluster lock (because we ran out of room for another
2044 * extent) will violate ordering rules.
2046 * Most of the time we'll only be seeing this 1 cluster at a time
2049 * Always lock for any unwritten extents - we might want to
2050 * add blocks during a split.
2052 if (!num_free_extents
||
2053 (ocfs2_sparse_alloc(osb
) && num_free_extents
< max_recs_needed
)) {
2054 ret
= ocfs2_reserve_new_metadata(osb
, et
->et_root_el
, meta_ac
);
2062 if (clusters_to_add
== 0)
2065 ret
= ocfs2_reserve_clusters(osb
, clusters_to_add
, data_ac
);
2075 ocfs2_free_alloc_context(*meta_ac
);
2080 * We cannot have an error and a non null *data_ac.