// SPDX-License-Identifier: GPL-2.0-or-later/* * suballoc.c * * metadata alloc and free * Inspired by ext3 block groups. * * Copyright (C) 2002, 2004 Oracle. All rights reserved. */ #include <linux/fs.h>#include <linux/types.h>#include <linux/slab.h>#include <linux/string.h>#include <linux/highmem.h> #include <cluster/masklog.h> #include "ocfs2.h" #include "alloc.h"#include "blockcheck.h"#include "dlmglue.h"#include "inode.h"#include "journal.h"#include "localalloc.h"#include "suballoc.h"#include "super.h"#include "sysfile.h"#include "uptodate.h"#include "ocfs2_trace.h" #include "buffer_head_io.h" #define NOT_ALLOC_NEW_GROUP 0#define ALLOC_NEW_GROUP 0x1#define ALLOC_GROUPS_FROM_GLOBAL 0x2 #define OCFS2_MAX_TO_STEAL 1024 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 */ unsigned int sr_bit_offset; /* The bit in the bg */ unsigned int sr_bits; /* How many bits we claimed */ unsigned int sr_max_contig_bits; /* The length for contiguous * free bits, only available * for cluster group */}; static u64 ocfs2_group_from_res(struct ocfs2_suballoc_result *res){ if (res->sr_blkno == 0) return 0; if (res->sr_bg_blkno) return res->sr_bg_blkno; return ocfs2_which_suballoc_group(res->sr_blkno, res->sr_bit_offset);} static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);static int ocfs2_block_group_fill(handle_t *handle, struct inode *alloc_inode, struct buffer_head *bg_bh, u64 group_blkno, unsigned int group_clusters, u16 my_chain, struct ocfs2_chain_list *cl);static int 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); static int 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);static int ocfs2_block_group_search(struct inode *inode, struct buffer_head *group_bh, u32 bits_wanted, u32 min_bits, u64 max_block, struct ocfs2_suballoc_result *res);static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac, handle_t *handle, u32 bits_wanted, u32 min_bits, struct ocfs2_suballoc_result *res);static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh, int nr);static int 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);static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg, u32 wanted);static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode, u64 bg_blkno, u16 bg_bit_off);static inline void ocfs2_block_to_cluster_group(struct inode *inode, u64 data_blkno, u64 *bg_blkno, u16 *bg_bit_off);static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb, u32 bits_wanted, u64 max_block, int flags, struct ocfs2_alloc_context **ac); void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac){ struct inode *inode = ac->ac_inode; if (inode) { if (ac->ac_which != OCFS2_AC_USE_LOCAL) ocfs2_inode_unlock(inode, 1); inode_unlock(inode); iput(inode); ac->ac_inode = NULL; } brelse(ac->ac_bh); ac->ac_bh = NULL; ac->ac_resv = NULL; kfree(ac->ac_find_loc_priv); ac->ac_find_loc_priv = NULL;} void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac){ ocfs2_free_ac_resource(ac); kfree(ac);} static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl){ return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);} #define do_error(fmt, ...) \do { \ if (resize) \ mlog(ML_ERROR, fmt, ##__VA_ARGS__); \ else \ return ocfs2_error(sb, fmt, ##__VA_ARGS__); \} while (0) static int ocfs2_validate_gd_self(struct super_block *sb, struct buffer_head *bh, int resize){ struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; if (!OCFS2_IS_VALID_GROUP_DESC(gd)) { do_error("Group descriptor #%llu has bad signature %.*s\n", (unsigned long long)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", (unsigned long long)bh->b_blocknr, (unsigned long long)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", (unsigned long long)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", (unsigned long long)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", (unsigned long long)bh->b_blocknr, le16_to_cpu(gd->bg_bits), 8 * le16_to_cpu(gd->bg_size)); } /* * For discontiguous block groups, validate the on-disk extent list * against the maximum number of extent records that can physically * fit in a single block. */ if (ocfs2_gd_is_discontig(gd)) { u16 max_recs = ocfs2_extent_recs_per_gd(sb); u16 l_count = le16_to_cpu(gd->bg_list.l_count); u16 l_next_free_rec = le16_to_cpu(gd->bg_list.l_next_free_rec); if (l_count != max_recs) { do_error("Group descriptor #%llu bad discontig l_count %u expected %u\n", (unsigned long long)bh->b_blocknr, l_count, max_recs); } if (l_next_free_rec > l_count) { do_error("Group descriptor #%llu bad discontig l_next_free_rec %u max %u\n", (unsigned long long)bh->b_blocknr, l_next_free_rec, l_count); } } return 0;} static int ocfs2_validate_gd_parent(struct super_block *sb, struct ocfs2_dinode *di, struct buffer_head *bh, int resize){ unsigned int max_bits; unsigned int max_bitmap_bits; unsigned int max_bitmap_size; int suballocator; struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; suballocator = le64_to_cpu(di->i_blkno) != OCFS2_SB(sb)->bitmap_blkno; max_bitmap_size = ocfs2_group_bitmap_size(sb, suballocator, OCFS2_SB(sb)->s_feature_incompat); max_bitmap_bits = max_bitmap_size * 8; if (di->i_blkno != gd->bg_parent_dinode) { do_error("Group descriptor #%llu has bad parent pointer (%llu, expected %llu)\n", (unsigned long long)bh->b_blocknr, (unsigned long long)le64_to_cpu(gd->bg_parent_dinode), (unsigned long long)le64_to_cpu(di->i_blkno)); } if (le16_to_cpu(gd->bg_size) > max_bitmap_size) { do_error("Group descriptor #%llu has bitmap size %u but physical max of %u\n", (unsigned long long)bh->b_blocknr, le16_to_cpu(gd->bg_size), max_bitmap_size); } if (le16_to_cpu(gd->bg_bits) > max_bitmap_bits) { do_error("Group descriptor #%llu has bit count %u but physical max of %u\n", (unsigned long long)bh->b_blocknr, le16_to_cpu(gd->bg_bits), max_bitmap_bits); } 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", (unsigned long long)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", (unsigned long long)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", (unsigned long long)bh->b_blocknr); } else rc = ocfs2_validate_gd_self(sb, bh, 1); if (!rc) rc = ocfs2_validate_gd_parent(sb, di, bh, 1); return rc;} static int ocfs2_validate_group_descriptor(struct super_block *sb, struct buffer_head *bh){ int rc; struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; trace_ocfs2_validate_group_descriptor( (unsigned long long)bh->b_blocknr); 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) return rc; /* * Errors after here are fatal. */ return ocfs2_validate_gd_self(sb, bh, 0);} /* * The hint group descriptor (gd) may already have been released * in _ocfs2_free_suballoc_bits(). We first check the gd signature, * then perform the standard ocfs2_read_group_descriptor() jobs. * * If the gd signature is invalid, we return 'rc=0' and set * '*released=1'. The caller is expected to handle this specific case. * Otherwise, we return the actual error code. * * We treat gd signature corruption case as a release case. The * caller ocfs2_claim_suballoc_bits() will use ocfs2_search_chain() * to search each gd block. The code will eventually find this * corrupted gd block - Late, but not missed. * * Note: * The caller is responsible for initializing the '*released' status. */static int ocfs2_read_hint_group_descriptor(struct inode *inode, struct ocfs2_dinode *di, u64 gd_blkno, struct buffer_head **bh, int *released){ int rc; struct buffer_head *tmp = *bh; struct ocfs2_group_desc *gd; rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp, NULL); if (rc) goto out; gd = (struct ocfs2_group_desc *) tmp->b_data; if (!OCFS2_IS_VALID_GROUP_DESC(gd)) { /* * Invalid gd cache was set in ocfs2_read_block(), * which will affect block_group allocation. * Path: * ocfs2_reserve_suballoc_bits * ocfs2_block_group_alloc * ocfs2_block_group_alloc_contig * ocfs2_set_new_buffer_uptodate */ ocfs2_remove_from_cache(INODE_CACHE(inode), tmp); *released = 1; /* we return 'rc=0' for this case */ goto free_bh; } /* below jobs same with ocfs2_read_group_descriptor() */ if (!buffer_jbd(tmp)) { rc = ocfs2_validate_group_descriptor(inode->i_sb, tmp); if (rc) goto free_bh; } rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0); if (rc) goto free_bh; /* If ocfs2_read_block() got us a new bh, pass it up. */ if (!*bh) *bh = tmp; return rc; free_bh: brelse(tmp);out: return rc;} 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; rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0); if (rc) { brelse(tmp); goto out; } /* If ocfs2_read_block() got us a new bh, pass it up. */ if (!*bh) *bh = tmp; out: return rc;} static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb, struct ocfs2_group_desc *bg, struct ocfs2_chain_list *cl, u64 p_blkno, unsigned int clusters){ struct ocfs2_extent_list *el = &bg->bg_list; struct ocfs2_extent_rec *rec; BUG_ON(!ocfs2_supports_discontig_bg(osb)); if (!el->l_next_free_rec) el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb)); rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)]; rec->e_blkno = cpu_to_le64(p_blkno); rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc)); rec->e_leaf_clusters = cpu_to_le16(clusters); le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc)); le16_add_cpu(&bg->bg_free_bits_count, clusters * le16_to_cpu(cl->cl_bpc)); le16_add_cpu(&el->l_next_free_rec, 1);} static int ocfs2_block_group_fill(handle_t *handle, struct inode *alloc_inode, struct buffer_head *bg_bh, u64 group_blkno, unsigned int group_clusters, u16 my_chain, struct ocfs2_chain_list *cl){ int status = 0; struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; struct super_block * sb = alloc_inode->i_sb; if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) { status = ocfs2_error(alloc_inode->i_sb, "group block (%llu) != b_blocknr (%llu)\n", (unsigned long long)group_blkno, (unsigned long long) bg_bh->b_blocknr); goto bail; } status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), bg_bh, OCFS2_JOURNAL_ACCESS_CREATE); if (status < 0) { mlog_errno(status); goto bail; } memset(bg, 0, sb->s_blocksize); strscpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE); bg->bg_generation = cpu_to_le32(osb->fs_generation); bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1, osb->s_feature_incompat)); bg->bg_chain = cpu_to_le16(my_chain); bg->bg_next_group = cl->cl_recs[my_chain].c_blkno; bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno); bg->bg_blkno = cpu_to_le64(group_blkno); if (group_clusters == le16_to_cpu(cl->cl_cpg)) bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));Lines 1–500 of 3325. Load more
// SPDX-License-Identifier: GPL-2.0-or-later /* * suballoc.c * * metadata alloc and free * Inspired by ext3 block groups. * * Copyright (C) 2002, 2004 Oracle. All rights reserved. */ #include <linux/fs.h> #include <linux/types.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/highmem.h> #include <cluster/masklog.h> #include "ocfs2.h" #include "alloc.h" #include "blockcheck.h" #include "dlmglue.h" #include "inode.h" #include "journal.h" #include "localalloc.h" #include "suballoc.h" #include "super.h" #include "sysfile.h" #include "uptodate.h" #include "ocfs2_trace.h" #include "buffer_head_io.h" #define NOT_ALLOC_NEW_GROUP 0 #define ALLOC_NEW_GROUP 0x1 #define ALLOC_GROUPS_FROM_GLOBAL 0x2 #define OCFS2_MAX_TO_STEAL 1024 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 */ unsigned int sr_bit_offset; /* The bit in the bg */ unsigned int sr_bits; /* How many bits we claimed */ unsigned int sr_max_contig_bits; /* The length for contiguous * free bits, only available * for cluster group */ }; static u64 ocfs2_group_from_res(struct ocfs2_suballoc_result *res) { if (res->sr_blkno == 0) return 0; if (res->sr_bg_blkno) return res->sr_bg_blkno; return ocfs2_which_suballoc_group(res->sr_blkno, res->sr_bit_offset); } static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl); static int ocfs2_block_group_fill(handle_t *handle, struct inode *alloc_inode, struct buffer_head *bg_bh, u64 group_blkno, unsigned int group_clusters, u16 my_chain, struct ocfs2_chain_list *cl); static int 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); static int 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); static int ocfs2_block_group_search(struct inode *inode, struct buffer_head *group_bh, u32 bits_wanted, u32 min_bits, u64 max_block, struct ocfs2_suballoc_result *res); static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac, handle_t *handle, u32 bits_wanted, u32 min_bits, struct ocfs2_suballoc_result *res); static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh, int nr); static int 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); static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg, u32 wanted); static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode, u64 bg_blkno, u16 bg_bit_off); static inline void ocfs2_block_to_cluster_group(struct inode *inode, u64 data_blkno, u64 *bg_blkno, u16 *bg_bit_off); static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb, u32 bits_wanted, u64 max_block, int flags, struct ocfs2_alloc_context **ac); void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac) { struct inode *inode = ac->ac_inode; if (inode) { if (ac->ac_which != OCFS2_AC_USE_LOCAL) ocfs2_inode_unlock(inode, 1); inode_unlock(inode); iput(inode); ac->ac_inode = NULL; } brelse(ac->ac_bh); ac->ac_bh = NULL; ac->ac_resv = NULL; kfree(ac->ac_find_loc_priv); ac->ac_find_loc_priv = NULL; } void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac) { ocfs2_free_ac_resource(ac); kfree(ac); } static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl) { return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc); } #define do_error(fmt, ...) \ do { \ if (resize) \ mlog(ML_ERROR, fmt, ##__VA_ARGS__); \ else \ return ocfs2_error(sb, fmt, ##__VA_ARGS__); \ } while (0) static int ocfs2_validate_gd_self(struct super_block *sb, struct buffer_head *bh, int resize) { struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; if (!OCFS2_IS_VALID_GROUP_DESC(gd)) { do_error("Group descriptor #%llu has bad signature %.*s\n", (unsigned long long)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", (unsigned long long)bh->b_blocknr, (unsigned long long)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", (unsigned long long)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", (unsigned long long)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", (unsigned long long)bh->b_blocknr, le16_to_cpu(gd->bg_bits), 8 * le16_to_cpu(gd->bg_size)); } /* * For discontiguous block groups, validate the on-disk extent list * against the maximum number of extent records that can physically * fit in a single block. */ if (ocfs2_gd_is_discontig(gd)) { u16 max_recs = ocfs2_extent_recs_per_gd(sb); u16 l_count = le16_to_cpu(gd->bg_list.l_count); u16 l_next_free_rec = le16_to_cpu(gd->bg_list.l_next_free_rec); if (l_count != max_recs) { do_error("Group descriptor #%llu bad discontig l_count %u expected %u\n", (unsigned long long)bh->b_blocknr, l_count, max_recs); } if (l_next_free_rec > l_count) { do_error("Group descriptor #%llu bad discontig l_next_free_rec %u max %u\n", (unsigned long long)bh->b_blocknr, l_next_free_rec, l_count); } } return 0; } static int ocfs2_validate_gd_parent(struct super_block *sb, struct ocfs2_dinode *di, struct buffer_head *bh, int resize) { unsigned int max_bits; unsigned int max_bitmap_bits; unsigned int max_bitmap_size; int suballocator; struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; suballocator = le64_to_cpu(di->i_blkno) != OCFS2_SB(sb)->bitmap_blkno; max_bitmap_size = ocfs2_group_bitmap_size(sb, suballocator, OCFS2_SB(sb)->s_feature_incompat); max_bitmap_bits = max_bitmap_size * 8; if (di->i_blkno != gd->bg_parent_dinode) { do_error("Group descriptor #%llu has bad parent pointer (%llu, expected %llu)\n", (unsigned long long)bh->b_blocknr, (unsigned long long)le64_to_cpu(gd->bg_parent_dinode), (unsigned long long)le64_to_cpu(di->i_blkno)); } if (le16_to_cpu(gd->bg_size) > max_bitmap_size) { do_error("Group descriptor #%llu has bitmap size %u but physical max of %u\n", (unsigned long long)bh->b_blocknr, le16_to_cpu(gd->bg_size), max_bitmap_size); } if (le16_to_cpu(gd->bg_bits) > max_bitmap_bits) { do_error("Group descriptor #%llu has bit count %u but physical max of %u\n", (unsigned long long)bh->b_blocknr, le16_to_cpu(gd->bg_bits), max_bitmap_bits); } 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", (unsigned long long)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", (unsigned long long)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", (unsigned long long)bh->b_blocknr); } else rc = ocfs2_validate_gd_self(sb, bh, 1); if (!rc) rc = ocfs2_validate_gd_parent(sb, di, bh, 1); return rc; } static int ocfs2_validate_group_descriptor(struct super_block *sb, struct buffer_head *bh) { int rc; struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; trace_ocfs2_validate_group_descriptor( (unsigned long long)bh->b_blocknr); 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) return rc; /* * Errors after here are fatal. */ return ocfs2_validate_gd_self(sb, bh, 0); } /* * The hint group descriptor (gd) may already have been released * in _ocfs2_free_suballoc_bits(). We first check the gd signature, * then perform the standard ocfs2_read_group_descriptor() jobs. * * If the gd signature is invalid, we return 'rc=0' and set * '*released=1'. The caller is expected to handle this specific case. * Otherwise, we return the actual error code. * * We treat gd signature corruption case as a release case. The * caller ocfs2_claim_suballoc_bits() will use ocfs2_search_chain() * to search each gd block. The code will eventually find this * corrupted gd block - Late, but not missed. * * Note: * The caller is responsible for initializing the '*released' status. */ static int ocfs2_read_hint_group_descriptor(struct inode *inode, struct ocfs2_dinode *di, u64 gd_blkno, struct buffer_head **bh, int *released) { int rc; struct buffer_head *tmp = *bh; struct ocfs2_group_desc *gd; rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp, NULL); if (rc) goto out; gd = (struct ocfs2_group_desc *) tmp->b_data; if (!OCFS2_IS_VALID_GROUP_DESC(gd)) { /* * Invalid gd cache was set in ocfs2_read_block(), * which will affect block_group allocation. * Path: * ocfs2_reserve_suballoc_bits * ocfs2_block_group_alloc * ocfs2_block_group_alloc_contig * ocfs2_set_new_buffer_uptodate */ ocfs2_remove_from_cache(INODE_CACHE(inode), tmp); *released = 1; /* we return 'rc=0' for this case */ goto free_bh; } /* below jobs same with ocfs2_read_group_descriptor() */ if (!buffer_jbd(tmp)) { rc = ocfs2_validate_group_descriptor(inode->i_sb, tmp); if (rc) goto free_bh; } rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0); if (rc) goto free_bh; /* If ocfs2_read_block() got us a new bh, pass it up. */ if (!*bh) *bh = tmp; return rc; free_bh: brelse(tmp); out: return rc; } 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; rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0); if (rc) { brelse(tmp); goto out; } /* If ocfs2_read_block() got us a new bh, pass it up. */ if (!*bh) *bh = tmp; out: return rc; } static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb, struct ocfs2_group_desc *bg, struct ocfs2_chain_list *cl, u64 p_blkno, unsigned int clusters) { struct ocfs2_extent_list *el = &bg->bg_list; struct ocfs2_extent_rec *rec; BUG_ON(!ocfs2_supports_discontig_bg(osb)); if (!el->l_next_free_rec) el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb)); rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)]; rec->e_blkno = cpu_to_le64(p_blkno); rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc)); rec->e_leaf_clusters = cpu_to_le16(clusters); le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc)); le16_add_cpu(&bg->bg_free_bits_count, clusters * le16_to_cpu(cl->cl_bpc)); le16_add_cpu(&el->l_next_free_rec, 1); } static int ocfs2_block_group_fill(handle_t *handle, struct inode *alloc_inode, struct buffer_head *bg_bh, u64 group_blkno, unsigned int group_clusters, u16 my_chain, struct ocfs2_chain_list *cl) { int status = 0; struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; struct super_block * sb = alloc_inode->i_sb; if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) { status = ocfs2_error(alloc_inode->i_sb, "group block (%llu) != b_blocknr (%llu)\n", (unsigned long long)group_blkno, (unsigned long long) bg_bh->b_blocknr); goto bail; } status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), bg_bh, OCFS2_JOURNAL_ACCESS_CREATE); if (status < 0) { mlog_errno(status); goto bail; } memset(bg, 0, sb->s_blocksize); strscpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE); bg->bg_generation = cpu_to_le32(osb->fs_generation); bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1, osb->s_feature_incompat)); bg->bg_chain = cpu_to_le16(my_chain); bg->bg_next_group = cl->cl_recs[my_chain].c_blkno; bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno); bg->bg_blkno = cpu_to_le64(group_blkno); if (group_clusters == le16_to_cpu(cl->cl_cpg)) bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));