struct ocfs2_suballoc_result {
u64 sr_bg_blkno; /* The bg we allocated from. Set to 0 when a block group is
contiguous. */
u64 sr_bg_stable_blkno; /* * Doesn't change, always * set to target block * group descriptor * block.
*/
u64 sr_blkno; /* The first allocated block */ unsignedint sr_bit_offset; /* The bit in the bg */ unsignedint sr_bits; /* How many bits we claimed */ unsignedint sr_max_contig_bits; /* The length for contiguous * free bits, only available * for cluster group
*/
};
if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
do_error("Group descriptor #%llu has bad signature %.*s\n",
(unsignedlonglong)bh->b_blocknr, 7,
gd->bg_signature);
}
if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
do_error("Group descriptor #%llu has an invalid bg_blkno of %llu\n",
(unsignedlonglong)bh->b_blocknr,
(unsignedlonglong)le64_to_cpu(gd->bg_blkno));
}
if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
do_error("Group descriptor #%llu has an invalid fs_generation of #%u\n",
(unsignedlonglong)bh->b_blocknr,
le32_to_cpu(gd->bg_generation));
}
if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
do_error("Group descriptor #%llu has bit count %u but claims that %u are free\n",
(unsignedlonglong)bh->b_blocknr,
le16_to_cpu(gd->bg_bits),
le16_to_cpu(gd->bg_free_bits_count));
}
if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
do_error("Group descriptor #%llu has bit count %u but max bitmap bits of %u\n",
(unsignedlonglong)bh->b_blocknr,
le16_to_cpu(gd->bg_bits),
8 * le16_to_cpu(gd->bg_size));
}
if (di->i_blkno != gd->bg_parent_dinode) {
do_error("Group descriptor #%llu has bad parent pointer (%llu, expected %llu)\n",
(unsignedlonglong)bh->b_blocknr,
(unsignedlonglong)le64_to_cpu(gd->bg_parent_dinode),
(unsignedlonglong)le64_to_cpu(di->i_blkno));
}
max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc); if (le16_to_cpu(gd->bg_bits) > max_bits) {
do_error("Group descriptor #%llu has bit count of %u\n",
(unsignedlonglong)bh->b_blocknr,
le16_to_cpu(gd->bg_bits));
}
/* In resize, we may meet the case bg_chain == cl_next_free_rec. */ if ((le16_to_cpu(gd->bg_chain) >
le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
((le16_to_cpu(gd->bg_chain) ==
le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
do_error("Group descriptor #%llu has bad chain %u\n",
(unsignedlonglong)bh->b_blocknr,
le16_to_cpu(gd->bg_chain));
}
return 0;
}
#undef do_error
/* * This version only prints errors. It does not fail the filesystem, and * exists only for resize.
*/ int ocfs2_check_group_descriptor(struct super_block *sb, struct ocfs2_dinode *di, struct buffer_head *bh)
{ int rc; struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
BUG_ON(!buffer_uptodate(bh));
/* * If the ecc fails, we return the error but otherwise * leave the filesystem running. We know any error is * local to this block.
*/
rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check); if (rc) {
mlog(ML_ERROR, "Checksum failed for group descriptor %llu\n",
(unsignedlonglong)bh->b_blocknr);
} else
rc = ocfs2_validate_gd_self(sb, bh, 1); if (!rc)
rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
/* * If the ecc fails, we return the error but otherwise * leave the filesystem running. We know any error is * local to this block.
*/
rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check); if (rc) return rc;
/* * Errors after here are fatal.
*/
return ocfs2_validate_gd_self(sb, bh, 0);
}
int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
u64 gd_blkno, struct buffer_head **bh)
{ int rc; struct buffer_head *tmp = *bh;
rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
ocfs2_validate_group_descriptor); if (rc) goto out;
/* set the 1st bit in the bitmap to account for the descriptor block */
ocfs2_set_bit(0, (unsignedlong *)bg->bg_bitmap);
bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
ocfs2_journal_dirty(handle, bg_bh);
/* There is no need to zero out or otherwise initialize the * other blocks in a group - All valid FS metadata in a block * group stores the superblock fs_generation value at
* allocation time. */
bail: if (status)
mlog_errno(status); return status;
}
bg = (struct ocfs2_group_desc *)bg_bh->b_data;
el = &bg->bg_list; for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
rec = &el->l_recs[i];
ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode,
cluster_ac->ac_bh,
le64_to_cpu(rec->e_blkno),
le16_to_cpu(rec->e_leaf_clusters)); if (ret)
mlog_errno(ret); /* Try all the clusters to free */
}
if (!ocfs2_supports_discontig_bg(osb)) {
status = -ENOSPC; goto bail;
}
status = ocfs2_extend_trans(handle,
ocfs2_calc_bg_discontig_credits(osb->sb)); if (status) {
mlog_errno(status); goto bail;
}
/* * We're going to be grabbing from multiple cluster groups. * We don't have enough credits to relink them all, and the * cluster groups will be staying in cache for the duration of * this operation.
*/
ac->ac_disable_chain_relink = 1;
/* Claim the first region */
status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
&bit_off, &num_bits); if (status < 0) { if (status != -ENOSPC)
mlog_errno(status); goto bail;
}
min_bits = num_bits;
/* setup the group */
bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
trace_ocfs2_block_group_alloc_discontig(
(unsignedlonglong)bg_blkno, alloc_rec);
bg_bh = sb_getblk(osb->sb, bg_blkno); if (!bg_bh) {
status = -ENOMEM;
mlog_errno(status); goto bail;
}
ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
bg_blkno, num_bits, alloc_rec, cl); if (status < 0) {
mlog_errno(status); goto bail;
}
status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
bg_bh, ac, cl, min_bits); if (status)
mlog_errno(status);
bail: if (status)
ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh); return status ? ERR_PTR(status) : bg_bh;
}
/* * We expect the block group allocator to already be locked.
*/ staticint ocfs2_block_group_alloc(struct ocfs2_super *osb, struct inode *alloc_inode, struct buffer_head *bh,
u64 max_block,
u64 *last_alloc_group, int flags)
{ int status, credits; struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data; struct ocfs2_chain_list *cl; struct ocfs2_alloc_context *ac = NULL;
handle_t *handle = NULL;
u16 alloc_rec; struct buffer_head *bg_bh = NULL; struct ocfs2_group_desc *bg;
BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
cl = &fe->id2.i_chain;
status = ocfs2_reserve_clusters_with_limit(osb,
le16_to_cpu(cl->cl_cpg),
max_block, flags, &ac); if (status < 0) { if (status != -ENOSPC)
mlog_errno(status); goto bail;
}
if (bits_wanted > free_bits) { /* cluster bitmap never grows */ if (ocfs2_is_cluster_bitmap(alloc_inode)) {
trace_ocfs2_reserve_suballoc_bits_nospc(bits_wanted,
free_bits);
status = -ENOSPC; goto bail;
}
if (!(flags & ALLOC_NEW_GROUP)) {
trace_ocfs2_reserve_suballoc_bits_no_new_group(
slot, bits_wanted, free_bits);
status = -ENOSPC; goto bail;
}
status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
ac->ac_max_block,
last_alloc_group, flags); if (status < 0) { if (status != -ENOSPC)
mlog_errno(status); goto bail;
}
atomic_inc(&osb->alloc_stats.bg_extends);
/* You should never ask for this much metadata */
BUG_ON(bits_wanted >
(le32_to_cpu(fe->id1.bitmap1.i_total)
- le32_to_cpu(fe->id1.bitmap1.i_used)));
}
staticint ocfs2_steal_resource(struct ocfs2_super *osb, struct ocfs2_alloc_context *ac, int type)
{ int i, status = -ENOSPC; int slot = __ocfs2_get_steal_slot(osb, type);
/* Start to steal resource from the first slot after ours. */ if (slot == OCFS2_INVALID_SLOT)
slot = osb->slot_num + 1;
for (i = 0; i < osb->max_slots; i++, slot++) { if (slot == osb->max_slots)
slot = 0;
if (slot == osb->slot_num) continue;
status = ocfs2_reserve_suballoc_bits(osb, ac,
type,
(u32)slot, NULL,
NOT_ALLOC_NEW_GROUP); if (status >= 0) {
__ocfs2_set_steal_slot(osb, slot, type); break;
}
int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb, int blocks, struct ocfs2_alloc_context **ac)
{ int status; int slot = ocfs2_get_meta_steal_slot(osb);
*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); if (!(*ac)) {
status = -ENOMEM;
mlog_errno(status); goto bail;
}
/* * stat(2) can't handle i_ino > 32bits, so we tell the * lower levels not to allocate us a block group past that * limit. The 'inode64' mount option avoids this behavior.
*/ if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
(*ac)->ac_max_block = (u32)~0U;
/* * slot is set when we successfully steal inode from other nodes. * It is reset in 3 places: * 1. when we flush the truncate log * 2. when we complete local alloc recovery. * 3. when we successfully allocate from our own slot. * After it is set, we will go on stealing inodes until we find the * need to check our slots to see whether there is some space for us.
*/ if (slot != OCFS2_INVALID_SLOT &&
atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL) goto inode_steal;
atomic_set(&osb->s_num_inodes_stolen, 0);
alloc_group = osb->osb_inode_alloc_group;
status = ocfs2_reserve_suballoc_bits(osb, *ac,
INODE_ALLOC_SYSTEM_INODE,
(u32)osb->slot_num,
&alloc_group,
ALLOC_NEW_GROUP |
ALLOC_GROUPS_FROM_GLOBAL); if (status >= 0) {
status = 0;
/* * Some inodes must be freed by us, so try to allocate * from our own next time.
*/ if (slot != OCFS2_INVALID_SLOT)
ocfs2_init_inode_steal_slot(osb); goto bail;
} elseif (status < 0 && status != -ENOSPC) {
mlog_errno(status); goto bail;
}
ocfs2_free_ac_resource(*ac);
inode_steal:
status = ocfs2_steal_inode(osb, *ac);
atomic_inc(&osb->s_num_inodes_stolen); if (status < 0) { if (status != -ENOSPC)
mlog_errno(status); goto bail;
}
status = 0;
bail: if ((status < 0) && *ac) {
ocfs2_free_alloc_context(*ac);
*ac = NULL;
}
if (status)
mlog_errno(status); return status;
}
/* local alloc code has to do the same thing, so rather than do this
* twice.. */ int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb, struct ocfs2_alloc_context *ac)
{ int status;
status = ocfs2_reserve_suballoc_bits(osb, ac,
GLOBAL_BITMAP_SYSTEM_INODE,
OCFS2_INVALID_SLOT, NULL,
ALLOC_NEW_GROUP); if (status < 0 && status != -ENOSPC)
mlog_errno(status);
return status;
}
/* Callers don't need to care which bitmap (local alloc or main) to * use so we figure it out for them, but unfortunately this clutters
* things a bit. */ staticint ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
u32 bits_wanted, u64 max_block, int flags, struct ocfs2_alloc_context **ac)
{ int status, ret = 0; int retried = 0;
*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); if (!(*ac)) {
status = -ENOMEM;
mlog_errno(status); goto bail;
}
status = -ENOSPC; if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
ocfs2_alloc_should_use_local(osb, bits_wanted)) {
status = ocfs2_reserve_local_alloc_bits(osb,
bits_wanted,
*ac); if ((status < 0) && (status != -ENOSPC)) {
mlog_errno(status); goto bail;
}
}
if (status == -ENOSPC) {
retry:
status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac); /* Retry if there is sufficient space cached in truncate log */ if (status == -ENOSPC && !retried) {
retried = 1;
ocfs2_inode_unlock((*ac)->ac_inode, 1);
inode_unlock((*ac)->ac_inode);
ret = ocfs2_try_to_free_truncate_log(osb, bits_wanted); if (ret == 1) {
iput((*ac)->ac_inode);
(*ac)->ac_inode = NULL; goto retry;
}
if (ret < 0)
mlog_errno(ret);
inode_lock((*ac)->ac_inode);
ret = ocfs2_inode_lock((*ac)->ac_inode, NULL, 1); if (ret < 0) {
mlog_errno(ret);
inode_unlock((*ac)->ac_inode);
iput((*ac)->ac_inode);
(*ac)->ac_inode = NULL; goto bail;
}
} if (status < 0) { if (status != -ENOSPC)
mlog_errno(status); goto bail;
}
}
status = 0;
bail: if ((status < 0) && *ac) {
ocfs2_free_alloc_context(*ac);
*ac = NULL;
}
/* * More or less lifted from ext3. I'll leave their description below: * * "For ext3 allocations, we must not reuse any blocks which are * allocated in the bitmap buffer's "last committed data" copy. This * prevents deletes from freeing up the page for reuse until we have * committed the delete transaction. * * If we didn't do this, then deleting something and reallocating it as * data would allow the old block to be overwritten before the * transaction committed (because we force data to disk before commit). * This would lead to corruption if we crashed between overwriting the * data and committing the delete. * * @@@ We may want to make this allocation behaviour conditional on * data-writes at some point, and disable it for metadata allocations or * sync-data inodes." * * Note: OCFS2 already does this differently for metadata vs data * allocations, as those bitmaps are separate and undo access is never * called on a metadata group descriptor.
*/ staticint ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh, int nr)
{ struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; struct journal_head *jh; int ret;
if (ocfs2_test_bit(nr, (unsignedlong *)bg->bg_bitmap)) return 0;
jh = jbd2_journal_grab_journal_head(bg_bh); if (!jh) return 1;
spin_lock(&jh->b_state_lock);
bg = (struct ocfs2_group_desc *) jh->b_committed_data; if (bg)
ret = !ocfs2_test_bit(nr, (unsignedlong *)bg->bg_bitmap); else
ret = 1;
spin_unlock(&jh->b_state_lock);
jbd2_journal_put_journal_head(jh);
while ((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) <
total_bits) { if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) { /* We found a zero, but we can't use it as it
* hasn't been put to disk yet! */
found = 0;
start = offset + 1;
} elseif (offset == start) { /* we found a zero */
found++; /* move start to the next bit to test */
start++;
} else { /* got a zero after some ones */
found = 1;
start = offset + 1;
prev_best_size = best_size;
} if (found > best_size) {
best_size = found;
best_offset = start - found;
} /* we got everything we needed */ if (found == bits_wanted) { /* mlog(0, "Found it all!\n"); */ break;
}
}
/* best_size will be allocated, we save prev_best_size */
res->sr_max_contig_bits = prev_best_size; if (best_size) {
res->sr_bit_offset = best_offset;
res->sr_bits = best_size;
} else {
status = -ENOSPC; /* No error log here -- see the comment above
* ocfs2_test_bg_bit_allocatable */
}
/* All callers get the descriptor via
* ocfs2_read_group_descriptor(). Any corruption is a code bug. */
BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
if (ocfs2_is_cluster_bitmap(alloc_inode))
journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
status = ocfs2_journal_access_gd(handle,
INODE_CACHE(alloc_inode),
group_bh,
journal_type); if (status < 0) {
mlog_errno(status); goto bail;
}
le16_add_cpu(&bg->bg_free_bits_count, -num_bits); if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) { return ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit count %u but claims %u are freed. num_bits %d\n",
(unsignedlonglong)le64_to_cpu(bg->bg_blkno),
le16_to_cpu(bg->bg_bits),
le16_to_cpu(bg->bg_free_bits_count),
num_bits);
} while(num_bits--)
ocfs2_set_bit(bit_off++, bitmap);
/* * this is optimize path, caller set old contig value * in max_contig_bits to bypass finding action.
*/ if (fastpath) {
bg->bg_contig_free_bits = cpu_to_le16(max_contig_bits);
} elseif (ocfs2_is_cluster_bitmap(alloc_inode)) { /* * Usually, the block group bitmap allocates only 1 bit * at a time, while the cluster group allocates n bits * each time. Therefore, we only save the contig bits for * the cluster group.
*/
contig_bits = ocfs2_find_max_contig_free_bits(bitmap,
le16_to_cpu(bg->bg_bits), start); if (contig_bits > max_contig_bits)
max_contig_bits = contig_bits;
bg->bg_contig_free_bits = cpu_to_le16(max_contig_bits);
ocfs2_local_alloc_seen_free_bits(osb, max_contig_bits);
} else {
bg->bg_contig_free_bits = 0;
}
ocfs2_journal_dirty(handle, group_bh);
bail: return status;
}
/* find the one with the most empty bits */ staticinline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
{
u16 curr, best;
BUG_ON(!cl->cl_next_free_rec);
best = curr = 0; while (curr < le16_to_cpu(cl->cl_next_free_rec)) { if (le32_to_cpu(cl->cl_recs[curr].c_free) >
le32_to_cpu(cl->cl_recs[best].c_free))
best = curr;
curr++;
}
staticint ocfs2_relink_block_group(handle_t *handle, struct inode *alloc_inode, struct buffer_head *fe_bh, struct buffer_head *bg_bh, struct buffer_head *prev_bg_bh,
u16 chain)
{ int status; /* there is a really tiny chance the journal calls could fail,
* but we wouldn't want inconsistent blocks in *any* case. */
u64 bg_ptr, prev_bg_ptr; struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data; struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
/* The caller got these descriptors from
* ocfs2_read_group_descriptor(). Any corruption is a code bug. */
BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
/* return 0 on success, -ENOSPC to keep searching and any other < 0
* value on error. */ staticint ocfs2_cluster_group_search(struct inode *inode, struct buffer_head *group_bh,
u32 bits_wanted, u32 min_bits,
u64 max_block, struct ocfs2_suballoc_result *res)
{ int search = -ENOSPC; int ret;
u64 blkoff; struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data; struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); unsignedint max_bits, gd_cluster_off;
BUG_ON(!ocfs2_is_cluster_bitmap(inode));
if (le16_to_cpu(gd->bg_contig_free_bits) &&
le16_to_cpu(gd->bg_contig_free_bits) < bits_wanted) return -ENOSPC;
/* ->bg_contig_free_bits may un-initialized, so compare again */ if (le16_to_cpu(gd->bg_free_bits_count) >= bits_wanted) {
max_bits = le16_to_cpu(gd->bg_bits);
/* Tail groups in cluster bitmaps which aren't cpg * aligned are prone to partial extension by a failed * fs resize. If the file system resize never got to * update the dinode cluster count, then we don't want * to trust any clusters past it, regardless of what
* the group descriptor says. */
gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
le64_to_cpu(gd->bg_blkno)); if ((gd_cluster_off + max_bits) >
OCFS2_I(inode)->ip_clusters) {
max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
trace_ocfs2_cluster_group_search_wrong_max_bits(
(unsignedlonglong)le64_to_cpu(gd->bg_blkno),
le16_to_cpu(gd->bg_bits),
OCFS2_I(inode)->ip_clusters, max_bits);
}
ret = ocfs2_block_group_find_clear_bits(osb,
group_bh, bits_wanted,
max_bits, res); if (ret) return ret;
/* ocfs2_block_group_find_clear_bits() might * return success, but we still want to return * -ENOSPC unless it found the minimum number
* of bits. */ if (min_bits <= res->sr_bits)
search = 0; /* success */
}
status = -ENOSPC; /* for now, the chain search is a bit simplistic. We just use
* the 1st group with any empty bits. */ while (1) { if (ac->ac_which == OCFS2_AC_USE_MAIN_DISCONTIG) {
contig_bits = le16_to_cpu(bg->bg_contig_free_bits); if (!contig_bits)
contig_bits = ocfs2_find_max_contig_free_bits(bg->bg_bitmap,
le16_to_cpu(bg->bg_bits), 0); if (bits_wanted > contig_bits && contig_bits >= min_bits)
bits_wanted = contig_bits;
}
status = ac->ac_group_search(alloc_inode, group_bh,
bits_wanted, min_bits,
ac->ac_max_block, res); if (status != -ENOSPC) break; if (!bg->bg_next_group) break;
BUG_ON(res->sr_bits == 0); if (!status)
ocfs2_bg_discontig_fix_result(ac, bg, res);
/* * sr_bg_blkno might have been changed by * ocfs2_bg_discontig_fix_result
*/
res->sr_bg_stable_blkno = group_bh->b_blocknr;
/* * Keep track of previous block descriptor read. When * we find a target, if we have read more than X * number of descriptors, and the target is reasonably * empty, relink him to top of his chain. * * We've read 0 extra blocks and only send one more to * the transaction, yet the next guy to search has a * much easier time. * * Do this *after* figuring out how many bits we're taking out * of our target group.
*/ if (!ac->ac_disable_chain_relink &&
(prev_group_bh) &&
(ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
status = ocfs2_relink_block_group(handle, alloc_inode,
ac->ac_bh, group_bh,
prev_group_bh, chain); if (status < 0) {
mlog_errno(status); goto bail;
}
}
if (ac->ac_find_loc_only) goto out_loc_only;
status = ocfs2_alloc_dinode_update_counts(alloc_inode, handle,
ac->ac_bh, res->sr_bits,
chain); if (status) {
mlog_errno(status); goto bail;
}
/* The bh was validated by the inode read during
* ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
le32_to_cpu(fe->id1.bitmap1.i_total)) {
status = ocfs2_error(ac->ac_inode->i_sb, "Chain allocator dinode %llu has %u used bits but only %u total\n",
(unsignedlonglong)le64_to_cpu(fe->i_blkno),
le32_to_cpu(fe->id1.bitmap1.i_used),
le32_to_cpu(fe->id1.bitmap1.i_total)); goto bail;
}
res->sr_bg_blkno = hint; if (res->sr_bg_blkno) { /* Attempt to short-circuit the usual search mechanism * by jumping straight to the most recently used * allocation group. This helps us maintain some
* contiguousness across allocations. */
status = ocfs2_search_one_group(ac, handle, bits_wanted,
min_bits, res, &bits_left); if (!status) goto set_hint; if (status < 0 && status != -ENOSPC) {
mlog_errno(status); goto bail;
}
}
search:
status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
res, &bits_left); if (!status) { if (ocfs2_is_cluster_bitmap(ac->ac_inode))
hint = res->sr_bg_blkno; else
hint = ocfs2_group_from_res(res); goto set_hint;
} if (status < 0 && status != -ENOSPC) {
mlog_errno(status); goto bail;
}
trace_ocfs2_claim_suballoc_bits(victim);
/* If we didn't pick a good victim, then just default to * searching each chain in order. Don't allow chain relinking * because we only calculate enough journal credits for one
* relink per alloc. */
ac->ac_disable_chain_relink = 1; for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) { if (i == victim) continue; if (le32_to_cpu(cl->cl_recs[i].c_free) < bits_wanted) continue;
ac->ac_chain = i;
status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
res, &bits_left); if (!status) {
hint = ocfs2_group_from_res(res); break;
} if (status < 0 && status != -ENOSPC) {
mlog_errno(status); goto bail;
}
}
/* Chains can't supply the bits_wanted contiguous space. * We should switch to using every single bit when allocating
* from the global bitmap. */ if (i == le16_to_cpu(cl->cl_next_free_rec) &&
status == -ENOSPC && ac->ac_which == OCFS2_AC_USE_MAIN) {
ac->ac_which = OCFS2_AC_USE_MAIN_DISCONTIG;
ac->ac_chain = victim; goto search;
}
set_hint: if (status != -ENOSPC) { /* If the next search of this group is not likely to * yield a suitable extent, then we reset the last
* group hint so as to not waste a disk read */ if (bits_left < min_bits)
ac->ac_last_group = 0; else
ac->ac_last_group = hint;
}
bail: if (status)
mlog_errno(status); return status;
}
int ocfs2_claim_metadata(handle_t *handle, struct ocfs2_alloc_context *ac,
u32 bits_wanted,
u64 *suballoc_loc,
u16 *suballoc_bit_start, unsignedint *num_bits,
u64 *blkno_start)
{ int status; struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
staticvoid ocfs2_init_inode_ac_group(struct inode *dir, struct buffer_head *parent_di_bh, struct ocfs2_alloc_context *ac)
{ struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data; /* * Try to allocate inodes from some specific group. * * If the parent dir has recorded the last group used in allocation, * cool, use it. Otherwise if we try to allocate new inode from the * same slot the parent dir belongs to, use the same chunk. * * We are very careful here to avoid the mistake of setting * ac_last_group to a group descriptor from a different (unlocked) slot.
*/ if (OCFS2_I(dir)->ip_last_used_group &&
OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group; elseif (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) { if (di->i_suballoc_loc)
ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc); else
ac->ac_last_group = ocfs2_which_suballoc_group(
le64_to_cpu(di->i_blkno),
le16_to_cpu(di->i_suballoc_bit));
}
}
res = kzalloc(sizeof(*res), GFP_NOFS); if (res == NULL) {
ret = -ENOMEM;
mlog_errno(ret); goto out;
}
ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
/* * The handle started here is for chain relink. Alternatively, * we could just disable relink for these calls.
*/
handle = ocfs2_start_trans(OCFS2_SB(dir->i_sb), OCFS2_SUBALLOC_ALLOC); if (IS_ERR(handle)) {
ret = PTR_ERR(handle);
handle = NULL;
mlog_errno(ret); goto out;
}
/* * This will instruct ocfs2_claim_suballoc_bits and * ocfs2_search_one_group to search but save actual allocation * for later.
*/
ac->ac_find_loc_only = 1;
ret = ocfs2_claim_suballoc_bits(ac, handle, 1, 1, res); if (ret < 0) {
mlog_errno(ret); goto out;
}
/* * Since di_blkno is being passed back in, we check for any * inconsistencies which may have happened between * calls. These are code bugs as di_blkno is not expected to * change once returned from ocfs2_find_new_inode_loc()
*/
BUG_ON(res->sr_blkno != di_blkno);
ret = ocfs2_read_group_descriptor(ac->ac_inode, di,
res->sr_bg_stable_blkno, &bg_bh); if (ret) {
mlog_errno(ret); goto out;
}
/* given a cluster offset, calculate which block group it belongs to
* and return that block offset. */
u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
{ struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
u32 group_no;
/* given the block number of a cluster start, calculate which cluster
* group and descriptor bitmap offset that corresponds to. */ staticinlinevoid ocfs2_block_to_cluster_group(struct inode *inode,
u64 data_blkno,
u64 *bg_blkno,
u16 *bg_bit_off)
{ struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
/* * min_bits - minimum contiguous chunk from this total allocation we * can handle. set to what we asked for originally for a full * contig. allocation, set to '1' to indicate we can deal with extents * of any size.
*/ int __ocfs2_claim_clusters(handle_t *handle, struct ocfs2_alloc_context *ac,
u32 min_clusters,
u32 max_clusters,
u32 *cluster_start,
u32 *num_clusters)
{ int status; unsignedint bits_wanted = max_clusters; struct ocfs2_suballoc_result res = { .sr_blkno = 0, }; struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
WARN_ON(min_clusters > 1);
status = ocfs2_claim_local_alloc_bits(osb,
handle,
ac,
bits_wanted,
cluster_start,
num_clusters); if (!status)
atomic_inc(&osb->alloc_stats.local_data);
} else { if (min_clusters > (osb->bitmap_cpg - 1)) { /* The only paths asking for contiguousness
* should know about this already. */
mlog(ML_ERROR, "minimum allocation requested %u exceeds " "group bitmap size %u!\n", min_clusters,
osb->bitmap_cpg);
status = -ENOSPC; goto bail;
} /* clamp the current request down to a realistic size. */ if (bits_wanted > (osb->bitmap_cpg - 1))
bits_wanted = osb->bitmap_cpg - 1;
status = ocfs2_claim_suballoc_bits(ac,
handle,
bits_wanted,
min_clusters,
&res); if (!status) {
BUG_ON(res.sr_blkno); /* cluster alloc can't set */
*cluster_start =
ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
res.sr_bg_blkno,
res.sr_bit_offset);
atomic_inc(&osb->alloc_stats.bitmap_data);
*num_clusters = res.sr_bits;
}
} if (status < 0) { if (status != -ENOSPC)
mlog_errno(status); goto bail;
}
ac->ac_bits_given += *num_clusters;
bail: if (status)
mlog_errno(status); return status;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.