1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * metadata alloc and free
7 * Inspired by ext3 block groups.
9 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
32 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33 #include <cluster/masklog.h>
41 #include "localalloc.h"
47 #include "buffer_head_io.h"
49 #define NOT_ALLOC_NEW_GROUP 0
50 #define ALLOC_NEW_GROUP 1
52 #define OCFS2_MAX_INODES_TO_STEAL 1024
54 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
55 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
56 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
57 static int ocfs2_block_group_fill(handle_t *handle,
58 struct inode *alloc_inode,
59 struct buffer_head *bg_bh,
62 struct ocfs2_chain_list *cl);
63 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
64 struct inode *alloc_inode,
65 struct buffer_head *bh,
68 static int ocfs2_cluster_group_search(struct inode *inode,
69 struct buffer_head *group_bh,
70 u32 bits_wanted, u32 min_bits,
72 u16 *bit_off, u16 *bits_found);
73 static int ocfs2_block_group_search(struct inode *inode,
74 struct buffer_head *group_bh,
75 u32 bits_wanted, u32 min_bits,
77 u16 *bit_off, u16 *bits_found);
78 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
79 struct ocfs2_alloc_context *ac,
84 unsigned int *num_bits,
86 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
88 static inline int ocfs2_block_group_set_bits(handle_t *handle,
89 struct inode *alloc_inode,
90 struct ocfs2_group_desc *bg,
91 struct buffer_head *group_bh,
93 unsigned int num_bits);
94 static inline int ocfs2_block_group_clear_bits(handle_t *handle,
95 struct inode *alloc_inode,
96 struct ocfs2_group_desc *bg,
97 struct buffer_head *group_bh,
99 unsigned int num_bits);
101 static int ocfs2_relink_block_group(handle_t *handle,
102 struct inode *alloc_inode,
103 struct buffer_head *fe_bh,
104 struct buffer_head *bg_bh,
105 struct buffer_head *prev_bg_bh,
107 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
109 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
112 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
116 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
117 u32 bits_wanted, u64 max_block,
118 struct ocfs2_alloc_context **ac);
120 void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
122 struct inode *inode = ac->ac_inode;
125 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
126 ocfs2_inode_unlock(inode, 1);
128 mutex_unlock(&inode->i_mutex);
137 void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
139 ocfs2_free_ac_resource(ac);
143 static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
145 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
148 /* somewhat more expensive than our other checks, so use sparingly. */
149 int ocfs2_check_group_descriptor(struct super_block *sb,
150 struct ocfs2_dinode *di,
151 struct ocfs2_group_desc *gd)
153 unsigned int max_bits;
155 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
156 OCFS2_RO_ON_INVALID_GROUP_DESC(sb, gd);
160 if (di->i_blkno != gd->bg_parent_dinode) {
161 ocfs2_error(sb, "Group descriptor # %llu has bad parent "
162 "pointer (%llu, expected %llu)",
163 (unsigned long long)le64_to_cpu(gd->bg_blkno),
164 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
165 (unsigned long long)le64_to_cpu(di->i_blkno));
169 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
170 if (le16_to_cpu(gd->bg_bits) > max_bits) {
171 ocfs2_error(sb, "Group descriptor # %llu has bit count of %u",
172 (unsigned long long)le64_to_cpu(gd->bg_blkno),
173 le16_to_cpu(gd->bg_bits));
177 if (le16_to_cpu(gd->bg_chain) >=
178 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
179 ocfs2_error(sb, "Group descriptor # %llu has bad chain %u",
180 (unsigned long long)le64_to_cpu(gd->bg_blkno),
181 le16_to_cpu(gd->bg_chain));
185 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
186 ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
187 "claims that %u are free",
188 (unsigned long long)le64_to_cpu(gd->bg_blkno),
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 ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
196 "max bitmap bits of %u",
197 (unsigned long long)le64_to_cpu(gd->bg_blkno),
198 le16_to_cpu(gd->bg_bits),
199 8 * le16_to_cpu(gd->bg_size));
206 static int ocfs2_block_group_fill(handle_t *handle,
207 struct inode *alloc_inode,
208 struct buffer_head *bg_bh,
211 struct ocfs2_chain_list *cl)
214 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
215 struct super_block * sb = alloc_inode->i_sb;
219 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
220 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
222 (unsigned long long)group_blkno,
223 (unsigned long long) bg_bh->b_blocknr);
228 status = ocfs2_journal_access(handle,
231 OCFS2_JOURNAL_ACCESS_CREATE);
237 memset(bg, 0, sb->s_blocksize);
238 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
239 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
240 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
241 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
242 bg->bg_chain = cpu_to_le16(my_chain);
243 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
244 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
245 bg->bg_blkno = cpu_to_le64(group_blkno);
246 /* set the 1st bit in the bitmap to account for the descriptor block */
247 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
248 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
250 status = ocfs2_journal_dirty(handle, bg_bh);
254 /* There is no need to zero out or otherwise initialize the
255 * other blocks in a group - All valid FS metadata in a block
256 * group stores the superblock fs_generation value at
257 * allocation time. */
264 static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
269 while (curr < le16_to_cpu(cl->cl_count)) {
270 if (le32_to_cpu(cl->cl_recs[best].c_total) >
271 le32_to_cpu(cl->cl_recs[curr].c_total))
279 * We expect the block group allocator to already be locked.
281 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
282 struct inode *alloc_inode,
283 struct buffer_head *bh,
287 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
288 struct ocfs2_chain_list *cl;
289 struct ocfs2_alloc_context *ac = NULL;
290 handle_t *handle = NULL;
291 u32 bit_off, num_bits;
294 struct buffer_head *bg_bh = NULL;
295 struct ocfs2_group_desc *bg;
297 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
301 cl = &fe->id2.i_chain;
302 status = ocfs2_reserve_clusters_with_limit(osb,
303 le16_to_cpu(cl->cl_cpg),
306 if (status != -ENOSPC)
311 credits = ocfs2_calc_group_alloc_credits(osb->sb,
312 le16_to_cpu(cl->cl_cpg));
313 handle = ocfs2_start_trans(osb, credits);
314 if (IS_ERR(handle)) {
315 status = PTR_ERR(handle);
321 status = ocfs2_claim_clusters(osb,
324 le16_to_cpu(cl->cl_cpg),
328 if (status != -ENOSPC)
333 alloc_rec = ocfs2_find_smallest_chain(cl);
335 /* setup the group */
336 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
337 mlog(0, "new descriptor, record %u, at block %llu\n",
338 alloc_rec, (unsigned long long)bg_blkno);
340 bg_bh = sb_getblk(osb->sb, bg_blkno);
346 ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
348 status = ocfs2_block_group_fill(handle,
359 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
361 status = ocfs2_journal_access(handle, alloc_inode,
362 bh, OCFS2_JOURNAL_ACCESS_WRITE);
368 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
369 le16_to_cpu(bg->bg_free_bits_count));
370 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
371 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno);
372 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
373 le16_add_cpu(&cl->cl_next_free_rec, 1);
375 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
376 le16_to_cpu(bg->bg_free_bits_count));
377 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
378 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
380 status = ocfs2_journal_dirty(handle, bh);
386 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
387 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
388 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
389 le32_to_cpu(fe->i_clusters)));
390 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
391 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
392 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
397 ocfs2_commit_trans(osb, handle);
400 ocfs2_free_alloc_context(ac);
408 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
409 struct ocfs2_alloc_context *ac,
415 u32 bits_wanted = ac->ac_bits_wanted;
416 struct inode *alloc_inode;
417 struct buffer_head *bh = NULL;
418 struct ocfs2_dinode *fe;
423 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
429 mutex_lock(&alloc_inode->i_mutex);
431 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
433 mutex_unlock(&alloc_inode->i_mutex);
440 ac->ac_inode = alloc_inode;
441 ac->ac_alloc_slot = slot;
443 fe = (struct ocfs2_dinode *) bh->b_data;
445 /* The bh was validated by the inode read inside
446 * ocfs2_inode_lock(). Any corruption is a code bug. */
447 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
449 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
450 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
451 (unsigned long long)le64_to_cpu(fe->i_blkno));
456 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
457 le32_to_cpu(fe->id1.bitmap1.i_used);
459 if (bits_wanted > free_bits) {
460 /* cluster bitmap never grows */
461 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
462 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
463 bits_wanted, free_bits);
468 if (alloc_new_group != ALLOC_NEW_GROUP) {
469 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
470 "and we don't alloc a new group for it.\n",
471 slot, bits_wanted, free_bits);
476 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
479 if (status != -ENOSPC)
483 atomic_inc(&osb->alloc_stats.bg_extends);
485 /* You should never ask for this much metadata */
487 (le32_to_cpu(fe->id1.bitmap1.i_total)
488 - le32_to_cpu(fe->id1.bitmap1.i_used)));
500 int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
502 struct ocfs2_alloc_context **ac)
507 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
514 (*ac)->ac_bits_wanted = blocks;
515 (*ac)->ac_which = OCFS2_AC_USE_META;
516 slot = osb->slot_num;
517 (*ac)->ac_group_search = ocfs2_block_group_search;
519 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
520 EXTENT_ALLOC_SYSTEM_INODE,
521 slot, ALLOC_NEW_GROUP);
523 if (status != -ENOSPC)
530 if ((status < 0) && *ac) {
531 ocfs2_free_alloc_context(*ac);
539 int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
540 struct ocfs2_extent_list *root_el,
541 struct ocfs2_alloc_context **ac)
543 return ocfs2_reserve_new_metadata_blocks(osb,
544 ocfs2_extend_meta_needed(root_el),
548 static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
549 struct ocfs2_alloc_context *ac)
551 int i, status = -ENOSPC;
552 s16 slot = ocfs2_get_inode_steal_slot(osb);
554 /* Start to steal inodes from the first slot after ours. */
555 if (slot == OCFS2_INVALID_SLOT)
556 slot = osb->slot_num + 1;
558 for (i = 0; i < osb->max_slots; i++, slot++) {
559 if (slot == osb->max_slots)
562 if (slot == osb->slot_num)
565 status = ocfs2_reserve_suballoc_bits(osb, ac,
566 INODE_ALLOC_SYSTEM_INODE,
567 slot, NOT_ALLOC_NEW_GROUP);
569 ocfs2_set_inode_steal_slot(osb, slot);
573 ocfs2_free_ac_resource(ac);
579 int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
580 struct ocfs2_alloc_context **ac)
583 s16 slot = ocfs2_get_inode_steal_slot(osb);
585 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
592 (*ac)->ac_bits_wanted = 1;
593 (*ac)->ac_which = OCFS2_AC_USE_INODE;
595 (*ac)->ac_group_search = ocfs2_block_group_search;
598 * stat(2) can't handle i_ino > 32bits, so we tell the
599 * lower levels not to allocate us a block group past that
600 * limit. The 'inode64' mount option avoids this behavior.
602 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
603 (*ac)->ac_max_block = (u32)~0U;
606 * slot is set when we successfully steal inode from other nodes.
607 * It is reset in 3 places:
608 * 1. when we flush the truncate log
609 * 2. when we complete local alloc recovery.
610 * 3. when we successfully allocate from our own slot.
611 * After it is set, we will go on stealing inodes until we find the
612 * need to check our slots to see whether there is some space for us.
614 if (slot != OCFS2_INVALID_SLOT &&
615 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_INODES_TO_STEAL)
618 atomic_set(&osb->s_num_inodes_stolen, 0);
619 status = ocfs2_reserve_suballoc_bits(osb, *ac,
620 INODE_ALLOC_SYSTEM_INODE,
621 osb->slot_num, ALLOC_NEW_GROUP);
626 * Some inodes must be freed by us, so try to allocate
627 * from our own next time.
629 if (slot != OCFS2_INVALID_SLOT)
630 ocfs2_init_inode_steal_slot(osb);
632 } else if (status < 0 && status != -ENOSPC) {
637 ocfs2_free_ac_resource(*ac);
640 status = ocfs2_steal_inode_from_other_nodes(osb, *ac);
641 atomic_inc(&osb->s_num_inodes_stolen);
643 if (status != -ENOSPC)
650 if ((status < 0) && *ac) {
651 ocfs2_free_alloc_context(*ac);
659 /* local alloc code has to do the same thing, so rather than do this
661 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
662 struct ocfs2_alloc_context *ac)
666 ac->ac_which = OCFS2_AC_USE_MAIN;
667 ac->ac_group_search = ocfs2_cluster_group_search;
669 status = ocfs2_reserve_suballoc_bits(osb, ac,
670 GLOBAL_BITMAP_SYSTEM_INODE,
673 if (status < 0 && status != -ENOSPC) {
682 /* Callers don't need to care which bitmap (local alloc or main) to
683 * use so we figure it out for them, but unfortunately this clutters
685 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
686 u32 bits_wanted, u64 max_block,
687 struct ocfs2_alloc_context **ac)
693 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
700 (*ac)->ac_bits_wanted = bits_wanted;
701 (*ac)->ac_max_block = max_block;
704 if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
705 status = ocfs2_reserve_local_alloc_bits(osb,
708 if (status == -EFBIG) {
709 /* The local alloc window is outside ac_max_block.
710 * use the main bitmap. */
712 } else if ((status < 0) && (status != -ENOSPC)) {
718 if (status == -ENOSPC) {
719 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
721 if (status != -ENOSPC)
729 if ((status < 0) && *ac) {
730 ocfs2_free_alloc_context(*ac);
738 int ocfs2_reserve_clusters(struct ocfs2_super *osb,
740 struct ocfs2_alloc_context **ac)
742 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0, ac);
746 * More or less lifted from ext3. I'll leave their description below:
748 * "For ext3 allocations, we must not reuse any blocks which are
749 * allocated in the bitmap buffer's "last committed data" copy. This
750 * prevents deletes from freeing up the page for reuse until we have
751 * committed the delete transaction.
753 * If we didn't do this, then deleting something and reallocating it as
754 * data would allow the old block to be overwritten before the
755 * transaction committed (because we force data to disk before commit).
756 * This would lead to corruption if we crashed between overwriting the
757 * data and committing the delete.
759 * @@@ We may want to make this allocation behaviour conditional on
760 * data-writes at some point, and disable it for metadata allocations or
763 * Note: OCFS2 already does this differently for metadata vs data
764 * allocations, as those bitmaps are separate and undo access is never
765 * called on a metadata group descriptor.
767 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
770 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
772 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
774 if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
777 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
778 return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
781 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
782 struct buffer_head *bg_bh,
783 unsigned int bits_wanted,
784 unsigned int total_bits,
789 u16 best_offset, best_size;
790 int offset, start, found, status = 0;
791 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
793 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
794 OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg);
798 found = start = best_offset = best_size = 0;
799 bitmap = bg->bg_bitmap;
801 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
802 if (offset == total_bits)
805 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
806 /* We found a zero, but we can't use it as it
807 * hasn't been put to disk yet! */
810 } else if (offset == start) {
811 /* we found a zero */
813 /* move start to the next bit to test */
816 /* got a zero after some ones */
820 if (found > best_size) {
822 best_offset = start - found;
824 /* we got everything we needed */
825 if (found == bits_wanted) {
826 /* mlog(0, "Found it all!\n"); */
831 /* XXX: I think the first clause is equivalent to the second
833 if (found == bits_wanted) {
834 *bit_off = start - found;
836 } else if (best_size) {
837 *bit_off = best_offset;
838 *bits_found = best_size;
841 /* No error log here -- see the comment above
842 * ocfs2_test_bg_bit_allocatable */
848 static inline int ocfs2_block_group_set_bits(handle_t *handle,
849 struct inode *alloc_inode,
850 struct ocfs2_group_desc *bg,
851 struct buffer_head *group_bh,
852 unsigned int bit_off,
853 unsigned int num_bits)
856 void *bitmap = bg->bg_bitmap;
857 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
861 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
862 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
866 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
868 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
871 if (ocfs2_is_cluster_bitmap(alloc_inode))
872 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
874 status = ocfs2_journal_access(handle,
883 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
886 ocfs2_set_bit(bit_off++, bitmap);
888 status = ocfs2_journal_dirty(handle,
900 /* find the one with the most empty bits */
901 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
905 BUG_ON(!cl->cl_next_free_rec);
908 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
909 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
910 le32_to_cpu(cl->cl_recs[best].c_free))
915 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
919 static int ocfs2_relink_block_group(handle_t *handle,
920 struct inode *alloc_inode,
921 struct buffer_head *fe_bh,
922 struct buffer_head *bg_bh,
923 struct buffer_head *prev_bg_bh,
927 /* there is a really tiny chance the journal calls could fail,
928 * but we wouldn't want inconsistent blocks in *any* case. */
929 u64 fe_ptr, bg_ptr, prev_bg_ptr;
930 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
931 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
932 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
934 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
935 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
939 if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) {
940 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg);
945 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
946 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
947 (unsigned long long)le64_to_cpu(bg->bg_blkno),
948 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
950 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
951 bg_ptr = le64_to_cpu(bg->bg_next_group);
952 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
954 status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh,
955 OCFS2_JOURNAL_ACCESS_WRITE);
961 prev_bg->bg_next_group = bg->bg_next_group;
963 status = ocfs2_journal_dirty(handle, prev_bg_bh);
969 status = ocfs2_journal_access(handle, alloc_inode, bg_bh,
970 OCFS2_JOURNAL_ACCESS_WRITE);
976 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
978 status = ocfs2_journal_dirty(handle, bg_bh);
984 status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
985 OCFS2_JOURNAL_ACCESS_WRITE);
991 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
993 status = ocfs2_journal_dirty(handle, fe_bh);
1002 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1003 bg->bg_next_group = cpu_to_le64(bg_ptr);
1004 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1011 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1014 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1017 /* return 0 on success, -ENOSPC to keep searching and any other < 0
1018 * value on error. */
1019 static int ocfs2_cluster_group_search(struct inode *inode,
1020 struct buffer_head *group_bh,
1021 u32 bits_wanted, u32 min_bits,
1023 u16 *bit_off, u16 *bits_found)
1025 int search = -ENOSPC;
1028 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
1029 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1030 u16 tmp_off, tmp_found;
1031 unsigned int max_bits, gd_cluster_off;
1033 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1035 if (gd->bg_free_bits_count) {
1036 max_bits = le16_to_cpu(gd->bg_bits);
1038 /* Tail groups in cluster bitmaps which aren't cpg
1039 * aligned are prone to partial extention by a failed
1040 * fs resize. If the file system resize never got to
1041 * update the dinode cluster count, then we don't want
1042 * to trust any clusters past it, regardless of what
1043 * the group descriptor says. */
1044 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1045 le64_to_cpu(gd->bg_blkno));
1046 if ((gd_cluster_off + max_bits) >
1047 OCFS2_I(inode)->ip_clusters) {
1048 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1049 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1050 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1051 le16_to_cpu(gd->bg_bits),
1052 OCFS2_I(inode)->ip_clusters, max_bits);
1055 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1056 group_bh, bits_wanted,
1058 &tmp_off, &tmp_found);
1063 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1065 tmp_off + tmp_found);
1066 mlog(0, "Checking %llu against %llu\n",
1067 (unsigned long long)blkoff,
1068 (unsigned long long)max_block);
1069 if (blkoff > max_block)
1073 /* ocfs2_block_group_find_clear_bits() might
1074 * return success, but we still want to return
1075 * -ENOSPC unless it found the minimum number
1077 if (min_bits <= tmp_found) {
1079 *bits_found = tmp_found;
1080 search = 0; /* success */
1081 } else if (tmp_found) {
1083 * Don't show bits which we'll be returning
1084 * for allocation to the local alloc bitmap.
1086 ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
1093 static int ocfs2_block_group_search(struct inode *inode,
1094 struct buffer_head *group_bh,
1095 u32 bits_wanted, u32 min_bits,
1097 u16 *bit_off, u16 *bits_found)
1101 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1103 BUG_ON(min_bits != 1);
1104 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1106 if (bg->bg_free_bits_count) {
1107 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1108 group_bh, bits_wanted,
1109 le16_to_cpu(bg->bg_bits),
1110 bit_off, bits_found);
1111 if (!ret && max_block) {
1112 blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
1114 mlog(0, "Checking %llu against %llu\n",
1115 (unsigned long long)blkoff,
1116 (unsigned long long)max_block);
1117 if (blkoff > max_block)
1125 static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
1127 struct buffer_head *di_bh,
1133 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1134 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1136 ret = ocfs2_journal_access(handle, inode, di_bh,
1137 OCFS2_JOURNAL_ACCESS_WRITE);
1143 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1144 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1145 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1147 ret = ocfs2_journal_dirty(handle, di_bh);
1155 static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1160 unsigned int *num_bits,
1166 struct buffer_head *group_bh = NULL;
1167 struct ocfs2_group_desc *gd;
1168 struct inode *alloc_inode = ac->ac_inode;
1170 ret = ocfs2_read_block(alloc_inode, gd_blkno, &group_bh);
1176 gd = (struct ocfs2_group_desc *) group_bh->b_data;
1177 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1178 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, gd);
1183 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1184 ac->ac_max_block, bit_off, &found);
1193 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1195 le16_to_cpu(gd->bg_chain));
1201 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1202 *bit_off, *num_bits);
1206 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1214 static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1219 unsigned int *num_bits,
1224 u16 chain, tmp_bits;
1227 struct inode *alloc_inode = ac->ac_inode;
1228 struct buffer_head *group_bh = NULL;
1229 struct buffer_head *prev_group_bh = NULL;
1230 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1231 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1232 struct ocfs2_group_desc *bg;
1234 chain = ac->ac_chain;
1235 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1237 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
1239 status = ocfs2_read_block(alloc_inode,
1240 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1246 bg = (struct ocfs2_group_desc *) group_bh->b_data;
1247 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
1254 /* for now, the chain search is a bit simplistic. We just use
1255 * the 1st group with any empty bits. */
1256 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1257 bits_wanted, min_bits,
1258 ac->ac_max_block, bit_off,
1259 &tmp_bits)) == -ENOSPC) {
1260 if (!bg->bg_next_group)
1263 brelse(prev_group_bh);
1264 prev_group_bh = NULL;
1266 next_group = le64_to_cpu(bg->bg_next_group);
1267 prev_group_bh = group_bh;
1269 status = ocfs2_read_block(alloc_inode,
1270 next_group, &group_bh);
1275 bg = (struct ocfs2_group_desc *) group_bh->b_data;
1276 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
1283 if (status != -ENOSPC)
1288 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1289 tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
1291 *num_bits = tmp_bits;
1293 BUG_ON(*num_bits == 0);
1296 * Keep track of previous block descriptor read. When
1297 * we find a target, if we have read more than X
1298 * number of descriptors, and the target is reasonably
1299 * empty, relink him to top of his chain.
1301 * We've read 0 extra blocks and only send one more to
1302 * the transaction, yet the next guy to search has a
1305 * Do this *after* figuring out how many bits we're taking out
1306 * of our target group.
1308 if (ac->ac_allow_chain_relink &&
1310 (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1311 status = ocfs2_relink_block_group(handle, alloc_inode,
1312 ac->ac_bh, group_bh,
1313 prev_group_bh, chain);
1320 /* Ok, claim our bits now: set the info on dinode, chainlist
1321 * and then the group */
1322 status = ocfs2_journal_access(handle,
1325 OCFS2_JOURNAL_ACCESS_WRITE);
1331 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1332 fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1333 le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1335 status = ocfs2_journal_dirty(handle,
1342 status = ocfs2_block_group_set_bits(handle,
1353 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
1354 (unsigned long long)le64_to_cpu(fe->i_blkno));
1356 *bg_blkno = le64_to_cpu(bg->bg_blkno);
1357 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
1360 brelse(prev_group_bh);
1366 /* will give out up to bits_wanted contiguous bits. */
1367 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1368 struct ocfs2_alloc_context *ac,
1373 unsigned int *num_bits,
1379 u64 hint_blkno = ac->ac_last_group;
1380 struct ocfs2_chain_list *cl;
1381 struct ocfs2_dinode *fe;
1385 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1386 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1389 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1391 /* The bh was validated by the inode read during
1392 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1393 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1395 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1396 le32_to_cpu(fe->id1.bitmap1.i_total)) {
1397 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1398 "bits but only %u total.",
1399 (unsigned long long)le64_to_cpu(fe->i_blkno),
1400 le32_to_cpu(fe->id1.bitmap1.i_used),
1401 le32_to_cpu(fe->id1.bitmap1.i_total));
1407 /* Attempt to short-circuit the usual search mechanism
1408 * by jumping straight to the most recently used
1409 * allocation group. This helps us mantain some
1410 * contiguousness across allocations. */
1411 status = ocfs2_search_one_group(ac, handle, bits_wanted,
1412 min_bits, bit_off, num_bits,
1413 hint_blkno, &bits_left);
1415 /* Be careful to update *bg_blkno here as the
1416 * caller is expecting it to be filled in, and
1417 * ocfs2_search_one_group() won't do that for
1419 *bg_blkno = hint_blkno;
1422 if (status < 0 && status != -ENOSPC) {
1428 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1430 victim = ocfs2_find_victim_chain(cl);
1431 ac->ac_chain = victim;
1432 ac->ac_allow_chain_relink = 1;
1434 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
1435 num_bits, bg_blkno, &bits_left);
1438 if (status < 0 && status != -ENOSPC) {
1443 mlog(0, "Search of victim chain %u came up with nothing, "
1444 "trying all chains now.\n", victim);
1446 /* If we didn't pick a good victim, then just default to
1447 * searching each chain in order. Don't allow chain relinking
1448 * because we only calculate enough journal credits for one
1449 * relink per alloc. */
1450 ac->ac_allow_chain_relink = 0;
1451 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1454 if (!cl->cl_recs[i].c_free)
1458 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1459 bit_off, num_bits, bg_blkno,
1463 if (status < 0 && status != -ENOSPC) {
1470 if (status != -ENOSPC) {
1471 /* If the next search of this group is not likely to
1472 * yield a suitable extent, then we reset the last
1473 * group hint so as to not waste a disk read */
1474 if (bits_left < min_bits)
1475 ac->ac_last_group = 0;
1477 ac->ac_last_group = *bg_blkno;
1485 int ocfs2_claim_metadata(struct ocfs2_super *osb,
1487 struct ocfs2_alloc_context *ac,
1489 u16 *suballoc_bit_start,
1490 unsigned int *num_bits,
1497 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1498 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
1500 status = ocfs2_claim_suballoc_bits(osb,
1512 atomic_inc(&osb->alloc_stats.bg_allocs);
1514 *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1515 ac->ac_bits_given += (*num_bits);
1522 int ocfs2_claim_new_inode(struct ocfs2_super *osb,
1524 struct ocfs2_alloc_context *ac,
1529 unsigned int num_bits;
1535 BUG_ON(ac->ac_bits_given != 0);
1536 BUG_ON(ac->ac_bits_wanted != 1);
1537 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
1539 status = ocfs2_claim_suballoc_bits(osb,
1551 atomic_inc(&osb->alloc_stats.bg_allocs);
1553 BUG_ON(num_bits != 1);
1555 *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1556 ac->ac_bits_given++;
1563 /* translate a group desc. blkno and it's bitmap offset into
1564 * disk cluster offset. */
1565 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1569 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1572 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1574 if (bg_blkno != osb->first_cluster_group_blkno)
1575 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1576 cluster += (u32) bg_bit_off;
1580 /* given a cluster offset, calculate which block group it belongs to
1581 * and return that block offset. */
1582 u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
1584 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1587 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1589 group_no = cluster / osb->bitmap_cpg;
1591 return osb->first_cluster_group_blkno;
1592 return ocfs2_clusters_to_blocks(inode->i_sb,
1593 group_no * osb->bitmap_cpg);
1596 /* given the block number of a cluster start, calculate which cluster
1597 * group and descriptor bitmap offset that corresponds to. */
1598 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1603 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1604 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1606 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1608 *bg_blkno = ocfs2_which_cluster_group(inode,
1611 if (*bg_blkno == osb->first_cluster_group_blkno)
1612 *bg_bit_off = (u16) data_cluster;
1614 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1615 data_blkno - *bg_blkno);
1619 * min_bits - minimum contiguous chunk from this total allocation we
1620 * can handle. set to what we asked for originally for a full
1621 * contig. allocation, set to '1' to indicate we can deal with extents
1624 int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1626 struct ocfs2_alloc_context *ac,
1633 unsigned int bits_wanted = max_clusters;
1639 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1641 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1642 && ac->ac_which != OCFS2_AC_USE_MAIN);
1644 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1645 status = ocfs2_claim_local_alloc_bits(osb,
1652 atomic_inc(&osb->alloc_stats.local_data);
1654 if (min_clusters > (osb->bitmap_cpg - 1)) {
1655 /* The only paths asking for contiguousness
1656 * should know about this already. */
1657 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
1658 "group bitmap size %u!\n", min_clusters,
1663 /* clamp the current request down to a realistic size. */
1664 if (bits_wanted > (osb->bitmap_cpg - 1))
1665 bits_wanted = osb->bitmap_cpg - 1;
1667 status = ocfs2_claim_suballoc_bits(osb,
1677 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1680 atomic_inc(&osb->alloc_stats.bitmap_data);
1684 if (status != -ENOSPC)
1689 ac->ac_bits_given += *num_clusters;
1696 int ocfs2_claim_clusters(struct ocfs2_super *osb,
1698 struct ocfs2_alloc_context *ac,
1703 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1705 return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1706 bits_wanted, cluster_start, num_clusters);
1709 static inline int ocfs2_block_group_clear_bits(handle_t *handle,
1710 struct inode *alloc_inode,
1711 struct ocfs2_group_desc *bg,
1712 struct buffer_head *group_bh,
1713 unsigned int bit_off,
1714 unsigned int num_bits)
1718 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1719 struct ocfs2_group_desc *undo_bg = NULL;
1723 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
1724 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
1729 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1731 if (ocfs2_is_cluster_bitmap(alloc_inode))
1732 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1734 status = ocfs2_journal_access(handle, alloc_inode, group_bh,
1741 if (ocfs2_is_cluster_bitmap(alloc_inode))
1742 undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
1746 ocfs2_clear_bit((bit_off + tmp),
1747 (unsigned long *) bg->bg_bitmap);
1748 if (ocfs2_is_cluster_bitmap(alloc_inode))
1749 ocfs2_set_bit(bit_off + tmp,
1750 (unsigned long *) undo_bg->bg_bitmap);
1752 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1754 status = ocfs2_journal_dirty(handle, group_bh);
1762 * expects the suballoc inode to already be locked.
1764 int ocfs2_free_suballoc_bits(handle_t *handle,
1765 struct inode *alloc_inode,
1766 struct buffer_head *alloc_bh,
1767 unsigned int start_bit,
1773 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1774 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1775 struct buffer_head *group_bh = NULL;
1776 struct ocfs2_group_desc *group;
1780 /* The alloc_bh comes from ocfs2_free_dinode() or
1781 * ocfs2_free_clusters(). The callers have all locked the
1782 * allocator and gotten alloc_bh from the lock call. This
1783 * validates the dinode buffer. Any corruption that has happended
1785 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1786 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1788 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1789 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1790 (unsigned long long)bg_blkno, start_bit);
1792 status = ocfs2_read_block(alloc_inode, bg_blkno, &group_bh);
1798 group = (struct ocfs2_group_desc *) group_bh->b_data;
1799 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, group);
1804 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1806 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1814 status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
1815 OCFS2_JOURNAL_ACCESS_WRITE);
1821 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
1823 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1824 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
1826 status = ocfs2_journal_dirty(handle, alloc_bh);
1839 int ocfs2_free_dinode(handle_t *handle,
1840 struct inode *inode_alloc_inode,
1841 struct buffer_head *inode_alloc_bh,
1842 struct ocfs2_dinode *di)
1844 u64 blk = le64_to_cpu(di->i_blkno);
1845 u16 bit = le16_to_cpu(di->i_suballoc_bit);
1846 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1848 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
1849 inode_alloc_bh, bit, bg_blkno, 1);
1852 int ocfs2_free_clusters(handle_t *handle,
1853 struct inode *bitmap_inode,
1854 struct buffer_head *bitmap_bh,
1856 unsigned int num_clusters)
1861 struct ocfs2_dinode *fe;
1863 /* You can't ever have a contiguous set of clusters
1864 * bigger than a block group bitmap so we never have to worry
1865 * about looping on them. */
1869 /* This is expensive. We can safely remove once this stuff has
1870 * gotten tested really well. */
1871 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
1873 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
1875 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
1878 mlog(0, "want to free %u clusters starting at block %llu\n",
1879 num_clusters, (unsigned long long)start_blk);
1880 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1881 (unsigned long long)bg_blkno, bg_start_bit);
1883 status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
1884 bg_start_bit, bg_blkno,
1891 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
1899 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
1901 printk("Block Group:\n");
1902 printk("bg_signature: %s\n", bg->bg_signature);
1903 printk("bg_size: %u\n", bg->bg_size);
1904 printk("bg_bits: %u\n", bg->bg_bits);
1905 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
1906 printk("bg_chain: %u\n", bg->bg_chain);
1907 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
1908 printk("bg_next_group: %llu\n",
1909 (unsigned long long)bg->bg_next_group);
1910 printk("bg_parent_dinode: %llu\n",
1911 (unsigned long long)bg->bg_parent_dinode);
1912 printk("bg_blkno: %llu\n",
1913 (unsigned long long)bg->bg_blkno);
1916 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
1920 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
1921 printk("i_signature: %s\n", fe->i_signature);
1922 printk("i_size: %llu\n",
1923 (unsigned long long)fe->i_size);
1924 printk("i_clusters: %u\n", fe->i_clusters);
1925 printk("i_generation: %u\n",
1926 le32_to_cpu(fe->i_generation));
1927 printk("id1.bitmap1.i_used: %u\n",
1928 le32_to_cpu(fe->id1.bitmap1.i_used));
1929 printk("id1.bitmap1.i_total: %u\n",
1930 le32_to_cpu(fe->id1.bitmap1.i_total));
1931 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
1932 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
1933 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
1934 printk("id2.i_chain.cl_next_free_rec: %u\n",
1935 fe->id2.i_chain.cl_next_free_rec);
1936 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
1937 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
1938 fe->id2.i_chain.cl_recs[i].c_free);
1939 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
1940 fe->id2.i_chain.cl_recs[i].c_total);
1941 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
1942 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
1947 * For a given allocation, determine which allocators will need to be
1948 * accessed, and lock them, reserving the appropriate number of bits.
1950 * Sparse file systems call this from ocfs2_write_begin_nolock()
1951 * and ocfs2_allocate_unwritten_extents().
1953 * File systems which don't support holes call this from
1954 * ocfs2_extend_allocation().
1956 int ocfs2_lock_allocators(struct inode *inode,
1957 struct ocfs2_extent_tree *et,
1958 u32 clusters_to_add, u32 extents_to_split,
1959 struct ocfs2_alloc_context **data_ac,
1960 struct ocfs2_alloc_context **meta_ac)
1962 int ret = 0, num_free_extents;
1963 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
1964 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1970 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
1972 num_free_extents = ocfs2_num_free_extents(osb, inode, et);
1973 if (num_free_extents < 0) {
1974 ret = num_free_extents;
1980 * Sparse allocation file systems need to be more conservative
1981 * with reserving room for expansion - the actual allocation
1982 * happens while we've got a journal handle open so re-taking
1983 * a cluster lock (because we ran out of room for another
1984 * extent) will violate ordering rules.
1986 * Most of the time we'll only be seeing this 1 cluster at a time
1989 * Always lock for any unwritten extents - we might want to
1990 * add blocks during a split.
1992 if (!num_free_extents ||
1993 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
1994 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
2002 if (clusters_to_add == 0)
2005 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2015 ocfs2_free_alloc_context(*meta_ac);
2020 * We cannot have an error and a non null *data_ac.