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);
265 rc = ocfs2_validate_gd_self(sb, bh, 1);
267 rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
272 static int ocfs2_validate_group_descriptor(struct super_block *sb,
273 struct buffer_head *bh)
276 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
278 mlog(0, "Validating group descriptor %llu\n",
279 (unsigned long long)bh->b_blocknr);
281 BUG_ON(!buffer_uptodate(bh));
284 * If the ecc fails, we return the error but otherwise
285 * leave the filesystem running. We know any error is
286 * local to this block.
288 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
293 * Errors after here are fatal.
296 return ocfs2_validate_gd_self(sb, bh, 0);
299 int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
300 u64 gd_blkno, struct buffer_head **bh)
303 struct buffer_head *tmp = *bh;
305 rc = ocfs2_read_block(inode, gd_blkno, &tmp,
306 ocfs2_validate_group_descriptor);
310 rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
316 /* If ocfs2_read_block() got us a new bh, pass it up. */
324 static int ocfs2_block_group_fill(handle_t *handle,
325 struct inode *alloc_inode,
326 struct buffer_head *bg_bh,
329 struct ocfs2_chain_list *cl)
332 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
333 struct super_block * sb = alloc_inode->i_sb;
337 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
338 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
340 (unsigned long long)group_blkno,
341 (unsigned long long) bg_bh->b_blocknr);
346 status = ocfs2_journal_access(handle,
349 OCFS2_JOURNAL_ACCESS_CREATE);
355 memset(bg, 0, sb->s_blocksize);
356 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
357 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
358 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
359 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
360 bg->bg_chain = cpu_to_le16(my_chain);
361 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
362 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
363 bg->bg_blkno = cpu_to_le64(group_blkno);
364 /* set the 1st bit in the bitmap to account for the descriptor block */
365 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
366 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
368 status = ocfs2_journal_dirty(handle, bg_bh);
372 /* There is no need to zero out or otherwise initialize the
373 * other blocks in a group - All valid FS metadata in a block
374 * group stores the superblock fs_generation value at
375 * allocation time. */
382 static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
387 while (curr < le16_to_cpu(cl->cl_count)) {
388 if (le32_to_cpu(cl->cl_recs[best].c_total) >
389 le32_to_cpu(cl->cl_recs[curr].c_total))
397 * We expect the block group allocator to already be locked.
399 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
400 struct inode *alloc_inode,
401 struct buffer_head *bh,
405 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
406 struct ocfs2_chain_list *cl;
407 struct ocfs2_alloc_context *ac = NULL;
408 handle_t *handle = NULL;
409 u32 bit_off, num_bits;
412 struct buffer_head *bg_bh = NULL;
413 struct ocfs2_group_desc *bg;
415 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
419 cl = &fe->id2.i_chain;
420 status = ocfs2_reserve_clusters_with_limit(osb,
421 le16_to_cpu(cl->cl_cpg),
424 if (status != -ENOSPC)
429 credits = ocfs2_calc_group_alloc_credits(osb->sb,
430 le16_to_cpu(cl->cl_cpg));
431 handle = ocfs2_start_trans(osb, credits);
432 if (IS_ERR(handle)) {
433 status = PTR_ERR(handle);
439 status = ocfs2_claim_clusters(osb,
442 le16_to_cpu(cl->cl_cpg),
446 if (status != -ENOSPC)
451 alloc_rec = ocfs2_find_smallest_chain(cl);
453 /* setup the group */
454 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
455 mlog(0, "new descriptor, record %u, at block %llu\n",
456 alloc_rec, (unsigned long long)bg_blkno);
458 bg_bh = sb_getblk(osb->sb, bg_blkno);
464 ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
466 status = ocfs2_block_group_fill(handle,
477 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
479 status = ocfs2_journal_access(handle, alloc_inode,
480 bh, OCFS2_JOURNAL_ACCESS_WRITE);
486 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
487 le16_to_cpu(bg->bg_free_bits_count));
488 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
489 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno);
490 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
491 le16_add_cpu(&cl->cl_next_free_rec, 1);
493 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
494 le16_to_cpu(bg->bg_free_bits_count));
495 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
496 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
498 status = ocfs2_journal_dirty(handle, bh);
504 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
505 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
506 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
507 le32_to_cpu(fe->i_clusters)));
508 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
509 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
510 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
515 ocfs2_commit_trans(osb, handle);
518 ocfs2_free_alloc_context(ac);
526 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
527 struct ocfs2_alloc_context *ac,
533 u32 bits_wanted = ac->ac_bits_wanted;
534 struct inode *alloc_inode;
535 struct buffer_head *bh = NULL;
536 struct ocfs2_dinode *fe;
541 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
547 mutex_lock(&alloc_inode->i_mutex);
549 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
551 mutex_unlock(&alloc_inode->i_mutex);
558 ac->ac_inode = alloc_inode;
559 ac->ac_alloc_slot = slot;
561 fe = (struct ocfs2_dinode *) bh->b_data;
563 /* The bh was validated by the inode read inside
564 * ocfs2_inode_lock(). Any corruption is a code bug. */
565 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
567 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
568 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
569 (unsigned long long)le64_to_cpu(fe->i_blkno));
574 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
575 le32_to_cpu(fe->id1.bitmap1.i_used);
577 if (bits_wanted > free_bits) {
578 /* cluster bitmap never grows */
579 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
580 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
581 bits_wanted, free_bits);
586 if (alloc_new_group != ALLOC_NEW_GROUP) {
587 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
588 "and we don't alloc a new group for it.\n",
589 slot, bits_wanted, free_bits);
594 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
597 if (status != -ENOSPC)
601 atomic_inc(&osb->alloc_stats.bg_extends);
603 /* You should never ask for this much metadata */
605 (le32_to_cpu(fe->id1.bitmap1.i_total)
606 - le32_to_cpu(fe->id1.bitmap1.i_used)));
618 int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
620 struct ocfs2_alloc_context **ac)
625 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
632 (*ac)->ac_bits_wanted = blocks;
633 (*ac)->ac_which = OCFS2_AC_USE_META;
634 slot = osb->slot_num;
635 (*ac)->ac_group_search = ocfs2_block_group_search;
637 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
638 EXTENT_ALLOC_SYSTEM_INODE,
639 slot, ALLOC_NEW_GROUP);
641 if (status != -ENOSPC)
648 if ((status < 0) && *ac) {
649 ocfs2_free_alloc_context(*ac);
657 int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
658 struct ocfs2_extent_list *root_el,
659 struct ocfs2_alloc_context **ac)
661 return ocfs2_reserve_new_metadata_blocks(osb,
662 ocfs2_extend_meta_needed(root_el),
666 static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
667 struct ocfs2_alloc_context *ac)
669 int i, status = -ENOSPC;
670 s16 slot = ocfs2_get_inode_steal_slot(osb);
672 /* Start to steal inodes from the first slot after ours. */
673 if (slot == OCFS2_INVALID_SLOT)
674 slot = osb->slot_num + 1;
676 for (i = 0; i < osb->max_slots; i++, slot++) {
677 if (slot == osb->max_slots)
680 if (slot == osb->slot_num)
683 status = ocfs2_reserve_suballoc_bits(osb, ac,
684 INODE_ALLOC_SYSTEM_INODE,
685 slot, NOT_ALLOC_NEW_GROUP);
687 ocfs2_set_inode_steal_slot(osb, slot);
691 ocfs2_free_ac_resource(ac);
697 int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
698 struct ocfs2_alloc_context **ac)
701 s16 slot = ocfs2_get_inode_steal_slot(osb);
703 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
710 (*ac)->ac_bits_wanted = 1;
711 (*ac)->ac_which = OCFS2_AC_USE_INODE;
713 (*ac)->ac_group_search = ocfs2_block_group_search;
716 * stat(2) can't handle i_ino > 32bits, so we tell the
717 * lower levels not to allocate us a block group past that
718 * limit. The 'inode64' mount option avoids this behavior.
720 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
721 (*ac)->ac_max_block = (u32)~0U;
724 * slot is set when we successfully steal inode from other nodes.
725 * It is reset in 3 places:
726 * 1. when we flush the truncate log
727 * 2. when we complete local alloc recovery.
728 * 3. when we successfully allocate from our own slot.
729 * After it is set, we will go on stealing inodes until we find the
730 * need to check our slots to see whether there is some space for us.
732 if (slot != OCFS2_INVALID_SLOT &&
733 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_INODES_TO_STEAL)
736 atomic_set(&osb->s_num_inodes_stolen, 0);
737 status = ocfs2_reserve_suballoc_bits(osb, *ac,
738 INODE_ALLOC_SYSTEM_INODE,
739 osb->slot_num, ALLOC_NEW_GROUP);
744 * Some inodes must be freed by us, so try to allocate
745 * from our own next time.
747 if (slot != OCFS2_INVALID_SLOT)
748 ocfs2_init_inode_steal_slot(osb);
750 } else if (status < 0 && status != -ENOSPC) {
755 ocfs2_free_ac_resource(*ac);
758 status = ocfs2_steal_inode_from_other_nodes(osb, *ac);
759 atomic_inc(&osb->s_num_inodes_stolen);
761 if (status != -ENOSPC)
768 if ((status < 0) && *ac) {
769 ocfs2_free_alloc_context(*ac);
777 /* local alloc code has to do the same thing, so rather than do this
779 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
780 struct ocfs2_alloc_context *ac)
784 ac->ac_which = OCFS2_AC_USE_MAIN;
785 ac->ac_group_search = ocfs2_cluster_group_search;
787 status = ocfs2_reserve_suballoc_bits(osb, ac,
788 GLOBAL_BITMAP_SYSTEM_INODE,
791 if (status < 0 && status != -ENOSPC) {
800 /* Callers don't need to care which bitmap (local alloc or main) to
801 * use so we figure it out for them, but unfortunately this clutters
803 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
804 u32 bits_wanted, u64 max_block,
805 struct ocfs2_alloc_context **ac)
811 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
818 (*ac)->ac_bits_wanted = bits_wanted;
819 (*ac)->ac_max_block = max_block;
822 if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
823 status = ocfs2_reserve_local_alloc_bits(osb,
826 if (status == -EFBIG) {
827 /* The local alloc window is outside ac_max_block.
828 * use the main bitmap. */
830 } else if ((status < 0) && (status != -ENOSPC)) {
836 if (status == -ENOSPC) {
837 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
839 if (status != -ENOSPC)
847 if ((status < 0) && *ac) {
848 ocfs2_free_alloc_context(*ac);
856 int ocfs2_reserve_clusters(struct ocfs2_super *osb,
858 struct ocfs2_alloc_context **ac)
860 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0, ac);
864 * More or less lifted from ext3. I'll leave their description below:
866 * "For ext3 allocations, we must not reuse any blocks which are
867 * allocated in the bitmap buffer's "last committed data" copy. This
868 * prevents deletes from freeing up the page for reuse until we have
869 * committed the delete transaction.
871 * If we didn't do this, then deleting something and reallocating it as
872 * data would allow the old block to be overwritten before the
873 * transaction committed (because we force data to disk before commit).
874 * This would lead to corruption if we crashed between overwriting the
875 * data and committing the delete.
877 * @@@ We may want to make this allocation behaviour conditional on
878 * data-writes at some point, and disable it for metadata allocations or
881 * Note: OCFS2 already does this differently for metadata vs data
882 * allocations, as those bitmaps are separate and undo access is never
883 * called on a metadata group descriptor.
885 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
888 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
890 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
892 if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
895 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
896 return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
899 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
900 struct buffer_head *bg_bh,
901 unsigned int bits_wanted,
902 unsigned int total_bits,
907 u16 best_offset, best_size;
908 int offset, start, found, status = 0;
909 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
911 /* Callers got this descriptor from
912 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
913 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
915 found = start = best_offset = best_size = 0;
916 bitmap = bg->bg_bitmap;
918 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
919 if (offset == total_bits)
922 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
923 /* We found a zero, but we can't use it as it
924 * hasn't been put to disk yet! */
927 } else if (offset == start) {
928 /* we found a zero */
930 /* move start to the next bit to test */
933 /* got a zero after some ones */
937 if (found > best_size) {
939 best_offset = start - found;
941 /* we got everything we needed */
942 if (found == bits_wanted) {
943 /* mlog(0, "Found it all!\n"); */
948 /* XXX: I think the first clause is equivalent to the second
950 if (found == bits_wanted) {
951 *bit_off = start - found;
953 } else if (best_size) {
954 *bit_off = best_offset;
955 *bits_found = best_size;
958 /* No error log here -- see the comment above
959 * ocfs2_test_bg_bit_allocatable */
965 static inline int ocfs2_block_group_set_bits(handle_t *handle,
966 struct inode *alloc_inode,
967 struct ocfs2_group_desc *bg,
968 struct buffer_head *group_bh,
969 unsigned int bit_off,
970 unsigned int num_bits)
973 void *bitmap = bg->bg_bitmap;
974 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
978 /* All callers get the descriptor via
979 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
980 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
981 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
983 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
986 if (ocfs2_is_cluster_bitmap(alloc_inode))
987 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
989 status = ocfs2_journal_access(handle,
998 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
1001 ocfs2_set_bit(bit_off++, bitmap);
1003 status = ocfs2_journal_dirty(handle,
1015 /* find the one with the most empty bits */
1016 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1020 BUG_ON(!cl->cl_next_free_rec);
1023 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1024 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1025 le32_to_cpu(cl->cl_recs[best].c_free))
1030 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1034 static int ocfs2_relink_block_group(handle_t *handle,
1035 struct inode *alloc_inode,
1036 struct buffer_head *fe_bh,
1037 struct buffer_head *bg_bh,
1038 struct buffer_head *prev_bg_bh,
1042 /* there is a really tiny chance the journal calls could fail,
1043 * but we wouldn't want inconsistent blocks in *any* case. */
1044 u64 fe_ptr, bg_ptr, prev_bg_ptr;
1045 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1046 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1047 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1049 /* The caller got these descriptors from
1050 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1051 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1052 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
1054 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
1055 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1056 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1057 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
1059 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
1060 bg_ptr = le64_to_cpu(bg->bg_next_group);
1061 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1063 status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh,
1064 OCFS2_JOURNAL_ACCESS_WRITE);
1070 prev_bg->bg_next_group = bg->bg_next_group;
1072 status = ocfs2_journal_dirty(handle, prev_bg_bh);
1078 status = ocfs2_journal_access(handle, alloc_inode, bg_bh,
1079 OCFS2_JOURNAL_ACCESS_WRITE);
1085 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
1087 status = ocfs2_journal_dirty(handle, bg_bh);
1093 status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
1094 OCFS2_JOURNAL_ACCESS_WRITE);
1100 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
1102 status = ocfs2_journal_dirty(handle, fe_bh);
1111 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1112 bg->bg_next_group = cpu_to_le64(bg_ptr);
1113 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1120 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1123 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1126 /* return 0 on success, -ENOSPC to keep searching and any other < 0
1127 * value on error. */
1128 static int ocfs2_cluster_group_search(struct inode *inode,
1129 struct buffer_head *group_bh,
1130 u32 bits_wanted, u32 min_bits,
1132 u16 *bit_off, u16 *bits_found)
1134 int search = -ENOSPC;
1137 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
1138 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1139 u16 tmp_off, tmp_found;
1140 unsigned int max_bits, gd_cluster_off;
1142 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1144 if (gd->bg_free_bits_count) {
1145 max_bits = le16_to_cpu(gd->bg_bits);
1147 /* Tail groups in cluster bitmaps which aren't cpg
1148 * aligned are prone to partial extention by a failed
1149 * fs resize. If the file system resize never got to
1150 * update the dinode cluster count, then we don't want
1151 * to trust any clusters past it, regardless of what
1152 * the group descriptor says. */
1153 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1154 le64_to_cpu(gd->bg_blkno));
1155 if ((gd_cluster_off + max_bits) >
1156 OCFS2_I(inode)->ip_clusters) {
1157 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1158 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1159 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1160 le16_to_cpu(gd->bg_bits),
1161 OCFS2_I(inode)->ip_clusters, max_bits);
1164 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1165 group_bh, bits_wanted,
1167 &tmp_off, &tmp_found);
1172 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1174 tmp_off + tmp_found);
1175 mlog(0, "Checking %llu against %llu\n",
1176 (unsigned long long)blkoff,
1177 (unsigned long long)max_block);
1178 if (blkoff > max_block)
1182 /* ocfs2_block_group_find_clear_bits() might
1183 * return success, but we still want to return
1184 * -ENOSPC unless it found the minimum number
1186 if (min_bits <= tmp_found) {
1188 *bits_found = tmp_found;
1189 search = 0; /* success */
1190 } else if (tmp_found) {
1192 * Don't show bits which we'll be returning
1193 * for allocation to the local alloc bitmap.
1195 ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
1202 static int ocfs2_block_group_search(struct inode *inode,
1203 struct buffer_head *group_bh,
1204 u32 bits_wanted, u32 min_bits,
1206 u16 *bit_off, u16 *bits_found)
1210 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1212 BUG_ON(min_bits != 1);
1213 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1215 if (bg->bg_free_bits_count) {
1216 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1217 group_bh, bits_wanted,
1218 le16_to_cpu(bg->bg_bits),
1219 bit_off, bits_found);
1220 if (!ret && max_block) {
1221 blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
1223 mlog(0, "Checking %llu against %llu\n",
1224 (unsigned long long)blkoff,
1225 (unsigned long long)max_block);
1226 if (blkoff > max_block)
1234 static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
1236 struct buffer_head *di_bh,
1242 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1243 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1245 ret = ocfs2_journal_access(handle, inode, di_bh,
1246 OCFS2_JOURNAL_ACCESS_WRITE);
1252 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1253 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1254 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1256 ret = ocfs2_journal_dirty(handle, di_bh);
1264 static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1269 unsigned int *num_bits,
1275 struct buffer_head *group_bh = NULL;
1276 struct ocfs2_group_desc *gd;
1277 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1278 struct inode *alloc_inode = ac->ac_inode;
1280 ret = ocfs2_read_group_descriptor(alloc_inode, di, gd_blkno,
1287 gd = (struct ocfs2_group_desc *) group_bh->b_data;
1288 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1289 ac->ac_max_block, bit_off, &found);
1298 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1300 le16_to_cpu(gd->bg_chain));
1306 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1307 *bit_off, *num_bits);
1311 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1319 static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1324 unsigned int *num_bits,
1329 u16 chain, tmp_bits;
1332 struct inode *alloc_inode = ac->ac_inode;
1333 struct buffer_head *group_bh = NULL;
1334 struct buffer_head *prev_group_bh = NULL;
1335 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1336 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1337 struct ocfs2_group_desc *bg;
1339 chain = ac->ac_chain;
1340 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1342 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
1344 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1345 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1351 bg = (struct ocfs2_group_desc *) group_bh->b_data;
1354 /* for now, the chain search is a bit simplistic. We just use
1355 * the 1st group with any empty bits. */
1356 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1357 bits_wanted, min_bits,
1358 ac->ac_max_block, bit_off,
1359 &tmp_bits)) == -ENOSPC) {
1360 if (!bg->bg_next_group)
1363 brelse(prev_group_bh);
1364 prev_group_bh = NULL;
1366 next_group = le64_to_cpu(bg->bg_next_group);
1367 prev_group_bh = group_bh;
1369 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1370 next_group, &group_bh);
1375 bg = (struct ocfs2_group_desc *) group_bh->b_data;
1378 if (status != -ENOSPC)
1383 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1384 tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
1386 *num_bits = tmp_bits;
1388 BUG_ON(*num_bits == 0);
1391 * Keep track of previous block descriptor read. When
1392 * we find a target, if we have read more than X
1393 * number of descriptors, and the target is reasonably
1394 * empty, relink him to top of his chain.
1396 * We've read 0 extra blocks and only send one more to
1397 * the transaction, yet the next guy to search has a
1400 * Do this *after* figuring out how many bits we're taking out
1401 * of our target group.
1403 if (ac->ac_allow_chain_relink &&
1405 (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1406 status = ocfs2_relink_block_group(handle, alloc_inode,
1407 ac->ac_bh, group_bh,
1408 prev_group_bh, chain);
1415 /* Ok, claim our bits now: set the info on dinode, chainlist
1416 * and then the group */
1417 status = ocfs2_journal_access(handle,
1420 OCFS2_JOURNAL_ACCESS_WRITE);
1426 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1427 fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1428 le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1430 status = ocfs2_journal_dirty(handle,
1437 status = ocfs2_block_group_set_bits(handle,
1448 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
1449 (unsigned long long)le64_to_cpu(fe->i_blkno));
1451 *bg_blkno = le64_to_cpu(bg->bg_blkno);
1452 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
1455 brelse(prev_group_bh);
1461 /* will give out up to bits_wanted contiguous bits. */
1462 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1463 struct ocfs2_alloc_context *ac,
1468 unsigned int *num_bits,
1474 u64 hint_blkno = ac->ac_last_group;
1475 struct ocfs2_chain_list *cl;
1476 struct ocfs2_dinode *fe;
1480 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1481 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1484 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1486 /* The bh was validated by the inode read during
1487 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1488 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1490 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1491 le32_to_cpu(fe->id1.bitmap1.i_total)) {
1492 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1493 "bits but only %u total.",
1494 (unsigned long long)le64_to_cpu(fe->i_blkno),
1495 le32_to_cpu(fe->id1.bitmap1.i_used),
1496 le32_to_cpu(fe->id1.bitmap1.i_total));
1502 /* Attempt to short-circuit the usual search mechanism
1503 * by jumping straight to the most recently used
1504 * allocation group. This helps us mantain some
1505 * contiguousness across allocations. */
1506 status = ocfs2_search_one_group(ac, handle, bits_wanted,
1507 min_bits, bit_off, num_bits,
1508 hint_blkno, &bits_left);
1510 /* Be careful to update *bg_blkno here as the
1511 * caller is expecting it to be filled in, and
1512 * ocfs2_search_one_group() won't do that for
1514 *bg_blkno = hint_blkno;
1517 if (status < 0 && status != -ENOSPC) {
1523 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1525 victim = ocfs2_find_victim_chain(cl);
1526 ac->ac_chain = victim;
1527 ac->ac_allow_chain_relink = 1;
1529 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
1530 num_bits, bg_blkno, &bits_left);
1533 if (status < 0 && status != -ENOSPC) {
1538 mlog(0, "Search of victim chain %u came up with nothing, "
1539 "trying all chains now.\n", victim);
1541 /* If we didn't pick a good victim, then just default to
1542 * searching each chain in order. Don't allow chain relinking
1543 * because we only calculate enough journal credits for one
1544 * relink per alloc. */
1545 ac->ac_allow_chain_relink = 0;
1546 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1549 if (!cl->cl_recs[i].c_free)
1553 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1554 bit_off, num_bits, bg_blkno,
1558 if (status < 0 && status != -ENOSPC) {
1565 if (status != -ENOSPC) {
1566 /* If the next search of this group is not likely to
1567 * yield a suitable extent, then we reset the last
1568 * group hint so as to not waste a disk read */
1569 if (bits_left < min_bits)
1570 ac->ac_last_group = 0;
1572 ac->ac_last_group = *bg_blkno;
1580 int ocfs2_claim_metadata(struct ocfs2_super *osb,
1582 struct ocfs2_alloc_context *ac,
1584 u16 *suballoc_bit_start,
1585 unsigned int *num_bits,
1592 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1593 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
1595 status = ocfs2_claim_suballoc_bits(osb,
1607 atomic_inc(&osb->alloc_stats.bg_allocs);
1609 *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1610 ac->ac_bits_given += (*num_bits);
1617 int ocfs2_claim_new_inode(struct ocfs2_super *osb,
1619 struct ocfs2_alloc_context *ac,
1624 unsigned int num_bits;
1630 BUG_ON(ac->ac_bits_given != 0);
1631 BUG_ON(ac->ac_bits_wanted != 1);
1632 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
1634 status = ocfs2_claim_suballoc_bits(osb,
1646 atomic_inc(&osb->alloc_stats.bg_allocs);
1648 BUG_ON(num_bits != 1);
1650 *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1651 ac->ac_bits_given++;
1658 /* translate a group desc. blkno and it's bitmap offset into
1659 * disk cluster offset. */
1660 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1664 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1667 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1669 if (bg_blkno != osb->first_cluster_group_blkno)
1670 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1671 cluster += (u32) bg_bit_off;
1675 /* given a cluster offset, calculate which block group it belongs to
1676 * and return that block offset. */
1677 u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
1679 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1682 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1684 group_no = cluster / osb->bitmap_cpg;
1686 return osb->first_cluster_group_blkno;
1687 return ocfs2_clusters_to_blocks(inode->i_sb,
1688 group_no * osb->bitmap_cpg);
1691 /* given the block number of a cluster start, calculate which cluster
1692 * group and descriptor bitmap offset that corresponds to. */
1693 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1698 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1699 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1701 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1703 *bg_blkno = ocfs2_which_cluster_group(inode,
1706 if (*bg_blkno == osb->first_cluster_group_blkno)
1707 *bg_bit_off = (u16) data_cluster;
1709 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1710 data_blkno - *bg_blkno);
1714 * min_bits - minimum contiguous chunk from this total allocation we
1715 * can handle. set to what we asked for originally for a full
1716 * contig. allocation, set to '1' to indicate we can deal with extents
1719 int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1721 struct ocfs2_alloc_context *ac,
1728 unsigned int bits_wanted = max_clusters;
1734 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1736 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1737 && ac->ac_which != OCFS2_AC_USE_MAIN);
1739 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1740 status = ocfs2_claim_local_alloc_bits(osb,
1747 atomic_inc(&osb->alloc_stats.local_data);
1749 if (min_clusters > (osb->bitmap_cpg - 1)) {
1750 /* The only paths asking for contiguousness
1751 * should know about this already. */
1752 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
1753 "group bitmap size %u!\n", min_clusters,
1758 /* clamp the current request down to a realistic size. */
1759 if (bits_wanted > (osb->bitmap_cpg - 1))
1760 bits_wanted = osb->bitmap_cpg - 1;
1762 status = ocfs2_claim_suballoc_bits(osb,
1772 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1775 atomic_inc(&osb->alloc_stats.bitmap_data);
1779 if (status != -ENOSPC)
1784 ac->ac_bits_given += *num_clusters;
1791 int ocfs2_claim_clusters(struct ocfs2_super *osb,
1793 struct ocfs2_alloc_context *ac,
1798 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1800 return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1801 bits_wanted, cluster_start, num_clusters);
1804 static inline int ocfs2_block_group_clear_bits(handle_t *handle,
1805 struct inode *alloc_inode,
1806 struct ocfs2_group_desc *bg,
1807 struct buffer_head *group_bh,
1808 unsigned int bit_off,
1809 unsigned int num_bits)
1813 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1814 struct ocfs2_group_desc *undo_bg = NULL;
1818 /* The caller got this descriptor from
1819 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1820 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1822 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1824 if (ocfs2_is_cluster_bitmap(alloc_inode))
1825 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1827 status = ocfs2_journal_access(handle, alloc_inode, group_bh,
1834 if (ocfs2_is_cluster_bitmap(alloc_inode))
1835 undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
1839 ocfs2_clear_bit((bit_off + tmp),
1840 (unsigned long *) bg->bg_bitmap);
1841 if (ocfs2_is_cluster_bitmap(alloc_inode))
1842 ocfs2_set_bit(bit_off + tmp,
1843 (unsigned long *) undo_bg->bg_bitmap);
1845 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1847 status = ocfs2_journal_dirty(handle, group_bh);
1855 * expects the suballoc inode to already be locked.
1857 int ocfs2_free_suballoc_bits(handle_t *handle,
1858 struct inode *alloc_inode,
1859 struct buffer_head *alloc_bh,
1860 unsigned int start_bit,
1866 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1867 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1868 struct buffer_head *group_bh = NULL;
1869 struct ocfs2_group_desc *group;
1873 /* The alloc_bh comes from ocfs2_free_dinode() or
1874 * ocfs2_free_clusters(). The callers have all locked the
1875 * allocator and gotten alloc_bh from the lock call. This
1876 * validates the dinode buffer. Any corruption that has happended
1878 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1879 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1881 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1882 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1883 (unsigned long long)bg_blkno, start_bit);
1885 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
1891 group = (struct ocfs2_group_desc *) group_bh->b_data;
1893 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1895 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1903 status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
1904 OCFS2_JOURNAL_ACCESS_WRITE);
1910 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
1912 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1913 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
1915 status = ocfs2_journal_dirty(handle, alloc_bh);
1928 int ocfs2_free_dinode(handle_t *handle,
1929 struct inode *inode_alloc_inode,
1930 struct buffer_head *inode_alloc_bh,
1931 struct ocfs2_dinode *di)
1933 u64 blk = le64_to_cpu(di->i_blkno);
1934 u16 bit = le16_to_cpu(di->i_suballoc_bit);
1935 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1937 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
1938 inode_alloc_bh, bit, bg_blkno, 1);
1941 int ocfs2_free_clusters(handle_t *handle,
1942 struct inode *bitmap_inode,
1943 struct buffer_head *bitmap_bh,
1945 unsigned int num_clusters)
1950 struct ocfs2_dinode *fe;
1952 /* You can't ever have a contiguous set of clusters
1953 * bigger than a block group bitmap so we never have to worry
1954 * about looping on them. */
1958 /* This is expensive. We can safely remove once this stuff has
1959 * gotten tested really well. */
1960 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
1962 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
1964 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
1967 mlog(0, "want to free %u clusters starting at block %llu\n",
1968 num_clusters, (unsigned long long)start_blk);
1969 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1970 (unsigned long long)bg_blkno, bg_start_bit);
1972 status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
1973 bg_start_bit, bg_blkno,
1980 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
1988 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
1990 printk("Block Group:\n");
1991 printk("bg_signature: %s\n", bg->bg_signature);
1992 printk("bg_size: %u\n", bg->bg_size);
1993 printk("bg_bits: %u\n", bg->bg_bits);
1994 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
1995 printk("bg_chain: %u\n", bg->bg_chain);
1996 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
1997 printk("bg_next_group: %llu\n",
1998 (unsigned long long)bg->bg_next_group);
1999 printk("bg_parent_dinode: %llu\n",
2000 (unsigned long long)bg->bg_parent_dinode);
2001 printk("bg_blkno: %llu\n",
2002 (unsigned long long)bg->bg_blkno);
2005 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2009 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
2010 printk("i_signature: %s\n", fe->i_signature);
2011 printk("i_size: %llu\n",
2012 (unsigned long long)fe->i_size);
2013 printk("i_clusters: %u\n", fe->i_clusters);
2014 printk("i_generation: %u\n",
2015 le32_to_cpu(fe->i_generation));
2016 printk("id1.bitmap1.i_used: %u\n",
2017 le32_to_cpu(fe->id1.bitmap1.i_used));
2018 printk("id1.bitmap1.i_total: %u\n",
2019 le32_to_cpu(fe->id1.bitmap1.i_total));
2020 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2021 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2022 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2023 printk("id2.i_chain.cl_next_free_rec: %u\n",
2024 fe->id2.i_chain.cl_next_free_rec);
2025 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2026 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2027 fe->id2.i_chain.cl_recs[i].c_free);
2028 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2029 fe->id2.i_chain.cl_recs[i].c_total);
2030 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2031 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
2036 * For a given allocation, determine which allocators will need to be
2037 * accessed, and lock them, reserving the appropriate number of bits.
2039 * Sparse file systems call this from ocfs2_write_begin_nolock()
2040 * and ocfs2_allocate_unwritten_extents().
2042 * File systems which don't support holes call this from
2043 * ocfs2_extend_allocation().
2045 int ocfs2_lock_allocators(struct inode *inode,
2046 struct ocfs2_extent_tree *et,
2047 u32 clusters_to_add, u32 extents_to_split,
2048 struct ocfs2_alloc_context **data_ac,
2049 struct ocfs2_alloc_context **meta_ac)
2051 int ret = 0, num_free_extents;
2052 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2053 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2059 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2061 num_free_extents = ocfs2_num_free_extents(osb, inode, et);
2062 if (num_free_extents < 0) {
2063 ret = num_free_extents;
2069 * Sparse allocation file systems need to be more conservative
2070 * with reserving room for expansion - the actual allocation
2071 * happens while we've got a journal handle open so re-taking
2072 * a cluster lock (because we ran out of room for another
2073 * extent) will violate ordering rules.
2075 * Most of the time we'll only be seeing this 1 cluster at a time
2078 * Always lock for any unwritten extents - we might want to
2079 * add blocks during a split.
2081 if (!num_free_extents ||
2082 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
2083 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
2091 if (clusters_to_add == 0)
2094 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2104 ocfs2_free_alloc_context(*meta_ac);
2109 * We cannot have an error and a non null *data_ac.