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 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
50 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
51 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
52 static int ocfs2_block_group_fill(struct ocfs2_journal_handle *handle,
53 struct inode *alloc_inode,
54 struct buffer_head *bg_bh,
57 struct ocfs2_chain_list *cl);
58 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
59 struct inode *alloc_inode,
60 struct buffer_head *bh);
62 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
63 struct ocfs2_alloc_context *ac);
65 static int ocfs2_cluster_group_search(struct inode *inode,
66 struct buffer_head *group_bh,
67 u32 bits_wanted, u32 min_bits,
68 u16 *bit_off, u16 *bits_found);
69 static int ocfs2_block_group_search(struct inode *inode,
70 struct buffer_head *group_bh,
71 u32 bits_wanted, u32 min_bits,
72 u16 *bit_off, u16 *bits_found);
73 static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
77 unsigned int *num_bits,
79 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
80 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 int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
89 struct buffer_head *bg_bh,
90 unsigned int bits_wanted,
93 static inline int ocfs2_block_group_set_bits(struct ocfs2_journal_handle *handle,
94 struct inode *alloc_inode,
95 struct ocfs2_group_desc *bg,
96 struct buffer_head *group_bh,
98 unsigned int num_bits);
99 static inline int ocfs2_block_group_clear_bits(struct ocfs2_journal_handle *handle,
100 struct inode *alloc_inode,
101 struct ocfs2_group_desc *bg,
102 struct buffer_head *group_bh,
103 unsigned int bit_off,
104 unsigned int num_bits);
106 static int ocfs2_relink_block_group(struct ocfs2_journal_handle *handle,
107 struct inode *alloc_inode,
108 struct buffer_head *fe_bh,
109 struct buffer_head *bg_bh,
110 struct buffer_head *prev_bg_bh,
112 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
114 static int ocfs2_free_suballoc_bits(struct ocfs2_journal_handle *handle,
115 struct inode *alloc_inode,
116 struct buffer_head *alloc_bh,
117 unsigned int start_bit,
120 static inline u64 ocfs2_which_suballoc_group(u64 block,
122 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
125 static inline u64 ocfs2_which_cluster_group(struct inode *inode,
127 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
132 void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
141 static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
143 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
146 static int ocfs2_block_group_fill(struct ocfs2_journal_handle *handle,
147 struct inode *alloc_inode,
148 struct buffer_head *bg_bh,
151 struct ocfs2_chain_list *cl)
154 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
155 struct super_block * sb = alloc_inode->i_sb;
159 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
160 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
162 (unsigned long long)group_blkno,
163 (unsigned long long) bg_bh->b_blocknr);
168 status = ocfs2_journal_access(handle,
171 OCFS2_JOURNAL_ACCESS_CREATE);
177 memset(bg, 0, sb->s_blocksize);
178 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
179 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
180 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
181 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
182 bg->bg_chain = cpu_to_le16(my_chain);
183 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
184 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
185 bg->bg_blkno = cpu_to_le64(group_blkno);
186 /* set the 1st bit in the bitmap to account for the descriptor block */
187 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
188 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
190 status = ocfs2_journal_dirty(handle, bg_bh);
194 /* There is no need to zero out or otherwise initialize the
195 * other blocks in a group - All valid FS metadata in a block
196 * group stores the superblock fs_generation value at
197 * allocation time. */
204 static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
209 while (curr < le16_to_cpu(cl->cl_count)) {
210 if (le32_to_cpu(cl->cl_recs[best].c_total) >
211 le32_to_cpu(cl->cl_recs[curr].c_total))
219 * We expect the block group allocator to already be locked.
221 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
222 struct inode *alloc_inode,
223 struct buffer_head *bh)
226 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
227 struct ocfs2_chain_list *cl;
228 struct ocfs2_alloc_context *ac = NULL;
229 struct ocfs2_journal_handle *handle = NULL;
230 u32 bit_off, num_bits;
233 struct buffer_head *bg_bh = NULL;
234 struct ocfs2_group_desc *bg;
236 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
240 handle = ocfs2_alloc_handle(osb);
247 cl = &fe->id2.i_chain;
248 status = ocfs2_reserve_clusters(osb,
250 le16_to_cpu(cl->cl_cpg),
253 if (status != -ENOSPC)
258 credits = ocfs2_calc_group_alloc_credits(osb->sb,
259 le16_to_cpu(cl->cl_cpg));
260 handle = ocfs2_start_trans(osb, handle, credits);
261 if (IS_ERR(handle)) {
262 status = PTR_ERR(handle);
268 status = ocfs2_claim_clusters(osb,
271 le16_to_cpu(cl->cl_cpg),
275 if (status != -ENOSPC)
280 alloc_rec = ocfs2_find_smallest_chain(cl);
282 /* setup the group */
283 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
284 mlog(0, "new descriptor, record %u, at block %llu\n",
285 alloc_rec, (unsigned long long)bg_blkno);
287 bg_bh = sb_getblk(osb->sb, bg_blkno);
293 ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
295 status = ocfs2_block_group_fill(handle,
306 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
308 status = ocfs2_journal_access(handle, alloc_inode,
309 bh, OCFS2_JOURNAL_ACCESS_WRITE);
315 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
316 le16_to_cpu(bg->bg_free_bits_count));
317 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
318 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno);
319 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
320 le16_add_cpu(&cl->cl_next_free_rec, 1);
322 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
323 le16_to_cpu(bg->bg_free_bits_count));
324 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
325 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
327 status = ocfs2_journal_dirty(handle, bh);
333 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
334 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
335 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
336 le32_to_cpu(fe->i_clusters)));
337 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
338 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
339 alloc_inode->i_blocks =
340 ocfs2_align_bytes_to_sectors(i_size_read(alloc_inode));
345 ocfs2_commit_trans(handle);
348 ocfs2_free_alloc_context(ac);
357 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
358 struct ocfs2_alloc_context *ac)
361 u32 bits_wanted = ac->ac_bits_wanted;
362 struct inode *alloc_inode = ac->ac_inode;
363 struct buffer_head *bh = NULL;
364 struct ocfs2_journal_handle *handle = ac->ac_handle;
365 struct ocfs2_dinode *fe;
370 BUG_ON(handle->flags & OCFS2_HANDLE_STARTED);
372 ocfs2_handle_add_inode(handle, alloc_inode);
373 status = ocfs2_meta_lock(alloc_inode, handle, &bh, 1);
379 fe = (struct ocfs2_dinode *) bh->b_data;
380 if (!OCFS2_IS_VALID_DINODE(fe)) {
381 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
385 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
386 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
387 (unsigned long long)le64_to_cpu(fe->i_blkno));
392 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
393 le32_to_cpu(fe->id1.bitmap1.i_used);
395 if (bits_wanted > free_bits) {
396 /* cluster bitmap never grows */
397 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
398 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
399 bits_wanted, free_bits);
404 status = ocfs2_block_group_alloc(osb, alloc_inode, bh);
406 if (status != -ENOSPC)
410 atomic_inc(&osb->alloc_stats.bg_extends);
412 /* You should never ask for this much metadata */
414 (le32_to_cpu(fe->id1.bitmap1.i_total)
415 - le32_to_cpu(fe->id1.bitmap1.i_used)));
428 int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
429 struct ocfs2_journal_handle *handle,
430 struct ocfs2_dinode *fe,
431 struct ocfs2_alloc_context **ac)
434 struct inode *alloc_inode = NULL;
436 *ac = kcalloc(1, sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
443 (*ac)->ac_bits_wanted = ocfs2_extend_meta_needed(fe);
444 (*ac)->ac_handle = handle;
445 (*ac)->ac_which = OCFS2_AC_USE_META;
447 #ifndef OCFS2_USE_ALL_METADATA_SUBALLOCATORS
448 alloc_inode = ocfs2_get_system_file_inode(osb,
449 EXTENT_ALLOC_SYSTEM_INODE,
452 alloc_inode = ocfs2_get_system_file_inode(osb,
453 EXTENT_ALLOC_SYSTEM_INODE,
462 (*ac)->ac_inode = igrab(alloc_inode);
463 (*ac)->ac_group_search = ocfs2_block_group_search;
465 status = ocfs2_reserve_suballoc_bits(osb, (*ac));
467 if (status != -ENOSPC)
474 if ((status < 0) && *ac) {
475 ocfs2_free_alloc_context(*ac);
486 int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
487 struct ocfs2_journal_handle *handle,
488 struct ocfs2_alloc_context **ac)
491 struct inode *alloc_inode = NULL;
493 *ac = kcalloc(1, sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
500 (*ac)->ac_bits_wanted = 1;
501 (*ac)->ac_handle = handle;
502 (*ac)->ac_which = OCFS2_AC_USE_INODE;
504 alloc_inode = ocfs2_get_system_file_inode(osb,
505 INODE_ALLOC_SYSTEM_INODE,
513 (*ac)->ac_inode = igrab(alloc_inode);
514 (*ac)->ac_group_search = ocfs2_block_group_search;
516 status = ocfs2_reserve_suballoc_bits(osb, *ac);
518 if (status != -ENOSPC)
525 if ((status < 0) && *ac) {
526 ocfs2_free_alloc_context(*ac);
537 /* local alloc code has to do the same thing, so rather than do this
539 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
540 struct ocfs2_alloc_context *ac)
544 ac->ac_inode = ocfs2_get_system_file_inode(osb,
545 GLOBAL_BITMAP_SYSTEM_INODE,
549 mlog(ML_ERROR, "Could not get bitmap inode!\n");
552 ac->ac_which = OCFS2_AC_USE_MAIN;
553 ac->ac_group_search = ocfs2_cluster_group_search;
555 status = ocfs2_reserve_suballoc_bits(osb, ac);
556 if (status < 0 && status != -ENOSPC)
562 /* Callers don't need to care which bitmap (local alloc or main) to
563 * use so we figure it out for them, but unfortunately this clutters
565 int ocfs2_reserve_clusters(struct ocfs2_super *osb,
566 struct ocfs2_journal_handle *handle,
568 struct ocfs2_alloc_context **ac)
576 *ac = kcalloc(1, sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
583 (*ac)->ac_bits_wanted = bits_wanted;
584 (*ac)->ac_handle = handle;
587 if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
588 status = ocfs2_reserve_local_alloc_bits(osb,
592 if ((status < 0) && (status != -ENOSPC)) {
595 } else if (status == -ENOSPC) {
596 /* reserve_local_bits will return enospc with
597 * the local alloc inode still locked, so we
598 * can change this safely here. */
599 mlog(0, "Disabling local alloc\n");
600 /* We set to OCFS2_LA_DISABLED so that umount
601 * can clean up what's left of the local
603 osb->local_alloc_state = OCFS2_LA_DISABLED;
607 if (status == -ENOSPC) {
608 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
610 if (status != -ENOSPC)
618 if ((status < 0) && *ac) {
619 ocfs2_free_alloc_context(*ac);
628 * More or less lifted from ext3. I'll leave their description below:
630 * "For ext3 allocations, we must not reuse any blocks which are
631 * allocated in the bitmap buffer's "last committed data" copy. This
632 * prevents deletes from freeing up the page for reuse until we have
633 * committed the delete transaction.
635 * If we didn't do this, then deleting something and reallocating it as
636 * data would allow the old block to be overwritten before the
637 * transaction committed (because we force data to disk before commit).
638 * This would lead to corruption if we crashed between overwriting the
639 * data and committing the delete.
641 * @@@ We may want to make this allocation behaviour conditional on
642 * data-writes at some point, and disable it for metadata allocations or
645 * Note: OCFS2 already does this differently for metadata vs data
646 * allocations, as those bitmaps are seperate and undo access is never
647 * called on a metadata group descriptor.
649 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
652 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
654 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
656 if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
659 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
660 return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
663 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
664 struct buffer_head *bg_bh,
665 unsigned int bits_wanted,
670 u16 best_offset, best_size;
671 int offset, start, found, status = 0;
672 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
674 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
675 OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg);
679 found = start = best_offset = best_size = 0;
680 bitmap = bg->bg_bitmap;
682 while((offset = ocfs2_find_next_zero_bit(bitmap,
683 le16_to_cpu(bg->bg_bits),
685 if (offset == le16_to_cpu(bg->bg_bits))
688 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
689 /* We found a zero, but we can't use it as it
690 * hasn't been put to disk yet! */
693 } else if (offset == start) {
694 /* we found a zero */
696 /* move start to the next bit to test */
699 /* got a zero after some ones */
703 if (found > best_size) {
705 best_offset = start - found;
707 /* we got everything we needed */
708 if (found == bits_wanted) {
709 /* mlog(0, "Found it all!\n"); */
714 /* XXX: I think the first clause is equivalent to the second
716 if (found == bits_wanted) {
717 *bit_off = start - found;
719 } else if (best_size) {
720 *bit_off = best_offset;
721 *bits_found = best_size;
724 /* No error log here -- see the comment above
725 * ocfs2_test_bg_bit_allocatable */
731 static inline int ocfs2_block_group_set_bits(struct ocfs2_journal_handle *handle,
732 struct inode *alloc_inode,
733 struct ocfs2_group_desc *bg,
734 struct buffer_head *group_bh,
735 unsigned int bit_off,
736 unsigned int num_bits)
739 void *bitmap = bg->bg_bitmap;
740 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
744 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
745 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
749 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
751 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
754 if (ocfs2_is_cluster_bitmap(alloc_inode))
755 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
757 status = ocfs2_journal_access(handle,
766 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
769 ocfs2_set_bit(bit_off++, bitmap);
771 status = ocfs2_journal_dirty(handle,
783 /* find the one with the most empty bits */
784 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
788 BUG_ON(!cl->cl_next_free_rec);
791 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
792 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
793 le32_to_cpu(cl->cl_recs[best].c_free))
798 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
802 static int ocfs2_relink_block_group(struct ocfs2_journal_handle *handle,
803 struct inode *alloc_inode,
804 struct buffer_head *fe_bh,
805 struct buffer_head *bg_bh,
806 struct buffer_head *prev_bg_bh,
810 /* there is a really tiny chance the journal calls could fail,
811 * but we wouldn't want inconsistent blocks in *any* case. */
812 u64 fe_ptr, bg_ptr, prev_bg_ptr;
813 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
814 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
815 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
817 if (!OCFS2_IS_VALID_DINODE(fe)) {
818 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
822 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
823 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
827 if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) {
828 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg);
833 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
834 (unsigned long long)fe->i_blkno, chain,
835 (unsigned long long)bg->bg_blkno,
836 (unsigned long long)prev_bg->bg_blkno);
838 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
839 bg_ptr = le64_to_cpu(bg->bg_next_group);
840 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
842 status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh,
843 OCFS2_JOURNAL_ACCESS_WRITE);
849 prev_bg->bg_next_group = bg->bg_next_group;
851 status = ocfs2_journal_dirty(handle, prev_bg_bh);
857 status = ocfs2_journal_access(handle, alloc_inode, bg_bh,
858 OCFS2_JOURNAL_ACCESS_WRITE);
864 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
866 status = ocfs2_journal_dirty(handle, bg_bh);
872 status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
873 OCFS2_JOURNAL_ACCESS_WRITE);
879 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
881 status = ocfs2_journal_dirty(handle, fe_bh);
890 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
891 bg->bg_next_group = cpu_to_le64(bg_ptr);
892 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
899 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
902 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
905 /* return 0 on success, -ENOSPC to keep searching and any other < 0
907 static int ocfs2_cluster_group_search(struct inode *inode,
908 struct buffer_head *group_bh,
909 u32 bits_wanted, u32 min_bits,
910 u16 *bit_off, u16 *bits_found)
912 int search = -ENOSPC;
914 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
915 u16 tmp_off, tmp_found;
917 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
919 if (bg->bg_free_bits_count) {
920 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
921 group_bh, bits_wanted,
922 &tmp_off, &tmp_found);
926 /* ocfs2_block_group_find_clear_bits() might
927 * return success, but we still want to return
928 * -ENOSPC unless it found the minimum number
930 if (min_bits <= tmp_found) {
932 *bits_found = tmp_found;
933 search = 0; /* success */
940 static int ocfs2_block_group_search(struct inode *inode,
941 struct buffer_head *group_bh,
942 u32 bits_wanted, u32 min_bits,
943 u16 *bit_off, u16 *bits_found)
946 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
948 BUG_ON(min_bits != 1);
949 BUG_ON(ocfs2_is_cluster_bitmap(inode));
951 if (bg->bg_free_bits_count)
952 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
953 group_bh, bits_wanted,
954 bit_off, bits_found);
959 static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
963 unsigned int *num_bits,
970 struct ocfs2_journal_handle *handle = ac->ac_handle;
971 struct inode *alloc_inode = ac->ac_inode;
972 struct buffer_head *group_bh = NULL;
973 struct buffer_head *prev_group_bh = NULL;
974 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
975 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
976 struct ocfs2_group_desc *bg;
978 chain = ac->ac_chain;
979 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
981 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
983 status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
984 le64_to_cpu(cl->cl_recs[chain].c_blkno),
985 &group_bh, OCFS2_BH_CACHED, alloc_inode);
990 bg = (struct ocfs2_group_desc *) group_bh->b_data;
991 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
992 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
998 /* for now, the chain search is a bit simplistic. We just use
999 * the 1st group with any empty bits. */
1000 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1001 bits_wanted, min_bits, bit_off,
1002 &tmp_bits)) == -ENOSPC) {
1003 if (!bg->bg_next_group)
1006 if (prev_group_bh) {
1007 brelse(prev_group_bh);
1008 prev_group_bh = NULL;
1010 next_group = le64_to_cpu(bg->bg_next_group);
1011 prev_group_bh = group_bh;
1013 status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1014 next_group, &group_bh,
1015 OCFS2_BH_CACHED, alloc_inode);
1020 bg = (struct ocfs2_group_desc *) group_bh->b_data;
1021 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
1022 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
1028 if (status != -ENOSPC)
1033 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1034 tmp_bits, (unsigned long long)bg->bg_blkno);
1036 *num_bits = tmp_bits;
1038 BUG_ON(*num_bits == 0);
1041 * Keep track of previous block descriptor read. When
1042 * we find a target, if we have read more than X
1043 * number of descriptors, and the target is reasonably
1044 * empty, relink him to top of his chain.
1046 * We've read 0 extra blocks and only send one more to
1047 * the transaction, yet the next guy to search has a
1050 * Do this *after* figuring out how many bits we're taking out
1051 * of our target group.
1053 if (ac->ac_allow_chain_relink &&
1055 (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1056 status = ocfs2_relink_block_group(handle, alloc_inode,
1057 ac->ac_bh, group_bh,
1058 prev_group_bh, chain);
1065 /* Ok, claim our bits now: set the info on dinode, chainlist
1066 * and then the group */
1067 status = ocfs2_journal_access(handle,
1070 OCFS2_JOURNAL_ACCESS_WRITE);
1076 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1077 fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1078 le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1080 status = ocfs2_journal_dirty(handle,
1087 status = ocfs2_block_group_set_bits(handle,
1098 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
1099 (unsigned long long)fe->i_blkno);
1101 *bg_blkno = le64_to_cpu(bg->bg_blkno);
1106 brelse(prev_group_bh);
1112 /* will give out up to bits_wanted contiguous bits. */
1113 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1114 struct ocfs2_alloc_context *ac,
1118 unsigned int *num_bits,
1123 struct ocfs2_chain_list *cl;
1124 struct ocfs2_dinode *fe;
1128 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1129 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1132 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1133 if (!OCFS2_IS_VALID_DINODE(fe)) {
1134 OCFS2_RO_ON_INVALID_DINODE(osb->sb, fe);
1138 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1139 le32_to_cpu(fe->id1.bitmap1.i_total)) {
1140 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1141 "bits but only %u total.",
1142 (unsigned long long)le64_to_cpu(fe->i_blkno),
1143 le32_to_cpu(fe->id1.bitmap1.i_used),
1144 le32_to_cpu(fe->id1.bitmap1.i_total));
1149 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1151 victim = ocfs2_find_victim_chain(cl);
1152 ac->ac_chain = victim;
1153 ac->ac_allow_chain_relink = 1;
1155 status = ocfs2_search_chain(ac, bits_wanted, min_bits, bit_off,
1156 num_bits, bg_blkno);
1159 if (status < 0 && status != -ENOSPC) {
1164 mlog(0, "Search of victim chain %u came up with nothing, "
1165 "trying all chains now.\n", victim);
1167 /* If we didn't pick a good victim, then just default to
1168 * searching each chain in order. Don't allow chain relinking
1169 * because we only calculate enough journal credits for one
1170 * relink per alloc. */
1171 ac->ac_allow_chain_relink = 0;
1172 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1175 if (!cl->cl_recs[i].c_free)
1179 status = ocfs2_search_chain(ac, bits_wanted, min_bits,
1184 if (status < 0 && status != -ENOSPC) {
1195 int ocfs2_claim_metadata(struct ocfs2_super *osb,
1196 struct ocfs2_journal_handle *handle,
1197 struct ocfs2_alloc_context *ac,
1199 u16 *suballoc_bit_start,
1200 unsigned int *num_bits,
1207 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1208 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
1209 BUG_ON(ac->ac_handle != handle);
1211 status = ocfs2_claim_suballoc_bits(osb,
1222 atomic_inc(&osb->alloc_stats.bg_allocs);
1224 *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1225 ac->ac_bits_given += (*num_bits);
1232 int ocfs2_claim_new_inode(struct ocfs2_super *osb,
1233 struct ocfs2_journal_handle *handle,
1234 struct ocfs2_alloc_context *ac,
1239 unsigned int num_bits;
1245 BUG_ON(ac->ac_bits_given != 0);
1246 BUG_ON(ac->ac_bits_wanted != 1);
1247 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
1248 BUG_ON(ac->ac_handle != handle);
1250 status = ocfs2_claim_suballoc_bits(osb,
1261 atomic_inc(&osb->alloc_stats.bg_allocs);
1263 BUG_ON(num_bits != 1);
1265 *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1266 ac->ac_bits_given++;
1273 /* translate a group desc. blkno and it's bitmap offset into
1274 * disk cluster offset. */
1275 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1279 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1282 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1284 if (bg_blkno != osb->first_cluster_group_blkno)
1285 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1286 cluster += (u32) bg_bit_off;
1290 /* given a cluster offset, calculate which block group it belongs to
1291 * and return that block offset. */
1292 static inline u64 ocfs2_which_cluster_group(struct inode *inode,
1295 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1298 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1300 group_no = cluster / osb->bitmap_cpg;
1302 return osb->first_cluster_group_blkno;
1303 return ocfs2_clusters_to_blocks(inode->i_sb,
1304 group_no * osb->bitmap_cpg);
1307 /* given the block number of a cluster start, calculate which cluster
1308 * group and descriptor bitmap offset that corresponds to. */
1309 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1314 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1315 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1317 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1319 *bg_blkno = ocfs2_which_cluster_group(inode,
1322 if (*bg_blkno == osb->first_cluster_group_blkno)
1323 *bg_bit_off = (u16) data_cluster;
1325 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1326 data_blkno - *bg_blkno);
1330 * min_bits - minimum contiguous chunk from this total allocation we
1331 * can handle. set to what we asked for originally for a full
1332 * contig. allocation, set to '1' to indicate we can deal with extents
1335 int ocfs2_claim_clusters(struct ocfs2_super *osb,
1336 struct ocfs2_journal_handle *handle,
1337 struct ocfs2_alloc_context *ac,
1343 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1350 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1352 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1353 && ac->ac_which != OCFS2_AC_USE_MAIN);
1354 BUG_ON(ac->ac_handle != handle);
1356 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1357 status = ocfs2_claim_local_alloc_bits(osb,
1364 atomic_inc(&osb->alloc_stats.local_data);
1366 if (min_clusters > (osb->bitmap_cpg - 1)) {
1367 /* The only paths asking for contiguousness
1368 * should know about this already. */
1369 mlog(ML_ERROR, "minimum allocation requested exceeds "
1370 "group bitmap size!");
1374 /* clamp the current request down to a realistic size. */
1375 if (bits_wanted > (osb->bitmap_cpg - 1))
1376 bits_wanted = osb->bitmap_cpg - 1;
1378 status = ocfs2_claim_suballoc_bits(osb,
1387 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1390 atomic_inc(&osb->alloc_stats.bitmap_data);
1394 if (status != -ENOSPC)
1399 ac->ac_bits_given += *num_clusters;
1406 static inline int ocfs2_block_group_clear_bits(struct ocfs2_journal_handle *handle,
1407 struct inode *alloc_inode,
1408 struct ocfs2_group_desc *bg,
1409 struct buffer_head *group_bh,
1410 unsigned int bit_off,
1411 unsigned int num_bits)
1415 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1416 struct ocfs2_group_desc *undo_bg = NULL;
1420 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
1421 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
1426 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1428 if (ocfs2_is_cluster_bitmap(alloc_inode))
1429 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1431 status = ocfs2_journal_access(handle, alloc_inode, group_bh,
1438 if (ocfs2_is_cluster_bitmap(alloc_inode))
1439 undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
1443 ocfs2_clear_bit((bit_off + tmp),
1444 (unsigned long *) bg->bg_bitmap);
1445 if (ocfs2_is_cluster_bitmap(alloc_inode))
1446 ocfs2_set_bit(bit_off + tmp,
1447 (unsigned long *) undo_bg->bg_bitmap);
1449 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1451 status = ocfs2_journal_dirty(handle, group_bh);
1459 * expects the suballoc inode to already be locked.
1461 static int ocfs2_free_suballoc_bits(struct ocfs2_journal_handle *handle,
1462 struct inode *alloc_inode,
1463 struct buffer_head *alloc_bh,
1464 unsigned int start_bit,
1470 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
1471 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1472 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1473 struct buffer_head *group_bh = NULL;
1474 struct ocfs2_group_desc *group;
1478 if (!OCFS2_IS_VALID_DINODE(fe)) {
1479 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
1483 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1485 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1486 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1487 (unsigned long long)bg_blkno, start_bit);
1489 status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED,
1496 group = (struct ocfs2_group_desc *) group_bh->b_data;
1497 if (!OCFS2_IS_VALID_GROUP_DESC(group)) {
1498 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, group);
1502 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1504 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1512 status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
1513 OCFS2_JOURNAL_ACCESS_WRITE);
1519 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
1521 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1522 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
1524 status = ocfs2_journal_dirty(handle, alloc_bh);
1538 static inline u64 ocfs2_which_suballoc_group(u64 block, unsigned int bit)
1540 u64 group = block - (u64) bit;
1545 int ocfs2_free_dinode(struct ocfs2_journal_handle *handle,
1546 struct inode *inode_alloc_inode,
1547 struct buffer_head *inode_alloc_bh,
1548 struct ocfs2_dinode *di)
1550 u64 blk = le64_to_cpu(di->i_blkno);
1551 u16 bit = le16_to_cpu(di->i_suballoc_bit);
1552 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1554 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
1555 inode_alloc_bh, bit, bg_blkno, 1);
1558 int ocfs2_free_extent_block(struct ocfs2_journal_handle *handle,
1559 struct inode *eb_alloc_inode,
1560 struct buffer_head *eb_alloc_bh,
1561 struct ocfs2_extent_block *eb)
1563 u64 blk = le64_to_cpu(eb->h_blkno);
1564 u16 bit = le16_to_cpu(eb->h_suballoc_bit);
1565 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1567 return ocfs2_free_suballoc_bits(handle, eb_alloc_inode, eb_alloc_bh,
1571 int ocfs2_free_clusters(struct ocfs2_journal_handle *handle,
1572 struct inode *bitmap_inode,
1573 struct buffer_head *bitmap_bh,
1575 unsigned int num_clusters)
1580 struct ocfs2_dinode *fe;
1582 /* You can't ever have a contiguous set of clusters
1583 * bigger than a block group bitmap so we never have to worry
1584 * about looping on them. */
1588 /* This is expensive. We can safely remove once this stuff has
1589 * gotten tested really well. */
1590 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
1592 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
1594 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
1597 mlog(0, "want to free %u clusters starting at block %llu\n",
1598 num_clusters, (unsigned long long)start_blk);
1599 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1600 (unsigned long long)bg_blkno, bg_start_bit);
1602 status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
1603 bg_start_bit, bg_blkno,
1612 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
1614 printk("Block Group:\n");
1615 printk("bg_signature: %s\n", bg->bg_signature);
1616 printk("bg_size: %u\n", bg->bg_size);
1617 printk("bg_bits: %u\n", bg->bg_bits);
1618 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
1619 printk("bg_chain: %u\n", bg->bg_chain);
1620 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
1621 printk("bg_next_group: %llu\n",
1622 (unsigned long long)bg->bg_next_group);
1623 printk("bg_parent_dinode: %llu\n",
1624 (unsigned long long)bg->bg_parent_dinode);
1625 printk("bg_blkno: %llu\n",
1626 (unsigned long long)bg->bg_blkno);
1629 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
1633 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
1634 printk("i_signature: %s\n", fe->i_signature);
1635 printk("i_size: %llu\n",
1636 (unsigned long long)fe->i_size);
1637 printk("i_clusters: %u\n", fe->i_clusters);
1638 printk("i_generation: %u\n",
1639 le32_to_cpu(fe->i_generation));
1640 printk("id1.bitmap1.i_used: %u\n",
1641 le32_to_cpu(fe->id1.bitmap1.i_used));
1642 printk("id1.bitmap1.i_total: %u\n",
1643 le32_to_cpu(fe->id1.bitmap1.i_total));
1644 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
1645 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
1646 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
1647 printk("id2.i_chain.cl_next_free_rec: %u\n",
1648 fe->id2.i_chain.cl_next_free_rec);
1649 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
1650 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
1651 fe->id2.i_chain.cl_recs[i].c_free);
1652 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
1653 fe->id2.i_chain.cl_recs[i].c_total);
1654 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
1655 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);