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>
38 #include "blockcheck.h"
42 #include "localalloc.h"
48 #include "buffer_head_io.h"
50 #define NOT_ALLOC_NEW_GROUP 0
51 #define ALLOC_NEW_GROUP 1
53 #define OCFS2_MAX_INODES_TO_STEAL 1024
55 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
56 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
57 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
58 static int ocfs2_block_group_fill(handle_t *handle,
59 struct inode *alloc_inode,
60 struct buffer_head *bg_bh,
63 struct ocfs2_chain_list *cl);
64 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
65 struct inode *alloc_inode,
66 struct buffer_head *bh,
69 static int ocfs2_cluster_group_search(struct inode *inode,
70 struct buffer_head *group_bh,
71 u32 bits_wanted, u32 min_bits,
73 u16 *bit_off, u16 *bits_found);
74 static int ocfs2_block_group_search(struct inode *inode,
75 struct buffer_head *group_bh,
76 u32 bits_wanted, u32 min_bits,
78 u16 *bit_off, u16 *bits_found);
79 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
80 struct ocfs2_alloc_context *ac,
85 unsigned int *num_bits,
87 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
89 static inline int ocfs2_block_group_set_bits(handle_t *handle,
90 struct inode *alloc_inode,
91 struct ocfs2_group_desc *bg,
92 struct buffer_head *group_bh,
94 unsigned int num_bits);
95 static inline int ocfs2_block_group_clear_bits(handle_t *handle,
96 struct inode *alloc_inode,
97 struct ocfs2_group_desc *bg,
98 struct buffer_head *group_bh,
100 unsigned int num_bits);
102 static int ocfs2_relink_block_group(handle_t *handle,
103 struct inode *alloc_inode,
104 struct buffer_head *fe_bh,
105 struct buffer_head *bg_bh,
106 struct buffer_head *prev_bg_bh,
108 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
110 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
113 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
117 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
118 u32 bits_wanted, u64 max_block,
119 struct ocfs2_alloc_context **ac);
121 void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
123 struct inode *inode = ac->ac_inode;
126 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
127 ocfs2_inode_unlock(inode, 1);
129 mutex_unlock(&inode->i_mutex);
138 void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
140 ocfs2_free_ac_resource(ac);
144 static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
146 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
149 #define do_error(fmt, ...) \
152 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
154 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
157 static int ocfs2_validate_gd_self(struct super_block *sb,
158 struct buffer_head *bh,
161 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
163 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
164 do_error("Group descriptor #%llu has bad signature %.*s",
165 (unsigned long long)bh->b_blocknr, 7,
170 if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
171 do_error("Group descriptor #%llu has an invalid bg_blkno "
173 (unsigned long long)bh->b_blocknr,
174 (unsigned long long)le64_to_cpu(gd->bg_blkno));
178 if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
179 do_error("Group descriptor #%llu has an invalid "
180 "fs_generation of #%u",
181 (unsigned long long)bh->b_blocknr,
182 le32_to_cpu(gd->bg_generation));
186 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
187 do_error("Group descriptor #%llu has bit count %u but "
188 "claims that %u are free",
189 (unsigned long long)bh->b_blocknr,
190 le16_to_cpu(gd->bg_bits),
191 le16_to_cpu(gd->bg_free_bits_count));
195 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
196 do_error("Group descriptor #%llu has bit count %u but "
197 "max bitmap bits of %u",
198 (unsigned long long)bh->b_blocknr,
199 le16_to_cpu(gd->bg_bits),
200 8 * le16_to_cpu(gd->bg_size));
207 static int ocfs2_validate_gd_parent(struct super_block *sb,
208 struct ocfs2_dinode *di,
209 struct buffer_head *bh,
212 unsigned int max_bits;
213 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
215 if (di->i_blkno != gd->bg_parent_dinode) {
216 do_error("Group descriptor #%llu has bad parent "
217 "pointer (%llu, expected %llu)",
218 (unsigned long long)bh->b_blocknr,
219 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
220 (unsigned long long)le64_to_cpu(di->i_blkno));
224 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
225 if (le16_to_cpu(gd->bg_bits) > max_bits) {
226 do_error("Group descriptor #%llu has bit count of %u",
227 (unsigned long long)bh->b_blocknr,
228 le16_to_cpu(gd->bg_bits));
232 if (le16_to_cpu(gd->bg_chain) >=
233 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
234 do_error("Group descriptor #%llu has bad chain %u",
235 (unsigned long long)bh->b_blocknr,
236 le16_to_cpu(gd->bg_chain));
246 * This version only prints errors. It does not fail the filesystem, and
247 * exists only for resize.
249 int ocfs2_check_group_descriptor(struct super_block *sb,
250 struct ocfs2_dinode *di,
251 struct buffer_head *bh)
254 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
256 BUG_ON(!buffer_uptodate(bh));
259 * If the ecc fails, we return the error but otherwise
260 * leave the filesystem running. We know any error is
261 * local to this block.
263 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
266 "Checksum failed for group descriptor %llu\n",
267 (unsigned long long)bh->b_blocknr);
269 rc = ocfs2_validate_gd_self(sb, bh, 1);
271 rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
276 static int ocfs2_validate_group_descriptor(struct super_block *sb,
277 struct buffer_head *bh)
280 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
282 mlog(0, "Validating group descriptor %llu\n",
283 (unsigned long long)bh->b_blocknr);
285 BUG_ON(!buffer_uptodate(bh));
288 * If the ecc fails, we return the error but otherwise
289 * leave the filesystem running. We know any error is
290 * local to this block.
292 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
297 * Errors after here are fatal.
300 return ocfs2_validate_gd_self(sb, bh, 0);
303 int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
304 u64 gd_blkno, struct buffer_head **bh)
307 struct buffer_head *tmp = *bh;
309 rc = ocfs2_read_block(inode, gd_blkno, &tmp,
310 ocfs2_validate_group_descriptor);
314 rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
320 /* If ocfs2_read_block() got us a new bh, pass it up. */
328 static int ocfs2_block_group_fill(handle_t *handle,
329 struct inode *alloc_inode,
330 struct buffer_head *bg_bh,
333 struct ocfs2_chain_list *cl)
336 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
337 struct super_block * sb = alloc_inode->i_sb;
341 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
342 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
344 (unsigned long long)group_blkno,
345 (unsigned long long) bg_bh->b_blocknr);
350 status = ocfs2_journal_access_gd(handle,
353 OCFS2_JOURNAL_ACCESS_CREATE);
359 memset(bg, 0, sb->s_blocksize);
360 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
361 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
362 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
363 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
364 bg->bg_chain = cpu_to_le16(my_chain);
365 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
366 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
367 bg->bg_blkno = cpu_to_le64(group_blkno);
368 /* set the 1st bit in the bitmap to account for the descriptor block */
369 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
370 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
372 status = ocfs2_journal_dirty(handle, bg_bh);
376 /* There is no need to zero out or otherwise initialize the
377 * other blocks in a group - All valid FS metadata in a block
378 * group stores the superblock fs_generation value at
379 * allocation time. */
386 static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
391 while (curr < le16_to_cpu(cl->cl_count)) {
392 if (le32_to_cpu(cl->cl_recs[best].c_total) >
393 le32_to_cpu(cl->cl_recs[curr].c_total))
401 * We expect the block group allocator to already be locked.
403 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
404 struct inode *alloc_inode,
405 struct buffer_head *bh,
409 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
410 struct ocfs2_chain_list *cl;
411 struct ocfs2_alloc_context *ac = NULL;
412 handle_t *handle = NULL;
413 u32 bit_off, num_bits;
416 struct buffer_head *bg_bh = NULL;
417 struct ocfs2_group_desc *bg;
419 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
423 cl = &fe->id2.i_chain;
424 status = ocfs2_reserve_clusters_with_limit(osb,
425 le16_to_cpu(cl->cl_cpg),
428 if (status != -ENOSPC)
433 credits = ocfs2_calc_group_alloc_credits(osb->sb,
434 le16_to_cpu(cl->cl_cpg));
435 handle = ocfs2_start_trans(osb, credits);
436 if (IS_ERR(handle)) {
437 status = PTR_ERR(handle);
443 status = ocfs2_claim_clusters(osb,
446 le16_to_cpu(cl->cl_cpg),
450 if (status != -ENOSPC)
455 alloc_rec = ocfs2_find_smallest_chain(cl);
457 /* setup the group */
458 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
459 mlog(0, "new descriptor, record %u, at block %llu\n",
460 alloc_rec, (unsigned long long)bg_blkno);
462 bg_bh = sb_getblk(osb->sb, bg_blkno);
468 ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
470 status = ocfs2_block_group_fill(handle,
481 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
483 status = ocfs2_journal_access_di(handle, alloc_inode,
484 bh, OCFS2_JOURNAL_ACCESS_WRITE);
490 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
491 le16_to_cpu(bg->bg_free_bits_count));
492 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
493 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno);
494 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
495 le16_add_cpu(&cl->cl_next_free_rec, 1);
497 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
498 le16_to_cpu(bg->bg_free_bits_count));
499 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
500 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
502 status = ocfs2_journal_dirty(handle, bh);
508 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
509 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
510 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
511 le32_to_cpu(fe->i_clusters)));
512 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
513 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
514 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
519 ocfs2_commit_trans(osb, handle);
522 ocfs2_free_alloc_context(ac);
530 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
531 struct ocfs2_alloc_context *ac,
537 u32 bits_wanted = ac->ac_bits_wanted;
538 struct inode *alloc_inode;
539 struct buffer_head *bh = NULL;
540 struct ocfs2_dinode *fe;
545 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
551 mutex_lock(&alloc_inode->i_mutex);
553 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
555 mutex_unlock(&alloc_inode->i_mutex);
562 ac->ac_inode = alloc_inode;
563 ac->ac_alloc_slot = slot;
565 fe = (struct ocfs2_dinode *) bh->b_data;
567 /* The bh was validated by the inode read inside
568 * ocfs2_inode_lock(). Any corruption is a code bug. */
569 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
571 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
572 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
573 (unsigned long long)le64_to_cpu(fe->i_blkno));
578 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
579 le32_to_cpu(fe->id1.bitmap1.i_used);
581 if (bits_wanted > free_bits) {
582 /* cluster bitmap never grows */
583 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
584 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
585 bits_wanted, free_bits);
590 if (alloc_new_group != ALLOC_NEW_GROUP) {
591 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
592 "and we don't alloc a new group for it.\n",
593 slot, bits_wanted, free_bits);
598 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
601 if (status != -ENOSPC)
605 atomic_inc(&osb->alloc_stats.bg_extends);
607 /* You should never ask for this much metadata */
609 (le32_to_cpu(fe->id1.bitmap1.i_total)
610 - le32_to_cpu(fe->id1.bitmap1.i_used)));
622 int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
624 struct ocfs2_alloc_context **ac)
629 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
636 (*ac)->ac_bits_wanted = blocks;
637 (*ac)->ac_which = OCFS2_AC_USE_META;
638 slot = osb->slot_num;
639 (*ac)->ac_group_search = ocfs2_block_group_search;
641 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
642 EXTENT_ALLOC_SYSTEM_INODE,
643 slot, ALLOC_NEW_GROUP);
645 if (status != -ENOSPC)
652 if ((status < 0) && *ac) {
653 ocfs2_free_alloc_context(*ac);
661 int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
662 struct ocfs2_extent_list *root_el,
663 struct ocfs2_alloc_context **ac)
665 return ocfs2_reserve_new_metadata_blocks(osb,
666 ocfs2_extend_meta_needed(root_el),
670 static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
671 struct ocfs2_alloc_context *ac)
673 int i, status = -ENOSPC;
674 s16 slot = ocfs2_get_inode_steal_slot(osb);
676 /* Start to steal inodes from the first slot after ours. */
677 if (slot == OCFS2_INVALID_SLOT)
678 slot = osb->slot_num + 1;
680 for (i = 0; i < osb->max_slots; i++, slot++) {
681 if (slot == osb->max_slots)
684 if (slot == osb->slot_num)
687 status = ocfs2_reserve_suballoc_bits(osb, ac,
688 INODE_ALLOC_SYSTEM_INODE,
689 slot, NOT_ALLOC_NEW_GROUP);
691 ocfs2_set_inode_steal_slot(osb, slot);
695 ocfs2_free_ac_resource(ac);
701 int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
702 struct ocfs2_alloc_context **ac)
705 s16 slot = ocfs2_get_inode_steal_slot(osb);
707 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
714 (*ac)->ac_bits_wanted = 1;
715 (*ac)->ac_which = OCFS2_AC_USE_INODE;
717 (*ac)->ac_group_search = ocfs2_block_group_search;
720 * stat(2) can't handle i_ino > 32bits, so we tell the
721 * lower levels not to allocate us a block group past that
722 * limit. The 'inode64' mount option avoids this behavior.
724 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
725 (*ac)->ac_max_block = (u32)~0U;
728 * slot is set when we successfully steal inode from other nodes.
729 * It is reset in 3 places:
730 * 1. when we flush the truncate log
731 * 2. when we complete local alloc recovery.
732 * 3. when we successfully allocate from our own slot.
733 * After it is set, we will go on stealing inodes until we find the
734 * need to check our slots to see whether there is some space for us.
736 if (slot != OCFS2_INVALID_SLOT &&
737 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_INODES_TO_STEAL)
740 atomic_set(&osb->s_num_inodes_stolen, 0);
741 status = ocfs2_reserve_suballoc_bits(osb, *ac,
742 INODE_ALLOC_SYSTEM_INODE,
743 osb->slot_num, ALLOC_NEW_GROUP);
748 * Some inodes must be freed by us, so try to allocate
749 * from our own next time.
751 if (slot != OCFS2_INVALID_SLOT)
752 ocfs2_init_inode_steal_slot(osb);
754 } else if (status < 0 && status != -ENOSPC) {
759 ocfs2_free_ac_resource(*ac);
762 status = ocfs2_steal_inode_from_other_nodes(osb, *ac);
763 atomic_inc(&osb->s_num_inodes_stolen);
765 if (status != -ENOSPC)
772 if ((status < 0) && *ac) {
773 ocfs2_free_alloc_context(*ac);
781 /* local alloc code has to do the same thing, so rather than do this
783 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
784 struct ocfs2_alloc_context *ac)
788 ac->ac_which = OCFS2_AC_USE_MAIN;
789 ac->ac_group_search = ocfs2_cluster_group_search;
791 status = ocfs2_reserve_suballoc_bits(osb, ac,
792 GLOBAL_BITMAP_SYSTEM_INODE,
795 if (status < 0 && status != -ENOSPC) {
804 /* Callers don't need to care which bitmap (local alloc or main) to
805 * use so we figure it out for them, but unfortunately this clutters
807 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
808 u32 bits_wanted, u64 max_block,
809 struct ocfs2_alloc_context **ac)
815 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
822 (*ac)->ac_bits_wanted = bits_wanted;
823 (*ac)->ac_max_block = max_block;
826 if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
827 status = ocfs2_reserve_local_alloc_bits(osb,
830 if (status == -EFBIG) {
831 /* The local alloc window is outside ac_max_block.
832 * use the main bitmap. */
834 } else if ((status < 0) && (status != -ENOSPC)) {
840 if (status == -ENOSPC) {
841 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
843 if (status != -ENOSPC)
851 if ((status < 0) && *ac) {
852 ocfs2_free_alloc_context(*ac);
860 int ocfs2_reserve_clusters(struct ocfs2_super *osb,
862 struct ocfs2_alloc_context **ac)
864 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0, ac);
868 * More or less lifted from ext3. I'll leave their description below:
870 * "For ext3 allocations, we must not reuse any blocks which are
871 * allocated in the bitmap buffer's "last committed data" copy. This
872 * prevents deletes from freeing up the page for reuse until we have
873 * committed the delete transaction.
875 * If we didn't do this, then deleting something and reallocating it as
876 * data would allow the old block to be overwritten before the
877 * transaction committed (because we force data to disk before commit).
878 * This would lead to corruption if we crashed between overwriting the
879 * data and committing the delete.
881 * @@@ We may want to make this allocation behaviour conditional on
882 * data-writes at some point, and disable it for metadata allocations or
885 * Note: OCFS2 already does this differently for metadata vs data
886 * allocations, as those bitmaps are separate and undo access is never
887 * called on a metadata group descriptor.
889 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
892 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
894 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
896 if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
899 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
900 return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
903 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
904 struct buffer_head *bg_bh,
905 unsigned int bits_wanted,
906 unsigned int total_bits,
911 u16 best_offset, best_size;
912 int offset, start, found, status = 0;
913 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
915 /* Callers got this descriptor from
916 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
917 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
919 found = start = best_offset = best_size = 0;
920 bitmap = bg->bg_bitmap;
922 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
923 if (offset == total_bits)
926 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
927 /* We found a zero, but we can't use it as it
928 * hasn't been put to disk yet! */
931 } else if (offset == start) {
932 /* we found a zero */
934 /* move start to the next bit to test */
937 /* got a zero after some ones */
941 if (found > best_size) {
943 best_offset = start - found;
945 /* we got everything we needed */
946 if (found == bits_wanted) {
947 /* mlog(0, "Found it all!\n"); */
952 /* XXX: I think the first clause is equivalent to the second
954 if (found == bits_wanted) {
955 *bit_off = start - found;
957 } else if (best_size) {
958 *bit_off = best_offset;
959 *bits_found = best_size;
962 /* No error log here -- see the comment above
963 * ocfs2_test_bg_bit_allocatable */
969 static inline int ocfs2_block_group_set_bits(handle_t *handle,
970 struct inode *alloc_inode,
971 struct ocfs2_group_desc *bg,
972 struct buffer_head *group_bh,
973 unsigned int bit_off,
974 unsigned int num_bits)
977 void *bitmap = bg->bg_bitmap;
978 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
982 /* All callers get the descriptor via
983 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
984 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
985 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
987 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
990 if (ocfs2_is_cluster_bitmap(alloc_inode))
991 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
993 status = ocfs2_journal_access_gd(handle,
1002 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
1005 ocfs2_set_bit(bit_off++, bitmap);
1007 status = ocfs2_journal_dirty(handle,
1019 /* find the one with the most empty bits */
1020 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1024 BUG_ON(!cl->cl_next_free_rec);
1027 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1028 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1029 le32_to_cpu(cl->cl_recs[best].c_free))
1034 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1038 static int ocfs2_relink_block_group(handle_t *handle,
1039 struct inode *alloc_inode,
1040 struct buffer_head *fe_bh,
1041 struct buffer_head *bg_bh,
1042 struct buffer_head *prev_bg_bh,
1046 /* there is a really tiny chance the journal calls could fail,
1047 * but we wouldn't want inconsistent blocks in *any* case. */
1048 u64 fe_ptr, bg_ptr, prev_bg_ptr;
1049 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1050 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1051 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1053 /* The caller got these descriptors from
1054 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1055 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1056 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
1058 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
1059 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1060 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1061 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
1063 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
1064 bg_ptr = le64_to_cpu(bg->bg_next_group);
1065 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1067 status = ocfs2_journal_access_gd(handle, alloc_inode, prev_bg_bh,
1068 OCFS2_JOURNAL_ACCESS_WRITE);
1074 prev_bg->bg_next_group = bg->bg_next_group;
1076 status = ocfs2_journal_dirty(handle, prev_bg_bh);
1082 status = ocfs2_journal_access_gd(handle, alloc_inode, bg_bh,
1083 OCFS2_JOURNAL_ACCESS_WRITE);
1089 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
1091 status = ocfs2_journal_dirty(handle, bg_bh);
1097 status = ocfs2_journal_access_di(handle, alloc_inode, fe_bh,
1098 OCFS2_JOURNAL_ACCESS_WRITE);
1104 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
1106 status = ocfs2_journal_dirty(handle, fe_bh);
1115 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1116 bg->bg_next_group = cpu_to_le64(bg_ptr);
1117 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1124 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1127 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1130 /* return 0 on success, -ENOSPC to keep searching and any other < 0
1131 * value on error. */
1132 static int ocfs2_cluster_group_search(struct inode *inode,
1133 struct buffer_head *group_bh,
1134 u32 bits_wanted, u32 min_bits,
1136 u16 *bit_off, u16 *bits_found)
1138 int search = -ENOSPC;
1141 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
1142 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1143 u16 tmp_off, tmp_found;
1144 unsigned int max_bits, gd_cluster_off;
1146 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1148 if (gd->bg_free_bits_count) {
1149 max_bits = le16_to_cpu(gd->bg_bits);
1151 /* Tail groups in cluster bitmaps which aren't cpg
1152 * aligned are prone to partial extention by a failed
1153 * fs resize. If the file system resize never got to
1154 * update the dinode cluster count, then we don't want
1155 * to trust any clusters past it, regardless of what
1156 * the group descriptor says. */
1157 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1158 le64_to_cpu(gd->bg_blkno));
1159 if ((gd_cluster_off + max_bits) >
1160 OCFS2_I(inode)->ip_clusters) {
1161 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1162 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1163 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1164 le16_to_cpu(gd->bg_bits),
1165 OCFS2_I(inode)->ip_clusters, max_bits);
1168 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1169 group_bh, bits_wanted,
1171 &tmp_off, &tmp_found);
1176 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1178 tmp_off + tmp_found);
1179 mlog(0, "Checking %llu against %llu\n",
1180 (unsigned long long)blkoff,
1181 (unsigned long long)max_block);
1182 if (blkoff > max_block)
1186 /* ocfs2_block_group_find_clear_bits() might
1187 * return success, but we still want to return
1188 * -ENOSPC unless it found the minimum number
1190 if (min_bits <= tmp_found) {
1192 *bits_found = tmp_found;
1193 search = 0; /* success */
1194 } else if (tmp_found) {
1196 * Don't show bits which we'll be returning
1197 * for allocation to the local alloc bitmap.
1199 ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
1206 static int ocfs2_block_group_search(struct inode *inode,
1207 struct buffer_head *group_bh,
1208 u32 bits_wanted, u32 min_bits,
1210 u16 *bit_off, u16 *bits_found)
1214 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1216 BUG_ON(min_bits != 1);
1217 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1219 if (bg->bg_free_bits_count) {
1220 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1221 group_bh, bits_wanted,
1222 le16_to_cpu(bg->bg_bits),
1223 bit_off, bits_found);
1224 if (!ret && max_block) {
1225 blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
1227 mlog(0, "Checking %llu against %llu\n",
1228 (unsigned long long)blkoff,
1229 (unsigned long long)max_block);
1230 if (blkoff > max_block)
1238 static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
1240 struct buffer_head *di_bh,
1246 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1247 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1249 ret = ocfs2_journal_access_di(handle, inode, di_bh,
1250 OCFS2_JOURNAL_ACCESS_WRITE);
1256 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1257 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1258 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1260 ret = ocfs2_journal_dirty(handle, di_bh);
1268 static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1273 unsigned int *num_bits,
1279 struct buffer_head *group_bh = NULL;
1280 struct ocfs2_group_desc *gd;
1281 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1282 struct inode *alloc_inode = ac->ac_inode;
1284 ret = ocfs2_read_group_descriptor(alloc_inode, di, gd_blkno,
1291 gd = (struct ocfs2_group_desc *) group_bh->b_data;
1292 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1293 ac->ac_max_block, bit_off, &found);
1302 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1304 le16_to_cpu(gd->bg_chain));
1310 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1311 *bit_off, *num_bits);
1315 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1323 static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1328 unsigned int *num_bits,
1333 u16 chain, tmp_bits;
1336 struct inode *alloc_inode = ac->ac_inode;
1337 struct buffer_head *group_bh = NULL;
1338 struct buffer_head *prev_group_bh = NULL;
1339 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1340 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1341 struct ocfs2_group_desc *bg;
1343 chain = ac->ac_chain;
1344 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1346 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
1348 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1349 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1355 bg = (struct ocfs2_group_desc *) group_bh->b_data;
1358 /* for now, the chain search is a bit simplistic. We just use
1359 * the 1st group with any empty bits. */
1360 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1361 bits_wanted, min_bits,
1362 ac->ac_max_block, bit_off,
1363 &tmp_bits)) == -ENOSPC) {
1364 if (!bg->bg_next_group)
1367 brelse(prev_group_bh);
1368 prev_group_bh = NULL;
1370 next_group = le64_to_cpu(bg->bg_next_group);
1371 prev_group_bh = group_bh;
1373 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1374 next_group, &group_bh);
1379 bg = (struct ocfs2_group_desc *) group_bh->b_data;
1382 if (status != -ENOSPC)
1387 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1388 tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
1390 *num_bits = tmp_bits;
1392 BUG_ON(*num_bits == 0);
1395 * Keep track of previous block descriptor read. When
1396 * we find a target, if we have read more than X
1397 * number of descriptors, and the target is reasonably
1398 * empty, relink him to top of his chain.
1400 * We've read 0 extra blocks and only send one more to
1401 * the transaction, yet the next guy to search has a
1404 * Do this *after* figuring out how many bits we're taking out
1405 * of our target group.
1407 if (ac->ac_allow_chain_relink &&
1409 (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1410 status = ocfs2_relink_block_group(handle, alloc_inode,
1411 ac->ac_bh, group_bh,
1412 prev_group_bh, chain);
1419 /* Ok, claim our bits now: set the info on dinode, chainlist
1420 * and then the group */
1421 status = ocfs2_journal_access_di(handle,
1424 OCFS2_JOURNAL_ACCESS_WRITE);
1430 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1431 fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1432 le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1434 status = ocfs2_journal_dirty(handle,
1441 status = ocfs2_block_group_set_bits(handle,
1452 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
1453 (unsigned long long)le64_to_cpu(fe->i_blkno));
1455 *bg_blkno = le64_to_cpu(bg->bg_blkno);
1456 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
1459 brelse(prev_group_bh);
1465 /* will give out up to bits_wanted contiguous bits. */
1466 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1467 struct ocfs2_alloc_context *ac,
1472 unsigned int *num_bits,
1478 u64 hint_blkno = ac->ac_last_group;
1479 struct ocfs2_chain_list *cl;
1480 struct ocfs2_dinode *fe;
1484 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1485 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1488 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1490 /* The bh was validated by the inode read during
1491 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1492 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1494 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1495 le32_to_cpu(fe->id1.bitmap1.i_total)) {
1496 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1497 "bits but only %u total.",
1498 (unsigned long long)le64_to_cpu(fe->i_blkno),
1499 le32_to_cpu(fe->id1.bitmap1.i_used),
1500 le32_to_cpu(fe->id1.bitmap1.i_total));
1506 /* Attempt to short-circuit the usual search mechanism
1507 * by jumping straight to the most recently used
1508 * allocation group. This helps us mantain some
1509 * contiguousness across allocations. */
1510 status = ocfs2_search_one_group(ac, handle, bits_wanted,
1511 min_bits, bit_off, num_bits,
1512 hint_blkno, &bits_left);
1514 /* Be careful to update *bg_blkno here as the
1515 * caller is expecting it to be filled in, and
1516 * ocfs2_search_one_group() won't do that for
1518 *bg_blkno = hint_blkno;
1521 if (status < 0 && status != -ENOSPC) {
1527 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1529 victim = ocfs2_find_victim_chain(cl);
1530 ac->ac_chain = victim;
1531 ac->ac_allow_chain_relink = 1;
1533 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
1534 num_bits, bg_blkno, &bits_left);
1537 if (status < 0 && status != -ENOSPC) {
1542 mlog(0, "Search of victim chain %u came up with nothing, "
1543 "trying all chains now.\n", victim);
1545 /* If we didn't pick a good victim, then just default to
1546 * searching each chain in order. Don't allow chain relinking
1547 * because we only calculate enough journal credits for one
1548 * relink per alloc. */
1549 ac->ac_allow_chain_relink = 0;
1550 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1553 if (!cl->cl_recs[i].c_free)
1557 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1558 bit_off, num_bits, bg_blkno,
1562 if (status < 0 && status != -ENOSPC) {
1569 if (status != -ENOSPC) {
1570 /* If the next search of this group is not likely to
1571 * yield a suitable extent, then we reset the last
1572 * group hint so as to not waste a disk read */
1573 if (bits_left < min_bits)
1574 ac->ac_last_group = 0;
1576 ac->ac_last_group = *bg_blkno;
1584 int ocfs2_claim_metadata(struct ocfs2_super *osb,
1586 struct ocfs2_alloc_context *ac,
1588 u16 *suballoc_bit_start,
1589 unsigned int *num_bits,
1596 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1597 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
1599 status = ocfs2_claim_suballoc_bits(osb,
1611 atomic_inc(&osb->alloc_stats.bg_allocs);
1613 *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1614 ac->ac_bits_given += (*num_bits);
1621 int ocfs2_claim_new_inode(struct ocfs2_super *osb,
1623 struct ocfs2_alloc_context *ac,
1628 unsigned int num_bits;
1634 BUG_ON(ac->ac_bits_given != 0);
1635 BUG_ON(ac->ac_bits_wanted != 1);
1636 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
1638 status = ocfs2_claim_suballoc_bits(osb,
1650 atomic_inc(&osb->alloc_stats.bg_allocs);
1652 BUG_ON(num_bits != 1);
1654 *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1655 ac->ac_bits_given++;
1662 /* translate a group desc. blkno and it's bitmap offset into
1663 * disk cluster offset. */
1664 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1668 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1671 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1673 if (bg_blkno != osb->first_cluster_group_blkno)
1674 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1675 cluster += (u32) bg_bit_off;
1679 /* given a cluster offset, calculate which block group it belongs to
1680 * and return that block offset. */
1681 u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
1683 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1686 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1688 group_no = cluster / osb->bitmap_cpg;
1690 return osb->first_cluster_group_blkno;
1691 return ocfs2_clusters_to_blocks(inode->i_sb,
1692 group_no * osb->bitmap_cpg);
1695 /* given the block number of a cluster start, calculate which cluster
1696 * group and descriptor bitmap offset that corresponds to. */
1697 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1702 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1703 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1705 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1707 *bg_blkno = ocfs2_which_cluster_group(inode,
1710 if (*bg_blkno == osb->first_cluster_group_blkno)
1711 *bg_bit_off = (u16) data_cluster;
1713 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1714 data_blkno - *bg_blkno);
1718 * min_bits - minimum contiguous chunk from this total allocation we
1719 * can handle. set to what we asked for originally for a full
1720 * contig. allocation, set to '1' to indicate we can deal with extents
1723 int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1725 struct ocfs2_alloc_context *ac,
1732 unsigned int bits_wanted = max_clusters;
1738 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1740 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1741 && ac->ac_which != OCFS2_AC_USE_MAIN);
1743 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1744 status = ocfs2_claim_local_alloc_bits(osb,
1751 atomic_inc(&osb->alloc_stats.local_data);
1753 if (min_clusters > (osb->bitmap_cpg - 1)) {
1754 /* The only paths asking for contiguousness
1755 * should know about this already. */
1756 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
1757 "group bitmap size %u!\n", min_clusters,
1762 /* clamp the current request down to a realistic size. */
1763 if (bits_wanted > (osb->bitmap_cpg - 1))
1764 bits_wanted = osb->bitmap_cpg - 1;
1766 status = ocfs2_claim_suballoc_bits(osb,
1776 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1779 atomic_inc(&osb->alloc_stats.bitmap_data);
1783 if (status != -ENOSPC)
1788 ac->ac_bits_given += *num_clusters;
1795 int ocfs2_claim_clusters(struct ocfs2_super *osb,
1797 struct ocfs2_alloc_context *ac,
1802 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1804 return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1805 bits_wanted, cluster_start, num_clusters);
1808 static inline int ocfs2_block_group_clear_bits(handle_t *handle,
1809 struct inode *alloc_inode,
1810 struct ocfs2_group_desc *bg,
1811 struct buffer_head *group_bh,
1812 unsigned int bit_off,
1813 unsigned int num_bits)
1817 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1818 struct ocfs2_group_desc *undo_bg = NULL;
1822 /* The caller got this descriptor from
1823 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1824 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1826 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1828 if (ocfs2_is_cluster_bitmap(alloc_inode))
1829 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1831 status = ocfs2_journal_access_gd(handle, alloc_inode, group_bh,
1838 if (ocfs2_is_cluster_bitmap(alloc_inode))
1839 undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
1843 ocfs2_clear_bit((bit_off + tmp),
1844 (unsigned long *) bg->bg_bitmap);
1845 if (ocfs2_is_cluster_bitmap(alloc_inode))
1846 ocfs2_set_bit(bit_off + tmp,
1847 (unsigned long *) undo_bg->bg_bitmap);
1849 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1851 status = ocfs2_journal_dirty(handle, group_bh);
1859 * expects the suballoc inode to already be locked.
1861 int ocfs2_free_suballoc_bits(handle_t *handle,
1862 struct inode *alloc_inode,
1863 struct buffer_head *alloc_bh,
1864 unsigned int start_bit,
1870 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1871 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1872 struct buffer_head *group_bh = NULL;
1873 struct ocfs2_group_desc *group;
1877 /* The alloc_bh comes from ocfs2_free_dinode() or
1878 * ocfs2_free_clusters(). The callers have all locked the
1879 * allocator and gotten alloc_bh from the lock call. This
1880 * validates the dinode buffer. Any corruption that has happended
1882 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1883 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1885 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1886 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1887 (unsigned long long)bg_blkno, start_bit);
1889 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
1895 group = (struct ocfs2_group_desc *) group_bh->b_data;
1897 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1899 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1907 status = ocfs2_journal_access_di(handle, alloc_inode, alloc_bh,
1908 OCFS2_JOURNAL_ACCESS_WRITE);
1914 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
1916 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1917 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
1919 status = ocfs2_journal_dirty(handle, alloc_bh);
1932 int ocfs2_free_dinode(handle_t *handle,
1933 struct inode *inode_alloc_inode,
1934 struct buffer_head *inode_alloc_bh,
1935 struct ocfs2_dinode *di)
1937 u64 blk = le64_to_cpu(di->i_blkno);
1938 u16 bit = le16_to_cpu(di->i_suballoc_bit);
1939 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1941 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
1942 inode_alloc_bh, bit, bg_blkno, 1);
1945 int ocfs2_free_clusters(handle_t *handle,
1946 struct inode *bitmap_inode,
1947 struct buffer_head *bitmap_bh,
1949 unsigned int num_clusters)
1954 struct ocfs2_dinode *fe;
1956 /* You can't ever have a contiguous set of clusters
1957 * bigger than a block group bitmap so we never have to worry
1958 * about looping on them. */
1962 /* This is expensive. We can safely remove once this stuff has
1963 * gotten tested really well. */
1964 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
1966 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
1968 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
1971 mlog(0, "want to free %u clusters starting at block %llu\n",
1972 num_clusters, (unsigned long long)start_blk);
1973 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1974 (unsigned long long)bg_blkno, bg_start_bit);
1976 status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
1977 bg_start_bit, bg_blkno,
1984 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
1992 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
1994 printk("Block Group:\n");
1995 printk("bg_signature: %s\n", bg->bg_signature);
1996 printk("bg_size: %u\n", bg->bg_size);
1997 printk("bg_bits: %u\n", bg->bg_bits);
1998 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
1999 printk("bg_chain: %u\n", bg->bg_chain);
2000 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
2001 printk("bg_next_group: %llu\n",
2002 (unsigned long long)bg->bg_next_group);
2003 printk("bg_parent_dinode: %llu\n",
2004 (unsigned long long)bg->bg_parent_dinode);
2005 printk("bg_blkno: %llu\n",
2006 (unsigned long long)bg->bg_blkno);
2009 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2013 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
2014 printk("i_signature: %s\n", fe->i_signature);
2015 printk("i_size: %llu\n",
2016 (unsigned long long)fe->i_size);
2017 printk("i_clusters: %u\n", fe->i_clusters);
2018 printk("i_generation: %u\n",
2019 le32_to_cpu(fe->i_generation));
2020 printk("id1.bitmap1.i_used: %u\n",
2021 le32_to_cpu(fe->id1.bitmap1.i_used));
2022 printk("id1.bitmap1.i_total: %u\n",
2023 le32_to_cpu(fe->id1.bitmap1.i_total));
2024 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2025 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2026 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2027 printk("id2.i_chain.cl_next_free_rec: %u\n",
2028 fe->id2.i_chain.cl_next_free_rec);
2029 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2030 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2031 fe->id2.i_chain.cl_recs[i].c_free);
2032 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2033 fe->id2.i_chain.cl_recs[i].c_total);
2034 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2035 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
2040 * For a given allocation, determine which allocators will need to be
2041 * accessed, and lock them, reserving the appropriate number of bits.
2043 * Sparse file systems call this from ocfs2_write_begin_nolock()
2044 * and ocfs2_allocate_unwritten_extents().
2046 * File systems which don't support holes call this from
2047 * ocfs2_extend_allocation().
2049 int ocfs2_lock_allocators(struct inode *inode,
2050 struct ocfs2_extent_tree *et,
2051 u32 clusters_to_add, u32 extents_to_split,
2052 struct ocfs2_alloc_context **data_ac,
2053 struct ocfs2_alloc_context **meta_ac)
2055 int ret = 0, num_free_extents;
2056 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2057 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2063 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2065 num_free_extents = ocfs2_num_free_extents(osb, inode, et);
2066 if (num_free_extents < 0) {
2067 ret = num_free_extents;
2073 * Sparse allocation file systems need to be more conservative
2074 * with reserving room for expansion - the actual allocation
2075 * happens while we've got a journal handle open so re-taking
2076 * a cluster lock (because we ran out of room for another
2077 * extent) will violate ordering rules.
2079 * Most of the time we'll only be seeing this 1 cluster at a time
2082 * Always lock for any unwritten extents - we might want to
2083 * add blocks during a split.
2085 if (!num_free_extents ||
2086 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
2087 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
2095 if (clusters_to_add == 0)
2098 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2108 ocfs2_free_alloc_context(*meta_ac);
2113 * We cannot have an error and a non null *data_ac.