1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * metadata alloc and free
7 * Inspired by ext3 block groups.
9 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
32 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33 #include <cluster/masklog.h>
38 #include "blockcheck.h"
42 #include "localalloc.h"
48 #include "buffer_head_io.h"
50 #define NOT_ALLOC_NEW_GROUP 0
51 #define ALLOC_NEW_GROUP 0x1
52 #define ALLOC_GROUPS_FROM_GLOBAL 0x2
54 #define OCFS2_MAX_TO_STEAL 1024
56 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
57 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
58 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
59 static int ocfs2_block_group_fill(handle_t *handle,
60 struct inode *alloc_inode,
61 struct buffer_head *bg_bh,
64 struct ocfs2_chain_list *cl);
65 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
66 struct inode *alloc_inode,
67 struct buffer_head *bh,
69 u64 *last_alloc_group,
72 static int ocfs2_cluster_group_search(struct inode *inode,
73 struct buffer_head *group_bh,
74 u32 bits_wanted, u32 min_bits,
76 u16 *bit_off, u16 *bits_found);
77 static int ocfs2_block_group_search(struct inode *inode,
78 struct buffer_head *group_bh,
79 u32 bits_wanted, u32 min_bits,
81 u16 *bit_off, u16 *bits_found);
82 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
83 struct ocfs2_alloc_context *ac,
88 unsigned int *num_bits,
90 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
92 static inline int ocfs2_block_group_set_bits(handle_t *handle,
93 struct inode *alloc_inode,
94 struct ocfs2_group_desc *bg,
95 struct buffer_head *group_bh,
97 unsigned int num_bits);
98 static int ocfs2_relink_block_group(handle_t *handle,
99 struct inode *alloc_inode,
100 struct buffer_head *fe_bh,
101 struct buffer_head *bg_bh,
102 struct buffer_head *prev_bg_bh,
104 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
106 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
109 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
113 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
114 u32 bits_wanted, u64 max_block,
116 struct ocfs2_alloc_context **ac);
118 void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
120 struct inode *inode = ac->ac_inode;
123 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
124 ocfs2_inode_unlock(inode, 1);
126 mutex_unlock(&inode->i_mutex);
135 void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
137 ocfs2_free_ac_resource(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 #define do_error(fmt, ...) \
149 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
151 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
154 static int ocfs2_validate_gd_self(struct super_block *sb,
155 struct buffer_head *bh,
158 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
160 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
161 do_error("Group descriptor #%llu has bad signature %.*s",
162 (unsigned long long)bh->b_blocknr, 7,
167 if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
168 do_error("Group descriptor #%llu has an invalid bg_blkno "
170 (unsigned long long)bh->b_blocknr,
171 (unsigned long long)le64_to_cpu(gd->bg_blkno));
175 if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
176 do_error("Group descriptor #%llu has an invalid "
177 "fs_generation of #%u",
178 (unsigned long long)bh->b_blocknr,
179 le32_to_cpu(gd->bg_generation));
183 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
184 do_error("Group descriptor #%llu has bit count %u but "
185 "claims that %u are free",
186 (unsigned long long)bh->b_blocknr,
187 le16_to_cpu(gd->bg_bits),
188 le16_to_cpu(gd->bg_free_bits_count));
192 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
193 do_error("Group descriptor #%llu has bit count %u but "
194 "max bitmap bits of %u",
195 (unsigned long long)bh->b_blocknr,
196 le16_to_cpu(gd->bg_bits),
197 8 * le16_to_cpu(gd->bg_size));
204 static int ocfs2_validate_gd_parent(struct super_block *sb,
205 struct ocfs2_dinode *di,
206 struct buffer_head *bh,
209 unsigned int max_bits;
210 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
212 if (di->i_blkno != gd->bg_parent_dinode) {
213 do_error("Group descriptor #%llu has bad parent "
214 "pointer (%llu, expected %llu)",
215 (unsigned long long)bh->b_blocknr,
216 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
217 (unsigned long long)le64_to_cpu(di->i_blkno));
221 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
222 if (le16_to_cpu(gd->bg_bits) > max_bits) {
223 do_error("Group descriptor #%llu has bit count of %u",
224 (unsigned long long)bh->b_blocknr,
225 le16_to_cpu(gd->bg_bits));
229 /* In resize, we may meet the case bg_chain == cl_next_free_rec. */
230 if ((le16_to_cpu(gd->bg_chain) >
231 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
232 ((le16_to_cpu(gd->bg_chain) ==
233 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
234 do_error("Group descriptor #%llu has bad chain %u",
235 (unsigned long long)bh->b_blocknr,
236 le16_to_cpu(gd->bg_chain));
246 * This version only prints errors. It does not fail the filesystem, and
247 * exists only for resize.
249 int ocfs2_check_group_descriptor(struct super_block *sb,
250 struct ocfs2_dinode *di,
251 struct buffer_head *bh)
254 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
256 BUG_ON(!buffer_uptodate(bh));
259 * If the ecc fails, we return the error but otherwise
260 * leave the filesystem running. We know any error is
261 * local to this block.
263 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
266 "Checksum failed for group descriptor %llu\n",
267 (unsigned long long)bh->b_blocknr);
269 rc = ocfs2_validate_gd_self(sb, bh, 1);
271 rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
276 static int ocfs2_validate_group_descriptor(struct super_block *sb,
277 struct buffer_head *bh)
280 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
282 mlog(0, "Validating group descriptor %llu\n",
283 (unsigned long long)bh->b_blocknr);
285 BUG_ON(!buffer_uptodate(bh));
288 * If the ecc fails, we return the error but otherwise
289 * leave the filesystem running. We know any error is
290 * local to this block.
292 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
297 * Errors after here are fatal.
300 return ocfs2_validate_gd_self(sb, bh, 0);
303 int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
304 u64 gd_blkno, struct buffer_head **bh)
307 struct buffer_head *tmp = *bh;
309 rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
310 ocfs2_validate_group_descriptor);
314 rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
320 /* If ocfs2_read_block() got us a new bh, pass it up. */
328 static int ocfs2_block_group_fill(handle_t *handle,
329 struct inode *alloc_inode,
330 struct buffer_head *bg_bh,
333 struct ocfs2_chain_list *cl)
336 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
337 struct super_block * sb = alloc_inode->i_sb;
341 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
342 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
344 (unsigned long long)group_blkno,
345 (unsigned long long) bg_bh->b_blocknr);
350 status = ocfs2_journal_access_gd(handle,
351 INODE_CACHE(alloc_inode),
353 OCFS2_JOURNAL_ACCESS_CREATE);
359 memset(bg, 0, sb->s_blocksize);
360 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
361 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
362 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
363 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
364 bg->bg_chain = cpu_to_le16(my_chain);
365 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
366 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
367 bg->bg_blkno = cpu_to_le64(group_blkno);
368 /* set the 1st bit in the bitmap to account for the descriptor block */
369 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
370 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
372 ocfs2_journal_dirty(handle, bg_bh);
374 /* There is no need to zero out or otherwise initialize the
375 * other blocks in a group - All valid FS metadata in a block
376 * group stores the superblock fs_generation value at
377 * allocation time. */
384 static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
389 while (curr < le16_to_cpu(cl->cl_count)) {
390 if (le32_to_cpu(cl->cl_recs[best].c_total) >
391 le32_to_cpu(cl->cl_recs[curr].c_total))
399 * We expect the block group allocator to already be locked.
401 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
402 struct inode *alloc_inode,
403 struct buffer_head *bh,
405 u64 *last_alloc_group,
409 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
410 struct ocfs2_chain_list *cl;
411 struct ocfs2_alloc_context *ac = NULL;
412 handle_t *handle = NULL;
413 u32 bit_off, num_bits;
416 struct buffer_head *bg_bh = NULL;
417 struct ocfs2_group_desc *bg;
419 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
423 cl = &fe->id2.i_chain;
424 status = ocfs2_reserve_clusters_with_limit(osb,
425 le16_to_cpu(cl->cl_cpg),
426 max_block, flags, &ac);
428 if (status != -ENOSPC)
433 credits = ocfs2_calc_group_alloc_credits(osb->sb,
434 le16_to_cpu(cl->cl_cpg));
435 handle = ocfs2_start_trans(osb, credits);
436 if (IS_ERR(handle)) {
437 status = PTR_ERR(handle);
443 if (last_alloc_group && *last_alloc_group != 0) {
444 mlog(0, "use old allocation group %llu for block group alloc\n",
445 (unsigned long long)*last_alloc_group);
446 ac->ac_last_group = *last_alloc_group;
448 status = ocfs2_claim_clusters(osb,
451 le16_to_cpu(cl->cl_cpg),
455 if (status != -ENOSPC)
460 alloc_rec = ocfs2_find_smallest_chain(cl);
462 /* setup the group */
463 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
464 mlog(0, "new descriptor, record %u, at block %llu\n",
465 alloc_rec, (unsigned long long)bg_blkno);
467 bg_bh = sb_getblk(osb->sb, bg_blkno);
473 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
475 status = ocfs2_block_group_fill(handle,
486 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
488 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
489 bh, OCFS2_JOURNAL_ACCESS_WRITE);
495 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
496 le16_to_cpu(bg->bg_free_bits_count));
497 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
498 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno);
499 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
500 le16_add_cpu(&cl->cl_next_free_rec, 1);
502 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
503 le16_to_cpu(bg->bg_free_bits_count));
504 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
505 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
507 ocfs2_journal_dirty(handle, bh);
509 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
510 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
511 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
512 le32_to_cpu(fe->i_clusters)));
513 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
514 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
515 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
519 /* save the new last alloc group so that the caller can cache it. */
520 if (last_alloc_group)
521 *last_alloc_group = ac->ac_last_group;
525 ocfs2_commit_trans(osb, handle);
528 ocfs2_free_alloc_context(ac);
536 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
537 struct ocfs2_alloc_context *ac,
540 u64 *last_alloc_group,
544 u32 bits_wanted = ac->ac_bits_wanted;
545 struct inode *alloc_inode;
546 struct buffer_head *bh = NULL;
547 struct ocfs2_dinode *fe;
552 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
558 mutex_lock(&alloc_inode->i_mutex);
560 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
562 mutex_unlock(&alloc_inode->i_mutex);
569 ac->ac_inode = alloc_inode;
570 ac->ac_alloc_slot = slot;
572 fe = (struct ocfs2_dinode *) bh->b_data;
574 /* The bh was validated by the inode read inside
575 * ocfs2_inode_lock(). Any corruption is a code bug. */
576 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
578 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
579 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
580 (unsigned long long)le64_to_cpu(fe->i_blkno));
585 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
586 le32_to_cpu(fe->id1.bitmap1.i_used);
588 if (bits_wanted > free_bits) {
589 /* cluster bitmap never grows */
590 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
591 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
592 bits_wanted, free_bits);
597 if (!(flags & ALLOC_NEW_GROUP)) {
598 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
599 "and we don't alloc a new group for it.\n",
600 slot, bits_wanted, free_bits);
605 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
607 last_alloc_group, flags);
609 if (status != -ENOSPC)
613 atomic_inc(&osb->alloc_stats.bg_extends);
615 /* You should never ask for this much metadata */
617 (le32_to_cpu(fe->id1.bitmap1.i_total)
618 - le32_to_cpu(fe->id1.bitmap1.i_used)));
630 static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
632 spin_lock(&osb->osb_lock);
633 osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
634 spin_unlock(&osb->osb_lock);
635 atomic_set(&osb->s_num_inodes_stolen, 0);
638 static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
640 spin_lock(&osb->osb_lock);
641 osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
642 spin_unlock(&osb->osb_lock);
643 atomic_set(&osb->s_num_meta_stolen, 0);
646 void ocfs2_init_steal_slots(struct ocfs2_super *osb)
648 ocfs2_init_inode_steal_slot(osb);
649 ocfs2_init_meta_steal_slot(osb);
652 static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
654 spin_lock(&osb->osb_lock);
655 if (type == INODE_ALLOC_SYSTEM_INODE)
656 osb->s_inode_steal_slot = slot;
657 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
658 osb->s_meta_steal_slot = slot;
659 spin_unlock(&osb->osb_lock);
662 static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
664 int slot = OCFS2_INVALID_SLOT;
666 spin_lock(&osb->osb_lock);
667 if (type == INODE_ALLOC_SYSTEM_INODE)
668 slot = osb->s_inode_steal_slot;
669 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
670 slot = osb->s_meta_steal_slot;
671 spin_unlock(&osb->osb_lock);
676 static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
678 return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
681 static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
683 return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
686 static int ocfs2_steal_resource(struct ocfs2_super *osb,
687 struct ocfs2_alloc_context *ac,
690 int i, status = -ENOSPC;
691 int slot = __ocfs2_get_steal_slot(osb, type);
693 /* Start to steal resource from the first slot after ours. */
694 if (slot == OCFS2_INVALID_SLOT)
695 slot = osb->slot_num + 1;
697 for (i = 0; i < osb->max_slots; i++, slot++) {
698 if (slot == osb->max_slots)
701 if (slot == osb->slot_num)
704 status = ocfs2_reserve_suballoc_bits(osb, ac,
707 NOT_ALLOC_NEW_GROUP);
709 __ocfs2_set_steal_slot(osb, slot, type);
713 ocfs2_free_ac_resource(ac);
719 static int ocfs2_steal_inode(struct ocfs2_super *osb,
720 struct ocfs2_alloc_context *ac)
722 return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
725 static int ocfs2_steal_meta(struct ocfs2_super *osb,
726 struct ocfs2_alloc_context *ac)
728 return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
731 int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
733 struct ocfs2_alloc_context **ac)
736 int slot = ocfs2_get_meta_steal_slot(osb);
738 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
745 (*ac)->ac_bits_wanted = blocks;
746 (*ac)->ac_which = OCFS2_AC_USE_META;
747 (*ac)->ac_group_search = ocfs2_block_group_search;
749 if (slot != OCFS2_INVALID_SLOT &&
750 atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
753 atomic_set(&osb->s_num_meta_stolen, 0);
754 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
755 EXTENT_ALLOC_SYSTEM_INODE,
756 (u32)osb->slot_num, NULL,
762 if (slot != OCFS2_INVALID_SLOT)
763 ocfs2_init_meta_steal_slot(osb);
765 } else if (status < 0 && status != -ENOSPC) {
770 ocfs2_free_ac_resource(*ac);
773 status = ocfs2_steal_meta(osb, *ac);
774 atomic_inc(&osb->s_num_meta_stolen);
776 if (status != -ENOSPC)
783 if ((status < 0) && *ac) {
784 ocfs2_free_alloc_context(*ac);
792 int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
793 struct ocfs2_extent_list *root_el,
794 struct ocfs2_alloc_context **ac)
796 return ocfs2_reserve_new_metadata_blocks(osb,
797 ocfs2_extend_meta_needed(root_el),
801 int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
802 struct ocfs2_alloc_context **ac)
805 int slot = ocfs2_get_inode_steal_slot(osb);
808 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
815 (*ac)->ac_bits_wanted = 1;
816 (*ac)->ac_which = OCFS2_AC_USE_INODE;
818 (*ac)->ac_group_search = ocfs2_block_group_search;
821 * stat(2) can't handle i_ino > 32bits, so we tell the
822 * lower levels not to allocate us a block group past that
823 * limit. The 'inode64' mount option avoids this behavior.
825 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
826 (*ac)->ac_max_block = (u32)~0U;
829 * slot is set when we successfully steal inode from other nodes.
830 * It is reset in 3 places:
831 * 1. when we flush the truncate log
832 * 2. when we complete local alloc recovery.
833 * 3. when we successfully allocate from our own slot.
834 * After it is set, we will go on stealing inodes until we find the
835 * need to check our slots to see whether there is some space for us.
837 if (slot != OCFS2_INVALID_SLOT &&
838 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
841 atomic_set(&osb->s_num_inodes_stolen, 0);
842 alloc_group = osb->osb_inode_alloc_group;
843 status = ocfs2_reserve_suballoc_bits(osb, *ac,
844 INODE_ALLOC_SYSTEM_INODE,
848 ALLOC_GROUPS_FROM_GLOBAL);
852 spin_lock(&osb->osb_lock);
853 osb->osb_inode_alloc_group = alloc_group;
854 spin_unlock(&osb->osb_lock);
855 mlog(0, "after reservation, new allocation group is "
856 "%llu\n", (unsigned long long)alloc_group);
859 * Some inodes must be freed by us, so try to allocate
860 * from our own next time.
862 if (slot != OCFS2_INVALID_SLOT)
863 ocfs2_init_inode_steal_slot(osb);
865 } else if (status < 0 && status != -ENOSPC) {
870 ocfs2_free_ac_resource(*ac);
873 status = ocfs2_steal_inode(osb, *ac);
874 atomic_inc(&osb->s_num_inodes_stolen);
876 if (status != -ENOSPC)
883 if ((status < 0) && *ac) {
884 ocfs2_free_alloc_context(*ac);
892 /* local alloc code has to do the same thing, so rather than do this
894 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
895 struct ocfs2_alloc_context *ac)
899 ac->ac_which = OCFS2_AC_USE_MAIN;
900 ac->ac_group_search = ocfs2_cluster_group_search;
902 status = ocfs2_reserve_suballoc_bits(osb, ac,
903 GLOBAL_BITMAP_SYSTEM_INODE,
904 OCFS2_INVALID_SLOT, NULL,
906 if (status < 0 && status != -ENOSPC) {
915 /* Callers don't need to care which bitmap (local alloc or main) to
916 * use so we figure it out for them, but unfortunately this clutters
918 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
919 u32 bits_wanted, u64 max_block,
921 struct ocfs2_alloc_context **ac)
927 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
934 (*ac)->ac_bits_wanted = bits_wanted;
935 (*ac)->ac_max_block = max_block;
938 if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
939 ocfs2_alloc_should_use_local(osb, bits_wanted)) {
940 status = ocfs2_reserve_local_alloc_bits(osb,
943 if (status == -EFBIG) {
944 /* The local alloc window is outside ac_max_block.
945 * use the main bitmap. */
947 } else if ((status < 0) && (status != -ENOSPC)) {
953 if (status == -ENOSPC) {
954 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
956 if (status != -ENOSPC)
964 if ((status < 0) && *ac) {
965 ocfs2_free_alloc_context(*ac);
973 int ocfs2_reserve_clusters(struct ocfs2_super *osb,
975 struct ocfs2_alloc_context **ac)
977 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
978 ALLOC_NEW_GROUP, ac);
982 * More or less lifted from ext3. I'll leave their description below:
984 * "For ext3 allocations, we must not reuse any blocks which are
985 * allocated in the bitmap buffer's "last committed data" copy. This
986 * prevents deletes from freeing up the page for reuse until we have
987 * committed the delete transaction.
989 * If we didn't do this, then deleting something and reallocating it as
990 * data would allow the old block to be overwritten before the
991 * transaction committed (because we force data to disk before commit).
992 * This would lead to corruption if we crashed between overwriting the
993 * data and committing the delete.
995 * @@@ We may want to make this allocation behaviour conditional on
996 * data-writes at some point, and disable it for metadata allocations or
999 * Note: OCFS2 already does this differently for metadata vs data
1000 * allocations, as those bitmaps are separate and undo access is never
1001 * called on a metadata group descriptor.
1003 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
1006 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1009 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
1012 if (!buffer_jbd(bg_bh))
1015 jbd_lock_bh_state(bg_bh);
1016 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
1018 ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
1021 jbd_unlock_bh_state(bg_bh);
1026 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1027 struct buffer_head *bg_bh,
1028 unsigned int bits_wanted,
1029 unsigned int total_bits,
1034 u16 best_offset, best_size;
1035 int offset, start, found, status = 0;
1036 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1038 /* Callers got this descriptor from
1039 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1040 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1042 found = start = best_offset = best_size = 0;
1043 bitmap = bg->bg_bitmap;
1045 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
1046 if (offset == total_bits)
1049 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
1050 /* We found a zero, but we can't use it as it
1051 * hasn't been put to disk yet! */
1054 } else if (offset == start) {
1055 /* we found a zero */
1057 /* move start to the next bit to test */
1060 /* got a zero after some ones */
1064 if (found > best_size) {
1066 best_offset = start - found;
1068 /* we got everything we needed */
1069 if (found == bits_wanted) {
1070 /* mlog(0, "Found it all!\n"); */
1075 /* XXX: I think the first clause is equivalent to the second
1077 if (found == bits_wanted) {
1078 *bit_off = start - found;
1079 *bits_found = found;
1080 } else if (best_size) {
1081 *bit_off = best_offset;
1082 *bits_found = best_size;
1085 /* No error log here -- see the comment above
1086 * ocfs2_test_bg_bit_allocatable */
1092 static inline int ocfs2_block_group_set_bits(handle_t *handle,
1093 struct inode *alloc_inode,
1094 struct ocfs2_group_desc *bg,
1095 struct buffer_head *group_bh,
1096 unsigned int bit_off,
1097 unsigned int num_bits)
1100 void *bitmap = bg->bg_bitmap;
1101 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1105 /* All callers get the descriptor via
1106 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1107 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1108 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
1110 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
1113 if (ocfs2_is_cluster_bitmap(alloc_inode))
1114 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1116 status = ocfs2_journal_access_gd(handle,
1117 INODE_CACHE(alloc_inode),
1125 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
1127 ocfs2_set_bit(bit_off++, bitmap);
1129 ocfs2_journal_dirty(handle, group_bh);
1136 /* find the one with the most empty bits */
1137 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1141 BUG_ON(!cl->cl_next_free_rec);
1144 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1145 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1146 le32_to_cpu(cl->cl_recs[best].c_free))
1151 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1155 static int ocfs2_relink_block_group(handle_t *handle,
1156 struct inode *alloc_inode,
1157 struct buffer_head *fe_bh,
1158 struct buffer_head *bg_bh,
1159 struct buffer_head *prev_bg_bh,
1163 /* there is a really tiny chance the journal calls could fail,
1164 * but we wouldn't want inconsistent blocks in *any* case. */
1165 u64 fe_ptr, bg_ptr, prev_bg_ptr;
1166 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1167 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1168 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1170 /* The caller got these descriptors from
1171 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1172 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1173 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
1175 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
1176 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1177 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1178 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
1180 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
1181 bg_ptr = le64_to_cpu(bg->bg_next_group);
1182 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1184 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1186 OCFS2_JOURNAL_ACCESS_WRITE);
1192 prev_bg->bg_next_group = bg->bg_next_group;
1193 ocfs2_journal_dirty(handle, prev_bg_bh);
1195 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1196 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
1202 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
1203 ocfs2_journal_dirty(handle, bg_bh);
1205 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
1206 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
1212 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
1213 ocfs2_journal_dirty(handle, fe_bh);
1217 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1218 bg->bg_next_group = cpu_to_le64(bg_ptr);
1219 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1226 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1229 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1232 /* return 0 on success, -ENOSPC to keep searching and any other < 0
1233 * value on error. */
1234 static int ocfs2_cluster_group_search(struct inode *inode,
1235 struct buffer_head *group_bh,
1236 u32 bits_wanted, u32 min_bits,
1238 u16 *bit_off, u16 *bits_found)
1240 int search = -ENOSPC;
1243 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
1244 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1245 u16 tmp_off, tmp_found;
1246 unsigned int max_bits, gd_cluster_off;
1248 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1250 if (gd->bg_free_bits_count) {
1251 max_bits = le16_to_cpu(gd->bg_bits);
1253 /* Tail groups in cluster bitmaps which aren't cpg
1254 * aligned are prone to partial extention by a failed
1255 * fs resize. If the file system resize never got to
1256 * update the dinode cluster count, then we don't want
1257 * to trust any clusters past it, regardless of what
1258 * the group descriptor says. */
1259 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1260 le64_to_cpu(gd->bg_blkno));
1261 if ((gd_cluster_off + max_bits) >
1262 OCFS2_I(inode)->ip_clusters) {
1263 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1264 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1265 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1266 le16_to_cpu(gd->bg_bits),
1267 OCFS2_I(inode)->ip_clusters, max_bits);
1270 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1271 group_bh, bits_wanted,
1273 &tmp_off, &tmp_found);
1278 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1280 tmp_off + tmp_found);
1281 mlog(0, "Checking %llu against %llu\n",
1282 (unsigned long long)blkoff,
1283 (unsigned long long)max_block);
1284 if (blkoff > max_block)
1288 /* ocfs2_block_group_find_clear_bits() might
1289 * return success, but we still want to return
1290 * -ENOSPC unless it found the minimum number
1292 if (min_bits <= tmp_found) {
1294 *bits_found = tmp_found;
1295 search = 0; /* success */
1296 } else if (tmp_found) {
1298 * Don't show bits which we'll be returning
1299 * for allocation to the local alloc bitmap.
1301 ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
1308 static int ocfs2_block_group_search(struct inode *inode,
1309 struct buffer_head *group_bh,
1310 u32 bits_wanted, u32 min_bits,
1312 u16 *bit_off, u16 *bits_found)
1316 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1318 BUG_ON(min_bits != 1);
1319 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1321 if (bg->bg_free_bits_count) {
1322 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1323 group_bh, bits_wanted,
1324 le16_to_cpu(bg->bg_bits),
1325 bit_off, bits_found);
1326 if (!ret && max_block) {
1327 blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
1329 mlog(0, "Checking %llu against %llu\n",
1330 (unsigned long long)blkoff,
1331 (unsigned long long)max_block);
1332 if (blkoff > max_block)
1340 static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
1342 struct buffer_head *di_bh,
1348 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1349 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1351 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
1352 OCFS2_JOURNAL_ACCESS_WRITE);
1358 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1359 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1360 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1361 ocfs2_journal_dirty(handle, di_bh);
1367 static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1372 unsigned int *num_bits,
1378 struct buffer_head *group_bh = NULL;
1379 struct ocfs2_group_desc *gd;
1380 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1381 struct inode *alloc_inode = ac->ac_inode;
1383 ret = ocfs2_read_group_descriptor(alloc_inode, di, gd_blkno,
1390 gd = (struct ocfs2_group_desc *) group_bh->b_data;
1391 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1392 ac->ac_max_block, bit_off, &found);
1401 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1403 le16_to_cpu(gd->bg_chain));
1409 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1410 *bit_off, *num_bits);
1414 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1422 static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1427 unsigned int *num_bits,
1432 u16 chain, tmp_bits;
1435 struct inode *alloc_inode = ac->ac_inode;
1436 struct buffer_head *group_bh = NULL;
1437 struct buffer_head *prev_group_bh = NULL;
1438 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1439 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1440 struct ocfs2_group_desc *bg;
1442 chain = ac->ac_chain;
1443 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1445 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
1447 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1448 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1454 bg = (struct ocfs2_group_desc *) group_bh->b_data;
1457 /* for now, the chain search is a bit simplistic. We just use
1458 * the 1st group with any empty bits. */
1459 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1460 bits_wanted, min_bits,
1461 ac->ac_max_block, bit_off,
1462 &tmp_bits)) == -ENOSPC) {
1463 if (!bg->bg_next_group)
1466 brelse(prev_group_bh);
1467 prev_group_bh = NULL;
1469 next_group = le64_to_cpu(bg->bg_next_group);
1470 prev_group_bh = group_bh;
1472 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1473 next_group, &group_bh);
1478 bg = (struct ocfs2_group_desc *) group_bh->b_data;
1481 if (status != -ENOSPC)
1486 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1487 tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
1489 *num_bits = tmp_bits;
1491 BUG_ON(*num_bits == 0);
1494 * Keep track of previous block descriptor read. When
1495 * we find a target, if we have read more than X
1496 * number of descriptors, and the target is reasonably
1497 * empty, relink him to top of his chain.
1499 * We've read 0 extra blocks and only send one more to
1500 * the transaction, yet the next guy to search has a
1503 * Do this *after* figuring out how many bits we're taking out
1504 * of our target group.
1506 if (ac->ac_allow_chain_relink &&
1508 (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1509 status = ocfs2_relink_block_group(handle, alloc_inode,
1510 ac->ac_bh, group_bh,
1511 prev_group_bh, chain);
1518 /* Ok, claim our bits now: set the info on dinode, chainlist
1519 * and then the group */
1520 status = ocfs2_journal_access_di(handle,
1521 INODE_CACHE(alloc_inode),
1523 OCFS2_JOURNAL_ACCESS_WRITE);
1529 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1530 fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1531 le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1532 ocfs2_journal_dirty(handle, ac->ac_bh);
1534 status = ocfs2_block_group_set_bits(handle,
1545 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
1546 (unsigned long long)le64_to_cpu(fe->i_blkno));
1548 *bg_blkno = le64_to_cpu(bg->bg_blkno);
1549 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
1552 brelse(prev_group_bh);
1558 /* will give out up to bits_wanted contiguous bits. */
1559 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1560 struct ocfs2_alloc_context *ac,
1565 unsigned int *num_bits,
1571 u64 hint_blkno = ac->ac_last_group;
1572 struct ocfs2_chain_list *cl;
1573 struct ocfs2_dinode *fe;
1577 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1578 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1581 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1583 /* The bh was validated by the inode read during
1584 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1585 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1587 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1588 le32_to_cpu(fe->id1.bitmap1.i_total)) {
1589 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1590 "bits but only %u total.",
1591 (unsigned long long)le64_to_cpu(fe->i_blkno),
1592 le32_to_cpu(fe->id1.bitmap1.i_used),
1593 le32_to_cpu(fe->id1.bitmap1.i_total));
1599 /* Attempt to short-circuit the usual search mechanism
1600 * by jumping straight to the most recently used
1601 * allocation group. This helps us mantain some
1602 * contiguousness across allocations. */
1603 status = ocfs2_search_one_group(ac, handle, bits_wanted,
1604 min_bits, bit_off, num_bits,
1605 hint_blkno, &bits_left);
1607 /* Be careful to update *bg_blkno here as the
1608 * caller is expecting it to be filled in, and
1609 * ocfs2_search_one_group() won't do that for
1611 *bg_blkno = hint_blkno;
1614 if (status < 0 && status != -ENOSPC) {
1620 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1622 victim = ocfs2_find_victim_chain(cl);
1623 ac->ac_chain = victim;
1624 ac->ac_allow_chain_relink = 1;
1626 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
1627 num_bits, bg_blkno, &bits_left);
1630 if (status < 0 && status != -ENOSPC) {
1635 mlog(0, "Search of victim chain %u came up with nothing, "
1636 "trying all chains now.\n", victim);
1638 /* If we didn't pick a good victim, then just default to
1639 * searching each chain in order. Don't allow chain relinking
1640 * because we only calculate enough journal credits for one
1641 * relink per alloc. */
1642 ac->ac_allow_chain_relink = 0;
1643 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1646 if (!cl->cl_recs[i].c_free)
1650 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1651 bit_off, num_bits, bg_blkno,
1655 if (status < 0 && status != -ENOSPC) {
1662 if (status != -ENOSPC) {
1663 /* If the next search of this group is not likely to
1664 * yield a suitable extent, then we reset the last
1665 * group hint so as to not waste a disk read */
1666 if (bits_left < min_bits)
1667 ac->ac_last_group = 0;
1669 ac->ac_last_group = *bg_blkno;
1677 int ocfs2_claim_metadata(struct ocfs2_super *osb,
1679 struct ocfs2_alloc_context *ac,
1681 u16 *suballoc_bit_start,
1682 unsigned int *num_bits,
1689 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1690 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
1692 status = ocfs2_claim_suballoc_bits(osb,
1704 atomic_inc(&osb->alloc_stats.bg_allocs);
1706 *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1707 ac->ac_bits_given += (*num_bits);
1714 static void ocfs2_init_inode_ac_group(struct inode *dir,
1715 struct buffer_head *parent_fe_bh,
1716 struct ocfs2_alloc_context *ac)
1718 struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1720 * Try to allocate inodes from some specific group.
1722 * If the parent dir has recorded the last group used in allocation,
1723 * cool, use it. Otherwise if we try to allocate new inode from the
1724 * same slot the parent dir belongs to, use the same chunk.
1726 * We are very careful here to avoid the mistake of setting
1727 * ac_last_group to a group descriptor from a different (unlocked) slot.
1729 if (OCFS2_I(dir)->ip_last_used_group &&
1730 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
1731 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
1732 else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
1733 ac->ac_last_group = ocfs2_which_suballoc_group(
1734 le64_to_cpu(fe->i_blkno),
1735 le16_to_cpu(fe->i_suballoc_bit));
1738 static inline void ocfs2_save_inode_ac_group(struct inode *dir,
1739 struct ocfs2_alloc_context *ac)
1741 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
1742 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
1745 int ocfs2_claim_new_inode(struct ocfs2_super *osb,
1748 struct buffer_head *parent_fe_bh,
1749 struct ocfs2_alloc_context *ac,
1754 unsigned int num_bits;
1760 BUG_ON(ac->ac_bits_given != 0);
1761 BUG_ON(ac->ac_bits_wanted != 1);
1762 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
1764 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
1766 status = ocfs2_claim_suballoc_bits(osb,
1778 atomic_inc(&osb->alloc_stats.bg_allocs);
1780 BUG_ON(num_bits != 1);
1782 *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1783 ac->ac_bits_given++;
1784 ocfs2_save_inode_ac_group(dir, ac);
1791 /* translate a group desc. blkno and it's bitmap offset into
1792 * disk cluster offset. */
1793 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1797 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1800 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1802 if (bg_blkno != osb->first_cluster_group_blkno)
1803 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1804 cluster += (u32) bg_bit_off;
1808 /* given a cluster offset, calculate which block group it belongs to
1809 * and return that block offset. */
1810 u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
1812 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1815 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1817 group_no = cluster / osb->bitmap_cpg;
1819 return osb->first_cluster_group_blkno;
1820 return ocfs2_clusters_to_blocks(inode->i_sb,
1821 group_no * osb->bitmap_cpg);
1824 /* given the block number of a cluster start, calculate which cluster
1825 * group and descriptor bitmap offset that corresponds to. */
1826 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1831 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1832 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1834 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1836 *bg_blkno = ocfs2_which_cluster_group(inode,
1839 if (*bg_blkno == osb->first_cluster_group_blkno)
1840 *bg_bit_off = (u16) data_cluster;
1842 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1843 data_blkno - *bg_blkno);
1847 * min_bits - minimum contiguous chunk from this total allocation we
1848 * can handle. set to what we asked for originally for a full
1849 * contig. allocation, set to '1' to indicate we can deal with extents
1852 int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1854 struct ocfs2_alloc_context *ac,
1861 unsigned int bits_wanted = max_clusters;
1867 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1869 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1870 && ac->ac_which != OCFS2_AC_USE_MAIN);
1872 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1873 status = ocfs2_claim_local_alloc_bits(osb,
1880 atomic_inc(&osb->alloc_stats.local_data);
1882 if (min_clusters > (osb->bitmap_cpg - 1)) {
1883 /* The only paths asking for contiguousness
1884 * should know about this already. */
1885 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
1886 "group bitmap size %u!\n", min_clusters,
1891 /* clamp the current request down to a realistic size. */
1892 if (bits_wanted > (osb->bitmap_cpg - 1))
1893 bits_wanted = osb->bitmap_cpg - 1;
1895 status = ocfs2_claim_suballoc_bits(osb,
1905 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1908 atomic_inc(&osb->alloc_stats.bitmap_data);
1912 if (status != -ENOSPC)
1917 ac->ac_bits_given += *num_clusters;
1924 int ocfs2_claim_clusters(struct ocfs2_super *osb,
1926 struct ocfs2_alloc_context *ac,
1931 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1933 return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1934 bits_wanted, cluster_start, num_clusters);
1937 static int ocfs2_block_group_clear_bits(handle_t *handle,
1938 struct inode *alloc_inode,
1939 struct ocfs2_group_desc *bg,
1940 struct buffer_head *group_bh,
1941 unsigned int bit_off,
1942 unsigned int num_bits,
1943 void (*undo_fn)(unsigned int bit,
1944 unsigned long *bmap))
1948 struct ocfs2_group_desc *undo_bg = NULL;
1952 /* The caller got this descriptor from
1953 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1954 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1956 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1958 BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
1959 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1962 OCFS2_JOURNAL_ACCESS_UNDO :
1963 OCFS2_JOURNAL_ACCESS_WRITE);
1970 jbd_lock_bh_state(group_bh);
1971 undo_bg = (struct ocfs2_group_desc *)
1972 bh2jh(group_bh)->b_committed_data;
1978 ocfs2_clear_bit((bit_off + tmp),
1979 (unsigned long *) bg->bg_bitmap);
1981 undo_fn(bit_off + tmp,
1982 (unsigned long *) undo_bg->bg_bitmap);
1984 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1987 jbd_unlock_bh_state(group_bh);
1989 ocfs2_journal_dirty(handle, group_bh);
1995 * expects the suballoc inode to already be locked.
1997 static int _ocfs2_free_suballoc_bits(handle_t *handle,
1998 struct inode *alloc_inode,
1999 struct buffer_head *alloc_bh,
2000 unsigned int start_bit,
2003 void (*undo_fn)(unsigned int bit,
2004 unsigned long *bitmap))
2008 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2009 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2010 struct buffer_head *group_bh = NULL;
2011 struct ocfs2_group_desc *group;
2015 /* The alloc_bh comes from ocfs2_free_dinode() or
2016 * ocfs2_free_clusters(). The callers have all locked the
2017 * allocator and gotten alloc_bh from the lock call. This
2018 * validates the dinode buffer. Any corruption that has happended
2020 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
2021 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2023 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2024 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
2025 (unsigned long long)bg_blkno, start_bit);
2027 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2033 group = (struct ocfs2_group_desc *) group_bh->b_data;
2035 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2037 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2039 start_bit, count, undo_fn);
2045 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2046 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
2052 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2054 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2055 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
2056 ocfs2_journal_dirty(handle, alloc_bh);
2065 int ocfs2_free_suballoc_bits(handle_t *handle,
2066 struct inode *alloc_inode,
2067 struct buffer_head *alloc_bh,
2068 unsigned int start_bit,
2072 return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2073 start_bit, bg_blkno, count, NULL);
2076 int ocfs2_free_dinode(handle_t *handle,
2077 struct inode *inode_alloc_inode,
2078 struct buffer_head *inode_alloc_bh,
2079 struct ocfs2_dinode *di)
2081 u64 blk = le64_to_cpu(di->i_blkno);
2082 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2083 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2085 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2086 inode_alloc_bh, bit, bg_blkno, 1);
2089 static int _ocfs2_free_clusters(handle_t *handle,
2090 struct inode *bitmap_inode,
2091 struct buffer_head *bitmap_bh,
2093 unsigned int num_clusters,
2094 void (*undo_fn)(unsigned int bit,
2095 unsigned long *bitmap))
2100 struct ocfs2_dinode *fe;
2102 /* You can't ever have a contiguous set of clusters
2103 * bigger than a block group bitmap so we never have to worry
2104 * about looping on them. */
2108 /* This is expensive. We can safely remove once this stuff has
2109 * gotten tested really well. */
2110 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2112 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2114 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2117 mlog(0, "want to free %u clusters starting at block %llu\n",
2118 num_clusters, (unsigned long long)start_blk);
2119 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2120 (unsigned long long)bg_blkno, bg_start_bit);
2122 status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2123 bg_start_bit, bg_blkno,
2124 num_clusters, undo_fn);
2130 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2138 int ocfs2_free_clusters(handle_t *handle,
2139 struct inode *bitmap_inode,
2140 struct buffer_head *bitmap_bh,
2142 unsigned int num_clusters)
2144 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2145 start_blk, num_clusters,
2150 * Give never-used clusters back to the global bitmap. We don't need
2151 * to protect these bits in the undo buffer.
2153 int ocfs2_release_clusters(handle_t *handle,
2154 struct inode *bitmap_inode,
2155 struct buffer_head *bitmap_bh,
2157 unsigned int num_clusters)
2159 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2160 start_blk, num_clusters,
2164 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2166 printk("Block Group:\n");
2167 printk("bg_signature: %s\n", bg->bg_signature);
2168 printk("bg_size: %u\n", bg->bg_size);
2169 printk("bg_bits: %u\n", bg->bg_bits);
2170 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2171 printk("bg_chain: %u\n", bg->bg_chain);
2172 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
2173 printk("bg_next_group: %llu\n",
2174 (unsigned long long)bg->bg_next_group);
2175 printk("bg_parent_dinode: %llu\n",
2176 (unsigned long long)bg->bg_parent_dinode);
2177 printk("bg_blkno: %llu\n",
2178 (unsigned long long)bg->bg_blkno);
2181 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2185 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
2186 printk("i_signature: %s\n", fe->i_signature);
2187 printk("i_size: %llu\n",
2188 (unsigned long long)fe->i_size);
2189 printk("i_clusters: %u\n", fe->i_clusters);
2190 printk("i_generation: %u\n",
2191 le32_to_cpu(fe->i_generation));
2192 printk("id1.bitmap1.i_used: %u\n",
2193 le32_to_cpu(fe->id1.bitmap1.i_used));
2194 printk("id1.bitmap1.i_total: %u\n",
2195 le32_to_cpu(fe->id1.bitmap1.i_total));
2196 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2197 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2198 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2199 printk("id2.i_chain.cl_next_free_rec: %u\n",
2200 fe->id2.i_chain.cl_next_free_rec);
2201 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2202 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2203 fe->id2.i_chain.cl_recs[i].c_free);
2204 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2205 fe->id2.i_chain.cl_recs[i].c_total);
2206 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2207 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
2212 * For a given allocation, determine which allocators will need to be
2213 * accessed, and lock them, reserving the appropriate number of bits.
2215 * Sparse file systems call this from ocfs2_write_begin_nolock()
2216 * and ocfs2_allocate_unwritten_extents().
2218 * File systems which don't support holes call this from
2219 * ocfs2_extend_allocation().
2221 int ocfs2_lock_allocators(struct inode *inode,
2222 struct ocfs2_extent_tree *et,
2223 u32 clusters_to_add, u32 extents_to_split,
2224 struct ocfs2_alloc_context **data_ac,
2225 struct ocfs2_alloc_context **meta_ac)
2227 int ret = 0, num_free_extents;
2228 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2229 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2235 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2237 num_free_extents = ocfs2_num_free_extents(osb, et);
2238 if (num_free_extents < 0) {
2239 ret = num_free_extents;
2245 * Sparse allocation file systems need to be more conservative
2246 * with reserving room for expansion - the actual allocation
2247 * happens while we've got a journal handle open so re-taking
2248 * a cluster lock (because we ran out of room for another
2249 * extent) will violate ordering rules.
2251 * Most of the time we'll only be seeing this 1 cluster at a time
2254 * Always lock for any unwritten extents - we might want to
2255 * add blocks during a split.
2257 if (!num_free_extents ||
2258 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
2259 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
2267 if (clusters_to_add == 0)
2270 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2280 ocfs2_free_alloc_context(*meta_ac);
2285 * We cannot have an error and a non null *data_ac.
2293 * Read the inode specified by blkno to get suballoc_slot and
2296 static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2297 u16 *suballoc_slot, u16 *suballoc_bit)
2300 struct buffer_head *inode_bh = NULL;
2301 struct ocfs2_dinode *inode_fe;
2303 mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
2305 /* dirty read disk */
2306 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2308 mlog(ML_ERROR, "read block %llu failed %d\n",
2309 (unsigned long long)blkno, status);
2313 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2314 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
2315 mlog(ML_ERROR, "invalid inode %llu requested\n",
2316 (unsigned long long)blkno);
2321 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
2322 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2323 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
2324 (unsigned long long)blkno,
2325 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
2331 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2333 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2343 * test whether bit is SET in allocator bitmap or not. on success, 0
2344 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2345 * is returned and *res is meaningless. Call this after you have
2346 * cluster locked against suballoc, or you may get a result based on
2347 * non-up2date contents
2349 static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2350 struct inode *suballoc,
2351 struct buffer_head *alloc_bh, u64 blkno,
2354 struct ocfs2_dinode *alloc_fe;
2355 struct ocfs2_group_desc *group;
2356 struct buffer_head *group_bh = NULL;
2360 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2363 alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
2364 if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
2365 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2367 ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
2372 bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
2373 status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
2376 mlog(ML_ERROR, "read group %llu failed %d\n",
2377 (unsigned long long)bg_blkno, status);
2381 group = (struct ocfs2_group_desc *) group_bh->b_data;
2382 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2392 * Test if the bit representing this inode (blkno) is set in the
2395 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2397 * In the event of failure, a negative value is returned and *res is
2400 * Callers must make sure to hold nfs_sync_lock to prevent
2401 * ocfs2_delete_inode() on another node from accessing the same
2402 * suballocator concurrently.
2404 int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2407 u16 suballoc_bit = 0, suballoc_slot = 0;
2408 struct inode *inode_alloc_inode;
2409 struct buffer_head *alloc_bh = NULL;
2411 mlog_entry("blkno: %llu", (unsigned long long)blkno);
2413 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2416 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2421 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2423 if (!inode_alloc_inode) {
2424 /* the error code could be inaccurate, but we are not able to
2425 * get the correct one. */
2427 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2428 (u32)suballoc_slot);
2432 mutex_lock(&inode_alloc_inode->i_mutex);
2433 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2435 mutex_unlock(&inode_alloc_inode->i_mutex);
2436 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2437 (u32)suballoc_slot, status);
2441 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2442 blkno, suballoc_bit, res);
2444 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2446 ocfs2_inode_unlock(inode_alloc_inode, 0);
2447 mutex_unlock(&inode_alloc_inode->i_mutex);
2449 iput(inode_alloc_inode);