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 { 572b6cb576SJoel Becker u64 sr_bg_blkno; /* The bg we allocated from. Set 582b6cb576SJoel Becker to 0 when a block group is 592b6cb576SJoel Becker contiguous. */ 60ba206635SJoel Becker u64 sr_blkno; /* The first allocated block */ 617d1fe093SJoel Becker unsigned int sr_bit_offset; /* The bit in the bg */ 627d1fe093SJoel Becker unsigned int sr_bits; /* How many bits we claimed */ 637d1fe093SJoel Becker }; 647d1fe093SJoel Becker 65ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg); 66ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe); 67ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl); 681fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle, 69ccd979bdSMark Fasheh struct inode *alloc_inode, 70ccd979bdSMark Fasheh struct buffer_head *bg_bh, 71ccd979bdSMark Fasheh u64 group_blkno, 72798db35fSJoel Becker unsigned int group_clusters, 73ccd979bdSMark Fasheh u16 my_chain, 74ccd979bdSMark Fasheh struct ocfs2_chain_list *cl); 75ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb, 76ccd979bdSMark Fasheh struct inode *alloc_inode, 771187c968SJoel Becker struct buffer_head *bh, 7860ca81e8STao Ma u64 max_block, 79feb473a6STao Ma u64 *last_alloc_group, 8060ca81e8STao Ma int flags); 81ccd979bdSMark Fasheh 82ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode, 83ccd979bdSMark Fasheh struct buffer_head *group_bh, 84ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 851187c968SJoel Becker u64 max_block, 867d1fe093SJoel Becker struct ocfs2_suballoc_result *res); 87ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode, 88ccd979bdSMark Fasheh struct buffer_head *group_bh, 89ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 901187c968SJoel Becker u64 max_block, 917d1fe093SJoel Becker struct ocfs2_suballoc_result *res); 92aa8f8e93SJoel Becker static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac, 931fabe148SMark Fasheh handle_t *handle, 94ccd979bdSMark Fasheh u32 bits_wanted, 95ccd979bdSMark Fasheh u32 min_bits, 967d1fe093SJoel Becker struct ocfs2_suballoc_result *res); 97ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh, 98ccd979bdSMark Fasheh int nr); 991fabe148SMark Fasheh static inline int ocfs2_block_group_set_bits(handle_t *handle, 100ccd979bdSMark Fasheh struct inode *alloc_inode, 101ccd979bdSMark Fasheh struct ocfs2_group_desc *bg, 102ccd979bdSMark Fasheh struct buffer_head *group_bh, 103ccd979bdSMark Fasheh unsigned int bit_off, 104ccd979bdSMark Fasheh unsigned int num_bits); 1051fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle, 106ccd979bdSMark Fasheh struct inode *alloc_inode, 107ccd979bdSMark Fasheh struct buffer_head *fe_bh, 108ccd979bdSMark Fasheh struct buffer_head *bg_bh, 109ccd979bdSMark Fasheh struct buffer_head *prev_bg_bh, 110ccd979bdSMark Fasheh u16 chain); 111ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg, 112ccd979bdSMark Fasheh u32 wanted); 113ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode, 114ccd979bdSMark Fasheh u64 bg_blkno, 115ccd979bdSMark Fasheh u16 bg_bit_off); 116ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode, 117ccd979bdSMark Fasheh u64 data_blkno, 118ccd979bdSMark Fasheh u64 *bg_blkno, 119ccd979bdSMark Fasheh u16 *bg_bit_off); 1201187c968SJoel Becker static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb, 1211187c968SJoel Becker u32 bits_wanted, u64 max_block, 12260ca81e8STao Ma int flags, 1231187c968SJoel Becker struct ocfs2_alloc_context **ac); 124ccd979bdSMark Fasheh 1259c7af40bSMark Fasheh void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac) 126ccd979bdSMark Fasheh { 127da5cbf2fSMark Fasheh struct inode *inode = ac->ac_inode; 128da5cbf2fSMark Fasheh 129da5cbf2fSMark Fasheh if (inode) { 130da5cbf2fSMark Fasheh if (ac->ac_which != OCFS2_AC_USE_LOCAL) 131e63aecb6SMark Fasheh ocfs2_inode_unlock(inode, 1); 132da5cbf2fSMark Fasheh 133da5cbf2fSMark Fasheh mutex_unlock(&inode->i_mutex); 134da5cbf2fSMark Fasheh 135da5cbf2fSMark Fasheh iput(inode); 1364d0ddb2cSTao Ma ac->ac_inode = NULL; 137da5cbf2fSMark Fasheh } 138ccd979bdSMark Fasheh brelse(ac->ac_bh); 1394d0ddb2cSTao Ma ac->ac_bh = NULL; 140e3b4a97dSMark Fasheh ac->ac_resv = NULL; 1414d0ddb2cSTao Ma } 1424d0ddb2cSTao Ma 1434d0ddb2cSTao Ma void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac) 1444d0ddb2cSTao Ma { 1454d0ddb2cSTao Ma ocfs2_free_ac_resource(ac); 146ccd979bdSMark Fasheh kfree(ac); 147ccd979bdSMark Fasheh } 148ccd979bdSMark Fasheh 149ccd979bdSMark Fasheh static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl) 150ccd979bdSMark Fasheh { 151ccd979bdSMark Fasheh return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc); 152ccd979bdSMark Fasheh } 153ccd979bdSMark Fasheh 15457e3e797SJoel Becker #define do_error(fmt, ...) \ 15557e3e797SJoel Becker do{ \ 15678c37eb0STao Ma if (resize) \ 15757e3e797SJoel Becker mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \ 15857e3e797SJoel Becker else \ 15957e3e797SJoel Becker ocfs2_error(sb, fmt, ##__VA_ARGS__); \ 16057e3e797SJoel Becker } while (0) 16157e3e797SJoel Becker 162970e4936SJoel Becker static int ocfs2_validate_gd_self(struct super_block *sb, 163970e4936SJoel Becker struct buffer_head *bh, 16478c37eb0STao Ma int resize) 165970e4936SJoel Becker { 166970e4936SJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 167970e4936SJoel Becker 1687bf72edeSMark Fasheh if (!OCFS2_IS_VALID_GROUP_DESC(gd)) { 16968f64d47SJoel Becker do_error("Group descriptor #%llu has bad signature %.*s", 17068f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 7, 17157e3e797SJoel Becker gd->bg_signature); 17257e3e797SJoel Becker return -EINVAL; 1737bf72edeSMark Fasheh } 1747bf72edeSMark Fasheh 17568f64d47SJoel Becker if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) { 17668f64d47SJoel Becker do_error("Group descriptor #%llu has an invalid bg_blkno " 17768f64d47SJoel Becker "of %llu", 17868f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 17968f64d47SJoel Becker (unsigned long long)le64_to_cpu(gd->bg_blkno)); 18068f64d47SJoel Becker return -EINVAL; 18168f64d47SJoel Becker } 18268f64d47SJoel Becker 18368f64d47SJoel Becker if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) { 18468f64d47SJoel Becker do_error("Group descriptor #%llu has an invalid " 18568f64d47SJoel Becker "fs_generation of #%u", 18668f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 18768f64d47SJoel Becker le32_to_cpu(gd->bg_generation)); 18868f64d47SJoel Becker return -EINVAL; 18968f64d47SJoel Becker } 19068f64d47SJoel Becker 191970e4936SJoel Becker if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) { 192970e4936SJoel Becker do_error("Group descriptor #%llu has bit count %u but " 193970e4936SJoel Becker "claims that %u are free", 194970e4936SJoel Becker (unsigned long long)bh->b_blocknr, 195970e4936SJoel Becker le16_to_cpu(gd->bg_bits), 196970e4936SJoel Becker le16_to_cpu(gd->bg_free_bits_count)); 197970e4936SJoel Becker return -EINVAL; 198970e4936SJoel Becker } 199970e4936SJoel Becker 200970e4936SJoel Becker if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) { 201970e4936SJoel Becker do_error("Group descriptor #%llu has bit count %u but " 202970e4936SJoel Becker "max bitmap bits of %u", 203970e4936SJoel Becker (unsigned long long)bh->b_blocknr, 204970e4936SJoel Becker le16_to_cpu(gd->bg_bits), 205970e4936SJoel Becker 8 * le16_to_cpu(gd->bg_size)); 206970e4936SJoel Becker return -EINVAL; 207970e4936SJoel Becker } 208970e4936SJoel Becker 209970e4936SJoel Becker return 0; 210970e4936SJoel Becker } 211970e4936SJoel Becker 212970e4936SJoel Becker static int ocfs2_validate_gd_parent(struct super_block *sb, 213970e4936SJoel Becker struct ocfs2_dinode *di, 214970e4936SJoel Becker struct buffer_head *bh, 21578c37eb0STao Ma int resize) 216970e4936SJoel Becker { 217970e4936SJoel Becker unsigned int max_bits; 218970e4936SJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 219970e4936SJoel Becker 2207bf72edeSMark Fasheh if (di->i_blkno != gd->bg_parent_dinode) { 22157e3e797SJoel Becker do_error("Group descriptor #%llu has bad parent " 2227bf72edeSMark Fasheh "pointer (%llu, expected %llu)", 22368f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 2247bf72edeSMark Fasheh (unsigned long long)le64_to_cpu(gd->bg_parent_dinode), 2257bf72edeSMark Fasheh (unsigned long long)le64_to_cpu(di->i_blkno)); 22657e3e797SJoel Becker return -EINVAL; 2277bf72edeSMark Fasheh } 2287bf72edeSMark Fasheh 2297bf72edeSMark Fasheh max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc); 2307bf72edeSMark Fasheh if (le16_to_cpu(gd->bg_bits) > max_bits) { 23157e3e797SJoel Becker do_error("Group descriptor #%llu has bit count of %u", 23268f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 2337bf72edeSMark Fasheh le16_to_cpu(gd->bg_bits)); 23457e3e797SJoel Becker return -EINVAL; 2357bf72edeSMark Fasheh } 2367bf72edeSMark Fasheh 23778c37eb0STao Ma /* In resize, we may meet the case bg_chain == cl_next_free_rec. */ 23878c37eb0STao Ma if ((le16_to_cpu(gd->bg_chain) > 23978c37eb0STao Ma le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) || 24078c37eb0STao Ma ((le16_to_cpu(gd->bg_chain) == 24178c37eb0STao Ma le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) { 24257e3e797SJoel Becker do_error("Group descriptor #%llu has bad chain %u", 24368f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 2447bf72edeSMark Fasheh le16_to_cpu(gd->bg_chain)); 24557e3e797SJoel Becker return -EINVAL; 2467bf72edeSMark Fasheh } 2477bf72edeSMark Fasheh 248970e4936SJoel Becker return 0; 2497bf72edeSMark Fasheh } 2507bf72edeSMark Fasheh 25157e3e797SJoel Becker #undef do_error 2527bf72edeSMark Fasheh 253970e4936SJoel Becker /* 254970e4936SJoel Becker * This version only prints errors. It does not fail the filesystem, and 255970e4936SJoel Becker * exists only for resize. 256970e4936SJoel Becker */ 257970e4936SJoel Becker int ocfs2_check_group_descriptor(struct super_block *sb, 258970e4936SJoel Becker struct ocfs2_dinode *di, 259970e4936SJoel Becker struct buffer_head *bh) 260970e4936SJoel Becker { 261970e4936SJoel Becker int rc; 262d6b32bbbSJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 263970e4936SJoel Becker 264d6b32bbbSJoel Becker BUG_ON(!buffer_uptodate(bh)); 265d6b32bbbSJoel Becker 266d6b32bbbSJoel Becker /* 267d6b32bbbSJoel Becker * If the ecc fails, we return the error but otherwise 268d6b32bbbSJoel Becker * leave the filesystem running. We know any error is 269d6b32bbbSJoel Becker * local to this block. 270d6b32bbbSJoel Becker */ 271d6b32bbbSJoel Becker rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check); 27213723d00SJoel Becker if (rc) { 27313723d00SJoel Becker mlog(ML_ERROR, 27413723d00SJoel Becker "Checksum failed for group descriptor %llu\n", 27513723d00SJoel Becker (unsigned long long)bh->b_blocknr); 27613723d00SJoel Becker } else 277970e4936SJoel Becker rc = ocfs2_validate_gd_self(sb, bh, 1); 278970e4936SJoel Becker if (!rc) 279970e4936SJoel Becker rc = ocfs2_validate_gd_parent(sb, di, bh, 1); 280970e4936SJoel Becker 281970e4936SJoel Becker return rc; 282970e4936SJoel Becker } 283970e4936SJoel Becker 284970e4936SJoel Becker static int ocfs2_validate_group_descriptor(struct super_block *sb, 285970e4936SJoel Becker struct buffer_head *bh) 286970e4936SJoel Becker { 287d6b32bbbSJoel Becker int rc; 288d6b32bbbSJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 289d6b32bbbSJoel Becker 290970e4936SJoel Becker mlog(0, "Validating group descriptor %llu\n", 291970e4936SJoel Becker (unsigned long long)bh->b_blocknr); 292970e4936SJoel Becker 293d6b32bbbSJoel Becker BUG_ON(!buffer_uptodate(bh)); 294d6b32bbbSJoel Becker 295d6b32bbbSJoel Becker /* 296d6b32bbbSJoel Becker * If the ecc fails, we return the error but otherwise 297d6b32bbbSJoel Becker * leave the filesystem running. We know any error is 298d6b32bbbSJoel Becker * local to this block. 299d6b32bbbSJoel Becker */ 300d6b32bbbSJoel Becker rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check); 301d6b32bbbSJoel Becker if (rc) 302d6b32bbbSJoel Becker return rc; 303d6b32bbbSJoel Becker 304d6b32bbbSJoel Becker /* 305d6b32bbbSJoel Becker * Errors after here are fatal. 306d6b32bbbSJoel Becker */ 307d6b32bbbSJoel Becker 308970e4936SJoel Becker return ocfs2_validate_gd_self(sb, bh, 0); 3097bf72edeSMark Fasheh } 3107bf72edeSMark Fasheh 31168f64d47SJoel Becker int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di, 31268f64d47SJoel Becker u64 gd_blkno, struct buffer_head **bh) 31368f64d47SJoel Becker { 31468f64d47SJoel Becker int rc; 31568f64d47SJoel Becker struct buffer_head *tmp = *bh; 31668f64d47SJoel Becker 3178cb471e8SJoel Becker rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp, 318970e4936SJoel Becker ocfs2_validate_group_descriptor); 31968f64d47SJoel Becker if (rc) 32068f64d47SJoel Becker goto out; 32168f64d47SJoel Becker 322970e4936SJoel Becker rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0); 32368f64d47SJoel Becker if (rc) { 32468f64d47SJoel Becker brelse(tmp); 32568f64d47SJoel Becker goto out; 32668f64d47SJoel Becker } 32768f64d47SJoel Becker 32868f64d47SJoel Becker /* If ocfs2_read_block() got us a new bh, pass it up. */ 32968f64d47SJoel Becker if (!*bh) 33068f64d47SJoel Becker *bh = tmp; 33168f64d47SJoel Becker 33268f64d47SJoel Becker out: 33368f64d47SJoel Becker return rc; 33468f64d47SJoel Becker } 33568f64d47SJoel Becker 336798db35fSJoel Becker static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb, 337798db35fSJoel Becker struct ocfs2_group_desc *bg, 338798db35fSJoel Becker struct ocfs2_chain_list *cl, 339798db35fSJoel Becker u64 p_blkno, u32 clusters) 340798db35fSJoel Becker { 341798db35fSJoel Becker struct ocfs2_extent_list *el = &bg->bg_list; 342798db35fSJoel Becker struct ocfs2_extent_rec *rec; 343798db35fSJoel Becker 3444711954eSTao Ma BUG_ON(!ocfs2_supports_discontig_bg(osb)); 345798db35fSJoel Becker if (!el->l_next_free_rec) 346798db35fSJoel Becker el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb)); 347798db35fSJoel Becker rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)]; 3484711954eSTao Ma rec->e_blkno = cpu_to_le64(p_blkno); 349798db35fSJoel Becker rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) / 350798db35fSJoel Becker le16_to_cpu(cl->cl_bpc)); 351798db35fSJoel Becker rec->e_leaf_clusters = cpu_to_le32(clusters); 352798db35fSJoel Becker le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc)); 3534711954eSTao Ma le16_add_cpu(&bg->bg_free_bits_count, 3544711954eSTao Ma clusters * le16_to_cpu(cl->cl_bpc)); 355798db35fSJoel Becker le16_add_cpu(&el->l_next_free_rec, 1); 356798db35fSJoel Becker } 357798db35fSJoel Becker 3581fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle, 359ccd979bdSMark Fasheh struct inode *alloc_inode, 360ccd979bdSMark Fasheh struct buffer_head *bg_bh, 361ccd979bdSMark Fasheh u64 group_blkno, 362798db35fSJoel Becker unsigned int group_clusters, 363ccd979bdSMark Fasheh u16 my_chain, 364ccd979bdSMark Fasheh struct ocfs2_chain_list *cl) 365ccd979bdSMark Fasheh { 366ccd979bdSMark Fasheh int status = 0; 367798db35fSJoel Becker struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); 368ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 369ccd979bdSMark Fasheh struct super_block * sb = alloc_inode->i_sb; 370ccd979bdSMark Fasheh 371ccd979bdSMark Fasheh mlog_entry_void(); 372ccd979bdSMark Fasheh 373ccd979bdSMark Fasheh if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) { 374b0697053SMark Fasheh ocfs2_error(alloc_inode->i_sb, "group block (%llu) != " 375b0697053SMark Fasheh "b_blocknr (%llu)", 376b0697053SMark Fasheh (unsigned long long)group_blkno, 377ccd979bdSMark Fasheh (unsigned long long) bg_bh->b_blocknr); 378ccd979bdSMark Fasheh status = -EIO; 379ccd979bdSMark Fasheh goto bail; 380ccd979bdSMark Fasheh } 381ccd979bdSMark Fasheh 38213723d00SJoel Becker status = ocfs2_journal_access_gd(handle, 3830cf2f763SJoel Becker INODE_CACHE(alloc_inode), 384ccd979bdSMark Fasheh bg_bh, 385ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_CREATE); 386ccd979bdSMark Fasheh if (status < 0) { 387ccd979bdSMark Fasheh mlog_errno(status); 388ccd979bdSMark Fasheh goto bail; 389ccd979bdSMark Fasheh } 390ccd979bdSMark Fasheh 391ccd979bdSMark Fasheh memset(bg, 0, sb->s_blocksize); 392ccd979bdSMark Fasheh strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE); 393ccd979bdSMark Fasheh bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation); 3948571882cSTao Ma bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1, 3958571882cSTao Ma osb->s_feature_incompat)); 396ccd979bdSMark Fasheh bg->bg_chain = cpu_to_le16(my_chain); 397ccd979bdSMark Fasheh bg->bg_next_group = cl->cl_recs[my_chain].c_blkno; 398ccd979bdSMark Fasheh bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno); 399ccd979bdSMark Fasheh bg->bg_blkno = cpu_to_le64(group_blkno); 400798db35fSJoel Becker if (group_clusters == le16_to_cpu(cl->cl_cpg)) 401798db35fSJoel Becker bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl)); 402798db35fSJoel Becker else 4034711954eSTao Ma ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno, 404798db35fSJoel Becker group_clusters); 405798db35fSJoel Becker 406ccd979bdSMark Fasheh /* set the 1st bit in the bitmap to account for the descriptor block */ 407ccd979bdSMark Fasheh ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap); 408ccd979bdSMark Fasheh bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1); 409ccd979bdSMark Fasheh 410ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bg_bh); 411ccd979bdSMark Fasheh 412ccd979bdSMark Fasheh /* There is no need to zero out or otherwise initialize the 413ccd979bdSMark Fasheh * other blocks in a group - All valid FS metadata in a block 414ccd979bdSMark Fasheh * group stores the superblock fs_generation value at 415ccd979bdSMark Fasheh * allocation time. */ 416ccd979bdSMark Fasheh 417ccd979bdSMark Fasheh bail: 418ccd979bdSMark Fasheh mlog_exit(status); 419ccd979bdSMark Fasheh return status; 420ccd979bdSMark Fasheh } 421ccd979bdSMark Fasheh 422ccd979bdSMark Fasheh static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl) 423ccd979bdSMark Fasheh { 424ccd979bdSMark Fasheh u16 curr, best; 425ccd979bdSMark Fasheh 426ccd979bdSMark Fasheh best = curr = 0; 427ccd979bdSMark Fasheh while (curr < le16_to_cpu(cl->cl_count)) { 428ccd979bdSMark Fasheh if (le32_to_cpu(cl->cl_recs[best].c_total) > 429ccd979bdSMark Fasheh le32_to_cpu(cl->cl_recs[curr].c_total)) 430ccd979bdSMark Fasheh best = curr; 431ccd979bdSMark Fasheh curr++; 432ccd979bdSMark Fasheh } 433ccd979bdSMark Fasheh return best; 434ccd979bdSMark Fasheh } 435ccd979bdSMark Fasheh 436798db35fSJoel Becker static struct buffer_head * 437798db35fSJoel Becker ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle, 438798db35fSJoel Becker struct inode *alloc_inode, 439798db35fSJoel Becker struct ocfs2_alloc_context *ac, 440798db35fSJoel Becker struct ocfs2_chain_list *cl) 441798db35fSJoel Becker { 442798db35fSJoel Becker int status; 443798db35fSJoel Becker u32 bit_off, num_bits; 444798db35fSJoel Becker u64 bg_blkno; 445798db35fSJoel Becker struct buffer_head *bg_bh; 446798db35fSJoel Becker unsigned int alloc_rec = ocfs2_find_smallest_chain(cl); 447798db35fSJoel Becker 4481ed9b777SJoel Becker status = ocfs2_claim_clusters(handle, ac, 449798db35fSJoel Becker le16_to_cpu(cl->cl_cpg), &bit_off, 450798db35fSJoel Becker &num_bits); 451798db35fSJoel Becker if (status < 0) { 452798db35fSJoel Becker if (status != -ENOSPC) 453798db35fSJoel Becker mlog_errno(status); 454798db35fSJoel Becker goto bail; 455798db35fSJoel Becker } 456798db35fSJoel Becker 457798db35fSJoel Becker /* setup the group */ 458798db35fSJoel Becker bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off); 459798db35fSJoel Becker mlog(0, "new descriptor, record %u, at block %llu\n", 460798db35fSJoel Becker alloc_rec, (unsigned long long)bg_blkno); 461798db35fSJoel Becker 462798db35fSJoel Becker bg_bh = sb_getblk(osb->sb, bg_blkno); 463798db35fSJoel Becker if (!bg_bh) { 464798db35fSJoel Becker status = -EIO; 465798db35fSJoel Becker mlog_errno(status); 466798db35fSJoel Becker goto bail; 467798db35fSJoel Becker } 468798db35fSJoel Becker ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh); 469798db35fSJoel Becker 470798db35fSJoel Becker status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh, 471798db35fSJoel Becker bg_blkno, num_bits, alloc_rec, cl); 472798db35fSJoel Becker if (status < 0) { 473798db35fSJoel Becker brelse(bg_bh); 474798db35fSJoel Becker mlog_errno(status); 475798db35fSJoel Becker } 476798db35fSJoel Becker 477798db35fSJoel Becker bail: 478798db35fSJoel Becker return status ? ERR_PTR(status) : bg_bh; 479798db35fSJoel Becker } 480798db35fSJoel Becker 481798db35fSJoel Becker static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb, 482798db35fSJoel Becker handle_t *handle, 483798db35fSJoel Becker struct ocfs2_alloc_context *ac, 484798db35fSJoel Becker unsigned int min_bits, 485798db35fSJoel Becker u32 *bit_off, u32 *num_bits) 486798db35fSJoel Becker { 48718d3a98fSJoel Becker int status = 0; 488798db35fSJoel Becker 489798db35fSJoel Becker while (min_bits) { 4901ed9b777SJoel Becker status = ocfs2_claim_clusters(handle, ac, min_bits, 491798db35fSJoel Becker bit_off, num_bits); 492798db35fSJoel Becker if (status != -ENOSPC) 493798db35fSJoel Becker break; 494798db35fSJoel Becker 495798db35fSJoel Becker min_bits >>= 1; 496798db35fSJoel Becker } 497798db35fSJoel Becker 498798db35fSJoel Becker return status; 499798db35fSJoel Becker } 500798db35fSJoel Becker 501798db35fSJoel Becker static int ocfs2_block_group_grow_discontig(handle_t *handle, 502798db35fSJoel Becker struct inode *alloc_inode, 503798db35fSJoel Becker struct buffer_head *bg_bh, 504798db35fSJoel Becker struct ocfs2_alloc_context *ac, 505798db35fSJoel Becker struct ocfs2_chain_list *cl, 506798db35fSJoel Becker unsigned int min_bits) 507798db35fSJoel Becker { 508798db35fSJoel Becker int status; 509798db35fSJoel Becker struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); 510798db35fSJoel Becker struct ocfs2_group_desc *bg = 511798db35fSJoel Becker (struct ocfs2_group_desc *)bg_bh->b_data; 5124711954eSTao Ma unsigned int needed = le16_to_cpu(cl->cl_cpg) - 5134711954eSTao Ma le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc); 514798db35fSJoel Becker u32 p_cpos, clusters; 515798db35fSJoel Becker u64 p_blkno; 516798db35fSJoel Becker struct ocfs2_extent_list *el = &bg->bg_list; 517798db35fSJoel Becker 518798db35fSJoel Becker status = ocfs2_journal_access_gd(handle, 519798db35fSJoel Becker INODE_CACHE(alloc_inode), 520798db35fSJoel Becker bg_bh, 521798db35fSJoel Becker OCFS2_JOURNAL_ACCESS_CREATE); 522798db35fSJoel Becker if (status < 0) { 523798db35fSJoel Becker mlog_errno(status); 524798db35fSJoel Becker goto bail; 525798db35fSJoel Becker } 526798db35fSJoel Becker 527798db35fSJoel Becker while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) < 528798db35fSJoel Becker le16_to_cpu(el->l_count))) { 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; 5444711954eSTao Ma needed = le16_to_cpu(cl->cl_cpg) - 5454711954eSTao Ma le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc); 546798db35fSJoel Becker } 547798db35fSJoel Becker 548798db35fSJoel Becker if (needed > 0) { 5494711954eSTao Ma /* 5504711954eSTao Ma * We have used up all the extent rec but can't fill up 5514711954eSTao Ma * the cpg. So bail out. 5524711954eSTao Ma */ 5534711954eSTao Ma status = -ENOSPC; 5544711954eSTao Ma goto bail; 555798db35fSJoel Becker } 556798db35fSJoel Becker 557798db35fSJoel Becker ocfs2_journal_dirty(handle, bg_bh); 558798db35fSJoel Becker 559798db35fSJoel Becker bail: 560798db35fSJoel Becker return status; 561798db35fSJoel Becker } 562798db35fSJoel Becker 5638b06bc59SJoel Becker static void ocfs2_bg_alloc_cleanup(handle_t *handle, 5648b06bc59SJoel Becker struct ocfs2_alloc_context *cluster_ac, 5658b06bc59SJoel Becker struct inode *alloc_inode, 5668b06bc59SJoel Becker struct buffer_head *bg_bh) 567798db35fSJoel Becker { 5688b06bc59SJoel Becker int i, ret; 569798db35fSJoel Becker struct ocfs2_group_desc *bg; 570798db35fSJoel Becker struct ocfs2_extent_list *el; 571798db35fSJoel Becker struct ocfs2_extent_rec *rec; 572798db35fSJoel Becker 573798db35fSJoel Becker if (!bg_bh) 574798db35fSJoel Becker return; 575798db35fSJoel Becker 576798db35fSJoel Becker bg = (struct ocfs2_group_desc *)bg_bh->b_data; 577798db35fSJoel Becker el = &bg->bg_list; 578798db35fSJoel Becker for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { 579798db35fSJoel Becker rec = &el->l_recs[i]; 5808b06bc59SJoel Becker ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode, 5818b06bc59SJoel Becker cluster_ac->ac_bh, 582798db35fSJoel Becker le64_to_cpu(rec->e_blkno), 583798db35fSJoel Becker le32_to_cpu(rec->e_leaf_clusters)); 5848b06bc59SJoel Becker if (ret) 5858b06bc59SJoel Becker mlog_errno(ret); 5868b06bc59SJoel Becker /* Try all the clusters to free */ 587798db35fSJoel Becker } 588798db35fSJoel Becker 589798db35fSJoel Becker ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh); 590798db35fSJoel Becker brelse(bg_bh); 591798db35fSJoel Becker } 592798db35fSJoel Becker 593798db35fSJoel Becker static struct buffer_head * 594798db35fSJoel Becker ocfs2_block_group_alloc_discontig(handle_t *handle, 595798db35fSJoel Becker struct inode *alloc_inode, 596798db35fSJoel Becker struct ocfs2_alloc_context *ac, 5978b06bc59SJoel Becker struct ocfs2_chain_list *cl) 598798db35fSJoel Becker { 599798db35fSJoel Becker int status; 600798db35fSJoel Becker u32 bit_off, num_bits; 601798db35fSJoel Becker u64 bg_blkno; 602798db35fSJoel Becker unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1; 603798db35fSJoel Becker struct buffer_head *bg_bh = NULL; 604798db35fSJoel Becker unsigned int alloc_rec = ocfs2_find_smallest_chain(cl); 605798db35fSJoel Becker struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); 606798db35fSJoel Becker 6074711954eSTao Ma if (!ocfs2_supports_discontig_bg(osb)) { 608798db35fSJoel Becker status = -ENOSPC; 609798db35fSJoel Becker goto bail; 610798db35fSJoel Becker } 611798db35fSJoel Becker 6128b06bc59SJoel Becker status = ocfs2_extend_trans(handle, 6138b06bc59SJoel Becker ocfs2_calc_bg_discontig_credits(osb->sb)); 6148b06bc59SJoel Becker if (status) { 6158b06bc59SJoel Becker mlog_errno(status); 6168b06bc59SJoel Becker goto bail; 6178b06bc59SJoel Becker } 6188b06bc59SJoel Becker 61995ec0adfSJoel Becker /* 62095ec0adfSJoel Becker * We're going to be grabbing from multiple cluster groups. 62195ec0adfSJoel Becker * We don't have enough credits to relink them all, and the 62295ec0adfSJoel Becker * cluster groups will be staying in cache for the duration of 62395ec0adfSJoel Becker * this operation. 62495ec0adfSJoel Becker */ 62595ec0adfSJoel Becker ac->ac_allow_chain_relink = 0; 62695ec0adfSJoel Becker 627798db35fSJoel Becker /* Claim the first region */ 628798db35fSJoel Becker status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits, 629798db35fSJoel Becker &bit_off, &num_bits); 630798db35fSJoel Becker if (status < 0) { 631798db35fSJoel Becker if (status != -ENOSPC) 632798db35fSJoel Becker mlog_errno(status); 633798db35fSJoel Becker goto bail; 634798db35fSJoel Becker } 635798db35fSJoel Becker min_bits = num_bits; 636798db35fSJoel Becker 637798db35fSJoel Becker /* setup the group */ 638798db35fSJoel Becker bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off); 639798db35fSJoel Becker mlog(0, "new descriptor, record %u, at block %llu\n", 640798db35fSJoel Becker alloc_rec, (unsigned long long)bg_blkno); 641798db35fSJoel Becker 642798db35fSJoel Becker bg_bh = sb_getblk(osb->sb, bg_blkno); 643798db35fSJoel Becker if (!bg_bh) { 644798db35fSJoel Becker status = -EIO; 645798db35fSJoel Becker mlog_errno(status); 646798db35fSJoel Becker goto bail; 647798db35fSJoel Becker } 648798db35fSJoel Becker ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh); 649798db35fSJoel Becker 650798db35fSJoel Becker status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh, 651798db35fSJoel Becker bg_blkno, num_bits, alloc_rec, cl); 652798db35fSJoel Becker if (status < 0) { 653798db35fSJoel Becker mlog_errno(status); 654798db35fSJoel Becker goto bail; 655798db35fSJoel Becker } 656798db35fSJoel Becker 657798db35fSJoel Becker status = ocfs2_block_group_grow_discontig(handle, alloc_inode, 658798db35fSJoel Becker bg_bh, ac, cl, min_bits); 659798db35fSJoel Becker if (status) 660798db35fSJoel Becker mlog_errno(status); 661798db35fSJoel Becker 662798db35fSJoel Becker bail: 663798db35fSJoel Becker if (status) 6648b06bc59SJoel Becker ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh); 665798db35fSJoel Becker return status ? ERR_PTR(status) : bg_bh; 666798db35fSJoel Becker } 667798db35fSJoel Becker 668ccd979bdSMark Fasheh /* 669ccd979bdSMark Fasheh * We expect the block group allocator to already be locked. 670ccd979bdSMark Fasheh */ 671ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb, 672ccd979bdSMark Fasheh struct inode *alloc_inode, 6731187c968SJoel Becker struct buffer_head *bh, 67460ca81e8STao Ma u64 max_block, 675feb473a6STao Ma u64 *last_alloc_group, 67660ca81e8STao Ma int flags) 677ccd979bdSMark Fasheh { 678ccd979bdSMark Fasheh int status, credits; 679ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data; 680ccd979bdSMark Fasheh struct ocfs2_chain_list *cl; 681ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac = NULL; 6821fabe148SMark Fasheh handle_t *handle = NULL; 6834711954eSTao Ma u16 alloc_rec; 684ccd979bdSMark Fasheh struct buffer_head *bg_bh = NULL; 685ccd979bdSMark Fasheh struct ocfs2_group_desc *bg; 686ccd979bdSMark Fasheh 687ccd979bdSMark Fasheh BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode)); 688ccd979bdSMark Fasheh 689ccd979bdSMark Fasheh mlog_entry_void(); 690ccd979bdSMark Fasheh 691ccd979bdSMark Fasheh cl = &fe->id2.i_chain; 6921187c968SJoel Becker status = ocfs2_reserve_clusters_with_limit(osb, 693ccd979bdSMark Fasheh le16_to_cpu(cl->cl_cpg), 69460ca81e8STao Ma max_block, flags, &ac); 695ccd979bdSMark Fasheh if (status < 0) { 696ccd979bdSMark Fasheh if (status != -ENOSPC) 697ccd979bdSMark Fasheh mlog_errno(status); 698ccd979bdSMark Fasheh goto bail; 699ccd979bdSMark Fasheh } 700ccd979bdSMark Fasheh 701ccd979bdSMark Fasheh credits = ocfs2_calc_group_alloc_credits(osb->sb, 702ccd979bdSMark Fasheh le16_to_cpu(cl->cl_cpg)); 70365eff9ccSMark Fasheh handle = ocfs2_start_trans(osb, credits); 704ccd979bdSMark Fasheh if (IS_ERR(handle)) { 705ccd979bdSMark Fasheh status = PTR_ERR(handle); 706ccd979bdSMark Fasheh handle = NULL; 707ccd979bdSMark Fasheh mlog_errno(status); 708ccd979bdSMark Fasheh goto bail; 709ccd979bdSMark Fasheh } 710ccd979bdSMark Fasheh 711feb473a6STao Ma if (last_alloc_group && *last_alloc_group != 0) { 712feb473a6STao Ma mlog(0, "use old allocation group %llu for block group alloc\n", 713feb473a6STao Ma (unsigned long long)*last_alloc_group); 714feb473a6STao Ma ac->ac_last_group = *last_alloc_group; 715feb473a6STao Ma } 716798db35fSJoel Becker 717798db35fSJoel Becker bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode, 718798db35fSJoel Becker ac, cl); 719798db35fSJoel Becker if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC)) 720798db35fSJoel Becker bg_bh = ocfs2_block_group_alloc_discontig(handle, 721798db35fSJoel Becker alloc_inode, 7228b06bc59SJoel Becker ac, cl); 723798db35fSJoel Becker if (IS_ERR(bg_bh)) { 724798db35fSJoel Becker status = PTR_ERR(bg_bh); 725798db35fSJoel Becker bg_bh = NULL; 726ccd979bdSMark Fasheh if (status != -ENOSPC) 727ccd979bdSMark Fasheh mlog_errno(status); 728ccd979bdSMark Fasheh goto bail; 729ccd979bdSMark Fasheh } 730ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) bg_bh->b_data; 731ccd979bdSMark Fasheh 7320cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), 733ccd979bdSMark Fasheh bh, OCFS2_JOURNAL_ACCESS_WRITE); 734ccd979bdSMark Fasheh if (status < 0) { 735ccd979bdSMark Fasheh mlog_errno(status); 736ccd979bdSMark Fasheh goto bail; 737ccd979bdSMark Fasheh } 738ccd979bdSMark Fasheh 7394711954eSTao Ma alloc_rec = le16_to_cpu(bg->bg_chain); 7404711954eSTao Ma le32_add_cpu(&cl->cl_recs[alloc_rec].c_free, 741ccd979bdSMark Fasheh le16_to_cpu(bg->bg_free_bits_count)); 7424711954eSTao Ma le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, 743798db35fSJoel Becker le16_to_cpu(bg->bg_bits)); 744*0a463b74STao Ma cl->cl_recs[alloc_rec].c_blkno = bg->bg_blkno; 745ccd979bdSMark Fasheh if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count)) 746ccd979bdSMark Fasheh le16_add_cpu(&cl->cl_next_free_rec, 1); 747ccd979bdSMark Fasheh 748ccd979bdSMark Fasheh le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) - 749ccd979bdSMark Fasheh le16_to_cpu(bg->bg_free_bits_count)); 750ccd979bdSMark Fasheh le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits)); 751ccd979bdSMark Fasheh le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg)); 752ccd979bdSMark Fasheh 753ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bh); 754ccd979bdSMark Fasheh 755ccd979bdSMark Fasheh spin_lock(&OCFS2_I(alloc_inode)->ip_lock); 756ccd979bdSMark Fasheh OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters); 757ccd979bdSMark Fasheh fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb, 758ccd979bdSMark Fasheh le32_to_cpu(fe->i_clusters))); 759ccd979bdSMark Fasheh spin_unlock(&OCFS2_I(alloc_inode)->ip_lock); 760ccd979bdSMark Fasheh i_size_write(alloc_inode, le64_to_cpu(fe->i_size)); 7618110b073SMark Fasheh alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode); 762ccd979bdSMark Fasheh 763ccd979bdSMark Fasheh status = 0; 764feb473a6STao Ma 765feb473a6STao Ma /* save the new last alloc group so that the caller can cache it. */ 766feb473a6STao Ma if (last_alloc_group) 767feb473a6STao Ma *last_alloc_group = ac->ac_last_group; 768feb473a6STao Ma 769ccd979bdSMark Fasheh bail: 770ccd979bdSMark Fasheh if (handle) 77102dc1af4SMark Fasheh ocfs2_commit_trans(osb, handle); 772ccd979bdSMark Fasheh 773ccd979bdSMark Fasheh if (ac) 774ccd979bdSMark Fasheh ocfs2_free_alloc_context(ac); 775ccd979bdSMark Fasheh 776ccd979bdSMark Fasheh brelse(bg_bh); 777ccd979bdSMark Fasheh 778ccd979bdSMark Fasheh mlog_exit(status); 779ccd979bdSMark Fasheh return status; 780ccd979bdSMark Fasheh } 781ccd979bdSMark Fasheh 782ccd979bdSMark Fasheh static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb, 783da5cbf2fSMark Fasheh struct ocfs2_alloc_context *ac, 784da5cbf2fSMark Fasheh int type, 785ffda89a3STao Ma u32 slot, 786feb473a6STao Ma u64 *last_alloc_group, 78760ca81e8STao Ma int flags) 788ccd979bdSMark Fasheh { 789ccd979bdSMark Fasheh int status; 790ccd979bdSMark Fasheh u32 bits_wanted = ac->ac_bits_wanted; 791da5cbf2fSMark Fasheh struct inode *alloc_inode; 792ccd979bdSMark Fasheh struct buffer_head *bh = NULL; 793ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 794ccd979bdSMark Fasheh u32 free_bits; 795ccd979bdSMark Fasheh 796ccd979bdSMark Fasheh mlog_entry_void(); 797ccd979bdSMark Fasheh 798da5cbf2fSMark Fasheh alloc_inode = ocfs2_get_system_file_inode(osb, type, slot); 799da5cbf2fSMark Fasheh if (!alloc_inode) { 800da5cbf2fSMark Fasheh mlog_errno(-EINVAL); 801da5cbf2fSMark Fasheh return -EINVAL; 802ccd979bdSMark Fasheh } 803ccd979bdSMark Fasheh 804da5cbf2fSMark Fasheh mutex_lock(&alloc_inode->i_mutex); 805da5cbf2fSMark Fasheh 806e63aecb6SMark Fasheh status = ocfs2_inode_lock(alloc_inode, &bh, 1); 807da5cbf2fSMark Fasheh if (status < 0) { 808da5cbf2fSMark Fasheh mutex_unlock(&alloc_inode->i_mutex); 809da5cbf2fSMark Fasheh iput(alloc_inode); 810da5cbf2fSMark Fasheh 811da5cbf2fSMark Fasheh mlog_errno(status); 812da5cbf2fSMark Fasheh return status; 813da5cbf2fSMark Fasheh } 814da5cbf2fSMark Fasheh 815da5cbf2fSMark Fasheh ac->ac_inode = alloc_inode; 816a4a48911STao Ma ac->ac_alloc_slot = slot; 817da5cbf2fSMark Fasheh 818ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) bh->b_data; 81910995aa2SJoel Becker 82010995aa2SJoel Becker /* The bh was validated by the inode read inside 82110995aa2SJoel Becker * ocfs2_inode_lock(). Any corruption is a code bug. */ 82210995aa2SJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); 82310995aa2SJoel Becker 824ccd979bdSMark Fasheh if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) { 825b0697053SMark Fasheh ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu", 826b0697053SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno)); 827ccd979bdSMark Fasheh status = -EIO; 828ccd979bdSMark Fasheh goto bail; 829ccd979bdSMark Fasheh } 830ccd979bdSMark Fasheh 831ccd979bdSMark Fasheh free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) - 832ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_used); 833ccd979bdSMark Fasheh 834ccd979bdSMark Fasheh if (bits_wanted > free_bits) { 835ccd979bdSMark Fasheh /* cluster bitmap never grows */ 836ccd979bdSMark Fasheh if (ocfs2_is_cluster_bitmap(alloc_inode)) { 837ccd979bdSMark Fasheh mlog(0, "Disk Full: wanted=%u, free_bits=%u\n", 838ccd979bdSMark Fasheh bits_wanted, free_bits); 839ccd979bdSMark Fasheh status = -ENOSPC; 840ccd979bdSMark Fasheh goto bail; 841ccd979bdSMark Fasheh } 842ccd979bdSMark Fasheh 84360ca81e8STao Ma if (!(flags & ALLOC_NEW_GROUP)) { 844ffda89a3STao Ma mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, " 845ffda89a3STao Ma "and we don't alloc a new group for it.\n", 846ffda89a3STao Ma slot, bits_wanted, free_bits); 847ffda89a3STao Ma status = -ENOSPC; 848ffda89a3STao Ma goto bail; 849ffda89a3STao Ma } 850ffda89a3STao Ma 8511187c968SJoel Becker status = ocfs2_block_group_alloc(osb, alloc_inode, bh, 852feb473a6STao Ma ac->ac_max_block, 853feb473a6STao Ma last_alloc_group, flags); 854ccd979bdSMark Fasheh if (status < 0) { 855ccd979bdSMark Fasheh if (status != -ENOSPC) 856ccd979bdSMark Fasheh mlog_errno(status); 857ccd979bdSMark Fasheh goto bail; 858ccd979bdSMark Fasheh } 859ccd979bdSMark Fasheh atomic_inc(&osb->alloc_stats.bg_extends); 860ccd979bdSMark Fasheh 861ccd979bdSMark Fasheh /* You should never ask for this much metadata */ 862ccd979bdSMark Fasheh BUG_ON(bits_wanted > 863ccd979bdSMark Fasheh (le32_to_cpu(fe->id1.bitmap1.i_total) 864ccd979bdSMark Fasheh - le32_to_cpu(fe->id1.bitmap1.i_used))); 865ccd979bdSMark Fasheh } 866ccd979bdSMark Fasheh 867ccd979bdSMark Fasheh get_bh(bh); 868ccd979bdSMark Fasheh ac->ac_bh = bh; 869ccd979bdSMark Fasheh bail: 870ccd979bdSMark Fasheh brelse(bh); 871ccd979bdSMark Fasheh 872ccd979bdSMark Fasheh mlog_exit(status); 873ccd979bdSMark Fasheh return status; 874ccd979bdSMark Fasheh } 875ccd979bdSMark Fasheh 876b89c5428STiger Yang static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb) 877b89c5428STiger Yang { 878b89c5428STiger Yang spin_lock(&osb->osb_lock); 879b89c5428STiger Yang osb->s_inode_steal_slot = OCFS2_INVALID_SLOT; 880b89c5428STiger Yang spin_unlock(&osb->osb_lock); 881b89c5428STiger Yang atomic_set(&osb->s_num_inodes_stolen, 0); 882b89c5428STiger Yang } 883b89c5428STiger Yang 884b89c5428STiger Yang static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb) 885b89c5428STiger Yang { 886b89c5428STiger Yang spin_lock(&osb->osb_lock); 887b89c5428STiger Yang osb->s_meta_steal_slot = OCFS2_INVALID_SLOT; 888b89c5428STiger Yang spin_unlock(&osb->osb_lock); 889b89c5428STiger Yang atomic_set(&osb->s_num_meta_stolen, 0); 890b89c5428STiger Yang } 891b89c5428STiger Yang 892b89c5428STiger Yang void ocfs2_init_steal_slots(struct ocfs2_super *osb) 893b89c5428STiger Yang { 894b89c5428STiger Yang ocfs2_init_inode_steal_slot(osb); 895b89c5428STiger Yang ocfs2_init_meta_steal_slot(osb); 896b89c5428STiger Yang } 897b89c5428STiger Yang 898b89c5428STiger Yang static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type) 899b89c5428STiger Yang { 900b89c5428STiger Yang spin_lock(&osb->osb_lock); 901b89c5428STiger Yang if (type == INODE_ALLOC_SYSTEM_INODE) 902b89c5428STiger Yang osb->s_inode_steal_slot = slot; 903b89c5428STiger Yang else if (type == EXTENT_ALLOC_SYSTEM_INODE) 904b89c5428STiger Yang osb->s_meta_steal_slot = slot; 905b89c5428STiger Yang spin_unlock(&osb->osb_lock); 906b89c5428STiger Yang } 907b89c5428STiger Yang 908b89c5428STiger Yang static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type) 909b89c5428STiger Yang { 910b89c5428STiger Yang int slot = OCFS2_INVALID_SLOT; 911b89c5428STiger Yang 912b89c5428STiger Yang spin_lock(&osb->osb_lock); 913b89c5428STiger Yang if (type == INODE_ALLOC_SYSTEM_INODE) 914b89c5428STiger Yang slot = osb->s_inode_steal_slot; 915b89c5428STiger Yang else if (type == EXTENT_ALLOC_SYSTEM_INODE) 916b89c5428STiger Yang slot = osb->s_meta_steal_slot; 917b89c5428STiger Yang spin_unlock(&osb->osb_lock); 918b89c5428STiger Yang 919b89c5428STiger Yang return slot; 920b89c5428STiger Yang } 921b89c5428STiger Yang 922b89c5428STiger Yang static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb) 923b89c5428STiger Yang { 924b89c5428STiger Yang return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE); 925b89c5428STiger Yang } 926b89c5428STiger Yang 927b89c5428STiger Yang static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb) 928b89c5428STiger Yang { 929b89c5428STiger Yang return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE); 930b89c5428STiger Yang } 931b89c5428STiger Yang 932b89c5428STiger Yang static int ocfs2_steal_resource(struct ocfs2_super *osb, 933b89c5428STiger Yang struct ocfs2_alloc_context *ac, 934b89c5428STiger Yang int type) 935b89c5428STiger Yang { 936b89c5428STiger Yang int i, status = -ENOSPC; 937b89c5428STiger Yang int slot = __ocfs2_get_steal_slot(osb, type); 938b89c5428STiger Yang 939b89c5428STiger Yang /* Start to steal resource from the first slot after ours. */ 940b89c5428STiger Yang if (slot == OCFS2_INVALID_SLOT) 941b89c5428STiger Yang slot = osb->slot_num + 1; 942b89c5428STiger Yang 943b89c5428STiger Yang for (i = 0; i < osb->max_slots; i++, slot++) { 944b89c5428STiger Yang if (slot == osb->max_slots) 945b89c5428STiger Yang slot = 0; 946b89c5428STiger Yang 947b89c5428STiger Yang if (slot == osb->slot_num) 948b89c5428STiger Yang continue; 949b89c5428STiger Yang 950b89c5428STiger Yang status = ocfs2_reserve_suballoc_bits(osb, ac, 951b89c5428STiger Yang type, 952b89c5428STiger Yang (u32)slot, NULL, 953b89c5428STiger Yang NOT_ALLOC_NEW_GROUP); 954b89c5428STiger Yang if (status >= 0) { 955b89c5428STiger Yang __ocfs2_set_steal_slot(osb, slot, type); 956b89c5428STiger Yang break; 957b89c5428STiger Yang } 958b89c5428STiger Yang 959b89c5428STiger Yang ocfs2_free_ac_resource(ac); 960b89c5428STiger Yang } 961b89c5428STiger Yang 962b89c5428STiger Yang return status; 963b89c5428STiger Yang } 964b89c5428STiger Yang 965b89c5428STiger Yang static int ocfs2_steal_inode(struct ocfs2_super *osb, 966b89c5428STiger Yang struct ocfs2_alloc_context *ac) 967b89c5428STiger Yang { 968b89c5428STiger Yang return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE); 969b89c5428STiger Yang } 970b89c5428STiger Yang 971b89c5428STiger Yang static int ocfs2_steal_meta(struct ocfs2_super *osb, 972b89c5428STiger Yang struct ocfs2_alloc_context *ac) 973b89c5428STiger Yang { 974b89c5428STiger Yang return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE); 975b89c5428STiger Yang } 976b89c5428STiger Yang 977cf1d6c76STiger Yang int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb, 978cf1d6c76STiger Yang int blocks, 979ccd979bdSMark Fasheh struct ocfs2_alloc_context **ac) 980ccd979bdSMark Fasheh { 981ccd979bdSMark Fasheh int status; 982b89c5428STiger Yang int slot = ocfs2_get_meta_steal_slot(osb); 983ccd979bdSMark Fasheh 984cd861280SRobert P. J. Day *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); 985ccd979bdSMark Fasheh if (!(*ac)) { 986ccd979bdSMark Fasheh status = -ENOMEM; 987ccd979bdSMark Fasheh mlog_errno(status); 988ccd979bdSMark Fasheh goto bail; 989ccd979bdSMark Fasheh } 990ccd979bdSMark Fasheh 991cf1d6c76STiger Yang (*ac)->ac_bits_wanted = blocks; 992ccd979bdSMark Fasheh (*ac)->ac_which = OCFS2_AC_USE_META; 993ccd979bdSMark Fasheh (*ac)->ac_group_search = ocfs2_block_group_search; 994ccd979bdSMark Fasheh 995b89c5428STiger Yang if (slot != OCFS2_INVALID_SLOT && 996b89c5428STiger Yang atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL) 997b89c5428STiger Yang goto extent_steal; 998b89c5428STiger Yang 999b89c5428STiger Yang atomic_set(&osb->s_num_meta_stolen, 0); 1000da5cbf2fSMark Fasheh status = ocfs2_reserve_suballoc_bits(osb, (*ac), 1001ffda89a3STao Ma EXTENT_ALLOC_SYSTEM_INODE, 1002b89c5428STiger Yang (u32)osb->slot_num, NULL, 100333d5d380SMark Fasheh ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP); 1004b89c5428STiger Yang 1005b89c5428STiger Yang 1006b89c5428STiger Yang if (status >= 0) { 1007b89c5428STiger Yang status = 0; 1008b89c5428STiger Yang if (slot != OCFS2_INVALID_SLOT) 1009b89c5428STiger Yang ocfs2_init_meta_steal_slot(osb); 1010b89c5428STiger Yang goto bail; 1011b89c5428STiger Yang } else if (status < 0 && status != -ENOSPC) { 1012b89c5428STiger Yang mlog_errno(status); 1013b89c5428STiger Yang goto bail; 1014b89c5428STiger Yang } 1015b89c5428STiger Yang 1016b89c5428STiger Yang ocfs2_free_ac_resource(*ac); 1017b89c5428STiger Yang 1018b89c5428STiger Yang extent_steal: 1019b89c5428STiger Yang status = ocfs2_steal_meta(osb, *ac); 1020b89c5428STiger Yang atomic_inc(&osb->s_num_meta_stolen); 1021ccd979bdSMark Fasheh if (status < 0) { 1022ccd979bdSMark Fasheh if (status != -ENOSPC) 1023ccd979bdSMark Fasheh mlog_errno(status); 1024ccd979bdSMark Fasheh goto bail; 1025ccd979bdSMark Fasheh } 1026ccd979bdSMark Fasheh 1027ccd979bdSMark Fasheh status = 0; 1028ccd979bdSMark Fasheh bail: 1029ccd979bdSMark Fasheh if ((status < 0) && *ac) { 1030ccd979bdSMark Fasheh ocfs2_free_alloc_context(*ac); 1031ccd979bdSMark Fasheh *ac = NULL; 1032ccd979bdSMark Fasheh } 1033ccd979bdSMark Fasheh 1034ccd979bdSMark Fasheh mlog_exit(status); 1035ccd979bdSMark Fasheh return status; 1036ccd979bdSMark Fasheh } 1037ccd979bdSMark Fasheh 1038cf1d6c76STiger Yang int ocfs2_reserve_new_metadata(struct ocfs2_super *osb, 1039cf1d6c76STiger Yang struct ocfs2_extent_list *root_el, 1040cf1d6c76STiger Yang struct ocfs2_alloc_context **ac) 1041cf1d6c76STiger Yang { 1042cf1d6c76STiger Yang return ocfs2_reserve_new_metadata_blocks(osb, 1043cf1d6c76STiger Yang ocfs2_extend_meta_needed(root_el), 1044cf1d6c76STiger Yang ac); 1045cf1d6c76STiger Yang } 1046cf1d6c76STiger Yang 1047ccd979bdSMark Fasheh int ocfs2_reserve_new_inode(struct ocfs2_super *osb, 1048ccd979bdSMark Fasheh struct ocfs2_alloc_context **ac) 1049ccd979bdSMark Fasheh { 1050ccd979bdSMark Fasheh int status; 1051b89c5428STiger Yang int slot = ocfs2_get_inode_steal_slot(osb); 1052feb473a6STao Ma u64 alloc_group; 1053ccd979bdSMark Fasheh 1054cd861280SRobert P. J. Day *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); 1055ccd979bdSMark Fasheh if (!(*ac)) { 1056ccd979bdSMark Fasheh status = -ENOMEM; 1057ccd979bdSMark Fasheh mlog_errno(status); 1058ccd979bdSMark Fasheh goto bail; 1059ccd979bdSMark Fasheh } 1060ccd979bdSMark Fasheh 1061ccd979bdSMark Fasheh (*ac)->ac_bits_wanted = 1; 1062ccd979bdSMark Fasheh (*ac)->ac_which = OCFS2_AC_USE_INODE; 1063ccd979bdSMark Fasheh 1064ccd979bdSMark Fasheh (*ac)->ac_group_search = ocfs2_block_group_search; 1065ccd979bdSMark Fasheh 10664d0ddb2cSTao Ma /* 10671187c968SJoel Becker * stat(2) can't handle i_ino > 32bits, so we tell the 10681187c968SJoel Becker * lower levels not to allocate us a block group past that 106912462f1dSJoel Becker * limit. The 'inode64' mount option avoids this behavior. 10701187c968SJoel Becker */ 107112462f1dSJoel Becker if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64)) 10721187c968SJoel Becker (*ac)->ac_max_block = (u32)~0U; 10731187c968SJoel Becker 10741187c968SJoel Becker /* 10754d0ddb2cSTao Ma * slot is set when we successfully steal inode from other nodes. 10764d0ddb2cSTao Ma * It is reset in 3 places: 10774d0ddb2cSTao Ma * 1. when we flush the truncate log 10784d0ddb2cSTao Ma * 2. when we complete local alloc recovery. 10794d0ddb2cSTao Ma * 3. when we successfully allocate from our own slot. 10804d0ddb2cSTao Ma * After it is set, we will go on stealing inodes until we find the 10814d0ddb2cSTao Ma * need to check our slots to see whether there is some space for us. 10824d0ddb2cSTao Ma */ 10834d0ddb2cSTao Ma if (slot != OCFS2_INVALID_SLOT && 1084b89c5428STiger Yang atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL) 10854d0ddb2cSTao Ma goto inode_steal; 10864d0ddb2cSTao Ma 10874d0ddb2cSTao Ma atomic_set(&osb->s_num_inodes_stolen, 0); 1088feb473a6STao Ma alloc_group = osb->osb_inode_alloc_group; 1089da5cbf2fSMark Fasheh status = ocfs2_reserve_suballoc_bits(osb, *ac, 1090da5cbf2fSMark Fasheh INODE_ALLOC_SYSTEM_INODE, 1091b89c5428STiger Yang (u32)osb->slot_num, 1092feb473a6STao Ma &alloc_group, 109360ca81e8STao Ma ALLOC_NEW_GROUP | 109460ca81e8STao Ma ALLOC_GROUPS_FROM_GLOBAL); 10954d0ddb2cSTao Ma if (status >= 0) { 10964d0ddb2cSTao Ma status = 0; 10974d0ddb2cSTao Ma 1098feb473a6STao Ma spin_lock(&osb->osb_lock); 1099feb473a6STao Ma osb->osb_inode_alloc_group = alloc_group; 1100feb473a6STao Ma spin_unlock(&osb->osb_lock); 1101feb473a6STao Ma mlog(0, "after reservation, new allocation group is " 1102feb473a6STao Ma "%llu\n", (unsigned long long)alloc_group); 1103feb473a6STao Ma 11044d0ddb2cSTao Ma /* 11054d0ddb2cSTao Ma * Some inodes must be freed by us, so try to allocate 11064d0ddb2cSTao Ma * from our own next time. 11074d0ddb2cSTao Ma */ 11084d0ddb2cSTao Ma if (slot != OCFS2_INVALID_SLOT) 11094d0ddb2cSTao Ma ocfs2_init_inode_steal_slot(osb); 11104d0ddb2cSTao Ma goto bail; 11114d0ddb2cSTao Ma } else if (status < 0 && status != -ENOSPC) { 11124d0ddb2cSTao Ma mlog_errno(status); 11134d0ddb2cSTao Ma goto bail; 11144d0ddb2cSTao Ma } 11154d0ddb2cSTao Ma 11164d0ddb2cSTao Ma ocfs2_free_ac_resource(*ac); 11174d0ddb2cSTao Ma 11184d0ddb2cSTao Ma inode_steal: 1119b89c5428STiger Yang status = ocfs2_steal_inode(osb, *ac); 11204d0ddb2cSTao Ma atomic_inc(&osb->s_num_inodes_stolen); 1121ccd979bdSMark Fasheh if (status < 0) { 1122ccd979bdSMark Fasheh if (status != -ENOSPC) 1123ccd979bdSMark Fasheh mlog_errno(status); 1124ccd979bdSMark Fasheh goto bail; 1125ccd979bdSMark Fasheh } 1126ccd979bdSMark Fasheh 1127ccd979bdSMark Fasheh status = 0; 1128ccd979bdSMark Fasheh bail: 1129ccd979bdSMark Fasheh if ((status < 0) && *ac) { 1130ccd979bdSMark Fasheh ocfs2_free_alloc_context(*ac); 1131ccd979bdSMark Fasheh *ac = NULL; 1132ccd979bdSMark Fasheh } 1133ccd979bdSMark Fasheh 1134ccd979bdSMark Fasheh mlog_exit(status); 1135ccd979bdSMark Fasheh return status; 1136ccd979bdSMark Fasheh } 1137ccd979bdSMark Fasheh 1138ccd979bdSMark Fasheh /* local alloc code has to do the same thing, so rather than do this 1139ccd979bdSMark Fasheh * twice.. */ 1140ccd979bdSMark Fasheh int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb, 1141ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac) 1142ccd979bdSMark Fasheh { 1143ccd979bdSMark Fasheh int status; 1144ccd979bdSMark Fasheh 1145ccd979bdSMark Fasheh ac->ac_which = OCFS2_AC_USE_MAIN; 1146ccd979bdSMark Fasheh ac->ac_group_search = ocfs2_cluster_group_search; 1147ccd979bdSMark Fasheh 1148da5cbf2fSMark Fasheh status = ocfs2_reserve_suballoc_bits(osb, ac, 1149da5cbf2fSMark Fasheh GLOBAL_BITMAP_SYSTEM_INODE, 1150feb473a6STao Ma OCFS2_INVALID_SLOT, NULL, 1151ffda89a3STao Ma ALLOC_NEW_GROUP); 1152da5cbf2fSMark Fasheh if (status < 0 && status != -ENOSPC) { 1153ccd979bdSMark Fasheh mlog_errno(status); 1154da5cbf2fSMark Fasheh goto bail; 1155da5cbf2fSMark Fasheh } 1156da5cbf2fSMark Fasheh 1157ccd979bdSMark Fasheh bail: 1158ccd979bdSMark Fasheh return status; 1159ccd979bdSMark Fasheh } 1160ccd979bdSMark Fasheh 1161ccd979bdSMark Fasheh /* Callers don't need to care which bitmap (local alloc or main) to 1162ccd979bdSMark Fasheh * use so we figure it out for them, but unfortunately this clutters 1163ccd979bdSMark Fasheh * things a bit. */ 11641187c968SJoel Becker static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb, 11651187c968SJoel Becker u32 bits_wanted, u64 max_block, 116660ca81e8STao Ma int flags, 1167ccd979bdSMark Fasheh struct ocfs2_alloc_context **ac) 1168ccd979bdSMark Fasheh { 1169ccd979bdSMark Fasheh int status; 1170ccd979bdSMark Fasheh 1171ccd979bdSMark Fasheh mlog_entry_void(); 1172ccd979bdSMark Fasheh 1173cd861280SRobert P. J. Day *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); 1174ccd979bdSMark Fasheh if (!(*ac)) { 1175ccd979bdSMark Fasheh status = -ENOMEM; 1176ccd979bdSMark Fasheh mlog_errno(status); 1177ccd979bdSMark Fasheh goto bail; 1178ccd979bdSMark Fasheh } 1179ccd979bdSMark Fasheh 1180ccd979bdSMark Fasheh (*ac)->ac_bits_wanted = bits_wanted; 11811187c968SJoel Becker (*ac)->ac_max_block = max_block; 1182ccd979bdSMark Fasheh 1183ccd979bdSMark Fasheh status = -ENOSPC; 118460ca81e8STao Ma if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) && 118560ca81e8STao Ma ocfs2_alloc_should_use_local(osb, bits_wanted)) { 1186ccd979bdSMark Fasheh status = ocfs2_reserve_local_alloc_bits(osb, 1187ccd979bdSMark Fasheh bits_wanted, 1188ccd979bdSMark Fasheh *ac); 1189a57c8fd2SMark Fasheh if ((status < 0) && (status != -ENOSPC)) { 1190ccd979bdSMark Fasheh mlog_errno(status); 1191ccd979bdSMark Fasheh goto bail; 1192ccd979bdSMark Fasheh } 1193ccd979bdSMark Fasheh } 1194ccd979bdSMark Fasheh 1195ccd979bdSMark Fasheh if (status == -ENOSPC) { 1196ccd979bdSMark Fasheh status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac); 1197ccd979bdSMark Fasheh if (status < 0) { 1198ccd979bdSMark Fasheh if (status != -ENOSPC) 1199ccd979bdSMark Fasheh mlog_errno(status); 1200ccd979bdSMark Fasheh goto bail; 1201ccd979bdSMark Fasheh } 1202ccd979bdSMark Fasheh } 1203ccd979bdSMark Fasheh 1204ccd979bdSMark Fasheh status = 0; 1205ccd979bdSMark Fasheh bail: 1206ccd979bdSMark Fasheh if ((status < 0) && *ac) { 1207ccd979bdSMark Fasheh ocfs2_free_alloc_context(*ac); 1208ccd979bdSMark Fasheh *ac = NULL; 1209ccd979bdSMark Fasheh } 1210ccd979bdSMark Fasheh 1211ccd979bdSMark Fasheh mlog_exit(status); 1212ccd979bdSMark Fasheh return status; 1213ccd979bdSMark Fasheh } 1214ccd979bdSMark Fasheh 12151187c968SJoel Becker int ocfs2_reserve_clusters(struct ocfs2_super *osb, 12161187c968SJoel Becker u32 bits_wanted, 12171187c968SJoel Becker struct ocfs2_alloc_context **ac) 12181187c968SJoel Becker { 121960ca81e8STao Ma return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0, 122060ca81e8STao Ma ALLOC_NEW_GROUP, ac); 12211187c968SJoel Becker } 12221187c968SJoel Becker 1223ccd979bdSMark Fasheh /* 1224ccd979bdSMark Fasheh * More or less lifted from ext3. I'll leave their description below: 1225ccd979bdSMark Fasheh * 1226ccd979bdSMark Fasheh * "For ext3 allocations, we must not reuse any blocks which are 1227ccd979bdSMark Fasheh * allocated in the bitmap buffer's "last committed data" copy. This 1228ccd979bdSMark Fasheh * prevents deletes from freeing up the page for reuse until we have 1229ccd979bdSMark Fasheh * committed the delete transaction. 1230ccd979bdSMark Fasheh * 1231ccd979bdSMark Fasheh * If we didn't do this, then deleting something and reallocating it as 1232ccd979bdSMark Fasheh * data would allow the old block to be overwritten before the 1233ccd979bdSMark Fasheh * transaction committed (because we force data to disk before commit). 1234ccd979bdSMark Fasheh * This would lead to corruption if we crashed between overwriting the 1235ccd979bdSMark Fasheh * data and committing the delete. 1236ccd979bdSMark Fasheh * 1237ccd979bdSMark Fasheh * @@@ We may want to make this allocation behaviour conditional on 1238ccd979bdSMark Fasheh * data-writes at some point, and disable it for metadata allocations or 1239ccd979bdSMark Fasheh * sync-data inodes." 1240ccd979bdSMark Fasheh * 1241ccd979bdSMark Fasheh * Note: OCFS2 already does this differently for metadata vs data 1242c78bad11SJoe Perches * allocations, as those bitmaps are separate and undo access is never 1243ccd979bdSMark Fasheh * called on a metadata group descriptor. 1244ccd979bdSMark Fasheh */ 1245ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh, 1246ccd979bdSMark Fasheh int nr) 1247ccd979bdSMark Fasheh { 1248ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 124994e41ecfSSunil Mushran int ret; 1250ccd979bdSMark Fasheh 1251ccd979bdSMark Fasheh if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap)) 1252ccd979bdSMark Fasheh return 0; 125394e41ecfSSunil Mushran 125494e41ecfSSunil Mushran if (!buffer_jbd(bg_bh)) 1255ccd979bdSMark Fasheh return 1; 1256ccd979bdSMark Fasheh 125794e41ecfSSunil Mushran jbd_lock_bh_state(bg_bh); 1258ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data; 125994e41ecfSSunil Mushran if (bg) 126094e41ecfSSunil Mushran ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap); 126194e41ecfSSunil Mushran else 126294e41ecfSSunil Mushran ret = 1; 126394e41ecfSSunil Mushran jbd_unlock_bh_state(bg_bh); 126494e41ecfSSunil Mushran 126594e41ecfSSunil Mushran return ret; 1266ccd979bdSMark Fasheh } 1267ccd979bdSMark Fasheh 1268ccd979bdSMark Fasheh static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb, 1269ccd979bdSMark Fasheh struct buffer_head *bg_bh, 1270ccd979bdSMark Fasheh unsigned int bits_wanted, 12717bf72edeSMark Fasheh unsigned int total_bits, 12727d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1273ccd979bdSMark Fasheh { 1274ccd979bdSMark Fasheh void *bitmap; 1275ccd979bdSMark Fasheh u16 best_offset, best_size; 1276ccd979bdSMark Fasheh int offset, start, found, status = 0; 1277ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 1278ccd979bdSMark Fasheh 127942035306SJoel Becker /* Callers got this descriptor from 128042035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 128142035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 1282ccd979bdSMark Fasheh 1283ccd979bdSMark Fasheh found = start = best_offset = best_size = 0; 1284ccd979bdSMark Fasheh bitmap = bg->bg_bitmap; 1285ccd979bdSMark Fasheh 12867bf72edeSMark Fasheh while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) { 12877bf72edeSMark Fasheh if (offset == total_bits) 1288ccd979bdSMark Fasheh break; 1289ccd979bdSMark Fasheh 1290ccd979bdSMark Fasheh if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) { 1291ccd979bdSMark Fasheh /* We found a zero, but we can't use it as it 1292ccd979bdSMark Fasheh * hasn't been put to disk yet! */ 1293ccd979bdSMark Fasheh found = 0; 1294ccd979bdSMark Fasheh start = offset + 1; 1295ccd979bdSMark Fasheh } else if (offset == start) { 1296ccd979bdSMark Fasheh /* we found a zero */ 1297ccd979bdSMark Fasheh found++; 1298ccd979bdSMark Fasheh /* move start to the next bit to test */ 1299ccd979bdSMark Fasheh start++; 1300ccd979bdSMark Fasheh } else { 1301ccd979bdSMark Fasheh /* got a zero after some ones */ 1302ccd979bdSMark Fasheh found = 1; 1303ccd979bdSMark Fasheh start = offset + 1; 1304ccd979bdSMark Fasheh } 1305ccd979bdSMark Fasheh if (found > best_size) { 1306ccd979bdSMark Fasheh best_size = found; 1307ccd979bdSMark Fasheh best_offset = start - found; 1308ccd979bdSMark Fasheh } 1309ccd979bdSMark Fasheh /* we got everything we needed */ 1310ccd979bdSMark Fasheh if (found == bits_wanted) { 1311ccd979bdSMark Fasheh /* mlog(0, "Found it all!\n"); */ 1312ccd979bdSMark Fasheh break; 1313ccd979bdSMark Fasheh } 1314ccd979bdSMark Fasheh } 1315ccd979bdSMark Fasheh 13167d1fe093SJoel Becker if (best_size) { 13177d1fe093SJoel Becker res->sr_bit_offset = best_offset; 13187d1fe093SJoel Becker res->sr_bits = best_size; 1319ccd979bdSMark Fasheh } else { 1320ccd979bdSMark Fasheh status = -ENOSPC; 1321ccd979bdSMark Fasheh /* No error log here -- see the comment above 1322ccd979bdSMark Fasheh * ocfs2_test_bg_bit_allocatable */ 1323ccd979bdSMark Fasheh } 1324ccd979bdSMark Fasheh 1325ccd979bdSMark Fasheh return status; 1326ccd979bdSMark Fasheh } 1327ccd979bdSMark Fasheh 13281fabe148SMark Fasheh static inline int ocfs2_block_group_set_bits(handle_t *handle, 1329ccd979bdSMark Fasheh struct inode *alloc_inode, 1330ccd979bdSMark Fasheh struct ocfs2_group_desc *bg, 1331ccd979bdSMark Fasheh struct buffer_head *group_bh, 1332ccd979bdSMark Fasheh unsigned int bit_off, 1333ccd979bdSMark Fasheh unsigned int num_bits) 1334ccd979bdSMark Fasheh { 1335ccd979bdSMark Fasheh int status; 1336ccd979bdSMark Fasheh void *bitmap = bg->bg_bitmap; 1337ccd979bdSMark Fasheh int journal_type = OCFS2_JOURNAL_ACCESS_WRITE; 1338ccd979bdSMark Fasheh 1339ccd979bdSMark Fasheh mlog_entry_void(); 1340ccd979bdSMark Fasheh 134142035306SJoel Becker /* All callers get the descriptor via 134242035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 134342035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 1344ccd979bdSMark Fasheh BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits); 1345ccd979bdSMark Fasheh 1346ccd979bdSMark Fasheh mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off, 1347ccd979bdSMark Fasheh num_bits); 1348ccd979bdSMark Fasheh 1349ccd979bdSMark Fasheh if (ocfs2_is_cluster_bitmap(alloc_inode)) 1350ccd979bdSMark Fasheh journal_type = OCFS2_JOURNAL_ACCESS_UNDO; 1351ccd979bdSMark Fasheh 135213723d00SJoel Becker status = ocfs2_journal_access_gd(handle, 13530cf2f763SJoel Becker INODE_CACHE(alloc_inode), 1354ccd979bdSMark Fasheh group_bh, 1355ccd979bdSMark Fasheh journal_type); 1356ccd979bdSMark Fasheh if (status < 0) { 1357ccd979bdSMark Fasheh mlog_errno(status); 1358ccd979bdSMark Fasheh goto bail; 1359ccd979bdSMark Fasheh } 1360ccd979bdSMark Fasheh 1361ccd979bdSMark Fasheh le16_add_cpu(&bg->bg_free_bits_count, -num_bits); 1362ccd979bdSMark Fasheh while(num_bits--) 1363ccd979bdSMark Fasheh ocfs2_set_bit(bit_off++, bitmap); 1364ccd979bdSMark Fasheh 1365ec20cec7SJoel Becker ocfs2_journal_dirty(handle, group_bh); 1366ccd979bdSMark Fasheh 1367ccd979bdSMark Fasheh bail: 1368ccd979bdSMark Fasheh mlog_exit(status); 1369ccd979bdSMark Fasheh return status; 1370ccd979bdSMark Fasheh } 1371ccd979bdSMark Fasheh 1372ccd979bdSMark Fasheh /* find the one with the most empty bits */ 1373ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl) 1374ccd979bdSMark Fasheh { 1375ccd979bdSMark Fasheh u16 curr, best; 1376ccd979bdSMark Fasheh 1377ccd979bdSMark Fasheh BUG_ON(!cl->cl_next_free_rec); 1378ccd979bdSMark Fasheh 1379ccd979bdSMark Fasheh best = curr = 0; 1380ccd979bdSMark Fasheh while (curr < le16_to_cpu(cl->cl_next_free_rec)) { 1381ccd979bdSMark Fasheh if (le32_to_cpu(cl->cl_recs[curr].c_free) > 1382ccd979bdSMark Fasheh le32_to_cpu(cl->cl_recs[best].c_free)) 1383ccd979bdSMark Fasheh best = curr; 1384ccd979bdSMark Fasheh curr++; 1385ccd979bdSMark Fasheh } 1386ccd979bdSMark Fasheh 1387ccd979bdSMark Fasheh BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec)); 1388ccd979bdSMark Fasheh return best; 1389ccd979bdSMark Fasheh } 1390ccd979bdSMark Fasheh 13911fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle, 1392ccd979bdSMark Fasheh struct inode *alloc_inode, 1393ccd979bdSMark Fasheh struct buffer_head *fe_bh, 1394ccd979bdSMark Fasheh struct buffer_head *bg_bh, 1395ccd979bdSMark Fasheh struct buffer_head *prev_bg_bh, 1396ccd979bdSMark Fasheh u16 chain) 1397ccd979bdSMark Fasheh { 1398ccd979bdSMark Fasheh int status; 1399ccd979bdSMark Fasheh /* there is a really tiny chance the journal calls could fail, 1400ccd979bdSMark Fasheh * but we wouldn't want inconsistent blocks in *any* case. */ 1401ccd979bdSMark Fasheh u64 fe_ptr, bg_ptr, prev_bg_ptr; 1402ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data; 1403ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 1404ccd979bdSMark Fasheh struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data; 1405ccd979bdSMark Fasheh 140642035306SJoel Becker /* The caller got these descriptors from 140742035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 140842035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 140942035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg)); 1410ccd979bdSMark Fasheh 1411b0697053SMark Fasheh mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n", 14121ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno), chain, 14131ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(bg->bg_blkno), 14141ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(prev_bg->bg_blkno)); 1415ccd979bdSMark Fasheh 1416ccd979bdSMark Fasheh fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno); 1417ccd979bdSMark Fasheh bg_ptr = le64_to_cpu(bg->bg_next_group); 1418ccd979bdSMark Fasheh prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group); 1419ccd979bdSMark Fasheh 14200cf2f763SJoel Becker status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), 14210cf2f763SJoel Becker prev_bg_bh, 1422ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 1423ccd979bdSMark Fasheh if (status < 0) { 1424ccd979bdSMark Fasheh mlog_errno(status); 1425ccd979bdSMark Fasheh goto out_rollback; 1426ccd979bdSMark Fasheh } 1427ccd979bdSMark Fasheh 1428ccd979bdSMark Fasheh prev_bg->bg_next_group = bg->bg_next_group; 1429ec20cec7SJoel Becker ocfs2_journal_dirty(handle, prev_bg_bh); 1430ccd979bdSMark Fasheh 14310cf2f763SJoel Becker status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), 14320cf2f763SJoel Becker bg_bh, OCFS2_JOURNAL_ACCESS_WRITE); 1433ccd979bdSMark Fasheh if (status < 0) { 1434ccd979bdSMark Fasheh mlog_errno(status); 1435ccd979bdSMark Fasheh goto out_rollback; 1436ccd979bdSMark Fasheh } 1437ccd979bdSMark Fasheh 1438ccd979bdSMark Fasheh bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno; 1439ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bg_bh); 1440ccd979bdSMark Fasheh 14410cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), 14420cf2f763SJoel Becker fe_bh, OCFS2_JOURNAL_ACCESS_WRITE); 1443ccd979bdSMark Fasheh if (status < 0) { 1444ccd979bdSMark Fasheh mlog_errno(status); 1445ccd979bdSMark Fasheh goto out_rollback; 1446ccd979bdSMark Fasheh } 1447ccd979bdSMark Fasheh 1448ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno; 1449ec20cec7SJoel Becker ocfs2_journal_dirty(handle, fe_bh); 1450ccd979bdSMark Fasheh 1451ccd979bdSMark Fasheh out_rollback: 1452ccd979bdSMark Fasheh if (status < 0) { 1453ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr); 1454ccd979bdSMark Fasheh bg->bg_next_group = cpu_to_le64(bg_ptr); 1455ccd979bdSMark Fasheh prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr); 1456ccd979bdSMark Fasheh } 145742035306SJoel Becker 1458ccd979bdSMark Fasheh mlog_exit(status); 1459ccd979bdSMark Fasheh return status; 1460ccd979bdSMark Fasheh } 1461ccd979bdSMark Fasheh 1462ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg, 1463ccd979bdSMark Fasheh u32 wanted) 1464ccd979bdSMark Fasheh { 1465ccd979bdSMark Fasheh return le16_to_cpu(bg->bg_free_bits_count) > wanted; 1466ccd979bdSMark Fasheh } 1467ccd979bdSMark Fasheh 1468ccd979bdSMark Fasheh /* return 0 on success, -ENOSPC to keep searching and any other < 0 1469ccd979bdSMark Fasheh * value on error. */ 1470ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode, 1471ccd979bdSMark Fasheh struct buffer_head *group_bh, 1472ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 14731187c968SJoel Becker u64 max_block, 14747d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1475ccd979bdSMark Fasheh { 1476ccd979bdSMark Fasheh int search = -ENOSPC; 1477ccd979bdSMark Fasheh int ret; 14781187c968SJoel Becker u64 blkoff; 14797bf72edeSMark Fasheh struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data; 14809c7af40bSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 14817bf72edeSMark Fasheh unsigned int max_bits, gd_cluster_off; 1482ccd979bdSMark Fasheh 1483ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 1484ccd979bdSMark Fasheh 14857bf72edeSMark Fasheh if (gd->bg_free_bits_count) { 14867bf72edeSMark Fasheh max_bits = le16_to_cpu(gd->bg_bits); 14877bf72edeSMark Fasheh 14887bf72edeSMark Fasheh /* Tail groups in cluster bitmaps which aren't cpg 14897bf72edeSMark Fasheh * aligned are prone to partial extention by a failed 14907bf72edeSMark Fasheh * fs resize. If the file system resize never got to 14917bf72edeSMark Fasheh * update the dinode cluster count, then we don't want 14927bf72edeSMark Fasheh * to trust any clusters past it, regardless of what 14937bf72edeSMark Fasheh * the group descriptor says. */ 14947bf72edeSMark Fasheh gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb, 14957bf72edeSMark Fasheh le64_to_cpu(gd->bg_blkno)); 14967bf72edeSMark Fasheh if ((gd_cluster_off + max_bits) > 14977bf72edeSMark Fasheh OCFS2_I(inode)->ip_clusters) { 14987bf72edeSMark Fasheh max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off; 14997bf72edeSMark Fasheh mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n", 15007bf72edeSMark Fasheh (unsigned long long)le64_to_cpu(gd->bg_blkno), 15017bf72edeSMark Fasheh le16_to_cpu(gd->bg_bits), 15027bf72edeSMark Fasheh OCFS2_I(inode)->ip_clusters, max_bits); 15037bf72edeSMark Fasheh } 15047bf72edeSMark Fasheh 1505ccd979bdSMark Fasheh ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb), 1506ccd979bdSMark Fasheh group_bh, bits_wanted, 15077d1fe093SJoel Becker max_bits, res); 1508ccd979bdSMark Fasheh if (ret) 1509ccd979bdSMark Fasheh return ret; 1510ccd979bdSMark Fasheh 15111187c968SJoel Becker if (max_block) { 15121187c968SJoel Becker blkoff = ocfs2_clusters_to_blocks(inode->i_sb, 15131187c968SJoel Becker gd_cluster_off + 15147d1fe093SJoel Becker res->sr_bit_offset + 15157d1fe093SJoel Becker res->sr_bits); 15161187c968SJoel Becker mlog(0, "Checking %llu against %llu\n", 15171187c968SJoel Becker (unsigned long long)blkoff, 15181187c968SJoel Becker (unsigned long long)max_block); 15191187c968SJoel Becker if (blkoff > max_block) 15201187c968SJoel Becker return -ENOSPC; 15211187c968SJoel Becker } 15221187c968SJoel Becker 1523ccd979bdSMark Fasheh /* ocfs2_block_group_find_clear_bits() might 1524ccd979bdSMark Fasheh * return success, but we still want to return 1525ccd979bdSMark Fasheh * -ENOSPC unless it found the minimum number 1526ccd979bdSMark Fasheh * of bits. */ 15277d1fe093SJoel Becker if (min_bits <= res->sr_bits) 1528ccd979bdSMark Fasheh search = 0; /* success */ 15297d1fe093SJoel Becker else if (res->sr_bits) { 15309c7af40bSMark Fasheh /* 15319c7af40bSMark Fasheh * Don't show bits which we'll be returning 15329c7af40bSMark Fasheh * for allocation to the local alloc bitmap. 15339c7af40bSMark Fasheh */ 15347d1fe093SJoel Becker ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits); 1535ccd979bdSMark Fasheh } 1536ccd979bdSMark Fasheh } 1537ccd979bdSMark Fasheh 1538ccd979bdSMark Fasheh return search; 1539ccd979bdSMark Fasheh } 1540ccd979bdSMark Fasheh 1541ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode, 1542ccd979bdSMark Fasheh struct buffer_head *group_bh, 1543ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 15441187c968SJoel Becker u64 max_block, 15457d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1546ccd979bdSMark Fasheh { 1547ccd979bdSMark Fasheh int ret = -ENOSPC; 15481187c968SJoel Becker u64 blkoff; 1549ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data; 1550ccd979bdSMark Fasheh 1551ccd979bdSMark Fasheh BUG_ON(min_bits != 1); 1552ccd979bdSMark Fasheh BUG_ON(ocfs2_is_cluster_bitmap(inode)); 1553ccd979bdSMark Fasheh 15541187c968SJoel Becker if (bg->bg_free_bits_count) { 1555ccd979bdSMark Fasheh ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb), 1556ccd979bdSMark Fasheh group_bh, bits_wanted, 15577bf72edeSMark Fasheh le16_to_cpu(bg->bg_bits), 15587d1fe093SJoel Becker res); 15591187c968SJoel Becker if (!ret && max_block) { 15607d1fe093SJoel Becker blkoff = le64_to_cpu(bg->bg_blkno) + 15617d1fe093SJoel Becker res->sr_bit_offset + res->sr_bits; 15621187c968SJoel Becker mlog(0, "Checking %llu against %llu\n", 15631187c968SJoel Becker (unsigned long long)blkoff, 15641187c968SJoel Becker (unsigned long long)max_block); 15651187c968SJoel Becker if (blkoff > max_block) 15661187c968SJoel Becker ret = -ENOSPC; 15671187c968SJoel Becker } 15681187c968SJoel Becker } 1569ccd979bdSMark Fasheh 1570ccd979bdSMark Fasheh return ret; 1571ccd979bdSMark Fasheh } 1572ccd979bdSMark Fasheh 1573883d4caeSMark Fasheh static int ocfs2_alloc_dinode_update_counts(struct inode *inode, 15741fabe148SMark Fasheh handle_t *handle, 1575883d4caeSMark Fasheh struct buffer_head *di_bh, 1576883d4caeSMark Fasheh u32 num_bits, 1577883d4caeSMark Fasheh u16 chain) 1578883d4caeSMark Fasheh { 1579883d4caeSMark Fasheh int ret; 1580883d4caeSMark Fasheh u32 tmp_used; 1581883d4caeSMark Fasheh struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; 1582883d4caeSMark Fasheh struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain; 1583883d4caeSMark Fasheh 15840cf2f763SJoel Becker ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 1585883d4caeSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 1586883d4caeSMark Fasheh if (ret < 0) { 1587883d4caeSMark Fasheh mlog_errno(ret); 1588883d4caeSMark Fasheh goto out; 1589883d4caeSMark Fasheh } 1590883d4caeSMark Fasheh 1591883d4caeSMark Fasheh tmp_used = le32_to_cpu(di->id1.bitmap1.i_used); 1592883d4caeSMark Fasheh di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used); 1593883d4caeSMark Fasheh le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits); 1594ec20cec7SJoel Becker ocfs2_journal_dirty(handle, di_bh); 1595883d4caeSMark Fasheh 1596883d4caeSMark Fasheh out: 1597883d4caeSMark Fasheh return ret; 1598883d4caeSMark Fasheh } 1599883d4caeSMark Fasheh 1600ba206635SJoel Becker static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res, 160113e434cfSJoel Becker struct ocfs2_extent_rec *rec, 160213e434cfSJoel Becker struct ocfs2_chain_list *cl) 160313e434cfSJoel Becker { 160413e434cfSJoel Becker unsigned int bpc = le16_to_cpu(cl->cl_bpc); 160513e434cfSJoel Becker unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc; 160613e434cfSJoel Becker unsigned int bitcount = le32_to_cpu(rec->e_leaf_clusters) * bpc; 160713e434cfSJoel Becker 160813e434cfSJoel Becker if (res->sr_bit_offset < bitoff) 160913e434cfSJoel Becker return 0; 161013e434cfSJoel Becker if (res->sr_bit_offset >= (bitoff + bitcount)) 161113e434cfSJoel Becker return 0; 1612ba206635SJoel Becker res->sr_blkno = le64_to_cpu(rec->e_blkno) + 1613ba206635SJoel Becker (res->sr_bit_offset - bitoff); 161413e434cfSJoel Becker if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount)) 161513e434cfSJoel Becker res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset; 161613e434cfSJoel Becker return 1; 161713e434cfSJoel Becker } 161813e434cfSJoel Becker 1619ba206635SJoel Becker static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac, 162013e434cfSJoel Becker struct ocfs2_group_desc *bg, 162113e434cfSJoel Becker struct ocfs2_suballoc_result *res) 162213e434cfSJoel Becker { 162313e434cfSJoel Becker int i; 16242b6cb576SJoel Becker u64 bg_blkno = res->sr_bg_blkno; /* Save off */ 162513e434cfSJoel Becker struct ocfs2_extent_rec *rec; 162613e434cfSJoel Becker struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data; 162713e434cfSJoel Becker struct ocfs2_chain_list *cl = &di->id2.i_chain; 162813e434cfSJoel Becker 1629ba206635SJoel Becker if (ocfs2_is_cluster_bitmap(ac->ac_inode)) { 1630ba206635SJoel Becker res->sr_blkno = 0; 163113e434cfSJoel Becker return; 1632ba206635SJoel Becker } 163313e434cfSJoel Becker 1634ba206635SJoel Becker res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset; 16352b6cb576SJoel Becker res->sr_bg_blkno = 0; /* Clear it for contig block groups */ 16364711954eSTao Ma if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) || 1637ba206635SJoel Becker !bg->bg_list.l_next_free_rec) 163813e434cfSJoel Becker return; 163913e434cfSJoel Becker 164013e434cfSJoel Becker for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) { 164113e434cfSJoel Becker rec = &bg->bg_list.l_recs[i]; 16422b6cb576SJoel Becker if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) { 16432b6cb576SJoel Becker res->sr_bg_blkno = bg_blkno; /* Restore */ 164413e434cfSJoel Becker break; 164513e434cfSJoel Becker } 164613e434cfSJoel Becker } 16472b6cb576SJoel Becker } 164813e434cfSJoel Becker 1649883d4caeSMark Fasheh static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac, 16501fabe148SMark Fasheh handle_t *handle, 1651883d4caeSMark Fasheh u32 bits_wanted, 1652883d4caeSMark Fasheh u32 min_bits, 16537d1fe093SJoel Becker struct ocfs2_suballoc_result *res, 1654883d4caeSMark Fasheh u16 *bits_left) 1655883d4caeSMark Fasheh { 1656883d4caeSMark Fasheh int ret; 1657883d4caeSMark Fasheh struct buffer_head *group_bh = NULL; 1658883d4caeSMark Fasheh struct ocfs2_group_desc *gd; 165968f64d47SJoel Becker struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data; 1660883d4caeSMark Fasheh struct inode *alloc_inode = ac->ac_inode; 1661883d4caeSMark Fasheh 16627d1fe093SJoel Becker ret = ocfs2_read_group_descriptor(alloc_inode, di, 16637d1fe093SJoel Becker res->sr_bg_blkno, &group_bh); 1664883d4caeSMark Fasheh if (ret < 0) { 1665883d4caeSMark Fasheh mlog_errno(ret); 1666883d4caeSMark Fasheh return ret; 1667883d4caeSMark Fasheh } 1668883d4caeSMark Fasheh 1669883d4caeSMark Fasheh gd = (struct ocfs2_group_desc *) group_bh->b_data; 1670883d4caeSMark Fasheh ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits, 16717d1fe093SJoel Becker ac->ac_max_block, res); 1672883d4caeSMark Fasheh if (ret < 0) { 1673883d4caeSMark Fasheh if (ret != -ENOSPC) 1674883d4caeSMark Fasheh mlog_errno(ret); 1675883d4caeSMark Fasheh goto out; 1676883d4caeSMark Fasheh } 1677883d4caeSMark Fasheh 167813e434cfSJoel Becker if (!ret) 1679ba206635SJoel Becker ocfs2_bg_discontig_fix_result(ac, gd, res); 168013e434cfSJoel Becker 1681883d4caeSMark Fasheh ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh, 16827d1fe093SJoel Becker res->sr_bits, 1683883d4caeSMark Fasheh le16_to_cpu(gd->bg_chain)); 1684883d4caeSMark Fasheh if (ret < 0) { 1685883d4caeSMark Fasheh mlog_errno(ret); 1686883d4caeSMark Fasheh goto out; 1687883d4caeSMark Fasheh } 1688883d4caeSMark Fasheh 1689883d4caeSMark Fasheh ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh, 16907d1fe093SJoel Becker res->sr_bit_offset, res->sr_bits); 1691883d4caeSMark Fasheh if (ret < 0) 1692883d4caeSMark Fasheh mlog_errno(ret); 1693883d4caeSMark Fasheh 1694883d4caeSMark Fasheh *bits_left = le16_to_cpu(gd->bg_free_bits_count); 1695883d4caeSMark Fasheh 1696883d4caeSMark Fasheh out: 1697883d4caeSMark Fasheh brelse(group_bh); 1698883d4caeSMark Fasheh 1699883d4caeSMark Fasheh return ret; 1700883d4caeSMark Fasheh } 1701883d4caeSMark Fasheh 1702ccd979bdSMark Fasheh static int ocfs2_search_chain(struct ocfs2_alloc_context *ac, 17031fabe148SMark Fasheh handle_t *handle, 1704ccd979bdSMark Fasheh u32 bits_wanted, 1705ccd979bdSMark Fasheh u32 min_bits, 17067d1fe093SJoel Becker struct ocfs2_suballoc_result *res, 1707883d4caeSMark Fasheh u16 *bits_left) 1708ccd979bdSMark Fasheh { 1709ccd979bdSMark Fasheh int status; 17107d1fe093SJoel Becker u16 chain; 1711ccd979bdSMark Fasheh u32 tmp_used; 1712ccd979bdSMark Fasheh u64 next_group; 1713ccd979bdSMark Fasheh struct inode *alloc_inode = ac->ac_inode; 1714ccd979bdSMark Fasheh struct buffer_head *group_bh = NULL; 1715ccd979bdSMark Fasheh struct buffer_head *prev_group_bh = NULL; 1716ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data; 1717ccd979bdSMark Fasheh struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain; 1718ccd979bdSMark Fasheh struct ocfs2_group_desc *bg; 1719ccd979bdSMark Fasheh 1720ccd979bdSMark Fasheh chain = ac->ac_chain; 1721b0697053SMark Fasheh mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n", 1722b0697053SMark Fasheh bits_wanted, chain, 1723b0697053SMark Fasheh (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno); 1724ccd979bdSMark Fasheh 172568f64d47SJoel Becker status = ocfs2_read_group_descriptor(alloc_inode, fe, 1726ccd979bdSMark Fasheh le64_to_cpu(cl->cl_recs[chain].c_blkno), 17270fcaa56aSJoel Becker &group_bh); 1728ccd979bdSMark Fasheh if (status < 0) { 1729ccd979bdSMark Fasheh mlog_errno(status); 1730ccd979bdSMark Fasheh goto bail; 1731ccd979bdSMark Fasheh } 1732ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) group_bh->b_data; 1733ccd979bdSMark Fasheh 1734ccd979bdSMark Fasheh status = -ENOSPC; 1735ccd979bdSMark Fasheh /* for now, the chain search is a bit simplistic. We just use 1736ccd979bdSMark Fasheh * the 1st group with any empty bits. */ 17371187c968SJoel Becker while ((status = ac->ac_group_search(alloc_inode, group_bh, 17381187c968SJoel Becker bits_wanted, min_bits, 17397d1fe093SJoel Becker ac->ac_max_block, 17407d1fe093SJoel Becker res)) == -ENOSPC) { 1741ccd979bdSMark Fasheh if (!bg->bg_next_group) 1742ccd979bdSMark Fasheh break; 1743a81cb88bSMark Fasheh 1744ccd979bdSMark Fasheh brelse(prev_group_bh); 1745ccd979bdSMark Fasheh prev_group_bh = NULL; 1746a81cb88bSMark Fasheh 1747ccd979bdSMark Fasheh next_group = le64_to_cpu(bg->bg_next_group); 1748ccd979bdSMark Fasheh prev_group_bh = group_bh; 1749ccd979bdSMark Fasheh group_bh = NULL; 175068f64d47SJoel Becker status = ocfs2_read_group_descriptor(alloc_inode, fe, 17510fcaa56aSJoel Becker next_group, &group_bh); 1752ccd979bdSMark Fasheh if (status < 0) { 1753ccd979bdSMark Fasheh mlog_errno(status); 1754ccd979bdSMark Fasheh goto bail; 1755ccd979bdSMark Fasheh } 1756ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) group_bh->b_data; 1757ccd979bdSMark Fasheh } 1758ccd979bdSMark Fasheh if (status < 0) { 1759ccd979bdSMark Fasheh if (status != -ENOSPC) 1760ccd979bdSMark Fasheh mlog_errno(status); 1761ccd979bdSMark Fasheh goto bail; 1762ccd979bdSMark Fasheh } 1763ccd979bdSMark Fasheh 1764b0697053SMark Fasheh mlog(0, "alloc succeeds: we give %u bits from block group %llu\n", 17657d1fe093SJoel Becker res->sr_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno)); 1766ccd979bdSMark Fasheh 17677d1fe093SJoel Becker res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno); 1768ccd979bdSMark Fasheh 17697d1fe093SJoel Becker BUG_ON(res->sr_bits == 0); 177013e434cfSJoel Becker if (!status) 1771ba206635SJoel Becker ocfs2_bg_discontig_fix_result(ac, bg, res); 177213e434cfSJoel Becker 1773ccd979bdSMark Fasheh 1774ccd979bdSMark Fasheh /* 1775ccd979bdSMark Fasheh * Keep track of previous block descriptor read. When 1776ccd979bdSMark Fasheh * we find a target, if we have read more than X 1777ccd979bdSMark Fasheh * number of descriptors, and the target is reasonably 1778ccd979bdSMark Fasheh * empty, relink him to top of his chain. 1779ccd979bdSMark Fasheh * 1780ccd979bdSMark Fasheh * We've read 0 extra blocks and only send one more to 1781ccd979bdSMark Fasheh * the transaction, yet the next guy to search has a 1782ccd979bdSMark Fasheh * much easier time. 1783ccd979bdSMark Fasheh * 1784ccd979bdSMark Fasheh * Do this *after* figuring out how many bits we're taking out 1785ccd979bdSMark Fasheh * of our target group. 1786ccd979bdSMark Fasheh */ 1787ccd979bdSMark Fasheh if (ac->ac_allow_chain_relink && 1788ccd979bdSMark Fasheh (prev_group_bh) && 17897d1fe093SJoel Becker (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) { 1790ccd979bdSMark Fasheh status = ocfs2_relink_block_group(handle, alloc_inode, 1791ccd979bdSMark Fasheh ac->ac_bh, group_bh, 1792ccd979bdSMark Fasheh prev_group_bh, chain); 1793ccd979bdSMark Fasheh if (status < 0) { 1794ccd979bdSMark Fasheh mlog_errno(status); 1795ccd979bdSMark Fasheh goto bail; 1796ccd979bdSMark Fasheh } 1797ccd979bdSMark Fasheh } 1798ccd979bdSMark Fasheh 1799ccd979bdSMark Fasheh /* Ok, claim our bits now: set the info on dinode, chainlist 1800ccd979bdSMark Fasheh * and then the group */ 180113723d00SJoel Becker status = ocfs2_journal_access_di(handle, 18020cf2f763SJoel Becker INODE_CACHE(alloc_inode), 1803ccd979bdSMark Fasheh ac->ac_bh, 1804ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 1805ccd979bdSMark Fasheh if (status < 0) { 1806ccd979bdSMark Fasheh mlog_errno(status); 1807ccd979bdSMark Fasheh goto bail; 1808ccd979bdSMark Fasheh } 1809ccd979bdSMark Fasheh 1810ccd979bdSMark Fasheh tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used); 18117d1fe093SJoel Becker fe->id1.bitmap1.i_used = cpu_to_le32(res->sr_bits + tmp_used); 18127d1fe093SJoel Becker le32_add_cpu(&cl->cl_recs[chain].c_free, -res->sr_bits); 1813ec20cec7SJoel Becker ocfs2_journal_dirty(handle, ac->ac_bh); 1814ccd979bdSMark Fasheh 1815ccd979bdSMark Fasheh status = ocfs2_block_group_set_bits(handle, 1816ccd979bdSMark Fasheh alloc_inode, 1817ccd979bdSMark Fasheh bg, 1818ccd979bdSMark Fasheh group_bh, 18197d1fe093SJoel Becker res->sr_bit_offset, 18207d1fe093SJoel Becker res->sr_bits); 1821ccd979bdSMark Fasheh if (status < 0) { 1822ccd979bdSMark Fasheh mlog_errno(status); 1823ccd979bdSMark Fasheh goto bail; 1824ccd979bdSMark Fasheh } 1825ccd979bdSMark Fasheh 18267d1fe093SJoel Becker mlog(0, "Allocated %u bits from suballocator %llu\n", res->sr_bits, 18271ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno)); 1828ccd979bdSMark Fasheh 1829883d4caeSMark Fasheh *bits_left = le16_to_cpu(bg->bg_free_bits_count); 1830ccd979bdSMark Fasheh bail: 1831ccd979bdSMark Fasheh brelse(group_bh); 1832ccd979bdSMark Fasheh brelse(prev_group_bh); 1833ccd979bdSMark Fasheh 1834ccd979bdSMark Fasheh mlog_exit(status); 1835ccd979bdSMark Fasheh return status; 1836ccd979bdSMark Fasheh } 1837ccd979bdSMark Fasheh 1838ccd979bdSMark Fasheh /* will give out up to bits_wanted contiguous bits. */ 1839aa8f8e93SJoel Becker static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac, 18401fabe148SMark Fasheh handle_t *handle, 1841ccd979bdSMark Fasheh u32 bits_wanted, 1842ccd979bdSMark Fasheh u32 min_bits, 18437d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1844ccd979bdSMark Fasheh { 1845ccd979bdSMark Fasheh int status; 1846ccd979bdSMark Fasheh u16 victim, i; 1847883d4caeSMark Fasheh u16 bits_left = 0; 1848ccd979bdSMark Fasheh struct ocfs2_chain_list *cl; 1849ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 1850ccd979bdSMark Fasheh 1851ccd979bdSMark Fasheh mlog_entry_void(); 1852ccd979bdSMark Fasheh 1853ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted); 1854ccd979bdSMark Fasheh BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given)); 1855ccd979bdSMark Fasheh BUG_ON(!ac->ac_bh); 1856ccd979bdSMark Fasheh 1857ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) ac->ac_bh->b_data; 185810995aa2SJoel Becker 185910995aa2SJoel Becker /* The bh was validated by the inode read during 186010995aa2SJoel Becker * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */ 186110995aa2SJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); 186210995aa2SJoel Becker 1863ccd979bdSMark Fasheh if (le32_to_cpu(fe->id1.bitmap1.i_used) >= 1864ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_total)) { 1865aa8f8e93SJoel Becker ocfs2_error(ac->ac_inode->i_sb, 1866aa8f8e93SJoel Becker "Chain allocator dinode %llu has %u used " 1867b0697053SMark Fasheh "bits but only %u total.", 1868b0697053SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno), 1869ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_used), 1870ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_total)); 1871ccd979bdSMark Fasheh status = -EIO; 1872ccd979bdSMark Fasheh goto bail; 1873ccd979bdSMark Fasheh } 1874ccd979bdSMark Fasheh 18757d1fe093SJoel Becker res->sr_bg_blkno = ac->ac_last_group; 18767d1fe093SJoel Becker if (res->sr_bg_blkno) { 1877883d4caeSMark Fasheh /* Attempt to short-circuit the usual search mechanism 1878883d4caeSMark Fasheh * by jumping straight to the most recently used 1879883d4caeSMark Fasheh * allocation group. This helps us mantain some 1880883d4caeSMark Fasheh * contiguousness across allocations. */ 1881da5cbf2fSMark Fasheh status = ocfs2_search_one_group(ac, handle, bits_wanted, 18827d1fe093SJoel Becker min_bits, res, &bits_left); 18837d1fe093SJoel Becker if (!status) 1884883d4caeSMark Fasheh goto set_hint; 1885883d4caeSMark Fasheh if (status < 0 && status != -ENOSPC) { 1886883d4caeSMark Fasheh mlog_errno(status); 1887883d4caeSMark Fasheh goto bail; 1888883d4caeSMark Fasheh } 1889883d4caeSMark Fasheh } 1890883d4caeSMark Fasheh 1891ccd979bdSMark Fasheh cl = (struct ocfs2_chain_list *) &fe->id2.i_chain; 1892ccd979bdSMark Fasheh 1893ccd979bdSMark Fasheh victim = ocfs2_find_victim_chain(cl); 1894ccd979bdSMark Fasheh ac->ac_chain = victim; 1895ccd979bdSMark Fasheh ac->ac_allow_chain_relink = 1; 1896ccd979bdSMark Fasheh 18977d1fe093SJoel Becker status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, 18987d1fe093SJoel Becker res, &bits_left); 1899ccd979bdSMark Fasheh if (!status) 1900883d4caeSMark Fasheh goto set_hint; 1901ccd979bdSMark Fasheh if (status < 0 && status != -ENOSPC) { 1902ccd979bdSMark Fasheh mlog_errno(status); 1903ccd979bdSMark Fasheh goto bail; 1904ccd979bdSMark Fasheh } 1905ccd979bdSMark Fasheh 1906ccd979bdSMark Fasheh mlog(0, "Search of victim chain %u came up with nothing, " 1907ccd979bdSMark Fasheh "trying all chains now.\n", victim); 1908ccd979bdSMark Fasheh 1909ccd979bdSMark Fasheh /* If we didn't pick a good victim, then just default to 1910ccd979bdSMark Fasheh * searching each chain in order. Don't allow chain relinking 1911ccd979bdSMark Fasheh * because we only calculate enough journal credits for one 1912ccd979bdSMark Fasheh * relink per alloc. */ 1913ccd979bdSMark Fasheh ac->ac_allow_chain_relink = 0; 1914ccd979bdSMark Fasheh for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) { 1915ccd979bdSMark Fasheh if (i == victim) 1916ccd979bdSMark Fasheh continue; 1917ccd979bdSMark Fasheh if (!cl->cl_recs[i].c_free) 1918ccd979bdSMark Fasheh continue; 1919ccd979bdSMark Fasheh 1920ccd979bdSMark Fasheh ac->ac_chain = i; 1921da5cbf2fSMark Fasheh status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, 19227d1fe093SJoel Becker res, &bits_left); 1923ccd979bdSMark Fasheh if (!status) 1924ccd979bdSMark Fasheh break; 1925ccd979bdSMark Fasheh if (status < 0 && status != -ENOSPC) { 1926ccd979bdSMark Fasheh mlog_errno(status); 1927ccd979bdSMark Fasheh goto bail; 1928ccd979bdSMark Fasheh } 1929ccd979bdSMark Fasheh } 1930ccd979bdSMark Fasheh 1931883d4caeSMark Fasheh set_hint: 1932883d4caeSMark Fasheh if (status != -ENOSPC) { 1933883d4caeSMark Fasheh /* If the next search of this group is not likely to 1934883d4caeSMark Fasheh * yield a suitable extent, then we reset the last 1935883d4caeSMark Fasheh * group hint so as to not waste a disk read */ 1936883d4caeSMark Fasheh if (bits_left < min_bits) 1937883d4caeSMark Fasheh ac->ac_last_group = 0; 1938883d4caeSMark Fasheh else 19397d1fe093SJoel Becker ac->ac_last_group = res->sr_bg_blkno; 1940883d4caeSMark Fasheh } 1941883d4caeSMark Fasheh 1942883d4caeSMark Fasheh bail: 1943ccd979bdSMark Fasheh mlog_exit(status); 1944ccd979bdSMark Fasheh return status; 1945ccd979bdSMark Fasheh } 1946ccd979bdSMark Fasheh 19471ed9b777SJoel Becker int ocfs2_claim_metadata(handle_t *handle, 1948ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac, 1949ccd979bdSMark Fasheh u32 bits_wanted, 19502b6cb576SJoel Becker u64 *suballoc_loc, 1951ccd979bdSMark Fasheh u16 *suballoc_bit_start, 1952ccd979bdSMark Fasheh unsigned int *num_bits, 1953ccd979bdSMark Fasheh u64 *blkno_start) 1954ccd979bdSMark Fasheh { 1955ccd979bdSMark Fasheh int status; 1956ba206635SJoel Becker struct ocfs2_suballoc_result res = { .sr_blkno = 0, }; 1957ccd979bdSMark Fasheh 1958ccd979bdSMark Fasheh BUG_ON(!ac); 1959ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted)); 1960ccd979bdSMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_META); 1961ccd979bdSMark Fasheh 1962aa8f8e93SJoel Becker status = ocfs2_claim_suballoc_bits(ac, 1963da5cbf2fSMark Fasheh handle, 1964ccd979bdSMark Fasheh bits_wanted, 1965ccd979bdSMark Fasheh 1, 19667d1fe093SJoel Becker &res); 1967ccd979bdSMark Fasheh if (status < 0) { 1968ccd979bdSMark Fasheh mlog_errno(status); 1969ccd979bdSMark Fasheh goto bail; 1970ccd979bdSMark Fasheh } 19711ed9b777SJoel Becker atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs); 1972ccd979bdSMark Fasheh 19732b6cb576SJoel Becker *suballoc_loc = res.sr_bg_blkno; 19747d1fe093SJoel Becker *suballoc_bit_start = res.sr_bit_offset; 1975ba206635SJoel Becker *blkno_start = res.sr_blkno; 19767d1fe093SJoel Becker ac->ac_bits_given += res.sr_bits; 19777d1fe093SJoel Becker *num_bits = res.sr_bits; 1978ccd979bdSMark Fasheh status = 0; 1979ccd979bdSMark Fasheh bail: 1980ccd979bdSMark Fasheh mlog_exit(status); 1981ccd979bdSMark Fasheh return status; 1982ccd979bdSMark Fasheh } 1983ccd979bdSMark Fasheh 198413821151STao Ma static void ocfs2_init_inode_ac_group(struct inode *dir, 1985abf1b3cbSTao Ma struct buffer_head *parent_di_bh, 198613821151STao Ma struct ocfs2_alloc_context *ac) 198713821151STao Ma { 1988abf1b3cbSTao Ma struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data; 198913821151STao Ma /* 199013821151STao Ma * Try to allocate inodes from some specific group. 199113821151STao Ma * 199213821151STao Ma * If the parent dir has recorded the last group used in allocation, 199313821151STao Ma * cool, use it. Otherwise if we try to allocate new inode from the 199413821151STao Ma * same slot the parent dir belongs to, use the same chunk. 199513821151STao Ma * 199613821151STao Ma * We are very careful here to avoid the mistake of setting 199713821151STao Ma * ac_last_group to a group descriptor from a different (unlocked) slot. 199813821151STao Ma */ 199913821151STao Ma if (OCFS2_I(dir)->ip_last_used_group && 200013821151STao Ma OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot) 200113821151STao Ma ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group; 2002abf1b3cbSTao Ma else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) { 2003abf1b3cbSTao Ma if (di->i_suballoc_loc) 2004abf1b3cbSTao Ma ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc); 2005abf1b3cbSTao Ma else 200613821151STao Ma ac->ac_last_group = ocfs2_which_suballoc_group( 2007abf1b3cbSTao Ma le64_to_cpu(di->i_blkno), 2008abf1b3cbSTao Ma le16_to_cpu(di->i_suballoc_bit)); 2009abf1b3cbSTao Ma } 201013821151STao Ma } 201113821151STao Ma 201213821151STao Ma static inline void ocfs2_save_inode_ac_group(struct inode *dir, 201313821151STao Ma struct ocfs2_alloc_context *ac) 201413821151STao Ma { 201513821151STao Ma OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group; 201613821151STao Ma OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot; 201713821151STao Ma } 201813821151STao Ma 20191ed9b777SJoel Becker int ocfs2_claim_new_inode(handle_t *handle, 202013821151STao Ma struct inode *dir, 202113821151STao Ma struct buffer_head *parent_fe_bh, 2022ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac, 20232b6cb576SJoel Becker u64 *suballoc_loc, 2024ccd979bdSMark Fasheh u16 *suballoc_bit, 2025ccd979bdSMark Fasheh u64 *fe_blkno) 2026ccd979bdSMark Fasheh { 2027ccd979bdSMark Fasheh int status; 20282b6cb576SJoel Becker struct ocfs2_suballoc_result res; 2029ccd979bdSMark Fasheh 2030ccd979bdSMark Fasheh mlog_entry_void(); 2031ccd979bdSMark Fasheh 2032ccd979bdSMark Fasheh BUG_ON(!ac); 2033ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_given != 0); 2034ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_wanted != 1); 2035ccd979bdSMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE); 2036ccd979bdSMark Fasheh 203713821151STao Ma ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac); 203813821151STao Ma 2039aa8f8e93SJoel Becker status = ocfs2_claim_suballoc_bits(ac, 2040da5cbf2fSMark Fasheh handle, 2041ccd979bdSMark Fasheh 1, 2042ccd979bdSMark Fasheh 1, 20437d1fe093SJoel Becker &res); 2044ccd979bdSMark Fasheh if (status < 0) { 2045ccd979bdSMark Fasheh mlog_errno(status); 2046ccd979bdSMark Fasheh goto bail; 2047ccd979bdSMark Fasheh } 20481ed9b777SJoel Becker atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs); 2049ccd979bdSMark Fasheh 20507d1fe093SJoel Becker BUG_ON(res.sr_bits != 1); 2051ccd979bdSMark Fasheh 20522b6cb576SJoel Becker *suballoc_loc = res.sr_bg_blkno; 20537d1fe093SJoel Becker *suballoc_bit = res.sr_bit_offset; 2054ba206635SJoel Becker *fe_blkno = res.sr_blkno; 2055ccd979bdSMark Fasheh ac->ac_bits_given++; 205613821151STao Ma ocfs2_save_inode_ac_group(dir, ac); 2057ccd979bdSMark Fasheh status = 0; 2058ccd979bdSMark Fasheh bail: 2059ccd979bdSMark Fasheh mlog_exit(status); 2060ccd979bdSMark Fasheh return status; 2061ccd979bdSMark Fasheh } 2062ccd979bdSMark Fasheh 2063ccd979bdSMark Fasheh /* translate a group desc. blkno and it's bitmap offset into 2064ccd979bdSMark Fasheh * disk cluster offset. */ 2065ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode, 2066ccd979bdSMark Fasheh u64 bg_blkno, 2067ccd979bdSMark Fasheh u16 bg_bit_off) 2068ccd979bdSMark Fasheh { 2069ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2070ccd979bdSMark Fasheh u32 cluster = 0; 2071ccd979bdSMark Fasheh 2072ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 2073ccd979bdSMark Fasheh 2074ccd979bdSMark Fasheh if (bg_blkno != osb->first_cluster_group_blkno) 2075ccd979bdSMark Fasheh cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno); 2076ccd979bdSMark Fasheh cluster += (u32) bg_bit_off; 2077ccd979bdSMark Fasheh return cluster; 2078ccd979bdSMark Fasheh } 2079ccd979bdSMark Fasheh 2080ccd979bdSMark Fasheh /* given a cluster offset, calculate which block group it belongs to 2081ccd979bdSMark Fasheh * and return that block offset. */ 2082d659072fSTao Ma u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster) 2083ccd979bdSMark Fasheh { 2084ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2085ccd979bdSMark Fasheh u32 group_no; 2086ccd979bdSMark Fasheh 2087ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 2088ccd979bdSMark Fasheh 2089ccd979bdSMark Fasheh group_no = cluster / osb->bitmap_cpg; 2090ccd979bdSMark Fasheh if (!group_no) 2091ccd979bdSMark Fasheh return osb->first_cluster_group_blkno; 2092ccd979bdSMark Fasheh return ocfs2_clusters_to_blocks(inode->i_sb, 2093ccd979bdSMark Fasheh group_no * osb->bitmap_cpg); 2094ccd979bdSMark Fasheh } 2095ccd979bdSMark Fasheh 2096ccd979bdSMark Fasheh /* given the block number of a cluster start, calculate which cluster 2097ccd979bdSMark Fasheh * group and descriptor bitmap offset that corresponds to. */ 2098ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode, 2099ccd979bdSMark Fasheh u64 data_blkno, 2100ccd979bdSMark Fasheh u64 *bg_blkno, 2101ccd979bdSMark Fasheh u16 *bg_bit_off) 2102ccd979bdSMark Fasheh { 2103ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2104ccd979bdSMark Fasheh u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno); 2105ccd979bdSMark Fasheh 2106ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 2107ccd979bdSMark Fasheh 2108ccd979bdSMark Fasheh *bg_blkno = ocfs2_which_cluster_group(inode, 2109ccd979bdSMark Fasheh data_cluster); 2110ccd979bdSMark Fasheh 2111ccd979bdSMark Fasheh if (*bg_blkno == osb->first_cluster_group_blkno) 2112ccd979bdSMark Fasheh *bg_bit_off = (u16) data_cluster; 2113ccd979bdSMark Fasheh else 2114ccd979bdSMark Fasheh *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb, 2115ccd979bdSMark Fasheh data_blkno - *bg_blkno); 2116ccd979bdSMark Fasheh } 2117ccd979bdSMark Fasheh 2118ccd979bdSMark Fasheh /* 2119ccd979bdSMark Fasheh * min_bits - minimum contiguous chunk from this total allocation we 2120ccd979bdSMark Fasheh * can handle. set to what we asked for originally for a full 2121ccd979bdSMark Fasheh * contig. allocation, set to '1' to indicate we can deal with extents 2122ccd979bdSMark Fasheh * of any size. 2123ccd979bdSMark Fasheh */ 21241ed9b777SJoel Becker int __ocfs2_claim_clusters(handle_t *handle, 2125ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac, 2126ccd979bdSMark Fasheh u32 min_clusters, 2127415cb800SMark Fasheh u32 max_clusters, 2128ccd979bdSMark Fasheh u32 *cluster_start, 2129ccd979bdSMark Fasheh u32 *num_clusters) 2130ccd979bdSMark Fasheh { 2131ccd979bdSMark Fasheh int status; 2132415cb800SMark Fasheh unsigned int bits_wanted = max_clusters; 2133ba206635SJoel Becker struct ocfs2_suballoc_result res = { .sr_blkno = 0, }; 21341ed9b777SJoel Becker struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb); 2135ccd979bdSMark Fasheh 2136ccd979bdSMark Fasheh mlog_entry_void(); 2137ccd979bdSMark Fasheh 2138ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted); 2139ccd979bdSMark Fasheh 2140ccd979bdSMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL 2141ccd979bdSMark Fasheh && ac->ac_which != OCFS2_AC_USE_MAIN); 2142ccd979bdSMark Fasheh 2143ccd979bdSMark Fasheh if (ac->ac_which == OCFS2_AC_USE_LOCAL) { 214433d5d380SMark Fasheh WARN_ON(min_clusters > 1); 214533d5d380SMark Fasheh 2146ccd979bdSMark Fasheh status = ocfs2_claim_local_alloc_bits(osb, 2147ccd979bdSMark Fasheh handle, 2148ccd979bdSMark Fasheh ac, 2149ccd979bdSMark Fasheh bits_wanted, 2150ccd979bdSMark Fasheh cluster_start, 2151ccd979bdSMark Fasheh num_clusters); 2152ccd979bdSMark Fasheh if (!status) 2153ccd979bdSMark Fasheh atomic_inc(&osb->alloc_stats.local_data); 2154ccd979bdSMark Fasheh } else { 2155ccd979bdSMark Fasheh if (min_clusters > (osb->bitmap_cpg - 1)) { 2156ccd979bdSMark Fasheh /* The only paths asking for contiguousness 2157ccd979bdSMark Fasheh * should know about this already. */ 21582fbe8d1eSSunil Mushran mlog(ML_ERROR, "minimum allocation requested %u exceeds " 21592fbe8d1eSSunil Mushran "group bitmap size %u!\n", min_clusters, 21602fbe8d1eSSunil Mushran osb->bitmap_cpg); 2161ccd979bdSMark Fasheh status = -ENOSPC; 2162ccd979bdSMark Fasheh goto bail; 2163ccd979bdSMark Fasheh } 2164ccd979bdSMark Fasheh /* clamp the current request down to a realistic size. */ 2165ccd979bdSMark Fasheh if (bits_wanted > (osb->bitmap_cpg - 1)) 2166ccd979bdSMark Fasheh bits_wanted = osb->bitmap_cpg - 1; 2167ccd979bdSMark Fasheh 2168aa8f8e93SJoel Becker status = ocfs2_claim_suballoc_bits(ac, 2169da5cbf2fSMark Fasheh handle, 2170ccd979bdSMark Fasheh bits_wanted, 2171ccd979bdSMark Fasheh min_clusters, 21727d1fe093SJoel Becker &res); 2173ccd979bdSMark Fasheh if (!status) { 2174ba206635SJoel Becker BUG_ON(res.sr_blkno); /* cluster alloc can't set */ 2175ccd979bdSMark Fasheh *cluster_start = 2176ccd979bdSMark Fasheh ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode, 21777d1fe093SJoel Becker res.sr_bg_blkno, 21787d1fe093SJoel Becker res.sr_bit_offset); 2179ccd979bdSMark Fasheh atomic_inc(&osb->alloc_stats.bitmap_data); 21804711954eSTao Ma *num_clusters = res.sr_bits; 2181ccd979bdSMark Fasheh } 2182ccd979bdSMark Fasheh } 2183ccd979bdSMark Fasheh if (status < 0) { 2184ccd979bdSMark Fasheh if (status != -ENOSPC) 2185ccd979bdSMark Fasheh mlog_errno(status); 2186ccd979bdSMark Fasheh goto bail; 2187ccd979bdSMark Fasheh } 2188ccd979bdSMark Fasheh 21894711954eSTao Ma ac->ac_bits_given += *num_clusters; 2190ccd979bdSMark Fasheh 2191ccd979bdSMark Fasheh bail: 2192ccd979bdSMark Fasheh mlog_exit(status); 2193ccd979bdSMark Fasheh return status; 2194ccd979bdSMark Fasheh } 2195ccd979bdSMark Fasheh 21961ed9b777SJoel Becker int ocfs2_claim_clusters(handle_t *handle, 2197415cb800SMark Fasheh struct ocfs2_alloc_context *ac, 2198415cb800SMark Fasheh u32 min_clusters, 2199415cb800SMark Fasheh u32 *cluster_start, 2200415cb800SMark Fasheh u32 *num_clusters) 2201415cb800SMark Fasheh { 2202415cb800SMark Fasheh unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given; 2203415cb800SMark Fasheh 22041ed9b777SJoel Becker return __ocfs2_claim_clusters(handle, ac, min_clusters, 2205415cb800SMark Fasheh bits_wanted, cluster_start, num_clusters); 2206415cb800SMark Fasheh } 2207415cb800SMark Fasheh 2208b4414eeaSMark Fasheh static int ocfs2_block_group_clear_bits(handle_t *handle, 2209ccd979bdSMark Fasheh struct inode *alloc_inode, 2210ccd979bdSMark Fasheh struct ocfs2_group_desc *bg, 2211ccd979bdSMark Fasheh struct buffer_head *group_bh, 2212ccd979bdSMark Fasheh unsigned int bit_off, 2213b4414eeaSMark Fasheh unsigned int num_bits, 2214b4414eeaSMark Fasheh void (*undo_fn)(unsigned int bit, 2215b4414eeaSMark Fasheh unsigned long *bmap)) 2216ccd979bdSMark Fasheh { 2217ccd979bdSMark Fasheh int status; 2218ccd979bdSMark Fasheh unsigned int tmp; 2219ccd979bdSMark Fasheh struct ocfs2_group_desc *undo_bg = NULL; 2220ccd979bdSMark Fasheh 2221ccd979bdSMark Fasheh mlog_entry_void(); 2222ccd979bdSMark Fasheh 222342035306SJoel Becker /* The caller got this descriptor from 222442035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 222542035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 2226ccd979bdSMark Fasheh 2227ccd979bdSMark Fasheh mlog(0, "off = %u, num = %u\n", bit_off, num_bits); 2228ccd979bdSMark Fasheh 2229b4414eeaSMark Fasheh BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode)); 22300cf2f763SJoel Becker status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), 2231b4414eeaSMark Fasheh group_bh, 2232b4414eeaSMark Fasheh undo_fn ? 2233b4414eeaSMark Fasheh OCFS2_JOURNAL_ACCESS_UNDO : 2234b4414eeaSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 2235ccd979bdSMark Fasheh if (status < 0) { 2236ccd979bdSMark Fasheh mlog_errno(status); 2237ccd979bdSMark Fasheh goto bail; 2238ccd979bdSMark Fasheh } 2239ccd979bdSMark Fasheh 2240b4414eeaSMark Fasheh if (undo_fn) { 224194e41ecfSSunil Mushran jbd_lock_bh_state(group_bh); 224294e41ecfSSunil Mushran undo_bg = (struct ocfs2_group_desc *) 224394e41ecfSSunil Mushran bh2jh(group_bh)->b_committed_data; 224494e41ecfSSunil Mushran BUG_ON(!undo_bg); 224594e41ecfSSunil Mushran } 2246ccd979bdSMark Fasheh 2247ccd979bdSMark Fasheh tmp = num_bits; 2248ccd979bdSMark Fasheh while(tmp--) { 2249ccd979bdSMark Fasheh ocfs2_clear_bit((bit_off + tmp), 2250ccd979bdSMark Fasheh (unsigned long *) bg->bg_bitmap); 2251b4414eeaSMark Fasheh if (undo_fn) 2252b4414eeaSMark Fasheh undo_fn(bit_off + tmp, 2253ccd979bdSMark Fasheh (unsigned long *) undo_bg->bg_bitmap); 2254ccd979bdSMark Fasheh } 2255ccd979bdSMark Fasheh le16_add_cpu(&bg->bg_free_bits_count, num_bits); 2256ccd979bdSMark Fasheh 2257b4414eeaSMark Fasheh if (undo_fn) 225894e41ecfSSunil Mushran jbd_unlock_bh_state(group_bh); 225994e41ecfSSunil Mushran 2260ec20cec7SJoel Becker ocfs2_journal_dirty(handle, group_bh); 2261ccd979bdSMark Fasheh bail: 2262ccd979bdSMark Fasheh return status; 2263ccd979bdSMark Fasheh } 2264ccd979bdSMark Fasheh 2265ccd979bdSMark Fasheh /* 2266ccd979bdSMark Fasheh * expects the suballoc inode to already be locked. 2267ccd979bdSMark Fasheh */ 2268b4414eeaSMark Fasheh static int _ocfs2_free_suballoc_bits(handle_t *handle, 2269ccd979bdSMark Fasheh struct inode *alloc_inode, 2270ccd979bdSMark Fasheh struct buffer_head *alloc_bh, 2271ccd979bdSMark Fasheh unsigned int start_bit, 2272ccd979bdSMark Fasheh u64 bg_blkno, 2273b4414eeaSMark Fasheh unsigned int count, 2274b4414eeaSMark Fasheh void (*undo_fn)(unsigned int bit, 2275b4414eeaSMark Fasheh unsigned long *bitmap)) 2276ccd979bdSMark Fasheh { 2277ccd979bdSMark Fasheh int status = 0; 2278ccd979bdSMark Fasheh u32 tmp_used; 2279ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data; 2280ccd979bdSMark Fasheh struct ocfs2_chain_list *cl = &fe->id2.i_chain; 2281ccd979bdSMark Fasheh struct buffer_head *group_bh = NULL; 2282ccd979bdSMark Fasheh struct ocfs2_group_desc *group; 2283ccd979bdSMark Fasheh 2284ccd979bdSMark Fasheh mlog_entry_void(); 2285ccd979bdSMark Fasheh 228610995aa2SJoel Becker /* The alloc_bh comes from ocfs2_free_dinode() or 228710995aa2SJoel Becker * ocfs2_free_clusters(). The callers have all locked the 228810995aa2SJoel Becker * allocator and gotten alloc_bh from the lock call. This 228910995aa2SJoel Becker * validates the dinode buffer. Any corruption that has happended 229010995aa2SJoel Becker * is a code bug. */ 229110995aa2SJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); 2292ccd979bdSMark Fasheh BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl)); 2293ccd979bdSMark Fasheh 2294b0697053SMark Fasheh mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n", 2295b0697053SMark Fasheh (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count, 2296b0697053SMark Fasheh (unsigned long long)bg_blkno, start_bit); 2297ccd979bdSMark Fasheh 229868f64d47SJoel Becker status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno, 229968f64d47SJoel Becker &group_bh); 2300ccd979bdSMark Fasheh if (status < 0) { 2301ccd979bdSMark Fasheh mlog_errno(status); 2302ccd979bdSMark Fasheh goto bail; 2303ccd979bdSMark Fasheh } 2304ccd979bdSMark Fasheh group = (struct ocfs2_group_desc *) group_bh->b_data; 230568f64d47SJoel Becker 2306ccd979bdSMark Fasheh BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits)); 2307ccd979bdSMark Fasheh 2308ccd979bdSMark Fasheh status = ocfs2_block_group_clear_bits(handle, alloc_inode, 2309ccd979bdSMark Fasheh group, group_bh, 2310b4414eeaSMark Fasheh start_bit, count, undo_fn); 2311ccd979bdSMark Fasheh if (status < 0) { 2312ccd979bdSMark Fasheh mlog_errno(status); 2313ccd979bdSMark Fasheh goto bail; 2314ccd979bdSMark Fasheh } 2315ccd979bdSMark Fasheh 23160cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), 23170cf2f763SJoel Becker alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE); 2318ccd979bdSMark Fasheh if (status < 0) { 2319ccd979bdSMark Fasheh mlog_errno(status); 2320ccd979bdSMark Fasheh goto bail; 2321ccd979bdSMark Fasheh } 2322ccd979bdSMark Fasheh 2323ccd979bdSMark Fasheh le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free, 2324ccd979bdSMark Fasheh count); 2325ccd979bdSMark Fasheh tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used); 2326ccd979bdSMark Fasheh fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count); 2327ec20cec7SJoel Becker ocfs2_journal_dirty(handle, alloc_bh); 2328ccd979bdSMark Fasheh 2329ccd979bdSMark Fasheh bail: 2330ccd979bdSMark Fasheh brelse(group_bh); 2331ccd979bdSMark Fasheh 2332ccd979bdSMark Fasheh mlog_exit(status); 2333ccd979bdSMark Fasheh return status; 2334ccd979bdSMark Fasheh } 2335ccd979bdSMark Fasheh 2336b4414eeaSMark Fasheh int ocfs2_free_suballoc_bits(handle_t *handle, 2337b4414eeaSMark Fasheh struct inode *alloc_inode, 2338b4414eeaSMark Fasheh struct buffer_head *alloc_bh, 2339b4414eeaSMark Fasheh unsigned int start_bit, 2340b4414eeaSMark Fasheh u64 bg_blkno, 2341b4414eeaSMark Fasheh unsigned int count) 2342b4414eeaSMark Fasheh { 2343b4414eeaSMark Fasheh return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh, 2344b4414eeaSMark Fasheh start_bit, bg_blkno, count, NULL); 2345b4414eeaSMark Fasheh } 2346b4414eeaSMark Fasheh 23471fabe148SMark Fasheh int ocfs2_free_dinode(handle_t *handle, 2348ccd979bdSMark Fasheh struct inode *inode_alloc_inode, 2349ccd979bdSMark Fasheh struct buffer_head *inode_alloc_bh, 2350ccd979bdSMark Fasheh struct ocfs2_dinode *di) 2351ccd979bdSMark Fasheh { 2352ccd979bdSMark Fasheh u64 blk = le64_to_cpu(di->i_blkno); 2353ccd979bdSMark Fasheh u16 bit = le16_to_cpu(di->i_suballoc_bit); 2354ccd979bdSMark Fasheh u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit); 2355ccd979bdSMark Fasheh 235674380c47STao Ma if (di->i_suballoc_loc) 235774380c47STao Ma bg_blkno = le64_to_cpu(di->i_suballoc_loc); 2358ccd979bdSMark Fasheh return ocfs2_free_suballoc_bits(handle, inode_alloc_inode, 2359ccd979bdSMark Fasheh inode_alloc_bh, bit, bg_blkno, 1); 2360ccd979bdSMark Fasheh } 2361ccd979bdSMark Fasheh 2362b4414eeaSMark Fasheh static int _ocfs2_free_clusters(handle_t *handle, 2363ccd979bdSMark Fasheh struct inode *bitmap_inode, 2364ccd979bdSMark Fasheh struct buffer_head *bitmap_bh, 2365ccd979bdSMark Fasheh u64 start_blk, 2366b4414eeaSMark Fasheh unsigned int num_clusters, 2367b4414eeaSMark Fasheh void (*undo_fn)(unsigned int bit, 2368b4414eeaSMark Fasheh unsigned long *bitmap)) 2369ccd979bdSMark Fasheh { 2370ccd979bdSMark Fasheh int status; 2371ccd979bdSMark Fasheh u16 bg_start_bit; 2372ccd979bdSMark Fasheh u64 bg_blkno; 2373ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 2374ccd979bdSMark Fasheh 2375ccd979bdSMark Fasheh /* You can't ever have a contiguous set of clusters 2376ccd979bdSMark Fasheh * bigger than a block group bitmap so we never have to worry 2377ccd979bdSMark Fasheh * about looping on them. */ 2378ccd979bdSMark Fasheh 2379ccd979bdSMark Fasheh mlog_entry_void(); 2380ccd979bdSMark Fasheh 2381ccd979bdSMark Fasheh /* This is expensive. We can safely remove once this stuff has 2382ccd979bdSMark Fasheh * gotten tested really well. */ 2383ccd979bdSMark Fasheh BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk))); 2384ccd979bdSMark Fasheh 2385ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) bitmap_bh->b_data; 2386ccd979bdSMark Fasheh 2387ccd979bdSMark Fasheh ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno, 2388ccd979bdSMark Fasheh &bg_start_bit); 2389ccd979bdSMark Fasheh 2390b0697053SMark Fasheh mlog(0, "want to free %u clusters starting at block %llu\n", 2391b0697053SMark Fasheh num_clusters, (unsigned long long)start_blk); 2392b0697053SMark Fasheh mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n", 2393b0697053SMark Fasheh (unsigned long long)bg_blkno, bg_start_bit); 2394ccd979bdSMark Fasheh 2395b4414eeaSMark Fasheh status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh, 2396ccd979bdSMark Fasheh bg_start_bit, bg_blkno, 2397b4414eeaSMark Fasheh num_clusters, undo_fn); 23989c7af40bSMark Fasheh if (status < 0) { 2399ccd979bdSMark Fasheh mlog_errno(status); 24009c7af40bSMark Fasheh goto out; 24019c7af40bSMark Fasheh } 2402ccd979bdSMark Fasheh 24039c7af40bSMark Fasheh ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb), 24049c7af40bSMark Fasheh num_clusters); 24059c7af40bSMark Fasheh 24069c7af40bSMark Fasheh out: 2407ccd979bdSMark Fasheh mlog_exit(status); 2408ccd979bdSMark Fasheh return status; 2409ccd979bdSMark Fasheh } 2410ccd979bdSMark Fasheh 2411b4414eeaSMark Fasheh int ocfs2_free_clusters(handle_t *handle, 2412b4414eeaSMark Fasheh struct inode *bitmap_inode, 2413b4414eeaSMark Fasheh struct buffer_head *bitmap_bh, 2414b4414eeaSMark Fasheh u64 start_blk, 2415b4414eeaSMark Fasheh unsigned int num_clusters) 2416b4414eeaSMark Fasheh { 2417b4414eeaSMark Fasheh return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh, 2418b4414eeaSMark Fasheh start_blk, num_clusters, 2419b4414eeaSMark Fasheh _ocfs2_set_bit); 2420b4414eeaSMark Fasheh } 2421b4414eeaSMark Fasheh 2422b4414eeaSMark Fasheh /* 2423b4414eeaSMark Fasheh * Give never-used clusters back to the global bitmap. We don't need 2424b4414eeaSMark Fasheh * to protect these bits in the undo buffer. 2425b4414eeaSMark Fasheh */ 2426b4414eeaSMark Fasheh int ocfs2_release_clusters(handle_t *handle, 2427b4414eeaSMark Fasheh struct inode *bitmap_inode, 2428b4414eeaSMark Fasheh struct buffer_head *bitmap_bh, 2429b4414eeaSMark Fasheh u64 start_blk, 2430b4414eeaSMark Fasheh unsigned int num_clusters) 2431b4414eeaSMark Fasheh { 2432b4414eeaSMark Fasheh return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh, 2433b4414eeaSMark Fasheh start_blk, num_clusters, 2434b4414eeaSMark Fasheh _ocfs2_clear_bit); 2435b4414eeaSMark Fasheh } 2436b4414eeaSMark Fasheh 2437ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg) 2438ccd979bdSMark Fasheh { 2439ccd979bdSMark Fasheh printk("Block Group:\n"); 2440ccd979bdSMark Fasheh printk("bg_signature: %s\n", bg->bg_signature); 2441ccd979bdSMark Fasheh printk("bg_size: %u\n", bg->bg_size); 2442ccd979bdSMark Fasheh printk("bg_bits: %u\n", bg->bg_bits); 2443ccd979bdSMark Fasheh printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count); 2444ccd979bdSMark Fasheh printk("bg_chain: %u\n", bg->bg_chain); 2445ccd979bdSMark Fasheh printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation)); 2446b0697053SMark Fasheh printk("bg_next_group: %llu\n", 2447b0697053SMark Fasheh (unsigned long long)bg->bg_next_group); 2448b0697053SMark Fasheh printk("bg_parent_dinode: %llu\n", 2449b0697053SMark Fasheh (unsigned long long)bg->bg_parent_dinode); 2450b0697053SMark Fasheh printk("bg_blkno: %llu\n", 2451b0697053SMark Fasheh (unsigned long long)bg->bg_blkno); 2452ccd979bdSMark Fasheh } 2453ccd979bdSMark Fasheh 2454ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe) 2455ccd979bdSMark Fasheh { 2456ccd979bdSMark Fasheh int i; 2457ccd979bdSMark Fasheh 2458b0697053SMark Fasheh printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno); 2459ccd979bdSMark Fasheh printk("i_signature: %s\n", fe->i_signature); 2460b0697053SMark Fasheh printk("i_size: %llu\n", 2461b0697053SMark Fasheh (unsigned long long)fe->i_size); 2462ccd979bdSMark Fasheh printk("i_clusters: %u\n", fe->i_clusters); 2463ccd979bdSMark Fasheh printk("i_generation: %u\n", 2464ccd979bdSMark Fasheh le32_to_cpu(fe->i_generation)); 2465ccd979bdSMark Fasheh printk("id1.bitmap1.i_used: %u\n", 2466ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_used)); 2467ccd979bdSMark Fasheh printk("id1.bitmap1.i_total: %u\n", 2468ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_total)); 2469ccd979bdSMark Fasheh printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg); 2470ccd979bdSMark Fasheh printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc); 2471ccd979bdSMark Fasheh printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count); 2472ccd979bdSMark Fasheh printk("id2.i_chain.cl_next_free_rec: %u\n", 2473ccd979bdSMark Fasheh fe->id2.i_chain.cl_next_free_rec); 2474ccd979bdSMark Fasheh for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) { 2475ccd979bdSMark Fasheh printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i, 2476ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[i].c_free); 2477ccd979bdSMark Fasheh printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i, 2478ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[i].c_total); 2479b0697053SMark Fasheh printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i, 2480b0697053SMark Fasheh (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno); 2481ccd979bdSMark Fasheh } 2482ccd979bdSMark Fasheh } 2483e7d4cb6bSTao Ma 2484e7d4cb6bSTao Ma /* 2485e7d4cb6bSTao Ma * For a given allocation, determine which allocators will need to be 2486e7d4cb6bSTao Ma * accessed, and lock them, reserving the appropriate number of bits. 2487e7d4cb6bSTao Ma * 2488e7d4cb6bSTao Ma * Sparse file systems call this from ocfs2_write_begin_nolock() 2489e7d4cb6bSTao Ma * and ocfs2_allocate_unwritten_extents(). 2490e7d4cb6bSTao Ma * 2491e7d4cb6bSTao Ma * File systems which don't support holes call this from 2492e7d4cb6bSTao Ma * ocfs2_extend_allocation(). 2493e7d4cb6bSTao Ma */ 2494f99b9b7cSJoel Becker int ocfs2_lock_allocators(struct inode *inode, 2495f99b9b7cSJoel Becker struct ocfs2_extent_tree *et, 2496e7d4cb6bSTao Ma u32 clusters_to_add, u32 extents_to_split, 2497e7d4cb6bSTao Ma struct ocfs2_alloc_context **data_ac, 2498f99b9b7cSJoel Becker struct ocfs2_alloc_context **meta_ac) 2499e7d4cb6bSTao Ma { 2500e7d4cb6bSTao Ma int ret = 0, num_free_extents; 2501e7d4cb6bSTao Ma unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split; 2502e7d4cb6bSTao Ma struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2503e7d4cb6bSTao Ma 2504e7d4cb6bSTao Ma *meta_ac = NULL; 2505e7d4cb6bSTao Ma if (data_ac) 2506e7d4cb6bSTao Ma *data_ac = NULL; 2507e7d4cb6bSTao Ma 2508e7d4cb6bSTao Ma BUG_ON(clusters_to_add != 0 && data_ac == NULL); 2509e7d4cb6bSTao Ma 25103d03a305SJoel Becker num_free_extents = ocfs2_num_free_extents(osb, et); 2511e7d4cb6bSTao Ma if (num_free_extents < 0) { 2512e7d4cb6bSTao Ma ret = num_free_extents; 2513e7d4cb6bSTao Ma mlog_errno(ret); 2514e7d4cb6bSTao Ma goto out; 2515e7d4cb6bSTao Ma } 2516e7d4cb6bSTao Ma 2517e7d4cb6bSTao Ma /* 2518e7d4cb6bSTao Ma * Sparse allocation file systems need to be more conservative 2519e7d4cb6bSTao Ma * with reserving room for expansion - the actual allocation 2520e7d4cb6bSTao Ma * happens while we've got a journal handle open so re-taking 2521e7d4cb6bSTao Ma * a cluster lock (because we ran out of room for another 2522e7d4cb6bSTao Ma * extent) will violate ordering rules. 2523e7d4cb6bSTao Ma * 2524e7d4cb6bSTao Ma * Most of the time we'll only be seeing this 1 cluster at a time 2525e7d4cb6bSTao Ma * anyway. 2526e7d4cb6bSTao Ma * 2527e7d4cb6bSTao Ma * Always lock for any unwritten extents - we might want to 2528e7d4cb6bSTao Ma * add blocks during a split. 2529e7d4cb6bSTao Ma */ 2530e7d4cb6bSTao Ma if (!num_free_extents || 2531e7d4cb6bSTao Ma (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) { 2532f99b9b7cSJoel Becker ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac); 2533e7d4cb6bSTao Ma if (ret < 0) { 2534e7d4cb6bSTao Ma if (ret != -ENOSPC) 2535e7d4cb6bSTao Ma mlog_errno(ret); 2536e7d4cb6bSTao Ma goto out; 2537e7d4cb6bSTao Ma } 2538e7d4cb6bSTao Ma } 2539e7d4cb6bSTao Ma 2540e7d4cb6bSTao Ma if (clusters_to_add == 0) 2541e7d4cb6bSTao Ma goto out; 2542e7d4cb6bSTao Ma 2543e7d4cb6bSTao Ma ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac); 2544e7d4cb6bSTao Ma if (ret < 0) { 2545e7d4cb6bSTao Ma if (ret != -ENOSPC) 2546e7d4cb6bSTao Ma mlog_errno(ret); 2547e7d4cb6bSTao Ma goto out; 2548e7d4cb6bSTao Ma } 2549e7d4cb6bSTao Ma 2550e7d4cb6bSTao Ma out: 2551e7d4cb6bSTao Ma if (ret) { 2552e7d4cb6bSTao Ma if (*meta_ac) { 2553e7d4cb6bSTao Ma ocfs2_free_alloc_context(*meta_ac); 2554e7d4cb6bSTao Ma *meta_ac = NULL; 2555e7d4cb6bSTao Ma } 2556e7d4cb6bSTao Ma 2557e7d4cb6bSTao Ma /* 2558e7d4cb6bSTao Ma * We cannot have an error and a non null *data_ac. 2559e7d4cb6bSTao Ma */ 2560e7d4cb6bSTao Ma } 2561e7d4cb6bSTao Ma 2562e7d4cb6bSTao Ma return ret; 2563e7d4cb6bSTao Ma } 25646ca497a8Swengang wang 25656ca497a8Swengang wang /* 25666ca497a8Swengang wang * Read the inode specified by blkno to get suballoc_slot and 25676ca497a8Swengang wang * suballoc_bit. 25686ca497a8Swengang wang */ 25696ca497a8Swengang wang static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno, 25706ca497a8Swengang wang u16 *suballoc_slot, u16 *suballoc_bit) 25716ca497a8Swengang wang { 25726ca497a8Swengang wang int status; 25736ca497a8Swengang wang struct buffer_head *inode_bh = NULL; 25746ca497a8Swengang wang struct ocfs2_dinode *inode_fe; 25756ca497a8Swengang wang 25765b09b507SJoel Becker mlog_entry("blkno: %llu\n", (unsigned long long)blkno); 25776ca497a8Swengang wang 25786ca497a8Swengang wang /* dirty read disk */ 25796ca497a8Swengang wang status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh); 25806ca497a8Swengang wang if (status < 0) { 25815b09b507SJoel Becker mlog(ML_ERROR, "read block %llu failed %d\n", 25825b09b507SJoel Becker (unsigned long long)blkno, status); 25836ca497a8Swengang wang goto bail; 25846ca497a8Swengang wang } 25856ca497a8Swengang wang 25866ca497a8Swengang wang inode_fe = (struct ocfs2_dinode *) inode_bh->b_data; 25876ca497a8Swengang wang if (!OCFS2_IS_VALID_DINODE(inode_fe)) { 25885b09b507SJoel Becker mlog(ML_ERROR, "invalid inode %llu requested\n", 25895b09b507SJoel Becker (unsigned long long)blkno); 25906ca497a8Swengang wang status = -EINVAL; 25916ca497a8Swengang wang goto bail; 25926ca497a8Swengang wang } 25936ca497a8Swengang wang 25940fba8137STao Ma if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT && 25956ca497a8Swengang wang (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) { 25966ca497a8Swengang wang mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n", 25975b09b507SJoel Becker (unsigned long long)blkno, 25985b09b507SJoel Becker (u32)le16_to_cpu(inode_fe->i_suballoc_slot)); 25996ca497a8Swengang wang status = -EINVAL; 26006ca497a8Swengang wang goto bail; 26016ca497a8Swengang wang } 26026ca497a8Swengang wang 26036ca497a8Swengang wang if (suballoc_slot) 26046ca497a8Swengang wang *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot); 26056ca497a8Swengang wang if (suballoc_bit) 26066ca497a8Swengang wang *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit); 26076ca497a8Swengang wang 26086ca497a8Swengang wang bail: 26096ca497a8Swengang wang brelse(inode_bh); 26106ca497a8Swengang wang 26116ca497a8Swengang wang mlog_exit(status); 26126ca497a8Swengang wang return status; 26136ca497a8Swengang wang } 26146ca497a8Swengang wang 26156ca497a8Swengang wang /* 26166ca497a8Swengang wang * test whether bit is SET in allocator bitmap or not. on success, 0 26176ca497a8Swengang wang * is returned and *res is 1 for SET; 0 otherwise. when fails, errno 26186ca497a8Swengang wang * is returned and *res is meaningless. Call this after you have 26196ca497a8Swengang wang * cluster locked against suballoc, or you may get a result based on 26206ca497a8Swengang wang * non-up2date contents 26216ca497a8Swengang wang */ 26226ca497a8Swengang wang static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb, 26236ca497a8Swengang wang struct inode *suballoc, 26246ca497a8Swengang wang struct buffer_head *alloc_bh, u64 blkno, 26256ca497a8Swengang wang u16 bit, int *res) 26266ca497a8Swengang wang { 2627abf1b3cbSTao Ma struct ocfs2_dinode *alloc_di; 26286ca497a8Swengang wang struct ocfs2_group_desc *group; 26296ca497a8Swengang wang struct buffer_head *group_bh = NULL; 26306ca497a8Swengang wang u64 bg_blkno; 26316ca497a8Swengang wang int status; 26326ca497a8Swengang wang 26335b09b507SJoel Becker mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno, 26345b09b507SJoel Becker (unsigned int)bit); 26356ca497a8Swengang wang 2636abf1b3cbSTao Ma alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data; 2637abf1b3cbSTao Ma if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) { 26386ca497a8Swengang wang mlog(ML_ERROR, "suballoc bit %u out of range of %u\n", 26396ca497a8Swengang wang (unsigned int)bit, 2640abf1b3cbSTao Ma ocfs2_bits_per_group(&alloc_di->id2.i_chain)); 26416ca497a8Swengang wang status = -EINVAL; 26426ca497a8Swengang wang goto bail; 26436ca497a8Swengang wang } 26446ca497a8Swengang wang 2645abf1b3cbSTao Ma if (alloc_di->i_suballoc_loc) 2646abf1b3cbSTao Ma bg_blkno = le64_to_cpu(alloc_di->i_suballoc_loc); 2647abf1b3cbSTao Ma else 26486ca497a8Swengang wang bg_blkno = ocfs2_which_suballoc_group(blkno, bit); 2649abf1b3cbSTao Ma status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno, 26506ca497a8Swengang wang &group_bh); 26516ca497a8Swengang wang if (status < 0) { 26525b09b507SJoel Becker mlog(ML_ERROR, "read group %llu failed %d\n", 26535b09b507SJoel Becker (unsigned long long)bg_blkno, status); 26546ca497a8Swengang wang goto bail; 26556ca497a8Swengang wang } 26566ca497a8Swengang wang 26576ca497a8Swengang wang group = (struct ocfs2_group_desc *) group_bh->b_data; 26586ca497a8Swengang wang *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap); 26596ca497a8Swengang wang 26606ca497a8Swengang wang bail: 26616ca497a8Swengang wang brelse(group_bh); 26626ca497a8Swengang wang 26636ca497a8Swengang wang mlog_exit(status); 26646ca497a8Swengang wang return status; 26656ca497a8Swengang wang } 26666ca497a8Swengang wang 26676ca497a8Swengang wang /* 26686ca497a8Swengang wang * Test if the bit representing this inode (blkno) is set in the 26696ca497a8Swengang wang * suballocator. 26706ca497a8Swengang wang * 26716ca497a8Swengang wang * On success, 0 is returned and *res is 1 for SET; 0 otherwise. 26726ca497a8Swengang wang * 26736ca497a8Swengang wang * In the event of failure, a negative value is returned and *res is 26746ca497a8Swengang wang * meaningless. 26756ca497a8Swengang wang * 26766ca497a8Swengang wang * Callers must make sure to hold nfs_sync_lock to prevent 26776ca497a8Swengang wang * ocfs2_delete_inode() on another node from accessing the same 26786ca497a8Swengang wang * suballocator concurrently. 26796ca497a8Swengang wang */ 26806ca497a8Swengang wang int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res) 26816ca497a8Swengang wang { 26826ca497a8Swengang wang int status; 26836ca497a8Swengang wang u16 suballoc_bit = 0, suballoc_slot = 0; 26846ca497a8Swengang wang struct inode *inode_alloc_inode; 26856ca497a8Swengang wang struct buffer_head *alloc_bh = NULL; 26866ca497a8Swengang wang 26875b09b507SJoel Becker mlog_entry("blkno: %llu", (unsigned long long)blkno); 26886ca497a8Swengang wang 26896ca497a8Swengang wang status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot, 26906ca497a8Swengang wang &suballoc_bit); 26916ca497a8Swengang wang if (status < 0) { 26926ca497a8Swengang wang mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status); 26936ca497a8Swengang wang goto bail; 26946ca497a8Swengang wang } 26956ca497a8Swengang wang 26966ca497a8Swengang wang inode_alloc_inode = 26976ca497a8Swengang wang ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE, 26986ca497a8Swengang wang suballoc_slot); 26996ca497a8Swengang wang if (!inode_alloc_inode) { 27006ca497a8Swengang wang /* the error code could be inaccurate, but we are not able to 27016ca497a8Swengang wang * get the correct one. */ 27026ca497a8Swengang wang status = -EINVAL; 27036ca497a8Swengang wang mlog(ML_ERROR, "unable to get alloc inode in slot %u\n", 27046ca497a8Swengang wang (u32)suballoc_slot); 27056ca497a8Swengang wang goto bail; 27066ca497a8Swengang wang } 27076ca497a8Swengang wang 27086ca497a8Swengang wang mutex_lock(&inode_alloc_inode->i_mutex); 27096ca497a8Swengang wang status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0); 27106ca497a8Swengang wang if (status < 0) { 27116ca497a8Swengang wang mutex_unlock(&inode_alloc_inode->i_mutex); 27126ca497a8Swengang wang mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n", 27136ca497a8Swengang wang (u32)suballoc_slot, status); 27146ca497a8Swengang wang goto bail; 27156ca497a8Swengang wang } 27166ca497a8Swengang wang 27176ca497a8Swengang wang status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh, 27186ca497a8Swengang wang blkno, suballoc_bit, res); 27196ca497a8Swengang wang if (status < 0) 27206ca497a8Swengang wang mlog(ML_ERROR, "test suballoc bit failed %d\n", status); 27216ca497a8Swengang wang 27226ca497a8Swengang wang ocfs2_inode_unlock(inode_alloc_inode, 0); 27236ca497a8Swengang wang mutex_unlock(&inode_alloc_inode->i_mutex); 27246ca497a8Swengang wang 27256ca497a8Swengang wang iput(inode_alloc_inode); 27266ca497a8Swengang wang brelse(alloc_bh); 27276ca497a8Swengang wang bail: 27286ca497a8Swengang wang mlog_exit(status); 27296ca497a8Swengang wang return status; 27306ca497a8Swengang wang } 2731