1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * metadata alloc and free
7 * Inspired by ext3 block groups.
9 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
32 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33 #include <cluster/masklog.h>
41 #include "localalloc.h"
47 #include "buffer_head_io.h"
49 #define NOT_ALLOC_NEW_GROUP 0
50 #define ALLOC_NEW_GROUP 1
52 #define OCFS2_MAX_INODES_TO_STEAL 1024
54 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
55 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
56 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
57 static int ocfs2_block_group_fill(handle_t *handle,
58 struct inode *alloc_inode,
59 struct buffer_head *bg_bh,
62 struct ocfs2_chain_list *cl);
63 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
64 struct inode *alloc_inode,
65 struct buffer_head *bh,
68 static int ocfs2_cluster_group_search(struct inode *inode,
69 struct buffer_head *group_bh,
70 u32 bits_wanted, u32 min_bits,
72 u16 *bit_off, u16 *bits_found);
73 static int ocfs2_block_group_search(struct inode *inode,
74 struct buffer_head *group_bh,
75 u32 bits_wanted, u32 min_bits,
77 u16 *bit_off, u16 *bits_found);
78 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
79 struct ocfs2_alloc_context *ac,
84 unsigned int *num_bits,
86 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
88 static inline int ocfs2_block_group_set_bits(handle_t *handle,
89 struct inode *alloc_inode,
90 struct ocfs2_group_desc *bg,
91 struct buffer_head *group_bh,
93 unsigned int num_bits);
94 static inline int ocfs2_block_group_clear_bits(handle_t *handle,
95 struct inode *alloc_inode,
96 struct ocfs2_group_desc *bg,
97 struct buffer_head *group_bh,
99 unsigned int num_bits);
101 static int ocfs2_relink_block_group(handle_t *handle,
102 struct inode *alloc_inode,
103 struct buffer_head *fe_bh,
104 struct buffer_head *bg_bh,
105 struct buffer_head *prev_bg_bh,
107 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
109 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
112 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
116 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
117 u32 bits_wanted, u64 max_block,
118 struct ocfs2_alloc_context **ac);
120 void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
122 struct inode *inode = ac->ac_inode;
125 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
126 ocfs2_inode_unlock(inode, 1);
128 mutex_unlock(&inode->i_mutex);
139 void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
141 ocfs2_free_ac_resource(ac);
145 static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
147 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
150 /* somewhat more expensive than our other checks, so use sparingly. */
151 int ocfs2_check_group_descriptor(struct super_block *sb,
152 struct ocfs2_dinode *di,
153 struct ocfs2_group_desc *gd)
155 unsigned int max_bits;
157 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
158 OCFS2_RO_ON_INVALID_GROUP_DESC(sb, gd);
162 if (di->i_blkno != gd->bg_parent_dinode) {
163 ocfs2_error(sb, "Group descriptor # %llu has bad parent "
164 "pointer (%llu, expected %llu)",
165 (unsigned long long)le64_to_cpu(gd->bg_blkno),
166 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
167 (unsigned long long)le64_to_cpu(di->i_blkno));
171 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
172 if (le16_to_cpu(gd->bg_bits) > max_bits) {
173 ocfs2_error(sb, "Group descriptor # %llu has bit count of %u",
174 (unsigned long long)le64_to_cpu(gd->bg_blkno),
175 le16_to_cpu(gd->bg_bits));
179 if (le16_to_cpu(gd->bg_chain) >=
180 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
181 ocfs2_error(sb, "Group descriptor # %llu has bad chain %u",
182 (unsigned long long)le64_to_cpu(gd->bg_blkno),
183 le16_to_cpu(gd->bg_chain));
187 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
188 ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
189 "claims that %u are free",
190 (unsigned long long)le64_to_cpu(gd->bg_blkno),
191 le16_to_cpu(gd->bg_bits),
192 le16_to_cpu(gd->bg_free_bits_count));
196 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
197 ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
198 "max bitmap bits of %u",
199 (unsigned long long)le64_to_cpu(gd->bg_blkno),
200 le16_to_cpu(gd->bg_bits),
201 8 * le16_to_cpu(gd->bg_size));
208 static int ocfs2_block_group_fill(handle_t *handle,
209 struct inode *alloc_inode,
210 struct buffer_head *bg_bh,
213 struct ocfs2_chain_list *cl)
216 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
217 struct super_block * sb = alloc_inode->i_sb;
221 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
222 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
224 (unsigned long long)group_blkno,
225 (unsigned long long) bg_bh->b_blocknr);
230 status = ocfs2_journal_access(handle,
233 OCFS2_JOURNAL_ACCESS_CREATE);
239 memset(bg, 0, sb->s_blocksize);
240 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
241 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
242 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
243 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
244 bg->bg_chain = cpu_to_le16(my_chain);
245 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
246 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
247 bg->bg_blkno = cpu_to_le64(group_blkno);
248 /* set the 1st bit in the bitmap to account for the descriptor block */
249 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
250 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
252 status = ocfs2_journal_dirty(handle, bg_bh);
256 /* There is no need to zero out or otherwise initialize the
257 * other blocks in a group - All valid FS metadata in a block
258 * group stores the superblock fs_generation value at
259 * allocation time. */
266 static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
271 while (curr < le16_to_cpu(cl->cl_count)) {
272 if (le32_to_cpu(cl->cl_recs[best].c_total) >
273 le32_to_cpu(cl->cl_recs[curr].c_total))
281 * We expect the block group allocator to already be locked.
283 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
284 struct inode *alloc_inode,
285 struct buffer_head *bh,
289 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
290 struct ocfs2_chain_list *cl;
291 struct ocfs2_alloc_context *ac = NULL;
292 handle_t *handle = NULL;
293 u32 bit_off, num_bits;
296 struct buffer_head *bg_bh = NULL;
297 struct ocfs2_group_desc *bg;
299 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
303 cl = &fe->id2.i_chain;
304 status = ocfs2_reserve_clusters_with_limit(osb,
305 le16_to_cpu(cl->cl_cpg),
308 if (status != -ENOSPC)
313 credits = ocfs2_calc_group_alloc_credits(osb->sb,
314 le16_to_cpu(cl->cl_cpg));
315 handle = ocfs2_start_trans(osb, credits);
316 if (IS_ERR(handle)) {
317 status = PTR_ERR(handle);
323 status = ocfs2_claim_clusters(osb,
326 le16_to_cpu(cl->cl_cpg),
330 if (status != -ENOSPC)
335 alloc_rec = ocfs2_find_smallest_chain(cl);
337 /* setup the group */
338 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
339 mlog(0, "new descriptor, record %u, at block %llu\n",
340 alloc_rec, (unsigned long long)bg_blkno);
342 bg_bh = sb_getblk(osb->sb, bg_blkno);
348 ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
350 status = ocfs2_block_group_fill(handle,
361 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
363 status = ocfs2_journal_access(handle, alloc_inode,
364 bh, OCFS2_JOURNAL_ACCESS_WRITE);
370 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
371 le16_to_cpu(bg->bg_free_bits_count));
372 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
373 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno);
374 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
375 le16_add_cpu(&cl->cl_next_free_rec, 1);
377 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
378 le16_to_cpu(bg->bg_free_bits_count));
379 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
380 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
382 status = ocfs2_journal_dirty(handle, bh);
388 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
389 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
390 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
391 le32_to_cpu(fe->i_clusters)));
392 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
393 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
394 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
399 ocfs2_commit_trans(osb, handle);
402 ocfs2_free_alloc_context(ac);
411 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
412 struct ocfs2_alloc_context *ac,
418 u32 bits_wanted = ac->ac_bits_wanted;
419 struct inode *alloc_inode;
420 struct buffer_head *bh = NULL;
421 struct ocfs2_dinode *fe;
426 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
432 mutex_lock(&alloc_inode->i_mutex);
434 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
436 mutex_unlock(&alloc_inode->i_mutex);
443 ac->ac_inode = alloc_inode;
444 ac->ac_alloc_slot = slot;
446 fe = (struct ocfs2_dinode *) bh->b_data;
447 if (!OCFS2_IS_VALID_DINODE(fe)) {
448 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
452 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
453 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
454 (unsigned long long)le64_to_cpu(fe->i_blkno));
459 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
460 le32_to_cpu(fe->id1.bitmap1.i_used);
462 if (bits_wanted > free_bits) {
463 /* cluster bitmap never grows */
464 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
465 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
466 bits_wanted, free_bits);
471 if (alloc_new_group != ALLOC_NEW_GROUP) {
472 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
473 "and we don't alloc a new group for it.\n",
474 slot, bits_wanted, free_bits);
479 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
482 if (status != -ENOSPC)
486 atomic_inc(&osb->alloc_stats.bg_extends);
488 /* You should never ask for this much metadata */
490 (le32_to_cpu(fe->id1.bitmap1.i_total)
491 - le32_to_cpu(fe->id1.bitmap1.i_used)));
504 int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
506 struct ocfs2_alloc_context **ac)
511 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
518 (*ac)->ac_bits_wanted = blocks;
519 (*ac)->ac_which = OCFS2_AC_USE_META;
520 slot = osb->slot_num;
521 (*ac)->ac_group_search = ocfs2_block_group_search;
523 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
524 EXTENT_ALLOC_SYSTEM_INODE,
525 slot, ALLOC_NEW_GROUP);
527 if (status != -ENOSPC)
534 if ((status < 0) && *ac) {
535 ocfs2_free_alloc_context(*ac);
543 int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
544 struct ocfs2_extent_list *root_el,
545 struct ocfs2_alloc_context **ac)
547 return ocfs2_reserve_new_metadata_blocks(osb,
548 ocfs2_extend_meta_needed(root_el),
552 static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
553 struct ocfs2_alloc_context *ac)
555 int i, status = -ENOSPC;
556 s16 slot = ocfs2_get_inode_steal_slot(osb);
558 /* Start to steal inodes from the first slot after ours. */
559 if (slot == OCFS2_INVALID_SLOT)
560 slot = osb->slot_num + 1;
562 for (i = 0; i < osb->max_slots; i++, slot++) {
563 if (slot == osb->max_slots)
566 if (slot == osb->slot_num)
569 status = ocfs2_reserve_suballoc_bits(osb, ac,
570 INODE_ALLOC_SYSTEM_INODE,
571 slot, NOT_ALLOC_NEW_GROUP);
573 ocfs2_set_inode_steal_slot(osb, slot);
577 ocfs2_free_ac_resource(ac);
583 int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
584 struct ocfs2_alloc_context **ac)
587 s16 slot = ocfs2_get_inode_steal_slot(osb);
589 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
596 (*ac)->ac_bits_wanted = 1;
597 (*ac)->ac_which = OCFS2_AC_USE_INODE;
599 (*ac)->ac_group_search = ocfs2_block_group_search;
602 * stat(2) can't handle i_ino > 32bits, so we tell the
603 * lower levels not to allocate us a block group past that
606 (*ac)->ac_max_block = (u32)~0U;
609 * slot is set when we successfully steal inode from other nodes.
610 * It is reset in 3 places:
611 * 1. when we flush the truncate log
612 * 2. when we complete local alloc recovery.
613 * 3. when we successfully allocate from our own slot.
614 * After it is set, we will go on stealing inodes until we find the
615 * need to check our slots to see whether there is some space for us.
617 if (slot != OCFS2_INVALID_SLOT &&
618 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_INODES_TO_STEAL)
621 atomic_set(&osb->s_num_inodes_stolen, 0);
622 status = ocfs2_reserve_suballoc_bits(osb, *ac,
623 INODE_ALLOC_SYSTEM_INODE,
624 osb->slot_num, ALLOC_NEW_GROUP);
629 * Some inodes must be freed by us, so try to allocate
630 * from our own next time.
632 if (slot != OCFS2_INVALID_SLOT)
633 ocfs2_init_inode_steal_slot(osb);
635 } else if (status < 0 && status != -ENOSPC) {
640 ocfs2_free_ac_resource(*ac);
643 status = ocfs2_steal_inode_from_other_nodes(osb, *ac);
644 atomic_inc(&osb->s_num_inodes_stolen);
646 if (status != -ENOSPC)
653 if ((status < 0) && *ac) {
654 ocfs2_free_alloc_context(*ac);
662 /* local alloc code has to do the same thing, so rather than do this
664 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
665 struct ocfs2_alloc_context *ac)
669 ac->ac_which = OCFS2_AC_USE_MAIN;
670 ac->ac_group_search = ocfs2_cluster_group_search;
672 status = ocfs2_reserve_suballoc_bits(osb, ac,
673 GLOBAL_BITMAP_SYSTEM_INODE,
676 if (status < 0 && status != -ENOSPC) {
685 /* Callers don't need to care which bitmap (local alloc or main) to
686 * use so we figure it out for them, but unfortunately this clutters
688 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
689 u32 bits_wanted, u64 max_block,
690 struct ocfs2_alloc_context **ac)
696 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
703 (*ac)->ac_bits_wanted = bits_wanted;
704 (*ac)->ac_max_block = max_block;
707 if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
708 status = ocfs2_reserve_local_alloc_bits(osb,
711 if (status == -EFBIG) {
712 /* The local alloc window is outside ac_max_block.
713 * use the main bitmap. */
715 } else if ((status < 0) && (status != -ENOSPC)) {
721 if (status == -ENOSPC) {
722 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
724 if (status != -ENOSPC)
732 if ((status < 0) && *ac) {
733 ocfs2_free_alloc_context(*ac);
741 int ocfs2_reserve_clusters(struct ocfs2_super *osb,
743 struct ocfs2_alloc_context **ac)
745 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0, ac);
749 * More or less lifted from ext3. I'll leave their description below:
751 * "For ext3 allocations, we must not reuse any blocks which are
752 * allocated in the bitmap buffer's "last committed data" copy. This
753 * prevents deletes from freeing up the page for reuse until we have
754 * committed the delete transaction.
756 * If we didn't do this, then deleting something and reallocating it as
757 * data would allow the old block to be overwritten before the
758 * transaction committed (because we force data to disk before commit).
759 * This would lead to corruption if we crashed between overwriting the
760 * data and committing the delete.
762 * @@@ We may want to make this allocation behaviour conditional on
763 * data-writes at some point, and disable it for metadata allocations or
766 * Note: OCFS2 already does this differently for metadata vs data
767 * allocations, as those bitmaps are separate and undo access is never
768 * called on a metadata group descriptor.
770 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
773 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
775 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
777 if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
780 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
781 return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
784 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
785 struct buffer_head *bg_bh,
786 unsigned int bits_wanted,
787 unsigned int total_bits,
792 u16 best_offset, best_size;
793 int offset, start, found, status = 0;
794 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
796 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
797 OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg);
801 found = start = best_offset = best_size = 0;
802 bitmap = bg->bg_bitmap;
804 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
805 if (offset == total_bits)
808 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
809 /* We found a zero, but we can't use it as it
810 * hasn't been put to disk yet! */
813 } else if (offset == start) {
814 /* we found a zero */
816 /* move start to the next bit to test */
819 /* got a zero after some ones */
823 if (found > best_size) {
825 best_offset = start - found;
827 /* we got everything we needed */
828 if (found == bits_wanted) {
829 /* mlog(0, "Found it all!\n"); */
834 /* XXX: I think the first clause is equivalent to the second
836 if (found == bits_wanted) {
837 *bit_off = start - found;
839 } else if (best_size) {
840 *bit_off = best_offset;
841 *bits_found = best_size;
844 /* No error log here -- see the comment above
845 * ocfs2_test_bg_bit_allocatable */
851 static inline int ocfs2_block_group_set_bits(handle_t *handle,
852 struct inode *alloc_inode,
853 struct ocfs2_group_desc *bg,
854 struct buffer_head *group_bh,
855 unsigned int bit_off,
856 unsigned int num_bits)
859 void *bitmap = bg->bg_bitmap;
860 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
864 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
865 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
869 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
871 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
874 if (ocfs2_is_cluster_bitmap(alloc_inode))
875 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
877 status = ocfs2_journal_access(handle,
886 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
889 ocfs2_set_bit(bit_off++, bitmap);
891 status = ocfs2_journal_dirty(handle,
903 /* find the one with the most empty bits */
904 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
908 BUG_ON(!cl->cl_next_free_rec);
911 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
912 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
913 le32_to_cpu(cl->cl_recs[best].c_free))
918 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
922 static int ocfs2_relink_block_group(handle_t *handle,
923 struct inode *alloc_inode,
924 struct buffer_head *fe_bh,
925 struct buffer_head *bg_bh,
926 struct buffer_head *prev_bg_bh,
930 /* there is a really tiny chance the journal calls could fail,
931 * but we wouldn't want inconsistent blocks in *any* case. */
932 u64 fe_ptr, bg_ptr, prev_bg_ptr;
933 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
934 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
935 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
937 if (!OCFS2_IS_VALID_DINODE(fe)) {
938 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
942 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
943 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
947 if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) {
948 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg);
953 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
954 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
955 (unsigned long long)le64_to_cpu(bg->bg_blkno),
956 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
958 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
959 bg_ptr = le64_to_cpu(bg->bg_next_group);
960 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
962 status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh,
963 OCFS2_JOURNAL_ACCESS_WRITE);
969 prev_bg->bg_next_group = bg->bg_next_group;
971 status = ocfs2_journal_dirty(handle, prev_bg_bh);
977 status = ocfs2_journal_access(handle, alloc_inode, bg_bh,
978 OCFS2_JOURNAL_ACCESS_WRITE);
984 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
986 status = ocfs2_journal_dirty(handle, bg_bh);
992 status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
993 OCFS2_JOURNAL_ACCESS_WRITE);
999 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
1001 status = ocfs2_journal_dirty(handle, fe_bh);
1010 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1011 bg->bg_next_group = cpu_to_le64(bg_ptr);
1012 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1019 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1022 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1025 /* return 0 on success, -ENOSPC to keep searching and any other < 0
1026 * value on error. */
1027 static int ocfs2_cluster_group_search(struct inode *inode,
1028 struct buffer_head *group_bh,
1029 u32 bits_wanted, u32 min_bits,
1031 u16 *bit_off, u16 *bits_found)
1033 int search = -ENOSPC;
1036 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
1037 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1038 u16 tmp_off, tmp_found;
1039 unsigned int max_bits, gd_cluster_off;
1041 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1043 if (gd->bg_free_bits_count) {
1044 max_bits = le16_to_cpu(gd->bg_bits);
1046 /* Tail groups in cluster bitmaps which aren't cpg
1047 * aligned are prone to partial extention by a failed
1048 * fs resize. If the file system resize never got to
1049 * update the dinode cluster count, then we don't want
1050 * to trust any clusters past it, regardless of what
1051 * the group descriptor says. */
1052 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1053 le64_to_cpu(gd->bg_blkno));
1054 if ((gd_cluster_off + max_bits) >
1055 OCFS2_I(inode)->ip_clusters) {
1056 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1057 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1058 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1059 le16_to_cpu(gd->bg_bits),
1060 OCFS2_I(inode)->ip_clusters, max_bits);
1063 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1064 group_bh, bits_wanted,
1066 &tmp_off, &tmp_found);
1071 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1073 tmp_off + tmp_found);
1074 mlog(0, "Checking %llu against %llu\n",
1075 (unsigned long long)blkoff,
1076 (unsigned long long)max_block);
1077 if (blkoff > max_block)
1081 /* ocfs2_block_group_find_clear_bits() might
1082 * return success, but we still want to return
1083 * -ENOSPC unless it found the minimum number
1085 if (min_bits <= tmp_found) {
1087 *bits_found = tmp_found;
1088 search = 0; /* success */
1089 } else if (tmp_found) {
1091 * Don't show bits which we'll be returning
1092 * for allocation to the local alloc bitmap.
1094 ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
1101 static int ocfs2_block_group_search(struct inode *inode,
1102 struct buffer_head *group_bh,
1103 u32 bits_wanted, u32 min_bits,
1105 u16 *bit_off, u16 *bits_found)
1109 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1111 BUG_ON(min_bits != 1);
1112 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1114 if (bg->bg_free_bits_count) {
1115 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1116 group_bh, bits_wanted,
1117 le16_to_cpu(bg->bg_bits),
1118 bit_off, bits_found);
1119 if (!ret && max_block) {
1120 blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
1122 mlog(0, "Checking %llu against %llu\n",
1123 (unsigned long long)blkoff,
1124 (unsigned long long)max_block);
1125 if (blkoff > max_block)
1133 static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
1135 struct buffer_head *di_bh,
1141 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1142 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1144 ret = ocfs2_journal_access(handle, inode, di_bh,
1145 OCFS2_JOURNAL_ACCESS_WRITE);
1151 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1152 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1153 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1155 ret = ocfs2_journal_dirty(handle, di_bh);
1163 static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1168 unsigned int *num_bits,
1174 struct buffer_head *group_bh = NULL;
1175 struct ocfs2_group_desc *gd;
1176 struct inode *alloc_inode = ac->ac_inode;
1178 ret = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), gd_blkno,
1179 &group_bh, OCFS2_BH_CACHED, alloc_inode);
1185 gd = (struct ocfs2_group_desc *) group_bh->b_data;
1186 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1187 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, gd);
1192 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1193 ac->ac_max_block, bit_off, &found);
1202 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1204 le16_to_cpu(gd->bg_chain));
1210 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1211 *bit_off, *num_bits);
1215 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1223 static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1228 unsigned int *num_bits,
1233 u16 chain, tmp_bits;
1236 struct inode *alloc_inode = ac->ac_inode;
1237 struct buffer_head *group_bh = NULL;
1238 struct buffer_head *prev_group_bh = NULL;
1239 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1240 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1241 struct ocfs2_group_desc *bg;
1243 chain = ac->ac_chain;
1244 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1246 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
1248 status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1249 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1250 &group_bh, OCFS2_BH_CACHED, alloc_inode);
1255 bg = (struct ocfs2_group_desc *) group_bh->b_data;
1256 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
1263 /* for now, the chain search is a bit simplistic. We just use
1264 * the 1st group with any empty bits. */
1265 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1266 bits_wanted, min_bits,
1267 ac->ac_max_block, bit_off,
1268 &tmp_bits)) == -ENOSPC) {
1269 if (!bg->bg_next_group)
1271 if (prev_group_bh) {
1272 brelse(prev_group_bh);
1273 prev_group_bh = NULL;
1275 next_group = le64_to_cpu(bg->bg_next_group);
1276 prev_group_bh = group_bh;
1278 status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1279 next_group, &group_bh,
1280 OCFS2_BH_CACHED, alloc_inode);
1285 bg = (struct ocfs2_group_desc *) group_bh->b_data;
1286 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
1293 if (status != -ENOSPC)
1298 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1299 tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
1301 *num_bits = tmp_bits;
1303 BUG_ON(*num_bits == 0);
1306 * Keep track of previous block descriptor read. When
1307 * we find a target, if we have read more than X
1308 * number of descriptors, and the target is reasonably
1309 * empty, relink him to top of his chain.
1311 * We've read 0 extra blocks and only send one more to
1312 * the transaction, yet the next guy to search has a
1315 * Do this *after* figuring out how many bits we're taking out
1316 * of our target group.
1318 if (ac->ac_allow_chain_relink &&
1320 (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1321 status = ocfs2_relink_block_group(handle, alloc_inode,
1322 ac->ac_bh, group_bh,
1323 prev_group_bh, chain);
1330 /* Ok, claim our bits now: set the info on dinode, chainlist
1331 * and then the group */
1332 status = ocfs2_journal_access(handle,
1335 OCFS2_JOURNAL_ACCESS_WRITE);
1341 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1342 fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1343 le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1345 status = ocfs2_journal_dirty(handle,
1352 status = ocfs2_block_group_set_bits(handle,
1363 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
1364 (unsigned long long)le64_to_cpu(fe->i_blkno));
1366 *bg_blkno = le64_to_cpu(bg->bg_blkno);
1367 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
1372 brelse(prev_group_bh);
1378 /* will give out up to bits_wanted contiguous bits. */
1379 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1380 struct ocfs2_alloc_context *ac,
1385 unsigned int *num_bits,
1391 u64 hint_blkno = ac->ac_last_group;
1392 struct ocfs2_chain_list *cl;
1393 struct ocfs2_dinode *fe;
1397 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1398 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1401 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1402 if (!OCFS2_IS_VALID_DINODE(fe)) {
1403 OCFS2_RO_ON_INVALID_DINODE(osb->sb, fe);
1407 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1408 le32_to_cpu(fe->id1.bitmap1.i_total)) {
1409 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1410 "bits but only %u total.",
1411 (unsigned long long)le64_to_cpu(fe->i_blkno),
1412 le32_to_cpu(fe->id1.bitmap1.i_used),
1413 le32_to_cpu(fe->id1.bitmap1.i_total));
1419 /* Attempt to short-circuit the usual search mechanism
1420 * by jumping straight to the most recently used
1421 * allocation group. This helps us mantain some
1422 * contiguousness across allocations. */
1423 status = ocfs2_search_one_group(ac, handle, bits_wanted,
1424 min_bits, bit_off, num_bits,
1425 hint_blkno, &bits_left);
1427 /* Be careful to update *bg_blkno here as the
1428 * caller is expecting it to be filled in, and
1429 * ocfs2_search_one_group() won't do that for
1431 *bg_blkno = hint_blkno;
1434 if (status < 0 && status != -ENOSPC) {
1440 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1442 victim = ocfs2_find_victim_chain(cl);
1443 ac->ac_chain = victim;
1444 ac->ac_allow_chain_relink = 1;
1446 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
1447 num_bits, bg_blkno, &bits_left);
1450 if (status < 0 && status != -ENOSPC) {
1455 mlog(0, "Search of victim chain %u came up with nothing, "
1456 "trying all chains now.\n", victim);
1458 /* If we didn't pick a good victim, then just default to
1459 * searching each chain in order. Don't allow chain relinking
1460 * because we only calculate enough journal credits for one
1461 * relink per alloc. */
1462 ac->ac_allow_chain_relink = 0;
1463 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1466 if (!cl->cl_recs[i].c_free)
1470 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1471 bit_off, num_bits, bg_blkno,
1475 if (status < 0 && status != -ENOSPC) {
1482 if (status != -ENOSPC) {
1483 /* If the next search of this group is not likely to
1484 * yield a suitable extent, then we reset the last
1485 * group hint so as to not waste a disk read */
1486 if (bits_left < min_bits)
1487 ac->ac_last_group = 0;
1489 ac->ac_last_group = *bg_blkno;
1497 int ocfs2_claim_metadata(struct ocfs2_super *osb,
1499 struct ocfs2_alloc_context *ac,
1501 u16 *suballoc_bit_start,
1502 unsigned int *num_bits,
1509 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1510 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
1512 status = ocfs2_claim_suballoc_bits(osb,
1524 atomic_inc(&osb->alloc_stats.bg_allocs);
1526 *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1527 ac->ac_bits_given += (*num_bits);
1534 int ocfs2_claim_new_inode(struct ocfs2_super *osb,
1536 struct ocfs2_alloc_context *ac,
1541 unsigned int num_bits;
1547 BUG_ON(ac->ac_bits_given != 0);
1548 BUG_ON(ac->ac_bits_wanted != 1);
1549 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
1551 status = ocfs2_claim_suballoc_bits(osb,
1563 atomic_inc(&osb->alloc_stats.bg_allocs);
1565 BUG_ON(num_bits != 1);
1567 *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1568 ac->ac_bits_given++;
1575 /* translate a group desc. blkno and it's bitmap offset into
1576 * disk cluster offset. */
1577 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1581 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1584 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1586 if (bg_blkno != osb->first_cluster_group_blkno)
1587 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1588 cluster += (u32) bg_bit_off;
1592 /* given a cluster offset, calculate which block group it belongs to
1593 * and return that block offset. */
1594 u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
1596 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1599 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1601 group_no = cluster / osb->bitmap_cpg;
1603 return osb->first_cluster_group_blkno;
1604 return ocfs2_clusters_to_blocks(inode->i_sb,
1605 group_no * osb->bitmap_cpg);
1608 /* given the block number of a cluster start, calculate which cluster
1609 * group and descriptor bitmap offset that corresponds to. */
1610 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1615 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1616 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1618 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1620 *bg_blkno = ocfs2_which_cluster_group(inode,
1623 if (*bg_blkno == osb->first_cluster_group_blkno)
1624 *bg_bit_off = (u16) data_cluster;
1626 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1627 data_blkno - *bg_blkno);
1631 * min_bits - minimum contiguous chunk from this total allocation we
1632 * can handle. set to what we asked for originally for a full
1633 * contig. allocation, set to '1' to indicate we can deal with extents
1636 int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1638 struct ocfs2_alloc_context *ac,
1645 unsigned int bits_wanted = max_clusters;
1651 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1653 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1654 && ac->ac_which != OCFS2_AC_USE_MAIN);
1656 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1657 status = ocfs2_claim_local_alloc_bits(osb,
1664 atomic_inc(&osb->alloc_stats.local_data);
1666 if (min_clusters > (osb->bitmap_cpg - 1)) {
1667 /* The only paths asking for contiguousness
1668 * should know about this already. */
1669 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
1670 "group bitmap size %u!\n", min_clusters,
1675 /* clamp the current request down to a realistic size. */
1676 if (bits_wanted > (osb->bitmap_cpg - 1))
1677 bits_wanted = osb->bitmap_cpg - 1;
1679 status = ocfs2_claim_suballoc_bits(osb,
1689 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1692 atomic_inc(&osb->alloc_stats.bitmap_data);
1696 if (status != -ENOSPC)
1701 ac->ac_bits_given += *num_clusters;
1708 int ocfs2_claim_clusters(struct ocfs2_super *osb,
1710 struct ocfs2_alloc_context *ac,
1715 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1717 return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1718 bits_wanted, cluster_start, num_clusters);
1721 static inline int ocfs2_block_group_clear_bits(handle_t *handle,
1722 struct inode *alloc_inode,
1723 struct ocfs2_group_desc *bg,
1724 struct buffer_head *group_bh,
1725 unsigned int bit_off,
1726 unsigned int num_bits)
1730 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1731 struct ocfs2_group_desc *undo_bg = NULL;
1735 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
1736 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
1741 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1743 if (ocfs2_is_cluster_bitmap(alloc_inode))
1744 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1746 status = ocfs2_journal_access(handle, alloc_inode, group_bh,
1753 if (ocfs2_is_cluster_bitmap(alloc_inode))
1754 undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
1758 ocfs2_clear_bit((bit_off + tmp),
1759 (unsigned long *) bg->bg_bitmap);
1760 if (ocfs2_is_cluster_bitmap(alloc_inode))
1761 ocfs2_set_bit(bit_off + tmp,
1762 (unsigned long *) undo_bg->bg_bitmap);
1764 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1766 status = ocfs2_journal_dirty(handle, group_bh);
1774 * expects the suballoc inode to already be locked.
1776 int ocfs2_free_suballoc_bits(handle_t *handle,
1777 struct inode *alloc_inode,
1778 struct buffer_head *alloc_bh,
1779 unsigned int start_bit,
1785 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
1786 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1787 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1788 struct buffer_head *group_bh = NULL;
1789 struct ocfs2_group_desc *group;
1793 if (!OCFS2_IS_VALID_DINODE(fe)) {
1794 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
1798 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1800 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1801 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1802 (unsigned long long)bg_blkno, start_bit);
1804 status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED,
1811 group = (struct ocfs2_group_desc *) group_bh->b_data;
1812 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, group);
1817 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1819 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1827 status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
1828 OCFS2_JOURNAL_ACCESS_WRITE);
1834 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
1836 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1837 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
1839 status = ocfs2_journal_dirty(handle, alloc_bh);
1853 int ocfs2_free_dinode(handle_t *handle,
1854 struct inode *inode_alloc_inode,
1855 struct buffer_head *inode_alloc_bh,
1856 struct ocfs2_dinode *di)
1858 u64 blk = le64_to_cpu(di->i_blkno);
1859 u16 bit = le16_to_cpu(di->i_suballoc_bit);
1860 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1862 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
1863 inode_alloc_bh, bit, bg_blkno, 1);
1866 int ocfs2_free_clusters(handle_t *handle,
1867 struct inode *bitmap_inode,
1868 struct buffer_head *bitmap_bh,
1870 unsigned int num_clusters)
1875 struct ocfs2_dinode *fe;
1877 /* You can't ever have a contiguous set of clusters
1878 * bigger than a block group bitmap so we never have to worry
1879 * about looping on them. */
1883 /* This is expensive. We can safely remove once this stuff has
1884 * gotten tested really well. */
1885 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
1887 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
1889 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
1892 mlog(0, "want to free %u clusters starting at block %llu\n",
1893 num_clusters, (unsigned long long)start_blk);
1894 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1895 (unsigned long long)bg_blkno, bg_start_bit);
1897 status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
1898 bg_start_bit, bg_blkno,
1905 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
1913 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
1915 printk("Block Group:\n");
1916 printk("bg_signature: %s\n", bg->bg_signature);
1917 printk("bg_size: %u\n", bg->bg_size);
1918 printk("bg_bits: %u\n", bg->bg_bits);
1919 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
1920 printk("bg_chain: %u\n", bg->bg_chain);
1921 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
1922 printk("bg_next_group: %llu\n",
1923 (unsigned long long)bg->bg_next_group);
1924 printk("bg_parent_dinode: %llu\n",
1925 (unsigned long long)bg->bg_parent_dinode);
1926 printk("bg_blkno: %llu\n",
1927 (unsigned long long)bg->bg_blkno);
1930 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
1934 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
1935 printk("i_signature: %s\n", fe->i_signature);
1936 printk("i_size: %llu\n",
1937 (unsigned long long)fe->i_size);
1938 printk("i_clusters: %u\n", fe->i_clusters);
1939 printk("i_generation: %u\n",
1940 le32_to_cpu(fe->i_generation));
1941 printk("id1.bitmap1.i_used: %u\n",
1942 le32_to_cpu(fe->id1.bitmap1.i_used));
1943 printk("id1.bitmap1.i_total: %u\n",
1944 le32_to_cpu(fe->id1.bitmap1.i_total));
1945 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
1946 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
1947 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
1948 printk("id2.i_chain.cl_next_free_rec: %u\n",
1949 fe->id2.i_chain.cl_next_free_rec);
1950 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
1951 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
1952 fe->id2.i_chain.cl_recs[i].c_free);
1953 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
1954 fe->id2.i_chain.cl_recs[i].c_total);
1955 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
1956 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
1961 * For a given allocation, determine which allocators will need to be
1962 * accessed, and lock them, reserving the appropriate number of bits.
1964 * Sparse file systems call this from ocfs2_write_begin_nolock()
1965 * and ocfs2_allocate_unwritten_extents().
1967 * File systems which don't support holes call this from
1968 * ocfs2_extend_allocation().
1970 int ocfs2_lock_allocators(struct inode *inode,
1971 struct ocfs2_extent_tree *et,
1972 u32 clusters_to_add, u32 extents_to_split,
1973 struct ocfs2_alloc_context **data_ac,
1974 struct ocfs2_alloc_context **meta_ac)
1976 int ret = 0, num_free_extents;
1977 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
1978 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1984 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
1986 num_free_extents = ocfs2_num_free_extents(osb, inode, et);
1987 if (num_free_extents < 0) {
1988 ret = num_free_extents;
1994 * Sparse allocation file systems need to be more conservative
1995 * with reserving room for expansion - the actual allocation
1996 * happens while we've got a journal handle open so re-taking
1997 * a cluster lock (because we ran out of room for another
1998 * extent) will violate ordering rules.
2000 * Most of the time we'll only be seeing this 1 cluster at a time
2003 * Always lock for any unwritten extents - we might want to
2004 * add blocks during a split.
2006 if (!num_free_extents ||
2007 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
2008 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
2016 if (clusters_to_add == 0)
2019 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2029 ocfs2_free_alloc_context(*meta_ac);
2034 * We cannot have an error and a non null *data_ac.