1ccd979bdSMark Fasheh /* -*- mode: c; c-basic-offset: 8; -*- 2ccd979bdSMark Fasheh * vim: noexpandtab sw=8 ts=8 sts=0: 3ccd979bdSMark Fasheh * 4ccd979bdSMark Fasheh * suballoc.c 5ccd979bdSMark Fasheh * 6ccd979bdSMark Fasheh * metadata alloc and free 7ccd979bdSMark Fasheh * Inspired by ext3 block groups. 8ccd979bdSMark Fasheh * 9ccd979bdSMark Fasheh * Copyright (C) 2002, 2004 Oracle. All rights reserved. 10ccd979bdSMark Fasheh * 11ccd979bdSMark Fasheh * This program is free software; you can redistribute it and/or 12ccd979bdSMark Fasheh * modify it under the terms of the GNU General Public 13ccd979bdSMark Fasheh * License as published by the Free Software Foundation; either 14ccd979bdSMark Fasheh * version 2 of the License, or (at your option) any later version. 15ccd979bdSMark Fasheh * 16ccd979bdSMark Fasheh * This program is distributed in the hope that it will be useful, 17ccd979bdSMark Fasheh * but WITHOUT ANY WARRANTY; without even the implied warranty of 18ccd979bdSMark Fasheh * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19ccd979bdSMark Fasheh * General Public License for more details. 20ccd979bdSMark Fasheh * 21ccd979bdSMark Fasheh * You should have received a copy of the GNU General Public 22ccd979bdSMark Fasheh * License along with this program; if not, write to the 23ccd979bdSMark Fasheh * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 24ccd979bdSMark Fasheh * Boston, MA 021110-1307, USA. 25ccd979bdSMark Fasheh */ 26ccd979bdSMark Fasheh 27ccd979bdSMark Fasheh #include <linux/fs.h> 28ccd979bdSMark Fasheh #include <linux/types.h> 29ccd979bdSMark Fasheh #include <linux/slab.h> 30ccd979bdSMark Fasheh #include <linux/highmem.h> 31ccd979bdSMark Fasheh 32ccd979bdSMark Fasheh #include <cluster/masklog.h> 33ccd979bdSMark Fasheh 34ccd979bdSMark Fasheh #include "ocfs2.h" 35ccd979bdSMark Fasheh 36ccd979bdSMark Fasheh #include "alloc.h" 37d6b32bbbSJoel Becker #include "blockcheck.h" 38ccd979bdSMark Fasheh #include "dlmglue.h" 39ccd979bdSMark Fasheh #include "inode.h" 40ccd979bdSMark Fasheh #include "journal.h" 41ccd979bdSMark Fasheh #include "localalloc.h" 42ccd979bdSMark Fasheh #include "suballoc.h" 43ccd979bdSMark Fasheh #include "super.h" 44ccd979bdSMark Fasheh #include "sysfile.h" 45ccd979bdSMark Fasheh #include "uptodate.h" 462f73e135STao Ma #include "ocfs2_trace.h" 47ccd979bdSMark Fasheh 48ccd979bdSMark Fasheh #include "buffer_head_io.h" 49ccd979bdSMark Fasheh 50ffda89a3STao Ma #define NOT_ALLOC_NEW_GROUP 0 5160ca81e8STao Ma #define ALLOC_NEW_GROUP 0x1 5260ca81e8STao Ma #define ALLOC_GROUPS_FROM_GLOBAL 0x2 53ffda89a3STao Ma 54b89c5428STiger Yang #define OCFS2_MAX_TO_STEAL 1024 554d0ddb2cSTao Ma 567d1fe093SJoel Becker struct ocfs2_suballoc_result { 572b6cb576SJoel Becker u64 sr_bg_blkno; /* The bg we allocated from. Set 582b6cb576SJoel Becker to 0 when a block group is 592b6cb576SJoel Becker contiguous. */ 60e49e2767SMark Fasheh u64 sr_bg_stable_blkno; /* 61e49e2767SMark Fasheh * Doesn't change, always 62e49e2767SMark Fasheh * set to target block 63e49e2767SMark Fasheh * group descriptor 64e49e2767SMark Fasheh * block. 65e49e2767SMark Fasheh */ 66ba206635SJoel Becker u64 sr_blkno; /* The first allocated block */ 677d1fe093SJoel Becker unsigned int sr_bit_offset; /* The bit in the bg */ 687d1fe093SJoel Becker unsigned int sr_bits; /* How many bits we claimed */ 697d1fe093SJoel Becker }; 707d1fe093SJoel Becker 71b2b6ebf5SMark Fasheh static u64 ocfs2_group_from_res(struct ocfs2_suballoc_result *res) 72b2b6ebf5SMark Fasheh { 73b2b6ebf5SMark Fasheh if (res->sr_blkno == 0) 74b2b6ebf5SMark Fasheh return 0; 75b2b6ebf5SMark Fasheh 76b2b6ebf5SMark Fasheh if (res->sr_bg_blkno) 77b2b6ebf5SMark Fasheh return res->sr_bg_blkno; 78b2b6ebf5SMark Fasheh 79b2b6ebf5SMark Fasheh return ocfs2_which_suballoc_group(res->sr_blkno, res->sr_bit_offset); 80b2b6ebf5SMark Fasheh } 81b2b6ebf5SMark Fasheh 82ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg); 83ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe); 84ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl); 851fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle, 86ccd979bdSMark Fasheh struct inode *alloc_inode, 87ccd979bdSMark Fasheh struct buffer_head *bg_bh, 88ccd979bdSMark Fasheh u64 group_blkno, 89798db35fSJoel Becker unsigned int group_clusters, 90ccd979bdSMark Fasheh u16 my_chain, 91ccd979bdSMark Fasheh struct ocfs2_chain_list *cl); 92ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb, 93ccd979bdSMark Fasheh struct inode *alloc_inode, 941187c968SJoel Becker struct buffer_head *bh, 9560ca81e8STao Ma u64 max_block, 96feb473a6STao Ma u64 *last_alloc_group, 9760ca81e8STao Ma int flags); 98ccd979bdSMark Fasheh 99ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode, 100ccd979bdSMark Fasheh struct buffer_head *group_bh, 101ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 1021187c968SJoel Becker u64 max_block, 1037d1fe093SJoel Becker struct ocfs2_suballoc_result *res); 104ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode, 105ccd979bdSMark Fasheh struct buffer_head *group_bh, 106ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 1071187c968SJoel Becker u64 max_block, 1087d1fe093SJoel Becker struct ocfs2_suballoc_result *res); 109aa8f8e93SJoel Becker static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac, 1101fabe148SMark Fasheh handle_t *handle, 111ccd979bdSMark Fasheh u32 bits_wanted, 112ccd979bdSMark Fasheh u32 min_bits, 1137d1fe093SJoel Becker struct ocfs2_suballoc_result *res); 114ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh, 115ccd979bdSMark Fasheh int nr); 1161fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle, 117ccd979bdSMark Fasheh struct inode *alloc_inode, 118ccd979bdSMark Fasheh struct buffer_head *fe_bh, 119ccd979bdSMark Fasheh struct buffer_head *bg_bh, 120ccd979bdSMark Fasheh struct buffer_head *prev_bg_bh, 121ccd979bdSMark Fasheh u16 chain); 122ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg, 123ccd979bdSMark Fasheh u32 wanted); 124ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode, 125ccd979bdSMark Fasheh u64 bg_blkno, 126ccd979bdSMark Fasheh u16 bg_bit_off); 127ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode, 128ccd979bdSMark Fasheh u64 data_blkno, 129ccd979bdSMark Fasheh u64 *bg_blkno, 130ccd979bdSMark Fasheh u16 *bg_bit_off); 1311187c968SJoel Becker static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb, 1321187c968SJoel Becker u32 bits_wanted, u64 max_block, 13360ca81e8STao Ma int flags, 1341187c968SJoel Becker struct ocfs2_alloc_context **ac); 135ccd979bdSMark Fasheh 1369c7af40bSMark Fasheh void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac) 137ccd979bdSMark Fasheh { 138da5cbf2fSMark Fasheh struct inode *inode = ac->ac_inode; 139da5cbf2fSMark Fasheh 140da5cbf2fSMark Fasheh if (inode) { 141da5cbf2fSMark Fasheh if (ac->ac_which != OCFS2_AC_USE_LOCAL) 142e63aecb6SMark Fasheh ocfs2_inode_unlock(inode, 1); 143da5cbf2fSMark Fasheh 144da5cbf2fSMark Fasheh mutex_unlock(&inode->i_mutex); 145da5cbf2fSMark Fasheh 146da5cbf2fSMark Fasheh iput(inode); 1474d0ddb2cSTao Ma ac->ac_inode = NULL; 148da5cbf2fSMark Fasheh } 149ccd979bdSMark Fasheh brelse(ac->ac_bh); 1504d0ddb2cSTao Ma ac->ac_bh = NULL; 151e3b4a97dSMark Fasheh ac->ac_resv = NULL; 152e49e2767SMark Fasheh kfree(ac->ac_find_loc_priv); 153e49e2767SMark Fasheh ac->ac_find_loc_priv = NULL; 154e49e2767SMark Fasheh } 1554d0ddb2cSTao Ma 1564d0ddb2cSTao Ma void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac) 1574d0ddb2cSTao Ma { 1584d0ddb2cSTao Ma ocfs2_free_ac_resource(ac); 159ccd979bdSMark Fasheh kfree(ac); 160ccd979bdSMark Fasheh } 161ccd979bdSMark Fasheh 162ccd979bdSMark Fasheh static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl) 163ccd979bdSMark Fasheh { 164ccd979bdSMark Fasheh return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc); 165ccd979bdSMark Fasheh } 166ccd979bdSMark Fasheh 16757e3e797SJoel Becker #define do_error(fmt, ...) \ 16857e3e797SJoel Becker do { \ 16978c37eb0STao Ma if (resize) \ 1707ecef14aSJoe Perches mlog(ML_ERROR, fmt, ##__VA_ARGS__); \ 17157e3e797SJoel Becker else \ 17217a5b9abSGoldwyn Rodrigues return ocfs2_error(sb, fmt, ##__VA_ARGS__); \ 17357e3e797SJoel Becker } while (0) 17457e3e797SJoel Becker 175970e4936SJoel Becker static int ocfs2_validate_gd_self(struct super_block *sb, 176970e4936SJoel Becker struct buffer_head *bh, 17778c37eb0STao Ma int resize) 178970e4936SJoel Becker { 179970e4936SJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 180970e4936SJoel Becker 1817bf72edeSMark Fasheh if (!OCFS2_IS_VALID_GROUP_DESC(gd)) { 1827ecef14aSJoe Perches do_error("Group descriptor #%llu has bad signature %.*s\n", 18368f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 7, 18457e3e797SJoel Becker gd->bg_signature); 1857bf72edeSMark Fasheh } 1867bf72edeSMark Fasheh 18768f64d47SJoel Becker if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) { 1887ecef14aSJoe Perches do_error("Group descriptor #%llu has an invalid bg_blkno of %llu\n", 18968f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 19068f64d47SJoel Becker (unsigned long long)le64_to_cpu(gd->bg_blkno)); 19168f64d47SJoel Becker } 19268f64d47SJoel Becker 19368f64d47SJoel Becker if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) { 1947ecef14aSJoe Perches do_error("Group descriptor #%llu has an invalid fs_generation of #%u\n", 19568f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 19668f64d47SJoel Becker le32_to_cpu(gd->bg_generation)); 19768f64d47SJoel Becker } 19868f64d47SJoel Becker 199970e4936SJoel Becker if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) { 2007ecef14aSJoe Perches do_error("Group descriptor #%llu has bit count %u but claims that %u are free\n", 201970e4936SJoel Becker (unsigned long long)bh->b_blocknr, 202970e4936SJoel Becker le16_to_cpu(gd->bg_bits), 203970e4936SJoel Becker le16_to_cpu(gd->bg_free_bits_count)); 204970e4936SJoel Becker } 205970e4936SJoel Becker 206970e4936SJoel Becker if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) { 2077ecef14aSJoe Perches do_error("Group descriptor #%llu has bit count %u but max bitmap bits of %u\n", 208970e4936SJoel Becker (unsigned long long)bh->b_blocknr, 209970e4936SJoel Becker le16_to_cpu(gd->bg_bits), 210970e4936SJoel Becker 8 * le16_to_cpu(gd->bg_size)); 211970e4936SJoel Becker } 212970e4936SJoel Becker 213970e4936SJoel Becker return 0; 214970e4936SJoel Becker } 215970e4936SJoel Becker 216970e4936SJoel Becker static int ocfs2_validate_gd_parent(struct super_block *sb, 217970e4936SJoel Becker struct ocfs2_dinode *di, 218970e4936SJoel Becker struct buffer_head *bh, 21978c37eb0STao Ma int resize) 220970e4936SJoel Becker { 221970e4936SJoel Becker unsigned int max_bits; 222970e4936SJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 223970e4936SJoel Becker 2247bf72edeSMark Fasheh if (di->i_blkno != gd->bg_parent_dinode) { 2257ecef14aSJoe Perches do_error("Group descriptor #%llu has bad parent pointer (%llu, expected %llu)\n", 22668f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 2277bf72edeSMark Fasheh (unsigned long long)le64_to_cpu(gd->bg_parent_dinode), 2287bf72edeSMark Fasheh (unsigned long long)le64_to_cpu(di->i_blkno)); 2297bf72edeSMark Fasheh } 2307bf72edeSMark Fasheh 2317bf72edeSMark Fasheh max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc); 2327bf72edeSMark Fasheh if (le16_to_cpu(gd->bg_bits) > max_bits) { 2337ecef14aSJoe Perches do_error("Group descriptor #%llu has bit count of %u\n", 23468f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 2357bf72edeSMark Fasheh le16_to_cpu(gd->bg_bits)); 2367bf72edeSMark Fasheh } 2377bf72edeSMark Fasheh 23878c37eb0STao Ma /* In resize, we may meet the case bg_chain == cl_next_free_rec. */ 23978c37eb0STao Ma if ((le16_to_cpu(gd->bg_chain) > 24078c37eb0STao Ma le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) || 24178c37eb0STao Ma ((le16_to_cpu(gd->bg_chain) == 24278c37eb0STao Ma le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) { 2437ecef14aSJoe Perches do_error("Group descriptor #%llu has bad chain %u\n", 24468f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 2457bf72edeSMark Fasheh le16_to_cpu(gd->bg_chain)); 2467bf72edeSMark Fasheh } 2477bf72edeSMark Fasheh 248970e4936SJoel Becker return 0; 2497bf72edeSMark Fasheh } 2507bf72edeSMark Fasheh 25157e3e797SJoel Becker #undef do_error 2527bf72edeSMark Fasheh 253970e4936SJoel Becker /* 254970e4936SJoel Becker * This version only prints errors. It does not fail the filesystem, and 255970e4936SJoel Becker * exists only for resize. 256970e4936SJoel Becker */ 257970e4936SJoel Becker int ocfs2_check_group_descriptor(struct super_block *sb, 258970e4936SJoel Becker struct ocfs2_dinode *di, 259970e4936SJoel Becker struct buffer_head *bh) 260970e4936SJoel Becker { 261970e4936SJoel Becker int rc; 262d6b32bbbSJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 263970e4936SJoel Becker 264d6b32bbbSJoel Becker BUG_ON(!buffer_uptodate(bh)); 265d6b32bbbSJoel Becker 266d6b32bbbSJoel Becker /* 267d6b32bbbSJoel Becker * If the ecc fails, we return the error but otherwise 268d6b32bbbSJoel Becker * leave the filesystem running. We know any error is 269d6b32bbbSJoel Becker * local to this block. 270d6b32bbbSJoel Becker */ 271d6b32bbbSJoel Becker rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check); 27213723d00SJoel Becker if (rc) { 27313723d00SJoel Becker mlog(ML_ERROR, 27413723d00SJoel Becker "Checksum failed for group descriptor %llu\n", 27513723d00SJoel Becker (unsigned long long)bh->b_blocknr); 27613723d00SJoel Becker } else 277970e4936SJoel Becker rc = ocfs2_validate_gd_self(sb, bh, 1); 278970e4936SJoel Becker if (!rc) 279970e4936SJoel Becker rc = ocfs2_validate_gd_parent(sb, di, bh, 1); 280970e4936SJoel Becker 281970e4936SJoel Becker return rc; 282970e4936SJoel Becker } 283970e4936SJoel Becker 284970e4936SJoel Becker static int ocfs2_validate_group_descriptor(struct super_block *sb, 285970e4936SJoel Becker struct buffer_head *bh) 286970e4936SJoel Becker { 287d6b32bbbSJoel Becker int rc; 288d6b32bbbSJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 289d6b32bbbSJoel Becker 2902f73e135STao Ma trace_ocfs2_validate_group_descriptor( 291970e4936SJoel Becker (unsigned long long)bh->b_blocknr); 292970e4936SJoel Becker 293d6b32bbbSJoel Becker BUG_ON(!buffer_uptodate(bh)); 294d6b32bbbSJoel Becker 295d6b32bbbSJoel Becker /* 296d6b32bbbSJoel Becker * If the ecc fails, we return the error but otherwise 297d6b32bbbSJoel Becker * leave the filesystem running. We know any error is 298d6b32bbbSJoel Becker * local to this block. 299d6b32bbbSJoel Becker */ 300d6b32bbbSJoel Becker rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check); 301d6b32bbbSJoel Becker if (rc) 302d6b32bbbSJoel Becker return rc; 303d6b32bbbSJoel Becker 304d6b32bbbSJoel Becker /* 305d6b32bbbSJoel Becker * Errors after here are fatal. 306d6b32bbbSJoel Becker */ 307d6b32bbbSJoel Becker 308970e4936SJoel Becker return ocfs2_validate_gd_self(sb, bh, 0); 3097bf72edeSMark Fasheh } 3107bf72edeSMark Fasheh 31168f64d47SJoel Becker int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di, 31268f64d47SJoel Becker u64 gd_blkno, struct buffer_head **bh) 31368f64d47SJoel Becker { 31468f64d47SJoel Becker int rc; 31568f64d47SJoel Becker struct buffer_head *tmp = *bh; 31668f64d47SJoel Becker 3178cb471e8SJoel Becker rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp, 318970e4936SJoel Becker ocfs2_validate_group_descriptor); 31968f64d47SJoel Becker if (rc) 32068f64d47SJoel Becker goto out; 32168f64d47SJoel Becker 322970e4936SJoel Becker rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0); 32368f64d47SJoel Becker if (rc) { 32468f64d47SJoel Becker brelse(tmp); 32568f64d47SJoel Becker goto out; 32668f64d47SJoel Becker } 32768f64d47SJoel Becker 32868f64d47SJoel Becker /* If ocfs2_read_block() got us a new bh, pass it up. */ 32968f64d47SJoel Becker if (!*bh) 33068f64d47SJoel Becker *bh = tmp; 33168f64d47SJoel Becker 33268f64d47SJoel Becker out: 33368f64d47SJoel Becker return rc; 33468f64d47SJoel Becker } 33568f64d47SJoel Becker 336798db35fSJoel Becker static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb, 337798db35fSJoel Becker struct ocfs2_group_desc *bg, 338798db35fSJoel Becker struct ocfs2_chain_list *cl, 33947dea423STao Ma u64 p_blkno, unsigned int clusters) 340798db35fSJoel Becker { 341798db35fSJoel Becker struct ocfs2_extent_list *el = &bg->bg_list; 342798db35fSJoel Becker struct ocfs2_extent_rec *rec; 343798db35fSJoel Becker 3444711954eSTao Ma BUG_ON(!ocfs2_supports_discontig_bg(osb)); 345798db35fSJoel Becker if (!el->l_next_free_rec) 346798db35fSJoel Becker el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb)); 347798db35fSJoel Becker rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)]; 3484711954eSTao Ma rec->e_blkno = cpu_to_le64(p_blkno); 349798db35fSJoel Becker rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) / 350798db35fSJoel Becker le16_to_cpu(cl->cl_bpc)); 35147dea423STao Ma rec->e_leaf_clusters = cpu_to_le16(clusters); 352798db35fSJoel Becker le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc)); 3534711954eSTao Ma le16_add_cpu(&bg->bg_free_bits_count, 3544711954eSTao Ma clusters * le16_to_cpu(cl->cl_bpc)); 355798db35fSJoel Becker le16_add_cpu(&el->l_next_free_rec, 1); 356798db35fSJoel Becker } 357798db35fSJoel Becker 3581fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle, 359ccd979bdSMark Fasheh struct inode *alloc_inode, 360ccd979bdSMark Fasheh struct buffer_head *bg_bh, 361ccd979bdSMark Fasheh u64 group_blkno, 362798db35fSJoel Becker unsigned int group_clusters, 363ccd979bdSMark Fasheh u16 my_chain, 364ccd979bdSMark Fasheh struct ocfs2_chain_list *cl) 365ccd979bdSMark Fasheh { 366ccd979bdSMark Fasheh int status = 0; 367798db35fSJoel Becker struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); 368ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 369ccd979bdSMark Fasheh struct super_block * sb = alloc_inode->i_sb; 370ccd979bdSMark Fasheh 371ccd979bdSMark Fasheh if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) { 3727ecef14aSJoe Perches status = ocfs2_error(alloc_inode->i_sb, 3737ecef14aSJoe Perches "group block (%llu) != b_blocknr (%llu)\n", 374b0697053SMark Fasheh (unsigned long long)group_blkno, 375ccd979bdSMark Fasheh (unsigned long long) bg_bh->b_blocknr); 376ccd979bdSMark Fasheh goto bail; 377ccd979bdSMark Fasheh } 378ccd979bdSMark Fasheh 37913723d00SJoel Becker status = ocfs2_journal_access_gd(handle, 3800cf2f763SJoel Becker INODE_CACHE(alloc_inode), 381ccd979bdSMark Fasheh bg_bh, 382ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_CREATE); 383ccd979bdSMark Fasheh if (status < 0) { 384ccd979bdSMark Fasheh mlog_errno(status); 385ccd979bdSMark Fasheh goto bail; 386ccd979bdSMark Fasheh } 387ccd979bdSMark Fasheh 388ccd979bdSMark Fasheh memset(bg, 0, sb->s_blocksize); 389ccd979bdSMark Fasheh strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE); 390ccd979bdSMark Fasheh bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation); 3918571882cSTao Ma bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1, 3928571882cSTao Ma osb->s_feature_incompat)); 393ccd979bdSMark Fasheh bg->bg_chain = cpu_to_le16(my_chain); 394ccd979bdSMark Fasheh bg->bg_next_group = cl->cl_recs[my_chain].c_blkno; 395ccd979bdSMark Fasheh bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno); 396ccd979bdSMark Fasheh bg->bg_blkno = cpu_to_le64(group_blkno); 397798db35fSJoel Becker if (group_clusters == le16_to_cpu(cl->cl_cpg)) 398798db35fSJoel Becker bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl)); 399798db35fSJoel Becker else 4004711954eSTao Ma ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno, 401798db35fSJoel Becker group_clusters); 402798db35fSJoel Becker 403ccd979bdSMark Fasheh /* set the 1st bit in the bitmap to account for the descriptor block */ 404ccd979bdSMark Fasheh ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap); 405ccd979bdSMark Fasheh bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1); 406ccd979bdSMark Fasheh 407ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bg_bh); 408ccd979bdSMark Fasheh 409ccd979bdSMark Fasheh /* There is no need to zero out or otherwise initialize the 410ccd979bdSMark Fasheh * other blocks in a group - All valid FS metadata in a block 411ccd979bdSMark Fasheh * group stores the superblock fs_generation value at 412ccd979bdSMark Fasheh * allocation time. */ 413ccd979bdSMark Fasheh 414ccd979bdSMark Fasheh bail: 415c1e8d35eSTao Ma if (status) 416c1e8d35eSTao Ma mlog_errno(status); 417ccd979bdSMark Fasheh return status; 418ccd979bdSMark Fasheh } 419ccd979bdSMark Fasheh 420ccd979bdSMark Fasheh static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl) 421ccd979bdSMark Fasheh { 422ccd979bdSMark Fasheh u16 curr, best; 423ccd979bdSMark Fasheh 424ccd979bdSMark Fasheh best = curr = 0; 425ccd979bdSMark Fasheh while (curr < le16_to_cpu(cl->cl_count)) { 426ccd979bdSMark Fasheh if (le32_to_cpu(cl->cl_recs[best].c_total) > 427ccd979bdSMark Fasheh le32_to_cpu(cl->cl_recs[curr].c_total)) 428ccd979bdSMark Fasheh best = curr; 429ccd979bdSMark Fasheh curr++; 430ccd979bdSMark Fasheh } 431ccd979bdSMark Fasheh return best; 432ccd979bdSMark Fasheh } 433ccd979bdSMark Fasheh 434798db35fSJoel Becker static struct buffer_head * 435798db35fSJoel Becker ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle, 436798db35fSJoel Becker struct inode *alloc_inode, 437798db35fSJoel Becker struct ocfs2_alloc_context *ac, 438798db35fSJoel Becker struct ocfs2_chain_list *cl) 439798db35fSJoel Becker { 440798db35fSJoel Becker int status; 441798db35fSJoel Becker u32 bit_off, num_bits; 442798db35fSJoel Becker u64 bg_blkno; 443798db35fSJoel Becker struct buffer_head *bg_bh; 444798db35fSJoel Becker unsigned int alloc_rec = ocfs2_find_smallest_chain(cl); 445798db35fSJoel Becker 4461ed9b777SJoel Becker status = ocfs2_claim_clusters(handle, ac, 447798db35fSJoel Becker le16_to_cpu(cl->cl_cpg), &bit_off, 448798db35fSJoel Becker &num_bits); 449798db35fSJoel Becker if (status < 0) { 450798db35fSJoel Becker if (status != -ENOSPC) 451798db35fSJoel Becker mlog_errno(status); 452798db35fSJoel Becker goto bail; 453798db35fSJoel Becker } 454798db35fSJoel Becker 455798db35fSJoel Becker /* setup the group */ 456798db35fSJoel Becker bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off); 4572f73e135STao Ma trace_ocfs2_block_group_alloc_contig( 4582f73e135STao Ma (unsigned long long)bg_blkno, alloc_rec); 459798db35fSJoel Becker 460798db35fSJoel Becker bg_bh = sb_getblk(osb->sb, bg_blkno); 461798db35fSJoel Becker if (!bg_bh) { 4627391a294SRui Xiang status = -ENOMEM; 463798db35fSJoel Becker mlog_errno(status); 464798db35fSJoel Becker goto bail; 465798db35fSJoel Becker } 466798db35fSJoel Becker ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh); 467798db35fSJoel Becker 468798db35fSJoel Becker status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh, 469798db35fSJoel Becker bg_blkno, num_bits, alloc_rec, cl); 470798db35fSJoel Becker if (status < 0) { 471798db35fSJoel Becker brelse(bg_bh); 472798db35fSJoel Becker mlog_errno(status); 473798db35fSJoel Becker } 474798db35fSJoel Becker 475798db35fSJoel Becker bail: 476798db35fSJoel Becker return status ? ERR_PTR(status) : bg_bh; 477798db35fSJoel Becker } 478798db35fSJoel Becker 479798db35fSJoel Becker static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb, 480798db35fSJoel Becker handle_t *handle, 481798db35fSJoel Becker struct ocfs2_alloc_context *ac, 482798db35fSJoel Becker unsigned int min_bits, 483798db35fSJoel Becker u32 *bit_off, u32 *num_bits) 484798db35fSJoel Becker { 48518d3a98fSJoel Becker int status = 0; 486798db35fSJoel Becker 487798db35fSJoel Becker while (min_bits) { 4881ed9b777SJoel Becker status = ocfs2_claim_clusters(handle, ac, min_bits, 489798db35fSJoel Becker bit_off, num_bits); 490798db35fSJoel Becker if (status != -ENOSPC) 491798db35fSJoel Becker break; 492798db35fSJoel Becker 493798db35fSJoel Becker min_bits >>= 1; 494798db35fSJoel Becker } 495798db35fSJoel Becker 496798db35fSJoel Becker return status; 497798db35fSJoel Becker } 498798db35fSJoel Becker 499798db35fSJoel Becker static int ocfs2_block_group_grow_discontig(handle_t *handle, 500798db35fSJoel Becker struct inode *alloc_inode, 501798db35fSJoel Becker struct buffer_head *bg_bh, 502798db35fSJoel Becker struct ocfs2_alloc_context *ac, 503798db35fSJoel Becker struct ocfs2_chain_list *cl, 504798db35fSJoel Becker unsigned int min_bits) 505798db35fSJoel Becker { 506798db35fSJoel Becker int status; 507798db35fSJoel Becker struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); 508798db35fSJoel Becker struct ocfs2_group_desc *bg = 509798db35fSJoel Becker (struct ocfs2_group_desc *)bg_bh->b_data; 5104711954eSTao Ma unsigned int needed = le16_to_cpu(cl->cl_cpg) - 5114711954eSTao Ma le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc); 512798db35fSJoel Becker u32 p_cpos, clusters; 513798db35fSJoel Becker u64 p_blkno; 514798db35fSJoel Becker struct ocfs2_extent_list *el = &bg->bg_list; 515798db35fSJoel Becker 516798db35fSJoel Becker status = ocfs2_journal_access_gd(handle, 517798db35fSJoel Becker INODE_CACHE(alloc_inode), 518798db35fSJoel Becker bg_bh, 519798db35fSJoel Becker OCFS2_JOURNAL_ACCESS_CREATE); 520798db35fSJoel Becker if (status < 0) { 521798db35fSJoel Becker mlog_errno(status); 522798db35fSJoel Becker goto bail; 523798db35fSJoel Becker } 524798db35fSJoel Becker 525798db35fSJoel Becker while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) < 526798db35fSJoel Becker le16_to_cpu(el->l_count))) { 527798db35fSJoel Becker if (min_bits > needed) 528798db35fSJoel Becker min_bits = needed; 529798db35fSJoel Becker status = ocfs2_block_group_claim_bits(osb, handle, ac, 530798db35fSJoel Becker min_bits, &p_cpos, 531798db35fSJoel Becker &clusters); 532798db35fSJoel Becker if (status < 0) { 533798db35fSJoel Becker if (status != -ENOSPC) 534798db35fSJoel Becker mlog_errno(status); 535798db35fSJoel Becker goto bail; 536798db35fSJoel Becker } 537798db35fSJoel Becker p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos); 538798db35fSJoel Becker ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno, 539798db35fSJoel Becker clusters); 540798db35fSJoel Becker 541798db35fSJoel Becker min_bits = clusters; 5424711954eSTao Ma needed = le16_to_cpu(cl->cl_cpg) - 5434711954eSTao Ma le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc); 544798db35fSJoel Becker } 545798db35fSJoel Becker 546798db35fSJoel Becker if (needed > 0) { 5474711954eSTao Ma /* 5484711954eSTao Ma * We have used up all the extent rec but can't fill up 5494711954eSTao Ma * the cpg. So bail out. 5504711954eSTao Ma */ 5514711954eSTao Ma status = -ENOSPC; 5524711954eSTao Ma goto bail; 553798db35fSJoel Becker } 554798db35fSJoel Becker 555798db35fSJoel Becker ocfs2_journal_dirty(handle, bg_bh); 556798db35fSJoel Becker 557798db35fSJoel Becker bail: 558798db35fSJoel Becker return status; 559798db35fSJoel Becker } 560798db35fSJoel Becker 5618b06bc59SJoel Becker static void ocfs2_bg_alloc_cleanup(handle_t *handle, 5628b06bc59SJoel Becker struct ocfs2_alloc_context *cluster_ac, 5638b06bc59SJoel Becker struct inode *alloc_inode, 5648b06bc59SJoel Becker struct buffer_head *bg_bh) 565798db35fSJoel Becker { 5668b06bc59SJoel Becker int i, ret; 567798db35fSJoel Becker struct ocfs2_group_desc *bg; 568798db35fSJoel Becker struct ocfs2_extent_list *el; 569798db35fSJoel Becker struct ocfs2_extent_rec *rec; 570798db35fSJoel Becker 571798db35fSJoel Becker if (!bg_bh) 572798db35fSJoel Becker return; 573798db35fSJoel Becker 574798db35fSJoel Becker bg = (struct ocfs2_group_desc *)bg_bh->b_data; 575798db35fSJoel Becker el = &bg->bg_list; 576798db35fSJoel Becker for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { 577798db35fSJoel Becker rec = &el->l_recs[i]; 5788b06bc59SJoel Becker ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode, 5798b06bc59SJoel Becker cluster_ac->ac_bh, 580798db35fSJoel Becker le64_to_cpu(rec->e_blkno), 58172094e43SAl Viro le16_to_cpu(rec->e_leaf_clusters)); 5828b06bc59SJoel Becker if (ret) 5838b06bc59SJoel Becker mlog_errno(ret); 5848b06bc59SJoel Becker /* Try all the clusters to free */ 585798db35fSJoel Becker } 586798db35fSJoel Becker 587798db35fSJoel Becker ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh); 588798db35fSJoel Becker brelse(bg_bh); 589798db35fSJoel Becker } 590798db35fSJoel Becker 591798db35fSJoel Becker static struct buffer_head * 592798db35fSJoel Becker ocfs2_block_group_alloc_discontig(handle_t *handle, 593798db35fSJoel Becker struct inode *alloc_inode, 594798db35fSJoel Becker struct ocfs2_alloc_context *ac, 5958b06bc59SJoel Becker struct ocfs2_chain_list *cl) 596798db35fSJoel Becker { 597798db35fSJoel Becker int status; 598798db35fSJoel Becker u32 bit_off, num_bits; 599798db35fSJoel Becker u64 bg_blkno; 600798db35fSJoel Becker unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1; 601798db35fSJoel Becker struct buffer_head *bg_bh = NULL; 602798db35fSJoel Becker unsigned int alloc_rec = ocfs2_find_smallest_chain(cl); 603798db35fSJoel Becker struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); 604798db35fSJoel Becker 6054711954eSTao Ma if (!ocfs2_supports_discontig_bg(osb)) { 606798db35fSJoel Becker status = -ENOSPC; 607798db35fSJoel Becker goto bail; 608798db35fSJoel Becker } 609798db35fSJoel Becker 6108b06bc59SJoel Becker status = ocfs2_extend_trans(handle, 6118b06bc59SJoel Becker ocfs2_calc_bg_discontig_credits(osb->sb)); 6128b06bc59SJoel Becker if (status) { 6138b06bc59SJoel Becker mlog_errno(status); 6148b06bc59SJoel Becker goto bail; 6158b06bc59SJoel Becker } 6168b06bc59SJoel Becker 61795ec0adfSJoel Becker /* 61895ec0adfSJoel Becker * We're going to be grabbing from multiple cluster groups. 61995ec0adfSJoel Becker * We don't have enough credits to relink them all, and the 62095ec0adfSJoel Becker * cluster groups will be staying in cache for the duration of 62195ec0adfSJoel Becker * this operation. 62295ec0adfSJoel Becker */ 623309a85b6SXiaowei.Hu ac->ac_disable_chain_relink = 1; 62495ec0adfSJoel Becker 625798db35fSJoel Becker /* Claim the first region */ 626798db35fSJoel Becker status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits, 627798db35fSJoel Becker &bit_off, &num_bits); 628798db35fSJoel Becker if (status < 0) { 629798db35fSJoel Becker if (status != -ENOSPC) 630798db35fSJoel Becker mlog_errno(status); 631798db35fSJoel Becker goto bail; 632798db35fSJoel Becker } 633798db35fSJoel Becker min_bits = num_bits; 634798db35fSJoel Becker 635798db35fSJoel Becker /* setup the group */ 636798db35fSJoel Becker bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off); 6372f73e135STao Ma trace_ocfs2_block_group_alloc_discontig( 6382f73e135STao Ma (unsigned long long)bg_blkno, alloc_rec); 639798db35fSJoel Becker 640798db35fSJoel Becker bg_bh = sb_getblk(osb->sb, bg_blkno); 641798db35fSJoel Becker if (!bg_bh) { 6427391a294SRui Xiang status = -ENOMEM; 643798db35fSJoel Becker mlog_errno(status); 644798db35fSJoel Becker goto bail; 645798db35fSJoel Becker } 646798db35fSJoel Becker ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh); 647798db35fSJoel Becker 648798db35fSJoel Becker status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh, 649798db35fSJoel Becker bg_blkno, num_bits, alloc_rec, cl); 650798db35fSJoel Becker if (status < 0) { 651798db35fSJoel Becker mlog_errno(status); 652798db35fSJoel Becker goto bail; 653798db35fSJoel Becker } 654798db35fSJoel Becker 655798db35fSJoel Becker status = ocfs2_block_group_grow_discontig(handle, alloc_inode, 656798db35fSJoel Becker bg_bh, ac, cl, min_bits); 657798db35fSJoel Becker if (status) 658798db35fSJoel Becker mlog_errno(status); 659798db35fSJoel Becker 660798db35fSJoel Becker bail: 661798db35fSJoel Becker if (status) 6628b06bc59SJoel Becker ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh); 663798db35fSJoel Becker return status ? ERR_PTR(status) : bg_bh; 664798db35fSJoel Becker } 665798db35fSJoel Becker 666ccd979bdSMark Fasheh /* 667ccd979bdSMark Fasheh * We expect the block group allocator to already be locked. 668ccd979bdSMark Fasheh */ 669ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb, 670ccd979bdSMark Fasheh struct inode *alloc_inode, 6711187c968SJoel Becker struct buffer_head *bh, 67260ca81e8STao Ma u64 max_block, 673feb473a6STao Ma u64 *last_alloc_group, 67460ca81e8STao Ma int flags) 675ccd979bdSMark Fasheh { 676ccd979bdSMark Fasheh int status, credits; 677ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data; 678ccd979bdSMark Fasheh struct ocfs2_chain_list *cl; 679ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac = NULL; 6801fabe148SMark Fasheh handle_t *handle = NULL; 6814711954eSTao Ma u16 alloc_rec; 682ccd979bdSMark Fasheh struct buffer_head *bg_bh = NULL; 683ccd979bdSMark Fasheh struct ocfs2_group_desc *bg; 684ccd979bdSMark Fasheh 685ccd979bdSMark Fasheh BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode)); 686ccd979bdSMark Fasheh 687ccd979bdSMark Fasheh cl = &fe->id2.i_chain; 6881187c968SJoel Becker status = ocfs2_reserve_clusters_with_limit(osb, 689ccd979bdSMark Fasheh le16_to_cpu(cl->cl_cpg), 69060ca81e8STao Ma max_block, flags, &ac); 691ccd979bdSMark Fasheh if (status < 0) { 692ccd979bdSMark Fasheh if (status != -ENOSPC) 693ccd979bdSMark Fasheh mlog_errno(status); 694ccd979bdSMark Fasheh goto bail; 695ccd979bdSMark Fasheh } 696ccd979bdSMark Fasheh 697ccd979bdSMark Fasheh credits = ocfs2_calc_group_alloc_credits(osb->sb, 698ccd979bdSMark Fasheh le16_to_cpu(cl->cl_cpg)); 69965eff9ccSMark Fasheh handle = ocfs2_start_trans(osb, credits); 700ccd979bdSMark Fasheh if (IS_ERR(handle)) { 701ccd979bdSMark Fasheh status = PTR_ERR(handle); 702ccd979bdSMark Fasheh handle = NULL; 703ccd979bdSMark Fasheh mlog_errno(status); 704ccd979bdSMark Fasheh goto bail; 705ccd979bdSMark Fasheh } 706ccd979bdSMark Fasheh 707feb473a6STao Ma if (last_alloc_group && *last_alloc_group != 0) { 7082f73e135STao Ma trace_ocfs2_block_group_alloc( 709feb473a6STao Ma (unsigned long long)*last_alloc_group); 710feb473a6STao Ma ac->ac_last_group = *last_alloc_group; 711feb473a6STao Ma } 712798db35fSJoel Becker 713798db35fSJoel Becker bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode, 714798db35fSJoel Becker ac, cl); 715798db35fSJoel Becker if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC)) 716798db35fSJoel Becker bg_bh = ocfs2_block_group_alloc_discontig(handle, 717798db35fSJoel Becker alloc_inode, 7188b06bc59SJoel Becker ac, cl); 719798db35fSJoel Becker if (IS_ERR(bg_bh)) { 720798db35fSJoel Becker status = PTR_ERR(bg_bh); 721798db35fSJoel Becker bg_bh = NULL; 722ccd979bdSMark Fasheh if (status != -ENOSPC) 723ccd979bdSMark Fasheh mlog_errno(status); 724ccd979bdSMark Fasheh goto bail; 725ccd979bdSMark Fasheh } 726ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) bg_bh->b_data; 727ccd979bdSMark Fasheh 7280cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), 729ccd979bdSMark Fasheh bh, OCFS2_JOURNAL_ACCESS_WRITE); 730ccd979bdSMark Fasheh if (status < 0) { 731ccd979bdSMark Fasheh mlog_errno(status); 732ccd979bdSMark Fasheh goto bail; 733ccd979bdSMark Fasheh } 734ccd979bdSMark Fasheh 7354711954eSTao Ma alloc_rec = le16_to_cpu(bg->bg_chain); 7364711954eSTao Ma le32_add_cpu(&cl->cl_recs[alloc_rec].c_free, 737ccd979bdSMark Fasheh le16_to_cpu(bg->bg_free_bits_count)); 7384711954eSTao Ma le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, 739798db35fSJoel Becker le16_to_cpu(bg->bg_bits)); 7400a463b74STao Ma cl->cl_recs[alloc_rec].c_blkno = bg->bg_blkno; 741ccd979bdSMark Fasheh if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count)) 742ccd979bdSMark Fasheh le16_add_cpu(&cl->cl_next_free_rec, 1); 743ccd979bdSMark Fasheh 744ccd979bdSMark Fasheh le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) - 745ccd979bdSMark Fasheh le16_to_cpu(bg->bg_free_bits_count)); 746ccd979bdSMark Fasheh le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits)); 747ccd979bdSMark Fasheh le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg)); 748ccd979bdSMark Fasheh 749ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bh); 750ccd979bdSMark Fasheh 751ccd979bdSMark Fasheh spin_lock(&OCFS2_I(alloc_inode)->ip_lock); 752ccd979bdSMark Fasheh OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters); 753ccd979bdSMark Fasheh fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb, 754ccd979bdSMark Fasheh le32_to_cpu(fe->i_clusters))); 755ccd979bdSMark Fasheh spin_unlock(&OCFS2_I(alloc_inode)->ip_lock); 756ccd979bdSMark Fasheh i_size_write(alloc_inode, le64_to_cpu(fe->i_size)); 7578110b073SMark Fasheh alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode); 7586fdb702dSDarrick J. Wong ocfs2_update_inode_fsync_trans(handle, alloc_inode, 0); 759ccd979bdSMark Fasheh 760ccd979bdSMark Fasheh status = 0; 761feb473a6STao Ma 762feb473a6STao Ma /* save the new last alloc group so that the caller can cache it. */ 763feb473a6STao Ma if (last_alloc_group) 764feb473a6STao Ma *last_alloc_group = ac->ac_last_group; 765feb473a6STao Ma 766ccd979bdSMark Fasheh bail: 767ccd979bdSMark Fasheh if (handle) 76802dc1af4SMark Fasheh ocfs2_commit_trans(osb, handle); 769ccd979bdSMark Fasheh 770ccd979bdSMark Fasheh if (ac) 771ccd979bdSMark Fasheh ocfs2_free_alloc_context(ac); 772ccd979bdSMark Fasheh 773ccd979bdSMark Fasheh brelse(bg_bh); 774ccd979bdSMark Fasheh 775c1e8d35eSTao Ma if (status) 776c1e8d35eSTao Ma mlog_errno(status); 777ccd979bdSMark Fasheh return status; 778ccd979bdSMark Fasheh } 779ccd979bdSMark Fasheh 780ccd979bdSMark Fasheh static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb, 781da5cbf2fSMark Fasheh struct ocfs2_alloc_context *ac, 782da5cbf2fSMark Fasheh int type, 783ffda89a3STao Ma u32 slot, 784feb473a6STao Ma u64 *last_alloc_group, 78560ca81e8STao Ma int flags) 786ccd979bdSMark Fasheh { 787ccd979bdSMark Fasheh int status; 788ccd979bdSMark Fasheh u32 bits_wanted = ac->ac_bits_wanted; 789da5cbf2fSMark Fasheh struct inode *alloc_inode; 790ccd979bdSMark Fasheh struct buffer_head *bh = NULL; 791ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 792ccd979bdSMark Fasheh u32 free_bits; 793ccd979bdSMark Fasheh 794da5cbf2fSMark Fasheh alloc_inode = ocfs2_get_system_file_inode(osb, type, slot); 795da5cbf2fSMark Fasheh if (!alloc_inode) { 796da5cbf2fSMark Fasheh mlog_errno(-EINVAL); 797da5cbf2fSMark Fasheh return -EINVAL; 798ccd979bdSMark Fasheh } 799ccd979bdSMark Fasheh 800da5cbf2fSMark Fasheh mutex_lock(&alloc_inode->i_mutex); 801da5cbf2fSMark Fasheh 802e63aecb6SMark Fasheh status = ocfs2_inode_lock(alloc_inode, &bh, 1); 803da5cbf2fSMark Fasheh if (status < 0) { 804da5cbf2fSMark Fasheh mutex_unlock(&alloc_inode->i_mutex); 805da5cbf2fSMark Fasheh iput(alloc_inode); 806da5cbf2fSMark Fasheh 807da5cbf2fSMark Fasheh mlog_errno(status); 808da5cbf2fSMark Fasheh return status; 809da5cbf2fSMark Fasheh } 810da5cbf2fSMark Fasheh 811da5cbf2fSMark Fasheh ac->ac_inode = alloc_inode; 812a4a48911STao Ma ac->ac_alloc_slot = slot; 813da5cbf2fSMark Fasheh 814ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) bh->b_data; 81510995aa2SJoel Becker 81610995aa2SJoel Becker /* The bh was validated by the inode read inside 81710995aa2SJoel Becker * ocfs2_inode_lock(). Any corruption is a code bug. */ 81810995aa2SJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); 81910995aa2SJoel Becker 820ccd979bdSMark Fasheh if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) { 8217ecef14aSJoe Perches status = ocfs2_error(alloc_inode->i_sb, 8227ecef14aSJoe Perches "Invalid chain allocator %llu\n", 823b0697053SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno)); 824ccd979bdSMark Fasheh goto bail; 825ccd979bdSMark Fasheh } 826ccd979bdSMark Fasheh 827ccd979bdSMark Fasheh free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) - 828ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_used); 829ccd979bdSMark Fasheh 830ccd979bdSMark Fasheh if (bits_wanted > free_bits) { 831ccd979bdSMark Fasheh /* cluster bitmap never grows */ 832ccd979bdSMark Fasheh if (ocfs2_is_cluster_bitmap(alloc_inode)) { 8332f73e135STao Ma trace_ocfs2_reserve_suballoc_bits_nospc(bits_wanted, 8342f73e135STao Ma free_bits); 835ccd979bdSMark Fasheh status = -ENOSPC; 836ccd979bdSMark Fasheh goto bail; 837ccd979bdSMark Fasheh } 838ccd979bdSMark Fasheh 83960ca81e8STao Ma if (!(flags & ALLOC_NEW_GROUP)) { 8402f73e135STao Ma trace_ocfs2_reserve_suballoc_bits_no_new_group( 841ffda89a3STao Ma slot, bits_wanted, free_bits); 842ffda89a3STao Ma status = -ENOSPC; 843ffda89a3STao Ma goto bail; 844ffda89a3STao Ma } 845ffda89a3STao Ma 8461187c968SJoel Becker status = ocfs2_block_group_alloc(osb, alloc_inode, bh, 847feb473a6STao Ma ac->ac_max_block, 848feb473a6STao Ma last_alloc_group, flags); 849ccd979bdSMark Fasheh if (status < 0) { 850ccd979bdSMark Fasheh if (status != -ENOSPC) 851ccd979bdSMark Fasheh mlog_errno(status); 852ccd979bdSMark Fasheh goto bail; 853ccd979bdSMark Fasheh } 854ccd979bdSMark Fasheh atomic_inc(&osb->alloc_stats.bg_extends); 855ccd979bdSMark Fasheh 856ccd979bdSMark Fasheh /* You should never ask for this much metadata */ 857ccd979bdSMark Fasheh BUG_ON(bits_wanted > 858ccd979bdSMark Fasheh (le32_to_cpu(fe->id1.bitmap1.i_total) 859ccd979bdSMark Fasheh - le32_to_cpu(fe->id1.bitmap1.i_used))); 860ccd979bdSMark Fasheh } 861ccd979bdSMark Fasheh 862ccd979bdSMark Fasheh get_bh(bh); 863ccd979bdSMark Fasheh ac->ac_bh = bh; 864ccd979bdSMark Fasheh bail: 865ccd979bdSMark Fasheh brelse(bh); 866ccd979bdSMark Fasheh 867c1e8d35eSTao Ma if (status) 868c1e8d35eSTao Ma mlog_errno(status); 869ccd979bdSMark Fasheh return status; 870ccd979bdSMark Fasheh } 871ccd979bdSMark Fasheh 872b89c5428STiger Yang static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb) 873b89c5428STiger Yang { 874b89c5428STiger Yang spin_lock(&osb->osb_lock); 875b89c5428STiger Yang osb->s_inode_steal_slot = OCFS2_INVALID_SLOT; 876b89c5428STiger Yang spin_unlock(&osb->osb_lock); 877b89c5428STiger Yang atomic_set(&osb->s_num_inodes_stolen, 0); 878b89c5428STiger Yang } 879b89c5428STiger Yang 880b89c5428STiger Yang static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb) 881b89c5428STiger Yang { 882b89c5428STiger Yang spin_lock(&osb->osb_lock); 883b89c5428STiger Yang osb->s_meta_steal_slot = OCFS2_INVALID_SLOT; 884b89c5428STiger Yang spin_unlock(&osb->osb_lock); 885b89c5428STiger Yang atomic_set(&osb->s_num_meta_stolen, 0); 886b89c5428STiger Yang } 887b89c5428STiger Yang 888b89c5428STiger Yang void ocfs2_init_steal_slots(struct ocfs2_super *osb) 889b89c5428STiger Yang { 890b89c5428STiger Yang ocfs2_init_inode_steal_slot(osb); 891b89c5428STiger Yang ocfs2_init_meta_steal_slot(osb); 892b89c5428STiger Yang } 893b89c5428STiger Yang 894b89c5428STiger Yang static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type) 895b89c5428STiger Yang { 896b89c5428STiger Yang spin_lock(&osb->osb_lock); 897b89c5428STiger Yang if (type == INODE_ALLOC_SYSTEM_INODE) 898b89c5428STiger Yang osb->s_inode_steal_slot = slot; 899b89c5428STiger Yang else if (type == EXTENT_ALLOC_SYSTEM_INODE) 900b89c5428STiger Yang osb->s_meta_steal_slot = slot; 901b89c5428STiger Yang spin_unlock(&osb->osb_lock); 902b89c5428STiger Yang } 903b89c5428STiger Yang 904b89c5428STiger Yang static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type) 905b89c5428STiger Yang { 906b89c5428STiger Yang int slot = OCFS2_INVALID_SLOT; 907b89c5428STiger Yang 908b89c5428STiger Yang spin_lock(&osb->osb_lock); 909b89c5428STiger Yang if (type == INODE_ALLOC_SYSTEM_INODE) 910b89c5428STiger Yang slot = osb->s_inode_steal_slot; 911b89c5428STiger Yang else if (type == EXTENT_ALLOC_SYSTEM_INODE) 912b89c5428STiger Yang slot = osb->s_meta_steal_slot; 913b89c5428STiger Yang spin_unlock(&osb->osb_lock); 914b89c5428STiger Yang 915b89c5428STiger Yang return slot; 916b89c5428STiger Yang } 917b89c5428STiger Yang 918b89c5428STiger Yang static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb) 919b89c5428STiger Yang { 920b89c5428STiger Yang return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE); 921b89c5428STiger Yang } 922b89c5428STiger Yang 923b89c5428STiger Yang static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb) 924b89c5428STiger Yang { 925b89c5428STiger Yang return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE); 926b89c5428STiger Yang } 927b89c5428STiger Yang 928b89c5428STiger Yang static int ocfs2_steal_resource(struct ocfs2_super *osb, 929b89c5428STiger Yang struct ocfs2_alloc_context *ac, 930b89c5428STiger Yang int type) 931b89c5428STiger Yang { 932b89c5428STiger Yang int i, status = -ENOSPC; 933b89c5428STiger Yang int slot = __ocfs2_get_steal_slot(osb, type); 934b89c5428STiger Yang 935b89c5428STiger Yang /* Start to steal resource from the first slot after ours. */ 936b89c5428STiger Yang if (slot == OCFS2_INVALID_SLOT) 937b89c5428STiger Yang slot = osb->slot_num + 1; 938b89c5428STiger Yang 939b89c5428STiger Yang for (i = 0; i < osb->max_slots; i++, slot++) { 940b89c5428STiger Yang if (slot == osb->max_slots) 941b89c5428STiger Yang slot = 0; 942b89c5428STiger Yang 943b89c5428STiger Yang if (slot == osb->slot_num) 944b89c5428STiger Yang continue; 945b89c5428STiger Yang 946b89c5428STiger Yang status = ocfs2_reserve_suballoc_bits(osb, ac, 947b89c5428STiger Yang type, 948b89c5428STiger Yang (u32)slot, NULL, 949b89c5428STiger Yang NOT_ALLOC_NEW_GROUP); 950b89c5428STiger Yang if (status >= 0) { 951b89c5428STiger Yang __ocfs2_set_steal_slot(osb, slot, type); 952b89c5428STiger Yang break; 953b89c5428STiger Yang } 954b89c5428STiger Yang 955b89c5428STiger Yang ocfs2_free_ac_resource(ac); 956b89c5428STiger Yang } 957b89c5428STiger Yang 958b89c5428STiger Yang return status; 959b89c5428STiger Yang } 960b89c5428STiger Yang 961b89c5428STiger Yang static int ocfs2_steal_inode(struct ocfs2_super *osb, 962b89c5428STiger Yang struct ocfs2_alloc_context *ac) 963b89c5428STiger Yang { 964b89c5428STiger Yang return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE); 965b89c5428STiger Yang } 966b89c5428STiger Yang 967b89c5428STiger Yang static int ocfs2_steal_meta(struct ocfs2_super *osb, 968b89c5428STiger Yang struct ocfs2_alloc_context *ac) 969b89c5428STiger Yang { 970b89c5428STiger Yang return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE); 971b89c5428STiger Yang } 972b89c5428STiger Yang 973cf1d6c76STiger Yang int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb, 974cf1d6c76STiger Yang int blocks, 975ccd979bdSMark Fasheh struct ocfs2_alloc_context **ac) 976ccd979bdSMark Fasheh { 977ccd979bdSMark Fasheh int status; 978b89c5428STiger Yang int slot = ocfs2_get_meta_steal_slot(osb); 979ccd979bdSMark Fasheh 980cd861280SRobert P. J. Day *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); 981ccd979bdSMark Fasheh if (!(*ac)) { 982ccd979bdSMark Fasheh status = -ENOMEM; 983ccd979bdSMark Fasheh mlog_errno(status); 984ccd979bdSMark Fasheh goto bail; 985ccd979bdSMark Fasheh } 986ccd979bdSMark Fasheh 987cf1d6c76STiger Yang (*ac)->ac_bits_wanted = blocks; 988ccd979bdSMark Fasheh (*ac)->ac_which = OCFS2_AC_USE_META; 989ccd979bdSMark Fasheh (*ac)->ac_group_search = ocfs2_block_group_search; 990ccd979bdSMark Fasheh 991b89c5428STiger Yang if (slot != OCFS2_INVALID_SLOT && 992b89c5428STiger Yang atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL) 993b89c5428STiger Yang goto extent_steal; 994b89c5428STiger Yang 995b89c5428STiger Yang atomic_set(&osb->s_num_meta_stolen, 0); 996da5cbf2fSMark Fasheh status = ocfs2_reserve_suballoc_bits(osb, (*ac), 997ffda89a3STao Ma EXTENT_ALLOC_SYSTEM_INODE, 998b89c5428STiger Yang (u32)osb->slot_num, NULL, 99933d5d380SMark Fasheh ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP); 1000b89c5428STiger Yang 1001b89c5428STiger Yang 1002b89c5428STiger Yang if (status >= 0) { 1003b89c5428STiger Yang status = 0; 1004b89c5428STiger Yang if (slot != OCFS2_INVALID_SLOT) 1005b89c5428STiger Yang ocfs2_init_meta_steal_slot(osb); 1006b89c5428STiger Yang goto bail; 1007b89c5428STiger Yang } else if (status < 0 && status != -ENOSPC) { 1008b89c5428STiger Yang mlog_errno(status); 1009b89c5428STiger Yang goto bail; 1010b89c5428STiger Yang } 1011b89c5428STiger Yang 1012b89c5428STiger Yang ocfs2_free_ac_resource(*ac); 1013b89c5428STiger Yang 1014b89c5428STiger Yang extent_steal: 1015b89c5428STiger Yang status = ocfs2_steal_meta(osb, *ac); 1016b89c5428STiger Yang atomic_inc(&osb->s_num_meta_stolen); 1017ccd979bdSMark Fasheh if (status < 0) { 1018ccd979bdSMark Fasheh if (status != -ENOSPC) 1019ccd979bdSMark Fasheh mlog_errno(status); 1020ccd979bdSMark Fasheh goto bail; 1021ccd979bdSMark Fasheh } 1022ccd979bdSMark Fasheh 1023ccd979bdSMark Fasheh status = 0; 1024ccd979bdSMark Fasheh bail: 1025ccd979bdSMark Fasheh if ((status < 0) && *ac) { 1026ccd979bdSMark Fasheh ocfs2_free_alloc_context(*ac); 1027ccd979bdSMark Fasheh *ac = NULL; 1028ccd979bdSMark Fasheh } 1029ccd979bdSMark Fasheh 1030c1e8d35eSTao Ma if (status) 1031c1e8d35eSTao Ma mlog_errno(status); 1032ccd979bdSMark Fasheh return status; 1033ccd979bdSMark Fasheh } 1034ccd979bdSMark Fasheh 1035cf1d6c76STiger Yang int ocfs2_reserve_new_metadata(struct ocfs2_super *osb, 1036cf1d6c76STiger Yang struct ocfs2_extent_list *root_el, 1037cf1d6c76STiger Yang struct ocfs2_alloc_context **ac) 1038cf1d6c76STiger Yang { 1039cf1d6c76STiger Yang return ocfs2_reserve_new_metadata_blocks(osb, 1040cf1d6c76STiger Yang ocfs2_extend_meta_needed(root_el), 1041cf1d6c76STiger Yang ac); 1042cf1d6c76STiger Yang } 1043cf1d6c76STiger Yang 1044ccd979bdSMark Fasheh int ocfs2_reserve_new_inode(struct ocfs2_super *osb, 1045ccd979bdSMark Fasheh struct ocfs2_alloc_context **ac) 1046ccd979bdSMark Fasheh { 1047ccd979bdSMark Fasheh int status; 1048b89c5428STiger Yang int slot = ocfs2_get_inode_steal_slot(osb); 1049feb473a6STao Ma u64 alloc_group; 1050ccd979bdSMark Fasheh 1051cd861280SRobert P. J. Day *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); 1052ccd979bdSMark Fasheh if (!(*ac)) { 1053ccd979bdSMark Fasheh status = -ENOMEM; 1054ccd979bdSMark Fasheh mlog_errno(status); 1055ccd979bdSMark Fasheh goto bail; 1056ccd979bdSMark Fasheh } 1057ccd979bdSMark Fasheh 1058ccd979bdSMark Fasheh (*ac)->ac_bits_wanted = 1; 1059ccd979bdSMark Fasheh (*ac)->ac_which = OCFS2_AC_USE_INODE; 1060ccd979bdSMark Fasheh 1061ccd979bdSMark Fasheh (*ac)->ac_group_search = ocfs2_block_group_search; 1062ccd979bdSMark Fasheh 10634d0ddb2cSTao Ma /* 10641187c968SJoel Becker * stat(2) can't handle i_ino > 32bits, so we tell the 10651187c968SJoel Becker * lower levels not to allocate us a block group past that 106612462f1dSJoel Becker * limit. The 'inode64' mount option avoids this behavior. 10671187c968SJoel Becker */ 106812462f1dSJoel Becker if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64)) 10691187c968SJoel Becker (*ac)->ac_max_block = (u32)~0U; 10701187c968SJoel Becker 10711187c968SJoel Becker /* 10724d0ddb2cSTao Ma * slot is set when we successfully steal inode from other nodes. 10734d0ddb2cSTao Ma * It is reset in 3 places: 10744d0ddb2cSTao Ma * 1. when we flush the truncate log 10754d0ddb2cSTao Ma * 2. when we complete local alloc recovery. 10764d0ddb2cSTao Ma * 3. when we successfully allocate from our own slot. 10774d0ddb2cSTao Ma * After it is set, we will go on stealing inodes until we find the 10784d0ddb2cSTao Ma * need to check our slots to see whether there is some space for us. 10794d0ddb2cSTao Ma */ 10804d0ddb2cSTao Ma if (slot != OCFS2_INVALID_SLOT && 1081b89c5428STiger Yang atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL) 10824d0ddb2cSTao Ma goto inode_steal; 10834d0ddb2cSTao Ma 10844d0ddb2cSTao Ma atomic_set(&osb->s_num_inodes_stolen, 0); 1085feb473a6STao Ma alloc_group = osb->osb_inode_alloc_group; 1086da5cbf2fSMark Fasheh status = ocfs2_reserve_suballoc_bits(osb, *ac, 1087da5cbf2fSMark Fasheh INODE_ALLOC_SYSTEM_INODE, 1088b89c5428STiger Yang (u32)osb->slot_num, 1089feb473a6STao Ma &alloc_group, 109060ca81e8STao Ma ALLOC_NEW_GROUP | 109160ca81e8STao Ma ALLOC_GROUPS_FROM_GLOBAL); 10924d0ddb2cSTao Ma if (status >= 0) { 10934d0ddb2cSTao Ma status = 0; 10944d0ddb2cSTao Ma 1095feb473a6STao Ma spin_lock(&osb->osb_lock); 1096feb473a6STao Ma osb->osb_inode_alloc_group = alloc_group; 1097feb473a6STao Ma spin_unlock(&osb->osb_lock); 10982f73e135STao Ma trace_ocfs2_reserve_new_inode_new_group( 10992f73e135STao Ma (unsigned long long)alloc_group); 1100feb473a6STao Ma 11014d0ddb2cSTao Ma /* 11024d0ddb2cSTao Ma * Some inodes must be freed by us, so try to allocate 11034d0ddb2cSTao Ma * from our own next time. 11044d0ddb2cSTao Ma */ 11054d0ddb2cSTao Ma if (slot != OCFS2_INVALID_SLOT) 11064d0ddb2cSTao Ma ocfs2_init_inode_steal_slot(osb); 11074d0ddb2cSTao Ma goto bail; 11084d0ddb2cSTao Ma } else if (status < 0 && status != -ENOSPC) { 11094d0ddb2cSTao Ma mlog_errno(status); 11104d0ddb2cSTao Ma goto bail; 11114d0ddb2cSTao Ma } 11124d0ddb2cSTao Ma 11134d0ddb2cSTao Ma ocfs2_free_ac_resource(*ac); 11144d0ddb2cSTao Ma 11154d0ddb2cSTao Ma inode_steal: 1116b89c5428STiger Yang status = ocfs2_steal_inode(osb, *ac); 11174d0ddb2cSTao Ma atomic_inc(&osb->s_num_inodes_stolen); 1118ccd979bdSMark Fasheh if (status < 0) { 1119ccd979bdSMark Fasheh if (status != -ENOSPC) 1120ccd979bdSMark Fasheh mlog_errno(status); 1121ccd979bdSMark Fasheh goto bail; 1122ccd979bdSMark Fasheh } 1123ccd979bdSMark Fasheh 1124ccd979bdSMark Fasheh status = 0; 1125ccd979bdSMark Fasheh bail: 1126ccd979bdSMark Fasheh if ((status < 0) && *ac) { 1127ccd979bdSMark Fasheh ocfs2_free_alloc_context(*ac); 1128ccd979bdSMark Fasheh *ac = NULL; 1129ccd979bdSMark Fasheh } 1130ccd979bdSMark Fasheh 1131c1e8d35eSTao Ma if (status) 1132c1e8d35eSTao Ma mlog_errno(status); 1133ccd979bdSMark Fasheh return status; 1134ccd979bdSMark Fasheh } 1135ccd979bdSMark Fasheh 1136ccd979bdSMark Fasheh /* local alloc code has to do the same thing, so rather than do this 1137ccd979bdSMark Fasheh * twice.. */ 1138ccd979bdSMark Fasheh int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb, 1139ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac) 1140ccd979bdSMark Fasheh { 1141ccd979bdSMark Fasheh int status; 1142ccd979bdSMark Fasheh 1143ccd979bdSMark Fasheh ac->ac_which = OCFS2_AC_USE_MAIN; 1144ccd979bdSMark Fasheh ac->ac_group_search = ocfs2_cluster_group_search; 1145ccd979bdSMark Fasheh 1146da5cbf2fSMark Fasheh status = ocfs2_reserve_suballoc_bits(osb, ac, 1147da5cbf2fSMark Fasheh GLOBAL_BITMAP_SYSTEM_INODE, 1148feb473a6STao Ma OCFS2_INVALID_SLOT, NULL, 1149ffda89a3STao Ma ALLOC_NEW_GROUP); 1150da5cbf2fSMark Fasheh if (status < 0 && status != -ENOSPC) { 1151ccd979bdSMark Fasheh mlog_errno(status); 1152da5cbf2fSMark Fasheh goto bail; 1153da5cbf2fSMark Fasheh } 1154da5cbf2fSMark Fasheh 1155ccd979bdSMark Fasheh bail: 1156ccd979bdSMark Fasheh return status; 1157ccd979bdSMark Fasheh } 1158ccd979bdSMark Fasheh 1159ccd979bdSMark Fasheh /* Callers don't need to care which bitmap (local alloc or main) to 1160ccd979bdSMark Fasheh * use so we figure it out for them, but unfortunately this clutters 1161ccd979bdSMark Fasheh * things a bit. */ 11621187c968SJoel Becker static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb, 11631187c968SJoel Becker u32 bits_wanted, u64 max_block, 116460ca81e8STao Ma int flags, 1165ccd979bdSMark Fasheh struct ocfs2_alloc_context **ac) 1166ccd979bdSMark Fasheh { 1167ccd979bdSMark Fasheh int status; 1168ccd979bdSMark Fasheh 1169cd861280SRobert P. J. Day *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); 1170ccd979bdSMark Fasheh if (!(*ac)) { 1171ccd979bdSMark Fasheh status = -ENOMEM; 1172ccd979bdSMark Fasheh mlog_errno(status); 1173ccd979bdSMark Fasheh goto bail; 1174ccd979bdSMark Fasheh } 1175ccd979bdSMark Fasheh 1176ccd979bdSMark Fasheh (*ac)->ac_bits_wanted = bits_wanted; 11771187c968SJoel Becker (*ac)->ac_max_block = max_block; 1178ccd979bdSMark Fasheh 1179ccd979bdSMark Fasheh status = -ENOSPC; 118060ca81e8STao Ma if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) && 118160ca81e8STao Ma ocfs2_alloc_should_use_local(osb, bits_wanted)) { 1182ccd979bdSMark Fasheh status = ocfs2_reserve_local_alloc_bits(osb, 1183ccd979bdSMark Fasheh bits_wanted, 1184ccd979bdSMark Fasheh *ac); 1185a57c8fd2SMark Fasheh if ((status < 0) && (status != -ENOSPC)) { 1186ccd979bdSMark Fasheh mlog_errno(status); 1187ccd979bdSMark Fasheh goto bail; 1188ccd979bdSMark Fasheh } 1189ccd979bdSMark Fasheh } 1190ccd979bdSMark Fasheh 1191ccd979bdSMark Fasheh if (status == -ENOSPC) { 1192ccd979bdSMark Fasheh status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac); 1193ccd979bdSMark Fasheh if (status < 0) { 1194ccd979bdSMark Fasheh if (status != -ENOSPC) 1195ccd979bdSMark Fasheh mlog_errno(status); 1196ccd979bdSMark Fasheh goto bail; 1197ccd979bdSMark Fasheh } 1198ccd979bdSMark Fasheh } 1199ccd979bdSMark Fasheh 1200ccd979bdSMark Fasheh status = 0; 1201ccd979bdSMark Fasheh bail: 1202ccd979bdSMark Fasheh if ((status < 0) && *ac) { 1203ccd979bdSMark Fasheh ocfs2_free_alloc_context(*ac); 1204ccd979bdSMark Fasheh *ac = NULL; 1205ccd979bdSMark Fasheh } 1206ccd979bdSMark Fasheh 1207c1e8d35eSTao Ma if (status) 1208c1e8d35eSTao Ma mlog_errno(status); 1209ccd979bdSMark Fasheh return status; 1210ccd979bdSMark Fasheh } 1211ccd979bdSMark Fasheh 12121187c968SJoel Becker int ocfs2_reserve_clusters(struct ocfs2_super *osb, 12131187c968SJoel Becker u32 bits_wanted, 12141187c968SJoel Becker struct ocfs2_alloc_context **ac) 12151187c968SJoel Becker { 121660ca81e8STao Ma return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0, 121760ca81e8STao Ma ALLOC_NEW_GROUP, ac); 12181187c968SJoel Becker } 12191187c968SJoel Becker 1220ccd979bdSMark Fasheh /* 1221ccd979bdSMark Fasheh * More or less lifted from ext3. I'll leave their description below: 1222ccd979bdSMark Fasheh * 1223ccd979bdSMark Fasheh * "For ext3 allocations, we must not reuse any blocks which are 1224ccd979bdSMark Fasheh * allocated in the bitmap buffer's "last committed data" copy. This 1225ccd979bdSMark Fasheh * prevents deletes from freeing up the page for reuse until we have 1226ccd979bdSMark Fasheh * committed the delete transaction. 1227ccd979bdSMark Fasheh * 1228ccd979bdSMark Fasheh * If we didn't do this, then deleting something and reallocating it as 1229ccd979bdSMark Fasheh * data would allow the old block to be overwritten before the 1230ccd979bdSMark Fasheh * transaction committed (because we force data to disk before commit). 1231ccd979bdSMark Fasheh * This would lead to corruption if we crashed between overwriting the 1232ccd979bdSMark Fasheh * data and committing the delete. 1233ccd979bdSMark Fasheh * 1234ccd979bdSMark Fasheh * @@@ We may want to make this allocation behaviour conditional on 1235ccd979bdSMark Fasheh * data-writes at some point, and disable it for metadata allocations or 1236ccd979bdSMark Fasheh * sync-data inodes." 1237ccd979bdSMark Fasheh * 1238ccd979bdSMark Fasheh * Note: OCFS2 already does this differently for metadata vs data 1239c78bad11SJoe Perches * allocations, as those bitmaps are separate and undo access is never 1240ccd979bdSMark Fasheh * called on a metadata group descriptor. 1241ccd979bdSMark Fasheh */ 1242ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh, 1243ccd979bdSMark Fasheh int nr) 1244ccd979bdSMark Fasheh { 1245ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 124694e41ecfSSunil Mushran int ret; 1247ccd979bdSMark Fasheh 1248ccd979bdSMark Fasheh if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap)) 1249ccd979bdSMark Fasheh return 0; 125094e41ecfSSunil Mushran 125194e41ecfSSunil Mushran if (!buffer_jbd(bg_bh)) 1252ccd979bdSMark Fasheh return 1; 1253ccd979bdSMark Fasheh 125494e41ecfSSunil Mushran jbd_lock_bh_state(bg_bh); 1255ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data; 125694e41ecfSSunil Mushran if (bg) 125794e41ecfSSunil Mushran ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap); 125894e41ecfSSunil Mushran else 125994e41ecfSSunil Mushran ret = 1; 126094e41ecfSSunil Mushran jbd_unlock_bh_state(bg_bh); 126194e41ecfSSunil Mushran 126294e41ecfSSunil Mushran return ret; 1263ccd979bdSMark Fasheh } 1264ccd979bdSMark Fasheh 1265ccd979bdSMark Fasheh static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb, 1266ccd979bdSMark Fasheh struct buffer_head *bg_bh, 1267ccd979bdSMark Fasheh unsigned int bits_wanted, 12687bf72edeSMark Fasheh unsigned int total_bits, 12697d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1270ccd979bdSMark Fasheh { 1271ccd979bdSMark Fasheh void *bitmap; 1272ccd979bdSMark Fasheh u16 best_offset, best_size; 1273ccd979bdSMark Fasheh int offset, start, found, status = 0; 1274ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 1275ccd979bdSMark Fasheh 127642035306SJoel Becker /* Callers got this descriptor from 127742035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 127842035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 1279ccd979bdSMark Fasheh 1280ccd979bdSMark Fasheh found = start = best_offset = best_size = 0; 1281ccd979bdSMark Fasheh bitmap = bg->bg_bitmap; 1282ccd979bdSMark Fasheh 12837bf72edeSMark Fasheh while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) { 12847bf72edeSMark Fasheh if (offset == total_bits) 1285ccd979bdSMark Fasheh break; 1286ccd979bdSMark Fasheh 1287ccd979bdSMark Fasheh if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) { 1288ccd979bdSMark Fasheh /* We found a zero, but we can't use it as it 1289ccd979bdSMark Fasheh * hasn't been put to disk yet! */ 1290ccd979bdSMark Fasheh found = 0; 1291ccd979bdSMark Fasheh start = offset + 1; 1292ccd979bdSMark Fasheh } else if (offset == start) { 1293ccd979bdSMark Fasheh /* we found a zero */ 1294ccd979bdSMark Fasheh found++; 1295ccd979bdSMark Fasheh /* move start to the next bit to test */ 1296ccd979bdSMark Fasheh start++; 1297ccd979bdSMark Fasheh } else { 1298ccd979bdSMark Fasheh /* got a zero after some ones */ 1299ccd979bdSMark Fasheh found = 1; 1300ccd979bdSMark Fasheh start = offset + 1; 1301ccd979bdSMark Fasheh } 1302ccd979bdSMark Fasheh if (found > best_size) { 1303ccd979bdSMark Fasheh best_size = found; 1304ccd979bdSMark Fasheh best_offset = start - found; 1305ccd979bdSMark Fasheh } 1306ccd979bdSMark Fasheh /* we got everything we needed */ 1307ccd979bdSMark Fasheh if (found == bits_wanted) { 1308ccd979bdSMark Fasheh /* mlog(0, "Found it all!\n"); */ 1309ccd979bdSMark Fasheh break; 1310ccd979bdSMark Fasheh } 1311ccd979bdSMark Fasheh } 1312ccd979bdSMark Fasheh 13137d1fe093SJoel Becker if (best_size) { 13147d1fe093SJoel Becker res->sr_bit_offset = best_offset; 13157d1fe093SJoel Becker res->sr_bits = best_size; 1316ccd979bdSMark Fasheh } else { 1317ccd979bdSMark Fasheh status = -ENOSPC; 1318ccd979bdSMark Fasheh /* No error log here -- see the comment above 1319ccd979bdSMark Fasheh * ocfs2_test_bg_bit_allocatable */ 1320ccd979bdSMark Fasheh } 1321ccd979bdSMark Fasheh 1322ccd979bdSMark Fasheh return status; 1323ccd979bdSMark Fasheh } 1324ccd979bdSMark Fasheh 13250a2fcd89SYounger Liu int ocfs2_block_group_set_bits(handle_t *handle, 1326ccd979bdSMark Fasheh struct inode *alloc_inode, 1327ccd979bdSMark Fasheh struct ocfs2_group_desc *bg, 1328ccd979bdSMark Fasheh struct buffer_head *group_bh, 1329ccd979bdSMark Fasheh unsigned int bit_off, 1330ccd979bdSMark Fasheh unsigned int num_bits) 1331ccd979bdSMark Fasheh { 1332ccd979bdSMark Fasheh int status; 1333ccd979bdSMark Fasheh void *bitmap = bg->bg_bitmap; 1334ccd979bdSMark Fasheh int journal_type = OCFS2_JOURNAL_ACCESS_WRITE; 1335ccd979bdSMark Fasheh 133642035306SJoel Becker /* All callers get the descriptor via 133742035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 133842035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 1339ccd979bdSMark Fasheh BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits); 1340ccd979bdSMark Fasheh 13412f73e135STao Ma trace_ocfs2_block_group_set_bits(bit_off, num_bits); 1342ccd979bdSMark Fasheh 1343ccd979bdSMark Fasheh if (ocfs2_is_cluster_bitmap(alloc_inode)) 1344ccd979bdSMark Fasheh journal_type = OCFS2_JOURNAL_ACCESS_UNDO; 1345ccd979bdSMark Fasheh 134613723d00SJoel Becker status = ocfs2_journal_access_gd(handle, 13470cf2f763SJoel Becker INODE_CACHE(alloc_inode), 1348ccd979bdSMark Fasheh group_bh, 1349ccd979bdSMark Fasheh journal_type); 1350ccd979bdSMark Fasheh if (status < 0) { 1351ccd979bdSMark Fasheh mlog_errno(status); 1352ccd979bdSMark Fasheh goto bail; 1353ccd979bdSMark Fasheh } 1354ccd979bdSMark Fasheh 1355ccd979bdSMark Fasheh le16_add_cpu(&bg->bg_free_bits_count, -num_bits); 13569b5cd10eSSrinivas Eeda if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) { 13577ecef14aSJoe Perches return ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit count %u but claims %u are freed. num_bits %d\n", 13589b5cd10eSSrinivas Eeda (unsigned long long)le64_to_cpu(bg->bg_blkno), 13599b5cd10eSSrinivas Eeda le16_to_cpu(bg->bg_bits), 13607ecef14aSJoe Perches le16_to_cpu(bg->bg_free_bits_count), 13617ecef14aSJoe Perches num_bits); 13629b5cd10eSSrinivas Eeda } 1363ccd979bdSMark Fasheh while(num_bits--) 1364ccd979bdSMark Fasheh ocfs2_set_bit(bit_off++, bitmap); 1365ccd979bdSMark Fasheh 1366ec20cec7SJoel Becker ocfs2_journal_dirty(handle, group_bh); 1367ccd979bdSMark Fasheh 1368ccd979bdSMark Fasheh bail: 1369ccd979bdSMark Fasheh return status; 1370ccd979bdSMark Fasheh } 1371ccd979bdSMark Fasheh 1372ccd979bdSMark Fasheh /* find the one with the most empty bits */ 1373ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl) 1374ccd979bdSMark Fasheh { 1375ccd979bdSMark Fasheh u16 curr, best; 1376ccd979bdSMark Fasheh 1377ccd979bdSMark Fasheh BUG_ON(!cl->cl_next_free_rec); 1378ccd979bdSMark Fasheh 1379ccd979bdSMark Fasheh best = curr = 0; 1380ccd979bdSMark Fasheh while (curr < le16_to_cpu(cl->cl_next_free_rec)) { 1381ccd979bdSMark Fasheh if (le32_to_cpu(cl->cl_recs[curr].c_free) > 1382ccd979bdSMark Fasheh le32_to_cpu(cl->cl_recs[best].c_free)) 1383ccd979bdSMark Fasheh best = curr; 1384ccd979bdSMark Fasheh curr++; 1385ccd979bdSMark Fasheh } 1386ccd979bdSMark Fasheh 1387ccd979bdSMark Fasheh BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec)); 1388ccd979bdSMark Fasheh return best; 1389ccd979bdSMark Fasheh } 1390ccd979bdSMark Fasheh 13911fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle, 1392ccd979bdSMark Fasheh struct inode *alloc_inode, 1393ccd979bdSMark Fasheh struct buffer_head *fe_bh, 1394ccd979bdSMark Fasheh struct buffer_head *bg_bh, 1395ccd979bdSMark Fasheh struct buffer_head *prev_bg_bh, 1396ccd979bdSMark Fasheh u16 chain) 1397ccd979bdSMark Fasheh { 1398ccd979bdSMark Fasheh int status; 1399ccd979bdSMark Fasheh /* there is a really tiny chance the journal calls could fail, 1400ccd979bdSMark Fasheh * but we wouldn't want inconsistent blocks in *any* case. */ 140149309841SJie Liu u64 bg_ptr, prev_bg_ptr; 1402ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data; 1403ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 1404ccd979bdSMark Fasheh struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data; 1405ccd979bdSMark Fasheh 140642035306SJoel Becker /* The caller got these descriptors from 140742035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 140842035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 140942035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg)); 1410ccd979bdSMark Fasheh 14112f73e135STao Ma trace_ocfs2_relink_block_group( 14121ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno), chain, 14131ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(bg->bg_blkno), 14141ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(prev_bg->bg_blkno)); 1415ccd979bdSMark Fasheh 1416ccd979bdSMark Fasheh bg_ptr = le64_to_cpu(bg->bg_next_group); 1417ccd979bdSMark Fasheh prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group); 1418ccd979bdSMark Fasheh 14190cf2f763SJoel Becker status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), 14200cf2f763SJoel Becker prev_bg_bh, 1421ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 142225e28921SAndrew Morton if (status < 0) 142349309841SJie Liu goto out; 1424ccd979bdSMark Fasheh 1425ccd979bdSMark Fasheh prev_bg->bg_next_group = bg->bg_next_group; 1426ec20cec7SJoel Becker ocfs2_journal_dirty(handle, prev_bg_bh); 1427ccd979bdSMark Fasheh 14280cf2f763SJoel Becker status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), 14290cf2f763SJoel Becker bg_bh, OCFS2_JOURNAL_ACCESS_WRITE); 143025e28921SAndrew Morton if (status < 0) 143149309841SJie Liu goto out_rollback_prev_bg; 1432ccd979bdSMark Fasheh 1433ccd979bdSMark Fasheh bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno; 1434ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bg_bh); 1435ccd979bdSMark Fasheh 14360cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), 14370cf2f763SJoel Becker fe_bh, OCFS2_JOURNAL_ACCESS_WRITE); 143825e28921SAndrew Morton if (status < 0) 143949309841SJie Liu goto out_rollback_bg; 1440ccd979bdSMark Fasheh 1441ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno; 1442ec20cec7SJoel Becker ocfs2_journal_dirty(handle, fe_bh); 1443ccd979bdSMark Fasheh 144449309841SJie Liu out: 144525e28921SAndrew Morton if (status < 0) 144625e28921SAndrew Morton mlog_errno(status); 144749309841SJie Liu return status; 144842035306SJoel Becker 144949309841SJie Liu out_rollback_bg: 145049309841SJie Liu bg->bg_next_group = cpu_to_le64(bg_ptr); 145149309841SJie Liu out_rollback_prev_bg: 145249309841SJie Liu prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr); 145325e28921SAndrew Morton goto out; 1454ccd979bdSMark Fasheh } 1455ccd979bdSMark Fasheh 1456ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg, 1457ccd979bdSMark Fasheh u32 wanted) 1458ccd979bdSMark Fasheh { 1459ccd979bdSMark Fasheh return le16_to_cpu(bg->bg_free_bits_count) > wanted; 1460ccd979bdSMark Fasheh } 1461ccd979bdSMark Fasheh 1462ccd979bdSMark Fasheh /* return 0 on success, -ENOSPC to keep searching and any other < 0 1463ccd979bdSMark Fasheh * value on error. */ 1464ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode, 1465ccd979bdSMark Fasheh struct buffer_head *group_bh, 1466ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 14671187c968SJoel Becker u64 max_block, 14687d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1469ccd979bdSMark Fasheh { 1470ccd979bdSMark Fasheh int search = -ENOSPC; 1471ccd979bdSMark Fasheh int ret; 14721187c968SJoel Becker u64 blkoff; 14737bf72edeSMark Fasheh struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data; 14749c7af40bSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 14757bf72edeSMark Fasheh unsigned int max_bits, gd_cluster_off; 1476ccd979bdSMark Fasheh 1477ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 1478ccd979bdSMark Fasheh 14797bf72edeSMark Fasheh if (gd->bg_free_bits_count) { 14807bf72edeSMark Fasheh max_bits = le16_to_cpu(gd->bg_bits); 14817bf72edeSMark Fasheh 14827bf72edeSMark Fasheh /* Tail groups in cluster bitmaps which aren't cpg 148325985edcSLucas De Marchi * aligned are prone to partial extension by a failed 14847bf72edeSMark Fasheh * fs resize. If the file system resize never got to 14857bf72edeSMark Fasheh * update the dinode cluster count, then we don't want 14867bf72edeSMark Fasheh * to trust any clusters past it, regardless of what 14877bf72edeSMark Fasheh * the group descriptor says. */ 14887bf72edeSMark Fasheh gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb, 14897bf72edeSMark Fasheh le64_to_cpu(gd->bg_blkno)); 14907bf72edeSMark Fasheh if ((gd_cluster_off + max_bits) > 14917bf72edeSMark Fasheh OCFS2_I(inode)->ip_clusters) { 14927bf72edeSMark Fasheh max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off; 14932f73e135STao Ma trace_ocfs2_cluster_group_search_wrong_max_bits( 14947bf72edeSMark Fasheh (unsigned long long)le64_to_cpu(gd->bg_blkno), 14957bf72edeSMark Fasheh le16_to_cpu(gd->bg_bits), 14967bf72edeSMark Fasheh OCFS2_I(inode)->ip_clusters, max_bits); 14977bf72edeSMark Fasheh } 14987bf72edeSMark Fasheh 1499ccd979bdSMark Fasheh ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb), 1500ccd979bdSMark Fasheh group_bh, bits_wanted, 15017d1fe093SJoel Becker max_bits, res); 1502ccd979bdSMark Fasheh if (ret) 1503ccd979bdSMark Fasheh return ret; 1504ccd979bdSMark Fasheh 15051187c968SJoel Becker if (max_block) { 15061187c968SJoel Becker blkoff = ocfs2_clusters_to_blocks(inode->i_sb, 15071187c968SJoel Becker gd_cluster_off + 15087d1fe093SJoel Becker res->sr_bit_offset + 15097d1fe093SJoel Becker res->sr_bits); 15102f73e135STao Ma trace_ocfs2_cluster_group_search_max_block( 15111187c968SJoel Becker (unsigned long long)blkoff, 15121187c968SJoel Becker (unsigned long long)max_block); 15131187c968SJoel Becker if (blkoff > max_block) 15141187c968SJoel Becker return -ENOSPC; 15151187c968SJoel Becker } 15161187c968SJoel Becker 1517ccd979bdSMark Fasheh /* ocfs2_block_group_find_clear_bits() might 1518ccd979bdSMark Fasheh * return success, but we still want to return 1519ccd979bdSMark Fasheh * -ENOSPC unless it found the minimum number 1520ccd979bdSMark Fasheh * of bits. */ 15217d1fe093SJoel Becker if (min_bits <= res->sr_bits) 1522ccd979bdSMark Fasheh search = 0; /* success */ 15237d1fe093SJoel Becker else if (res->sr_bits) { 15249c7af40bSMark Fasheh /* 15259c7af40bSMark Fasheh * Don't show bits which we'll be returning 15269c7af40bSMark Fasheh * for allocation to the local alloc bitmap. 15279c7af40bSMark Fasheh */ 15287d1fe093SJoel Becker ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits); 1529ccd979bdSMark Fasheh } 1530ccd979bdSMark Fasheh } 1531ccd979bdSMark Fasheh 1532ccd979bdSMark Fasheh return search; 1533ccd979bdSMark Fasheh } 1534ccd979bdSMark Fasheh 1535ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode, 1536ccd979bdSMark Fasheh struct buffer_head *group_bh, 1537ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 15381187c968SJoel Becker u64 max_block, 15397d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1540ccd979bdSMark Fasheh { 1541ccd979bdSMark Fasheh int ret = -ENOSPC; 15421187c968SJoel Becker u64 blkoff; 1543ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data; 1544ccd979bdSMark Fasheh 1545ccd979bdSMark Fasheh BUG_ON(min_bits != 1); 1546ccd979bdSMark Fasheh BUG_ON(ocfs2_is_cluster_bitmap(inode)); 1547ccd979bdSMark Fasheh 15481187c968SJoel Becker if (bg->bg_free_bits_count) { 1549ccd979bdSMark Fasheh ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb), 1550ccd979bdSMark Fasheh group_bh, bits_wanted, 15517bf72edeSMark Fasheh le16_to_cpu(bg->bg_bits), 15527d1fe093SJoel Becker res); 15531187c968SJoel Becker if (!ret && max_block) { 15547d1fe093SJoel Becker blkoff = le64_to_cpu(bg->bg_blkno) + 15557d1fe093SJoel Becker res->sr_bit_offset + res->sr_bits; 15562f73e135STao Ma trace_ocfs2_block_group_search_max_block( 15571187c968SJoel Becker (unsigned long long)blkoff, 15581187c968SJoel Becker (unsigned long long)max_block); 15591187c968SJoel Becker if (blkoff > max_block) 15601187c968SJoel Becker ret = -ENOSPC; 15611187c968SJoel Becker } 15621187c968SJoel Becker } 1563ccd979bdSMark Fasheh 1564ccd979bdSMark Fasheh return ret; 1565ccd979bdSMark Fasheh } 1566ccd979bdSMark Fasheh 15670a2fcd89SYounger Liu int ocfs2_alloc_dinode_update_counts(struct inode *inode, 15681fabe148SMark Fasheh handle_t *handle, 1569883d4caeSMark Fasheh struct buffer_head *di_bh, 1570883d4caeSMark Fasheh u32 num_bits, 1571883d4caeSMark Fasheh u16 chain) 1572883d4caeSMark Fasheh { 1573883d4caeSMark Fasheh int ret; 1574883d4caeSMark Fasheh u32 tmp_used; 1575883d4caeSMark Fasheh struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; 1576883d4caeSMark Fasheh struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain; 1577883d4caeSMark Fasheh 15780cf2f763SJoel Becker ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 1579883d4caeSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 1580883d4caeSMark Fasheh if (ret < 0) { 1581883d4caeSMark Fasheh mlog_errno(ret); 1582883d4caeSMark Fasheh goto out; 1583883d4caeSMark Fasheh } 1584883d4caeSMark Fasheh 1585883d4caeSMark Fasheh tmp_used = le32_to_cpu(di->id1.bitmap1.i_used); 1586883d4caeSMark Fasheh di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used); 1587883d4caeSMark Fasheh le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits); 1588ec20cec7SJoel Becker ocfs2_journal_dirty(handle, di_bh); 1589883d4caeSMark Fasheh 1590883d4caeSMark Fasheh out: 1591883d4caeSMark Fasheh return ret; 1592883d4caeSMark Fasheh } 1593883d4caeSMark Fasheh 1594db66c715SYounger Liu void ocfs2_rollback_alloc_dinode_counts(struct inode *inode, 1595db66c715SYounger Liu struct buffer_head *di_bh, 1596db66c715SYounger Liu u32 num_bits, 1597db66c715SYounger Liu u16 chain) 1598db66c715SYounger Liu { 1599db66c715SYounger Liu u32 tmp_used; 1600db66c715SYounger Liu struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; 1601db66c715SYounger Liu struct ocfs2_chain_list *cl; 1602db66c715SYounger Liu 1603db66c715SYounger Liu cl = (struct ocfs2_chain_list *)&di->id2.i_chain; 1604db66c715SYounger Liu tmp_used = le32_to_cpu(di->id1.bitmap1.i_used); 1605db66c715SYounger Liu di->id1.bitmap1.i_used = cpu_to_le32(tmp_used - num_bits); 1606db66c715SYounger Liu le32_add_cpu(&cl->cl_recs[chain].c_free, num_bits); 1607db66c715SYounger Liu } 1608db66c715SYounger Liu 1609ba206635SJoel Becker static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res, 161013e434cfSJoel Becker struct ocfs2_extent_rec *rec, 161113e434cfSJoel Becker struct ocfs2_chain_list *cl) 161213e434cfSJoel Becker { 161313e434cfSJoel Becker unsigned int bpc = le16_to_cpu(cl->cl_bpc); 161413e434cfSJoel Becker unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc; 161572094e43SAl Viro unsigned int bitcount = le16_to_cpu(rec->e_leaf_clusters) * bpc; 161613e434cfSJoel Becker 161713e434cfSJoel Becker if (res->sr_bit_offset < bitoff) 161813e434cfSJoel Becker return 0; 161913e434cfSJoel Becker if (res->sr_bit_offset >= (bitoff + bitcount)) 162013e434cfSJoel Becker return 0; 1621ba206635SJoel Becker res->sr_blkno = le64_to_cpu(rec->e_blkno) + 1622ba206635SJoel Becker (res->sr_bit_offset - bitoff); 162313e434cfSJoel Becker if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount)) 162413e434cfSJoel Becker res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset; 162513e434cfSJoel Becker return 1; 162613e434cfSJoel Becker } 162713e434cfSJoel Becker 1628ba206635SJoel Becker static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac, 162913e434cfSJoel Becker struct ocfs2_group_desc *bg, 163013e434cfSJoel Becker struct ocfs2_suballoc_result *res) 163113e434cfSJoel Becker { 163213e434cfSJoel Becker int i; 16332b6cb576SJoel Becker u64 bg_blkno = res->sr_bg_blkno; /* Save off */ 163413e434cfSJoel Becker struct ocfs2_extent_rec *rec; 163513e434cfSJoel Becker struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data; 163613e434cfSJoel Becker struct ocfs2_chain_list *cl = &di->id2.i_chain; 163713e434cfSJoel Becker 1638ba206635SJoel Becker if (ocfs2_is_cluster_bitmap(ac->ac_inode)) { 1639ba206635SJoel Becker res->sr_blkno = 0; 164013e434cfSJoel Becker return; 1641ba206635SJoel Becker } 164213e434cfSJoel Becker 1643ba206635SJoel Becker res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset; 16442b6cb576SJoel Becker res->sr_bg_blkno = 0; /* Clear it for contig block groups */ 16454711954eSTao Ma if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) || 1646ba206635SJoel Becker !bg->bg_list.l_next_free_rec) 164713e434cfSJoel Becker return; 164813e434cfSJoel Becker 164913e434cfSJoel Becker for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) { 165013e434cfSJoel Becker rec = &bg->bg_list.l_recs[i]; 16512b6cb576SJoel Becker if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) { 16522b6cb576SJoel Becker res->sr_bg_blkno = bg_blkno; /* Restore */ 165313e434cfSJoel Becker break; 165413e434cfSJoel Becker } 165513e434cfSJoel Becker } 16562b6cb576SJoel Becker } 165713e434cfSJoel Becker 1658883d4caeSMark Fasheh static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac, 16591fabe148SMark Fasheh handle_t *handle, 1660883d4caeSMark Fasheh u32 bits_wanted, 1661883d4caeSMark Fasheh u32 min_bits, 16627d1fe093SJoel Becker struct ocfs2_suballoc_result *res, 1663883d4caeSMark Fasheh u16 *bits_left) 1664883d4caeSMark Fasheh { 1665883d4caeSMark Fasheh int ret; 1666883d4caeSMark Fasheh struct buffer_head *group_bh = NULL; 1667883d4caeSMark Fasheh struct ocfs2_group_desc *gd; 166868f64d47SJoel Becker struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data; 1669883d4caeSMark Fasheh struct inode *alloc_inode = ac->ac_inode; 1670883d4caeSMark Fasheh 16717d1fe093SJoel Becker ret = ocfs2_read_group_descriptor(alloc_inode, di, 16727d1fe093SJoel Becker res->sr_bg_blkno, &group_bh); 1673883d4caeSMark Fasheh if (ret < 0) { 1674883d4caeSMark Fasheh mlog_errno(ret); 1675883d4caeSMark Fasheh return ret; 1676883d4caeSMark Fasheh } 1677883d4caeSMark Fasheh 1678883d4caeSMark Fasheh gd = (struct ocfs2_group_desc *) group_bh->b_data; 1679883d4caeSMark Fasheh ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits, 16807d1fe093SJoel Becker ac->ac_max_block, res); 1681883d4caeSMark Fasheh if (ret < 0) { 1682883d4caeSMark Fasheh if (ret != -ENOSPC) 1683883d4caeSMark Fasheh mlog_errno(ret); 1684883d4caeSMark Fasheh goto out; 1685883d4caeSMark Fasheh } 1686883d4caeSMark Fasheh 168713e434cfSJoel Becker if (!ret) 1688ba206635SJoel Becker ocfs2_bg_discontig_fix_result(ac, gd, res); 168913e434cfSJoel Becker 1690e49e2767SMark Fasheh /* 1691e49e2767SMark Fasheh * sr_bg_blkno might have been changed by 1692e49e2767SMark Fasheh * ocfs2_bg_discontig_fix_result 1693e49e2767SMark Fasheh */ 1694e49e2767SMark Fasheh res->sr_bg_stable_blkno = group_bh->b_blocknr; 1695e49e2767SMark Fasheh 1696e49e2767SMark Fasheh if (ac->ac_find_loc_only) 1697e49e2767SMark Fasheh goto out_loc_only; 1698e49e2767SMark Fasheh 1699883d4caeSMark Fasheh ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh, 17007d1fe093SJoel Becker res->sr_bits, 1701883d4caeSMark Fasheh le16_to_cpu(gd->bg_chain)); 1702883d4caeSMark Fasheh if (ret < 0) { 1703883d4caeSMark Fasheh mlog_errno(ret); 1704883d4caeSMark Fasheh goto out; 1705883d4caeSMark Fasheh } 1706883d4caeSMark Fasheh 1707883d4caeSMark Fasheh ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh, 17087d1fe093SJoel Becker res->sr_bit_offset, res->sr_bits); 1709db66c715SYounger Liu if (ret < 0) { 1710db66c715SYounger Liu ocfs2_rollback_alloc_dinode_counts(alloc_inode, ac->ac_bh, 1711db66c715SYounger Liu res->sr_bits, 1712db66c715SYounger Liu le16_to_cpu(gd->bg_chain)); 1713883d4caeSMark Fasheh mlog_errno(ret); 1714db66c715SYounger Liu } 1715883d4caeSMark Fasheh 1716e49e2767SMark Fasheh out_loc_only: 1717883d4caeSMark Fasheh *bits_left = le16_to_cpu(gd->bg_free_bits_count); 1718883d4caeSMark Fasheh 1719883d4caeSMark Fasheh out: 1720883d4caeSMark Fasheh brelse(group_bh); 1721883d4caeSMark Fasheh 1722883d4caeSMark Fasheh return ret; 1723883d4caeSMark Fasheh } 1724883d4caeSMark Fasheh 1725ccd979bdSMark Fasheh static int ocfs2_search_chain(struct ocfs2_alloc_context *ac, 17261fabe148SMark Fasheh handle_t *handle, 1727ccd979bdSMark Fasheh u32 bits_wanted, 1728ccd979bdSMark Fasheh u32 min_bits, 17297d1fe093SJoel Becker struct ocfs2_suballoc_result *res, 1730883d4caeSMark Fasheh u16 *bits_left) 1731ccd979bdSMark Fasheh { 1732ccd979bdSMark Fasheh int status; 17337d1fe093SJoel Becker u16 chain; 1734ccd979bdSMark Fasheh u64 next_group; 1735ccd979bdSMark Fasheh struct inode *alloc_inode = ac->ac_inode; 1736ccd979bdSMark Fasheh struct buffer_head *group_bh = NULL; 1737ccd979bdSMark Fasheh struct buffer_head *prev_group_bh = NULL; 1738ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data; 1739ccd979bdSMark Fasheh struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain; 1740ccd979bdSMark Fasheh struct ocfs2_group_desc *bg; 1741ccd979bdSMark Fasheh 1742ccd979bdSMark Fasheh chain = ac->ac_chain; 17432f73e135STao Ma trace_ocfs2_search_chain_begin( 17442f73e135STao Ma (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, 17452f73e135STao Ma bits_wanted, chain); 1746ccd979bdSMark Fasheh 174768f64d47SJoel Becker status = ocfs2_read_group_descriptor(alloc_inode, fe, 1748ccd979bdSMark Fasheh le64_to_cpu(cl->cl_recs[chain].c_blkno), 17490fcaa56aSJoel Becker &group_bh); 1750ccd979bdSMark Fasheh if (status < 0) { 1751ccd979bdSMark Fasheh mlog_errno(status); 1752ccd979bdSMark Fasheh goto bail; 1753ccd979bdSMark Fasheh } 1754ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) group_bh->b_data; 1755ccd979bdSMark Fasheh 1756ccd979bdSMark Fasheh status = -ENOSPC; 1757ccd979bdSMark Fasheh /* for now, the chain search is a bit simplistic. We just use 1758ccd979bdSMark Fasheh * the 1st group with any empty bits. */ 17591187c968SJoel Becker while ((status = ac->ac_group_search(alloc_inode, group_bh, 17601187c968SJoel Becker bits_wanted, min_bits, 17617d1fe093SJoel Becker ac->ac_max_block, 17627d1fe093SJoel Becker res)) == -ENOSPC) { 1763ccd979bdSMark Fasheh if (!bg->bg_next_group) 1764ccd979bdSMark Fasheh break; 1765a81cb88bSMark Fasheh 1766ccd979bdSMark Fasheh brelse(prev_group_bh); 1767ccd979bdSMark Fasheh prev_group_bh = NULL; 1768a81cb88bSMark Fasheh 1769ccd979bdSMark Fasheh next_group = le64_to_cpu(bg->bg_next_group); 1770ccd979bdSMark Fasheh prev_group_bh = group_bh; 1771ccd979bdSMark Fasheh group_bh = NULL; 177268f64d47SJoel Becker status = ocfs2_read_group_descriptor(alloc_inode, fe, 17730fcaa56aSJoel Becker next_group, &group_bh); 1774ccd979bdSMark Fasheh if (status < 0) { 1775ccd979bdSMark Fasheh mlog_errno(status); 1776ccd979bdSMark Fasheh goto bail; 1777ccd979bdSMark Fasheh } 1778ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) group_bh->b_data; 1779ccd979bdSMark Fasheh } 1780ccd979bdSMark Fasheh if (status < 0) { 1781ccd979bdSMark Fasheh if (status != -ENOSPC) 1782ccd979bdSMark Fasheh mlog_errno(status); 1783ccd979bdSMark Fasheh goto bail; 1784ccd979bdSMark Fasheh } 1785ccd979bdSMark Fasheh 17862f73e135STao Ma trace_ocfs2_search_chain_succ( 17872f73e135STao Ma (unsigned long long)le64_to_cpu(bg->bg_blkno), res->sr_bits); 1788ccd979bdSMark Fasheh 17897d1fe093SJoel Becker res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno); 1790ccd979bdSMark Fasheh 17917d1fe093SJoel Becker BUG_ON(res->sr_bits == 0); 179213e434cfSJoel Becker if (!status) 1793ba206635SJoel Becker ocfs2_bg_discontig_fix_result(ac, bg, res); 179413e434cfSJoel Becker 1795e49e2767SMark Fasheh /* 1796e49e2767SMark Fasheh * sr_bg_blkno might have been changed by 1797e49e2767SMark Fasheh * ocfs2_bg_discontig_fix_result 1798e49e2767SMark Fasheh */ 1799e49e2767SMark Fasheh res->sr_bg_stable_blkno = group_bh->b_blocknr; 1800ccd979bdSMark Fasheh 1801ccd979bdSMark Fasheh /* 1802ccd979bdSMark Fasheh * Keep track of previous block descriptor read. When 1803ccd979bdSMark Fasheh * we find a target, if we have read more than X 1804ccd979bdSMark Fasheh * number of descriptors, and the target is reasonably 1805ccd979bdSMark Fasheh * empty, relink him to top of his chain. 1806ccd979bdSMark Fasheh * 1807ccd979bdSMark Fasheh * We've read 0 extra blocks and only send one more to 1808ccd979bdSMark Fasheh * the transaction, yet the next guy to search has a 1809ccd979bdSMark Fasheh * much easier time. 1810ccd979bdSMark Fasheh * 1811ccd979bdSMark Fasheh * Do this *after* figuring out how many bits we're taking out 1812ccd979bdSMark Fasheh * of our target group. 1813ccd979bdSMark Fasheh */ 1814309a85b6SXiaowei.Hu if (!ac->ac_disable_chain_relink && 1815ccd979bdSMark Fasheh (prev_group_bh) && 18167d1fe093SJoel Becker (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) { 1817ccd979bdSMark Fasheh status = ocfs2_relink_block_group(handle, alloc_inode, 1818ccd979bdSMark Fasheh ac->ac_bh, group_bh, 1819ccd979bdSMark Fasheh prev_group_bh, chain); 1820ccd979bdSMark Fasheh if (status < 0) { 1821ccd979bdSMark Fasheh mlog_errno(status); 1822ccd979bdSMark Fasheh goto bail; 1823ccd979bdSMark Fasheh } 1824ccd979bdSMark Fasheh } 1825ccd979bdSMark Fasheh 1826e49e2767SMark Fasheh if (ac->ac_find_loc_only) 1827e49e2767SMark Fasheh goto out_loc_only; 1828e49e2767SMark Fasheh 1829d5134982SMark Fasheh status = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, 1830d5134982SMark Fasheh ac->ac_bh, res->sr_bits, 1831d5134982SMark Fasheh chain); 1832d5134982SMark Fasheh if (status) { 1833ccd979bdSMark Fasheh mlog_errno(status); 1834ccd979bdSMark Fasheh goto bail; 1835ccd979bdSMark Fasheh } 1836ccd979bdSMark Fasheh 1837ccd979bdSMark Fasheh status = ocfs2_block_group_set_bits(handle, 1838ccd979bdSMark Fasheh alloc_inode, 1839ccd979bdSMark Fasheh bg, 1840ccd979bdSMark Fasheh group_bh, 18417d1fe093SJoel Becker res->sr_bit_offset, 18427d1fe093SJoel Becker res->sr_bits); 1843ccd979bdSMark Fasheh if (status < 0) { 1844db66c715SYounger Liu ocfs2_rollback_alloc_dinode_counts(alloc_inode, 1845db66c715SYounger Liu ac->ac_bh, res->sr_bits, chain); 1846ccd979bdSMark Fasheh mlog_errno(status); 1847ccd979bdSMark Fasheh goto bail; 1848ccd979bdSMark Fasheh } 1849ccd979bdSMark Fasheh 18502f73e135STao Ma trace_ocfs2_search_chain_end( 18512f73e135STao Ma (unsigned long long)le64_to_cpu(fe->i_blkno), 18522f73e135STao Ma res->sr_bits); 1853ccd979bdSMark Fasheh 1854e49e2767SMark Fasheh out_loc_only: 1855883d4caeSMark Fasheh *bits_left = le16_to_cpu(bg->bg_free_bits_count); 1856ccd979bdSMark Fasheh bail: 1857ccd979bdSMark Fasheh brelse(group_bh); 1858ccd979bdSMark Fasheh brelse(prev_group_bh); 1859ccd979bdSMark Fasheh 1860c1e8d35eSTao Ma if (status) 1861c1e8d35eSTao Ma mlog_errno(status); 1862ccd979bdSMark Fasheh return status; 1863ccd979bdSMark Fasheh } 1864ccd979bdSMark Fasheh 1865ccd979bdSMark Fasheh /* will give out up to bits_wanted contiguous bits. */ 1866aa8f8e93SJoel Becker static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac, 18671fabe148SMark Fasheh handle_t *handle, 1868ccd979bdSMark Fasheh u32 bits_wanted, 1869ccd979bdSMark Fasheh u32 min_bits, 18707d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1871ccd979bdSMark Fasheh { 1872ccd979bdSMark Fasheh int status; 1873ccd979bdSMark Fasheh u16 victim, i; 1874883d4caeSMark Fasheh u16 bits_left = 0; 1875b2b6ebf5SMark Fasheh u64 hint = ac->ac_last_group; 1876ccd979bdSMark Fasheh struct ocfs2_chain_list *cl; 1877ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 1878ccd979bdSMark Fasheh 1879ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted); 1880ccd979bdSMark Fasheh BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given)); 1881ccd979bdSMark Fasheh BUG_ON(!ac->ac_bh); 1882ccd979bdSMark Fasheh 1883ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) ac->ac_bh->b_data; 188410995aa2SJoel Becker 188510995aa2SJoel Becker /* The bh was validated by the inode read during 188610995aa2SJoel Becker * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */ 188710995aa2SJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); 188810995aa2SJoel Becker 1889ccd979bdSMark Fasheh if (le32_to_cpu(fe->id1.bitmap1.i_used) >= 1890ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_total)) { 189117a5b9abSGoldwyn Rodrigues status = ocfs2_error(ac->ac_inode->i_sb, 18927ecef14aSJoe Perches "Chain allocator dinode %llu has %u used bits but only %u total\n", 1893b0697053SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno), 1894ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_used), 1895ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_total)); 1896ccd979bdSMark Fasheh goto bail; 1897ccd979bdSMark Fasheh } 1898ccd979bdSMark Fasheh 1899b2b6ebf5SMark Fasheh res->sr_bg_blkno = hint; 19007d1fe093SJoel Becker if (res->sr_bg_blkno) { 1901883d4caeSMark Fasheh /* Attempt to short-circuit the usual search mechanism 1902883d4caeSMark Fasheh * by jumping straight to the most recently used 1903b595076aSUwe Kleine-König * allocation group. This helps us maintain some 1904883d4caeSMark Fasheh * contiguousness across allocations. */ 1905da5cbf2fSMark Fasheh status = ocfs2_search_one_group(ac, handle, bits_wanted, 19067d1fe093SJoel Becker min_bits, res, &bits_left); 19077d1fe093SJoel Becker if (!status) 1908883d4caeSMark Fasheh goto set_hint; 1909883d4caeSMark Fasheh if (status < 0 && status != -ENOSPC) { 1910883d4caeSMark Fasheh mlog_errno(status); 1911883d4caeSMark Fasheh goto bail; 1912883d4caeSMark Fasheh } 1913883d4caeSMark Fasheh } 1914883d4caeSMark Fasheh 1915ccd979bdSMark Fasheh cl = (struct ocfs2_chain_list *) &fe->id2.i_chain; 1916ccd979bdSMark Fasheh 1917ccd979bdSMark Fasheh victim = ocfs2_find_victim_chain(cl); 1918ccd979bdSMark Fasheh ac->ac_chain = victim; 1919ccd979bdSMark Fasheh 19207d1fe093SJoel Becker status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, 19217d1fe093SJoel Becker res, &bits_left); 1922b2b6ebf5SMark Fasheh if (!status) { 1923*1d1aff8cSJoseph Qi if (ocfs2_is_cluster_bitmap(ac->ac_inode)) 1924*1d1aff8cSJoseph Qi hint = res->sr_bg_blkno; 1925*1d1aff8cSJoseph Qi else 1926b2b6ebf5SMark Fasheh hint = ocfs2_group_from_res(res); 1927883d4caeSMark Fasheh goto set_hint; 1928b2b6ebf5SMark Fasheh } 1929ccd979bdSMark Fasheh if (status < 0 && status != -ENOSPC) { 1930ccd979bdSMark Fasheh mlog_errno(status); 1931ccd979bdSMark Fasheh goto bail; 1932ccd979bdSMark Fasheh } 1933ccd979bdSMark Fasheh 19342f73e135STao Ma trace_ocfs2_claim_suballoc_bits(victim); 1935ccd979bdSMark Fasheh 1936ccd979bdSMark Fasheh /* If we didn't pick a good victim, then just default to 1937ccd979bdSMark Fasheh * searching each chain in order. Don't allow chain relinking 1938ccd979bdSMark Fasheh * because we only calculate enough journal credits for one 1939ccd979bdSMark Fasheh * relink per alloc. */ 1940309a85b6SXiaowei.Hu ac->ac_disable_chain_relink = 1; 1941ccd979bdSMark Fasheh for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) { 1942ccd979bdSMark Fasheh if (i == victim) 1943ccd979bdSMark Fasheh continue; 1944ccd979bdSMark Fasheh if (!cl->cl_recs[i].c_free) 1945ccd979bdSMark Fasheh continue; 1946ccd979bdSMark Fasheh 1947ccd979bdSMark Fasheh ac->ac_chain = i; 1948da5cbf2fSMark Fasheh status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, 19497d1fe093SJoel Becker res, &bits_left); 1950b2b6ebf5SMark Fasheh if (!status) { 1951b2b6ebf5SMark Fasheh hint = ocfs2_group_from_res(res); 1952ccd979bdSMark Fasheh break; 1953b2b6ebf5SMark Fasheh } 1954ccd979bdSMark Fasheh if (status < 0 && status != -ENOSPC) { 1955ccd979bdSMark Fasheh mlog_errno(status); 1956ccd979bdSMark Fasheh goto bail; 1957ccd979bdSMark Fasheh } 1958ccd979bdSMark Fasheh } 1959ccd979bdSMark Fasheh 1960883d4caeSMark Fasheh set_hint: 1961883d4caeSMark Fasheh if (status != -ENOSPC) { 1962883d4caeSMark Fasheh /* If the next search of this group is not likely to 1963883d4caeSMark Fasheh * yield a suitable extent, then we reset the last 1964883d4caeSMark Fasheh * group hint so as to not waste a disk read */ 1965883d4caeSMark Fasheh if (bits_left < min_bits) 1966883d4caeSMark Fasheh ac->ac_last_group = 0; 1967883d4caeSMark Fasheh else 1968b2b6ebf5SMark Fasheh ac->ac_last_group = hint; 1969883d4caeSMark Fasheh } 1970883d4caeSMark Fasheh 1971883d4caeSMark Fasheh bail: 1972c1e8d35eSTao Ma if (status) 1973c1e8d35eSTao Ma mlog_errno(status); 1974ccd979bdSMark Fasheh return status; 1975ccd979bdSMark Fasheh } 1976ccd979bdSMark Fasheh 19771ed9b777SJoel Becker int ocfs2_claim_metadata(handle_t *handle, 1978ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac, 1979ccd979bdSMark Fasheh u32 bits_wanted, 19802b6cb576SJoel Becker u64 *suballoc_loc, 1981ccd979bdSMark Fasheh u16 *suballoc_bit_start, 1982ccd979bdSMark Fasheh unsigned int *num_bits, 1983ccd979bdSMark Fasheh u64 *blkno_start) 1984ccd979bdSMark Fasheh { 1985ccd979bdSMark Fasheh int status; 1986ba206635SJoel Becker struct ocfs2_suballoc_result res = { .sr_blkno = 0, }; 1987ccd979bdSMark Fasheh 1988ccd979bdSMark Fasheh BUG_ON(!ac); 1989ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted)); 1990ccd979bdSMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_META); 1991ccd979bdSMark Fasheh 1992aa8f8e93SJoel Becker status = ocfs2_claim_suballoc_bits(ac, 1993da5cbf2fSMark Fasheh handle, 1994ccd979bdSMark Fasheh bits_wanted, 1995ccd979bdSMark Fasheh 1, 19967d1fe093SJoel Becker &res); 1997ccd979bdSMark Fasheh if (status < 0) { 1998ccd979bdSMark Fasheh mlog_errno(status); 1999ccd979bdSMark Fasheh goto bail; 2000ccd979bdSMark Fasheh } 20011ed9b777SJoel Becker atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs); 2002ccd979bdSMark Fasheh 20032b6cb576SJoel Becker *suballoc_loc = res.sr_bg_blkno; 20047d1fe093SJoel Becker *suballoc_bit_start = res.sr_bit_offset; 2005ba206635SJoel Becker *blkno_start = res.sr_blkno; 20067d1fe093SJoel Becker ac->ac_bits_given += res.sr_bits; 20077d1fe093SJoel Becker *num_bits = res.sr_bits; 2008ccd979bdSMark Fasheh status = 0; 2009ccd979bdSMark Fasheh bail: 2010c1e8d35eSTao Ma if (status) 2011c1e8d35eSTao Ma mlog_errno(status); 2012ccd979bdSMark Fasheh return status; 2013ccd979bdSMark Fasheh } 2014ccd979bdSMark Fasheh 201513821151STao Ma static void ocfs2_init_inode_ac_group(struct inode *dir, 2016abf1b3cbSTao Ma struct buffer_head *parent_di_bh, 201713821151STao Ma struct ocfs2_alloc_context *ac) 201813821151STao Ma { 2019abf1b3cbSTao Ma struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data; 202013821151STao Ma /* 202113821151STao Ma * Try to allocate inodes from some specific group. 202213821151STao Ma * 202313821151STao Ma * If the parent dir has recorded the last group used in allocation, 202413821151STao Ma * cool, use it. Otherwise if we try to allocate new inode from the 202513821151STao Ma * same slot the parent dir belongs to, use the same chunk. 202613821151STao Ma * 202713821151STao Ma * We are very careful here to avoid the mistake of setting 202813821151STao Ma * ac_last_group to a group descriptor from a different (unlocked) slot. 202913821151STao Ma */ 203013821151STao Ma if (OCFS2_I(dir)->ip_last_used_group && 203113821151STao Ma OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot) 203213821151STao Ma ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group; 2033abf1b3cbSTao Ma else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) { 2034abf1b3cbSTao Ma if (di->i_suballoc_loc) 2035abf1b3cbSTao Ma ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc); 2036abf1b3cbSTao Ma else 203713821151STao Ma ac->ac_last_group = ocfs2_which_suballoc_group( 2038abf1b3cbSTao Ma le64_to_cpu(di->i_blkno), 2039abf1b3cbSTao Ma le16_to_cpu(di->i_suballoc_bit)); 2040abf1b3cbSTao Ma } 204113821151STao Ma } 204213821151STao Ma 204313821151STao Ma static inline void ocfs2_save_inode_ac_group(struct inode *dir, 204413821151STao Ma struct ocfs2_alloc_context *ac) 204513821151STao Ma { 204613821151STao Ma OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group; 204713821151STao Ma OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot; 204813821151STao Ma } 204913821151STao Ma 2050e49e2767SMark Fasheh int ocfs2_find_new_inode_loc(struct inode *dir, 2051e49e2767SMark Fasheh struct buffer_head *parent_fe_bh, 2052e49e2767SMark Fasheh struct ocfs2_alloc_context *ac, 2053e49e2767SMark Fasheh u64 *fe_blkno) 2054e49e2767SMark Fasheh { 2055e49e2767SMark Fasheh int ret; 2056e49e2767SMark Fasheh handle_t *handle = NULL; 2057e49e2767SMark Fasheh struct ocfs2_suballoc_result *res; 2058e49e2767SMark Fasheh 2059e49e2767SMark Fasheh BUG_ON(!ac); 2060e49e2767SMark Fasheh BUG_ON(ac->ac_bits_given != 0); 2061e49e2767SMark Fasheh BUG_ON(ac->ac_bits_wanted != 1); 2062e49e2767SMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE); 2063e49e2767SMark Fasheh 2064e49e2767SMark Fasheh res = kzalloc(sizeof(*res), GFP_NOFS); 2065e49e2767SMark Fasheh if (res == NULL) { 2066e49e2767SMark Fasheh ret = -ENOMEM; 2067e49e2767SMark Fasheh mlog_errno(ret); 2068e49e2767SMark Fasheh goto out; 2069e49e2767SMark Fasheh } 2070e49e2767SMark Fasheh 2071e49e2767SMark Fasheh ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac); 2072e49e2767SMark Fasheh 2073e49e2767SMark Fasheh /* 2074e49e2767SMark Fasheh * The handle started here is for chain relink. Alternatively, 2075e49e2767SMark Fasheh * we could just disable relink for these calls. 2076e49e2767SMark Fasheh */ 2077e49e2767SMark Fasheh handle = ocfs2_start_trans(OCFS2_SB(dir->i_sb), OCFS2_SUBALLOC_ALLOC); 2078e49e2767SMark Fasheh if (IS_ERR(handle)) { 2079e49e2767SMark Fasheh ret = PTR_ERR(handle); 2080e49e2767SMark Fasheh handle = NULL; 2081e49e2767SMark Fasheh mlog_errno(ret); 2082e49e2767SMark Fasheh goto out; 2083e49e2767SMark Fasheh } 2084e49e2767SMark Fasheh 2085e49e2767SMark Fasheh /* 2086e49e2767SMark Fasheh * This will instruct ocfs2_claim_suballoc_bits and 2087e49e2767SMark Fasheh * ocfs2_search_one_group to search but save actual allocation 2088e49e2767SMark Fasheh * for later. 2089e49e2767SMark Fasheh */ 2090e49e2767SMark Fasheh ac->ac_find_loc_only = 1; 2091e49e2767SMark Fasheh 2092e49e2767SMark Fasheh ret = ocfs2_claim_suballoc_bits(ac, handle, 1, 1, res); 2093e49e2767SMark Fasheh if (ret < 0) { 2094e49e2767SMark Fasheh mlog_errno(ret); 2095e49e2767SMark Fasheh goto out; 2096e49e2767SMark Fasheh } 2097e49e2767SMark Fasheh 2098e49e2767SMark Fasheh ac->ac_find_loc_priv = res; 2099e49e2767SMark Fasheh *fe_blkno = res->sr_blkno; 21006fdb702dSDarrick J. Wong ocfs2_update_inode_fsync_trans(handle, dir, 0); 2101e49e2767SMark Fasheh out: 2102e49e2767SMark Fasheh if (handle) 2103e49e2767SMark Fasheh ocfs2_commit_trans(OCFS2_SB(dir->i_sb), handle); 2104e49e2767SMark Fasheh 2105e49e2767SMark Fasheh if (ret) 2106e49e2767SMark Fasheh kfree(res); 2107e49e2767SMark Fasheh 2108e49e2767SMark Fasheh return ret; 2109e49e2767SMark Fasheh } 2110e49e2767SMark Fasheh 2111e49e2767SMark Fasheh int ocfs2_claim_new_inode_at_loc(handle_t *handle, 2112e49e2767SMark Fasheh struct inode *dir, 2113e49e2767SMark Fasheh struct ocfs2_alloc_context *ac, 2114e49e2767SMark Fasheh u64 *suballoc_loc, 2115e49e2767SMark Fasheh u16 *suballoc_bit, 2116e49e2767SMark Fasheh u64 di_blkno) 2117e49e2767SMark Fasheh { 2118e49e2767SMark Fasheh int ret; 2119e49e2767SMark Fasheh u16 chain; 2120e49e2767SMark Fasheh struct ocfs2_suballoc_result *res = ac->ac_find_loc_priv; 2121e49e2767SMark Fasheh struct buffer_head *bg_bh = NULL; 2122e49e2767SMark Fasheh struct ocfs2_group_desc *bg; 2123e49e2767SMark Fasheh struct ocfs2_dinode *di = (struct ocfs2_dinode *) ac->ac_bh->b_data; 2124e49e2767SMark Fasheh 2125e49e2767SMark Fasheh /* 2126e49e2767SMark Fasheh * Since di_blkno is being passed back in, we check for any 2127e49e2767SMark Fasheh * inconsistencies which may have happened between 2128e49e2767SMark Fasheh * calls. These are code bugs as di_blkno is not expected to 2129e49e2767SMark Fasheh * change once returned from ocfs2_find_new_inode_loc() 2130e49e2767SMark Fasheh */ 2131e49e2767SMark Fasheh BUG_ON(res->sr_blkno != di_blkno); 2132e49e2767SMark Fasheh 2133e49e2767SMark Fasheh ret = ocfs2_read_group_descriptor(ac->ac_inode, di, 2134e49e2767SMark Fasheh res->sr_bg_stable_blkno, &bg_bh); 2135e49e2767SMark Fasheh if (ret) { 2136e49e2767SMark Fasheh mlog_errno(ret); 2137e49e2767SMark Fasheh goto out; 2138e49e2767SMark Fasheh } 2139e49e2767SMark Fasheh 2140e49e2767SMark Fasheh bg = (struct ocfs2_group_desc *) bg_bh->b_data; 2141e49e2767SMark Fasheh chain = le16_to_cpu(bg->bg_chain); 2142e49e2767SMark Fasheh 2143e49e2767SMark Fasheh ret = ocfs2_alloc_dinode_update_counts(ac->ac_inode, handle, 2144e49e2767SMark Fasheh ac->ac_bh, res->sr_bits, 2145e49e2767SMark Fasheh chain); 2146e49e2767SMark Fasheh if (ret) { 2147e49e2767SMark Fasheh mlog_errno(ret); 2148e49e2767SMark Fasheh goto out; 2149e49e2767SMark Fasheh } 2150e49e2767SMark Fasheh 2151e49e2767SMark Fasheh ret = ocfs2_block_group_set_bits(handle, 2152e49e2767SMark Fasheh ac->ac_inode, 2153e49e2767SMark Fasheh bg, 2154e49e2767SMark Fasheh bg_bh, 2155e49e2767SMark Fasheh res->sr_bit_offset, 2156e49e2767SMark Fasheh res->sr_bits); 2157e49e2767SMark Fasheh if (ret < 0) { 2158db66c715SYounger Liu ocfs2_rollback_alloc_dinode_counts(ac->ac_inode, 2159db66c715SYounger Liu ac->ac_bh, res->sr_bits, chain); 2160e49e2767SMark Fasheh mlog_errno(ret); 2161e49e2767SMark Fasheh goto out; 2162e49e2767SMark Fasheh } 2163e49e2767SMark Fasheh 21642f73e135STao Ma trace_ocfs2_claim_new_inode_at_loc((unsigned long long)di_blkno, 21652f73e135STao Ma res->sr_bits); 2166e49e2767SMark Fasheh 2167e49e2767SMark Fasheh atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs); 2168e49e2767SMark Fasheh 2169e49e2767SMark Fasheh BUG_ON(res->sr_bits != 1); 2170e49e2767SMark Fasheh 2171e49e2767SMark Fasheh *suballoc_loc = res->sr_bg_blkno; 2172e49e2767SMark Fasheh *suballoc_bit = res->sr_bit_offset; 2173e49e2767SMark Fasheh ac->ac_bits_given++; 2174e49e2767SMark Fasheh ocfs2_save_inode_ac_group(dir, ac); 2175e49e2767SMark Fasheh 2176e49e2767SMark Fasheh out: 2177e49e2767SMark Fasheh brelse(bg_bh); 2178e49e2767SMark Fasheh 2179e49e2767SMark Fasheh return ret; 2180e49e2767SMark Fasheh } 2181e49e2767SMark Fasheh 21821ed9b777SJoel Becker int ocfs2_claim_new_inode(handle_t *handle, 218313821151STao Ma struct inode *dir, 218413821151STao Ma struct buffer_head *parent_fe_bh, 2185ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac, 21862b6cb576SJoel Becker u64 *suballoc_loc, 2187ccd979bdSMark Fasheh u16 *suballoc_bit, 2188ccd979bdSMark Fasheh u64 *fe_blkno) 2189ccd979bdSMark Fasheh { 2190ccd979bdSMark Fasheh int status; 21912b6cb576SJoel Becker struct ocfs2_suballoc_result res; 2192ccd979bdSMark Fasheh 2193ccd979bdSMark Fasheh BUG_ON(!ac); 2194ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_given != 0); 2195ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_wanted != 1); 2196ccd979bdSMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE); 2197ccd979bdSMark Fasheh 219813821151STao Ma ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac); 219913821151STao Ma 2200aa8f8e93SJoel Becker status = ocfs2_claim_suballoc_bits(ac, 2201da5cbf2fSMark Fasheh handle, 2202ccd979bdSMark Fasheh 1, 2203ccd979bdSMark Fasheh 1, 22047d1fe093SJoel Becker &res); 2205ccd979bdSMark Fasheh if (status < 0) { 2206ccd979bdSMark Fasheh mlog_errno(status); 2207ccd979bdSMark Fasheh goto bail; 2208ccd979bdSMark Fasheh } 22091ed9b777SJoel Becker atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs); 2210ccd979bdSMark Fasheh 22117d1fe093SJoel Becker BUG_ON(res.sr_bits != 1); 2212ccd979bdSMark Fasheh 22132b6cb576SJoel Becker *suballoc_loc = res.sr_bg_blkno; 22147d1fe093SJoel Becker *suballoc_bit = res.sr_bit_offset; 2215ba206635SJoel Becker *fe_blkno = res.sr_blkno; 2216ccd979bdSMark Fasheh ac->ac_bits_given++; 221713821151STao Ma ocfs2_save_inode_ac_group(dir, ac); 2218ccd979bdSMark Fasheh status = 0; 2219ccd979bdSMark Fasheh bail: 2220c1e8d35eSTao Ma if (status) 2221c1e8d35eSTao Ma mlog_errno(status); 2222ccd979bdSMark Fasheh return status; 2223ccd979bdSMark Fasheh } 2224ccd979bdSMark Fasheh 2225ccd979bdSMark Fasheh /* translate a group desc. blkno and it's bitmap offset into 2226ccd979bdSMark Fasheh * disk cluster offset. */ 2227ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode, 2228ccd979bdSMark Fasheh u64 bg_blkno, 2229ccd979bdSMark Fasheh u16 bg_bit_off) 2230ccd979bdSMark Fasheh { 2231ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2232ccd979bdSMark Fasheh u32 cluster = 0; 2233ccd979bdSMark Fasheh 2234ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 2235ccd979bdSMark Fasheh 2236ccd979bdSMark Fasheh if (bg_blkno != osb->first_cluster_group_blkno) 2237ccd979bdSMark Fasheh cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno); 2238ccd979bdSMark Fasheh cluster += (u32) bg_bit_off; 2239ccd979bdSMark Fasheh return cluster; 2240ccd979bdSMark Fasheh } 2241ccd979bdSMark Fasheh 2242ccd979bdSMark Fasheh /* given a cluster offset, calculate which block group it belongs to 2243ccd979bdSMark Fasheh * and return that block offset. */ 2244d659072fSTao Ma u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster) 2245ccd979bdSMark Fasheh { 2246ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2247ccd979bdSMark Fasheh u32 group_no; 2248ccd979bdSMark Fasheh 2249ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 2250ccd979bdSMark Fasheh 2251ccd979bdSMark Fasheh group_no = cluster / osb->bitmap_cpg; 2252ccd979bdSMark Fasheh if (!group_no) 2253ccd979bdSMark Fasheh return osb->first_cluster_group_blkno; 2254ccd979bdSMark Fasheh return ocfs2_clusters_to_blocks(inode->i_sb, 2255ccd979bdSMark Fasheh group_no * osb->bitmap_cpg); 2256ccd979bdSMark Fasheh } 2257ccd979bdSMark Fasheh 2258ccd979bdSMark Fasheh /* given the block number of a cluster start, calculate which cluster 2259ccd979bdSMark Fasheh * group and descriptor bitmap offset that corresponds to. */ 2260ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode, 2261ccd979bdSMark Fasheh u64 data_blkno, 2262ccd979bdSMark Fasheh u64 *bg_blkno, 2263ccd979bdSMark Fasheh u16 *bg_bit_off) 2264ccd979bdSMark Fasheh { 2265ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2266ccd979bdSMark Fasheh u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno); 2267ccd979bdSMark Fasheh 2268ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 2269ccd979bdSMark Fasheh 2270ccd979bdSMark Fasheh *bg_blkno = ocfs2_which_cluster_group(inode, 2271ccd979bdSMark Fasheh data_cluster); 2272ccd979bdSMark Fasheh 2273ccd979bdSMark Fasheh if (*bg_blkno == osb->first_cluster_group_blkno) 2274ccd979bdSMark Fasheh *bg_bit_off = (u16) data_cluster; 2275ccd979bdSMark Fasheh else 2276ccd979bdSMark Fasheh *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb, 2277ccd979bdSMark Fasheh data_blkno - *bg_blkno); 2278ccd979bdSMark Fasheh } 2279ccd979bdSMark Fasheh 2280ccd979bdSMark Fasheh /* 2281ccd979bdSMark Fasheh * min_bits - minimum contiguous chunk from this total allocation we 2282ccd979bdSMark Fasheh * can handle. set to what we asked for originally for a full 2283ccd979bdSMark Fasheh * contig. allocation, set to '1' to indicate we can deal with extents 2284ccd979bdSMark Fasheh * of any size. 2285ccd979bdSMark Fasheh */ 22861ed9b777SJoel Becker int __ocfs2_claim_clusters(handle_t *handle, 2287ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac, 2288ccd979bdSMark Fasheh u32 min_clusters, 2289415cb800SMark Fasheh u32 max_clusters, 2290ccd979bdSMark Fasheh u32 *cluster_start, 2291ccd979bdSMark Fasheh u32 *num_clusters) 2292ccd979bdSMark Fasheh { 2293ccd979bdSMark Fasheh int status; 2294415cb800SMark Fasheh unsigned int bits_wanted = max_clusters; 2295ba206635SJoel Becker struct ocfs2_suballoc_result res = { .sr_blkno = 0, }; 22961ed9b777SJoel Becker struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb); 2297ccd979bdSMark Fasheh 2298ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted); 2299ccd979bdSMark Fasheh 2300ccd979bdSMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL 2301ccd979bdSMark Fasheh && ac->ac_which != OCFS2_AC_USE_MAIN); 2302ccd979bdSMark Fasheh 2303ccd979bdSMark Fasheh if (ac->ac_which == OCFS2_AC_USE_LOCAL) { 230433d5d380SMark Fasheh WARN_ON(min_clusters > 1); 230533d5d380SMark Fasheh 2306ccd979bdSMark Fasheh status = ocfs2_claim_local_alloc_bits(osb, 2307ccd979bdSMark Fasheh handle, 2308ccd979bdSMark Fasheh ac, 2309ccd979bdSMark Fasheh bits_wanted, 2310ccd979bdSMark Fasheh cluster_start, 2311ccd979bdSMark Fasheh num_clusters); 2312ccd979bdSMark Fasheh if (!status) 2313ccd979bdSMark Fasheh atomic_inc(&osb->alloc_stats.local_data); 2314ccd979bdSMark Fasheh } else { 2315ccd979bdSMark Fasheh if (min_clusters > (osb->bitmap_cpg - 1)) { 2316ccd979bdSMark Fasheh /* The only paths asking for contiguousness 2317ccd979bdSMark Fasheh * should know about this already. */ 23182fbe8d1eSSunil Mushran mlog(ML_ERROR, "minimum allocation requested %u exceeds " 23192fbe8d1eSSunil Mushran "group bitmap size %u!\n", min_clusters, 23202fbe8d1eSSunil Mushran osb->bitmap_cpg); 2321ccd979bdSMark Fasheh status = -ENOSPC; 2322ccd979bdSMark Fasheh goto bail; 2323ccd979bdSMark Fasheh } 2324ccd979bdSMark Fasheh /* clamp the current request down to a realistic size. */ 2325ccd979bdSMark Fasheh if (bits_wanted > (osb->bitmap_cpg - 1)) 2326ccd979bdSMark Fasheh bits_wanted = osb->bitmap_cpg - 1; 2327ccd979bdSMark Fasheh 2328aa8f8e93SJoel Becker status = ocfs2_claim_suballoc_bits(ac, 2329da5cbf2fSMark Fasheh handle, 2330ccd979bdSMark Fasheh bits_wanted, 2331ccd979bdSMark Fasheh min_clusters, 23327d1fe093SJoel Becker &res); 2333ccd979bdSMark Fasheh if (!status) { 2334ba206635SJoel Becker BUG_ON(res.sr_blkno); /* cluster alloc can't set */ 2335ccd979bdSMark Fasheh *cluster_start = 2336ccd979bdSMark Fasheh ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode, 23377d1fe093SJoel Becker res.sr_bg_blkno, 23387d1fe093SJoel Becker res.sr_bit_offset); 2339ccd979bdSMark Fasheh atomic_inc(&osb->alloc_stats.bitmap_data); 23404711954eSTao Ma *num_clusters = res.sr_bits; 2341ccd979bdSMark Fasheh } 2342ccd979bdSMark Fasheh } 2343ccd979bdSMark Fasheh if (status < 0) { 2344ccd979bdSMark Fasheh if (status != -ENOSPC) 2345ccd979bdSMark Fasheh mlog_errno(status); 2346ccd979bdSMark Fasheh goto bail; 2347ccd979bdSMark Fasheh } 2348ccd979bdSMark Fasheh 23494711954eSTao Ma ac->ac_bits_given += *num_clusters; 2350ccd979bdSMark Fasheh 2351ccd979bdSMark Fasheh bail: 2352c1e8d35eSTao Ma if (status) 2353c1e8d35eSTao Ma mlog_errno(status); 2354ccd979bdSMark Fasheh return status; 2355ccd979bdSMark Fasheh } 2356ccd979bdSMark Fasheh 23571ed9b777SJoel Becker int ocfs2_claim_clusters(handle_t *handle, 2358415cb800SMark Fasheh struct ocfs2_alloc_context *ac, 2359415cb800SMark Fasheh u32 min_clusters, 2360415cb800SMark Fasheh u32 *cluster_start, 2361415cb800SMark Fasheh u32 *num_clusters) 2362415cb800SMark Fasheh { 2363415cb800SMark Fasheh unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given; 2364415cb800SMark Fasheh 23651ed9b777SJoel Becker return __ocfs2_claim_clusters(handle, ac, min_clusters, 2366415cb800SMark Fasheh bits_wanted, cluster_start, num_clusters); 2367415cb800SMark Fasheh } 2368415cb800SMark Fasheh 2369b4414eeaSMark Fasheh static int ocfs2_block_group_clear_bits(handle_t *handle, 2370ccd979bdSMark Fasheh struct inode *alloc_inode, 2371ccd979bdSMark Fasheh struct ocfs2_group_desc *bg, 2372ccd979bdSMark Fasheh struct buffer_head *group_bh, 2373ccd979bdSMark Fasheh unsigned int bit_off, 2374b4414eeaSMark Fasheh unsigned int num_bits, 2375b4414eeaSMark Fasheh void (*undo_fn)(unsigned int bit, 2376b4414eeaSMark Fasheh unsigned long *bmap)) 2377ccd979bdSMark Fasheh { 2378ccd979bdSMark Fasheh int status; 2379ccd979bdSMark Fasheh unsigned int tmp; 2380ccd979bdSMark Fasheh struct ocfs2_group_desc *undo_bg = NULL; 2381ccd979bdSMark Fasheh 238242035306SJoel Becker /* The caller got this descriptor from 238342035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 238442035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 2385ccd979bdSMark Fasheh 23862f73e135STao Ma trace_ocfs2_block_group_clear_bits(bit_off, num_bits); 2387ccd979bdSMark Fasheh 2388b4414eeaSMark Fasheh BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode)); 23890cf2f763SJoel Becker status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), 2390b4414eeaSMark Fasheh group_bh, 2391b4414eeaSMark Fasheh undo_fn ? 2392b4414eeaSMark Fasheh OCFS2_JOURNAL_ACCESS_UNDO : 2393b4414eeaSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 2394ccd979bdSMark Fasheh if (status < 0) { 2395ccd979bdSMark Fasheh mlog_errno(status); 2396ccd979bdSMark Fasheh goto bail; 2397ccd979bdSMark Fasheh } 2398ccd979bdSMark Fasheh 2399b4414eeaSMark Fasheh if (undo_fn) { 240094e41ecfSSunil Mushran jbd_lock_bh_state(group_bh); 240194e41ecfSSunil Mushran undo_bg = (struct ocfs2_group_desc *) 240294e41ecfSSunil Mushran bh2jh(group_bh)->b_committed_data; 240394e41ecfSSunil Mushran BUG_ON(!undo_bg); 240494e41ecfSSunil Mushran } 2405ccd979bdSMark Fasheh 2406ccd979bdSMark Fasheh tmp = num_bits; 2407ccd979bdSMark Fasheh while(tmp--) { 2408ccd979bdSMark Fasheh ocfs2_clear_bit((bit_off + tmp), 2409ccd979bdSMark Fasheh (unsigned long *) bg->bg_bitmap); 2410b4414eeaSMark Fasheh if (undo_fn) 2411b4414eeaSMark Fasheh undo_fn(bit_off + tmp, 2412ccd979bdSMark Fasheh (unsigned long *) undo_bg->bg_bitmap); 2413ccd979bdSMark Fasheh } 2414ccd979bdSMark Fasheh le16_add_cpu(&bg->bg_free_bits_count, num_bits); 24159b5cd10eSSrinivas Eeda if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) { 24167ecef14aSJoe Perches return ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit count %u but claims %u are freed. num_bits %d\n", 24179b5cd10eSSrinivas Eeda (unsigned long long)le64_to_cpu(bg->bg_blkno), 24189b5cd10eSSrinivas Eeda le16_to_cpu(bg->bg_bits), 24197ecef14aSJoe Perches le16_to_cpu(bg->bg_free_bits_count), 24207ecef14aSJoe Perches num_bits); 24219b5cd10eSSrinivas Eeda } 2422ccd979bdSMark Fasheh 2423b4414eeaSMark Fasheh if (undo_fn) 242494e41ecfSSunil Mushran jbd_unlock_bh_state(group_bh); 242594e41ecfSSunil Mushran 2426ec20cec7SJoel Becker ocfs2_journal_dirty(handle, group_bh); 2427ccd979bdSMark Fasheh bail: 2428ccd979bdSMark Fasheh return status; 2429ccd979bdSMark Fasheh } 2430ccd979bdSMark Fasheh 2431ccd979bdSMark Fasheh /* 2432ccd979bdSMark Fasheh * expects the suballoc inode to already be locked. 2433ccd979bdSMark Fasheh */ 2434b4414eeaSMark Fasheh static int _ocfs2_free_suballoc_bits(handle_t *handle, 2435ccd979bdSMark Fasheh struct inode *alloc_inode, 2436ccd979bdSMark Fasheh struct buffer_head *alloc_bh, 2437ccd979bdSMark Fasheh unsigned int start_bit, 2438ccd979bdSMark Fasheh u64 bg_blkno, 2439b4414eeaSMark Fasheh unsigned int count, 2440b4414eeaSMark Fasheh void (*undo_fn)(unsigned int bit, 2441b4414eeaSMark Fasheh unsigned long *bitmap)) 2442ccd979bdSMark Fasheh { 2443ccd979bdSMark Fasheh int status = 0; 2444ccd979bdSMark Fasheh u32 tmp_used; 2445ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data; 2446ccd979bdSMark Fasheh struct ocfs2_chain_list *cl = &fe->id2.i_chain; 2447ccd979bdSMark Fasheh struct buffer_head *group_bh = NULL; 2448ccd979bdSMark Fasheh struct ocfs2_group_desc *group; 2449ccd979bdSMark Fasheh 245010995aa2SJoel Becker /* The alloc_bh comes from ocfs2_free_dinode() or 245110995aa2SJoel Becker * ocfs2_free_clusters(). The callers have all locked the 245210995aa2SJoel Becker * allocator and gotten alloc_bh from the lock call. This 245325985edcSLucas De Marchi * validates the dinode buffer. Any corruption that has happened 245410995aa2SJoel Becker * is a code bug. */ 245510995aa2SJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); 2456ccd979bdSMark Fasheh BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl)); 2457ccd979bdSMark Fasheh 24582f73e135STao Ma trace_ocfs2_free_suballoc_bits( 24592f73e135STao Ma (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, 24602f73e135STao Ma (unsigned long long)bg_blkno, 24612f73e135STao Ma start_bit, count); 2462ccd979bdSMark Fasheh 246368f64d47SJoel Becker status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno, 246468f64d47SJoel Becker &group_bh); 2465ccd979bdSMark Fasheh if (status < 0) { 2466ccd979bdSMark Fasheh mlog_errno(status); 2467ccd979bdSMark Fasheh goto bail; 2468ccd979bdSMark Fasheh } 2469ccd979bdSMark Fasheh group = (struct ocfs2_group_desc *) group_bh->b_data; 247068f64d47SJoel Becker 2471ccd979bdSMark Fasheh BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits)); 2472ccd979bdSMark Fasheh 2473ccd979bdSMark Fasheh status = ocfs2_block_group_clear_bits(handle, alloc_inode, 2474ccd979bdSMark Fasheh group, group_bh, 2475b4414eeaSMark Fasheh start_bit, count, undo_fn); 2476ccd979bdSMark Fasheh if (status < 0) { 2477ccd979bdSMark Fasheh mlog_errno(status); 2478ccd979bdSMark Fasheh goto bail; 2479ccd979bdSMark Fasheh } 2480ccd979bdSMark Fasheh 24810cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), 24820cf2f763SJoel Becker alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE); 2483ccd979bdSMark Fasheh if (status < 0) { 2484ccd979bdSMark Fasheh mlog_errno(status); 2485a47726bcSJoseph Qi ocfs2_block_group_set_bits(handle, alloc_inode, group, group_bh, 2486a47726bcSJoseph Qi start_bit, count); 2487ccd979bdSMark Fasheh goto bail; 2488ccd979bdSMark Fasheh } 2489ccd979bdSMark Fasheh 2490ccd979bdSMark Fasheh le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free, 2491ccd979bdSMark Fasheh count); 2492ccd979bdSMark Fasheh tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used); 2493ccd979bdSMark Fasheh fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count); 2494ec20cec7SJoel Becker ocfs2_journal_dirty(handle, alloc_bh); 2495ccd979bdSMark Fasheh 2496ccd979bdSMark Fasheh bail: 2497ccd979bdSMark Fasheh brelse(group_bh); 2498ccd979bdSMark Fasheh 2499c1e8d35eSTao Ma if (status) 2500c1e8d35eSTao Ma mlog_errno(status); 2501ccd979bdSMark Fasheh return status; 2502ccd979bdSMark Fasheh } 2503ccd979bdSMark Fasheh 2504b4414eeaSMark Fasheh int ocfs2_free_suballoc_bits(handle_t *handle, 2505b4414eeaSMark Fasheh struct inode *alloc_inode, 2506b4414eeaSMark Fasheh struct buffer_head *alloc_bh, 2507b4414eeaSMark Fasheh unsigned int start_bit, 2508b4414eeaSMark Fasheh u64 bg_blkno, 2509b4414eeaSMark Fasheh unsigned int count) 2510b4414eeaSMark Fasheh { 2511b4414eeaSMark Fasheh return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh, 2512b4414eeaSMark Fasheh start_bit, bg_blkno, count, NULL); 2513b4414eeaSMark Fasheh } 2514b4414eeaSMark Fasheh 25151fabe148SMark Fasheh int ocfs2_free_dinode(handle_t *handle, 2516ccd979bdSMark Fasheh struct inode *inode_alloc_inode, 2517ccd979bdSMark Fasheh struct buffer_head *inode_alloc_bh, 2518ccd979bdSMark Fasheh struct ocfs2_dinode *di) 2519ccd979bdSMark Fasheh { 2520ccd979bdSMark Fasheh u64 blk = le64_to_cpu(di->i_blkno); 2521ccd979bdSMark Fasheh u16 bit = le16_to_cpu(di->i_suballoc_bit); 2522ccd979bdSMark Fasheh u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit); 2523ccd979bdSMark Fasheh 252474380c47STao Ma if (di->i_suballoc_loc) 252574380c47STao Ma bg_blkno = le64_to_cpu(di->i_suballoc_loc); 2526ccd979bdSMark Fasheh return ocfs2_free_suballoc_bits(handle, inode_alloc_inode, 2527ccd979bdSMark Fasheh inode_alloc_bh, bit, bg_blkno, 1); 2528ccd979bdSMark Fasheh } 2529ccd979bdSMark Fasheh 2530b4414eeaSMark Fasheh static int _ocfs2_free_clusters(handle_t *handle, 2531ccd979bdSMark Fasheh struct inode *bitmap_inode, 2532ccd979bdSMark Fasheh struct buffer_head *bitmap_bh, 2533ccd979bdSMark Fasheh u64 start_blk, 2534b4414eeaSMark Fasheh unsigned int num_clusters, 2535b4414eeaSMark Fasheh void (*undo_fn)(unsigned int bit, 2536b4414eeaSMark Fasheh unsigned long *bitmap)) 2537ccd979bdSMark Fasheh { 2538ccd979bdSMark Fasheh int status; 2539ccd979bdSMark Fasheh u16 bg_start_bit; 2540ccd979bdSMark Fasheh u64 bg_blkno; 2541ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 2542ccd979bdSMark Fasheh 2543ccd979bdSMark Fasheh /* You can't ever have a contiguous set of clusters 2544ccd979bdSMark Fasheh * bigger than a block group bitmap so we never have to worry 2545ef6b689bSTao Ma * about looping on them. 2546ef6b689bSTao Ma * This is expensive. We can safely remove once this stuff has 2547ccd979bdSMark Fasheh * gotten tested really well. */ 2548ccd979bdSMark Fasheh BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk))); 2549ccd979bdSMark Fasheh 2550ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) bitmap_bh->b_data; 2551ccd979bdSMark Fasheh 2552ccd979bdSMark Fasheh ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno, 2553ccd979bdSMark Fasheh &bg_start_bit); 2554ccd979bdSMark Fasheh 25552f73e135STao Ma trace_ocfs2_free_clusters((unsigned long long)bg_blkno, 25562f73e135STao Ma (unsigned long long)start_blk, 25572f73e135STao Ma bg_start_bit, num_clusters); 2558ccd979bdSMark Fasheh 2559b4414eeaSMark Fasheh status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh, 2560ccd979bdSMark Fasheh bg_start_bit, bg_blkno, 2561b4414eeaSMark Fasheh num_clusters, undo_fn); 25629c7af40bSMark Fasheh if (status < 0) { 2563ccd979bdSMark Fasheh mlog_errno(status); 25649c7af40bSMark Fasheh goto out; 25659c7af40bSMark Fasheh } 2566ccd979bdSMark Fasheh 25679c7af40bSMark Fasheh ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb), 25689c7af40bSMark Fasheh num_clusters); 25699c7af40bSMark Fasheh 25709c7af40bSMark Fasheh out: 2571c1e8d35eSTao Ma if (status) 2572c1e8d35eSTao Ma mlog_errno(status); 2573ccd979bdSMark Fasheh return status; 2574ccd979bdSMark Fasheh } 2575ccd979bdSMark Fasheh 2576b4414eeaSMark Fasheh int ocfs2_free_clusters(handle_t *handle, 2577b4414eeaSMark Fasheh struct inode *bitmap_inode, 2578b4414eeaSMark Fasheh struct buffer_head *bitmap_bh, 2579b4414eeaSMark Fasheh u64 start_blk, 2580b4414eeaSMark Fasheh unsigned int num_clusters) 2581b4414eeaSMark Fasheh { 2582b4414eeaSMark Fasheh return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh, 2583b4414eeaSMark Fasheh start_blk, num_clusters, 2584b4414eeaSMark Fasheh _ocfs2_set_bit); 2585b4414eeaSMark Fasheh } 2586b4414eeaSMark Fasheh 2587b4414eeaSMark Fasheh /* 2588b4414eeaSMark Fasheh * Give never-used clusters back to the global bitmap. We don't need 2589b4414eeaSMark Fasheh * to protect these bits in the undo buffer. 2590b4414eeaSMark Fasheh */ 2591b4414eeaSMark Fasheh int ocfs2_release_clusters(handle_t *handle, 2592b4414eeaSMark Fasheh struct inode *bitmap_inode, 2593b4414eeaSMark Fasheh struct buffer_head *bitmap_bh, 2594b4414eeaSMark Fasheh u64 start_blk, 2595b4414eeaSMark Fasheh unsigned int num_clusters) 2596b4414eeaSMark Fasheh { 2597b4414eeaSMark Fasheh return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh, 2598b4414eeaSMark Fasheh start_blk, num_clusters, 2599b4414eeaSMark Fasheh _ocfs2_clear_bit); 2600b4414eeaSMark Fasheh } 2601b4414eeaSMark Fasheh 2602ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg) 2603ccd979bdSMark Fasheh { 2604ccd979bdSMark Fasheh printk("Block Group:\n"); 2605ccd979bdSMark Fasheh printk("bg_signature: %s\n", bg->bg_signature); 2606ccd979bdSMark Fasheh printk("bg_size: %u\n", bg->bg_size); 2607ccd979bdSMark Fasheh printk("bg_bits: %u\n", bg->bg_bits); 2608ccd979bdSMark Fasheh printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count); 2609ccd979bdSMark Fasheh printk("bg_chain: %u\n", bg->bg_chain); 2610ccd979bdSMark Fasheh printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation)); 2611b0697053SMark Fasheh printk("bg_next_group: %llu\n", 2612b0697053SMark Fasheh (unsigned long long)bg->bg_next_group); 2613b0697053SMark Fasheh printk("bg_parent_dinode: %llu\n", 2614b0697053SMark Fasheh (unsigned long long)bg->bg_parent_dinode); 2615b0697053SMark Fasheh printk("bg_blkno: %llu\n", 2616b0697053SMark Fasheh (unsigned long long)bg->bg_blkno); 2617ccd979bdSMark Fasheh } 2618ccd979bdSMark Fasheh 2619ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe) 2620ccd979bdSMark Fasheh { 2621ccd979bdSMark Fasheh int i; 2622ccd979bdSMark Fasheh 2623b0697053SMark Fasheh printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno); 2624ccd979bdSMark Fasheh printk("i_signature: %s\n", fe->i_signature); 2625b0697053SMark Fasheh printk("i_size: %llu\n", 2626b0697053SMark Fasheh (unsigned long long)fe->i_size); 2627ccd979bdSMark Fasheh printk("i_clusters: %u\n", fe->i_clusters); 2628ccd979bdSMark Fasheh printk("i_generation: %u\n", 2629ccd979bdSMark Fasheh le32_to_cpu(fe->i_generation)); 2630ccd979bdSMark Fasheh printk("id1.bitmap1.i_used: %u\n", 2631ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_used)); 2632ccd979bdSMark Fasheh printk("id1.bitmap1.i_total: %u\n", 2633ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_total)); 2634ccd979bdSMark Fasheh printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg); 2635ccd979bdSMark Fasheh printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc); 2636ccd979bdSMark Fasheh printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count); 2637ccd979bdSMark Fasheh printk("id2.i_chain.cl_next_free_rec: %u\n", 2638ccd979bdSMark Fasheh fe->id2.i_chain.cl_next_free_rec); 2639ccd979bdSMark Fasheh for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) { 2640ccd979bdSMark Fasheh printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i, 2641ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[i].c_free); 2642ccd979bdSMark Fasheh printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i, 2643ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[i].c_total); 2644b0697053SMark Fasheh printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i, 2645b0697053SMark Fasheh (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno); 2646ccd979bdSMark Fasheh } 2647ccd979bdSMark Fasheh } 2648e7d4cb6bSTao Ma 2649e7d4cb6bSTao Ma /* 2650e7d4cb6bSTao Ma * For a given allocation, determine which allocators will need to be 2651e7d4cb6bSTao Ma * accessed, and lock them, reserving the appropriate number of bits. 2652e7d4cb6bSTao Ma * 2653e7d4cb6bSTao Ma * Sparse file systems call this from ocfs2_write_begin_nolock() 2654e7d4cb6bSTao Ma * and ocfs2_allocate_unwritten_extents(). 2655e7d4cb6bSTao Ma * 2656e7d4cb6bSTao Ma * File systems which don't support holes call this from 2657e7d4cb6bSTao Ma * ocfs2_extend_allocation(). 2658e7d4cb6bSTao Ma */ 2659f99b9b7cSJoel Becker int ocfs2_lock_allocators(struct inode *inode, 2660f99b9b7cSJoel Becker struct ocfs2_extent_tree *et, 2661e7d4cb6bSTao Ma u32 clusters_to_add, u32 extents_to_split, 2662e7d4cb6bSTao Ma struct ocfs2_alloc_context **data_ac, 2663f99b9b7cSJoel Becker struct ocfs2_alloc_context **meta_ac) 2664e7d4cb6bSTao Ma { 2665e7d4cb6bSTao Ma int ret = 0, num_free_extents; 2666e7d4cb6bSTao Ma unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split; 2667e7d4cb6bSTao Ma struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2668e7d4cb6bSTao Ma 2669e7d4cb6bSTao Ma *meta_ac = NULL; 2670e7d4cb6bSTao Ma if (data_ac) 2671e7d4cb6bSTao Ma *data_ac = NULL; 2672e7d4cb6bSTao Ma 2673e7d4cb6bSTao Ma BUG_ON(clusters_to_add != 0 && data_ac == NULL); 2674e7d4cb6bSTao Ma 26753d03a305SJoel Becker num_free_extents = ocfs2_num_free_extents(osb, et); 2676e7d4cb6bSTao Ma if (num_free_extents < 0) { 2677e7d4cb6bSTao Ma ret = num_free_extents; 2678e7d4cb6bSTao Ma mlog_errno(ret); 2679e7d4cb6bSTao Ma goto out; 2680e7d4cb6bSTao Ma } 2681e7d4cb6bSTao Ma 2682e7d4cb6bSTao Ma /* 2683e7d4cb6bSTao Ma * Sparse allocation file systems need to be more conservative 2684e7d4cb6bSTao Ma * with reserving room for expansion - the actual allocation 2685e7d4cb6bSTao Ma * happens while we've got a journal handle open so re-taking 2686e7d4cb6bSTao Ma * a cluster lock (because we ran out of room for another 2687e7d4cb6bSTao Ma * extent) will violate ordering rules. 2688e7d4cb6bSTao Ma * 2689e7d4cb6bSTao Ma * Most of the time we'll only be seeing this 1 cluster at a time 2690e7d4cb6bSTao Ma * anyway. 2691e7d4cb6bSTao Ma * 2692e7d4cb6bSTao Ma * Always lock for any unwritten extents - we might want to 2693e7d4cb6bSTao Ma * add blocks during a split. 2694e7d4cb6bSTao Ma */ 2695e7d4cb6bSTao Ma if (!num_free_extents || 2696e7d4cb6bSTao Ma (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) { 2697f99b9b7cSJoel Becker ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac); 2698e7d4cb6bSTao Ma if (ret < 0) { 2699e7d4cb6bSTao Ma if (ret != -ENOSPC) 2700e7d4cb6bSTao Ma mlog_errno(ret); 2701e7d4cb6bSTao Ma goto out; 2702e7d4cb6bSTao Ma } 2703e7d4cb6bSTao Ma } 2704e7d4cb6bSTao Ma 2705e7d4cb6bSTao Ma if (clusters_to_add == 0) 2706e7d4cb6bSTao Ma goto out; 2707e7d4cb6bSTao Ma 2708e7d4cb6bSTao Ma ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac); 2709e7d4cb6bSTao Ma if (ret < 0) { 2710e7d4cb6bSTao Ma if (ret != -ENOSPC) 2711e7d4cb6bSTao Ma mlog_errno(ret); 2712e7d4cb6bSTao Ma goto out; 2713e7d4cb6bSTao Ma } 2714e7d4cb6bSTao Ma 2715e7d4cb6bSTao Ma out: 2716e7d4cb6bSTao Ma if (ret) { 2717e7d4cb6bSTao Ma if (*meta_ac) { 2718e7d4cb6bSTao Ma ocfs2_free_alloc_context(*meta_ac); 2719e7d4cb6bSTao Ma *meta_ac = NULL; 2720e7d4cb6bSTao Ma } 2721e7d4cb6bSTao Ma 2722e7d4cb6bSTao Ma /* 2723e7d4cb6bSTao Ma * We cannot have an error and a non null *data_ac. 2724e7d4cb6bSTao Ma */ 2725e7d4cb6bSTao Ma } 2726e7d4cb6bSTao Ma 2727e7d4cb6bSTao Ma return ret; 2728e7d4cb6bSTao Ma } 27296ca497a8Swengang wang 27306ca497a8Swengang wang /* 27316ca497a8Swengang wang * Read the inode specified by blkno to get suballoc_slot and 27326ca497a8Swengang wang * suballoc_bit. 27336ca497a8Swengang wang */ 27346ca497a8Swengang wang static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno, 2735889f004aSTao Ma u16 *suballoc_slot, u64 *group_blkno, 2736889f004aSTao Ma u16 *suballoc_bit) 27376ca497a8Swengang wang { 27386ca497a8Swengang wang int status; 27396ca497a8Swengang wang struct buffer_head *inode_bh = NULL; 27406ca497a8Swengang wang struct ocfs2_dinode *inode_fe; 27416ca497a8Swengang wang 27422f73e135STao Ma trace_ocfs2_get_suballoc_slot_bit((unsigned long long)blkno); 27436ca497a8Swengang wang 27446ca497a8Swengang wang /* dirty read disk */ 27456ca497a8Swengang wang status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh); 27466ca497a8Swengang wang if (status < 0) { 27475b09b507SJoel Becker mlog(ML_ERROR, "read block %llu failed %d\n", 27485b09b507SJoel Becker (unsigned long long)blkno, status); 27496ca497a8Swengang wang goto bail; 27506ca497a8Swengang wang } 27516ca497a8Swengang wang 27526ca497a8Swengang wang inode_fe = (struct ocfs2_dinode *) inode_bh->b_data; 27536ca497a8Swengang wang if (!OCFS2_IS_VALID_DINODE(inode_fe)) { 27545b09b507SJoel Becker mlog(ML_ERROR, "invalid inode %llu requested\n", 27555b09b507SJoel Becker (unsigned long long)blkno); 27566ca497a8Swengang wang status = -EINVAL; 27576ca497a8Swengang wang goto bail; 27586ca497a8Swengang wang } 27596ca497a8Swengang wang 27600fba8137STao Ma if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT && 27616ca497a8Swengang wang (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) { 27626ca497a8Swengang wang mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n", 27635b09b507SJoel Becker (unsigned long long)blkno, 27645b09b507SJoel Becker (u32)le16_to_cpu(inode_fe->i_suballoc_slot)); 27656ca497a8Swengang wang status = -EINVAL; 27666ca497a8Swengang wang goto bail; 27676ca497a8Swengang wang } 27686ca497a8Swengang wang 27696ca497a8Swengang wang if (suballoc_slot) 27706ca497a8Swengang wang *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot); 27716ca497a8Swengang wang if (suballoc_bit) 27726ca497a8Swengang wang *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit); 2773889f004aSTao Ma if (group_blkno) 2774889f004aSTao Ma *group_blkno = le64_to_cpu(inode_fe->i_suballoc_loc); 27756ca497a8Swengang wang 27766ca497a8Swengang wang bail: 27776ca497a8Swengang wang brelse(inode_bh); 27786ca497a8Swengang wang 2779c1e8d35eSTao Ma if (status) 2780c1e8d35eSTao Ma mlog_errno(status); 27816ca497a8Swengang wang return status; 27826ca497a8Swengang wang } 27836ca497a8Swengang wang 27846ca497a8Swengang wang /* 27856ca497a8Swengang wang * test whether bit is SET in allocator bitmap or not. on success, 0 27866ca497a8Swengang wang * is returned and *res is 1 for SET; 0 otherwise. when fails, errno 27876ca497a8Swengang wang * is returned and *res is meaningless. Call this after you have 27886ca497a8Swengang wang * cluster locked against suballoc, or you may get a result based on 27896ca497a8Swengang wang * non-up2date contents 27906ca497a8Swengang wang */ 27916ca497a8Swengang wang static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb, 27926ca497a8Swengang wang struct inode *suballoc, 2793889f004aSTao Ma struct buffer_head *alloc_bh, 2794889f004aSTao Ma u64 group_blkno, u64 blkno, 27956ca497a8Swengang wang u16 bit, int *res) 27966ca497a8Swengang wang { 2797abf1b3cbSTao Ma struct ocfs2_dinode *alloc_di; 27986ca497a8Swengang wang struct ocfs2_group_desc *group; 27996ca497a8Swengang wang struct buffer_head *group_bh = NULL; 28006ca497a8Swengang wang u64 bg_blkno; 28016ca497a8Swengang wang int status; 28026ca497a8Swengang wang 28032f73e135STao Ma trace_ocfs2_test_suballoc_bit((unsigned long long)blkno, 28045b09b507SJoel Becker (unsigned int)bit); 28056ca497a8Swengang wang 2806abf1b3cbSTao Ma alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data; 2807abf1b3cbSTao Ma if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) { 28086ca497a8Swengang wang mlog(ML_ERROR, "suballoc bit %u out of range of %u\n", 28096ca497a8Swengang wang (unsigned int)bit, 2810abf1b3cbSTao Ma ocfs2_bits_per_group(&alloc_di->id2.i_chain)); 28116ca497a8Swengang wang status = -EINVAL; 28126ca497a8Swengang wang goto bail; 28136ca497a8Swengang wang } 28146ca497a8Swengang wang 2815889f004aSTao Ma bg_blkno = group_blkno ? group_blkno : 2816889f004aSTao Ma ocfs2_which_suballoc_group(blkno, bit); 2817abf1b3cbSTao Ma status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno, 28186ca497a8Swengang wang &group_bh); 28196ca497a8Swengang wang if (status < 0) { 28205b09b507SJoel Becker mlog(ML_ERROR, "read group %llu failed %d\n", 28215b09b507SJoel Becker (unsigned long long)bg_blkno, status); 28226ca497a8Swengang wang goto bail; 28236ca497a8Swengang wang } 28246ca497a8Swengang wang 28256ca497a8Swengang wang group = (struct ocfs2_group_desc *) group_bh->b_data; 28266ca497a8Swengang wang *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap); 28276ca497a8Swengang wang 28286ca497a8Swengang wang bail: 28296ca497a8Swengang wang brelse(group_bh); 28306ca497a8Swengang wang 2831c1e8d35eSTao Ma if (status) 2832c1e8d35eSTao Ma mlog_errno(status); 28336ca497a8Swengang wang return status; 28346ca497a8Swengang wang } 28356ca497a8Swengang wang 28366ca497a8Swengang wang /* 28376ca497a8Swengang wang * Test if the bit representing this inode (blkno) is set in the 28386ca497a8Swengang wang * suballocator. 28396ca497a8Swengang wang * 28406ca497a8Swengang wang * On success, 0 is returned and *res is 1 for SET; 0 otherwise. 28416ca497a8Swengang wang * 28426ca497a8Swengang wang * In the event of failure, a negative value is returned and *res is 28436ca497a8Swengang wang * meaningless. 28446ca497a8Swengang wang * 28456ca497a8Swengang wang * Callers must make sure to hold nfs_sync_lock to prevent 28466ca497a8Swengang wang * ocfs2_delete_inode() on another node from accessing the same 28476ca497a8Swengang wang * suballocator concurrently. 28486ca497a8Swengang wang */ 28496ca497a8Swengang wang int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res) 28506ca497a8Swengang wang { 28516ca497a8Swengang wang int status; 2852889f004aSTao Ma u64 group_blkno = 0; 28536ca497a8Swengang wang u16 suballoc_bit = 0, suballoc_slot = 0; 28546ca497a8Swengang wang struct inode *inode_alloc_inode; 28556ca497a8Swengang wang struct buffer_head *alloc_bh = NULL; 28566ca497a8Swengang wang 28572f73e135STao Ma trace_ocfs2_test_inode_bit((unsigned long long)blkno); 28586ca497a8Swengang wang 28596ca497a8Swengang wang status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot, 2860889f004aSTao Ma &group_blkno, &suballoc_bit); 28616ca497a8Swengang wang if (status < 0) { 28626ca497a8Swengang wang mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status); 28636ca497a8Swengang wang goto bail; 28646ca497a8Swengang wang } 28656ca497a8Swengang wang 28666ca497a8Swengang wang inode_alloc_inode = 28676ca497a8Swengang wang ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE, 28686ca497a8Swengang wang suballoc_slot); 28696ca497a8Swengang wang if (!inode_alloc_inode) { 28706ca497a8Swengang wang /* the error code could be inaccurate, but we are not able to 28716ca497a8Swengang wang * get the correct one. */ 28726ca497a8Swengang wang status = -EINVAL; 28736ca497a8Swengang wang mlog(ML_ERROR, "unable to get alloc inode in slot %u\n", 28746ca497a8Swengang wang (u32)suballoc_slot); 28756ca497a8Swengang wang goto bail; 28766ca497a8Swengang wang } 28776ca497a8Swengang wang 28786ca497a8Swengang wang mutex_lock(&inode_alloc_inode->i_mutex); 28796ca497a8Swengang wang status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0); 28806ca497a8Swengang wang if (status < 0) { 28816ca497a8Swengang wang mutex_unlock(&inode_alloc_inode->i_mutex); 28827dc3e839Sjiangyiwen iput(inode_alloc_inode); 28836ca497a8Swengang wang mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n", 28846ca497a8Swengang wang (u32)suballoc_slot, status); 28856ca497a8Swengang wang goto bail; 28866ca497a8Swengang wang } 28876ca497a8Swengang wang 28886ca497a8Swengang wang status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh, 2889889f004aSTao Ma group_blkno, blkno, suballoc_bit, res); 28906ca497a8Swengang wang if (status < 0) 28916ca497a8Swengang wang mlog(ML_ERROR, "test suballoc bit failed %d\n", status); 28926ca497a8Swengang wang 28936ca497a8Swengang wang ocfs2_inode_unlock(inode_alloc_inode, 0); 28946ca497a8Swengang wang mutex_unlock(&inode_alloc_inode->i_mutex); 28956ca497a8Swengang wang 28966ca497a8Swengang wang iput(inode_alloc_inode); 28976ca497a8Swengang wang brelse(alloc_bh); 28986ca497a8Swengang wang bail: 2899c1e8d35eSTao Ma if (status) 2900c1e8d35eSTao Ma mlog_errno(status); 29016ca497a8Swengang wang return status; 29026ca497a8Swengang wang } 2903