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 #define MLOG_MASK_PREFIX ML_DISK_ALLOC 33ccd979bdSMark Fasheh #include <cluster/masklog.h> 34ccd979bdSMark Fasheh 35ccd979bdSMark Fasheh #include "ocfs2.h" 36ccd979bdSMark Fasheh 37ccd979bdSMark Fasheh #include "alloc.h" 38d6b32bbbSJoel Becker #include "blockcheck.h" 39ccd979bdSMark Fasheh #include "dlmglue.h" 40ccd979bdSMark Fasheh #include "inode.h" 41ccd979bdSMark Fasheh #include "journal.h" 42ccd979bdSMark Fasheh #include "localalloc.h" 43ccd979bdSMark Fasheh #include "suballoc.h" 44ccd979bdSMark Fasheh #include "super.h" 45ccd979bdSMark Fasheh #include "sysfile.h" 46ccd979bdSMark Fasheh #include "uptodate.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 { 577d1fe093SJoel Becker u64 sr_bg_blkno; /* The bg we allocated from */ 587d1fe093SJoel Becker unsigned int sr_bit_offset; /* The bit in the bg */ 597d1fe093SJoel Becker unsigned int sr_bits; /* How many bits we claimed */ 607d1fe093SJoel Becker }; 617d1fe093SJoel Becker 62ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg); 63ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe); 64ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl); 651fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle, 66ccd979bdSMark Fasheh struct inode *alloc_inode, 67ccd979bdSMark Fasheh struct buffer_head *bg_bh, 68ccd979bdSMark Fasheh u64 group_blkno, 69798db35fSJoel Becker unsigned int group_clusters, 70ccd979bdSMark Fasheh u16 my_chain, 71ccd979bdSMark Fasheh struct ocfs2_chain_list *cl); 72ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb, 73ccd979bdSMark Fasheh struct inode *alloc_inode, 741187c968SJoel Becker struct buffer_head *bh, 7560ca81e8STao Ma u64 max_block, 76feb473a6STao Ma u64 *last_alloc_group, 7760ca81e8STao Ma int flags); 78ccd979bdSMark Fasheh 79ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode, 80ccd979bdSMark Fasheh struct buffer_head *group_bh, 81ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 821187c968SJoel Becker u64 max_block, 837d1fe093SJoel Becker struct ocfs2_suballoc_result *res); 84ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode, 85ccd979bdSMark Fasheh struct buffer_head *group_bh, 86ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 871187c968SJoel Becker u64 max_block, 887d1fe093SJoel Becker struct ocfs2_suballoc_result *res); 89aa8f8e93SJoel Becker static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac, 901fabe148SMark Fasheh handle_t *handle, 91ccd979bdSMark Fasheh u32 bits_wanted, 92ccd979bdSMark Fasheh u32 min_bits, 937d1fe093SJoel Becker struct ocfs2_suballoc_result *res); 94ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh, 95ccd979bdSMark Fasheh int nr); 961fabe148SMark Fasheh static inline int ocfs2_block_group_set_bits(handle_t *handle, 97ccd979bdSMark Fasheh struct inode *alloc_inode, 98ccd979bdSMark Fasheh struct ocfs2_group_desc *bg, 99ccd979bdSMark Fasheh struct buffer_head *group_bh, 100ccd979bdSMark Fasheh unsigned int bit_off, 101ccd979bdSMark Fasheh unsigned int num_bits); 1021fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle, 103ccd979bdSMark Fasheh struct inode *alloc_inode, 104ccd979bdSMark Fasheh struct buffer_head *fe_bh, 105ccd979bdSMark Fasheh struct buffer_head *bg_bh, 106ccd979bdSMark Fasheh struct buffer_head *prev_bg_bh, 107ccd979bdSMark Fasheh u16 chain); 108ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg, 109ccd979bdSMark Fasheh u32 wanted); 110ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode, 111ccd979bdSMark Fasheh u64 bg_blkno, 112ccd979bdSMark Fasheh u16 bg_bit_off); 113ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode, 114ccd979bdSMark Fasheh u64 data_blkno, 115ccd979bdSMark Fasheh u64 *bg_blkno, 116ccd979bdSMark Fasheh u16 *bg_bit_off); 1171187c968SJoel Becker static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb, 1181187c968SJoel Becker u32 bits_wanted, u64 max_block, 11960ca81e8STao Ma int flags, 1201187c968SJoel Becker struct ocfs2_alloc_context **ac); 121ccd979bdSMark Fasheh 1229c7af40bSMark Fasheh void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac) 123ccd979bdSMark Fasheh { 124da5cbf2fSMark Fasheh struct inode *inode = ac->ac_inode; 125da5cbf2fSMark Fasheh 126da5cbf2fSMark Fasheh if (inode) { 127da5cbf2fSMark Fasheh if (ac->ac_which != OCFS2_AC_USE_LOCAL) 128e63aecb6SMark Fasheh ocfs2_inode_unlock(inode, 1); 129da5cbf2fSMark Fasheh 130da5cbf2fSMark Fasheh mutex_unlock(&inode->i_mutex); 131da5cbf2fSMark Fasheh 132da5cbf2fSMark Fasheh iput(inode); 1334d0ddb2cSTao Ma ac->ac_inode = NULL; 134da5cbf2fSMark Fasheh } 135ccd979bdSMark Fasheh brelse(ac->ac_bh); 1364d0ddb2cSTao Ma ac->ac_bh = NULL; 137e3b4a97dSMark Fasheh ac->ac_resv = NULL; 1384d0ddb2cSTao Ma } 1394d0ddb2cSTao Ma 1404d0ddb2cSTao Ma void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac) 1414d0ddb2cSTao Ma { 1424d0ddb2cSTao Ma ocfs2_free_ac_resource(ac); 143ccd979bdSMark Fasheh kfree(ac); 144ccd979bdSMark Fasheh } 145ccd979bdSMark Fasheh 146ccd979bdSMark Fasheh static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl) 147ccd979bdSMark Fasheh { 148ccd979bdSMark Fasheh return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc); 149ccd979bdSMark Fasheh } 150ccd979bdSMark Fasheh 15157e3e797SJoel Becker #define do_error(fmt, ...) \ 15257e3e797SJoel Becker do{ \ 15378c37eb0STao Ma if (resize) \ 15457e3e797SJoel Becker mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \ 15557e3e797SJoel Becker else \ 15657e3e797SJoel Becker ocfs2_error(sb, fmt, ##__VA_ARGS__); \ 15757e3e797SJoel Becker } while (0) 15857e3e797SJoel Becker 159970e4936SJoel Becker static int ocfs2_validate_gd_self(struct super_block *sb, 160970e4936SJoel Becker struct buffer_head *bh, 16178c37eb0STao Ma int resize) 162970e4936SJoel Becker { 163970e4936SJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 164970e4936SJoel Becker 1657bf72edeSMark Fasheh if (!OCFS2_IS_VALID_GROUP_DESC(gd)) { 16668f64d47SJoel Becker do_error("Group descriptor #%llu has bad signature %.*s", 16768f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 7, 16857e3e797SJoel Becker gd->bg_signature); 16957e3e797SJoel Becker return -EINVAL; 1707bf72edeSMark Fasheh } 1717bf72edeSMark Fasheh 17268f64d47SJoel Becker if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) { 17368f64d47SJoel Becker do_error("Group descriptor #%llu has an invalid bg_blkno " 17468f64d47SJoel Becker "of %llu", 17568f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 17668f64d47SJoel Becker (unsigned long long)le64_to_cpu(gd->bg_blkno)); 17768f64d47SJoel Becker return -EINVAL; 17868f64d47SJoel Becker } 17968f64d47SJoel Becker 18068f64d47SJoel Becker if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) { 18168f64d47SJoel Becker do_error("Group descriptor #%llu has an invalid " 18268f64d47SJoel Becker "fs_generation of #%u", 18368f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 18468f64d47SJoel Becker le32_to_cpu(gd->bg_generation)); 18568f64d47SJoel Becker return -EINVAL; 18668f64d47SJoel Becker } 18768f64d47SJoel Becker 188970e4936SJoel Becker if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) { 189970e4936SJoel Becker do_error("Group descriptor #%llu has bit count %u but " 190970e4936SJoel Becker "claims that %u are free", 191970e4936SJoel Becker (unsigned long long)bh->b_blocknr, 192970e4936SJoel Becker le16_to_cpu(gd->bg_bits), 193970e4936SJoel Becker le16_to_cpu(gd->bg_free_bits_count)); 194970e4936SJoel Becker return -EINVAL; 195970e4936SJoel Becker } 196970e4936SJoel Becker 197970e4936SJoel Becker if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) { 198970e4936SJoel Becker do_error("Group descriptor #%llu has bit count %u but " 199970e4936SJoel Becker "max bitmap bits of %u", 200970e4936SJoel Becker (unsigned long long)bh->b_blocknr, 201970e4936SJoel Becker le16_to_cpu(gd->bg_bits), 202970e4936SJoel Becker 8 * le16_to_cpu(gd->bg_size)); 203970e4936SJoel Becker return -EINVAL; 204970e4936SJoel Becker } 205970e4936SJoel Becker 206970e4936SJoel Becker return 0; 207970e4936SJoel Becker } 208970e4936SJoel Becker 209970e4936SJoel Becker static int ocfs2_validate_gd_parent(struct super_block *sb, 210970e4936SJoel Becker struct ocfs2_dinode *di, 211970e4936SJoel Becker struct buffer_head *bh, 21278c37eb0STao Ma int resize) 213970e4936SJoel Becker { 214970e4936SJoel Becker unsigned int max_bits; 215970e4936SJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 216970e4936SJoel Becker 2177bf72edeSMark Fasheh if (di->i_blkno != gd->bg_parent_dinode) { 21857e3e797SJoel Becker do_error("Group descriptor #%llu has bad parent " 2197bf72edeSMark Fasheh "pointer (%llu, expected %llu)", 22068f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 2217bf72edeSMark Fasheh (unsigned long long)le64_to_cpu(gd->bg_parent_dinode), 2227bf72edeSMark Fasheh (unsigned long long)le64_to_cpu(di->i_blkno)); 22357e3e797SJoel Becker return -EINVAL; 2247bf72edeSMark Fasheh } 2257bf72edeSMark Fasheh 2267bf72edeSMark Fasheh max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc); 2277bf72edeSMark Fasheh if (le16_to_cpu(gd->bg_bits) > max_bits) { 22857e3e797SJoel Becker do_error("Group descriptor #%llu has bit count of %u", 22968f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 2307bf72edeSMark Fasheh le16_to_cpu(gd->bg_bits)); 23157e3e797SJoel Becker return -EINVAL; 2327bf72edeSMark Fasheh } 2337bf72edeSMark Fasheh 23478c37eb0STao Ma /* In resize, we may meet the case bg_chain == cl_next_free_rec. */ 23578c37eb0STao Ma if ((le16_to_cpu(gd->bg_chain) > 23678c37eb0STao Ma le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) || 23778c37eb0STao Ma ((le16_to_cpu(gd->bg_chain) == 23878c37eb0STao Ma le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) { 23957e3e797SJoel Becker do_error("Group descriptor #%llu has bad chain %u", 24068f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 2417bf72edeSMark Fasheh le16_to_cpu(gd->bg_chain)); 24257e3e797SJoel Becker return -EINVAL; 2437bf72edeSMark Fasheh } 2447bf72edeSMark Fasheh 245970e4936SJoel Becker return 0; 2467bf72edeSMark Fasheh } 2477bf72edeSMark Fasheh 24857e3e797SJoel Becker #undef do_error 2497bf72edeSMark Fasheh 250970e4936SJoel Becker /* 251970e4936SJoel Becker * This version only prints errors. It does not fail the filesystem, and 252970e4936SJoel Becker * exists only for resize. 253970e4936SJoel Becker */ 254970e4936SJoel Becker int ocfs2_check_group_descriptor(struct super_block *sb, 255970e4936SJoel Becker struct ocfs2_dinode *di, 256970e4936SJoel Becker struct buffer_head *bh) 257970e4936SJoel Becker { 258970e4936SJoel Becker int rc; 259d6b32bbbSJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 260970e4936SJoel Becker 261d6b32bbbSJoel Becker BUG_ON(!buffer_uptodate(bh)); 262d6b32bbbSJoel Becker 263d6b32bbbSJoel Becker /* 264d6b32bbbSJoel Becker * If the ecc fails, we return the error but otherwise 265d6b32bbbSJoel Becker * leave the filesystem running. We know any error is 266d6b32bbbSJoel Becker * local to this block. 267d6b32bbbSJoel Becker */ 268d6b32bbbSJoel Becker rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check); 26913723d00SJoel Becker if (rc) { 27013723d00SJoel Becker mlog(ML_ERROR, 27113723d00SJoel Becker "Checksum failed for group descriptor %llu\n", 27213723d00SJoel Becker (unsigned long long)bh->b_blocknr); 27313723d00SJoel Becker } else 274970e4936SJoel Becker rc = ocfs2_validate_gd_self(sb, bh, 1); 275970e4936SJoel Becker if (!rc) 276970e4936SJoel Becker rc = ocfs2_validate_gd_parent(sb, di, bh, 1); 277970e4936SJoel Becker 278970e4936SJoel Becker return rc; 279970e4936SJoel Becker } 280970e4936SJoel Becker 281970e4936SJoel Becker static int ocfs2_validate_group_descriptor(struct super_block *sb, 282970e4936SJoel Becker struct buffer_head *bh) 283970e4936SJoel Becker { 284d6b32bbbSJoel Becker int rc; 285d6b32bbbSJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 286d6b32bbbSJoel Becker 287970e4936SJoel Becker mlog(0, "Validating group descriptor %llu\n", 288970e4936SJoel Becker (unsigned long long)bh->b_blocknr); 289970e4936SJoel Becker 290d6b32bbbSJoel Becker BUG_ON(!buffer_uptodate(bh)); 291d6b32bbbSJoel Becker 292d6b32bbbSJoel Becker /* 293d6b32bbbSJoel Becker * If the ecc fails, we return the error but otherwise 294d6b32bbbSJoel Becker * leave the filesystem running. We know any error is 295d6b32bbbSJoel Becker * local to this block. 296d6b32bbbSJoel Becker */ 297d6b32bbbSJoel Becker rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check); 298d6b32bbbSJoel Becker if (rc) 299d6b32bbbSJoel Becker return rc; 300d6b32bbbSJoel Becker 301d6b32bbbSJoel Becker /* 302d6b32bbbSJoel Becker * Errors after here are fatal. 303d6b32bbbSJoel Becker */ 304d6b32bbbSJoel Becker 305970e4936SJoel Becker return ocfs2_validate_gd_self(sb, bh, 0); 3067bf72edeSMark Fasheh } 3077bf72edeSMark Fasheh 30868f64d47SJoel Becker int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di, 30968f64d47SJoel Becker u64 gd_blkno, struct buffer_head **bh) 31068f64d47SJoel Becker { 31168f64d47SJoel Becker int rc; 31268f64d47SJoel Becker struct buffer_head *tmp = *bh; 31368f64d47SJoel Becker 3148cb471e8SJoel Becker rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp, 315970e4936SJoel Becker ocfs2_validate_group_descriptor); 31668f64d47SJoel Becker if (rc) 31768f64d47SJoel Becker goto out; 31868f64d47SJoel Becker 319970e4936SJoel Becker rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0); 32068f64d47SJoel Becker if (rc) { 32168f64d47SJoel Becker brelse(tmp); 32268f64d47SJoel Becker goto out; 32368f64d47SJoel Becker } 32468f64d47SJoel Becker 32568f64d47SJoel Becker /* If ocfs2_read_block() got us a new bh, pass it up. */ 32668f64d47SJoel Becker if (!*bh) 32768f64d47SJoel Becker *bh = tmp; 32868f64d47SJoel Becker 32968f64d47SJoel Becker out: 33068f64d47SJoel Becker return rc; 33168f64d47SJoel Becker } 33268f64d47SJoel Becker 333798db35fSJoel Becker static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb, 334798db35fSJoel Becker struct ocfs2_group_desc *bg, 335798db35fSJoel Becker struct ocfs2_chain_list *cl, 336798db35fSJoel Becker u64 p_blkno, u32 clusters) 337798db35fSJoel Becker { 338798db35fSJoel Becker struct ocfs2_extent_list *el = &bg->bg_list; 339798db35fSJoel Becker struct ocfs2_extent_rec *rec; 340798db35fSJoel Becker 341798db35fSJoel Becker BUG_ON(!ocfs2_supports_discontig_bh(osb)); 342798db35fSJoel Becker if (!el->l_next_free_rec) 343798db35fSJoel Becker el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb)); 344798db35fSJoel Becker rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)]; 345798db35fSJoel Becker rec->e_blkno = p_blkno; 346798db35fSJoel Becker rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) / 347798db35fSJoel Becker le16_to_cpu(cl->cl_bpc)); 348798db35fSJoel Becker rec->e_leaf_clusters = cpu_to_le32(clusters); 349798db35fSJoel Becker le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc)); 350798db35fSJoel Becker le16_add_cpu(&el->l_next_free_rec, 1); 351798db35fSJoel Becker } 352798db35fSJoel Becker 3531fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle, 354ccd979bdSMark Fasheh struct inode *alloc_inode, 355ccd979bdSMark Fasheh struct buffer_head *bg_bh, 356ccd979bdSMark Fasheh u64 group_blkno, 357798db35fSJoel Becker unsigned int group_clusters, 358ccd979bdSMark Fasheh u16 my_chain, 359ccd979bdSMark Fasheh struct ocfs2_chain_list *cl) 360ccd979bdSMark Fasheh { 361ccd979bdSMark Fasheh int status = 0; 362798db35fSJoel Becker struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); 363ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 364ccd979bdSMark Fasheh struct super_block * sb = alloc_inode->i_sb; 365ccd979bdSMark Fasheh 366ccd979bdSMark Fasheh mlog_entry_void(); 367ccd979bdSMark Fasheh 368ccd979bdSMark Fasheh if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) { 369b0697053SMark Fasheh ocfs2_error(alloc_inode->i_sb, "group block (%llu) != " 370b0697053SMark Fasheh "b_blocknr (%llu)", 371b0697053SMark Fasheh (unsigned long long)group_blkno, 372ccd979bdSMark Fasheh (unsigned long long) bg_bh->b_blocknr); 373ccd979bdSMark Fasheh status = -EIO; 374ccd979bdSMark Fasheh goto bail; 375ccd979bdSMark Fasheh } 376ccd979bdSMark Fasheh 37713723d00SJoel Becker status = ocfs2_journal_access_gd(handle, 3780cf2f763SJoel Becker INODE_CACHE(alloc_inode), 379ccd979bdSMark Fasheh bg_bh, 380ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_CREATE); 381ccd979bdSMark Fasheh if (status < 0) { 382ccd979bdSMark Fasheh mlog_errno(status); 383ccd979bdSMark Fasheh goto bail; 384ccd979bdSMark Fasheh } 385ccd979bdSMark Fasheh 386ccd979bdSMark Fasheh memset(bg, 0, sb->s_blocksize); 387ccd979bdSMark Fasheh strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE); 388ccd979bdSMark Fasheh bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation); 3894cbe4249SJoel Becker bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1)); 390ccd979bdSMark Fasheh bg->bg_chain = cpu_to_le16(my_chain); 391ccd979bdSMark Fasheh bg->bg_next_group = cl->cl_recs[my_chain].c_blkno; 392ccd979bdSMark Fasheh bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno); 393ccd979bdSMark Fasheh bg->bg_blkno = cpu_to_le64(group_blkno); 394798db35fSJoel Becker if (group_clusters == le16_to_cpu(cl->cl_cpg)) 395798db35fSJoel Becker bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl)); 396798db35fSJoel Becker else 397798db35fSJoel Becker ocfs2_bg_discontig_add_extent(osb, bg, cl, bg->bg_blkno, 398798db35fSJoel Becker group_clusters); 399798db35fSJoel Becker 400ccd979bdSMark Fasheh /* set the 1st bit in the bitmap to account for the descriptor block */ 401ccd979bdSMark Fasheh ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap); 402ccd979bdSMark Fasheh bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1); 403ccd979bdSMark Fasheh 404ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bg_bh); 405ccd979bdSMark Fasheh 406ccd979bdSMark Fasheh /* There is no need to zero out or otherwise initialize the 407ccd979bdSMark Fasheh * other blocks in a group - All valid FS metadata in a block 408ccd979bdSMark Fasheh * group stores the superblock fs_generation value at 409ccd979bdSMark Fasheh * allocation time. */ 410ccd979bdSMark Fasheh 411ccd979bdSMark Fasheh bail: 412ccd979bdSMark Fasheh mlog_exit(status); 413ccd979bdSMark Fasheh return status; 414ccd979bdSMark Fasheh } 415ccd979bdSMark Fasheh 416ccd979bdSMark Fasheh static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl) 417ccd979bdSMark Fasheh { 418ccd979bdSMark Fasheh u16 curr, best; 419ccd979bdSMark Fasheh 420ccd979bdSMark Fasheh best = curr = 0; 421ccd979bdSMark Fasheh while (curr < le16_to_cpu(cl->cl_count)) { 422ccd979bdSMark Fasheh if (le32_to_cpu(cl->cl_recs[best].c_total) > 423ccd979bdSMark Fasheh le32_to_cpu(cl->cl_recs[curr].c_total)) 424ccd979bdSMark Fasheh best = curr; 425ccd979bdSMark Fasheh curr++; 426ccd979bdSMark Fasheh } 427ccd979bdSMark Fasheh return best; 428ccd979bdSMark Fasheh } 429ccd979bdSMark Fasheh 430798db35fSJoel Becker static struct buffer_head * 431798db35fSJoel Becker ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle, 432798db35fSJoel Becker struct inode *alloc_inode, 433798db35fSJoel Becker struct ocfs2_alloc_context *ac, 434798db35fSJoel Becker struct ocfs2_chain_list *cl) 435798db35fSJoel Becker { 436798db35fSJoel Becker int status; 437798db35fSJoel Becker u32 bit_off, num_bits; 438798db35fSJoel Becker u64 bg_blkno; 439798db35fSJoel Becker struct buffer_head *bg_bh; 440798db35fSJoel Becker unsigned int alloc_rec = ocfs2_find_smallest_chain(cl); 441798db35fSJoel Becker 4421ed9b777SJoel Becker status = ocfs2_claim_clusters(handle, ac, 443798db35fSJoel Becker le16_to_cpu(cl->cl_cpg), &bit_off, 444798db35fSJoel Becker &num_bits); 445798db35fSJoel Becker if (status < 0) { 446798db35fSJoel Becker if (status != -ENOSPC) 447798db35fSJoel Becker mlog_errno(status); 448798db35fSJoel Becker goto bail; 449798db35fSJoel Becker } 450798db35fSJoel Becker 451798db35fSJoel Becker /* setup the group */ 452798db35fSJoel Becker bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off); 453798db35fSJoel Becker mlog(0, "new descriptor, record %u, at block %llu\n", 454798db35fSJoel Becker alloc_rec, (unsigned long long)bg_blkno); 455798db35fSJoel Becker 456798db35fSJoel Becker bg_bh = sb_getblk(osb->sb, bg_blkno); 457798db35fSJoel Becker if (!bg_bh) { 458798db35fSJoel Becker status = -EIO; 459798db35fSJoel Becker mlog_errno(status); 460798db35fSJoel Becker goto bail; 461798db35fSJoel Becker } 462798db35fSJoel Becker ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh); 463798db35fSJoel Becker 464798db35fSJoel Becker status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh, 465798db35fSJoel Becker bg_blkno, num_bits, alloc_rec, cl); 466798db35fSJoel Becker if (status < 0) { 467798db35fSJoel Becker brelse(bg_bh); 468798db35fSJoel Becker mlog_errno(status); 469798db35fSJoel Becker } 470798db35fSJoel Becker 471798db35fSJoel Becker bail: 472798db35fSJoel Becker return status ? ERR_PTR(status) : bg_bh; 473798db35fSJoel Becker } 474798db35fSJoel Becker 475798db35fSJoel Becker static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb, 476798db35fSJoel Becker handle_t *handle, 477798db35fSJoel Becker struct ocfs2_alloc_context *ac, 478798db35fSJoel Becker unsigned int min_bits, 479798db35fSJoel Becker u32 *bit_off, u32 *num_bits) 480798db35fSJoel Becker { 481798db35fSJoel Becker int status; 482798db35fSJoel Becker 483798db35fSJoel Becker while (min_bits) { 4841ed9b777SJoel Becker status = ocfs2_claim_clusters(handle, ac, min_bits, 485798db35fSJoel Becker bit_off, num_bits); 486798db35fSJoel Becker if (status != -ENOSPC) 487798db35fSJoel Becker break; 488798db35fSJoel Becker 489798db35fSJoel Becker min_bits >>= 1; 490798db35fSJoel Becker } 491798db35fSJoel Becker 492798db35fSJoel Becker return status; 493798db35fSJoel Becker } 494798db35fSJoel Becker 495798db35fSJoel Becker static int ocfs2_block_group_grow_discontig(handle_t *handle, 496798db35fSJoel Becker struct inode *alloc_inode, 497798db35fSJoel Becker struct buffer_head *bg_bh, 498798db35fSJoel Becker struct ocfs2_alloc_context *ac, 499798db35fSJoel Becker struct ocfs2_chain_list *cl, 500798db35fSJoel Becker unsigned int min_bits) 501798db35fSJoel Becker { 502798db35fSJoel Becker int status; 503798db35fSJoel Becker struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); 504798db35fSJoel Becker struct ocfs2_group_desc *bg = 505798db35fSJoel Becker (struct ocfs2_group_desc *)bg_bh->b_data; 506798db35fSJoel Becker unsigned int needed = 507798db35fSJoel Becker ocfs2_bits_per_group(cl) - le16_to_cpu(bg->bg_bits); 508798db35fSJoel Becker u32 p_cpos, clusters; 509798db35fSJoel Becker u64 p_blkno; 510798db35fSJoel Becker struct ocfs2_extent_list *el = &bg->bg_list; 511798db35fSJoel Becker 512798db35fSJoel Becker status = ocfs2_journal_access_gd(handle, 513798db35fSJoel Becker INODE_CACHE(alloc_inode), 514798db35fSJoel Becker bg_bh, 515798db35fSJoel Becker OCFS2_JOURNAL_ACCESS_CREATE); 516798db35fSJoel Becker if (status < 0) { 517798db35fSJoel Becker mlog_errno(status); 518798db35fSJoel Becker goto bail; 519798db35fSJoel Becker } 520798db35fSJoel Becker 521798db35fSJoel Becker while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) < 522798db35fSJoel Becker le16_to_cpu(el->l_count))) { 523798db35fSJoel Becker status = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_ALLOC); 524798db35fSJoel Becker if (status) { 525798db35fSJoel Becker mlog_errno(status); 526798db35fSJoel Becker goto bail; 527798db35fSJoel Becker } 528798db35fSJoel Becker 529798db35fSJoel Becker if (min_bits > needed) 530798db35fSJoel Becker min_bits = needed; 531798db35fSJoel Becker status = ocfs2_block_group_claim_bits(osb, handle, ac, 532798db35fSJoel Becker min_bits, &p_cpos, 533798db35fSJoel Becker &clusters); 534798db35fSJoel Becker if (status < 0) { 535798db35fSJoel Becker if (status != -ENOSPC) 536798db35fSJoel Becker mlog_errno(status); 537798db35fSJoel Becker goto bail; 538798db35fSJoel Becker } 539798db35fSJoel Becker p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos); 540798db35fSJoel Becker ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno, 541798db35fSJoel Becker clusters); 542798db35fSJoel Becker 543798db35fSJoel Becker min_bits = clusters; 544798db35fSJoel Becker needed = ocfs2_bits_per_group(cl) - le16_to_cpu(bg->bg_bits); 545798db35fSJoel Becker } 546798db35fSJoel Becker 547798db35fSJoel Becker if (needed > 0) { 548798db35fSJoel Becker } 549798db35fSJoel Becker 550798db35fSJoel Becker ocfs2_journal_dirty(handle, bg_bh); 551798db35fSJoel Becker 552798db35fSJoel Becker bail: 553798db35fSJoel Becker return status; 554798db35fSJoel Becker } 555798db35fSJoel Becker 556798db35fSJoel Becker static void ocfs2_bg_alloc_cleanup(struct inode *alloc_inode, 557798db35fSJoel Becker struct buffer_head *bg_bh, 558798db35fSJoel Becker struct ocfs2_cached_dealloc_ctxt *dealloc) 559798db35fSJoel Becker { 560798db35fSJoel Becker int i; 561798db35fSJoel Becker struct ocfs2_group_desc *bg; 562798db35fSJoel Becker struct ocfs2_extent_list *el; 563798db35fSJoel Becker struct ocfs2_extent_rec *rec; 564798db35fSJoel Becker 565798db35fSJoel Becker if (!bg_bh) 566798db35fSJoel Becker return; 567798db35fSJoel Becker 568798db35fSJoel Becker bg = (struct ocfs2_group_desc *)bg_bh->b_data; 569798db35fSJoel Becker el = &bg->bg_list; 570798db35fSJoel Becker for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { 571798db35fSJoel Becker rec = &el->l_recs[i]; 572798db35fSJoel Becker ocfs2_cache_cluster_dealloc(dealloc, 573798db35fSJoel Becker le64_to_cpu(rec->e_blkno), 574798db35fSJoel Becker le32_to_cpu(rec->e_leaf_clusters)); 575798db35fSJoel Becker } 576798db35fSJoel Becker 577798db35fSJoel Becker ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh); 578798db35fSJoel Becker brelse(bg_bh); 579798db35fSJoel Becker } 580798db35fSJoel Becker 581798db35fSJoel Becker static struct buffer_head * 582798db35fSJoel Becker ocfs2_block_group_alloc_discontig(handle_t *handle, 583798db35fSJoel Becker struct inode *alloc_inode, 584798db35fSJoel Becker struct ocfs2_alloc_context *ac, 585798db35fSJoel Becker struct ocfs2_chain_list *cl, 586798db35fSJoel Becker struct ocfs2_cached_dealloc_ctxt *dealloc) 587798db35fSJoel Becker { 588798db35fSJoel Becker int status; 589798db35fSJoel Becker u32 bit_off, num_bits; 590798db35fSJoel Becker u64 bg_blkno; 591798db35fSJoel Becker unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1; 592798db35fSJoel Becker struct buffer_head *bg_bh = NULL; 593798db35fSJoel Becker unsigned int alloc_rec = ocfs2_find_smallest_chain(cl); 594798db35fSJoel Becker struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); 595798db35fSJoel Becker 596798db35fSJoel Becker if (!ocfs2_supports_discontig_bh(osb)) { 597798db35fSJoel Becker status = -ENOSPC; 598798db35fSJoel Becker goto bail; 599798db35fSJoel Becker } 600798db35fSJoel Becker 601798db35fSJoel Becker /* Claim the first region */ 602798db35fSJoel Becker status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits, 603798db35fSJoel Becker &bit_off, &num_bits); 604798db35fSJoel Becker if (status < 0) { 605798db35fSJoel Becker if (status != -ENOSPC) 606798db35fSJoel Becker mlog_errno(status); 607798db35fSJoel Becker goto bail; 608798db35fSJoel Becker } 609798db35fSJoel Becker min_bits = num_bits; 610798db35fSJoel Becker 611798db35fSJoel Becker /* setup the group */ 612798db35fSJoel Becker bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off); 613798db35fSJoel Becker mlog(0, "new descriptor, record %u, at block %llu\n", 614798db35fSJoel Becker alloc_rec, (unsigned long long)bg_blkno); 615798db35fSJoel Becker 616798db35fSJoel Becker bg_bh = sb_getblk(osb->sb, bg_blkno); 617798db35fSJoel Becker if (!bg_bh) { 618798db35fSJoel Becker status = -EIO; 619798db35fSJoel Becker mlog_errno(status); 620798db35fSJoel Becker goto bail; 621798db35fSJoel Becker } 622798db35fSJoel Becker ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh); 623798db35fSJoel Becker 624798db35fSJoel Becker status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh, 625798db35fSJoel Becker bg_blkno, num_bits, alloc_rec, cl); 626798db35fSJoel Becker if (status < 0) { 627798db35fSJoel Becker mlog_errno(status); 628798db35fSJoel Becker goto bail; 629798db35fSJoel Becker } 630798db35fSJoel Becker 631798db35fSJoel Becker status = ocfs2_block_group_grow_discontig(handle, alloc_inode, 632798db35fSJoel Becker bg_bh, ac, cl, min_bits); 633798db35fSJoel Becker if (status) 634798db35fSJoel Becker mlog_errno(status); 635798db35fSJoel Becker 636798db35fSJoel Becker bail: 637798db35fSJoel Becker if (status) 638798db35fSJoel Becker ocfs2_bg_alloc_cleanup(alloc_inode, bg_bh, dealloc); 639798db35fSJoel Becker return status ? ERR_PTR(status) : bg_bh; 640798db35fSJoel Becker } 641798db35fSJoel Becker 642ccd979bdSMark Fasheh /* 643ccd979bdSMark Fasheh * We expect the block group allocator to already be locked. 644ccd979bdSMark Fasheh */ 645ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb, 646ccd979bdSMark Fasheh struct inode *alloc_inode, 6471187c968SJoel Becker struct buffer_head *bh, 64860ca81e8STao Ma u64 max_block, 649feb473a6STao Ma u64 *last_alloc_group, 65060ca81e8STao Ma int flags) 651ccd979bdSMark Fasheh { 652ccd979bdSMark Fasheh int status, credits; 653ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data; 654ccd979bdSMark Fasheh struct ocfs2_chain_list *cl; 655ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac = NULL; 6561fabe148SMark Fasheh handle_t *handle = NULL; 657ccd979bdSMark Fasheh u64 bg_blkno; 658ccd979bdSMark Fasheh struct buffer_head *bg_bh = NULL; 659ccd979bdSMark Fasheh struct ocfs2_group_desc *bg; 660798db35fSJoel Becker struct ocfs2_cached_dealloc_ctxt dealloc; 661ccd979bdSMark Fasheh 662ccd979bdSMark Fasheh BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode)); 663ccd979bdSMark Fasheh 664ccd979bdSMark Fasheh mlog_entry_void(); 665ccd979bdSMark Fasheh 666798db35fSJoel Becker ocfs2_init_dealloc_ctxt(&dealloc); 667798db35fSJoel Becker 668ccd979bdSMark Fasheh cl = &fe->id2.i_chain; 6691187c968SJoel Becker status = ocfs2_reserve_clusters_with_limit(osb, 670ccd979bdSMark Fasheh le16_to_cpu(cl->cl_cpg), 67160ca81e8STao Ma max_block, flags, &ac); 672ccd979bdSMark Fasheh if (status < 0) { 673ccd979bdSMark Fasheh if (status != -ENOSPC) 674ccd979bdSMark Fasheh mlog_errno(status); 675ccd979bdSMark Fasheh goto bail; 676ccd979bdSMark Fasheh } 677ccd979bdSMark Fasheh 678ccd979bdSMark Fasheh credits = ocfs2_calc_group_alloc_credits(osb->sb, 679ccd979bdSMark Fasheh le16_to_cpu(cl->cl_cpg)); 68065eff9ccSMark Fasheh handle = ocfs2_start_trans(osb, credits); 681ccd979bdSMark Fasheh if (IS_ERR(handle)) { 682ccd979bdSMark Fasheh status = PTR_ERR(handle); 683ccd979bdSMark Fasheh handle = NULL; 684ccd979bdSMark Fasheh mlog_errno(status); 685ccd979bdSMark Fasheh goto bail; 686ccd979bdSMark Fasheh } 687ccd979bdSMark Fasheh 688feb473a6STao Ma if (last_alloc_group && *last_alloc_group != 0) { 689feb473a6STao Ma mlog(0, "use old allocation group %llu for block group alloc\n", 690feb473a6STao Ma (unsigned long long)*last_alloc_group); 691feb473a6STao Ma ac->ac_last_group = *last_alloc_group; 692feb473a6STao Ma } 693798db35fSJoel Becker 694798db35fSJoel Becker bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode, 695798db35fSJoel Becker ac, cl); 696798db35fSJoel Becker if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC)) 697798db35fSJoel Becker bg_bh = ocfs2_block_group_alloc_discontig(handle, 698798db35fSJoel Becker alloc_inode, 699798db35fSJoel Becker ac, cl, 700798db35fSJoel Becker &dealloc); 701798db35fSJoel Becker if (IS_ERR(bg_bh)) { 702798db35fSJoel Becker status = PTR_ERR(bg_bh); 703798db35fSJoel Becker bg_bh = NULL; 704ccd979bdSMark Fasheh if (status != -ENOSPC) 705ccd979bdSMark Fasheh mlog_errno(status); 706ccd979bdSMark Fasheh goto bail; 707ccd979bdSMark Fasheh } 708ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) bg_bh->b_data; 709ccd979bdSMark Fasheh 7100cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), 711ccd979bdSMark Fasheh bh, OCFS2_JOURNAL_ACCESS_WRITE); 712ccd979bdSMark Fasheh if (status < 0) { 713ccd979bdSMark Fasheh mlog_errno(status); 714ccd979bdSMark Fasheh goto bail; 715ccd979bdSMark Fasheh } 716ccd979bdSMark Fasheh 717798db35fSJoel Becker le32_add_cpu(&cl->cl_recs[bg->bg_chain].c_free, 718ccd979bdSMark Fasheh le16_to_cpu(bg->bg_free_bits_count)); 719798db35fSJoel Becker le32_add_cpu(&cl->cl_recs[bg->bg_chain].c_total, 720798db35fSJoel Becker le16_to_cpu(bg->bg_bits)); 721798db35fSJoel Becker cl->cl_recs[bg->bg_chain].c_blkno = cpu_to_le64(bg_blkno); 722ccd979bdSMark Fasheh if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count)) 723ccd979bdSMark Fasheh le16_add_cpu(&cl->cl_next_free_rec, 1); 724ccd979bdSMark Fasheh 725ccd979bdSMark Fasheh le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) - 726ccd979bdSMark Fasheh le16_to_cpu(bg->bg_free_bits_count)); 727ccd979bdSMark Fasheh le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits)); 728ccd979bdSMark Fasheh le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg)); 729ccd979bdSMark Fasheh 730ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bh); 731ccd979bdSMark Fasheh 732ccd979bdSMark Fasheh spin_lock(&OCFS2_I(alloc_inode)->ip_lock); 733ccd979bdSMark Fasheh OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters); 734ccd979bdSMark Fasheh fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb, 735ccd979bdSMark Fasheh le32_to_cpu(fe->i_clusters))); 736ccd979bdSMark Fasheh spin_unlock(&OCFS2_I(alloc_inode)->ip_lock); 737ccd979bdSMark Fasheh i_size_write(alloc_inode, le64_to_cpu(fe->i_size)); 7388110b073SMark Fasheh alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode); 739ccd979bdSMark Fasheh 740ccd979bdSMark Fasheh status = 0; 741feb473a6STao Ma 742feb473a6STao Ma /* save the new last alloc group so that the caller can cache it. */ 743feb473a6STao Ma if (last_alloc_group) 744feb473a6STao Ma *last_alloc_group = ac->ac_last_group; 745feb473a6STao Ma 746ccd979bdSMark Fasheh bail: 747ccd979bdSMark Fasheh if (handle) 74802dc1af4SMark Fasheh ocfs2_commit_trans(osb, handle); 749ccd979bdSMark Fasheh 750798db35fSJoel Becker if (ocfs2_dealloc_has_cluster(&dealloc)) { 751798db35fSJoel Becker ocfs2_schedule_truncate_log_flush(osb, 1); 752798db35fSJoel Becker ocfs2_run_deallocs(osb, &dealloc); 753798db35fSJoel Becker } 754798db35fSJoel Becker 755ccd979bdSMark Fasheh if (ac) 756ccd979bdSMark Fasheh ocfs2_free_alloc_context(ac); 757ccd979bdSMark Fasheh 758ccd979bdSMark Fasheh brelse(bg_bh); 759ccd979bdSMark Fasheh 760ccd979bdSMark Fasheh mlog_exit(status); 761ccd979bdSMark Fasheh return status; 762ccd979bdSMark Fasheh } 763ccd979bdSMark Fasheh 764ccd979bdSMark Fasheh static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb, 765da5cbf2fSMark Fasheh struct ocfs2_alloc_context *ac, 766da5cbf2fSMark Fasheh int type, 767ffda89a3STao Ma u32 slot, 768feb473a6STao Ma u64 *last_alloc_group, 76960ca81e8STao Ma int flags) 770ccd979bdSMark Fasheh { 771ccd979bdSMark Fasheh int status; 772ccd979bdSMark Fasheh u32 bits_wanted = ac->ac_bits_wanted; 773da5cbf2fSMark Fasheh struct inode *alloc_inode; 774ccd979bdSMark Fasheh struct buffer_head *bh = NULL; 775ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 776ccd979bdSMark Fasheh u32 free_bits; 777ccd979bdSMark Fasheh 778ccd979bdSMark Fasheh mlog_entry_void(); 779ccd979bdSMark Fasheh 780da5cbf2fSMark Fasheh alloc_inode = ocfs2_get_system_file_inode(osb, type, slot); 781da5cbf2fSMark Fasheh if (!alloc_inode) { 782da5cbf2fSMark Fasheh mlog_errno(-EINVAL); 783da5cbf2fSMark Fasheh return -EINVAL; 784ccd979bdSMark Fasheh } 785ccd979bdSMark Fasheh 786da5cbf2fSMark Fasheh mutex_lock(&alloc_inode->i_mutex); 787da5cbf2fSMark Fasheh 788e63aecb6SMark Fasheh status = ocfs2_inode_lock(alloc_inode, &bh, 1); 789da5cbf2fSMark Fasheh if (status < 0) { 790da5cbf2fSMark Fasheh mutex_unlock(&alloc_inode->i_mutex); 791da5cbf2fSMark Fasheh iput(alloc_inode); 792da5cbf2fSMark Fasheh 793da5cbf2fSMark Fasheh mlog_errno(status); 794da5cbf2fSMark Fasheh return status; 795da5cbf2fSMark Fasheh } 796da5cbf2fSMark Fasheh 797da5cbf2fSMark Fasheh ac->ac_inode = alloc_inode; 798a4a48911STao Ma ac->ac_alloc_slot = slot; 799da5cbf2fSMark Fasheh 800ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) bh->b_data; 80110995aa2SJoel Becker 80210995aa2SJoel Becker /* The bh was validated by the inode read inside 80310995aa2SJoel Becker * ocfs2_inode_lock(). Any corruption is a code bug. */ 80410995aa2SJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); 80510995aa2SJoel Becker 806ccd979bdSMark Fasheh if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) { 807b0697053SMark Fasheh ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu", 808b0697053SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno)); 809ccd979bdSMark Fasheh status = -EIO; 810ccd979bdSMark Fasheh goto bail; 811ccd979bdSMark Fasheh } 812ccd979bdSMark Fasheh 813ccd979bdSMark Fasheh free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) - 814ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_used); 815ccd979bdSMark Fasheh 816ccd979bdSMark Fasheh if (bits_wanted > free_bits) { 817ccd979bdSMark Fasheh /* cluster bitmap never grows */ 818ccd979bdSMark Fasheh if (ocfs2_is_cluster_bitmap(alloc_inode)) { 819ccd979bdSMark Fasheh mlog(0, "Disk Full: wanted=%u, free_bits=%u\n", 820ccd979bdSMark Fasheh bits_wanted, free_bits); 821ccd979bdSMark Fasheh status = -ENOSPC; 822ccd979bdSMark Fasheh goto bail; 823ccd979bdSMark Fasheh } 824ccd979bdSMark Fasheh 82560ca81e8STao Ma if (!(flags & ALLOC_NEW_GROUP)) { 826ffda89a3STao Ma mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, " 827ffda89a3STao Ma "and we don't alloc a new group for it.\n", 828ffda89a3STao Ma slot, bits_wanted, free_bits); 829ffda89a3STao Ma status = -ENOSPC; 830ffda89a3STao Ma goto bail; 831ffda89a3STao Ma } 832ffda89a3STao Ma 8331187c968SJoel Becker status = ocfs2_block_group_alloc(osb, alloc_inode, bh, 834feb473a6STao Ma ac->ac_max_block, 835feb473a6STao Ma last_alloc_group, flags); 836ccd979bdSMark Fasheh if (status < 0) { 837ccd979bdSMark Fasheh if (status != -ENOSPC) 838ccd979bdSMark Fasheh mlog_errno(status); 839ccd979bdSMark Fasheh goto bail; 840ccd979bdSMark Fasheh } 841ccd979bdSMark Fasheh atomic_inc(&osb->alloc_stats.bg_extends); 842ccd979bdSMark Fasheh 843ccd979bdSMark Fasheh /* You should never ask for this much metadata */ 844ccd979bdSMark Fasheh BUG_ON(bits_wanted > 845ccd979bdSMark Fasheh (le32_to_cpu(fe->id1.bitmap1.i_total) 846ccd979bdSMark Fasheh - le32_to_cpu(fe->id1.bitmap1.i_used))); 847ccd979bdSMark Fasheh } 848ccd979bdSMark Fasheh 849ccd979bdSMark Fasheh get_bh(bh); 850ccd979bdSMark Fasheh ac->ac_bh = bh; 851ccd979bdSMark Fasheh bail: 852ccd979bdSMark Fasheh brelse(bh); 853ccd979bdSMark Fasheh 854ccd979bdSMark Fasheh mlog_exit(status); 855ccd979bdSMark Fasheh return status; 856ccd979bdSMark Fasheh } 857ccd979bdSMark Fasheh 858b89c5428STiger Yang static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb) 859b89c5428STiger Yang { 860b89c5428STiger Yang spin_lock(&osb->osb_lock); 861b89c5428STiger Yang osb->s_inode_steal_slot = OCFS2_INVALID_SLOT; 862b89c5428STiger Yang spin_unlock(&osb->osb_lock); 863b89c5428STiger Yang atomic_set(&osb->s_num_inodes_stolen, 0); 864b89c5428STiger Yang } 865b89c5428STiger Yang 866b89c5428STiger Yang static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb) 867b89c5428STiger Yang { 868b89c5428STiger Yang spin_lock(&osb->osb_lock); 869b89c5428STiger Yang osb->s_meta_steal_slot = OCFS2_INVALID_SLOT; 870b89c5428STiger Yang spin_unlock(&osb->osb_lock); 871b89c5428STiger Yang atomic_set(&osb->s_num_meta_stolen, 0); 872b89c5428STiger Yang } 873b89c5428STiger Yang 874b89c5428STiger Yang void ocfs2_init_steal_slots(struct ocfs2_super *osb) 875b89c5428STiger Yang { 876b89c5428STiger Yang ocfs2_init_inode_steal_slot(osb); 877b89c5428STiger Yang ocfs2_init_meta_steal_slot(osb); 878b89c5428STiger Yang } 879b89c5428STiger Yang 880b89c5428STiger Yang static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type) 881b89c5428STiger Yang { 882b89c5428STiger Yang spin_lock(&osb->osb_lock); 883b89c5428STiger Yang if (type == INODE_ALLOC_SYSTEM_INODE) 884b89c5428STiger Yang osb->s_inode_steal_slot = slot; 885b89c5428STiger Yang else if (type == EXTENT_ALLOC_SYSTEM_INODE) 886b89c5428STiger Yang osb->s_meta_steal_slot = slot; 887b89c5428STiger Yang spin_unlock(&osb->osb_lock); 888b89c5428STiger Yang } 889b89c5428STiger Yang 890b89c5428STiger Yang static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type) 891b89c5428STiger Yang { 892b89c5428STiger Yang int slot = OCFS2_INVALID_SLOT; 893b89c5428STiger Yang 894b89c5428STiger Yang spin_lock(&osb->osb_lock); 895b89c5428STiger Yang if (type == INODE_ALLOC_SYSTEM_INODE) 896b89c5428STiger Yang slot = osb->s_inode_steal_slot; 897b89c5428STiger Yang else if (type == EXTENT_ALLOC_SYSTEM_INODE) 898b89c5428STiger Yang slot = osb->s_meta_steal_slot; 899b89c5428STiger Yang spin_unlock(&osb->osb_lock); 900b89c5428STiger Yang 901b89c5428STiger Yang return slot; 902b89c5428STiger Yang } 903b89c5428STiger Yang 904b89c5428STiger Yang static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb) 905b89c5428STiger Yang { 906b89c5428STiger Yang return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE); 907b89c5428STiger Yang } 908b89c5428STiger Yang 909b89c5428STiger Yang static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb) 910b89c5428STiger Yang { 911b89c5428STiger Yang return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE); 912b89c5428STiger Yang } 913b89c5428STiger Yang 914b89c5428STiger Yang static int ocfs2_steal_resource(struct ocfs2_super *osb, 915b89c5428STiger Yang struct ocfs2_alloc_context *ac, 916b89c5428STiger Yang int type) 917b89c5428STiger Yang { 918b89c5428STiger Yang int i, status = -ENOSPC; 919b89c5428STiger Yang int slot = __ocfs2_get_steal_slot(osb, type); 920b89c5428STiger Yang 921b89c5428STiger Yang /* Start to steal resource from the first slot after ours. */ 922b89c5428STiger Yang if (slot == OCFS2_INVALID_SLOT) 923b89c5428STiger Yang slot = osb->slot_num + 1; 924b89c5428STiger Yang 925b89c5428STiger Yang for (i = 0; i < osb->max_slots; i++, slot++) { 926b89c5428STiger Yang if (slot == osb->max_slots) 927b89c5428STiger Yang slot = 0; 928b89c5428STiger Yang 929b89c5428STiger Yang if (slot == osb->slot_num) 930b89c5428STiger Yang continue; 931b89c5428STiger Yang 932b89c5428STiger Yang status = ocfs2_reserve_suballoc_bits(osb, ac, 933b89c5428STiger Yang type, 934b89c5428STiger Yang (u32)slot, NULL, 935b89c5428STiger Yang NOT_ALLOC_NEW_GROUP); 936b89c5428STiger Yang if (status >= 0) { 937b89c5428STiger Yang __ocfs2_set_steal_slot(osb, slot, type); 938b89c5428STiger Yang break; 939b89c5428STiger Yang } 940b89c5428STiger Yang 941b89c5428STiger Yang ocfs2_free_ac_resource(ac); 942b89c5428STiger Yang } 943b89c5428STiger Yang 944b89c5428STiger Yang return status; 945b89c5428STiger Yang } 946b89c5428STiger Yang 947b89c5428STiger Yang static int ocfs2_steal_inode(struct ocfs2_super *osb, 948b89c5428STiger Yang struct ocfs2_alloc_context *ac) 949b89c5428STiger Yang { 950b89c5428STiger Yang return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE); 951b89c5428STiger Yang } 952b89c5428STiger Yang 953b89c5428STiger Yang static int ocfs2_steal_meta(struct ocfs2_super *osb, 954b89c5428STiger Yang struct ocfs2_alloc_context *ac) 955b89c5428STiger Yang { 956b89c5428STiger Yang return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE); 957b89c5428STiger Yang } 958b89c5428STiger Yang 959cf1d6c76STiger Yang int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb, 960cf1d6c76STiger Yang int blocks, 961ccd979bdSMark Fasheh struct ocfs2_alloc_context **ac) 962ccd979bdSMark Fasheh { 963ccd979bdSMark Fasheh int status; 964b89c5428STiger Yang int slot = ocfs2_get_meta_steal_slot(osb); 965ccd979bdSMark Fasheh 966cd861280SRobert P. J. Day *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); 967ccd979bdSMark Fasheh if (!(*ac)) { 968ccd979bdSMark Fasheh status = -ENOMEM; 969ccd979bdSMark Fasheh mlog_errno(status); 970ccd979bdSMark Fasheh goto bail; 971ccd979bdSMark Fasheh } 972ccd979bdSMark Fasheh 973cf1d6c76STiger Yang (*ac)->ac_bits_wanted = blocks; 974ccd979bdSMark Fasheh (*ac)->ac_which = OCFS2_AC_USE_META; 975ccd979bdSMark Fasheh (*ac)->ac_group_search = ocfs2_block_group_search; 976ccd979bdSMark Fasheh 977b89c5428STiger Yang if (slot != OCFS2_INVALID_SLOT && 978b89c5428STiger Yang atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL) 979b89c5428STiger Yang goto extent_steal; 980b89c5428STiger Yang 981b89c5428STiger Yang atomic_set(&osb->s_num_meta_stolen, 0); 982da5cbf2fSMark Fasheh status = ocfs2_reserve_suballoc_bits(osb, (*ac), 983ffda89a3STao Ma EXTENT_ALLOC_SYSTEM_INODE, 984b89c5428STiger Yang (u32)osb->slot_num, NULL, 98533d5d380SMark Fasheh ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP); 986b89c5428STiger Yang 987b89c5428STiger Yang 988b89c5428STiger Yang if (status >= 0) { 989b89c5428STiger Yang status = 0; 990b89c5428STiger Yang if (slot != OCFS2_INVALID_SLOT) 991b89c5428STiger Yang ocfs2_init_meta_steal_slot(osb); 992b89c5428STiger Yang goto bail; 993b89c5428STiger Yang } else if (status < 0 && status != -ENOSPC) { 994b89c5428STiger Yang mlog_errno(status); 995b89c5428STiger Yang goto bail; 996b89c5428STiger Yang } 997b89c5428STiger Yang 998b89c5428STiger Yang ocfs2_free_ac_resource(*ac); 999b89c5428STiger Yang 1000b89c5428STiger Yang extent_steal: 1001b89c5428STiger Yang status = ocfs2_steal_meta(osb, *ac); 1002b89c5428STiger Yang atomic_inc(&osb->s_num_meta_stolen); 1003ccd979bdSMark Fasheh if (status < 0) { 1004ccd979bdSMark Fasheh if (status != -ENOSPC) 1005ccd979bdSMark Fasheh mlog_errno(status); 1006ccd979bdSMark Fasheh goto bail; 1007ccd979bdSMark Fasheh } 1008ccd979bdSMark Fasheh 1009ccd979bdSMark Fasheh status = 0; 1010ccd979bdSMark Fasheh bail: 1011ccd979bdSMark Fasheh if ((status < 0) && *ac) { 1012ccd979bdSMark Fasheh ocfs2_free_alloc_context(*ac); 1013ccd979bdSMark Fasheh *ac = NULL; 1014ccd979bdSMark Fasheh } 1015ccd979bdSMark Fasheh 1016ccd979bdSMark Fasheh mlog_exit(status); 1017ccd979bdSMark Fasheh return status; 1018ccd979bdSMark Fasheh } 1019ccd979bdSMark Fasheh 1020cf1d6c76STiger Yang int ocfs2_reserve_new_metadata(struct ocfs2_super *osb, 1021cf1d6c76STiger Yang struct ocfs2_extent_list *root_el, 1022cf1d6c76STiger Yang struct ocfs2_alloc_context **ac) 1023cf1d6c76STiger Yang { 1024cf1d6c76STiger Yang return ocfs2_reserve_new_metadata_blocks(osb, 1025cf1d6c76STiger Yang ocfs2_extend_meta_needed(root_el), 1026cf1d6c76STiger Yang ac); 1027cf1d6c76STiger Yang } 1028cf1d6c76STiger Yang 1029ccd979bdSMark Fasheh int ocfs2_reserve_new_inode(struct ocfs2_super *osb, 1030ccd979bdSMark Fasheh struct ocfs2_alloc_context **ac) 1031ccd979bdSMark Fasheh { 1032ccd979bdSMark Fasheh int status; 1033b89c5428STiger Yang int slot = ocfs2_get_inode_steal_slot(osb); 1034feb473a6STao Ma u64 alloc_group; 1035ccd979bdSMark Fasheh 1036cd861280SRobert P. J. Day *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); 1037ccd979bdSMark Fasheh if (!(*ac)) { 1038ccd979bdSMark Fasheh status = -ENOMEM; 1039ccd979bdSMark Fasheh mlog_errno(status); 1040ccd979bdSMark Fasheh goto bail; 1041ccd979bdSMark Fasheh } 1042ccd979bdSMark Fasheh 1043ccd979bdSMark Fasheh (*ac)->ac_bits_wanted = 1; 1044ccd979bdSMark Fasheh (*ac)->ac_which = OCFS2_AC_USE_INODE; 1045ccd979bdSMark Fasheh 1046ccd979bdSMark Fasheh (*ac)->ac_group_search = ocfs2_block_group_search; 1047ccd979bdSMark Fasheh 10484d0ddb2cSTao Ma /* 10491187c968SJoel Becker * stat(2) can't handle i_ino > 32bits, so we tell the 10501187c968SJoel Becker * lower levels not to allocate us a block group past that 105112462f1dSJoel Becker * limit. The 'inode64' mount option avoids this behavior. 10521187c968SJoel Becker */ 105312462f1dSJoel Becker if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64)) 10541187c968SJoel Becker (*ac)->ac_max_block = (u32)~0U; 10551187c968SJoel Becker 10561187c968SJoel Becker /* 10574d0ddb2cSTao Ma * slot is set when we successfully steal inode from other nodes. 10584d0ddb2cSTao Ma * It is reset in 3 places: 10594d0ddb2cSTao Ma * 1. when we flush the truncate log 10604d0ddb2cSTao Ma * 2. when we complete local alloc recovery. 10614d0ddb2cSTao Ma * 3. when we successfully allocate from our own slot. 10624d0ddb2cSTao Ma * After it is set, we will go on stealing inodes until we find the 10634d0ddb2cSTao Ma * need to check our slots to see whether there is some space for us. 10644d0ddb2cSTao Ma */ 10654d0ddb2cSTao Ma if (slot != OCFS2_INVALID_SLOT && 1066b89c5428STiger Yang atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL) 10674d0ddb2cSTao Ma goto inode_steal; 10684d0ddb2cSTao Ma 10694d0ddb2cSTao Ma atomic_set(&osb->s_num_inodes_stolen, 0); 1070feb473a6STao Ma alloc_group = osb->osb_inode_alloc_group; 1071da5cbf2fSMark Fasheh status = ocfs2_reserve_suballoc_bits(osb, *ac, 1072da5cbf2fSMark Fasheh INODE_ALLOC_SYSTEM_INODE, 1073b89c5428STiger Yang (u32)osb->slot_num, 1074feb473a6STao Ma &alloc_group, 107560ca81e8STao Ma ALLOC_NEW_GROUP | 107660ca81e8STao Ma ALLOC_GROUPS_FROM_GLOBAL); 10774d0ddb2cSTao Ma if (status >= 0) { 10784d0ddb2cSTao Ma status = 0; 10794d0ddb2cSTao Ma 1080feb473a6STao Ma spin_lock(&osb->osb_lock); 1081feb473a6STao Ma osb->osb_inode_alloc_group = alloc_group; 1082feb473a6STao Ma spin_unlock(&osb->osb_lock); 1083feb473a6STao Ma mlog(0, "after reservation, new allocation group is " 1084feb473a6STao Ma "%llu\n", (unsigned long long)alloc_group); 1085feb473a6STao Ma 10864d0ddb2cSTao Ma /* 10874d0ddb2cSTao Ma * Some inodes must be freed by us, so try to allocate 10884d0ddb2cSTao Ma * from our own next time. 10894d0ddb2cSTao Ma */ 10904d0ddb2cSTao Ma if (slot != OCFS2_INVALID_SLOT) 10914d0ddb2cSTao Ma ocfs2_init_inode_steal_slot(osb); 10924d0ddb2cSTao Ma goto bail; 10934d0ddb2cSTao Ma } else if (status < 0 && status != -ENOSPC) { 10944d0ddb2cSTao Ma mlog_errno(status); 10954d0ddb2cSTao Ma goto bail; 10964d0ddb2cSTao Ma } 10974d0ddb2cSTao Ma 10984d0ddb2cSTao Ma ocfs2_free_ac_resource(*ac); 10994d0ddb2cSTao Ma 11004d0ddb2cSTao Ma inode_steal: 1101b89c5428STiger Yang status = ocfs2_steal_inode(osb, *ac); 11024d0ddb2cSTao Ma atomic_inc(&osb->s_num_inodes_stolen); 1103ccd979bdSMark Fasheh if (status < 0) { 1104ccd979bdSMark Fasheh if (status != -ENOSPC) 1105ccd979bdSMark Fasheh mlog_errno(status); 1106ccd979bdSMark Fasheh goto bail; 1107ccd979bdSMark Fasheh } 1108ccd979bdSMark Fasheh 1109ccd979bdSMark Fasheh status = 0; 1110ccd979bdSMark Fasheh bail: 1111ccd979bdSMark Fasheh if ((status < 0) && *ac) { 1112ccd979bdSMark Fasheh ocfs2_free_alloc_context(*ac); 1113ccd979bdSMark Fasheh *ac = NULL; 1114ccd979bdSMark Fasheh } 1115ccd979bdSMark Fasheh 1116ccd979bdSMark Fasheh mlog_exit(status); 1117ccd979bdSMark Fasheh return status; 1118ccd979bdSMark Fasheh } 1119ccd979bdSMark Fasheh 1120ccd979bdSMark Fasheh /* local alloc code has to do the same thing, so rather than do this 1121ccd979bdSMark Fasheh * twice.. */ 1122ccd979bdSMark Fasheh int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb, 1123ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac) 1124ccd979bdSMark Fasheh { 1125ccd979bdSMark Fasheh int status; 1126ccd979bdSMark Fasheh 1127ccd979bdSMark Fasheh ac->ac_which = OCFS2_AC_USE_MAIN; 1128ccd979bdSMark Fasheh ac->ac_group_search = ocfs2_cluster_group_search; 1129ccd979bdSMark Fasheh 1130da5cbf2fSMark Fasheh status = ocfs2_reserve_suballoc_bits(osb, ac, 1131da5cbf2fSMark Fasheh GLOBAL_BITMAP_SYSTEM_INODE, 1132feb473a6STao Ma OCFS2_INVALID_SLOT, NULL, 1133ffda89a3STao Ma ALLOC_NEW_GROUP); 1134da5cbf2fSMark Fasheh if (status < 0 && status != -ENOSPC) { 1135ccd979bdSMark Fasheh mlog_errno(status); 1136da5cbf2fSMark Fasheh goto bail; 1137da5cbf2fSMark Fasheh } 1138da5cbf2fSMark Fasheh 1139ccd979bdSMark Fasheh bail: 1140ccd979bdSMark Fasheh return status; 1141ccd979bdSMark Fasheh } 1142ccd979bdSMark Fasheh 1143ccd979bdSMark Fasheh /* Callers don't need to care which bitmap (local alloc or main) to 1144ccd979bdSMark Fasheh * use so we figure it out for them, but unfortunately this clutters 1145ccd979bdSMark Fasheh * things a bit. */ 11461187c968SJoel Becker static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb, 11471187c968SJoel Becker u32 bits_wanted, u64 max_block, 114860ca81e8STao Ma int flags, 1149ccd979bdSMark Fasheh struct ocfs2_alloc_context **ac) 1150ccd979bdSMark Fasheh { 1151ccd979bdSMark Fasheh int status; 1152ccd979bdSMark Fasheh 1153ccd979bdSMark Fasheh mlog_entry_void(); 1154ccd979bdSMark Fasheh 1155cd861280SRobert P. J. Day *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); 1156ccd979bdSMark Fasheh if (!(*ac)) { 1157ccd979bdSMark Fasheh status = -ENOMEM; 1158ccd979bdSMark Fasheh mlog_errno(status); 1159ccd979bdSMark Fasheh goto bail; 1160ccd979bdSMark Fasheh } 1161ccd979bdSMark Fasheh 1162ccd979bdSMark Fasheh (*ac)->ac_bits_wanted = bits_wanted; 11631187c968SJoel Becker (*ac)->ac_max_block = max_block; 1164ccd979bdSMark Fasheh 1165ccd979bdSMark Fasheh status = -ENOSPC; 116660ca81e8STao Ma if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) && 116760ca81e8STao Ma ocfs2_alloc_should_use_local(osb, bits_wanted)) { 1168ccd979bdSMark Fasheh status = ocfs2_reserve_local_alloc_bits(osb, 1169ccd979bdSMark Fasheh bits_wanted, 1170ccd979bdSMark Fasheh *ac); 1171*a57c8fd2SMark Fasheh if ((status < 0) && (status != -ENOSPC)) { 1172ccd979bdSMark Fasheh mlog_errno(status); 1173ccd979bdSMark Fasheh goto bail; 1174ccd979bdSMark Fasheh } 1175ccd979bdSMark Fasheh } 1176ccd979bdSMark Fasheh 1177ccd979bdSMark Fasheh if (status == -ENOSPC) { 1178ccd979bdSMark Fasheh status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac); 1179ccd979bdSMark Fasheh if (status < 0) { 1180ccd979bdSMark Fasheh if (status != -ENOSPC) 1181ccd979bdSMark Fasheh mlog_errno(status); 1182ccd979bdSMark Fasheh goto bail; 1183ccd979bdSMark Fasheh } 1184ccd979bdSMark Fasheh } 1185ccd979bdSMark Fasheh 1186ccd979bdSMark Fasheh status = 0; 1187ccd979bdSMark Fasheh bail: 1188ccd979bdSMark Fasheh if ((status < 0) && *ac) { 1189ccd979bdSMark Fasheh ocfs2_free_alloc_context(*ac); 1190ccd979bdSMark Fasheh *ac = NULL; 1191ccd979bdSMark Fasheh } 1192ccd979bdSMark Fasheh 1193ccd979bdSMark Fasheh mlog_exit(status); 1194ccd979bdSMark Fasheh return status; 1195ccd979bdSMark Fasheh } 1196ccd979bdSMark Fasheh 11971187c968SJoel Becker int ocfs2_reserve_clusters(struct ocfs2_super *osb, 11981187c968SJoel Becker u32 bits_wanted, 11991187c968SJoel Becker struct ocfs2_alloc_context **ac) 12001187c968SJoel Becker { 120160ca81e8STao Ma return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0, 120260ca81e8STao Ma ALLOC_NEW_GROUP, ac); 12031187c968SJoel Becker } 12041187c968SJoel Becker 1205ccd979bdSMark Fasheh /* 1206ccd979bdSMark Fasheh * More or less lifted from ext3. I'll leave their description below: 1207ccd979bdSMark Fasheh * 1208ccd979bdSMark Fasheh * "For ext3 allocations, we must not reuse any blocks which are 1209ccd979bdSMark Fasheh * allocated in the bitmap buffer's "last committed data" copy. This 1210ccd979bdSMark Fasheh * prevents deletes from freeing up the page for reuse until we have 1211ccd979bdSMark Fasheh * committed the delete transaction. 1212ccd979bdSMark Fasheh * 1213ccd979bdSMark Fasheh * If we didn't do this, then deleting something and reallocating it as 1214ccd979bdSMark Fasheh * data would allow the old block to be overwritten before the 1215ccd979bdSMark Fasheh * transaction committed (because we force data to disk before commit). 1216ccd979bdSMark Fasheh * This would lead to corruption if we crashed between overwriting the 1217ccd979bdSMark Fasheh * data and committing the delete. 1218ccd979bdSMark Fasheh * 1219ccd979bdSMark Fasheh * @@@ We may want to make this allocation behaviour conditional on 1220ccd979bdSMark Fasheh * data-writes at some point, and disable it for metadata allocations or 1221ccd979bdSMark Fasheh * sync-data inodes." 1222ccd979bdSMark Fasheh * 1223ccd979bdSMark Fasheh * Note: OCFS2 already does this differently for metadata vs data 1224c78bad11SJoe Perches * allocations, as those bitmaps are separate and undo access is never 1225ccd979bdSMark Fasheh * called on a metadata group descriptor. 1226ccd979bdSMark Fasheh */ 1227ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh, 1228ccd979bdSMark Fasheh int nr) 1229ccd979bdSMark Fasheh { 1230ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 123194e41ecfSSunil Mushran int ret; 1232ccd979bdSMark Fasheh 1233ccd979bdSMark Fasheh if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap)) 1234ccd979bdSMark Fasheh return 0; 123594e41ecfSSunil Mushran 123694e41ecfSSunil Mushran if (!buffer_jbd(bg_bh)) 1237ccd979bdSMark Fasheh return 1; 1238ccd979bdSMark Fasheh 123994e41ecfSSunil Mushran jbd_lock_bh_state(bg_bh); 1240ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data; 124194e41ecfSSunil Mushran if (bg) 124294e41ecfSSunil Mushran ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap); 124394e41ecfSSunil Mushran else 124494e41ecfSSunil Mushran ret = 1; 124594e41ecfSSunil Mushran jbd_unlock_bh_state(bg_bh); 124694e41ecfSSunil Mushran 124794e41ecfSSunil Mushran return ret; 1248ccd979bdSMark Fasheh } 1249ccd979bdSMark Fasheh 1250ccd979bdSMark Fasheh static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb, 1251ccd979bdSMark Fasheh struct buffer_head *bg_bh, 1252ccd979bdSMark Fasheh unsigned int bits_wanted, 12537bf72edeSMark Fasheh unsigned int total_bits, 12547d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1255ccd979bdSMark Fasheh { 1256ccd979bdSMark Fasheh void *bitmap; 1257ccd979bdSMark Fasheh u16 best_offset, best_size; 1258ccd979bdSMark Fasheh int offset, start, found, status = 0; 1259ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 1260ccd979bdSMark Fasheh 126142035306SJoel Becker /* Callers got this descriptor from 126242035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 126342035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 1264ccd979bdSMark Fasheh 1265ccd979bdSMark Fasheh found = start = best_offset = best_size = 0; 1266ccd979bdSMark Fasheh bitmap = bg->bg_bitmap; 1267ccd979bdSMark Fasheh 12687bf72edeSMark Fasheh while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) { 12697bf72edeSMark Fasheh if (offset == total_bits) 1270ccd979bdSMark Fasheh break; 1271ccd979bdSMark Fasheh 1272ccd979bdSMark Fasheh if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) { 1273ccd979bdSMark Fasheh /* We found a zero, but we can't use it as it 1274ccd979bdSMark Fasheh * hasn't been put to disk yet! */ 1275ccd979bdSMark Fasheh found = 0; 1276ccd979bdSMark Fasheh start = offset + 1; 1277ccd979bdSMark Fasheh } else if (offset == start) { 1278ccd979bdSMark Fasheh /* we found a zero */ 1279ccd979bdSMark Fasheh found++; 1280ccd979bdSMark Fasheh /* move start to the next bit to test */ 1281ccd979bdSMark Fasheh start++; 1282ccd979bdSMark Fasheh } else { 1283ccd979bdSMark Fasheh /* got a zero after some ones */ 1284ccd979bdSMark Fasheh found = 1; 1285ccd979bdSMark Fasheh start = offset + 1; 1286ccd979bdSMark Fasheh } 1287ccd979bdSMark Fasheh if (found > best_size) { 1288ccd979bdSMark Fasheh best_size = found; 1289ccd979bdSMark Fasheh best_offset = start - found; 1290ccd979bdSMark Fasheh } 1291ccd979bdSMark Fasheh /* we got everything we needed */ 1292ccd979bdSMark Fasheh if (found == bits_wanted) { 1293ccd979bdSMark Fasheh /* mlog(0, "Found it all!\n"); */ 1294ccd979bdSMark Fasheh break; 1295ccd979bdSMark Fasheh } 1296ccd979bdSMark Fasheh } 1297ccd979bdSMark Fasheh 12987d1fe093SJoel Becker if (best_size) { 12997d1fe093SJoel Becker res->sr_bit_offset = best_offset; 13007d1fe093SJoel Becker res->sr_bits = best_size; 1301ccd979bdSMark Fasheh } else { 1302ccd979bdSMark Fasheh status = -ENOSPC; 1303ccd979bdSMark Fasheh /* No error log here -- see the comment above 1304ccd979bdSMark Fasheh * ocfs2_test_bg_bit_allocatable */ 1305ccd979bdSMark Fasheh } 1306ccd979bdSMark Fasheh 1307ccd979bdSMark Fasheh return status; 1308ccd979bdSMark Fasheh } 1309ccd979bdSMark Fasheh 13101fabe148SMark Fasheh static inline int ocfs2_block_group_set_bits(handle_t *handle, 1311ccd979bdSMark Fasheh struct inode *alloc_inode, 1312ccd979bdSMark Fasheh struct ocfs2_group_desc *bg, 1313ccd979bdSMark Fasheh struct buffer_head *group_bh, 1314ccd979bdSMark Fasheh unsigned int bit_off, 1315ccd979bdSMark Fasheh unsigned int num_bits) 1316ccd979bdSMark Fasheh { 1317ccd979bdSMark Fasheh int status; 1318ccd979bdSMark Fasheh void *bitmap = bg->bg_bitmap; 1319ccd979bdSMark Fasheh int journal_type = OCFS2_JOURNAL_ACCESS_WRITE; 1320ccd979bdSMark Fasheh 1321ccd979bdSMark Fasheh mlog_entry_void(); 1322ccd979bdSMark Fasheh 132342035306SJoel Becker /* All callers get the descriptor via 132442035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 132542035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 1326ccd979bdSMark Fasheh BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits); 1327ccd979bdSMark Fasheh 1328ccd979bdSMark Fasheh mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off, 1329ccd979bdSMark Fasheh num_bits); 1330ccd979bdSMark Fasheh 1331ccd979bdSMark Fasheh if (ocfs2_is_cluster_bitmap(alloc_inode)) 1332ccd979bdSMark Fasheh journal_type = OCFS2_JOURNAL_ACCESS_UNDO; 1333ccd979bdSMark Fasheh 133413723d00SJoel Becker status = ocfs2_journal_access_gd(handle, 13350cf2f763SJoel Becker INODE_CACHE(alloc_inode), 1336ccd979bdSMark Fasheh group_bh, 1337ccd979bdSMark Fasheh journal_type); 1338ccd979bdSMark Fasheh if (status < 0) { 1339ccd979bdSMark Fasheh mlog_errno(status); 1340ccd979bdSMark Fasheh goto bail; 1341ccd979bdSMark Fasheh } 1342ccd979bdSMark Fasheh 1343ccd979bdSMark Fasheh le16_add_cpu(&bg->bg_free_bits_count, -num_bits); 1344ccd979bdSMark Fasheh while(num_bits--) 1345ccd979bdSMark Fasheh ocfs2_set_bit(bit_off++, bitmap); 1346ccd979bdSMark Fasheh 1347ec20cec7SJoel Becker ocfs2_journal_dirty(handle, group_bh); 1348ccd979bdSMark Fasheh 1349ccd979bdSMark Fasheh bail: 1350ccd979bdSMark Fasheh mlog_exit(status); 1351ccd979bdSMark Fasheh return status; 1352ccd979bdSMark Fasheh } 1353ccd979bdSMark Fasheh 1354ccd979bdSMark Fasheh /* find the one with the most empty bits */ 1355ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl) 1356ccd979bdSMark Fasheh { 1357ccd979bdSMark Fasheh u16 curr, best; 1358ccd979bdSMark Fasheh 1359ccd979bdSMark Fasheh BUG_ON(!cl->cl_next_free_rec); 1360ccd979bdSMark Fasheh 1361ccd979bdSMark Fasheh best = curr = 0; 1362ccd979bdSMark Fasheh while (curr < le16_to_cpu(cl->cl_next_free_rec)) { 1363ccd979bdSMark Fasheh if (le32_to_cpu(cl->cl_recs[curr].c_free) > 1364ccd979bdSMark Fasheh le32_to_cpu(cl->cl_recs[best].c_free)) 1365ccd979bdSMark Fasheh best = curr; 1366ccd979bdSMark Fasheh curr++; 1367ccd979bdSMark Fasheh } 1368ccd979bdSMark Fasheh 1369ccd979bdSMark Fasheh BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec)); 1370ccd979bdSMark Fasheh return best; 1371ccd979bdSMark Fasheh } 1372ccd979bdSMark Fasheh 13731fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle, 1374ccd979bdSMark Fasheh struct inode *alloc_inode, 1375ccd979bdSMark Fasheh struct buffer_head *fe_bh, 1376ccd979bdSMark Fasheh struct buffer_head *bg_bh, 1377ccd979bdSMark Fasheh struct buffer_head *prev_bg_bh, 1378ccd979bdSMark Fasheh u16 chain) 1379ccd979bdSMark Fasheh { 1380ccd979bdSMark Fasheh int status; 1381ccd979bdSMark Fasheh /* there is a really tiny chance the journal calls could fail, 1382ccd979bdSMark Fasheh * but we wouldn't want inconsistent blocks in *any* case. */ 1383ccd979bdSMark Fasheh u64 fe_ptr, bg_ptr, prev_bg_ptr; 1384ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data; 1385ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 1386ccd979bdSMark Fasheh struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data; 1387ccd979bdSMark Fasheh 138842035306SJoel Becker /* The caller got these descriptors from 138942035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 139042035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 139142035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg)); 1392ccd979bdSMark Fasheh 1393b0697053SMark Fasheh mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n", 13941ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno), chain, 13951ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(bg->bg_blkno), 13961ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(prev_bg->bg_blkno)); 1397ccd979bdSMark Fasheh 1398ccd979bdSMark Fasheh fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno); 1399ccd979bdSMark Fasheh bg_ptr = le64_to_cpu(bg->bg_next_group); 1400ccd979bdSMark Fasheh prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group); 1401ccd979bdSMark Fasheh 14020cf2f763SJoel Becker status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), 14030cf2f763SJoel Becker prev_bg_bh, 1404ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 1405ccd979bdSMark Fasheh if (status < 0) { 1406ccd979bdSMark Fasheh mlog_errno(status); 1407ccd979bdSMark Fasheh goto out_rollback; 1408ccd979bdSMark Fasheh } 1409ccd979bdSMark Fasheh 1410ccd979bdSMark Fasheh prev_bg->bg_next_group = bg->bg_next_group; 1411ec20cec7SJoel Becker ocfs2_journal_dirty(handle, prev_bg_bh); 1412ccd979bdSMark Fasheh 14130cf2f763SJoel Becker status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), 14140cf2f763SJoel Becker bg_bh, OCFS2_JOURNAL_ACCESS_WRITE); 1415ccd979bdSMark Fasheh if (status < 0) { 1416ccd979bdSMark Fasheh mlog_errno(status); 1417ccd979bdSMark Fasheh goto out_rollback; 1418ccd979bdSMark Fasheh } 1419ccd979bdSMark Fasheh 1420ccd979bdSMark Fasheh bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno; 1421ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bg_bh); 1422ccd979bdSMark Fasheh 14230cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), 14240cf2f763SJoel Becker fe_bh, OCFS2_JOURNAL_ACCESS_WRITE); 1425ccd979bdSMark Fasheh if (status < 0) { 1426ccd979bdSMark Fasheh mlog_errno(status); 1427ccd979bdSMark Fasheh goto out_rollback; 1428ccd979bdSMark Fasheh } 1429ccd979bdSMark Fasheh 1430ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno; 1431ec20cec7SJoel Becker ocfs2_journal_dirty(handle, fe_bh); 1432ccd979bdSMark Fasheh 1433ccd979bdSMark Fasheh out_rollback: 1434ccd979bdSMark Fasheh if (status < 0) { 1435ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr); 1436ccd979bdSMark Fasheh bg->bg_next_group = cpu_to_le64(bg_ptr); 1437ccd979bdSMark Fasheh prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr); 1438ccd979bdSMark Fasheh } 143942035306SJoel Becker 1440ccd979bdSMark Fasheh mlog_exit(status); 1441ccd979bdSMark Fasheh return status; 1442ccd979bdSMark Fasheh } 1443ccd979bdSMark Fasheh 1444ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg, 1445ccd979bdSMark Fasheh u32 wanted) 1446ccd979bdSMark Fasheh { 1447ccd979bdSMark Fasheh return le16_to_cpu(bg->bg_free_bits_count) > wanted; 1448ccd979bdSMark Fasheh } 1449ccd979bdSMark Fasheh 1450ccd979bdSMark Fasheh /* return 0 on success, -ENOSPC to keep searching and any other < 0 1451ccd979bdSMark Fasheh * value on error. */ 1452ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode, 1453ccd979bdSMark Fasheh struct buffer_head *group_bh, 1454ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 14551187c968SJoel Becker u64 max_block, 14567d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1457ccd979bdSMark Fasheh { 1458ccd979bdSMark Fasheh int search = -ENOSPC; 1459ccd979bdSMark Fasheh int ret; 14601187c968SJoel Becker u64 blkoff; 14617bf72edeSMark Fasheh struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data; 14629c7af40bSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 14637bf72edeSMark Fasheh unsigned int max_bits, gd_cluster_off; 1464ccd979bdSMark Fasheh 1465ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 1466ccd979bdSMark Fasheh 14677bf72edeSMark Fasheh if (gd->bg_free_bits_count) { 14687bf72edeSMark Fasheh max_bits = le16_to_cpu(gd->bg_bits); 14697bf72edeSMark Fasheh 14707bf72edeSMark Fasheh /* Tail groups in cluster bitmaps which aren't cpg 14717bf72edeSMark Fasheh * aligned are prone to partial extention by a failed 14727bf72edeSMark Fasheh * fs resize. If the file system resize never got to 14737bf72edeSMark Fasheh * update the dinode cluster count, then we don't want 14747bf72edeSMark Fasheh * to trust any clusters past it, regardless of what 14757bf72edeSMark Fasheh * the group descriptor says. */ 14767bf72edeSMark Fasheh gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb, 14777bf72edeSMark Fasheh le64_to_cpu(gd->bg_blkno)); 14787bf72edeSMark Fasheh if ((gd_cluster_off + max_bits) > 14797bf72edeSMark Fasheh OCFS2_I(inode)->ip_clusters) { 14807bf72edeSMark Fasheh max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off; 14817bf72edeSMark Fasheh mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n", 14827bf72edeSMark Fasheh (unsigned long long)le64_to_cpu(gd->bg_blkno), 14837bf72edeSMark Fasheh le16_to_cpu(gd->bg_bits), 14847bf72edeSMark Fasheh OCFS2_I(inode)->ip_clusters, max_bits); 14857bf72edeSMark Fasheh } 14867bf72edeSMark Fasheh 1487ccd979bdSMark Fasheh ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb), 1488ccd979bdSMark Fasheh group_bh, bits_wanted, 14897d1fe093SJoel Becker max_bits, res); 1490ccd979bdSMark Fasheh if (ret) 1491ccd979bdSMark Fasheh return ret; 1492ccd979bdSMark Fasheh 14931187c968SJoel Becker if (max_block) { 14941187c968SJoel Becker blkoff = ocfs2_clusters_to_blocks(inode->i_sb, 14951187c968SJoel Becker gd_cluster_off + 14967d1fe093SJoel Becker res->sr_bit_offset + 14977d1fe093SJoel Becker res->sr_bits); 14981187c968SJoel Becker mlog(0, "Checking %llu against %llu\n", 14991187c968SJoel Becker (unsigned long long)blkoff, 15001187c968SJoel Becker (unsigned long long)max_block); 15011187c968SJoel Becker if (blkoff > max_block) 15021187c968SJoel Becker return -ENOSPC; 15031187c968SJoel Becker } 15041187c968SJoel Becker 1505ccd979bdSMark Fasheh /* ocfs2_block_group_find_clear_bits() might 1506ccd979bdSMark Fasheh * return success, but we still want to return 1507ccd979bdSMark Fasheh * -ENOSPC unless it found the minimum number 1508ccd979bdSMark Fasheh * of bits. */ 15097d1fe093SJoel Becker if (min_bits <= res->sr_bits) 1510ccd979bdSMark Fasheh search = 0; /* success */ 15117d1fe093SJoel Becker else if (res->sr_bits) { 15129c7af40bSMark Fasheh /* 15139c7af40bSMark Fasheh * Don't show bits which we'll be returning 15149c7af40bSMark Fasheh * for allocation to the local alloc bitmap. 15159c7af40bSMark Fasheh */ 15167d1fe093SJoel Becker ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits); 1517ccd979bdSMark Fasheh } 1518ccd979bdSMark Fasheh } 1519ccd979bdSMark Fasheh 1520ccd979bdSMark Fasheh return search; 1521ccd979bdSMark Fasheh } 1522ccd979bdSMark Fasheh 1523ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode, 1524ccd979bdSMark Fasheh struct buffer_head *group_bh, 1525ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 15261187c968SJoel Becker u64 max_block, 15277d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1528ccd979bdSMark Fasheh { 1529ccd979bdSMark Fasheh int ret = -ENOSPC; 15301187c968SJoel Becker u64 blkoff; 1531ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data; 1532ccd979bdSMark Fasheh 1533ccd979bdSMark Fasheh BUG_ON(min_bits != 1); 1534ccd979bdSMark Fasheh BUG_ON(ocfs2_is_cluster_bitmap(inode)); 1535ccd979bdSMark Fasheh 15361187c968SJoel Becker if (bg->bg_free_bits_count) { 1537ccd979bdSMark Fasheh ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb), 1538ccd979bdSMark Fasheh group_bh, bits_wanted, 15397bf72edeSMark Fasheh le16_to_cpu(bg->bg_bits), 15407d1fe093SJoel Becker res); 15411187c968SJoel Becker if (!ret && max_block) { 15427d1fe093SJoel Becker blkoff = le64_to_cpu(bg->bg_blkno) + 15437d1fe093SJoel Becker res->sr_bit_offset + res->sr_bits; 15441187c968SJoel Becker mlog(0, "Checking %llu against %llu\n", 15451187c968SJoel Becker (unsigned long long)blkoff, 15461187c968SJoel Becker (unsigned long long)max_block); 15471187c968SJoel Becker if (blkoff > max_block) 15481187c968SJoel Becker ret = -ENOSPC; 15491187c968SJoel Becker } 15501187c968SJoel Becker } 1551ccd979bdSMark Fasheh 1552ccd979bdSMark Fasheh return ret; 1553ccd979bdSMark Fasheh } 1554ccd979bdSMark Fasheh 1555883d4caeSMark Fasheh static int ocfs2_alloc_dinode_update_counts(struct inode *inode, 15561fabe148SMark Fasheh handle_t *handle, 1557883d4caeSMark Fasheh struct buffer_head *di_bh, 1558883d4caeSMark Fasheh u32 num_bits, 1559883d4caeSMark Fasheh u16 chain) 1560883d4caeSMark Fasheh { 1561883d4caeSMark Fasheh int ret; 1562883d4caeSMark Fasheh u32 tmp_used; 1563883d4caeSMark Fasheh struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; 1564883d4caeSMark Fasheh struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain; 1565883d4caeSMark Fasheh 15660cf2f763SJoel Becker ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 1567883d4caeSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 1568883d4caeSMark Fasheh if (ret < 0) { 1569883d4caeSMark Fasheh mlog_errno(ret); 1570883d4caeSMark Fasheh goto out; 1571883d4caeSMark Fasheh } 1572883d4caeSMark Fasheh 1573883d4caeSMark Fasheh tmp_used = le32_to_cpu(di->id1.bitmap1.i_used); 1574883d4caeSMark Fasheh di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used); 1575883d4caeSMark Fasheh le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits); 1576ec20cec7SJoel Becker ocfs2_journal_dirty(handle, di_bh); 1577883d4caeSMark Fasheh 1578883d4caeSMark Fasheh out: 1579883d4caeSMark Fasheh return ret; 1580883d4caeSMark Fasheh } 1581883d4caeSMark Fasheh 158213e434cfSJoel Becker static int ocfs2_bg_discontig_trim_by_rec(struct ocfs2_suballoc_result *res, 158313e434cfSJoel Becker struct ocfs2_extent_rec *rec, 158413e434cfSJoel Becker struct ocfs2_chain_list *cl) 158513e434cfSJoel Becker { 158613e434cfSJoel Becker unsigned int bpc = le16_to_cpu(cl->cl_bpc); 158713e434cfSJoel Becker unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc; 158813e434cfSJoel Becker unsigned int bitcount = le32_to_cpu(rec->e_leaf_clusters) * bpc; 158913e434cfSJoel Becker 159013e434cfSJoel Becker if (res->sr_bit_offset < bitoff) 159113e434cfSJoel Becker return 0; 159213e434cfSJoel Becker if (res->sr_bit_offset >= (bitoff + bitcount)) 159313e434cfSJoel Becker return 0; 159413e434cfSJoel Becker if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount)) 159513e434cfSJoel Becker res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset; 159613e434cfSJoel Becker return 1; 159713e434cfSJoel Becker } 159813e434cfSJoel Becker 159913e434cfSJoel Becker static void ocfs2_bg_discontig_trim_result(struct ocfs2_alloc_context *ac, 160013e434cfSJoel Becker struct ocfs2_group_desc *bg, 160113e434cfSJoel Becker struct ocfs2_suballoc_result *res) 160213e434cfSJoel Becker { 160313e434cfSJoel Becker int i; 160413e434cfSJoel Becker struct ocfs2_extent_rec *rec; 160513e434cfSJoel Becker struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data; 160613e434cfSJoel Becker struct ocfs2_chain_list *cl = &di->id2.i_chain; 160713e434cfSJoel Becker 160813e434cfSJoel Becker if (!ocfs2_supports_discontig_bh(OCFS2_SB(ac->ac_inode->i_sb))) 160913e434cfSJoel Becker return; 161013e434cfSJoel Becker 161113e434cfSJoel Becker if (ocfs2_is_cluster_bitmap(ac->ac_inode)) 161213e434cfSJoel Becker return; 161313e434cfSJoel Becker 161413e434cfSJoel Becker if (!bg->bg_list.l_next_free_rec) 161513e434cfSJoel Becker return; 161613e434cfSJoel Becker 161713e434cfSJoel Becker for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) { 161813e434cfSJoel Becker rec = &bg->bg_list.l_recs[i]; 161913e434cfSJoel Becker if (ocfs2_bg_discontig_trim_by_rec(res, rec, cl)) 162013e434cfSJoel Becker break; 162113e434cfSJoel Becker } 162213e434cfSJoel Becker } 162313e434cfSJoel Becker 1624883d4caeSMark Fasheh static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac, 16251fabe148SMark Fasheh handle_t *handle, 1626883d4caeSMark Fasheh u32 bits_wanted, 1627883d4caeSMark Fasheh u32 min_bits, 16287d1fe093SJoel Becker struct ocfs2_suballoc_result *res, 1629883d4caeSMark Fasheh u16 *bits_left) 1630883d4caeSMark Fasheh { 1631883d4caeSMark Fasheh int ret; 1632883d4caeSMark Fasheh struct buffer_head *group_bh = NULL; 1633883d4caeSMark Fasheh struct ocfs2_group_desc *gd; 163468f64d47SJoel Becker struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data; 1635883d4caeSMark Fasheh struct inode *alloc_inode = ac->ac_inode; 1636883d4caeSMark Fasheh 16377d1fe093SJoel Becker ret = ocfs2_read_group_descriptor(alloc_inode, di, 16387d1fe093SJoel Becker res->sr_bg_blkno, &group_bh); 1639883d4caeSMark Fasheh if (ret < 0) { 1640883d4caeSMark Fasheh mlog_errno(ret); 1641883d4caeSMark Fasheh return ret; 1642883d4caeSMark Fasheh } 1643883d4caeSMark Fasheh 1644883d4caeSMark Fasheh gd = (struct ocfs2_group_desc *) group_bh->b_data; 1645883d4caeSMark Fasheh ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits, 16467d1fe093SJoel Becker ac->ac_max_block, res); 1647883d4caeSMark Fasheh if (ret < 0) { 1648883d4caeSMark Fasheh if (ret != -ENOSPC) 1649883d4caeSMark Fasheh mlog_errno(ret); 1650883d4caeSMark Fasheh goto out; 1651883d4caeSMark Fasheh } 1652883d4caeSMark Fasheh 165313e434cfSJoel Becker if (!ret) 165413e434cfSJoel Becker ocfs2_bg_discontig_trim_result(ac, gd, res); 165513e434cfSJoel Becker 1656883d4caeSMark Fasheh ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh, 16577d1fe093SJoel Becker res->sr_bits, 1658883d4caeSMark Fasheh le16_to_cpu(gd->bg_chain)); 1659883d4caeSMark Fasheh if (ret < 0) { 1660883d4caeSMark Fasheh mlog_errno(ret); 1661883d4caeSMark Fasheh goto out; 1662883d4caeSMark Fasheh } 1663883d4caeSMark Fasheh 1664883d4caeSMark Fasheh ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh, 16657d1fe093SJoel Becker res->sr_bit_offset, res->sr_bits); 1666883d4caeSMark Fasheh if (ret < 0) 1667883d4caeSMark Fasheh mlog_errno(ret); 1668883d4caeSMark Fasheh 1669883d4caeSMark Fasheh *bits_left = le16_to_cpu(gd->bg_free_bits_count); 1670883d4caeSMark Fasheh 1671883d4caeSMark Fasheh out: 1672883d4caeSMark Fasheh brelse(group_bh); 1673883d4caeSMark Fasheh 1674883d4caeSMark Fasheh return ret; 1675883d4caeSMark Fasheh } 1676883d4caeSMark Fasheh 1677ccd979bdSMark Fasheh static int ocfs2_search_chain(struct ocfs2_alloc_context *ac, 16781fabe148SMark Fasheh handle_t *handle, 1679ccd979bdSMark Fasheh u32 bits_wanted, 1680ccd979bdSMark Fasheh u32 min_bits, 16817d1fe093SJoel Becker struct ocfs2_suballoc_result *res, 1682883d4caeSMark Fasheh u16 *bits_left) 1683ccd979bdSMark Fasheh { 1684ccd979bdSMark Fasheh int status; 16857d1fe093SJoel Becker u16 chain; 1686ccd979bdSMark Fasheh u32 tmp_used; 1687ccd979bdSMark Fasheh u64 next_group; 1688ccd979bdSMark Fasheh struct inode *alloc_inode = ac->ac_inode; 1689ccd979bdSMark Fasheh struct buffer_head *group_bh = NULL; 1690ccd979bdSMark Fasheh struct buffer_head *prev_group_bh = NULL; 1691ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data; 1692ccd979bdSMark Fasheh struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain; 1693ccd979bdSMark Fasheh struct ocfs2_group_desc *bg; 1694ccd979bdSMark Fasheh 1695ccd979bdSMark Fasheh chain = ac->ac_chain; 1696b0697053SMark Fasheh mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n", 1697b0697053SMark Fasheh bits_wanted, chain, 1698b0697053SMark Fasheh (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno); 1699ccd979bdSMark Fasheh 170068f64d47SJoel Becker status = ocfs2_read_group_descriptor(alloc_inode, fe, 1701ccd979bdSMark Fasheh le64_to_cpu(cl->cl_recs[chain].c_blkno), 17020fcaa56aSJoel Becker &group_bh); 1703ccd979bdSMark Fasheh if (status < 0) { 1704ccd979bdSMark Fasheh mlog_errno(status); 1705ccd979bdSMark Fasheh goto bail; 1706ccd979bdSMark Fasheh } 1707ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) group_bh->b_data; 1708ccd979bdSMark Fasheh 1709ccd979bdSMark Fasheh status = -ENOSPC; 1710ccd979bdSMark Fasheh /* for now, the chain search is a bit simplistic. We just use 1711ccd979bdSMark Fasheh * the 1st group with any empty bits. */ 17121187c968SJoel Becker while ((status = ac->ac_group_search(alloc_inode, group_bh, 17131187c968SJoel Becker bits_wanted, min_bits, 17147d1fe093SJoel Becker ac->ac_max_block, 17157d1fe093SJoel Becker res)) == -ENOSPC) { 1716ccd979bdSMark Fasheh if (!bg->bg_next_group) 1717ccd979bdSMark Fasheh break; 1718a81cb88bSMark Fasheh 1719ccd979bdSMark Fasheh brelse(prev_group_bh); 1720ccd979bdSMark Fasheh prev_group_bh = NULL; 1721a81cb88bSMark Fasheh 1722ccd979bdSMark Fasheh next_group = le64_to_cpu(bg->bg_next_group); 1723ccd979bdSMark Fasheh prev_group_bh = group_bh; 1724ccd979bdSMark Fasheh group_bh = NULL; 172568f64d47SJoel Becker status = ocfs2_read_group_descriptor(alloc_inode, fe, 17260fcaa56aSJoel Becker next_group, &group_bh); 1727ccd979bdSMark Fasheh if (status < 0) { 1728ccd979bdSMark Fasheh mlog_errno(status); 1729ccd979bdSMark Fasheh goto bail; 1730ccd979bdSMark Fasheh } 1731ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) group_bh->b_data; 1732ccd979bdSMark Fasheh } 1733ccd979bdSMark Fasheh if (status < 0) { 1734ccd979bdSMark Fasheh if (status != -ENOSPC) 1735ccd979bdSMark Fasheh mlog_errno(status); 1736ccd979bdSMark Fasheh goto bail; 1737ccd979bdSMark Fasheh } 1738ccd979bdSMark Fasheh 1739b0697053SMark Fasheh mlog(0, "alloc succeeds: we give %u bits from block group %llu\n", 17407d1fe093SJoel Becker res->sr_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno)); 1741ccd979bdSMark Fasheh 17427d1fe093SJoel Becker res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno); 1743ccd979bdSMark Fasheh 17447d1fe093SJoel Becker BUG_ON(res->sr_bits == 0); 174513e434cfSJoel Becker if (!status) 174613e434cfSJoel Becker ocfs2_bg_discontig_trim_result(ac, bg, res); 174713e434cfSJoel Becker 1748ccd979bdSMark Fasheh 1749ccd979bdSMark Fasheh /* 1750ccd979bdSMark Fasheh * Keep track of previous block descriptor read. When 1751ccd979bdSMark Fasheh * we find a target, if we have read more than X 1752ccd979bdSMark Fasheh * number of descriptors, and the target is reasonably 1753ccd979bdSMark Fasheh * empty, relink him to top of his chain. 1754ccd979bdSMark Fasheh * 1755ccd979bdSMark Fasheh * We've read 0 extra blocks and only send one more to 1756ccd979bdSMark Fasheh * the transaction, yet the next guy to search has a 1757ccd979bdSMark Fasheh * much easier time. 1758ccd979bdSMark Fasheh * 1759ccd979bdSMark Fasheh * Do this *after* figuring out how many bits we're taking out 1760ccd979bdSMark Fasheh * of our target group. 1761ccd979bdSMark Fasheh */ 1762ccd979bdSMark Fasheh if (ac->ac_allow_chain_relink && 1763ccd979bdSMark Fasheh (prev_group_bh) && 17647d1fe093SJoel Becker (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) { 1765ccd979bdSMark Fasheh status = ocfs2_relink_block_group(handle, alloc_inode, 1766ccd979bdSMark Fasheh ac->ac_bh, group_bh, 1767ccd979bdSMark Fasheh prev_group_bh, chain); 1768ccd979bdSMark Fasheh if (status < 0) { 1769ccd979bdSMark Fasheh mlog_errno(status); 1770ccd979bdSMark Fasheh goto bail; 1771ccd979bdSMark Fasheh } 1772ccd979bdSMark Fasheh } 1773ccd979bdSMark Fasheh 1774ccd979bdSMark Fasheh /* Ok, claim our bits now: set the info on dinode, chainlist 1775ccd979bdSMark Fasheh * and then the group */ 177613723d00SJoel Becker status = ocfs2_journal_access_di(handle, 17770cf2f763SJoel Becker INODE_CACHE(alloc_inode), 1778ccd979bdSMark Fasheh ac->ac_bh, 1779ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 1780ccd979bdSMark Fasheh if (status < 0) { 1781ccd979bdSMark Fasheh mlog_errno(status); 1782ccd979bdSMark Fasheh goto bail; 1783ccd979bdSMark Fasheh } 1784ccd979bdSMark Fasheh 1785ccd979bdSMark Fasheh tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used); 17867d1fe093SJoel Becker fe->id1.bitmap1.i_used = cpu_to_le32(res->sr_bits + tmp_used); 17877d1fe093SJoel Becker le32_add_cpu(&cl->cl_recs[chain].c_free, -res->sr_bits); 1788ec20cec7SJoel Becker ocfs2_journal_dirty(handle, ac->ac_bh); 1789ccd979bdSMark Fasheh 1790ccd979bdSMark Fasheh status = ocfs2_block_group_set_bits(handle, 1791ccd979bdSMark Fasheh alloc_inode, 1792ccd979bdSMark Fasheh bg, 1793ccd979bdSMark Fasheh group_bh, 17947d1fe093SJoel Becker res->sr_bit_offset, 17957d1fe093SJoel Becker res->sr_bits); 1796ccd979bdSMark Fasheh if (status < 0) { 1797ccd979bdSMark Fasheh mlog_errno(status); 1798ccd979bdSMark Fasheh goto bail; 1799ccd979bdSMark Fasheh } 1800ccd979bdSMark Fasheh 18017d1fe093SJoel Becker mlog(0, "Allocated %u bits from suballocator %llu\n", res->sr_bits, 18021ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno)); 1803ccd979bdSMark Fasheh 1804883d4caeSMark Fasheh *bits_left = le16_to_cpu(bg->bg_free_bits_count); 1805ccd979bdSMark Fasheh bail: 1806ccd979bdSMark Fasheh brelse(group_bh); 1807ccd979bdSMark Fasheh brelse(prev_group_bh); 1808ccd979bdSMark Fasheh 1809ccd979bdSMark Fasheh mlog_exit(status); 1810ccd979bdSMark Fasheh return status; 1811ccd979bdSMark Fasheh } 1812ccd979bdSMark Fasheh 1813ccd979bdSMark Fasheh /* will give out up to bits_wanted contiguous bits. */ 1814aa8f8e93SJoel Becker static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac, 18151fabe148SMark Fasheh handle_t *handle, 1816ccd979bdSMark Fasheh u32 bits_wanted, 1817ccd979bdSMark Fasheh u32 min_bits, 18187d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1819ccd979bdSMark Fasheh { 1820ccd979bdSMark Fasheh int status; 1821ccd979bdSMark Fasheh u16 victim, i; 1822883d4caeSMark Fasheh u16 bits_left = 0; 1823ccd979bdSMark Fasheh struct ocfs2_chain_list *cl; 1824ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 1825ccd979bdSMark Fasheh 1826ccd979bdSMark Fasheh mlog_entry_void(); 1827ccd979bdSMark Fasheh 1828ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted); 1829ccd979bdSMark Fasheh BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given)); 1830ccd979bdSMark Fasheh BUG_ON(!ac->ac_bh); 1831ccd979bdSMark Fasheh 1832ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) ac->ac_bh->b_data; 183310995aa2SJoel Becker 183410995aa2SJoel Becker /* The bh was validated by the inode read during 183510995aa2SJoel Becker * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */ 183610995aa2SJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); 183710995aa2SJoel Becker 1838ccd979bdSMark Fasheh if (le32_to_cpu(fe->id1.bitmap1.i_used) >= 1839ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_total)) { 1840aa8f8e93SJoel Becker ocfs2_error(ac->ac_inode->i_sb, 1841aa8f8e93SJoel Becker "Chain allocator dinode %llu has %u used " 1842b0697053SMark Fasheh "bits but only %u total.", 1843b0697053SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno), 1844ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_used), 1845ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_total)); 1846ccd979bdSMark Fasheh status = -EIO; 1847ccd979bdSMark Fasheh goto bail; 1848ccd979bdSMark Fasheh } 1849ccd979bdSMark Fasheh 18507d1fe093SJoel Becker res->sr_bg_blkno = ac->ac_last_group; 18517d1fe093SJoel Becker if (res->sr_bg_blkno) { 1852883d4caeSMark Fasheh /* Attempt to short-circuit the usual search mechanism 1853883d4caeSMark Fasheh * by jumping straight to the most recently used 1854883d4caeSMark Fasheh * allocation group. This helps us mantain some 1855883d4caeSMark Fasheh * contiguousness across allocations. */ 1856da5cbf2fSMark Fasheh status = ocfs2_search_one_group(ac, handle, bits_wanted, 18577d1fe093SJoel Becker min_bits, res, &bits_left); 18587d1fe093SJoel Becker if (!status) 1859883d4caeSMark Fasheh goto set_hint; 1860883d4caeSMark Fasheh if (status < 0 && status != -ENOSPC) { 1861883d4caeSMark Fasheh mlog_errno(status); 1862883d4caeSMark Fasheh goto bail; 1863883d4caeSMark Fasheh } 1864883d4caeSMark Fasheh } 1865883d4caeSMark Fasheh 1866ccd979bdSMark Fasheh cl = (struct ocfs2_chain_list *) &fe->id2.i_chain; 1867ccd979bdSMark Fasheh 1868ccd979bdSMark Fasheh victim = ocfs2_find_victim_chain(cl); 1869ccd979bdSMark Fasheh ac->ac_chain = victim; 1870ccd979bdSMark Fasheh ac->ac_allow_chain_relink = 1; 1871ccd979bdSMark Fasheh 18727d1fe093SJoel Becker status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, 18737d1fe093SJoel Becker res, &bits_left); 1874ccd979bdSMark Fasheh if (!status) 1875883d4caeSMark Fasheh goto set_hint; 1876ccd979bdSMark Fasheh if (status < 0 && status != -ENOSPC) { 1877ccd979bdSMark Fasheh mlog_errno(status); 1878ccd979bdSMark Fasheh goto bail; 1879ccd979bdSMark Fasheh } 1880ccd979bdSMark Fasheh 1881ccd979bdSMark Fasheh mlog(0, "Search of victim chain %u came up with nothing, " 1882ccd979bdSMark Fasheh "trying all chains now.\n", victim); 1883ccd979bdSMark Fasheh 1884ccd979bdSMark Fasheh /* If we didn't pick a good victim, then just default to 1885ccd979bdSMark Fasheh * searching each chain in order. Don't allow chain relinking 1886ccd979bdSMark Fasheh * because we only calculate enough journal credits for one 1887ccd979bdSMark Fasheh * relink per alloc. */ 1888ccd979bdSMark Fasheh ac->ac_allow_chain_relink = 0; 1889ccd979bdSMark Fasheh for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) { 1890ccd979bdSMark Fasheh if (i == victim) 1891ccd979bdSMark Fasheh continue; 1892ccd979bdSMark Fasheh if (!cl->cl_recs[i].c_free) 1893ccd979bdSMark Fasheh continue; 1894ccd979bdSMark Fasheh 1895ccd979bdSMark Fasheh ac->ac_chain = i; 1896da5cbf2fSMark Fasheh status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, 18977d1fe093SJoel Becker res, &bits_left); 1898ccd979bdSMark Fasheh if (!status) 1899ccd979bdSMark Fasheh break; 1900ccd979bdSMark Fasheh if (status < 0 && status != -ENOSPC) { 1901ccd979bdSMark Fasheh mlog_errno(status); 1902ccd979bdSMark Fasheh goto bail; 1903ccd979bdSMark Fasheh } 1904ccd979bdSMark Fasheh } 1905ccd979bdSMark Fasheh 1906883d4caeSMark Fasheh set_hint: 1907883d4caeSMark Fasheh if (status != -ENOSPC) { 1908883d4caeSMark Fasheh /* If the next search of this group is not likely to 1909883d4caeSMark Fasheh * yield a suitable extent, then we reset the last 1910883d4caeSMark Fasheh * group hint so as to not waste a disk read */ 1911883d4caeSMark Fasheh if (bits_left < min_bits) 1912883d4caeSMark Fasheh ac->ac_last_group = 0; 1913883d4caeSMark Fasheh else 19147d1fe093SJoel Becker ac->ac_last_group = res->sr_bg_blkno; 1915883d4caeSMark Fasheh } 1916883d4caeSMark Fasheh 1917883d4caeSMark Fasheh bail: 1918ccd979bdSMark Fasheh mlog_exit(status); 1919ccd979bdSMark Fasheh return status; 1920ccd979bdSMark Fasheh } 1921ccd979bdSMark Fasheh 19221ed9b777SJoel Becker int ocfs2_claim_metadata(handle_t *handle, 1923ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac, 1924ccd979bdSMark Fasheh u32 bits_wanted, 1925ccd979bdSMark Fasheh u16 *suballoc_bit_start, 1926ccd979bdSMark Fasheh unsigned int *num_bits, 1927ccd979bdSMark Fasheh u64 *blkno_start) 1928ccd979bdSMark Fasheh { 1929ccd979bdSMark Fasheh int status; 19307d1fe093SJoel Becker struct ocfs2_suballoc_result res; 1931ccd979bdSMark Fasheh 1932ccd979bdSMark Fasheh BUG_ON(!ac); 1933ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted)); 1934ccd979bdSMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_META); 1935ccd979bdSMark Fasheh 1936aa8f8e93SJoel Becker status = ocfs2_claim_suballoc_bits(ac, 1937da5cbf2fSMark Fasheh handle, 1938ccd979bdSMark Fasheh bits_wanted, 1939ccd979bdSMark Fasheh 1, 19407d1fe093SJoel Becker &res); 1941ccd979bdSMark Fasheh if (status < 0) { 1942ccd979bdSMark Fasheh mlog_errno(status); 1943ccd979bdSMark Fasheh goto bail; 1944ccd979bdSMark Fasheh } 19451ed9b777SJoel Becker atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs); 1946ccd979bdSMark Fasheh 19477d1fe093SJoel Becker *suballoc_bit_start = res.sr_bit_offset; 19487d1fe093SJoel Becker *blkno_start = res.sr_bg_blkno + (u64)(res.sr_bit_offset); 19497d1fe093SJoel Becker ac->ac_bits_given += res.sr_bits; 19507d1fe093SJoel Becker *num_bits = res.sr_bits; 1951ccd979bdSMark Fasheh status = 0; 1952ccd979bdSMark Fasheh bail: 1953ccd979bdSMark Fasheh mlog_exit(status); 1954ccd979bdSMark Fasheh return status; 1955ccd979bdSMark Fasheh } 1956ccd979bdSMark Fasheh 195713821151STao Ma static void ocfs2_init_inode_ac_group(struct inode *dir, 195813821151STao Ma struct buffer_head *parent_fe_bh, 195913821151STao Ma struct ocfs2_alloc_context *ac) 196013821151STao Ma { 196113821151STao Ma struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data; 196213821151STao Ma /* 196313821151STao Ma * Try to allocate inodes from some specific group. 196413821151STao Ma * 196513821151STao Ma * If the parent dir has recorded the last group used in allocation, 196613821151STao Ma * cool, use it. Otherwise if we try to allocate new inode from the 196713821151STao Ma * same slot the parent dir belongs to, use the same chunk. 196813821151STao Ma * 196913821151STao Ma * We are very careful here to avoid the mistake of setting 197013821151STao Ma * ac_last_group to a group descriptor from a different (unlocked) slot. 197113821151STao Ma */ 197213821151STao Ma if (OCFS2_I(dir)->ip_last_used_group && 197313821151STao Ma OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot) 197413821151STao Ma ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group; 197513821151STao Ma else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot) 197613821151STao Ma ac->ac_last_group = ocfs2_which_suballoc_group( 197713821151STao Ma le64_to_cpu(fe->i_blkno), 197813821151STao Ma le16_to_cpu(fe->i_suballoc_bit)); 197913821151STao Ma } 198013821151STao Ma 198113821151STao Ma static inline void ocfs2_save_inode_ac_group(struct inode *dir, 198213821151STao Ma struct ocfs2_alloc_context *ac) 198313821151STao Ma { 198413821151STao Ma OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group; 198513821151STao Ma OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot; 198613821151STao Ma } 198713821151STao Ma 19881ed9b777SJoel Becker int ocfs2_claim_new_inode(handle_t *handle, 198913821151STao Ma struct inode *dir, 199013821151STao Ma struct buffer_head *parent_fe_bh, 1991ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac, 1992ccd979bdSMark Fasheh u16 *suballoc_bit, 1993ccd979bdSMark Fasheh u64 *fe_blkno) 1994ccd979bdSMark Fasheh { 1995ccd979bdSMark Fasheh int status; 19967d1fe093SJoel Becker struct ocfs2_suballoc_result res; 1997ccd979bdSMark Fasheh 1998ccd979bdSMark Fasheh mlog_entry_void(); 1999ccd979bdSMark Fasheh 2000ccd979bdSMark Fasheh BUG_ON(!ac); 2001ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_given != 0); 2002ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_wanted != 1); 2003ccd979bdSMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE); 2004ccd979bdSMark Fasheh 200513821151STao Ma ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac); 200613821151STao Ma 2007aa8f8e93SJoel Becker status = ocfs2_claim_suballoc_bits(ac, 2008da5cbf2fSMark Fasheh handle, 2009ccd979bdSMark Fasheh 1, 2010ccd979bdSMark Fasheh 1, 20117d1fe093SJoel Becker &res); 2012ccd979bdSMark Fasheh if (status < 0) { 2013ccd979bdSMark Fasheh mlog_errno(status); 2014ccd979bdSMark Fasheh goto bail; 2015ccd979bdSMark Fasheh } 20161ed9b777SJoel Becker atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs); 2017ccd979bdSMark Fasheh 20187d1fe093SJoel Becker BUG_ON(res.sr_bits != 1); 2019ccd979bdSMark Fasheh 20207d1fe093SJoel Becker *suballoc_bit = res.sr_bit_offset; 20217d1fe093SJoel Becker *fe_blkno = res.sr_bg_blkno + (u64)(res.sr_bit_offset); 2022ccd979bdSMark Fasheh ac->ac_bits_given++; 202313821151STao Ma ocfs2_save_inode_ac_group(dir, ac); 2024ccd979bdSMark Fasheh status = 0; 2025ccd979bdSMark Fasheh bail: 2026ccd979bdSMark Fasheh mlog_exit(status); 2027ccd979bdSMark Fasheh return status; 2028ccd979bdSMark Fasheh } 2029ccd979bdSMark Fasheh 2030ccd979bdSMark Fasheh /* translate a group desc. blkno and it's bitmap offset into 2031ccd979bdSMark Fasheh * disk cluster offset. */ 2032ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode, 2033ccd979bdSMark Fasheh u64 bg_blkno, 2034ccd979bdSMark Fasheh u16 bg_bit_off) 2035ccd979bdSMark Fasheh { 2036ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2037ccd979bdSMark Fasheh u32 cluster = 0; 2038ccd979bdSMark Fasheh 2039ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 2040ccd979bdSMark Fasheh 2041ccd979bdSMark Fasheh if (bg_blkno != osb->first_cluster_group_blkno) 2042ccd979bdSMark Fasheh cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno); 2043ccd979bdSMark Fasheh cluster += (u32) bg_bit_off; 2044ccd979bdSMark Fasheh return cluster; 2045ccd979bdSMark Fasheh } 2046ccd979bdSMark Fasheh 2047ccd979bdSMark Fasheh /* given a cluster offset, calculate which block group it belongs to 2048ccd979bdSMark Fasheh * and return that block offset. */ 2049d659072fSTao Ma u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster) 2050ccd979bdSMark Fasheh { 2051ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2052ccd979bdSMark Fasheh u32 group_no; 2053ccd979bdSMark Fasheh 2054ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 2055ccd979bdSMark Fasheh 2056ccd979bdSMark Fasheh group_no = cluster / osb->bitmap_cpg; 2057ccd979bdSMark Fasheh if (!group_no) 2058ccd979bdSMark Fasheh return osb->first_cluster_group_blkno; 2059ccd979bdSMark Fasheh return ocfs2_clusters_to_blocks(inode->i_sb, 2060ccd979bdSMark Fasheh group_no * osb->bitmap_cpg); 2061ccd979bdSMark Fasheh } 2062ccd979bdSMark Fasheh 2063ccd979bdSMark Fasheh /* given the block number of a cluster start, calculate which cluster 2064ccd979bdSMark Fasheh * group and descriptor bitmap offset that corresponds to. */ 2065ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode, 2066ccd979bdSMark Fasheh u64 data_blkno, 2067ccd979bdSMark Fasheh u64 *bg_blkno, 2068ccd979bdSMark Fasheh u16 *bg_bit_off) 2069ccd979bdSMark Fasheh { 2070ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2071ccd979bdSMark Fasheh u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno); 2072ccd979bdSMark Fasheh 2073ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 2074ccd979bdSMark Fasheh 2075ccd979bdSMark Fasheh *bg_blkno = ocfs2_which_cluster_group(inode, 2076ccd979bdSMark Fasheh data_cluster); 2077ccd979bdSMark Fasheh 2078ccd979bdSMark Fasheh if (*bg_blkno == osb->first_cluster_group_blkno) 2079ccd979bdSMark Fasheh *bg_bit_off = (u16) data_cluster; 2080ccd979bdSMark Fasheh else 2081ccd979bdSMark Fasheh *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb, 2082ccd979bdSMark Fasheh data_blkno - *bg_blkno); 2083ccd979bdSMark Fasheh } 2084ccd979bdSMark Fasheh 2085ccd979bdSMark Fasheh /* 2086ccd979bdSMark Fasheh * min_bits - minimum contiguous chunk from this total allocation we 2087ccd979bdSMark Fasheh * can handle. set to what we asked for originally for a full 2088ccd979bdSMark Fasheh * contig. allocation, set to '1' to indicate we can deal with extents 2089ccd979bdSMark Fasheh * of any size. 2090ccd979bdSMark Fasheh */ 20911ed9b777SJoel Becker int __ocfs2_claim_clusters(handle_t *handle, 2092ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac, 2093ccd979bdSMark Fasheh u32 min_clusters, 2094415cb800SMark Fasheh u32 max_clusters, 2095ccd979bdSMark Fasheh u32 *cluster_start, 2096ccd979bdSMark Fasheh u32 *num_clusters) 2097ccd979bdSMark Fasheh { 2098ccd979bdSMark Fasheh int status; 2099415cb800SMark Fasheh unsigned int bits_wanted = max_clusters; 21007d1fe093SJoel Becker struct ocfs2_suballoc_result res; 21011ed9b777SJoel Becker struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb); 2102ccd979bdSMark Fasheh 2103ccd979bdSMark Fasheh mlog_entry_void(); 2104ccd979bdSMark Fasheh 2105ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted); 2106ccd979bdSMark Fasheh 2107ccd979bdSMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL 2108ccd979bdSMark Fasheh && ac->ac_which != OCFS2_AC_USE_MAIN); 2109ccd979bdSMark Fasheh 2110ccd979bdSMark Fasheh if (ac->ac_which == OCFS2_AC_USE_LOCAL) { 211133d5d380SMark Fasheh WARN_ON(min_clusters > 1); 211233d5d380SMark Fasheh 2113ccd979bdSMark Fasheh status = ocfs2_claim_local_alloc_bits(osb, 2114ccd979bdSMark Fasheh handle, 2115ccd979bdSMark Fasheh ac, 2116ccd979bdSMark Fasheh bits_wanted, 2117ccd979bdSMark Fasheh cluster_start, 2118ccd979bdSMark Fasheh num_clusters); 2119ccd979bdSMark Fasheh if (!status) 2120ccd979bdSMark Fasheh atomic_inc(&osb->alloc_stats.local_data); 2121ccd979bdSMark Fasheh } else { 2122ccd979bdSMark Fasheh if (min_clusters > (osb->bitmap_cpg - 1)) { 2123ccd979bdSMark Fasheh /* The only paths asking for contiguousness 2124ccd979bdSMark Fasheh * should know about this already. */ 21252fbe8d1eSSunil Mushran mlog(ML_ERROR, "minimum allocation requested %u exceeds " 21262fbe8d1eSSunil Mushran "group bitmap size %u!\n", min_clusters, 21272fbe8d1eSSunil Mushran osb->bitmap_cpg); 2128ccd979bdSMark Fasheh status = -ENOSPC; 2129ccd979bdSMark Fasheh goto bail; 2130ccd979bdSMark Fasheh } 2131ccd979bdSMark Fasheh /* clamp the current request down to a realistic size. */ 2132ccd979bdSMark Fasheh if (bits_wanted > (osb->bitmap_cpg - 1)) 2133ccd979bdSMark Fasheh bits_wanted = osb->bitmap_cpg - 1; 2134ccd979bdSMark Fasheh 2135aa8f8e93SJoel Becker status = ocfs2_claim_suballoc_bits(ac, 2136da5cbf2fSMark Fasheh handle, 2137ccd979bdSMark Fasheh bits_wanted, 2138ccd979bdSMark Fasheh min_clusters, 21397d1fe093SJoel Becker &res); 2140ccd979bdSMark Fasheh if (!status) { 2141ccd979bdSMark Fasheh *cluster_start = 2142ccd979bdSMark Fasheh ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode, 21437d1fe093SJoel Becker res.sr_bg_blkno, 21447d1fe093SJoel Becker res.sr_bit_offset); 2145ccd979bdSMark Fasheh atomic_inc(&osb->alloc_stats.bitmap_data); 2146ccd979bdSMark Fasheh } 2147ccd979bdSMark Fasheh } 2148ccd979bdSMark Fasheh if (status < 0) { 2149ccd979bdSMark Fasheh if (status != -ENOSPC) 2150ccd979bdSMark Fasheh mlog_errno(status); 2151ccd979bdSMark Fasheh goto bail; 2152ccd979bdSMark Fasheh } 2153ccd979bdSMark Fasheh 21547d1fe093SJoel Becker ac->ac_bits_given += res.sr_bits; 21557d1fe093SJoel Becker *num_clusters = res.sr_bits; 2156ccd979bdSMark Fasheh 2157ccd979bdSMark Fasheh bail: 2158ccd979bdSMark Fasheh mlog_exit(status); 2159ccd979bdSMark Fasheh return status; 2160ccd979bdSMark Fasheh } 2161ccd979bdSMark Fasheh 21621ed9b777SJoel Becker int ocfs2_claim_clusters(handle_t *handle, 2163415cb800SMark Fasheh struct ocfs2_alloc_context *ac, 2164415cb800SMark Fasheh u32 min_clusters, 2165415cb800SMark Fasheh u32 *cluster_start, 2166415cb800SMark Fasheh u32 *num_clusters) 2167415cb800SMark Fasheh { 2168415cb800SMark Fasheh unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given; 2169415cb800SMark Fasheh 21701ed9b777SJoel Becker return __ocfs2_claim_clusters(handle, ac, min_clusters, 2171415cb800SMark Fasheh bits_wanted, cluster_start, num_clusters); 2172415cb800SMark Fasheh } 2173415cb800SMark Fasheh 2174b4414eeaSMark Fasheh static int ocfs2_block_group_clear_bits(handle_t *handle, 2175ccd979bdSMark Fasheh struct inode *alloc_inode, 2176ccd979bdSMark Fasheh struct ocfs2_group_desc *bg, 2177ccd979bdSMark Fasheh struct buffer_head *group_bh, 2178ccd979bdSMark Fasheh unsigned int bit_off, 2179b4414eeaSMark Fasheh unsigned int num_bits, 2180b4414eeaSMark Fasheh void (*undo_fn)(unsigned int bit, 2181b4414eeaSMark Fasheh unsigned long *bmap)) 2182ccd979bdSMark Fasheh { 2183ccd979bdSMark Fasheh int status; 2184ccd979bdSMark Fasheh unsigned int tmp; 2185ccd979bdSMark Fasheh struct ocfs2_group_desc *undo_bg = NULL; 2186ccd979bdSMark Fasheh 2187ccd979bdSMark Fasheh mlog_entry_void(); 2188ccd979bdSMark Fasheh 218942035306SJoel Becker /* The caller got this descriptor from 219042035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 219142035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 2192ccd979bdSMark Fasheh 2193ccd979bdSMark Fasheh mlog(0, "off = %u, num = %u\n", bit_off, num_bits); 2194ccd979bdSMark Fasheh 2195b4414eeaSMark Fasheh BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode)); 21960cf2f763SJoel Becker status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), 2197b4414eeaSMark Fasheh group_bh, 2198b4414eeaSMark Fasheh undo_fn ? 2199b4414eeaSMark Fasheh OCFS2_JOURNAL_ACCESS_UNDO : 2200b4414eeaSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 2201ccd979bdSMark Fasheh if (status < 0) { 2202ccd979bdSMark Fasheh mlog_errno(status); 2203ccd979bdSMark Fasheh goto bail; 2204ccd979bdSMark Fasheh } 2205ccd979bdSMark Fasheh 2206b4414eeaSMark Fasheh if (undo_fn) { 220794e41ecfSSunil Mushran jbd_lock_bh_state(group_bh); 220894e41ecfSSunil Mushran undo_bg = (struct ocfs2_group_desc *) 220994e41ecfSSunil Mushran bh2jh(group_bh)->b_committed_data; 221094e41ecfSSunil Mushran BUG_ON(!undo_bg); 221194e41ecfSSunil Mushran } 2212ccd979bdSMark Fasheh 2213ccd979bdSMark Fasheh tmp = num_bits; 2214ccd979bdSMark Fasheh while(tmp--) { 2215ccd979bdSMark Fasheh ocfs2_clear_bit((bit_off + tmp), 2216ccd979bdSMark Fasheh (unsigned long *) bg->bg_bitmap); 2217b4414eeaSMark Fasheh if (undo_fn) 2218b4414eeaSMark Fasheh undo_fn(bit_off + tmp, 2219ccd979bdSMark Fasheh (unsigned long *) undo_bg->bg_bitmap); 2220ccd979bdSMark Fasheh } 2221ccd979bdSMark Fasheh le16_add_cpu(&bg->bg_free_bits_count, num_bits); 2222ccd979bdSMark Fasheh 2223b4414eeaSMark Fasheh if (undo_fn) 222494e41ecfSSunil Mushran jbd_unlock_bh_state(group_bh); 222594e41ecfSSunil Mushran 2226ec20cec7SJoel Becker ocfs2_journal_dirty(handle, group_bh); 2227ccd979bdSMark Fasheh bail: 2228ccd979bdSMark Fasheh return status; 2229ccd979bdSMark Fasheh } 2230ccd979bdSMark Fasheh 2231ccd979bdSMark Fasheh /* 2232ccd979bdSMark Fasheh * expects the suballoc inode to already be locked. 2233ccd979bdSMark Fasheh */ 2234b4414eeaSMark Fasheh static int _ocfs2_free_suballoc_bits(handle_t *handle, 2235ccd979bdSMark Fasheh struct inode *alloc_inode, 2236ccd979bdSMark Fasheh struct buffer_head *alloc_bh, 2237ccd979bdSMark Fasheh unsigned int start_bit, 2238ccd979bdSMark Fasheh u64 bg_blkno, 2239b4414eeaSMark Fasheh unsigned int count, 2240b4414eeaSMark Fasheh void (*undo_fn)(unsigned int bit, 2241b4414eeaSMark Fasheh unsigned long *bitmap)) 2242ccd979bdSMark Fasheh { 2243ccd979bdSMark Fasheh int status = 0; 2244ccd979bdSMark Fasheh u32 tmp_used; 2245ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data; 2246ccd979bdSMark Fasheh struct ocfs2_chain_list *cl = &fe->id2.i_chain; 2247ccd979bdSMark Fasheh struct buffer_head *group_bh = NULL; 2248ccd979bdSMark Fasheh struct ocfs2_group_desc *group; 2249ccd979bdSMark Fasheh 2250ccd979bdSMark Fasheh mlog_entry_void(); 2251ccd979bdSMark Fasheh 225210995aa2SJoel Becker /* The alloc_bh comes from ocfs2_free_dinode() or 225310995aa2SJoel Becker * ocfs2_free_clusters(). The callers have all locked the 225410995aa2SJoel Becker * allocator and gotten alloc_bh from the lock call. This 225510995aa2SJoel Becker * validates the dinode buffer. Any corruption that has happended 225610995aa2SJoel Becker * is a code bug. */ 225710995aa2SJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); 2258ccd979bdSMark Fasheh BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl)); 2259ccd979bdSMark Fasheh 2260b0697053SMark Fasheh mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n", 2261b0697053SMark Fasheh (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count, 2262b0697053SMark Fasheh (unsigned long long)bg_blkno, start_bit); 2263ccd979bdSMark Fasheh 226468f64d47SJoel Becker status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno, 226568f64d47SJoel Becker &group_bh); 2266ccd979bdSMark Fasheh if (status < 0) { 2267ccd979bdSMark Fasheh mlog_errno(status); 2268ccd979bdSMark Fasheh goto bail; 2269ccd979bdSMark Fasheh } 2270ccd979bdSMark Fasheh group = (struct ocfs2_group_desc *) group_bh->b_data; 227168f64d47SJoel Becker 2272ccd979bdSMark Fasheh BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits)); 2273ccd979bdSMark Fasheh 2274ccd979bdSMark Fasheh status = ocfs2_block_group_clear_bits(handle, alloc_inode, 2275ccd979bdSMark Fasheh group, group_bh, 2276b4414eeaSMark Fasheh start_bit, count, undo_fn); 2277ccd979bdSMark Fasheh if (status < 0) { 2278ccd979bdSMark Fasheh mlog_errno(status); 2279ccd979bdSMark Fasheh goto bail; 2280ccd979bdSMark Fasheh } 2281ccd979bdSMark Fasheh 22820cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), 22830cf2f763SJoel Becker alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE); 2284ccd979bdSMark Fasheh if (status < 0) { 2285ccd979bdSMark Fasheh mlog_errno(status); 2286ccd979bdSMark Fasheh goto bail; 2287ccd979bdSMark Fasheh } 2288ccd979bdSMark Fasheh 2289ccd979bdSMark Fasheh le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free, 2290ccd979bdSMark Fasheh count); 2291ccd979bdSMark Fasheh tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used); 2292ccd979bdSMark Fasheh fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count); 2293ec20cec7SJoel Becker ocfs2_journal_dirty(handle, alloc_bh); 2294ccd979bdSMark Fasheh 2295ccd979bdSMark Fasheh bail: 2296ccd979bdSMark Fasheh brelse(group_bh); 2297ccd979bdSMark Fasheh 2298ccd979bdSMark Fasheh mlog_exit(status); 2299ccd979bdSMark Fasheh return status; 2300ccd979bdSMark Fasheh } 2301ccd979bdSMark Fasheh 2302b4414eeaSMark Fasheh int ocfs2_free_suballoc_bits(handle_t *handle, 2303b4414eeaSMark Fasheh struct inode *alloc_inode, 2304b4414eeaSMark Fasheh struct buffer_head *alloc_bh, 2305b4414eeaSMark Fasheh unsigned int start_bit, 2306b4414eeaSMark Fasheh u64 bg_blkno, 2307b4414eeaSMark Fasheh unsigned int count) 2308b4414eeaSMark Fasheh { 2309b4414eeaSMark Fasheh return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh, 2310b4414eeaSMark Fasheh start_bit, bg_blkno, count, NULL); 2311b4414eeaSMark Fasheh } 2312b4414eeaSMark Fasheh 23131fabe148SMark Fasheh int ocfs2_free_dinode(handle_t *handle, 2314ccd979bdSMark Fasheh struct inode *inode_alloc_inode, 2315ccd979bdSMark Fasheh struct buffer_head *inode_alloc_bh, 2316ccd979bdSMark Fasheh struct ocfs2_dinode *di) 2317ccd979bdSMark Fasheh { 2318ccd979bdSMark Fasheh u64 blk = le64_to_cpu(di->i_blkno); 2319ccd979bdSMark Fasheh u16 bit = le16_to_cpu(di->i_suballoc_bit); 2320ccd979bdSMark Fasheh u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit); 2321ccd979bdSMark Fasheh 2322ccd979bdSMark Fasheh return ocfs2_free_suballoc_bits(handle, inode_alloc_inode, 2323ccd979bdSMark Fasheh inode_alloc_bh, bit, bg_blkno, 1); 2324ccd979bdSMark Fasheh } 2325ccd979bdSMark Fasheh 2326b4414eeaSMark Fasheh static int _ocfs2_free_clusters(handle_t *handle, 2327ccd979bdSMark Fasheh struct inode *bitmap_inode, 2328ccd979bdSMark Fasheh struct buffer_head *bitmap_bh, 2329ccd979bdSMark Fasheh u64 start_blk, 2330b4414eeaSMark Fasheh unsigned int num_clusters, 2331b4414eeaSMark Fasheh void (*undo_fn)(unsigned int bit, 2332b4414eeaSMark Fasheh unsigned long *bitmap)) 2333ccd979bdSMark Fasheh { 2334ccd979bdSMark Fasheh int status; 2335ccd979bdSMark Fasheh u16 bg_start_bit; 2336ccd979bdSMark Fasheh u64 bg_blkno; 2337ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 2338ccd979bdSMark Fasheh 2339ccd979bdSMark Fasheh /* You can't ever have a contiguous set of clusters 2340ccd979bdSMark Fasheh * bigger than a block group bitmap so we never have to worry 2341ccd979bdSMark Fasheh * about looping on them. */ 2342ccd979bdSMark Fasheh 2343ccd979bdSMark Fasheh mlog_entry_void(); 2344ccd979bdSMark Fasheh 2345ccd979bdSMark Fasheh /* This is expensive. We can safely remove once this stuff has 2346ccd979bdSMark Fasheh * gotten tested really well. */ 2347ccd979bdSMark Fasheh BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk))); 2348ccd979bdSMark Fasheh 2349ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) bitmap_bh->b_data; 2350ccd979bdSMark Fasheh 2351ccd979bdSMark Fasheh ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno, 2352ccd979bdSMark Fasheh &bg_start_bit); 2353ccd979bdSMark Fasheh 2354b0697053SMark Fasheh mlog(0, "want to free %u clusters starting at block %llu\n", 2355b0697053SMark Fasheh num_clusters, (unsigned long long)start_blk); 2356b0697053SMark Fasheh mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n", 2357b0697053SMark Fasheh (unsigned long long)bg_blkno, bg_start_bit); 2358ccd979bdSMark Fasheh 2359b4414eeaSMark Fasheh status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh, 2360ccd979bdSMark Fasheh bg_start_bit, bg_blkno, 2361b4414eeaSMark Fasheh num_clusters, undo_fn); 23629c7af40bSMark Fasheh if (status < 0) { 2363ccd979bdSMark Fasheh mlog_errno(status); 23649c7af40bSMark Fasheh goto out; 23659c7af40bSMark Fasheh } 2366ccd979bdSMark Fasheh 23679c7af40bSMark Fasheh ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb), 23689c7af40bSMark Fasheh num_clusters); 23699c7af40bSMark Fasheh 23709c7af40bSMark Fasheh out: 2371ccd979bdSMark Fasheh mlog_exit(status); 2372ccd979bdSMark Fasheh return status; 2373ccd979bdSMark Fasheh } 2374ccd979bdSMark Fasheh 2375b4414eeaSMark Fasheh int ocfs2_free_clusters(handle_t *handle, 2376b4414eeaSMark Fasheh struct inode *bitmap_inode, 2377b4414eeaSMark Fasheh struct buffer_head *bitmap_bh, 2378b4414eeaSMark Fasheh u64 start_blk, 2379b4414eeaSMark Fasheh unsigned int num_clusters) 2380b4414eeaSMark Fasheh { 2381b4414eeaSMark Fasheh return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh, 2382b4414eeaSMark Fasheh start_blk, num_clusters, 2383b4414eeaSMark Fasheh _ocfs2_set_bit); 2384b4414eeaSMark Fasheh } 2385b4414eeaSMark Fasheh 2386b4414eeaSMark Fasheh /* 2387b4414eeaSMark Fasheh * Give never-used clusters back to the global bitmap. We don't need 2388b4414eeaSMark Fasheh * to protect these bits in the undo buffer. 2389b4414eeaSMark Fasheh */ 2390b4414eeaSMark Fasheh int ocfs2_release_clusters(handle_t *handle, 2391b4414eeaSMark Fasheh struct inode *bitmap_inode, 2392b4414eeaSMark Fasheh struct buffer_head *bitmap_bh, 2393b4414eeaSMark Fasheh u64 start_blk, 2394b4414eeaSMark Fasheh unsigned int num_clusters) 2395b4414eeaSMark Fasheh { 2396b4414eeaSMark Fasheh return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh, 2397b4414eeaSMark Fasheh start_blk, num_clusters, 2398b4414eeaSMark Fasheh _ocfs2_clear_bit); 2399b4414eeaSMark Fasheh } 2400b4414eeaSMark Fasheh 2401ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg) 2402ccd979bdSMark Fasheh { 2403ccd979bdSMark Fasheh printk("Block Group:\n"); 2404ccd979bdSMark Fasheh printk("bg_signature: %s\n", bg->bg_signature); 2405ccd979bdSMark Fasheh printk("bg_size: %u\n", bg->bg_size); 2406ccd979bdSMark Fasheh printk("bg_bits: %u\n", bg->bg_bits); 2407ccd979bdSMark Fasheh printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count); 2408ccd979bdSMark Fasheh printk("bg_chain: %u\n", bg->bg_chain); 2409ccd979bdSMark Fasheh printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation)); 2410b0697053SMark Fasheh printk("bg_next_group: %llu\n", 2411b0697053SMark Fasheh (unsigned long long)bg->bg_next_group); 2412b0697053SMark Fasheh printk("bg_parent_dinode: %llu\n", 2413b0697053SMark Fasheh (unsigned long long)bg->bg_parent_dinode); 2414b0697053SMark Fasheh printk("bg_blkno: %llu\n", 2415b0697053SMark Fasheh (unsigned long long)bg->bg_blkno); 2416ccd979bdSMark Fasheh } 2417ccd979bdSMark Fasheh 2418ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe) 2419ccd979bdSMark Fasheh { 2420ccd979bdSMark Fasheh int i; 2421ccd979bdSMark Fasheh 2422b0697053SMark Fasheh printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno); 2423ccd979bdSMark Fasheh printk("i_signature: %s\n", fe->i_signature); 2424b0697053SMark Fasheh printk("i_size: %llu\n", 2425b0697053SMark Fasheh (unsigned long long)fe->i_size); 2426ccd979bdSMark Fasheh printk("i_clusters: %u\n", fe->i_clusters); 2427ccd979bdSMark Fasheh printk("i_generation: %u\n", 2428ccd979bdSMark Fasheh le32_to_cpu(fe->i_generation)); 2429ccd979bdSMark Fasheh printk("id1.bitmap1.i_used: %u\n", 2430ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_used)); 2431ccd979bdSMark Fasheh printk("id1.bitmap1.i_total: %u\n", 2432ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_total)); 2433ccd979bdSMark Fasheh printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg); 2434ccd979bdSMark Fasheh printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc); 2435ccd979bdSMark Fasheh printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count); 2436ccd979bdSMark Fasheh printk("id2.i_chain.cl_next_free_rec: %u\n", 2437ccd979bdSMark Fasheh fe->id2.i_chain.cl_next_free_rec); 2438ccd979bdSMark Fasheh for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) { 2439ccd979bdSMark Fasheh printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i, 2440ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[i].c_free); 2441ccd979bdSMark Fasheh printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i, 2442ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[i].c_total); 2443b0697053SMark Fasheh printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i, 2444b0697053SMark Fasheh (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno); 2445ccd979bdSMark Fasheh } 2446ccd979bdSMark Fasheh } 2447e7d4cb6bSTao Ma 2448e7d4cb6bSTao Ma /* 2449e7d4cb6bSTao Ma * For a given allocation, determine which allocators will need to be 2450e7d4cb6bSTao Ma * accessed, and lock them, reserving the appropriate number of bits. 2451e7d4cb6bSTao Ma * 2452e7d4cb6bSTao Ma * Sparse file systems call this from ocfs2_write_begin_nolock() 2453e7d4cb6bSTao Ma * and ocfs2_allocate_unwritten_extents(). 2454e7d4cb6bSTao Ma * 2455e7d4cb6bSTao Ma * File systems which don't support holes call this from 2456e7d4cb6bSTao Ma * ocfs2_extend_allocation(). 2457e7d4cb6bSTao Ma */ 2458f99b9b7cSJoel Becker int ocfs2_lock_allocators(struct inode *inode, 2459f99b9b7cSJoel Becker struct ocfs2_extent_tree *et, 2460e7d4cb6bSTao Ma u32 clusters_to_add, u32 extents_to_split, 2461e7d4cb6bSTao Ma struct ocfs2_alloc_context **data_ac, 2462f99b9b7cSJoel Becker struct ocfs2_alloc_context **meta_ac) 2463e7d4cb6bSTao Ma { 2464e7d4cb6bSTao Ma int ret = 0, num_free_extents; 2465e7d4cb6bSTao Ma unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split; 2466e7d4cb6bSTao Ma struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2467e7d4cb6bSTao Ma 2468e7d4cb6bSTao Ma *meta_ac = NULL; 2469e7d4cb6bSTao Ma if (data_ac) 2470e7d4cb6bSTao Ma *data_ac = NULL; 2471e7d4cb6bSTao Ma 2472e7d4cb6bSTao Ma BUG_ON(clusters_to_add != 0 && data_ac == NULL); 2473e7d4cb6bSTao Ma 24743d03a305SJoel Becker num_free_extents = ocfs2_num_free_extents(osb, et); 2475e7d4cb6bSTao Ma if (num_free_extents < 0) { 2476e7d4cb6bSTao Ma ret = num_free_extents; 2477e7d4cb6bSTao Ma mlog_errno(ret); 2478e7d4cb6bSTao Ma goto out; 2479e7d4cb6bSTao Ma } 2480e7d4cb6bSTao Ma 2481e7d4cb6bSTao Ma /* 2482e7d4cb6bSTao Ma * Sparse allocation file systems need to be more conservative 2483e7d4cb6bSTao Ma * with reserving room for expansion - the actual allocation 2484e7d4cb6bSTao Ma * happens while we've got a journal handle open so re-taking 2485e7d4cb6bSTao Ma * a cluster lock (because we ran out of room for another 2486e7d4cb6bSTao Ma * extent) will violate ordering rules. 2487e7d4cb6bSTao Ma * 2488e7d4cb6bSTao Ma * Most of the time we'll only be seeing this 1 cluster at a time 2489e7d4cb6bSTao Ma * anyway. 2490e7d4cb6bSTao Ma * 2491e7d4cb6bSTao Ma * Always lock for any unwritten extents - we might want to 2492e7d4cb6bSTao Ma * add blocks during a split. 2493e7d4cb6bSTao Ma */ 2494e7d4cb6bSTao Ma if (!num_free_extents || 2495e7d4cb6bSTao Ma (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) { 2496f99b9b7cSJoel Becker ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac); 2497e7d4cb6bSTao Ma if (ret < 0) { 2498e7d4cb6bSTao Ma if (ret != -ENOSPC) 2499e7d4cb6bSTao Ma mlog_errno(ret); 2500e7d4cb6bSTao Ma goto out; 2501e7d4cb6bSTao Ma } 2502e7d4cb6bSTao Ma } 2503e7d4cb6bSTao Ma 2504e7d4cb6bSTao Ma if (clusters_to_add == 0) 2505e7d4cb6bSTao Ma goto out; 2506e7d4cb6bSTao Ma 2507e7d4cb6bSTao Ma ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac); 2508e7d4cb6bSTao Ma if (ret < 0) { 2509e7d4cb6bSTao Ma if (ret != -ENOSPC) 2510e7d4cb6bSTao Ma mlog_errno(ret); 2511e7d4cb6bSTao Ma goto out; 2512e7d4cb6bSTao Ma } 2513e7d4cb6bSTao Ma 2514e7d4cb6bSTao Ma out: 2515e7d4cb6bSTao Ma if (ret) { 2516e7d4cb6bSTao Ma if (*meta_ac) { 2517e7d4cb6bSTao Ma ocfs2_free_alloc_context(*meta_ac); 2518e7d4cb6bSTao Ma *meta_ac = NULL; 2519e7d4cb6bSTao Ma } 2520e7d4cb6bSTao Ma 2521e7d4cb6bSTao Ma /* 2522e7d4cb6bSTao Ma * We cannot have an error and a non null *data_ac. 2523e7d4cb6bSTao Ma */ 2524e7d4cb6bSTao Ma } 2525e7d4cb6bSTao Ma 2526e7d4cb6bSTao Ma return ret; 2527e7d4cb6bSTao Ma } 25286ca497a8Swengang wang 25296ca497a8Swengang wang /* 25306ca497a8Swengang wang * Read the inode specified by blkno to get suballoc_slot and 25316ca497a8Swengang wang * suballoc_bit. 25326ca497a8Swengang wang */ 25336ca497a8Swengang wang static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno, 25346ca497a8Swengang wang u16 *suballoc_slot, u16 *suballoc_bit) 25356ca497a8Swengang wang { 25366ca497a8Swengang wang int status; 25376ca497a8Swengang wang struct buffer_head *inode_bh = NULL; 25386ca497a8Swengang wang struct ocfs2_dinode *inode_fe; 25396ca497a8Swengang wang 25405b09b507SJoel Becker mlog_entry("blkno: %llu\n", (unsigned long long)blkno); 25416ca497a8Swengang wang 25426ca497a8Swengang wang /* dirty read disk */ 25436ca497a8Swengang wang status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh); 25446ca497a8Swengang wang if (status < 0) { 25455b09b507SJoel Becker mlog(ML_ERROR, "read block %llu failed %d\n", 25465b09b507SJoel Becker (unsigned long long)blkno, status); 25476ca497a8Swengang wang goto bail; 25486ca497a8Swengang wang } 25496ca497a8Swengang wang 25506ca497a8Swengang wang inode_fe = (struct ocfs2_dinode *) inode_bh->b_data; 25516ca497a8Swengang wang if (!OCFS2_IS_VALID_DINODE(inode_fe)) { 25525b09b507SJoel Becker mlog(ML_ERROR, "invalid inode %llu requested\n", 25535b09b507SJoel Becker (unsigned long long)blkno); 25546ca497a8Swengang wang status = -EINVAL; 25556ca497a8Swengang wang goto bail; 25566ca497a8Swengang wang } 25576ca497a8Swengang wang 25580fba8137STao Ma if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT && 25596ca497a8Swengang wang (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) { 25606ca497a8Swengang wang mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n", 25615b09b507SJoel Becker (unsigned long long)blkno, 25625b09b507SJoel Becker (u32)le16_to_cpu(inode_fe->i_suballoc_slot)); 25636ca497a8Swengang wang status = -EINVAL; 25646ca497a8Swengang wang goto bail; 25656ca497a8Swengang wang } 25666ca497a8Swengang wang 25676ca497a8Swengang wang if (suballoc_slot) 25686ca497a8Swengang wang *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot); 25696ca497a8Swengang wang if (suballoc_bit) 25706ca497a8Swengang wang *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit); 25716ca497a8Swengang wang 25726ca497a8Swengang wang bail: 25736ca497a8Swengang wang brelse(inode_bh); 25746ca497a8Swengang wang 25756ca497a8Swengang wang mlog_exit(status); 25766ca497a8Swengang wang return status; 25776ca497a8Swengang wang } 25786ca497a8Swengang wang 25796ca497a8Swengang wang /* 25806ca497a8Swengang wang * test whether bit is SET in allocator bitmap or not. on success, 0 25816ca497a8Swengang wang * is returned and *res is 1 for SET; 0 otherwise. when fails, errno 25826ca497a8Swengang wang * is returned and *res is meaningless. Call this after you have 25836ca497a8Swengang wang * cluster locked against suballoc, or you may get a result based on 25846ca497a8Swengang wang * non-up2date contents 25856ca497a8Swengang wang */ 25866ca497a8Swengang wang static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb, 25876ca497a8Swengang wang struct inode *suballoc, 25886ca497a8Swengang wang struct buffer_head *alloc_bh, u64 blkno, 25896ca497a8Swengang wang u16 bit, int *res) 25906ca497a8Swengang wang { 25916ca497a8Swengang wang struct ocfs2_dinode *alloc_fe; 25926ca497a8Swengang wang struct ocfs2_group_desc *group; 25936ca497a8Swengang wang struct buffer_head *group_bh = NULL; 25946ca497a8Swengang wang u64 bg_blkno; 25956ca497a8Swengang wang int status; 25966ca497a8Swengang wang 25975b09b507SJoel Becker mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno, 25985b09b507SJoel Becker (unsigned int)bit); 25996ca497a8Swengang wang 26006ca497a8Swengang wang alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data; 26016ca497a8Swengang wang if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) { 26026ca497a8Swengang wang mlog(ML_ERROR, "suballoc bit %u out of range of %u\n", 26036ca497a8Swengang wang (unsigned int)bit, 26046ca497a8Swengang wang ocfs2_bits_per_group(&alloc_fe->id2.i_chain)); 26056ca497a8Swengang wang status = -EINVAL; 26066ca497a8Swengang wang goto bail; 26076ca497a8Swengang wang } 26086ca497a8Swengang wang 26096ca497a8Swengang wang bg_blkno = ocfs2_which_suballoc_group(blkno, bit); 26106ca497a8Swengang wang status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno, 26116ca497a8Swengang wang &group_bh); 26126ca497a8Swengang wang if (status < 0) { 26135b09b507SJoel Becker mlog(ML_ERROR, "read group %llu failed %d\n", 26145b09b507SJoel Becker (unsigned long long)bg_blkno, status); 26156ca497a8Swengang wang goto bail; 26166ca497a8Swengang wang } 26176ca497a8Swengang wang 26186ca497a8Swengang wang group = (struct ocfs2_group_desc *) group_bh->b_data; 26196ca497a8Swengang wang *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap); 26206ca497a8Swengang wang 26216ca497a8Swengang wang bail: 26226ca497a8Swengang wang brelse(group_bh); 26236ca497a8Swengang wang 26246ca497a8Swengang wang mlog_exit(status); 26256ca497a8Swengang wang return status; 26266ca497a8Swengang wang } 26276ca497a8Swengang wang 26286ca497a8Swengang wang /* 26296ca497a8Swengang wang * Test if the bit representing this inode (blkno) is set in the 26306ca497a8Swengang wang * suballocator. 26316ca497a8Swengang wang * 26326ca497a8Swengang wang * On success, 0 is returned and *res is 1 for SET; 0 otherwise. 26336ca497a8Swengang wang * 26346ca497a8Swengang wang * In the event of failure, a negative value is returned and *res is 26356ca497a8Swengang wang * meaningless. 26366ca497a8Swengang wang * 26376ca497a8Swengang wang * Callers must make sure to hold nfs_sync_lock to prevent 26386ca497a8Swengang wang * ocfs2_delete_inode() on another node from accessing the same 26396ca497a8Swengang wang * suballocator concurrently. 26406ca497a8Swengang wang */ 26416ca497a8Swengang wang int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res) 26426ca497a8Swengang wang { 26436ca497a8Swengang wang int status; 26446ca497a8Swengang wang u16 suballoc_bit = 0, suballoc_slot = 0; 26456ca497a8Swengang wang struct inode *inode_alloc_inode; 26466ca497a8Swengang wang struct buffer_head *alloc_bh = NULL; 26476ca497a8Swengang wang 26485b09b507SJoel Becker mlog_entry("blkno: %llu", (unsigned long long)blkno); 26496ca497a8Swengang wang 26506ca497a8Swengang wang status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot, 26516ca497a8Swengang wang &suballoc_bit); 26526ca497a8Swengang wang if (status < 0) { 26536ca497a8Swengang wang mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status); 26546ca497a8Swengang wang goto bail; 26556ca497a8Swengang wang } 26566ca497a8Swengang wang 26576ca497a8Swengang wang inode_alloc_inode = 26586ca497a8Swengang wang ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE, 26596ca497a8Swengang wang suballoc_slot); 26606ca497a8Swengang wang if (!inode_alloc_inode) { 26616ca497a8Swengang wang /* the error code could be inaccurate, but we are not able to 26626ca497a8Swengang wang * get the correct one. */ 26636ca497a8Swengang wang status = -EINVAL; 26646ca497a8Swengang wang mlog(ML_ERROR, "unable to get alloc inode in slot %u\n", 26656ca497a8Swengang wang (u32)suballoc_slot); 26666ca497a8Swengang wang goto bail; 26676ca497a8Swengang wang } 26686ca497a8Swengang wang 26696ca497a8Swengang wang mutex_lock(&inode_alloc_inode->i_mutex); 26706ca497a8Swengang wang status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0); 26716ca497a8Swengang wang if (status < 0) { 26726ca497a8Swengang wang mutex_unlock(&inode_alloc_inode->i_mutex); 26736ca497a8Swengang wang mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n", 26746ca497a8Swengang wang (u32)suballoc_slot, status); 26756ca497a8Swengang wang goto bail; 26766ca497a8Swengang wang } 26776ca497a8Swengang wang 26786ca497a8Swengang wang status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh, 26796ca497a8Swengang wang blkno, suballoc_bit, res); 26806ca497a8Swengang wang if (status < 0) 26816ca497a8Swengang wang mlog(ML_ERROR, "test suballoc bit failed %d\n", status); 26826ca497a8Swengang wang 26836ca497a8Swengang wang ocfs2_inode_unlock(inode_alloc_inode, 0); 26846ca497a8Swengang wang mutex_unlock(&inode_alloc_inode->i_mutex); 26856ca497a8Swengang wang 26866ca497a8Swengang wang iput(inode_alloc_inode); 26876ca497a8Swengang wang brelse(alloc_bh); 26886ca497a8Swengang wang bail: 26896ca497a8Swengang wang mlog_exit(status); 26906ca497a8Swengang wang return status; 26916ca497a8Swengang wang } 2692