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. */ 60e49e2767SMark Fasheh u64 sr_bg_stable_blkno; /* 61e49e2767SMark Fasheh * Doesn't change, always 62e49e2767SMark Fasheh * set to target block 63e49e2767SMark Fasheh * group descriptor 64e49e2767SMark Fasheh * block. 65e49e2767SMark Fasheh */ 66ba206635SJoel Becker u64 sr_blkno; /* The first allocated block */ 677d1fe093SJoel Becker unsigned int sr_bit_offset; /* The bit in the bg */ 687d1fe093SJoel Becker unsigned int sr_bits; /* How many bits we claimed */ 697d1fe093SJoel Becker }; 707d1fe093SJoel Becker 71b2b6ebf5SMark Fasheh static u64 ocfs2_group_from_res(struct ocfs2_suballoc_result *res) 72b2b6ebf5SMark Fasheh { 73b2b6ebf5SMark Fasheh if (res->sr_blkno == 0) 74b2b6ebf5SMark Fasheh return 0; 75b2b6ebf5SMark Fasheh 76b2b6ebf5SMark Fasheh if (res->sr_bg_blkno) 77b2b6ebf5SMark Fasheh return res->sr_bg_blkno; 78b2b6ebf5SMark Fasheh 79b2b6ebf5SMark Fasheh return ocfs2_which_suballoc_group(res->sr_blkno, res->sr_bit_offset); 80b2b6ebf5SMark Fasheh } 81b2b6ebf5SMark Fasheh 82ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg); 83ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe); 84ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl); 851fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle, 86ccd979bdSMark Fasheh struct inode *alloc_inode, 87ccd979bdSMark Fasheh struct buffer_head *bg_bh, 88ccd979bdSMark Fasheh u64 group_blkno, 89798db35fSJoel Becker unsigned int group_clusters, 90ccd979bdSMark Fasheh u16 my_chain, 91ccd979bdSMark Fasheh struct ocfs2_chain_list *cl); 92ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb, 93ccd979bdSMark Fasheh struct inode *alloc_inode, 941187c968SJoel Becker struct buffer_head *bh, 9560ca81e8STao Ma u64 max_block, 96feb473a6STao Ma u64 *last_alloc_group, 9760ca81e8STao Ma int flags); 98ccd979bdSMark Fasheh 99ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode, 100ccd979bdSMark Fasheh struct buffer_head *group_bh, 101ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 1021187c968SJoel Becker u64 max_block, 1037d1fe093SJoel Becker struct ocfs2_suballoc_result *res); 104ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode, 105ccd979bdSMark Fasheh struct buffer_head *group_bh, 106ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 1071187c968SJoel Becker u64 max_block, 1087d1fe093SJoel Becker struct ocfs2_suballoc_result *res); 109aa8f8e93SJoel Becker static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac, 1101fabe148SMark Fasheh handle_t *handle, 111ccd979bdSMark Fasheh u32 bits_wanted, 112ccd979bdSMark Fasheh u32 min_bits, 1137d1fe093SJoel Becker struct ocfs2_suballoc_result *res); 114ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh, 115ccd979bdSMark Fasheh int nr); 1161fabe148SMark Fasheh static inline int ocfs2_block_group_set_bits(handle_t *handle, 117ccd979bdSMark Fasheh struct inode *alloc_inode, 118ccd979bdSMark Fasheh struct ocfs2_group_desc *bg, 119ccd979bdSMark Fasheh struct buffer_head *group_bh, 120ccd979bdSMark Fasheh unsigned int bit_off, 121ccd979bdSMark Fasheh unsigned int num_bits); 1221fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle, 123ccd979bdSMark Fasheh struct inode *alloc_inode, 124ccd979bdSMark Fasheh struct buffer_head *fe_bh, 125ccd979bdSMark Fasheh struct buffer_head *bg_bh, 126ccd979bdSMark Fasheh struct buffer_head *prev_bg_bh, 127ccd979bdSMark Fasheh u16 chain); 128ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg, 129ccd979bdSMark Fasheh u32 wanted); 130ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode, 131ccd979bdSMark Fasheh u64 bg_blkno, 132ccd979bdSMark Fasheh u16 bg_bit_off); 133ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode, 134ccd979bdSMark Fasheh u64 data_blkno, 135ccd979bdSMark Fasheh u64 *bg_blkno, 136ccd979bdSMark Fasheh u16 *bg_bit_off); 1371187c968SJoel Becker static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb, 1381187c968SJoel Becker u32 bits_wanted, u64 max_block, 13960ca81e8STao Ma int flags, 1401187c968SJoel Becker struct ocfs2_alloc_context **ac); 141ccd979bdSMark Fasheh 1429c7af40bSMark Fasheh void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac) 143ccd979bdSMark Fasheh { 144da5cbf2fSMark Fasheh struct inode *inode = ac->ac_inode; 145da5cbf2fSMark Fasheh 146da5cbf2fSMark Fasheh if (inode) { 147da5cbf2fSMark Fasheh if (ac->ac_which != OCFS2_AC_USE_LOCAL) 148e63aecb6SMark Fasheh ocfs2_inode_unlock(inode, 1); 149da5cbf2fSMark Fasheh 150da5cbf2fSMark Fasheh mutex_unlock(&inode->i_mutex); 151da5cbf2fSMark Fasheh 152da5cbf2fSMark Fasheh iput(inode); 1534d0ddb2cSTao Ma ac->ac_inode = NULL; 154da5cbf2fSMark Fasheh } 155ccd979bdSMark Fasheh brelse(ac->ac_bh); 1564d0ddb2cSTao Ma ac->ac_bh = NULL; 157e3b4a97dSMark Fasheh ac->ac_resv = NULL; 158e49e2767SMark Fasheh if (ac->ac_find_loc_priv) { 159e49e2767SMark Fasheh kfree(ac->ac_find_loc_priv); 160e49e2767SMark Fasheh ac->ac_find_loc_priv = NULL; 161e49e2767SMark Fasheh } 1624d0ddb2cSTao Ma } 1634d0ddb2cSTao Ma 1644d0ddb2cSTao Ma void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac) 1654d0ddb2cSTao Ma { 1664d0ddb2cSTao Ma ocfs2_free_ac_resource(ac); 167ccd979bdSMark Fasheh kfree(ac); 168ccd979bdSMark Fasheh } 169ccd979bdSMark Fasheh 170ccd979bdSMark Fasheh static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl) 171ccd979bdSMark Fasheh { 172ccd979bdSMark Fasheh return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc); 173ccd979bdSMark Fasheh } 174ccd979bdSMark Fasheh 17557e3e797SJoel Becker #define do_error(fmt, ...) \ 17657e3e797SJoel Becker do{ \ 17778c37eb0STao Ma if (resize) \ 17857e3e797SJoel Becker mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \ 17957e3e797SJoel Becker else \ 18057e3e797SJoel Becker ocfs2_error(sb, fmt, ##__VA_ARGS__); \ 18157e3e797SJoel Becker } while (0) 18257e3e797SJoel Becker 183970e4936SJoel Becker static int ocfs2_validate_gd_self(struct super_block *sb, 184970e4936SJoel Becker struct buffer_head *bh, 18578c37eb0STao Ma int resize) 186970e4936SJoel Becker { 187970e4936SJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 188970e4936SJoel Becker 1897bf72edeSMark Fasheh if (!OCFS2_IS_VALID_GROUP_DESC(gd)) { 19068f64d47SJoel Becker do_error("Group descriptor #%llu has bad signature %.*s", 19168f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 7, 19257e3e797SJoel Becker gd->bg_signature); 19357e3e797SJoel Becker return -EINVAL; 1947bf72edeSMark Fasheh } 1957bf72edeSMark Fasheh 19668f64d47SJoel Becker if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) { 19768f64d47SJoel Becker do_error("Group descriptor #%llu has an invalid bg_blkno " 19868f64d47SJoel Becker "of %llu", 19968f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 20068f64d47SJoel Becker (unsigned long long)le64_to_cpu(gd->bg_blkno)); 20168f64d47SJoel Becker return -EINVAL; 20268f64d47SJoel Becker } 20368f64d47SJoel Becker 20468f64d47SJoel Becker if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) { 20568f64d47SJoel Becker do_error("Group descriptor #%llu has an invalid " 20668f64d47SJoel Becker "fs_generation of #%u", 20768f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 20868f64d47SJoel Becker le32_to_cpu(gd->bg_generation)); 20968f64d47SJoel Becker return -EINVAL; 21068f64d47SJoel Becker } 21168f64d47SJoel Becker 212970e4936SJoel Becker if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) { 213970e4936SJoel Becker do_error("Group descriptor #%llu has bit count %u but " 214970e4936SJoel Becker "claims that %u are free", 215970e4936SJoel Becker (unsigned long long)bh->b_blocknr, 216970e4936SJoel Becker le16_to_cpu(gd->bg_bits), 217970e4936SJoel Becker le16_to_cpu(gd->bg_free_bits_count)); 218970e4936SJoel Becker return -EINVAL; 219970e4936SJoel Becker } 220970e4936SJoel Becker 221970e4936SJoel Becker if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) { 222970e4936SJoel Becker do_error("Group descriptor #%llu has bit count %u but " 223970e4936SJoel Becker "max bitmap bits of %u", 224970e4936SJoel Becker (unsigned long long)bh->b_blocknr, 225970e4936SJoel Becker le16_to_cpu(gd->bg_bits), 226970e4936SJoel Becker 8 * le16_to_cpu(gd->bg_size)); 227970e4936SJoel Becker return -EINVAL; 228970e4936SJoel Becker } 229970e4936SJoel Becker 230970e4936SJoel Becker return 0; 231970e4936SJoel Becker } 232970e4936SJoel Becker 233970e4936SJoel Becker static int ocfs2_validate_gd_parent(struct super_block *sb, 234970e4936SJoel Becker struct ocfs2_dinode *di, 235970e4936SJoel Becker struct buffer_head *bh, 23678c37eb0STao Ma int resize) 237970e4936SJoel Becker { 238970e4936SJoel Becker unsigned int max_bits; 239970e4936SJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 240970e4936SJoel Becker 2417bf72edeSMark Fasheh if (di->i_blkno != gd->bg_parent_dinode) { 24257e3e797SJoel Becker do_error("Group descriptor #%llu has bad parent " 2437bf72edeSMark Fasheh "pointer (%llu, expected %llu)", 24468f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 2457bf72edeSMark Fasheh (unsigned long long)le64_to_cpu(gd->bg_parent_dinode), 2467bf72edeSMark Fasheh (unsigned long long)le64_to_cpu(di->i_blkno)); 24757e3e797SJoel Becker return -EINVAL; 2487bf72edeSMark Fasheh } 2497bf72edeSMark Fasheh 2507bf72edeSMark Fasheh max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc); 2517bf72edeSMark Fasheh if (le16_to_cpu(gd->bg_bits) > max_bits) { 25257e3e797SJoel Becker do_error("Group descriptor #%llu has bit count of %u", 25368f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 2547bf72edeSMark Fasheh le16_to_cpu(gd->bg_bits)); 25557e3e797SJoel Becker return -EINVAL; 2567bf72edeSMark Fasheh } 2577bf72edeSMark Fasheh 25878c37eb0STao Ma /* In resize, we may meet the case bg_chain == cl_next_free_rec. */ 25978c37eb0STao Ma if ((le16_to_cpu(gd->bg_chain) > 26078c37eb0STao Ma le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) || 26178c37eb0STao Ma ((le16_to_cpu(gd->bg_chain) == 26278c37eb0STao Ma le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) { 26357e3e797SJoel Becker do_error("Group descriptor #%llu has bad chain %u", 26468f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 2657bf72edeSMark Fasheh le16_to_cpu(gd->bg_chain)); 26657e3e797SJoel Becker return -EINVAL; 2677bf72edeSMark Fasheh } 2687bf72edeSMark Fasheh 269970e4936SJoel Becker return 0; 2707bf72edeSMark Fasheh } 2717bf72edeSMark Fasheh 27257e3e797SJoel Becker #undef do_error 2737bf72edeSMark Fasheh 274970e4936SJoel Becker /* 275970e4936SJoel Becker * This version only prints errors. It does not fail the filesystem, and 276970e4936SJoel Becker * exists only for resize. 277970e4936SJoel Becker */ 278970e4936SJoel Becker int ocfs2_check_group_descriptor(struct super_block *sb, 279970e4936SJoel Becker struct ocfs2_dinode *di, 280970e4936SJoel Becker struct buffer_head *bh) 281970e4936SJoel Becker { 282970e4936SJoel Becker int rc; 283d6b32bbbSJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 284970e4936SJoel Becker 285d6b32bbbSJoel Becker BUG_ON(!buffer_uptodate(bh)); 286d6b32bbbSJoel Becker 287d6b32bbbSJoel Becker /* 288d6b32bbbSJoel Becker * If the ecc fails, we return the error but otherwise 289d6b32bbbSJoel Becker * leave the filesystem running. We know any error is 290d6b32bbbSJoel Becker * local to this block. 291d6b32bbbSJoel Becker */ 292d6b32bbbSJoel Becker rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check); 29313723d00SJoel Becker if (rc) { 29413723d00SJoel Becker mlog(ML_ERROR, 29513723d00SJoel Becker "Checksum failed for group descriptor %llu\n", 29613723d00SJoel Becker (unsigned long long)bh->b_blocknr); 29713723d00SJoel Becker } else 298970e4936SJoel Becker rc = ocfs2_validate_gd_self(sb, bh, 1); 299970e4936SJoel Becker if (!rc) 300970e4936SJoel Becker rc = ocfs2_validate_gd_parent(sb, di, bh, 1); 301970e4936SJoel Becker 302970e4936SJoel Becker return rc; 303970e4936SJoel Becker } 304970e4936SJoel Becker 305970e4936SJoel Becker static int ocfs2_validate_group_descriptor(struct super_block *sb, 306970e4936SJoel Becker struct buffer_head *bh) 307970e4936SJoel Becker { 308d6b32bbbSJoel Becker int rc; 309d6b32bbbSJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 310d6b32bbbSJoel Becker 311970e4936SJoel Becker mlog(0, "Validating group descriptor %llu\n", 312970e4936SJoel Becker (unsigned long long)bh->b_blocknr); 313970e4936SJoel Becker 314d6b32bbbSJoel Becker BUG_ON(!buffer_uptodate(bh)); 315d6b32bbbSJoel Becker 316d6b32bbbSJoel Becker /* 317d6b32bbbSJoel Becker * If the ecc fails, we return the error but otherwise 318d6b32bbbSJoel Becker * leave the filesystem running. We know any error is 319d6b32bbbSJoel Becker * local to this block. 320d6b32bbbSJoel Becker */ 321d6b32bbbSJoel Becker rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check); 322d6b32bbbSJoel Becker if (rc) 323d6b32bbbSJoel Becker return rc; 324d6b32bbbSJoel Becker 325d6b32bbbSJoel Becker /* 326d6b32bbbSJoel Becker * Errors after here are fatal. 327d6b32bbbSJoel Becker */ 328d6b32bbbSJoel Becker 329970e4936SJoel Becker return ocfs2_validate_gd_self(sb, bh, 0); 3307bf72edeSMark Fasheh } 3317bf72edeSMark Fasheh 33268f64d47SJoel Becker int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di, 33368f64d47SJoel Becker u64 gd_blkno, struct buffer_head **bh) 33468f64d47SJoel Becker { 33568f64d47SJoel Becker int rc; 33668f64d47SJoel Becker struct buffer_head *tmp = *bh; 33768f64d47SJoel Becker 3388cb471e8SJoel Becker rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp, 339970e4936SJoel Becker ocfs2_validate_group_descriptor); 34068f64d47SJoel Becker if (rc) 34168f64d47SJoel Becker goto out; 34268f64d47SJoel Becker 343970e4936SJoel Becker rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0); 34468f64d47SJoel Becker if (rc) { 34568f64d47SJoel Becker brelse(tmp); 34668f64d47SJoel Becker goto out; 34768f64d47SJoel Becker } 34868f64d47SJoel Becker 34968f64d47SJoel Becker /* If ocfs2_read_block() got us a new bh, pass it up. */ 35068f64d47SJoel Becker if (!*bh) 35168f64d47SJoel Becker *bh = tmp; 35268f64d47SJoel Becker 35368f64d47SJoel Becker out: 35468f64d47SJoel Becker return rc; 35568f64d47SJoel Becker } 35668f64d47SJoel Becker 357798db35fSJoel Becker static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb, 358798db35fSJoel Becker struct ocfs2_group_desc *bg, 359798db35fSJoel Becker struct ocfs2_chain_list *cl, 360*47dea423STao Ma u64 p_blkno, unsigned int clusters) 361798db35fSJoel Becker { 362798db35fSJoel Becker struct ocfs2_extent_list *el = &bg->bg_list; 363798db35fSJoel Becker struct ocfs2_extent_rec *rec; 364798db35fSJoel Becker 3654711954eSTao Ma BUG_ON(!ocfs2_supports_discontig_bg(osb)); 366798db35fSJoel Becker if (!el->l_next_free_rec) 367798db35fSJoel Becker el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb)); 368798db35fSJoel Becker rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)]; 3694711954eSTao Ma rec->e_blkno = cpu_to_le64(p_blkno); 370798db35fSJoel Becker rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) / 371798db35fSJoel Becker le16_to_cpu(cl->cl_bpc)); 372*47dea423STao Ma rec->e_leaf_clusters = cpu_to_le16(clusters); 373798db35fSJoel Becker le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc)); 3744711954eSTao Ma le16_add_cpu(&bg->bg_free_bits_count, 3754711954eSTao Ma clusters * le16_to_cpu(cl->cl_bpc)); 376798db35fSJoel Becker le16_add_cpu(&el->l_next_free_rec, 1); 377798db35fSJoel Becker } 378798db35fSJoel Becker 3791fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle, 380ccd979bdSMark Fasheh struct inode *alloc_inode, 381ccd979bdSMark Fasheh struct buffer_head *bg_bh, 382ccd979bdSMark Fasheh u64 group_blkno, 383798db35fSJoel Becker unsigned int group_clusters, 384ccd979bdSMark Fasheh u16 my_chain, 385ccd979bdSMark Fasheh struct ocfs2_chain_list *cl) 386ccd979bdSMark Fasheh { 387ccd979bdSMark Fasheh int status = 0; 388798db35fSJoel Becker struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); 389ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 390ccd979bdSMark Fasheh struct super_block * sb = alloc_inode->i_sb; 391ccd979bdSMark Fasheh 392ccd979bdSMark Fasheh mlog_entry_void(); 393ccd979bdSMark Fasheh 394ccd979bdSMark Fasheh if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) { 395b0697053SMark Fasheh ocfs2_error(alloc_inode->i_sb, "group block (%llu) != " 396b0697053SMark Fasheh "b_blocknr (%llu)", 397b0697053SMark Fasheh (unsigned long long)group_blkno, 398ccd979bdSMark Fasheh (unsigned long long) bg_bh->b_blocknr); 399ccd979bdSMark Fasheh status = -EIO; 400ccd979bdSMark Fasheh goto bail; 401ccd979bdSMark Fasheh } 402ccd979bdSMark Fasheh 40313723d00SJoel Becker status = ocfs2_journal_access_gd(handle, 4040cf2f763SJoel Becker INODE_CACHE(alloc_inode), 405ccd979bdSMark Fasheh bg_bh, 406ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_CREATE); 407ccd979bdSMark Fasheh if (status < 0) { 408ccd979bdSMark Fasheh mlog_errno(status); 409ccd979bdSMark Fasheh goto bail; 410ccd979bdSMark Fasheh } 411ccd979bdSMark Fasheh 412ccd979bdSMark Fasheh memset(bg, 0, sb->s_blocksize); 413ccd979bdSMark Fasheh strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE); 414ccd979bdSMark Fasheh bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation); 4158571882cSTao Ma bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1, 4168571882cSTao Ma osb->s_feature_incompat)); 417ccd979bdSMark Fasheh bg->bg_chain = cpu_to_le16(my_chain); 418ccd979bdSMark Fasheh bg->bg_next_group = cl->cl_recs[my_chain].c_blkno; 419ccd979bdSMark Fasheh bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno); 420ccd979bdSMark Fasheh bg->bg_blkno = cpu_to_le64(group_blkno); 421798db35fSJoel Becker if (group_clusters == le16_to_cpu(cl->cl_cpg)) 422798db35fSJoel Becker bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl)); 423798db35fSJoel Becker else 4244711954eSTao Ma ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno, 425798db35fSJoel Becker group_clusters); 426798db35fSJoel Becker 427ccd979bdSMark Fasheh /* set the 1st bit in the bitmap to account for the descriptor block */ 428ccd979bdSMark Fasheh ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap); 429ccd979bdSMark Fasheh bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1); 430ccd979bdSMark Fasheh 431ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bg_bh); 432ccd979bdSMark Fasheh 433ccd979bdSMark Fasheh /* There is no need to zero out or otherwise initialize the 434ccd979bdSMark Fasheh * other blocks in a group - All valid FS metadata in a block 435ccd979bdSMark Fasheh * group stores the superblock fs_generation value at 436ccd979bdSMark Fasheh * allocation time. */ 437ccd979bdSMark Fasheh 438ccd979bdSMark Fasheh bail: 439ccd979bdSMark Fasheh mlog_exit(status); 440ccd979bdSMark Fasheh return status; 441ccd979bdSMark Fasheh } 442ccd979bdSMark Fasheh 443ccd979bdSMark Fasheh static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl) 444ccd979bdSMark Fasheh { 445ccd979bdSMark Fasheh u16 curr, best; 446ccd979bdSMark Fasheh 447ccd979bdSMark Fasheh best = curr = 0; 448ccd979bdSMark Fasheh while (curr < le16_to_cpu(cl->cl_count)) { 449ccd979bdSMark Fasheh if (le32_to_cpu(cl->cl_recs[best].c_total) > 450ccd979bdSMark Fasheh le32_to_cpu(cl->cl_recs[curr].c_total)) 451ccd979bdSMark Fasheh best = curr; 452ccd979bdSMark Fasheh curr++; 453ccd979bdSMark Fasheh } 454ccd979bdSMark Fasheh return best; 455ccd979bdSMark Fasheh } 456ccd979bdSMark Fasheh 457798db35fSJoel Becker static struct buffer_head * 458798db35fSJoel Becker ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle, 459798db35fSJoel Becker struct inode *alloc_inode, 460798db35fSJoel Becker struct ocfs2_alloc_context *ac, 461798db35fSJoel Becker struct ocfs2_chain_list *cl) 462798db35fSJoel Becker { 463798db35fSJoel Becker int status; 464798db35fSJoel Becker u32 bit_off, num_bits; 465798db35fSJoel Becker u64 bg_blkno; 466798db35fSJoel Becker struct buffer_head *bg_bh; 467798db35fSJoel Becker unsigned int alloc_rec = ocfs2_find_smallest_chain(cl); 468798db35fSJoel Becker 4691ed9b777SJoel Becker status = ocfs2_claim_clusters(handle, ac, 470798db35fSJoel Becker le16_to_cpu(cl->cl_cpg), &bit_off, 471798db35fSJoel Becker &num_bits); 472798db35fSJoel Becker if (status < 0) { 473798db35fSJoel Becker if (status != -ENOSPC) 474798db35fSJoel Becker mlog_errno(status); 475798db35fSJoel Becker goto bail; 476798db35fSJoel Becker } 477798db35fSJoel Becker 478798db35fSJoel Becker /* setup the group */ 479798db35fSJoel Becker bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off); 480798db35fSJoel Becker mlog(0, "new descriptor, record %u, at block %llu\n", 481798db35fSJoel Becker alloc_rec, (unsigned long long)bg_blkno); 482798db35fSJoel Becker 483798db35fSJoel Becker bg_bh = sb_getblk(osb->sb, bg_blkno); 484798db35fSJoel Becker if (!bg_bh) { 485798db35fSJoel Becker status = -EIO; 486798db35fSJoel Becker mlog_errno(status); 487798db35fSJoel Becker goto bail; 488798db35fSJoel Becker } 489798db35fSJoel Becker ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh); 490798db35fSJoel Becker 491798db35fSJoel Becker status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh, 492798db35fSJoel Becker bg_blkno, num_bits, alloc_rec, cl); 493798db35fSJoel Becker if (status < 0) { 494798db35fSJoel Becker brelse(bg_bh); 495798db35fSJoel Becker mlog_errno(status); 496798db35fSJoel Becker } 497798db35fSJoel Becker 498798db35fSJoel Becker bail: 499798db35fSJoel Becker return status ? ERR_PTR(status) : bg_bh; 500798db35fSJoel Becker } 501798db35fSJoel Becker 502798db35fSJoel Becker static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb, 503798db35fSJoel Becker handle_t *handle, 504798db35fSJoel Becker struct ocfs2_alloc_context *ac, 505798db35fSJoel Becker unsigned int min_bits, 506798db35fSJoel Becker u32 *bit_off, u32 *num_bits) 507798db35fSJoel Becker { 50818d3a98fSJoel Becker int status = 0; 509798db35fSJoel Becker 510798db35fSJoel Becker while (min_bits) { 5111ed9b777SJoel Becker status = ocfs2_claim_clusters(handle, ac, min_bits, 512798db35fSJoel Becker bit_off, num_bits); 513798db35fSJoel Becker if (status != -ENOSPC) 514798db35fSJoel Becker break; 515798db35fSJoel Becker 516798db35fSJoel Becker min_bits >>= 1; 517798db35fSJoel Becker } 518798db35fSJoel Becker 519798db35fSJoel Becker return status; 520798db35fSJoel Becker } 521798db35fSJoel Becker 522798db35fSJoel Becker static int ocfs2_block_group_grow_discontig(handle_t *handle, 523798db35fSJoel Becker struct inode *alloc_inode, 524798db35fSJoel Becker struct buffer_head *bg_bh, 525798db35fSJoel Becker struct ocfs2_alloc_context *ac, 526798db35fSJoel Becker struct ocfs2_chain_list *cl, 527798db35fSJoel Becker unsigned int min_bits) 528798db35fSJoel Becker { 529798db35fSJoel Becker int status; 530798db35fSJoel Becker struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); 531798db35fSJoel Becker struct ocfs2_group_desc *bg = 532798db35fSJoel Becker (struct ocfs2_group_desc *)bg_bh->b_data; 5334711954eSTao Ma unsigned int needed = le16_to_cpu(cl->cl_cpg) - 5344711954eSTao Ma le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc); 535798db35fSJoel Becker u32 p_cpos, clusters; 536798db35fSJoel Becker u64 p_blkno; 537798db35fSJoel Becker struct ocfs2_extent_list *el = &bg->bg_list; 538798db35fSJoel Becker 539798db35fSJoel Becker status = ocfs2_journal_access_gd(handle, 540798db35fSJoel Becker INODE_CACHE(alloc_inode), 541798db35fSJoel Becker bg_bh, 542798db35fSJoel Becker OCFS2_JOURNAL_ACCESS_CREATE); 543798db35fSJoel Becker if (status < 0) { 544798db35fSJoel Becker mlog_errno(status); 545798db35fSJoel Becker goto bail; 546798db35fSJoel Becker } 547798db35fSJoel Becker 548798db35fSJoel Becker while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) < 549798db35fSJoel Becker le16_to_cpu(el->l_count))) { 550798db35fSJoel Becker if (min_bits > needed) 551798db35fSJoel Becker min_bits = needed; 552798db35fSJoel Becker status = ocfs2_block_group_claim_bits(osb, handle, ac, 553798db35fSJoel Becker min_bits, &p_cpos, 554798db35fSJoel Becker &clusters); 555798db35fSJoel Becker if (status < 0) { 556798db35fSJoel Becker if (status != -ENOSPC) 557798db35fSJoel Becker mlog_errno(status); 558798db35fSJoel Becker goto bail; 559798db35fSJoel Becker } 560798db35fSJoel Becker p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos); 561798db35fSJoel Becker ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno, 562798db35fSJoel Becker clusters); 563798db35fSJoel Becker 564798db35fSJoel Becker min_bits = clusters; 5654711954eSTao Ma needed = le16_to_cpu(cl->cl_cpg) - 5664711954eSTao Ma le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc); 567798db35fSJoel Becker } 568798db35fSJoel Becker 569798db35fSJoel Becker if (needed > 0) { 5704711954eSTao Ma /* 5714711954eSTao Ma * We have used up all the extent rec but can't fill up 5724711954eSTao Ma * the cpg. So bail out. 5734711954eSTao Ma */ 5744711954eSTao Ma status = -ENOSPC; 5754711954eSTao Ma goto bail; 576798db35fSJoel Becker } 577798db35fSJoel Becker 578798db35fSJoel Becker ocfs2_journal_dirty(handle, bg_bh); 579798db35fSJoel Becker 580798db35fSJoel Becker bail: 581798db35fSJoel Becker return status; 582798db35fSJoel Becker } 583798db35fSJoel Becker 5848b06bc59SJoel Becker static void ocfs2_bg_alloc_cleanup(handle_t *handle, 5858b06bc59SJoel Becker struct ocfs2_alloc_context *cluster_ac, 5868b06bc59SJoel Becker struct inode *alloc_inode, 5878b06bc59SJoel Becker struct buffer_head *bg_bh) 588798db35fSJoel Becker { 5898b06bc59SJoel Becker int i, ret; 590798db35fSJoel Becker struct ocfs2_group_desc *bg; 591798db35fSJoel Becker struct ocfs2_extent_list *el; 592798db35fSJoel Becker struct ocfs2_extent_rec *rec; 593798db35fSJoel Becker 594798db35fSJoel Becker if (!bg_bh) 595798db35fSJoel Becker return; 596798db35fSJoel Becker 597798db35fSJoel Becker bg = (struct ocfs2_group_desc *)bg_bh->b_data; 598798db35fSJoel Becker el = &bg->bg_list; 599798db35fSJoel Becker for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { 600798db35fSJoel Becker rec = &el->l_recs[i]; 6018b06bc59SJoel Becker ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode, 6028b06bc59SJoel Becker cluster_ac->ac_bh, 603798db35fSJoel Becker le64_to_cpu(rec->e_blkno), 604798db35fSJoel Becker le32_to_cpu(rec->e_leaf_clusters)); 6058b06bc59SJoel Becker if (ret) 6068b06bc59SJoel Becker mlog_errno(ret); 6078b06bc59SJoel Becker /* Try all the clusters to free */ 608798db35fSJoel Becker } 609798db35fSJoel Becker 610798db35fSJoel Becker ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh); 611798db35fSJoel Becker brelse(bg_bh); 612798db35fSJoel Becker } 613798db35fSJoel Becker 614798db35fSJoel Becker static struct buffer_head * 615798db35fSJoel Becker ocfs2_block_group_alloc_discontig(handle_t *handle, 616798db35fSJoel Becker struct inode *alloc_inode, 617798db35fSJoel Becker struct ocfs2_alloc_context *ac, 6188b06bc59SJoel Becker struct ocfs2_chain_list *cl) 619798db35fSJoel Becker { 620798db35fSJoel Becker int status; 621798db35fSJoel Becker u32 bit_off, num_bits; 622798db35fSJoel Becker u64 bg_blkno; 623798db35fSJoel Becker unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1; 624798db35fSJoel Becker struct buffer_head *bg_bh = NULL; 625798db35fSJoel Becker unsigned int alloc_rec = ocfs2_find_smallest_chain(cl); 626798db35fSJoel Becker struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); 627798db35fSJoel Becker 6284711954eSTao Ma if (!ocfs2_supports_discontig_bg(osb)) { 629798db35fSJoel Becker status = -ENOSPC; 630798db35fSJoel Becker goto bail; 631798db35fSJoel Becker } 632798db35fSJoel Becker 6338b06bc59SJoel Becker status = ocfs2_extend_trans(handle, 6348b06bc59SJoel Becker ocfs2_calc_bg_discontig_credits(osb->sb)); 6358b06bc59SJoel Becker if (status) { 6368b06bc59SJoel Becker mlog_errno(status); 6378b06bc59SJoel Becker goto bail; 6388b06bc59SJoel Becker } 6398b06bc59SJoel Becker 64095ec0adfSJoel Becker /* 64195ec0adfSJoel Becker * We're going to be grabbing from multiple cluster groups. 64295ec0adfSJoel Becker * We don't have enough credits to relink them all, and the 64395ec0adfSJoel Becker * cluster groups will be staying in cache for the duration of 64495ec0adfSJoel Becker * this operation. 64595ec0adfSJoel Becker */ 64695ec0adfSJoel Becker ac->ac_allow_chain_relink = 0; 64795ec0adfSJoel Becker 648798db35fSJoel Becker /* Claim the first region */ 649798db35fSJoel Becker status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits, 650798db35fSJoel Becker &bit_off, &num_bits); 651798db35fSJoel Becker if (status < 0) { 652798db35fSJoel Becker if (status != -ENOSPC) 653798db35fSJoel Becker mlog_errno(status); 654798db35fSJoel Becker goto bail; 655798db35fSJoel Becker } 656798db35fSJoel Becker min_bits = num_bits; 657798db35fSJoel Becker 658798db35fSJoel Becker /* setup the group */ 659798db35fSJoel Becker bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off); 660798db35fSJoel Becker mlog(0, "new descriptor, record %u, at block %llu\n", 661798db35fSJoel Becker alloc_rec, (unsigned long long)bg_blkno); 662798db35fSJoel Becker 663798db35fSJoel Becker bg_bh = sb_getblk(osb->sb, bg_blkno); 664798db35fSJoel Becker if (!bg_bh) { 665798db35fSJoel Becker status = -EIO; 666798db35fSJoel Becker mlog_errno(status); 667798db35fSJoel Becker goto bail; 668798db35fSJoel Becker } 669798db35fSJoel Becker ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh); 670798db35fSJoel Becker 671798db35fSJoel Becker status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh, 672798db35fSJoel Becker bg_blkno, num_bits, alloc_rec, cl); 673798db35fSJoel Becker if (status < 0) { 674798db35fSJoel Becker mlog_errno(status); 675798db35fSJoel Becker goto bail; 676798db35fSJoel Becker } 677798db35fSJoel Becker 678798db35fSJoel Becker status = ocfs2_block_group_grow_discontig(handle, alloc_inode, 679798db35fSJoel Becker bg_bh, ac, cl, min_bits); 680798db35fSJoel Becker if (status) 681798db35fSJoel Becker mlog_errno(status); 682798db35fSJoel Becker 683798db35fSJoel Becker bail: 684798db35fSJoel Becker if (status) 6858b06bc59SJoel Becker ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh); 686798db35fSJoel Becker return status ? ERR_PTR(status) : bg_bh; 687798db35fSJoel Becker } 688798db35fSJoel Becker 689ccd979bdSMark Fasheh /* 690ccd979bdSMark Fasheh * We expect the block group allocator to already be locked. 691ccd979bdSMark Fasheh */ 692ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb, 693ccd979bdSMark Fasheh struct inode *alloc_inode, 6941187c968SJoel Becker struct buffer_head *bh, 69560ca81e8STao Ma u64 max_block, 696feb473a6STao Ma u64 *last_alloc_group, 69760ca81e8STao Ma int flags) 698ccd979bdSMark Fasheh { 699ccd979bdSMark Fasheh int status, credits; 700ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data; 701ccd979bdSMark Fasheh struct ocfs2_chain_list *cl; 702ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac = NULL; 7031fabe148SMark Fasheh handle_t *handle = NULL; 7044711954eSTao Ma u16 alloc_rec; 705ccd979bdSMark Fasheh struct buffer_head *bg_bh = NULL; 706ccd979bdSMark Fasheh struct ocfs2_group_desc *bg; 707ccd979bdSMark Fasheh 708ccd979bdSMark Fasheh BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode)); 709ccd979bdSMark Fasheh 710ccd979bdSMark Fasheh mlog_entry_void(); 711ccd979bdSMark Fasheh 712ccd979bdSMark Fasheh cl = &fe->id2.i_chain; 7131187c968SJoel Becker status = ocfs2_reserve_clusters_with_limit(osb, 714ccd979bdSMark Fasheh le16_to_cpu(cl->cl_cpg), 71560ca81e8STao Ma max_block, flags, &ac); 716ccd979bdSMark Fasheh if (status < 0) { 717ccd979bdSMark Fasheh if (status != -ENOSPC) 718ccd979bdSMark Fasheh mlog_errno(status); 719ccd979bdSMark Fasheh goto bail; 720ccd979bdSMark Fasheh } 721ccd979bdSMark Fasheh 722ccd979bdSMark Fasheh credits = ocfs2_calc_group_alloc_credits(osb->sb, 723ccd979bdSMark Fasheh le16_to_cpu(cl->cl_cpg)); 72465eff9ccSMark Fasheh handle = ocfs2_start_trans(osb, credits); 725ccd979bdSMark Fasheh if (IS_ERR(handle)) { 726ccd979bdSMark Fasheh status = PTR_ERR(handle); 727ccd979bdSMark Fasheh handle = NULL; 728ccd979bdSMark Fasheh mlog_errno(status); 729ccd979bdSMark Fasheh goto bail; 730ccd979bdSMark Fasheh } 731ccd979bdSMark Fasheh 732feb473a6STao Ma if (last_alloc_group && *last_alloc_group != 0) { 733feb473a6STao Ma mlog(0, "use old allocation group %llu for block group alloc\n", 734feb473a6STao Ma (unsigned long long)*last_alloc_group); 735feb473a6STao Ma ac->ac_last_group = *last_alloc_group; 736feb473a6STao Ma } 737798db35fSJoel Becker 738798db35fSJoel Becker bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode, 739798db35fSJoel Becker ac, cl); 740798db35fSJoel Becker if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC)) 741798db35fSJoel Becker bg_bh = ocfs2_block_group_alloc_discontig(handle, 742798db35fSJoel Becker alloc_inode, 7438b06bc59SJoel Becker ac, cl); 744798db35fSJoel Becker if (IS_ERR(bg_bh)) { 745798db35fSJoel Becker status = PTR_ERR(bg_bh); 746798db35fSJoel Becker bg_bh = NULL; 747ccd979bdSMark Fasheh if (status != -ENOSPC) 748ccd979bdSMark Fasheh mlog_errno(status); 749ccd979bdSMark Fasheh goto bail; 750ccd979bdSMark Fasheh } 751ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) bg_bh->b_data; 752ccd979bdSMark Fasheh 7530cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), 754ccd979bdSMark Fasheh bh, OCFS2_JOURNAL_ACCESS_WRITE); 755ccd979bdSMark Fasheh if (status < 0) { 756ccd979bdSMark Fasheh mlog_errno(status); 757ccd979bdSMark Fasheh goto bail; 758ccd979bdSMark Fasheh } 759ccd979bdSMark Fasheh 7604711954eSTao Ma alloc_rec = le16_to_cpu(bg->bg_chain); 7614711954eSTao Ma le32_add_cpu(&cl->cl_recs[alloc_rec].c_free, 762ccd979bdSMark Fasheh le16_to_cpu(bg->bg_free_bits_count)); 7634711954eSTao Ma le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, 764798db35fSJoel Becker le16_to_cpu(bg->bg_bits)); 7650a463b74STao Ma cl->cl_recs[alloc_rec].c_blkno = bg->bg_blkno; 766ccd979bdSMark Fasheh if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count)) 767ccd979bdSMark Fasheh le16_add_cpu(&cl->cl_next_free_rec, 1); 768ccd979bdSMark Fasheh 769ccd979bdSMark Fasheh le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) - 770ccd979bdSMark Fasheh le16_to_cpu(bg->bg_free_bits_count)); 771ccd979bdSMark Fasheh le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits)); 772ccd979bdSMark Fasheh le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg)); 773ccd979bdSMark Fasheh 774ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bh); 775ccd979bdSMark Fasheh 776ccd979bdSMark Fasheh spin_lock(&OCFS2_I(alloc_inode)->ip_lock); 777ccd979bdSMark Fasheh OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters); 778ccd979bdSMark Fasheh fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb, 779ccd979bdSMark Fasheh le32_to_cpu(fe->i_clusters))); 780ccd979bdSMark Fasheh spin_unlock(&OCFS2_I(alloc_inode)->ip_lock); 781ccd979bdSMark Fasheh i_size_write(alloc_inode, le64_to_cpu(fe->i_size)); 7828110b073SMark Fasheh alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode); 783ccd979bdSMark Fasheh 784ccd979bdSMark Fasheh status = 0; 785feb473a6STao Ma 786feb473a6STao Ma /* save the new last alloc group so that the caller can cache it. */ 787feb473a6STao Ma if (last_alloc_group) 788feb473a6STao Ma *last_alloc_group = ac->ac_last_group; 789feb473a6STao Ma 790ccd979bdSMark Fasheh bail: 791ccd979bdSMark Fasheh if (handle) 79202dc1af4SMark Fasheh ocfs2_commit_trans(osb, handle); 793ccd979bdSMark Fasheh 794ccd979bdSMark Fasheh if (ac) 795ccd979bdSMark Fasheh ocfs2_free_alloc_context(ac); 796ccd979bdSMark Fasheh 797ccd979bdSMark Fasheh brelse(bg_bh); 798ccd979bdSMark Fasheh 799ccd979bdSMark Fasheh mlog_exit(status); 800ccd979bdSMark Fasheh return status; 801ccd979bdSMark Fasheh } 802ccd979bdSMark Fasheh 803ccd979bdSMark Fasheh static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb, 804da5cbf2fSMark Fasheh struct ocfs2_alloc_context *ac, 805da5cbf2fSMark Fasheh int type, 806ffda89a3STao Ma u32 slot, 807feb473a6STao Ma u64 *last_alloc_group, 80860ca81e8STao Ma int flags) 809ccd979bdSMark Fasheh { 810ccd979bdSMark Fasheh int status; 811ccd979bdSMark Fasheh u32 bits_wanted = ac->ac_bits_wanted; 812da5cbf2fSMark Fasheh struct inode *alloc_inode; 813ccd979bdSMark Fasheh struct buffer_head *bh = NULL; 814ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 815ccd979bdSMark Fasheh u32 free_bits; 816ccd979bdSMark Fasheh 817ccd979bdSMark Fasheh mlog_entry_void(); 818ccd979bdSMark Fasheh 819da5cbf2fSMark Fasheh alloc_inode = ocfs2_get_system_file_inode(osb, type, slot); 820da5cbf2fSMark Fasheh if (!alloc_inode) { 821da5cbf2fSMark Fasheh mlog_errno(-EINVAL); 822da5cbf2fSMark Fasheh return -EINVAL; 823ccd979bdSMark Fasheh } 824ccd979bdSMark Fasheh 825da5cbf2fSMark Fasheh mutex_lock(&alloc_inode->i_mutex); 826da5cbf2fSMark Fasheh 827e63aecb6SMark Fasheh status = ocfs2_inode_lock(alloc_inode, &bh, 1); 828da5cbf2fSMark Fasheh if (status < 0) { 829da5cbf2fSMark Fasheh mutex_unlock(&alloc_inode->i_mutex); 830da5cbf2fSMark Fasheh iput(alloc_inode); 831da5cbf2fSMark Fasheh 832da5cbf2fSMark Fasheh mlog_errno(status); 833da5cbf2fSMark Fasheh return status; 834da5cbf2fSMark Fasheh } 835da5cbf2fSMark Fasheh 836da5cbf2fSMark Fasheh ac->ac_inode = alloc_inode; 837a4a48911STao Ma ac->ac_alloc_slot = slot; 838da5cbf2fSMark Fasheh 839ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) bh->b_data; 84010995aa2SJoel Becker 84110995aa2SJoel Becker /* The bh was validated by the inode read inside 84210995aa2SJoel Becker * ocfs2_inode_lock(). Any corruption is a code bug. */ 84310995aa2SJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); 84410995aa2SJoel Becker 845ccd979bdSMark Fasheh if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) { 846b0697053SMark Fasheh ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu", 847b0697053SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno)); 848ccd979bdSMark Fasheh status = -EIO; 849ccd979bdSMark Fasheh goto bail; 850ccd979bdSMark Fasheh } 851ccd979bdSMark Fasheh 852ccd979bdSMark Fasheh free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) - 853ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_used); 854ccd979bdSMark Fasheh 855ccd979bdSMark Fasheh if (bits_wanted > free_bits) { 856ccd979bdSMark Fasheh /* cluster bitmap never grows */ 857ccd979bdSMark Fasheh if (ocfs2_is_cluster_bitmap(alloc_inode)) { 858ccd979bdSMark Fasheh mlog(0, "Disk Full: wanted=%u, free_bits=%u\n", 859ccd979bdSMark Fasheh bits_wanted, free_bits); 860ccd979bdSMark Fasheh status = -ENOSPC; 861ccd979bdSMark Fasheh goto bail; 862ccd979bdSMark Fasheh } 863ccd979bdSMark Fasheh 86460ca81e8STao Ma if (!(flags & ALLOC_NEW_GROUP)) { 865ffda89a3STao Ma mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, " 866ffda89a3STao Ma "and we don't alloc a new group for it.\n", 867ffda89a3STao Ma slot, bits_wanted, free_bits); 868ffda89a3STao Ma status = -ENOSPC; 869ffda89a3STao Ma goto bail; 870ffda89a3STao Ma } 871ffda89a3STao Ma 8721187c968SJoel Becker status = ocfs2_block_group_alloc(osb, alloc_inode, bh, 873feb473a6STao Ma ac->ac_max_block, 874feb473a6STao Ma last_alloc_group, flags); 875ccd979bdSMark Fasheh if (status < 0) { 876ccd979bdSMark Fasheh if (status != -ENOSPC) 877ccd979bdSMark Fasheh mlog_errno(status); 878ccd979bdSMark Fasheh goto bail; 879ccd979bdSMark Fasheh } 880ccd979bdSMark Fasheh atomic_inc(&osb->alloc_stats.bg_extends); 881ccd979bdSMark Fasheh 882ccd979bdSMark Fasheh /* You should never ask for this much metadata */ 883ccd979bdSMark Fasheh BUG_ON(bits_wanted > 884ccd979bdSMark Fasheh (le32_to_cpu(fe->id1.bitmap1.i_total) 885ccd979bdSMark Fasheh - le32_to_cpu(fe->id1.bitmap1.i_used))); 886ccd979bdSMark Fasheh } 887ccd979bdSMark Fasheh 888ccd979bdSMark Fasheh get_bh(bh); 889ccd979bdSMark Fasheh ac->ac_bh = bh; 890ccd979bdSMark Fasheh bail: 891ccd979bdSMark Fasheh brelse(bh); 892ccd979bdSMark Fasheh 893ccd979bdSMark Fasheh mlog_exit(status); 894ccd979bdSMark Fasheh return status; 895ccd979bdSMark Fasheh } 896ccd979bdSMark Fasheh 897b89c5428STiger Yang static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb) 898b89c5428STiger Yang { 899b89c5428STiger Yang spin_lock(&osb->osb_lock); 900b89c5428STiger Yang osb->s_inode_steal_slot = OCFS2_INVALID_SLOT; 901b89c5428STiger Yang spin_unlock(&osb->osb_lock); 902b89c5428STiger Yang atomic_set(&osb->s_num_inodes_stolen, 0); 903b89c5428STiger Yang } 904b89c5428STiger Yang 905b89c5428STiger Yang static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb) 906b89c5428STiger Yang { 907b89c5428STiger Yang spin_lock(&osb->osb_lock); 908b89c5428STiger Yang osb->s_meta_steal_slot = OCFS2_INVALID_SLOT; 909b89c5428STiger Yang spin_unlock(&osb->osb_lock); 910b89c5428STiger Yang atomic_set(&osb->s_num_meta_stolen, 0); 911b89c5428STiger Yang } 912b89c5428STiger Yang 913b89c5428STiger Yang void ocfs2_init_steal_slots(struct ocfs2_super *osb) 914b89c5428STiger Yang { 915b89c5428STiger Yang ocfs2_init_inode_steal_slot(osb); 916b89c5428STiger Yang ocfs2_init_meta_steal_slot(osb); 917b89c5428STiger Yang } 918b89c5428STiger Yang 919b89c5428STiger Yang static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type) 920b89c5428STiger Yang { 921b89c5428STiger Yang spin_lock(&osb->osb_lock); 922b89c5428STiger Yang if (type == INODE_ALLOC_SYSTEM_INODE) 923b89c5428STiger Yang osb->s_inode_steal_slot = slot; 924b89c5428STiger Yang else if (type == EXTENT_ALLOC_SYSTEM_INODE) 925b89c5428STiger Yang osb->s_meta_steal_slot = slot; 926b89c5428STiger Yang spin_unlock(&osb->osb_lock); 927b89c5428STiger Yang } 928b89c5428STiger Yang 929b89c5428STiger Yang static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type) 930b89c5428STiger Yang { 931b89c5428STiger Yang int slot = OCFS2_INVALID_SLOT; 932b89c5428STiger Yang 933b89c5428STiger Yang spin_lock(&osb->osb_lock); 934b89c5428STiger Yang if (type == INODE_ALLOC_SYSTEM_INODE) 935b89c5428STiger Yang slot = osb->s_inode_steal_slot; 936b89c5428STiger Yang else if (type == EXTENT_ALLOC_SYSTEM_INODE) 937b89c5428STiger Yang slot = osb->s_meta_steal_slot; 938b89c5428STiger Yang spin_unlock(&osb->osb_lock); 939b89c5428STiger Yang 940b89c5428STiger Yang return slot; 941b89c5428STiger Yang } 942b89c5428STiger Yang 943b89c5428STiger Yang static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb) 944b89c5428STiger Yang { 945b89c5428STiger Yang return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE); 946b89c5428STiger Yang } 947b89c5428STiger Yang 948b89c5428STiger Yang static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb) 949b89c5428STiger Yang { 950b89c5428STiger Yang return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE); 951b89c5428STiger Yang } 952b89c5428STiger Yang 953b89c5428STiger Yang static int ocfs2_steal_resource(struct ocfs2_super *osb, 954b89c5428STiger Yang struct ocfs2_alloc_context *ac, 955b89c5428STiger Yang int type) 956b89c5428STiger Yang { 957b89c5428STiger Yang int i, status = -ENOSPC; 958b89c5428STiger Yang int slot = __ocfs2_get_steal_slot(osb, type); 959b89c5428STiger Yang 960b89c5428STiger Yang /* Start to steal resource from the first slot after ours. */ 961b89c5428STiger Yang if (slot == OCFS2_INVALID_SLOT) 962b89c5428STiger Yang slot = osb->slot_num + 1; 963b89c5428STiger Yang 964b89c5428STiger Yang for (i = 0; i < osb->max_slots; i++, slot++) { 965b89c5428STiger Yang if (slot == osb->max_slots) 966b89c5428STiger Yang slot = 0; 967b89c5428STiger Yang 968b89c5428STiger Yang if (slot == osb->slot_num) 969b89c5428STiger Yang continue; 970b89c5428STiger Yang 971b89c5428STiger Yang status = ocfs2_reserve_suballoc_bits(osb, ac, 972b89c5428STiger Yang type, 973b89c5428STiger Yang (u32)slot, NULL, 974b89c5428STiger Yang NOT_ALLOC_NEW_GROUP); 975b89c5428STiger Yang if (status >= 0) { 976b89c5428STiger Yang __ocfs2_set_steal_slot(osb, slot, type); 977b89c5428STiger Yang break; 978b89c5428STiger Yang } 979b89c5428STiger Yang 980b89c5428STiger Yang ocfs2_free_ac_resource(ac); 981b89c5428STiger Yang } 982b89c5428STiger Yang 983b89c5428STiger Yang return status; 984b89c5428STiger Yang } 985b89c5428STiger Yang 986b89c5428STiger Yang static int ocfs2_steal_inode(struct ocfs2_super *osb, 987b89c5428STiger Yang struct ocfs2_alloc_context *ac) 988b89c5428STiger Yang { 989b89c5428STiger Yang return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE); 990b89c5428STiger Yang } 991b89c5428STiger Yang 992b89c5428STiger Yang static int ocfs2_steal_meta(struct ocfs2_super *osb, 993b89c5428STiger Yang struct ocfs2_alloc_context *ac) 994b89c5428STiger Yang { 995b89c5428STiger Yang return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE); 996b89c5428STiger Yang } 997b89c5428STiger Yang 998cf1d6c76STiger Yang int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb, 999cf1d6c76STiger Yang int blocks, 1000ccd979bdSMark Fasheh struct ocfs2_alloc_context **ac) 1001ccd979bdSMark Fasheh { 1002ccd979bdSMark Fasheh int status; 1003b89c5428STiger Yang int slot = ocfs2_get_meta_steal_slot(osb); 1004ccd979bdSMark Fasheh 1005cd861280SRobert P. J. Day *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); 1006ccd979bdSMark Fasheh if (!(*ac)) { 1007ccd979bdSMark Fasheh status = -ENOMEM; 1008ccd979bdSMark Fasheh mlog_errno(status); 1009ccd979bdSMark Fasheh goto bail; 1010ccd979bdSMark Fasheh } 1011ccd979bdSMark Fasheh 1012cf1d6c76STiger Yang (*ac)->ac_bits_wanted = blocks; 1013ccd979bdSMark Fasheh (*ac)->ac_which = OCFS2_AC_USE_META; 1014ccd979bdSMark Fasheh (*ac)->ac_group_search = ocfs2_block_group_search; 1015ccd979bdSMark Fasheh 1016b89c5428STiger Yang if (slot != OCFS2_INVALID_SLOT && 1017b89c5428STiger Yang atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL) 1018b89c5428STiger Yang goto extent_steal; 1019b89c5428STiger Yang 1020b89c5428STiger Yang atomic_set(&osb->s_num_meta_stolen, 0); 1021da5cbf2fSMark Fasheh status = ocfs2_reserve_suballoc_bits(osb, (*ac), 1022ffda89a3STao Ma EXTENT_ALLOC_SYSTEM_INODE, 1023b89c5428STiger Yang (u32)osb->slot_num, NULL, 102433d5d380SMark Fasheh ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP); 1025b89c5428STiger Yang 1026b89c5428STiger Yang 1027b89c5428STiger Yang if (status >= 0) { 1028b89c5428STiger Yang status = 0; 1029b89c5428STiger Yang if (slot != OCFS2_INVALID_SLOT) 1030b89c5428STiger Yang ocfs2_init_meta_steal_slot(osb); 1031b89c5428STiger Yang goto bail; 1032b89c5428STiger Yang } else if (status < 0 && status != -ENOSPC) { 1033b89c5428STiger Yang mlog_errno(status); 1034b89c5428STiger Yang goto bail; 1035b89c5428STiger Yang } 1036b89c5428STiger Yang 1037b89c5428STiger Yang ocfs2_free_ac_resource(*ac); 1038b89c5428STiger Yang 1039b89c5428STiger Yang extent_steal: 1040b89c5428STiger Yang status = ocfs2_steal_meta(osb, *ac); 1041b89c5428STiger Yang atomic_inc(&osb->s_num_meta_stolen); 1042ccd979bdSMark Fasheh if (status < 0) { 1043ccd979bdSMark Fasheh if (status != -ENOSPC) 1044ccd979bdSMark Fasheh mlog_errno(status); 1045ccd979bdSMark Fasheh goto bail; 1046ccd979bdSMark Fasheh } 1047ccd979bdSMark Fasheh 1048ccd979bdSMark Fasheh status = 0; 1049ccd979bdSMark Fasheh bail: 1050ccd979bdSMark Fasheh if ((status < 0) && *ac) { 1051ccd979bdSMark Fasheh ocfs2_free_alloc_context(*ac); 1052ccd979bdSMark Fasheh *ac = NULL; 1053ccd979bdSMark Fasheh } 1054ccd979bdSMark Fasheh 1055ccd979bdSMark Fasheh mlog_exit(status); 1056ccd979bdSMark Fasheh return status; 1057ccd979bdSMark Fasheh } 1058ccd979bdSMark Fasheh 1059cf1d6c76STiger Yang int ocfs2_reserve_new_metadata(struct ocfs2_super *osb, 1060cf1d6c76STiger Yang struct ocfs2_extent_list *root_el, 1061cf1d6c76STiger Yang struct ocfs2_alloc_context **ac) 1062cf1d6c76STiger Yang { 1063cf1d6c76STiger Yang return ocfs2_reserve_new_metadata_blocks(osb, 1064cf1d6c76STiger Yang ocfs2_extend_meta_needed(root_el), 1065cf1d6c76STiger Yang ac); 1066cf1d6c76STiger Yang } 1067cf1d6c76STiger Yang 1068ccd979bdSMark Fasheh int ocfs2_reserve_new_inode(struct ocfs2_super *osb, 1069ccd979bdSMark Fasheh struct ocfs2_alloc_context **ac) 1070ccd979bdSMark Fasheh { 1071ccd979bdSMark Fasheh int status; 1072b89c5428STiger Yang int slot = ocfs2_get_inode_steal_slot(osb); 1073feb473a6STao Ma u64 alloc_group; 1074ccd979bdSMark Fasheh 1075cd861280SRobert P. J. Day *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); 1076ccd979bdSMark Fasheh if (!(*ac)) { 1077ccd979bdSMark Fasheh status = -ENOMEM; 1078ccd979bdSMark Fasheh mlog_errno(status); 1079ccd979bdSMark Fasheh goto bail; 1080ccd979bdSMark Fasheh } 1081ccd979bdSMark Fasheh 1082ccd979bdSMark Fasheh (*ac)->ac_bits_wanted = 1; 1083ccd979bdSMark Fasheh (*ac)->ac_which = OCFS2_AC_USE_INODE; 1084ccd979bdSMark Fasheh 1085ccd979bdSMark Fasheh (*ac)->ac_group_search = ocfs2_block_group_search; 1086ccd979bdSMark Fasheh 10874d0ddb2cSTao Ma /* 10881187c968SJoel Becker * stat(2) can't handle i_ino > 32bits, so we tell the 10891187c968SJoel Becker * lower levels not to allocate us a block group past that 109012462f1dSJoel Becker * limit. The 'inode64' mount option avoids this behavior. 10911187c968SJoel Becker */ 109212462f1dSJoel Becker if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64)) 10931187c968SJoel Becker (*ac)->ac_max_block = (u32)~0U; 10941187c968SJoel Becker 10951187c968SJoel Becker /* 10964d0ddb2cSTao Ma * slot is set when we successfully steal inode from other nodes. 10974d0ddb2cSTao Ma * It is reset in 3 places: 10984d0ddb2cSTao Ma * 1. when we flush the truncate log 10994d0ddb2cSTao Ma * 2. when we complete local alloc recovery. 11004d0ddb2cSTao Ma * 3. when we successfully allocate from our own slot. 11014d0ddb2cSTao Ma * After it is set, we will go on stealing inodes until we find the 11024d0ddb2cSTao Ma * need to check our slots to see whether there is some space for us. 11034d0ddb2cSTao Ma */ 11044d0ddb2cSTao Ma if (slot != OCFS2_INVALID_SLOT && 1105b89c5428STiger Yang atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL) 11064d0ddb2cSTao Ma goto inode_steal; 11074d0ddb2cSTao Ma 11084d0ddb2cSTao Ma atomic_set(&osb->s_num_inodes_stolen, 0); 1109feb473a6STao Ma alloc_group = osb->osb_inode_alloc_group; 1110da5cbf2fSMark Fasheh status = ocfs2_reserve_suballoc_bits(osb, *ac, 1111da5cbf2fSMark Fasheh INODE_ALLOC_SYSTEM_INODE, 1112b89c5428STiger Yang (u32)osb->slot_num, 1113feb473a6STao Ma &alloc_group, 111460ca81e8STao Ma ALLOC_NEW_GROUP | 111560ca81e8STao Ma ALLOC_GROUPS_FROM_GLOBAL); 11164d0ddb2cSTao Ma if (status >= 0) { 11174d0ddb2cSTao Ma status = 0; 11184d0ddb2cSTao Ma 1119feb473a6STao Ma spin_lock(&osb->osb_lock); 1120feb473a6STao Ma osb->osb_inode_alloc_group = alloc_group; 1121feb473a6STao Ma spin_unlock(&osb->osb_lock); 1122feb473a6STao Ma mlog(0, "after reservation, new allocation group is " 1123feb473a6STao Ma "%llu\n", (unsigned long long)alloc_group); 1124feb473a6STao Ma 11254d0ddb2cSTao Ma /* 11264d0ddb2cSTao Ma * Some inodes must be freed by us, so try to allocate 11274d0ddb2cSTao Ma * from our own next time. 11284d0ddb2cSTao Ma */ 11294d0ddb2cSTao Ma if (slot != OCFS2_INVALID_SLOT) 11304d0ddb2cSTao Ma ocfs2_init_inode_steal_slot(osb); 11314d0ddb2cSTao Ma goto bail; 11324d0ddb2cSTao Ma } else if (status < 0 && status != -ENOSPC) { 11334d0ddb2cSTao Ma mlog_errno(status); 11344d0ddb2cSTao Ma goto bail; 11354d0ddb2cSTao Ma } 11364d0ddb2cSTao Ma 11374d0ddb2cSTao Ma ocfs2_free_ac_resource(*ac); 11384d0ddb2cSTao Ma 11394d0ddb2cSTao Ma inode_steal: 1140b89c5428STiger Yang status = ocfs2_steal_inode(osb, *ac); 11414d0ddb2cSTao Ma atomic_inc(&osb->s_num_inodes_stolen); 1142ccd979bdSMark Fasheh if (status < 0) { 1143ccd979bdSMark Fasheh if (status != -ENOSPC) 1144ccd979bdSMark Fasheh mlog_errno(status); 1145ccd979bdSMark Fasheh goto bail; 1146ccd979bdSMark Fasheh } 1147ccd979bdSMark Fasheh 1148ccd979bdSMark Fasheh status = 0; 1149ccd979bdSMark Fasheh bail: 1150ccd979bdSMark Fasheh if ((status < 0) && *ac) { 1151ccd979bdSMark Fasheh ocfs2_free_alloc_context(*ac); 1152ccd979bdSMark Fasheh *ac = NULL; 1153ccd979bdSMark Fasheh } 1154ccd979bdSMark Fasheh 1155ccd979bdSMark Fasheh mlog_exit(status); 1156ccd979bdSMark Fasheh return status; 1157ccd979bdSMark Fasheh } 1158ccd979bdSMark Fasheh 1159ccd979bdSMark Fasheh /* local alloc code has to do the same thing, so rather than do this 1160ccd979bdSMark Fasheh * twice.. */ 1161ccd979bdSMark Fasheh int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb, 1162ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac) 1163ccd979bdSMark Fasheh { 1164ccd979bdSMark Fasheh int status; 1165ccd979bdSMark Fasheh 1166ccd979bdSMark Fasheh ac->ac_which = OCFS2_AC_USE_MAIN; 1167ccd979bdSMark Fasheh ac->ac_group_search = ocfs2_cluster_group_search; 1168ccd979bdSMark Fasheh 1169da5cbf2fSMark Fasheh status = ocfs2_reserve_suballoc_bits(osb, ac, 1170da5cbf2fSMark Fasheh GLOBAL_BITMAP_SYSTEM_INODE, 1171feb473a6STao Ma OCFS2_INVALID_SLOT, NULL, 1172ffda89a3STao Ma ALLOC_NEW_GROUP); 1173da5cbf2fSMark Fasheh if (status < 0 && status != -ENOSPC) { 1174ccd979bdSMark Fasheh mlog_errno(status); 1175da5cbf2fSMark Fasheh goto bail; 1176da5cbf2fSMark Fasheh } 1177da5cbf2fSMark Fasheh 1178ccd979bdSMark Fasheh bail: 1179ccd979bdSMark Fasheh return status; 1180ccd979bdSMark Fasheh } 1181ccd979bdSMark Fasheh 1182ccd979bdSMark Fasheh /* Callers don't need to care which bitmap (local alloc or main) to 1183ccd979bdSMark Fasheh * use so we figure it out for them, but unfortunately this clutters 1184ccd979bdSMark Fasheh * things a bit. */ 11851187c968SJoel Becker static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb, 11861187c968SJoel Becker u32 bits_wanted, u64 max_block, 118760ca81e8STao Ma int flags, 1188ccd979bdSMark Fasheh struct ocfs2_alloc_context **ac) 1189ccd979bdSMark Fasheh { 1190ccd979bdSMark Fasheh int status; 1191ccd979bdSMark Fasheh 1192ccd979bdSMark Fasheh mlog_entry_void(); 1193ccd979bdSMark Fasheh 1194cd861280SRobert P. J. Day *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); 1195ccd979bdSMark Fasheh if (!(*ac)) { 1196ccd979bdSMark Fasheh status = -ENOMEM; 1197ccd979bdSMark Fasheh mlog_errno(status); 1198ccd979bdSMark Fasheh goto bail; 1199ccd979bdSMark Fasheh } 1200ccd979bdSMark Fasheh 1201ccd979bdSMark Fasheh (*ac)->ac_bits_wanted = bits_wanted; 12021187c968SJoel Becker (*ac)->ac_max_block = max_block; 1203ccd979bdSMark Fasheh 1204ccd979bdSMark Fasheh status = -ENOSPC; 120560ca81e8STao Ma if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) && 120660ca81e8STao Ma ocfs2_alloc_should_use_local(osb, bits_wanted)) { 1207ccd979bdSMark Fasheh status = ocfs2_reserve_local_alloc_bits(osb, 1208ccd979bdSMark Fasheh bits_wanted, 1209ccd979bdSMark Fasheh *ac); 1210a57c8fd2SMark Fasheh if ((status < 0) && (status != -ENOSPC)) { 1211ccd979bdSMark Fasheh mlog_errno(status); 1212ccd979bdSMark Fasheh goto bail; 1213ccd979bdSMark Fasheh } 1214ccd979bdSMark Fasheh } 1215ccd979bdSMark Fasheh 1216ccd979bdSMark Fasheh if (status == -ENOSPC) { 1217ccd979bdSMark Fasheh status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac); 1218ccd979bdSMark Fasheh if (status < 0) { 1219ccd979bdSMark Fasheh if (status != -ENOSPC) 1220ccd979bdSMark Fasheh mlog_errno(status); 1221ccd979bdSMark Fasheh goto bail; 1222ccd979bdSMark Fasheh } 1223ccd979bdSMark Fasheh } 1224ccd979bdSMark Fasheh 1225ccd979bdSMark Fasheh status = 0; 1226ccd979bdSMark Fasheh bail: 1227ccd979bdSMark Fasheh if ((status < 0) && *ac) { 1228ccd979bdSMark Fasheh ocfs2_free_alloc_context(*ac); 1229ccd979bdSMark Fasheh *ac = NULL; 1230ccd979bdSMark Fasheh } 1231ccd979bdSMark Fasheh 1232ccd979bdSMark Fasheh mlog_exit(status); 1233ccd979bdSMark Fasheh return status; 1234ccd979bdSMark Fasheh } 1235ccd979bdSMark Fasheh 12361187c968SJoel Becker int ocfs2_reserve_clusters(struct ocfs2_super *osb, 12371187c968SJoel Becker u32 bits_wanted, 12381187c968SJoel Becker struct ocfs2_alloc_context **ac) 12391187c968SJoel Becker { 124060ca81e8STao Ma return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0, 124160ca81e8STao Ma ALLOC_NEW_GROUP, ac); 12421187c968SJoel Becker } 12431187c968SJoel Becker 1244ccd979bdSMark Fasheh /* 1245ccd979bdSMark Fasheh * More or less lifted from ext3. I'll leave their description below: 1246ccd979bdSMark Fasheh * 1247ccd979bdSMark Fasheh * "For ext3 allocations, we must not reuse any blocks which are 1248ccd979bdSMark Fasheh * allocated in the bitmap buffer's "last committed data" copy. This 1249ccd979bdSMark Fasheh * prevents deletes from freeing up the page for reuse until we have 1250ccd979bdSMark Fasheh * committed the delete transaction. 1251ccd979bdSMark Fasheh * 1252ccd979bdSMark Fasheh * If we didn't do this, then deleting something and reallocating it as 1253ccd979bdSMark Fasheh * data would allow the old block to be overwritten before the 1254ccd979bdSMark Fasheh * transaction committed (because we force data to disk before commit). 1255ccd979bdSMark Fasheh * This would lead to corruption if we crashed between overwriting the 1256ccd979bdSMark Fasheh * data and committing the delete. 1257ccd979bdSMark Fasheh * 1258ccd979bdSMark Fasheh * @@@ We may want to make this allocation behaviour conditional on 1259ccd979bdSMark Fasheh * data-writes at some point, and disable it for metadata allocations or 1260ccd979bdSMark Fasheh * sync-data inodes." 1261ccd979bdSMark Fasheh * 1262ccd979bdSMark Fasheh * Note: OCFS2 already does this differently for metadata vs data 1263c78bad11SJoe Perches * allocations, as those bitmaps are separate and undo access is never 1264ccd979bdSMark Fasheh * called on a metadata group descriptor. 1265ccd979bdSMark Fasheh */ 1266ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh, 1267ccd979bdSMark Fasheh int nr) 1268ccd979bdSMark Fasheh { 1269ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 127094e41ecfSSunil Mushran int ret; 1271ccd979bdSMark Fasheh 1272ccd979bdSMark Fasheh if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap)) 1273ccd979bdSMark Fasheh return 0; 127494e41ecfSSunil Mushran 127594e41ecfSSunil Mushran if (!buffer_jbd(bg_bh)) 1276ccd979bdSMark Fasheh return 1; 1277ccd979bdSMark Fasheh 127894e41ecfSSunil Mushran jbd_lock_bh_state(bg_bh); 1279ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data; 128094e41ecfSSunil Mushran if (bg) 128194e41ecfSSunil Mushran ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap); 128294e41ecfSSunil Mushran else 128394e41ecfSSunil Mushran ret = 1; 128494e41ecfSSunil Mushran jbd_unlock_bh_state(bg_bh); 128594e41ecfSSunil Mushran 128694e41ecfSSunil Mushran return ret; 1287ccd979bdSMark Fasheh } 1288ccd979bdSMark Fasheh 1289ccd979bdSMark Fasheh static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb, 1290ccd979bdSMark Fasheh struct buffer_head *bg_bh, 1291ccd979bdSMark Fasheh unsigned int bits_wanted, 12927bf72edeSMark Fasheh unsigned int total_bits, 12937d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1294ccd979bdSMark Fasheh { 1295ccd979bdSMark Fasheh void *bitmap; 1296ccd979bdSMark Fasheh u16 best_offset, best_size; 1297ccd979bdSMark Fasheh int offset, start, found, status = 0; 1298ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 1299ccd979bdSMark Fasheh 130042035306SJoel Becker /* Callers got this descriptor from 130142035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 130242035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 1303ccd979bdSMark Fasheh 1304ccd979bdSMark Fasheh found = start = best_offset = best_size = 0; 1305ccd979bdSMark Fasheh bitmap = bg->bg_bitmap; 1306ccd979bdSMark Fasheh 13077bf72edeSMark Fasheh while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) { 13087bf72edeSMark Fasheh if (offset == total_bits) 1309ccd979bdSMark Fasheh break; 1310ccd979bdSMark Fasheh 1311ccd979bdSMark Fasheh if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) { 1312ccd979bdSMark Fasheh /* We found a zero, but we can't use it as it 1313ccd979bdSMark Fasheh * hasn't been put to disk yet! */ 1314ccd979bdSMark Fasheh found = 0; 1315ccd979bdSMark Fasheh start = offset + 1; 1316ccd979bdSMark Fasheh } else if (offset == start) { 1317ccd979bdSMark Fasheh /* we found a zero */ 1318ccd979bdSMark Fasheh found++; 1319ccd979bdSMark Fasheh /* move start to the next bit to test */ 1320ccd979bdSMark Fasheh start++; 1321ccd979bdSMark Fasheh } else { 1322ccd979bdSMark Fasheh /* got a zero after some ones */ 1323ccd979bdSMark Fasheh found = 1; 1324ccd979bdSMark Fasheh start = offset + 1; 1325ccd979bdSMark Fasheh } 1326ccd979bdSMark Fasheh if (found > best_size) { 1327ccd979bdSMark Fasheh best_size = found; 1328ccd979bdSMark Fasheh best_offset = start - found; 1329ccd979bdSMark Fasheh } 1330ccd979bdSMark Fasheh /* we got everything we needed */ 1331ccd979bdSMark Fasheh if (found == bits_wanted) { 1332ccd979bdSMark Fasheh /* mlog(0, "Found it all!\n"); */ 1333ccd979bdSMark Fasheh break; 1334ccd979bdSMark Fasheh } 1335ccd979bdSMark Fasheh } 1336ccd979bdSMark Fasheh 13377d1fe093SJoel Becker if (best_size) { 13387d1fe093SJoel Becker res->sr_bit_offset = best_offset; 13397d1fe093SJoel Becker res->sr_bits = best_size; 1340ccd979bdSMark Fasheh } else { 1341ccd979bdSMark Fasheh status = -ENOSPC; 1342ccd979bdSMark Fasheh /* No error log here -- see the comment above 1343ccd979bdSMark Fasheh * ocfs2_test_bg_bit_allocatable */ 1344ccd979bdSMark Fasheh } 1345ccd979bdSMark Fasheh 1346ccd979bdSMark Fasheh return status; 1347ccd979bdSMark Fasheh } 1348ccd979bdSMark Fasheh 13491fabe148SMark Fasheh static inline int ocfs2_block_group_set_bits(handle_t *handle, 1350ccd979bdSMark Fasheh struct inode *alloc_inode, 1351ccd979bdSMark Fasheh struct ocfs2_group_desc *bg, 1352ccd979bdSMark Fasheh struct buffer_head *group_bh, 1353ccd979bdSMark Fasheh unsigned int bit_off, 1354ccd979bdSMark Fasheh unsigned int num_bits) 1355ccd979bdSMark Fasheh { 1356ccd979bdSMark Fasheh int status; 1357ccd979bdSMark Fasheh void *bitmap = bg->bg_bitmap; 1358ccd979bdSMark Fasheh int journal_type = OCFS2_JOURNAL_ACCESS_WRITE; 1359ccd979bdSMark Fasheh 1360ccd979bdSMark Fasheh mlog_entry_void(); 1361ccd979bdSMark Fasheh 136242035306SJoel Becker /* All callers get the descriptor via 136342035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 136442035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 1365ccd979bdSMark Fasheh BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits); 1366ccd979bdSMark Fasheh 1367ccd979bdSMark Fasheh mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off, 1368ccd979bdSMark Fasheh num_bits); 1369ccd979bdSMark Fasheh 1370ccd979bdSMark Fasheh if (ocfs2_is_cluster_bitmap(alloc_inode)) 1371ccd979bdSMark Fasheh journal_type = OCFS2_JOURNAL_ACCESS_UNDO; 1372ccd979bdSMark Fasheh 137313723d00SJoel Becker status = ocfs2_journal_access_gd(handle, 13740cf2f763SJoel Becker INODE_CACHE(alloc_inode), 1375ccd979bdSMark Fasheh group_bh, 1376ccd979bdSMark Fasheh journal_type); 1377ccd979bdSMark Fasheh if (status < 0) { 1378ccd979bdSMark Fasheh mlog_errno(status); 1379ccd979bdSMark Fasheh goto bail; 1380ccd979bdSMark Fasheh } 1381ccd979bdSMark Fasheh 1382ccd979bdSMark Fasheh le16_add_cpu(&bg->bg_free_bits_count, -num_bits); 1383ccd979bdSMark Fasheh while(num_bits--) 1384ccd979bdSMark Fasheh ocfs2_set_bit(bit_off++, bitmap); 1385ccd979bdSMark Fasheh 1386ec20cec7SJoel Becker ocfs2_journal_dirty(handle, group_bh); 1387ccd979bdSMark Fasheh 1388ccd979bdSMark Fasheh bail: 1389ccd979bdSMark Fasheh mlog_exit(status); 1390ccd979bdSMark Fasheh return status; 1391ccd979bdSMark Fasheh } 1392ccd979bdSMark Fasheh 1393ccd979bdSMark Fasheh /* find the one with the most empty bits */ 1394ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl) 1395ccd979bdSMark Fasheh { 1396ccd979bdSMark Fasheh u16 curr, best; 1397ccd979bdSMark Fasheh 1398ccd979bdSMark Fasheh BUG_ON(!cl->cl_next_free_rec); 1399ccd979bdSMark Fasheh 1400ccd979bdSMark Fasheh best = curr = 0; 1401ccd979bdSMark Fasheh while (curr < le16_to_cpu(cl->cl_next_free_rec)) { 1402ccd979bdSMark Fasheh if (le32_to_cpu(cl->cl_recs[curr].c_free) > 1403ccd979bdSMark Fasheh le32_to_cpu(cl->cl_recs[best].c_free)) 1404ccd979bdSMark Fasheh best = curr; 1405ccd979bdSMark Fasheh curr++; 1406ccd979bdSMark Fasheh } 1407ccd979bdSMark Fasheh 1408ccd979bdSMark Fasheh BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec)); 1409ccd979bdSMark Fasheh return best; 1410ccd979bdSMark Fasheh } 1411ccd979bdSMark Fasheh 14121fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle, 1413ccd979bdSMark Fasheh struct inode *alloc_inode, 1414ccd979bdSMark Fasheh struct buffer_head *fe_bh, 1415ccd979bdSMark Fasheh struct buffer_head *bg_bh, 1416ccd979bdSMark Fasheh struct buffer_head *prev_bg_bh, 1417ccd979bdSMark Fasheh u16 chain) 1418ccd979bdSMark Fasheh { 1419ccd979bdSMark Fasheh int status; 1420ccd979bdSMark Fasheh /* there is a really tiny chance the journal calls could fail, 1421ccd979bdSMark Fasheh * but we wouldn't want inconsistent blocks in *any* case. */ 1422ccd979bdSMark Fasheh u64 fe_ptr, bg_ptr, prev_bg_ptr; 1423ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data; 1424ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; 1425ccd979bdSMark Fasheh struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data; 1426ccd979bdSMark Fasheh 142742035306SJoel Becker /* The caller got these descriptors from 142842035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 142942035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 143042035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg)); 1431ccd979bdSMark Fasheh 1432b0697053SMark Fasheh mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n", 14331ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno), chain, 14341ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(bg->bg_blkno), 14351ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(prev_bg->bg_blkno)); 1436ccd979bdSMark Fasheh 1437ccd979bdSMark Fasheh fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno); 1438ccd979bdSMark Fasheh bg_ptr = le64_to_cpu(bg->bg_next_group); 1439ccd979bdSMark Fasheh prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group); 1440ccd979bdSMark Fasheh 14410cf2f763SJoel Becker status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), 14420cf2f763SJoel Becker prev_bg_bh, 1443ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 1444ccd979bdSMark Fasheh if (status < 0) { 1445ccd979bdSMark Fasheh mlog_errno(status); 1446ccd979bdSMark Fasheh goto out_rollback; 1447ccd979bdSMark Fasheh } 1448ccd979bdSMark Fasheh 1449ccd979bdSMark Fasheh prev_bg->bg_next_group = bg->bg_next_group; 1450ec20cec7SJoel Becker ocfs2_journal_dirty(handle, prev_bg_bh); 1451ccd979bdSMark Fasheh 14520cf2f763SJoel Becker status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), 14530cf2f763SJoel Becker bg_bh, OCFS2_JOURNAL_ACCESS_WRITE); 1454ccd979bdSMark Fasheh if (status < 0) { 1455ccd979bdSMark Fasheh mlog_errno(status); 1456ccd979bdSMark Fasheh goto out_rollback; 1457ccd979bdSMark Fasheh } 1458ccd979bdSMark Fasheh 1459ccd979bdSMark Fasheh bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno; 1460ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bg_bh); 1461ccd979bdSMark Fasheh 14620cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), 14630cf2f763SJoel Becker fe_bh, OCFS2_JOURNAL_ACCESS_WRITE); 1464ccd979bdSMark Fasheh if (status < 0) { 1465ccd979bdSMark Fasheh mlog_errno(status); 1466ccd979bdSMark Fasheh goto out_rollback; 1467ccd979bdSMark Fasheh } 1468ccd979bdSMark Fasheh 1469ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno; 1470ec20cec7SJoel Becker ocfs2_journal_dirty(handle, fe_bh); 1471ccd979bdSMark Fasheh 1472ccd979bdSMark Fasheh out_rollback: 1473ccd979bdSMark Fasheh if (status < 0) { 1474ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr); 1475ccd979bdSMark Fasheh bg->bg_next_group = cpu_to_le64(bg_ptr); 1476ccd979bdSMark Fasheh prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr); 1477ccd979bdSMark Fasheh } 147842035306SJoel Becker 1479ccd979bdSMark Fasheh mlog_exit(status); 1480ccd979bdSMark Fasheh return status; 1481ccd979bdSMark Fasheh } 1482ccd979bdSMark Fasheh 1483ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg, 1484ccd979bdSMark Fasheh u32 wanted) 1485ccd979bdSMark Fasheh { 1486ccd979bdSMark Fasheh return le16_to_cpu(bg->bg_free_bits_count) > wanted; 1487ccd979bdSMark Fasheh } 1488ccd979bdSMark Fasheh 1489ccd979bdSMark Fasheh /* return 0 on success, -ENOSPC to keep searching and any other < 0 1490ccd979bdSMark Fasheh * value on error. */ 1491ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode, 1492ccd979bdSMark Fasheh struct buffer_head *group_bh, 1493ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 14941187c968SJoel Becker u64 max_block, 14957d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1496ccd979bdSMark Fasheh { 1497ccd979bdSMark Fasheh int search = -ENOSPC; 1498ccd979bdSMark Fasheh int ret; 14991187c968SJoel Becker u64 blkoff; 15007bf72edeSMark Fasheh struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data; 15019c7af40bSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 15027bf72edeSMark Fasheh unsigned int max_bits, gd_cluster_off; 1503ccd979bdSMark Fasheh 1504ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 1505ccd979bdSMark Fasheh 15067bf72edeSMark Fasheh if (gd->bg_free_bits_count) { 15077bf72edeSMark Fasheh max_bits = le16_to_cpu(gd->bg_bits); 15087bf72edeSMark Fasheh 15097bf72edeSMark Fasheh /* Tail groups in cluster bitmaps which aren't cpg 15107bf72edeSMark Fasheh * aligned are prone to partial extention by a failed 15117bf72edeSMark Fasheh * fs resize. If the file system resize never got to 15127bf72edeSMark Fasheh * update the dinode cluster count, then we don't want 15137bf72edeSMark Fasheh * to trust any clusters past it, regardless of what 15147bf72edeSMark Fasheh * the group descriptor says. */ 15157bf72edeSMark Fasheh gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb, 15167bf72edeSMark Fasheh le64_to_cpu(gd->bg_blkno)); 15177bf72edeSMark Fasheh if ((gd_cluster_off + max_bits) > 15187bf72edeSMark Fasheh OCFS2_I(inode)->ip_clusters) { 15197bf72edeSMark Fasheh max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off; 15207bf72edeSMark Fasheh mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n", 15217bf72edeSMark Fasheh (unsigned long long)le64_to_cpu(gd->bg_blkno), 15227bf72edeSMark Fasheh le16_to_cpu(gd->bg_bits), 15237bf72edeSMark Fasheh OCFS2_I(inode)->ip_clusters, max_bits); 15247bf72edeSMark Fasheh } 15257bf72edeSMark Fasheh 1526ccd979bdSMark Fasheh ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb), 1527ccd979bdSMark Fasheh group_bh, bits_wanted, 15287d1fe093SJoel Becker max_bits, res); 1529ccd979bdSMark Fasheh if (ret) 1530ccd979bdSMark Fasheh return ret; 1531ccd979bdSMark Fasheh 15321187c968SJoel Becker if (max_block) { 15331187c968SJoel Becker blkoff = ocfs2_clusters_to_blocks(inode->i_sb, 15341187c968SJoel Becker gd_cluster_off + 15357d1fe093SJoel Becker res->sr_bit_offset + 15367d1fe093SJoel Becker res->sr_bits); 15371187c968SJoel Becker mlog(0, "Checking %llu against %llu\n", 15381187c968SJoel Becker (unsigned long long)blkoff, 15391187c968SJoel Becker (unsigned long long)max_block); 15401187c968SJoel Becker if (blkoff > max_block) 15411187c968SJoel Becker return -ENOSPC; 15421187c968SJoel Becker } 15431187c968SJoel Becker 1544ccd979bdSMark Fasheh /* ocfs2_block_group_find_clear_bits() might 1545ccd979bdSMark Fasheh * return success, but we still want to return 1546ccd979bdSMark Fasheh * -ENOSPC unless it found the minimum number 1547ccd979bdSMark Fasheh * of bits. */ 15487d1fe093SJoel Becker if (min_bits <= res->sr_bits) 1549ccd979bdSMark Fasheh search = 0; /* success */ 15507d1fe093SJoel Becker else if (res->sr_bits) { 15519c7af40bSMark Fasheh /* 15529c7af40bSMark Fasheh * Don't show bits which we'll be returning 15539c7af40bSMark Fasheh * for allocation to the local alloc bitmap. 15549c7af40bSMark Fasheh */ 15557d1fe093SJoel Becker ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits); 1556ccd979bdSMark Fasheh } 1557ccd979bdSMark Fasheh } 1558ccd979bdSMark Fasheh 1559ccd979bdSMark Fasheh return search; 1560ccd979bdSMark Fasheh } 1561ccd979bdSMark Fasheh 1562ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode, 1563ccd979bdSMark Fasheh struct buffer_head *group_bh, 1564ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits, 15651187c968SJoel Becker u64 max_block, 15667d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1567ccd979bdSMark Fasheh { 1568ccd979bdSMark Fasheh int ret = -ENOSPC; 15691187c968SJoel Becker u64 blkoff; 1570ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data; 1571ccd979bdSMark Fasheh 1572ccd979bdSMark Fasheh BUG_ON(min_bits != 1); 1573ccd979bdSMark Fasheh BUG_ON(ocfs2_is_cluster_bitmap(inode)); 1574ccd979bdSMark Fasheh 15751187c968SJoel Becker if (bg->bg_free_bits_count) { 1576ccd979bdSMark Fasheh ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb), 1577ccd979bdSMark Fasheh group_bh, bits_wanted, 15787bf72edeSMark Fasheh le16_to_cpu(bg->bg_bits), 15797d1fe093SJoel Becker res); 15801187c968SJoel Becker if (!ret && max_block) { 15817d1fe093SJoel Becker blkoff = le64_to_cpu(bg->bg_blkno) + 15827d1fe093SJoel Becker res->sr_bit_offset + res->sr_bits; 15831187c968SJoel Becker mlog(0, "Checking %llu against %llu\n", 15841187c968SJoel Becker (unsigned long long)blkoff, 15851187c968SJoel Becker (unsigned long long)max_block); 15861187c968SJoel Becker if (blkoff > max_block) 15871187c968SJoel Becker ret = -ENOSPC; 15881187c968SJoel Becker } 15891187c968SJoel Becker } 1590ccd979bdSMark Fasheh 1591ccd979bdSMark Fasheh return ret; 1592ccd979bdSMark Fasheh } 1593ccd979bdSMark Fasheh 1594883d4caeSMark Fasheh static int ocfs2_alloc_dinode_update_counts(struct inode *inode, 15951fabe148SMark Fasheh handle_t *handle, 1596883d4caeSMark Fasheh struct buffer_head *di_bh, 1597883d4caeSMark Fasheh u32 num_bits, 1598883d4caeSMark Fasheh u16 chain) 1599883d4caeSMark Fasheh { 1600883d4caeSMark Fasheh int ret; 1601883d4caeSMark Fasheh u32 tmp_used; 1602883d4caeSMark Fasheh struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; 1603883d4caeSMark Fasheh struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain; 1604883d4caeSMark Fasheh 16050cf2f763SJoel Becker ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 1606883d4caeSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 1607883d4caeSMark Fasheh if (ret < 0) { 1608883d4caeSMark Fasheh mlog_errno(ret); 1609883d4caeSMark Fasheh goto out; 1610883d4caeSMark Fasheh } 1611883d4caeSMark Fasheh 1612883d4caeSMark Fasheh tmp_used = le32_to_cpu(di->id1.bitmap1.i_used); 1613883d4caeSMark Fasheh di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used); 1614883d4caeSMark Fasheh le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits); 1615ec20cec7SJoel Becker ocfs2_journal_dirty(handle, di_bh); 1616883d4caeSMark Fasheh 1617883d4caeSMark Fasheh out: 1618883d4caeSMark Fasheh return ret; 1619883d4caeSMark Fasheh } 1620883d4caeSMark Fasheh 1621ba206635SJoel Becker static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res, 162213e434cfSJoel Becker struct ocfs2_extent_rec *rec, 162313e434cfSJoel Becker struct ocfs2_chain_list *cl) 162413e434cfSJoel Becker { 162513e434cfSJoel Becker unsigned int bpc = le16_to_cpu(cl->cl_bpc); 162613e434cfSJoel Becker unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc; 162713e434cfSJoel Becker unsigned int bitcount = le32_to_cpu(rec->e_leaf_clusters) * bpc; 162813e434cfSJoel Becker 162913e434cfSJoel Becker if (res->sr_bit_offset < bitoff) 163013e434cfSJoel Becker return 0; 163113e434cfSJoel Becker if (res->sr_bit_offset >= (bitoff + bitcount)) 163213e434cfSJoel Becker return 0; 1633ba206635SJoel Becker res->sr_blkno = le64_to_cpu(rec->e_blkno) + 1634ba206635SJoel Becker (res->sr_bit_offset - bitoff); 163513e434cfSJoel Becker if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount)) 163613e434cfSJoel Becker res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset; 163713e434cfSJoel Becker return 1; 163813e434cfSJoel Becker } 163913e434cfSJoel Becker 1640ba206635SJoel Becker static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac, 164113e434cfSJoel Becker struct ocfs2_group_desc *bg, 164213e434cfSJoel Becker struct ocfs2_suballoc_result *res) 164313e434cfSJoel Becker { 164413e434cfSJoel Becker int i; 16452b6cb576SJoel Becker u64 bg_blkno = res->sr_bg_blkno; /* Save off */ 164613e434cfSJoel Becker struct ocfs2_extent_rec *rec; 164713e434cfSJoel Becker struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data; 164813e434cfSJoel Becker struct ocfs2_chain_list *cl = &di->id2.i_chain; 164913e434cfSJoel Becker 1650ba206635SJoel Becker if (ocfs2_is_cluster_bitmap(ac->ac_inode)) { 1651ba206635SJoel Becker res->sr_blkno = 0; 165213e434cfSJoel Becker return; 1653ba206635SJoel Becker } 165413e434cfSJoel Becker 1655ba206635SJoel Becker res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset; 16562b6cb576SJoel Becker res->sr_bg_blkno = 0; /* Clear it for contig block groups */ 16574711954eSTao Ma if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) || 1658ba206635SJoel Becker !bg->bg_list.l_next_free_rec) 165913e434cfSJoel Becker return; 166013e434cfSJoel Becker 166113e434cfSJoel Becker for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) { 166213e434cfSJoel Becker rec = &bg->bg_list.l_recs[i]; 16632b6cb576SJoel Becker if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) { 16642b6cb576SJoel Becker res->sr_bg_blkno = bg_blkno; /* Restore */ 166513e434cfSJoel Becker break; 166613e434cfSJoel Becker } 166713e434cfSJoel Becker } 16682b6cb576SJoel Becker } 166913e434cfSJoel Becker 1670883d4caeSMark Fasheh static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac, 16711fabe148SMark Fasheh handle_t *handle, 1672883d4caeSMark Fasheh u32 bits_wanted, 1673883d4caeSMark Fasheh u32 min_bits, 16747d1fe093SJoel Becker struct ocfs2_suballoc_result *res, 1675883d4caeSMark Fasheh u16 *bits_left) 1676883d4caeSMark Fasheh { 1677883d4caeSMark Fasheh int ret; 1678883d4caeSMark Fasheh struct buffer_head *group_bh = NULL; 1679883d4caeSMark Fasheh struct ocfs2_group_desc *gd; 168068f64d47SJoel Becker struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data; 1681883d4caeSMark Fasheh struct inode *alloc_inode = ac->ac_inode; 1682883d4caeSMark Fasheh 16837d1fe093SJoel Becker ret = ocfs2_read_group_descriptor(alloc_inode, di, 16847d1fe093SJoel Becker res->sr_bg_blkno, &group_bh); 1685883d4caeSMark Fasheh if (ret < 0) { 1686883d4caeSMark Fasheh mlog_errno(ret); 1687883d4caeSMark Fasheh return ret; 1688883d4caeSMark Fasheh } 1689883d4caeSMark Fasheh 1690883d4caeSMark Fasheh gd = (struct ocfs2_group_desc *) group_bh->b_data; 1691883d4caeSMark Fasheh ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits, 16927d1fe093SJoel Becker ac->ac_max_block, res); 1693883d4caeSMark Fasheh if (ret < 0) { 1694883d4caeSMark Fasheh if (ret != -ENOSPC) 1695883d4caeSMark Fasheh mlog_errno(ret); 1696883d4caeSMark Fasheh goto out; 1697883d4caeSMark Fasheh } 1698883d4caeSMark Fasheh 169913e434cfSJoel Becker if (!ret) 1700ba206635SJoel Becker ocfs2_bg_discontig_fix_result(ac, gd, res); 170113e434cfSJoel Becker 1702e49e2767SMark Fasheh /* 1703e49e2767SMark Fasheh * sr_bg_blkno might have been changed by 1704e49e2767SMark Fasheh * ocfs2_bg_discontig_fix_result 1705e49e2767SMark Fasheh */ 1706e49e2767SMark Fasheh res->sr_bg_stable_blkno = group_bh->b_blocknr; 1707e49e2767SMark Fasheh 1708e49e2767SMark Fasheh if (ac->ac_find_loc_only) 1709e49e2767SMark Fasheh goto out_loc_only; 1710e49e2767SMark Fasheh 1711883d4caeSMark Fasheh ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh, 17127d1fe093SJoel Becker res->sr_bits, 1713883d4caeSMark Fasheh le16_to_cpu(gd->bg_chain)); 1714883d4caeSMark Fasheh if (ret < 0) { 1715883d4caeSMark Fasheh mlog_errno(ret); 1716883d4caeSMark Fasheh goto out; 1717883d4caeSMark Fasheh } 1718883d4caeSMark Fasheh 1719883d4caeSMark Fasheh ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh, 17207d1fe093SJoel Becker res->sr_bit_offset, res->sr_bits); 1721883d4caeSMark Fasheh if (ret < 0) 1722883d4caeSMark Fasheh mlog_errno(ret); 1723883d4caeSMark Fasheh 1724e49e2767SMark Fasheh out_loc_only: 1725883d4caeSMark Fasheh *bits_left = le16_to_cpu(gd->bg_free_bits_count); 1726883d4caeSMark Fasheh 1727883d4caeSMark Fasheh out: 1728883d4caeSMark Fasheh brelse(group_bh); 1729883d4caeSMark Fasheh 1730883d4caeSMark Fasheh return ret; 1731883d4caeSMark Fasheh } 1732883d4caeSMark Fasheh 1733ccd979bdSMark Fasheh static int ocfs2_search_chain(struct ocfs2_alloc_context *ac, 17341fabe148SMark Fasheh handle_t *handle, 1735ccd979bdSMark Fasheh u32 bits_wanted, 1736ccd979bdSMark Fasheh u32 min_bits, 17377d1fe093SJoel Becker struct ocfs2_suballoc_result *res, 1738883d4caeSMark Fasheh u16 *bits_left) 1739ccd979bdSMark Fasheh { 1740ccd979bdSMark Fasheh int status; 17417d1fe093SJoel Becker u16 chain; 1742ccd979bdSMark Fasheh u64 next_group; 1743ccd979bdSMark Fasheh struct inode *alloc_inode = ac->ac_inode; 1744ccd979bdSMark Fasheh struct buffer_head *group_bh = NULL; 1745ccd979bdSMark Fasheh struct buffer_head *prev_group_bh = NULL; 1746ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data; 1747ccd979bdSMark Fasheh struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain; 1748ccd979bdSMark Fasheh struct ocfs2_group_desc *bg; 1749ccd979bdSMark Fasheh 1750ccd979bdSMark Fasheh chain = ac->ac_chain; 1751b0697053SMark Fasheh mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n", 1752b0697053SMark Fasheh bits_wanted, chain, 1753b0697053SMark Fasheh (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno); 1754ccd979bdSMark Fasheh 175568f64d47SJoel Becker status = ocfs2_read_group_descriptor(alloc_inode, fe, 1756ccd979bdSMark Fasheh le64_to_cpu(cl->cl_recs[chain].c_blkno), 17570fcaa56aSJoel Becker &group_bh); 1758ccd979bdSMark Fasheh if (status < 0) { 1759ccd979bdSMark Fasheh mlog_errno(status); 1760ccd979bdSMark Fasheh goto bail; 1761ccd979bdSMark Fasheh } 1762ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) group_bh->b_data; 1763ccd979bdSMark Fasheh 1764ccd979bdSMark Fasheh status = -ENOSPC; 1765ccd979bdSMark Fasheh /* for now, the chain search is a bit simplistic. We just use 1766ccd979bdSMark Fasheh * the 1st group with any empty bits. */ 17671187c968SJoel Becker while ((status = ac->ac_group_search(alloc_inode, group_bh, 17681187c968SJoel Becker bits_wanted, min_bits, 17697d1fe093SJoel Becker ac->ac_max_block, 17707d1fe093SJoel Becker res)) == -ENOSPC) { 1771ccd979bdSMark Fasheh if (!bg->bg_next_group) 1772ccd979bdSMark Fasheh break; 1773a81cb88bSMark Fasheh 1774ccd979bdSMark Fasheh brelse(prev_group_bh); 1775ccd979bdSMark Fasheh prev_group_bh = NULL; 1776a81cb88bSMark Fasheh 1777ccd979bdSMark Fasheh next_group = le64_to_cpu(bg->bg_next_group); 1778ccd979bdSMark Fasheh prev_group_bh = group_bh; 1779ccd979bdSMark Fasheh group_bh = NULL; 178068f64d47SJoel Becker status = ocfs2_read_group_descriptor(alloc_inode, fe, 17810fcaa56aSJoel Becker next_group, &group_bh); 1782ccd979bdSMark Fasheh if (status < 0) { 1783ccd979bdSMark Fasheh mlog_errno(status); 1784ccd979bdSMark Fasheh goto bail; 1785ccd979bdSMark Fasheh } 1786ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) group_bh->b_data; 1787ccd979bdSMark Fasheh } 1788ccd979bdSMark Fasheh if (status < 0) { 1789ccd979bdSMark Fasheh if (status != -ENOSPC) 1790ccd979bdSMark Fasheh mlog_errno(status); 1791ccd979bdSMark Fasheh goto bail; 1792ccd979bdSMark Fasheh } 1793ccd979bdSMark Fasheh 1794b0697053SMark Fasheh mlog(0, "alloc succeeds: we give %u bits from block group %llu\n", 17957d1fe093SJoel Becker res->sr_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno)); 1796ccd979bdSMark Fasheh 17977d1fe093SJoel Becker res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno); 1798ccd979bdSMark Fasheh 17997d1fe093SJoel Becker BUG_ON(res->sr_bits == 0); 180013e434cfSJoel Becker if (!status) 1801ba206635SJoel Becker ocfs2_bg_discontig_fix_result(ac, bg, res); 180213e434cfSJoel Becker 1803e49e2767SMark Fasheh /* 1804e49e2767SMark Fasheh * sr_bg_blkno might have been changed by 1805e49e2767SMark Fasheh * ocfs2_bg_discontig_fix_result 1806e49e2767SMark Fasheh */ 1807e49e2767SMark Fasheh res->sr_bg_stable_blkno = group_bh->b_blocknr; 1808ccd979bdSMark Fasheh 1809ccd979bdSMark Fasheh /* 1810ccd979bdSMark Fasheh * Keep track of previous block descriptor read. When 1811ccd979bdSMark Fasheh * we find a target, if we have read more than X 1812ccd979bdSMark Fasheh * number of descriptors, and the target is reasonably 1813ccd979bdSMark Fasheh * empty, relink him to top of his chain. 1814ccd979bdSMark Fasheh * 1815ccd979bdSMark Fasheh * We've read 0 extra blocks and only send one more to 1816ccd979bdSMark Fasheh * the transaction, yet the next guy to search has a 1817ccd979bdSMark Fasheh * much easier time. 1818ccd979bdSMark Fasheh * 1819ccd979bdSMark Fasheh * Do this *after* figuring out how many bits we're taking out 1820ccd979bdSMark Fasheh * of our target group. 1821ccd979bdSMark Fasheh */ 1822ccd979bdSMark Fasheh if (ac->ac_allow_chain_relink && 1823ccd979bdSMark Fasheh (prev_group_bh) && 18247d1fe093SJoel Becker (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) { 1825ccd979bdSMark Fasheh status = ocfs2_relink_block_group(handle, alloc_inode, 1826ccd979bdSMark Fasheh ac->ac_bh, group_bh, 1827ccd979bdSMark Fasheh prev_group_bh, chain); 1828ccd979bdSMark Fasheh if (status < 0) { 1829ccd979bdSMark Fasheh mlog_errno(status); 1830ccd979bdSMark Fasheh goto bail; 1831ccd979bdSMark Fasheh } 1832ccd979bdSMark Fasheh } 1833ccd979bdSMark Fasheh 1834e49e2767SMark Fasheh if (ac->ac_find_loc_only) 1835e49e2767SMark Fasheh goto out_loc_only; 1836e49e2767SMark Fasheh 1837d5134982SMark Fasheh status = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, 1838d5134982SMark Fasheh ac->ac_bh, res->sr_bits, 1839d5134982SMark Fasheh chain); 1840d5134982SMark Fasheh if (status) { 1841ccd979bdSMark Fasheh mlog_errno(status); 1842ccd979bdSMark Fasheh goto bail; 1843ccd979bdSMark Fasheh } 1844ccd979bdSMark Fasheh 1845ccd979bdSMark Fasheh status = ocfs2_block_group_set_bits(handle, 1846ccd979bdSMark Fasheh alloc_inode, 1847ccd979bdSMark Fasheh bg, 1848ccd979bdSMark Fasheh group_bh, 18497d1fe093SJoel Becker res->sr_bit_offset, 18507d1fe093SJoel Becker res->sr_bits); 1851ccd979bdSMark Fasheh if (status < 0) { 1852ccd979bdSMark Fasheh mlog_errno(status); 1853ccd979bdSMark Fasheh goto bail; 1854ccd979bdSMark Fasheh } 1855ccd979bdSMark Fasheh 18567d1fe093SJoel Becker mlog(0, "Allocated %u bits from suballocator %llu\n", res->sr_bits, 18571ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno)); 1858ccd979bdSMark Fasheh 1859e49e2767SMark Fasheh out_loc_only: 1860883d4caeSMark Fasheh *bits_left = le16_to_cpu(bg->bg_free_bits_count); 1861ccd979bdSMark Fasheh bail: 1862ccd979bdSMark Fasheh brelse(group_bh); 1863ccd979bdSMark Fasheh brelse(prev_group_bh); 1864ccd979bdSMark Fasheh 1865ccd979bdSMark Fasheh mlog_exit(status); 1866ccd979bdSMark Fasheh return status; 1867ccd979bdSMark Fasheh } 1868ccd979bdSMark Fasheh 1869ccd979bdSMark Fasheh /* will give out up to bits_wanted contiguous bits. */ 1870aa8f8e93SJoel Becker static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac, 18711fabe148SMark Fasheh handle_t *handle, 1872ccd979bdSMark Fasheh u32 bits_wanted, 1873ccd979bdSMark Fasheh u32 min_bits, 18747d1fe093SJoel Becker struct ocfs2_suballoc_result *res) 1875ccd979bdSMark Fasheh { 1876ccd979bdSMark Fasheh int status; 1877ccd979bdSMark Fasheh u16 victim, i; 1878883d4caeSMark Fasheh u16 bits_left = 0; 1879b2b6ebf5SMark Fasheh u64 hint = ac->ac_last_group; 1880ccd979bdSMark Fasheh struct ocfs2_chain_list *cl; 1881ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 1882ccd979bdSMark Fasheh 1883ccd979bdSMark Fasheh mlog_entry_void(); 1884ccd979bdSMark Fasheh 1885ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted); 1886ccd979bdSMark Fasheh BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given)); 1887ccd979bdSMark Fasheh BUG_ON(!ac->ac_bh); 1888ccd979bdSMark Fasheh 1889ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) ac->ac_bh->b_data; 189010995aa2SJoel Becker 189110995aa2SJoel Becker /* The bh was validated by the inode read during 189210995aa2SJoel Becker * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */ 189310995aa2SJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); 189410995aa2SJoel Becker 1895ccd979bdSMark Fasheh if (le32_to_cpu(fe->id1.bitmap1.i_used) >= 1896ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_total)) { 1897aa8f8e93SJoel Becker ocfs2_error(ac->ac_inode->i_sb, 1898aa8f8e93SJoel Becker "Chain allocator dinode %llu has %u used " 1899b0697053SMark Fasheh "bits but only %u total.", 1900b0697053SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno), 1901ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_used), 1902ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_total)); 1903ccd979bdSMark Fasheh status = -EIO; 1904ccd979bdSMark Fasheh goto bail; 1905ccd979bdSMark Fasheh } 1906ccd979bdSMark Fasheh 1907b2b6ebf5SMark Fasheh res->sr_bg_blkno = hint; 19087d1fe093SJoel Becker if (res->sr_bg_blkno) { 1909883d4caeSMark Fasheh /* Attempt to short-circuit the usual search mechanism 1910883d4caeSMark Fasheh * by jumping straight to the most recently used 1911883d4caeSMark Fasheh * allocation group. This helps us mantain some 1912883d4caeSMark Fasheh * contiguousness across allocations. */ 1913da5cbf2fSMark Fasheh status = ocfs2_search_one_group(ac, handle, bits_wanted, 19147d1fe093SJoel Becker min_bits, res, &bits_left); 19157d1fe093SJoel Becker if (!status) 1916883d4caeSMark Fasheh goto set_hint; 1917883d4caeSMark Fasheh if (status < 0 && status != -ENOSPC) { 1918883d4caeSMark Fasheh mlog_errno(status); 1919883d4caeSMark Fasheh goto bail; 1920883d4caeSMark Fasheh } 1921883d4caeSMark Fasheh } 1922883d4caeSMark Fasheh 1923ccd979bdSMark Fasheh cl = (struct ocfs2_chain_list *) &fe->id2.i_chain; 1924ccd979bdSMark Fasheh 1925ccd979bdSMark Fasheh victim = ocfs2_find_victim_chain(cl); 1926ccd979bdSMark Fasheh ac->ac_chain = victim; 1927ccd979bdSMark Fasheh ac->ac_allow_chain_relink = 1; 1928ccd979bdSMark Fasheh 19297d1fe093SJoel Becker status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, 19307d1fe093SJoel Becker res, &bits_left); 1931b2b6ebf5SMark Fasheh if (!status) { 1932b2b6ebf5SMark Fasheh hint = ocfs2_group_from_res(res); 1933883d4caeSMark Fasheh goto set_hint; 1934b2b6ebf5SMark Fasheh } 1935ccd979bdSMark Fasheh if (status < 0 && status != -ENOSPC) { 1936ccd979bdSMark Fasheh mlog_errno(status); 1937ccd979bdSMark Fasheh goto bail; 1938ccd979bdSMark Fasheh } 1939ccd979bdSMark Fasheh 1940ccd979bdSMark Fasheh mlog(0, "Search of victim chain %u came up with nothing, " 1941ccd979bdSMark Fasheh "trying all chains now.\n", victim); 1942ccd979bdSMark Fasheh 1943ccd979bdSMark Fasheh /* If we didn't pick a good victim, then just default to 1944ccd979bdSMark Fasheh * searching each chain in order. Don't allow chain relinking 1945ccd979bdSMark Fasheh * because we only calculate enough journal credits for one 1946ccd979bdSMark Fasheh * relink per alloc. */ 1947ccd979bdSMark Fasheh ac->ac_allow_chain_relink = 0; 1948ccd979bdSMark Fasheh for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) { 1949ccd979bdSMark Fasheh if (i == victim) 1950ccd979bdSMark Fasheh continue; 1951ccd979bdSMark Fasheh if (!cl->cl_recs[i].c_free) 1952ccd979bdSMark Fasheh continue; 1953ccd979bdSMark Fasheh 1954ccd979bdSMark Fasheh ac->ac_chain = i; 1955da5cbf2fSMark Fasheh status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, 19567d1fe093SJoel Becker res, &bits_left); 1957b2b6ebf5SMark Fasheh if (!status) { 1958b2b6ebf5SMark Fasheh hint = ocfs2_group_from_res(res); 1959ccd979bdSMark Fasheh break; 1960b2b6ebf5SMark Fasheh } 1961ccd979bdSMark Fasheh if (status < 0 && status != -ENOSPC) { 1962ccd979bdSMark Fasheh mlog_errno(status); 1963ccd979bdSMark Fasheh goto bail; 1964ccd979bdSMark Fasheh } 1965ccd979bdSMark Fasheh } 1966ccd979bdSMark Fasheh 1967883d4caeSMark Fasheh set_hint: 1968883d4caeSMark Fasheh if (status != -ENOSPC) { 1969883d4caeSMark Fasheh /* If the next search of this group is not likely to 1970883d4caeSMark Fasheh * yield a suitable extent, then we reset the last 1971883d4caeSMark Fasheh * group hint so as to not waste a disk read */ 1972883d4caeSMark Fasheh if (bits_left < min_bits) 1973883d4caeSMark Fasheh ac->ac_last_group = 0; 1974883d4caeSMark Fasheh else 1975b2b6ebf5SMark Fasheh ac->ac_last_group = hint; 1976883d4caeSMark Fasheh } 1977883d4caeSMark Fasheh 1978883d4caeSMark Fasheh bail: 1979ccd979bdSMark Fasheh mlog_exit(status); 1980ccd979bdSMark Fasheh return status; 1981ccd979bdSMark Fasheh } 1982ccd979bdSMark Fasheh 19831ed9b777SJoel Becker int ocfs2_claim_metadata(handle_t *handle, 1984ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac, 1985ccd979bdSMark Fasheh u32 bits_wanted, 19862b6cb576SJoel Becker u64 *suballoc_loc, 1987ccd979bdSMark Fasheh u16 *suballoc_bit_start, 1988ccd979bdSMark Fasheh unsigned int *num_bits, 1989ccd979bdSMark Fasheh u64 *blkno_start) 1990ccd979bdSMark Fasheh { 1991ccd979bdSMark Fasheh int status; 1992ba206635SJoel Becker struct ocfs2_suballoc_result res = { .sr_blkno = 0, }; 1993ccd979bdSMark Fasheh 1994ccd979bdSMark Fasheh BUG_ON(!ac); 1995ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted)); 1996ccd979bdSMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_META); 1997ccd979bdSMark Fasheh 1998aa8f8e93SJoel Becker status = ocfs2_claim_suballoc_bits(ac, 1999da5cbf2fSMark Fasheh handle, 2000ccd979bdSMark Fasheh bits_wanted, 2001ccd979bdSMark Fasheh 1, 20027d1fe093SJoel Becker &res); 2003ccd979bdSMark Fasheh if (status < 0) { 2004ccd979bdSMark Fasheh mlog_errno(status); 2005ccd979bdSMark Fasheh goto bail; 2006ccd979bdSMark Fasheh } 20071ed9b777SJoel Becker atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs); 2008ccd979bdSMark Fasheh 20092b6cb576SJoel Becker *suballoc_loc = res.sr_bg_blkno; 20107d1fe093SJoel Becker *suballoc_bit_start = res.sr_bit_offset; 2011ba206635SJoel Becker *blkno_start = res.sr_blkno; 20127d1fe093SJoel Becker ac->ac_bits_given += res.sr_bits; 20137d1fe093SJoel Becker *num_bits = res.sr_bits; 2014ccd979bdSMark Fasheh status = 0; 2015ccd979bdSMark Fasheh bail: 2016ccd979bdSMark Fasheh mlog_exit(status); 2017ccd979bdSMark Fasheh return status; 2018ccd979bdSMark Fasheh } 2019ccd979bdSMark Fasheh 202013821151STao Ma static void ocfs2_init_inode_ac_group(struct inode *dir, 2021abf1b3cbSTao Ma struct buffer_head *parent_di_bh, 202213821151STao Ma struct ocfs2_alloc_context *ac) 202313821151STao Ma { 2024abf1b3cbSTao Ma struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data; 202513821151STao Ma /* 202613821151STao Ma * Try to allocate inodes from some specific group. 202713821151STao Ma * 202813821151STao Ma * If the parent dir has recorded the last group used in allocation, 202913821151STao Ma * cool, use it. Otherwise if we try to allocate new inode from the 203013821151STao Ma * same slot the parent dir belongs to, use the same chunk. 203113821151STao Ma * 203213821151STao Ma * We are very careful here to avoid the mistake of setting 203313821151STao Ma * ac_last_group to a group descriptor from a different (unlocked) slot. 203413821151STao Ma */ 203513821151STao Ma if (OCFS2_I(dir)->ip_last_used_group && 203613821151STao Ma OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot) 203713821151STao Ma ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group; 2038abf1b3cbSTao Ma else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) { 2039abf1b3cbSTao Ma if (di->i_suballoc_loc) 2040abf1b3cbSTao Ma ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc); 2041abf1b3cbSTao Ma else 204213821151STao Ma ac->ac_last_group = ocfs2_which_suballoc_group( 2043abf1b3cbSTao Ma le64_to_cpu(di->i_blkno), 2044abf1b3cbSTao Ma le16_to_cpu(di->i_suballoc_bit)); 2045abf1b3cbSTao Ma } 204613821151STao Ma } 204713821151STao Ma 204813821151STao Ma static inline void ocfs2_save_inode_ac_group(struct inode *dir, 204913821151STao Ma struct ocfs2_alloc_context *ac) 205013821151STao Ma { 205113821151STao Ma OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group; 205213821151STao Ma OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot; 205313821151STao Ma } 205413821151STao Ma 2055e49e2767SMark Fasheh int ocfs2_find_new_inode_loc(struct inode *dir, 2056e49e2767SMark Fasheh struct buffer_head *parent_fe_bh, 2057e49e2767SMark Fasheh struct ocfs2_alloc_context *ac, 2058e49e2767SMark Fasheh u64 *fe_blkno) 2059e49e2767SMark Fasheh { 2060e49e2767SMark Fasheh int ret; 2061e49e2767SMark Fasheh handle_t *handle = NULL; 2062e49e2767SMark Fasheh struct ocfs2_suballoc_result *res; 2063e49e2767SMark Fasheh 2064e49e2767SMark Fasheh BUG_ON(!ac); 2065e49e2767SMark Fasheh BUG_ON(ac->ac_bits_given != 0); 2066e49e2767SMark Fasheh BUG_ON(ac->ac_bits_wanted != 1); 2067e49e2767SMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE); 2068e49e2767SMark Fasheh 2069e49e2767SMark Fasheh res = kzalloc(sizeof(*res), GFP_NOFS); 2070e49e2767SMark Fasheh if (res == NULL) { 2071e49e2767SMark Fasheh ret = -ENOMEM; 2072e49e2767SMark Fasheh mlog_errno(ret); 2073e49e2767SMark Fasheh goto out; 2074e49e2767SMark Fasheh } 2075e49e2767SMark Fasheh 2076e49e2767SMark Fasheh ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac); 2077e49e2767SMark Fasheh 2078e49e2767SMark Fasheh /* 2079e49e2767SMark Fasheh * The handle started here is for chain relink. Alternatively, 2080e49e2767SMark Fasheh * we could just disable relink for these calls. 2081e49e2767SMark Fasheh */ 2082e49e2767SMark Fasheh handle = ocfs2_start_trans(OCFS2_SB(dir->i_sb), OCFS2_SUBALLOC_ALLOC); 2083e49e2767SMark Fasheh if (IS_ERR(handle)) { 2084e49e2767SMark Fasheh ret = PTR_ERR(handle); 2085e49e2767SMark Fasheh handle = NULL; 2086e49e2767SMark Fasheh mlog_errno(ret); 2087e49e2767SMark Fasheh goto out; 2088e49e2767SMark Fasheh } 2089e49e2767SMark Fasheh 2090e49e2767SMark Fasheh /* 2091e49e2767SMark Fasheh * This will instruct ocfs2_claim_suballoc_bits and 2092e49e2767SMark Fasheh * ocfs2_search_one_group to search but save actual allocation 2093e49e2767SMark Fasheh * for later. 2094e49e2767SMark Fasheh */ 2095e49e2767SMark Fasheh ac->ac_find_loc_only = 1; 2096e49e2767SMark Fasheh 2097e49e2767SMark Fasheh ret = ocfs2_claim_suballoc_bits(ac, handle, 1, 1, res); 2098e49e2767SMark Fasheh if (ret < 0) { 2099e49e2767SMark Fasheh mlog_errno(ret); 2100e49e2767SMark Fasheh goto out; 2101e49e2767SMark Fasheh } 2102e49e2767SMark Fasheh 2103e49e2767SMark Fasheh ac->ac_find_loc_priv = res; 2104e49e2767SMark Fasheh *fe_blkno = res->sr_blkno; 2105e49e2767SMark Fasheh 2106e49e2767SMark Fasheh out: 2107e49e2767SMark Fasheh if (handle) 2108e49e2767SMark Fasheh ocfs2_commit_trans(OCFS2_SB(dir->i_sb), handle); 2109e49e2767SMark Fasheh 2110e49e2767SMark Fasheh if (ret) 2111e49e2767SMark Fasheh kfree(res); 2112e49e2767SMark Fasheh 2113e49e2767SMark Fasheh return ret; 2114e49e2767SMark Fasheh } 2115e49e2767SMark Fasheh 2116e49e2767SMark Fasheh int ocfs2_claim_new_inode_at_loc(handle_t *handle, 2117e49e2767SMark Fasheh struct inode *dir, 2118e49e2767SMark Fasheh struct ocfs2_alloc_context *ac, 2119e49e2767SMark Fasheh u64 *suballoc_loc, 2120e49e2767SMark Fasheh u16 *suballoc_bit, 2121e49e2767SMark Fasheh u64 di_blkno) 2122e49e2767SMark Fasheh { 2123e49e2767SMark Fasheh int ret; 2124e49e2767SMark Fasheh u16 chain; 2125e49e2767SMark Fasheh struct ocfs2_suballoc_result *res = ac->ac_find_loc_priv; 2126e49e2767SMark Fasheh struct buffer_head *bg_bh = NULL; 2127e49e2767SMark Fasheh struct ocfs2_group_desc *bg; 2128e49e2767SMark Fasheh struct ocfs2_dinode *di = (struct ocfs2_dinode *) ac->ac_bh->b_data; 2129e49e2767SMark Fasheh 2130e49e2767SMark Fasheh /* 2131e49e2767SMark Fasheh * Since di_blkno is being passed back in, we check for any 2132e49e2767SMark Fasheh * inconsistencies which may have happened between 2133e49e2767SMark Fasheh * calls. These are code bugs as di_blkno is not expected to 2134e49e2767SMark Fasheh * change once returned from ocfs2_find_new_inode_loc() 2135e49e2767SMark Fasheh */ 2136e49e2767SMark Fasheh BUG_ON(res->sr_blkno != di_blkno); 2137e49e2767SMark Fasheh 2138e49e2767SMark Fasheh ret = ocfs2_read_group_descriptor(ac->ac_inode, di, 2139e49e2767SMark Fasheh res->sr_bg_stable_blkno, &bg_bh); 2140e49e2767SMark Fasheh if (ret) { 2141e49e2767SMark Fasheh mlog_errno(ret); 2142e49e2767SMark Fasheh goto out; 2143e49e2767SMark Fasheh } 2144e49e2767SMark Fasheh 2145e49e2767SMark Fasheh bg = (struct ocfs2_group_desc *) bg_bh->b_data; 2146e49e2767SMark Fasheh chain = le16_to_cpu(bg->bg_chain); 2147e49e2767SMark Fasheh 2148e49e2767SMark Fasheh ret = ocfs2_alloc_dinode_update_counts(ac->ac_inode, handle, 2149e49e2767SMark Fasheh ac->ac_bh, res->sr_bits, 2150e49e2767SMark Fasheh chain); 2151e49e2767SMark Fasheh if (ret) { 2152e49e2767SMark Fasheh mlog_errno(ret); 2153e49e2767SMark Fasheh goto out; 2154e49e2767SMark Fasheh } 2155e49e2767SMark Fasheh 2156e49e2767SMark Fasheh ret = ocfs2_block_group_set_bits(handle, 2157e49e2767SMark Fasheh ac->ac_inode, 2158e49e2767SMark Fasheh bg, 2159e49e2767SMark Fasheh bg_bh, 2160e49e2767SMark Fasheh res->sr_bit_offset, 2161e49e2767SMark Fasheh res->sr_bits); 2162e49e2767SMark Fasheh if (ret < 0) { 2163e49e2767SMark Fasheh mlog_errno(ret); 2164e49e2767SMark Fasheh goto out; 2165e49e2767SMark Fasheh } 2166e49e2767SMark Fasheh 2167e49e2767SMark Fasheh mlog(0, "Allocated %u bits from suballocator %llu\n", res->sr_bits, 2168e49e2767SMark Fasheh (unsigned long long)di_blkno); 2169e49e2767SMark Fasheh 2170e49e2767SMark Fasheh atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs); 2171e49e2767SMark Fasheh 2172e49e2767SMark Fasheh BUG_ON(res->sr_bits != 1); 2173e49e2767SMark Fasheh 2174e49e2767SMark Fasheh *suballoc_loc = res->sr_bg_blkno; 2175e49e2767SMark Fasheh *suballoc_bit = res->sr_bit_offset; 2176e49e2767SMark Fasheh ac->ac_bits_given++; 2177e49e2767SMark Fasheh ocfs2_save_inode_ac_group(dir, ac); 2178e49e2767SMark Fasheh 2179e49e2767SMark Fasheh out: 2180e49e2767SMark Fasheh brelse(bg_bh); 2181e49e2767SMark Fasheh 2182e49e2767SMark Fasheh return ret; 2183e49e2767SMark Fasheh } 2184e49e2767SMark Fasheh 21851ed9b777SJoel Becker int ocfs2_claim_new_inode(handle_t *handle, 218613821151STao Ma struct inode *dir, 218713821151STao Ma struct buffer_head *parent_fe_bh, 2188ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac, 21892b6cb576SJoel Becker u64 *suballoc_loc, 2190ccd979bdSMark Fasheh u16 *suballoc_bit, 2191ccd979bdSMark Fasheh u64 *fe_blkno) 2192ccd979bdSMark Fasheh { 2193ccd979bdSMark Fasheh int status; 21942b6cb576SJoel Becker struct ocfs2_suballoc_result res; 2195ccd979bdSMark Fasheh 2196ccd979bdSMark Fasheh mlog_entry_void(); 2197ccd979bdSMark Fasheh 2198ccd979bdSMark Fasheh BUG_ON(!ac); 2199ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_given != 0); 2200ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_wanted != 1); 2201ccd979bdSMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE); 2202ccd979bdSMark Fasheh 220313821151STao Ma ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac); 220413821151STao Ma 2205aa8f8e93SJoel Becker status = ocfs2_claim_suballoc_bits(ac, 2206da5cbf2fSMark Fasheh handle, 2207ccd979bdSMark Fasheh 1, 2208ccd979bdSMark Fasheh 1, 22097d1fe093SJoel Becker &res); 2210ccd979bdSMark Fasheh if (status < 0) { 2211ccd979bdSMark Fasheh mlog_errno(status); 2212ccd979bdSMark Fasheh goto bail; 2213ccd979bdSMark Fasheh } 22141ed9b777SJoel Becker atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs); 2215ccd979bdSMark Fasheh 22167d1fe093SJoel Becker BUG_ON(res.sr_bits != 1); 2217ccd979bdSMark Fasheh 22182b6cb576SJoel Becker *suballoc_loc = res.sr_bg_blkno; 22197d1fe093SJoel Becker *suballoc_bit = res.sr_bit_offset; 2220ba206635SJoel Becker *fe_blkno = res.sr_blkno; 2221ccd979bdSMark Fasheh ac->ac_bits_given++; 222213821151STao Ma ocfs2_save_inode_ac_group(dir, ac); 2223ccd979bdSMark Fasheh status = 0; 2224ccd979bdSMark Fasheh bail: 2225ccd979bdSMark Fasheh mlog_exit(status); 2226ccd979bdSMark Fasheh return status; 2227ccd979bdSMark Fasheh } 2228ccd979bdSMark Fasheh 2229ccd979bdSMark Fasheh /* translate a group desc. blkno and it's bitmap offset into 2230ccd979bdSMark Fasheh * disk cluster offset. */ 2231ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode, 2232ccd979bdSMark Fasheh u64 bg_blkno, 2233ccd979bdSMark Fasheh u16 bg_bit_off) 2234ccd979bdSMark Fasheh { 2235ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2236ccd979bdSMark Fasheh u32 cluster = 0; 2237ccd979bdSMark Fasheh 2238ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 2239ccd979bdSMark Fasheh 2240ccd979bdSMark Fasheh if (bg_blkno != osb->first_cluster_group_blkno) 2241ccd979bdSMark Fasheh cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno); 2242ccd979bdSMark Fasheh cluster += (u32) bg_bit_off; 2243ccd979bdSMark Fasheh return cluster; 2244ccd979bdSMark Fasheh } 2245ccd979bdSMark Fasheh 2246ccd979bdSMark Fasheh /* given a cluster offset, calculate which block group it belongs to 2247ccd979bdSMark Fasheh * and return that block offset. */ 2248d659072fSTao Ma u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster) 2249ccd979bdSMark Fasheh { 2250ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2251ccd979bdSMark Fasheh u32 group_no; 2252ccd979bdSMark Fasheh 2253ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 2254ccd979bdSMark Fasheh 2255ccd979bdSMark Fasheh group_no = cluster / osb->bitmap_cpg; 2256ccd979bdSMark Fasheh if (!group_no) 2257ccd979bdSMark Fasheh return osb->first_cluster_group_blkno; 2258ccd979bdSMark Fasheh return ocfs2_clusters_to_blocks(inode->i_sb, 2259ccd979bdSMark Fasheh group_no * osb->bitmap_cpg); 2260ccd979bdSMark Fasheh } 2261ccd979bdSMark Fasheh 2262ccd979bdSMark Fasheh /* given the block number of a cluster start, calculate which cluster 2263ccd979bdSMark Fasheh * group and descriptor bitmap offset that corresponds to. */ 2264ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode, 2265ccd979bdSMark Fasheh u64 data_blkno, 2266ccd979bdSMark Fasheh u64 *bg_blkno, 2267ccd979bdSMark Fasheh u16 *bg_bit_off) 2268ccd979bdSMark Fasheh { 2269ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2270ccd979bdSMark Fasheh u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno); 2271ccd979bdSMark Fasheh 2272ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode)); 2273ccd979bdSMark Fasheh 2274ccd979bdSMark Fasheh *bg_blkno = ocfs2_which_cluster_group(inode, 2275ccd979bdSMark Fasheh data_cluster); 2276ccd979bdSMark Fasheh 2277ccd979bdSMark Fasheh if (*bg_blkno == osb->first_cluster_group_blkno) 2278ccd979bdSMark Fasheh *bg_bit_off = (u16) data_cluster; 2279ccd979bdSMark Fasheh else 2280ccd979bdSMark Fasheh *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb, 2281ccd979bdSMark Fasheh data_blkno - *bg_blkno); 2282ccd979bdSMark Fasheh } 2283ccd979bdSMark Fasheh 2284ccd979bdSMark Fasheh /* 2285ccd979bdSMark Fasheh * min_bits - minimum contiguous chunk from this total allocation we 2286ccd979bdSMark Fasheh * can handle. set to what we asked for originally for a full 2287ccd979bdSMark Fasheh * contig. allocation, set to '1' to indicate we can deal with extents 2288ccd979bdSMark Fasheh * of any size. 2289ccd979bdSMark Fasheh */ 22901ed9b777SJoel Becker int __ocfs2_claim_clusters(handle_t *handle, 2291ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac, 2292ccd979bdSMark Fasheh u32 min_clusters, 2293415cb800SMark Fasheh u32 max_clusters, 2294ccd979bdSMark Fasheh u32 *cluster_start, 2295ccd979bdSMark Fasheh u32 *num_clusters) 2296ccd979bdSMark Fasheh { 2297ccd979bdSMark Fasheh int status; 2298415cb800SMark Fasheh unsigned int bits_wanted = max_clusters; 2299ba206635SJoel Becker struct ocfs2_suballoc_result res = { .sr_blkno = 0, }; 23001ed9b777SJoel Becker struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb); 2301ccd979bdSMark Fasheh 2302ccd979bdSMark Fasheh mlog_entry_void(); 2303ccd979bdSMark Fasheh 2304ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted); 2305ccd979bdSMark Fasheh 2306ccd979bdSMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL 2307ccd979bdSMark Fasheh && ac->ac_which != OCFS2_AC_USE_MAIN); 2308ccd979bdSMark Fasheh 2309ccd979bdSMark Fasheh if (ac->ac_which == OCFS2_AC_USE_LOCAL) { 231033d5d380SMark Fasheh WARN_ON(min_clusters > 1); 231133d5d380SMark Fasheh 2312ccd979bdSMark Fasheh status = ocfs2_claim_local_alloc_bits(osb, 2313ccd979bdSMark Fasheh handle, 2314ccd979bdSMark Fasheh ac, 2315ccd979bdSMark Fasheh bits_wanted, 2316ccd979bdSMark Fasheh cluster_start, 2317ccd979bdSMark Fasheh num_clusters); 2318ccd979bdSMark Fasheh if (!status) 2319ccd979bdSMark Fasheh atomic_inc(&osb->alloc_stats.local_data); 2320ccd979bdSMark Fasheh } else { 2321ccd979bdSMark Fasheh if (min_clusters > (osb->bitmap_cpg - 1)) { 2322ccd979bdSMark Fasheh /* The only paths asking for contiguousness 2323ccd979bdSMark Fasheh * should know about this already. */ 23242fbe8d1eSSunil Mushran mlog(ML_ERROR, "minimum allocation requested %u exceeds " 23252fbe8d1eSSunil Mushran "group bitmap size %u!\n", min_clusters, 23262fbe8d1eSSunil Mushran osb->bitmap_cpg); 2327ccd979bdSMark Fasheh status = -ENOSPC; 2328ccd979bdSMark Fasheh goto bail; 2329ccd979bdSMark Fasheh } 2330ccd979bdSMark Fasheh /* clamp the current request down to a realistic size. */ 2331ccd979bdSMark Fasheh if (bits_wanted > (osb->bitmap_cpg - 1)) 2332ccd979bdSMark Fasheh bits_wanted = osb->bitmap_cpg - 1; 2333ccd979bdSMark Fasheh 2334aa8f8e93SJoel Becker status = ocfs2_claim_suballoc_bits(ac, 2335da5cbf2fSMark Fasheh handle, 2336ccd979bdSMark Fasheh bits_wanted, 2337ccd979bdSMark Fasheh min_clusters, 23387d1fe093SJoel Becker &res); 2339ccd979bdSMark Fasheh if (!status) { 2340ba206635SJoel Becker BUG_ON(res.sr_blkno); /* cluster alloc can't set */ 2341ccd979bdSMark Fasheh *cluster_start = 2342ccd979bdSMark Fasheh ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode, 23437d1fe093SJoel Becker res.sr_bg_blkno, 23447d1fe093SJoel Becker res.sr_bit_offset); 2345ccd979bdSMark Fasheh atomic_inc(&osb->alloc_stats.bitmap_data); 23464711954eSTao Ma *num_clusters = res.sr_bits; 2347ccd979bdSMark Fasheh } 2348ccd979bdSMark Fasheh } 2349ccd979bdSMark Fasheh if (status < 0) { 2350ccd979bdSMark Fasheh if (status != -ENOSPC) 2351ccd979bdSMark Fasheh mlog_errno(status); 2352ccd979bdSMark Fasheh goto bail; 2353ccd979bdSMark Fasheh } 2354ccd979bdSMark Fasheh 23554711954eSTao Ma ac->ac_bits_given += *num_clusters; 2356ccd979bdSMark Fasheh 2357ccd979bdSMark Fasheh bail: 2358ccd979bdSMark Fasheh mlog_exit(status); 2359ccd979bdSMark Fasheh return status; 2360ccd979bdSMark Fasheh } 2361ccd979bdSMark Fasheh 23621ed9b777SJoel Becker int ocfs2_claim_clusters(handle_t *handle, 2363415cb800SMark Fasheh struct ocfs2_alloc_context *ac, 2364415cb800SMark Fasheh u32 min_clusters, 2365415cb800SMark Fasheh u32 *cluster_start, 2366415cb800SMark Fasheh u32 *num_clusters) 2367415cb800SMark Fasheh { 2368415cb800SMark Fasheh unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given; 2369415cb800SMark Fasheh 23701ed9b777SJoel Becker return __ocfs2_claim_clusters(handle, ac, min_clusters, 2371415cb800SMark Fasheh bits_wanted, cluster_start, num_clusters); 2372415cb800SMark Fasheh } 2373415cb800SMark Fasheh 2374b4414eeaSMark Fasheh static int ocfs2_block_group_clear_bits(handle_t *handle, 2375ccd979bdSMark Fasheh struct inode *alloc_inode, 2376ccd979bdSMark Fasheh struct ocfs2_group_desc *bg, 2377ccd979bdSMark Fasheh struct buffer_head *group_bh, 2378ccd979bdSMark Fasheh unsigned int bit_off, 2379b4414eeaSMark Fasheh unsigned int num_bits, 2380b4414eeaSMark Fasheh void (*undo_fn)(unsigned int bit, 2381b4414eeaSMark Fasheh unsigned long *bmap)) 2382ccd979bdSMark Fasheh { 2383ccd979bdSMark Fasheh int status; 2384ccd979bdSMark Fasheh unsigned int tmp; 2385ccd979bdSMark Fasheh struct ocfs2_group_desc *undo_bg = NULL; 2386ccd979bdSMark Fasheh 2387ccd979bdSMark Fasheh mlog_entry_void(); 2388ccd979bdSMark Fasheh 238942035306SJoel Becker /* The caller got this descriptor from 239042035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ 239142035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); 2392ccd979bdSMark Fasheh 2393ccd979bdSMark Fasheh mlog(0, "off = %u, num = %u\n", bit_off, num_bits); 2394ccd979bdSMark Fasheh 2395b4414eeaSMark Fasheh BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode)); 23960cf2f763SJoel Becker status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), 2397b4414eeaSMark Fasheh group_bh, 2398b4414eeaSMark Fasheh undo_fn ? 2399b4414eeaSMark Fasheh OCFS2_JOURNAL_ACCESS_UNDO : 2400b4414eeaSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 2401ccd979bdSMark Fasheh if (status < 0) { 2402ccd979bdSMark Fasheh mlog_errno(status); 2403ccd979bdSMark Fasheh goto bail; 2404ccd979bdSMark Fasheh } 2405ccd979bdSMark Fasheh 2406b4414eeaSMark Fasheh if (undo_fn) { 240794e41ecfSSunil Mushran jbd_lock_bh_state(group_bh); 240894e41ecfSSunil Mushran undo_bg = (struct ocfs2_group_desc *) 240994e41ecfSSunil Mushran bh2jh(group_bh)->b_committed_data; 241094e41ecfSSunil Mushran BUG_ON(!undo_bg); 241194e41ecfSSunil Mushran } 2412ccd979bdSMark Fasheh 2413ccd979bdSMark Fasheh tmp = num_bits; 2414ccd979bdSMark Fasheh while(tmp--) { 2415ccd979bdSMark Fasheh ocfs2_clear_bit((bit_off + tmp), 2416ccd979bdSMark Fasheh (unsigned long *) bg->bg_bitmap); 2417b4414eeaSMark Fasheh if (undo_fn) 2418b4414eeaSMark Fasheh undo_fn(bit_off + tmp, 2419ccd979bdSMark Fasheh (unsigned long *) undo_bg->bg_bitmap); 2420ccd979bdSMark Fasheh } 2421ccd979bdSMark Fasheh le16_add_cpu(&bg->bg_free_bits_count, num_bits); 2422ccd979bdSMark Fasheh 2423b4414eeaSMark Fasheh if (undo_fn) 242494e41ecfSSunil Mushran jbd_unlock_bh_state(group_bh); 242594e41ecfSSunil Mushran 2426ec20cec7SJoel Becker ocfs2_journal_dirty(handle, group_bh); 2427ccd979bdSMark Fasheh bail: 2428ccd979bdSMark Fasheh return status; 2429ccd979bdSMark Fasheh } 2430ccd979bdSMark Fasheh 2431ccd979bdSMark Fasheh /* 2432ccd979bdSMark Fasheh * expects the suballoc inode to already be locked. 2433ccd979bdSMark Fasheh */ 2434b4414eeaSMark Fasheh static int _ocfs2_free_suballoc_bits(handle_t *handle, 2435ccd979bdSMark Fasheh struct inode *alloc_inode, 2436ccd979bdSMark Fasheh struct buffer_head *alloc_bh, 2437ccd979bdSMark Fasheh unsigned int start_bit, 2438ccd979bdSMark Fasheh u64 bg_blkno, 2439b4414eeaSMark Fasheh unsigned int count, 2440b4414eeaSMark Fasheh void (*undo_fn)(unsigned int bit, 2441b4414eeaSMark Fasheh unsigned long *bitmap)) 2442ccd979bdSMark Fasheh { 2443ccd979bdSMark Fasheh int status = 0; 2444ccd979bdSMark Fasheh u32 tmp_used; 2445ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data; 2446ccd979bdSMark Fasheh struct ocfs2_chain_list *cl = &fe->id2.i_chain; 2447ccd979bdSMark Fasheh struct buffer_head *group_bh = NULL; 2448ccd979bdSMark Fasheh struct ocfs2_group_desc *group; 2449ccd979bdSMark Fasheh 2450ccd979bdSMark Fasheh mlog_entry_void(); 2451ccd979bdSMark Fasheh 245210995aa2SJoel Becker /* The alloc_bh comes from ocfs2_free_dinode() or 245310995aa2SJoel Becker * ocfs2_free_clusters(). The callers have all locked the 245410995aa2SJoel Becker * allocator and gotten alloc_bh from the lock call. This 245510995aa2SJoel Becker * validates the dinode buffer. Any corruption that has happended 245610995aa2SJoel Becker * is a code bug. */ 245710995aa2SJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); 2458ccd979bdSMark Fasheh BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl)); 2459ccd979bdSMark Fasheh 2460b0697053SMark Fasheh mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n", 2461b0697053SMark Fasheh (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count, 2462b0697053SMark Fasheh (unsigned long long)bg_blkno, start_bit); 2463ccd979bdSMark Fasheh 246468f64d47SJoel Becker status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno, 246568f64d47SJoel Becker &group_bh); 2466ccd979bdSMark Fasheh if (status < 0) { 2467ccd979bdSMark Fasheh mlog_errno(status); 2468ccd979bdSMark Fasheh goto bail; 2469ccd979bdSMark Fasheh } 2470ccd979bdSMark Fasheh group = (struct ocfs2_group_desc *) group_bh->b_data; 247168f64d47SJoel Becker 2472ccd979bdSMark Fasheh BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits)); 2473ccd979bdSMark Fasheh 2474ccd979bdSMark Fasheh status = ocfs2_block_group_clear_bits(handle, alloc_inode, 2475ccd979bdSMark Fasheh group, group_bh, 2476b4414eeaSMark Fasheh start_bit, count, undo_fn); 2477ccd979bdSMark Fasheh if (status < 0) { 2478ccd979bdSMark Fasheh mlog_errno(status); 2479ccd979bdSMark Fasheh goto bail; 2480ccd979bdSMark Fasheh } 2481ccd979bdSMark Fasheh 24820cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), 24830cf2f763SJoel Becker alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE); 2484ccd979bdSMark Fasheh if (status < 0) { 2485ccd979bdSMark Fasheh mlog_errno(status); 2486ccd979bdSMark Fasheh goto bail; 2487ccd979bdSMark Fasheh } 2488ccd979bdSMark Fasheh 2489ccd979bdSMark Fasheh le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free, 2490ccd979bdSMark Fasheh count); 2491ccd979bdSMark Fasheh tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used); 2492ccd979bdSMark Fasheh fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count); 2493ec20cec7SJoel Becker ocfs2_journal_dirty(handle, alloc_bh); 2494ccd979bdSMark Fasheh 2495ccd979bdSMark Fasheh bail: 2496ccd979bdSMark Fasheh brelse(group_bh); 2497ccd979bdSMark Fasheh 2498ccd979bdSMark Fasheh mlog_exit(status); 2499ccd979bdSMark Fasheh return status; 2500ccd979bdSMark Fasheh } 2501ccd979bdSMark Fasheh 2502b4414eeaSMark Fasheh int ocfs2_free_suballoc_bits(handle_t *handle, 2503b4414eeaSMark Fasheh struct inode *alloc_inode, 2504b4414eeaSMark Fasheh struct buffer_head *alloc_bh, 2505b4414eeaSMark Fasheh unsigned int start_bit, 2506b4414eeaSMark Fasheh u64 bg_blkno, 2507b4414eeaSMark Fasheh unsigned int count) 2508b4414eeaSMark Fasheh { 2509b4414eeaSMark Fasheh return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh, 2510b4414eeaSMark Fasheh start_bit, bg_blkno, count, NULL); 2511b4414eeaSMark Fasheh } 2512b4414eeaSMark Fasheh 25131fabe148SMark Fasheh int ocfs2_free_dinode(handle_t *handle, 2514ccd979bdSMark Fasheh struct inode *inode_alloc_inode, 2515ccd979bdSMark Fasheh struct buffer_head *inode_alloc_bh, 2516ccd979bdSMark Fasheh struct ocfs2_dinode *di) 2517ccd979bdSMark Fasheh { 2518ccd979bdSMark Fasheh u64 blk = le64_to_cpu(di->i_blkno); 2519ccd979bdSMark Fasheh u16 bit = le16_to_cpu(di->i_suballoc_bit); 2520ccd979bdSMark Fasheh u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit); 2521ccd979bdSMark Fasheh 252274380c47STao Ma if (di->i_suballoc_loc) 252374380c47STao Ma bg_blkno = le64_to_cpu(di->i_suballoc_loc); 2524ccd979bdSMark Fasheh return ocfs2_free_suballoc_bits(handle, inode_alloc_inode, 2525ccd979bdSMark Fasheh inode_alloc_bh, bit, bg_blkno, 1); 2526ccd979bdSMark Fasheh } 2527ccd979bdSMark Fasheh 2528b4414eeaSMark Fasheh static int _ocfs2_free_clusters(handle_t *handle, 2529ccd979bdSMark Fasheh struct inode *bitmap_inode, 2530ccd979bdSMark Fasheh struct buffer_head *bitmap_bh, 2531ccd979bdSMark Fasheh u64 start_blk, 2532b4414eeaSMark Fasheh unsigned int num_clusters, 2533b4414eeaSMark Fasheh void (*undo_fn)(unsigned int bit, 2534b4414eeaSMark Fasheh unsigned long *bitmap)) 2535ccd979bdSMark Fasheh { 2536ccd979bdSMark Fasheh int status; 2537ccd979bdSMark Fasheh u16 bg_start_bit; 2538ccd979bdSMark Fasheh u64 bg_blkno; 2539ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 2540ccd979bdSMark Fasheh 2541ccd979bdSMark Fasheh /* You can't ever have a contiguous set of clusters 2542ccd979bdSMark Fasheh * bigger than a block group bitmap so we never have to worry 2543ccd979bdSMark Fasheh * about looping on them. */ 2544ccd979bdSMark Fasheh 2545ccd979bdSMark Fasheh mlog_entry_void(); 2546ccd979bdSMark Fasheh 2547ccd979bdSMark Fasheh /* This is expensive. We can safely remove once this stuff has 2548ccd979bdSMark Fasheh * gotten tested really well. */ 2549ccd979bdSMark Fasheh BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk))); 2550ccd979bdSMark Fasheh 2551ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) bitmap_bh->b_data; 2552ccd979bdSMark Fasheh 2553ccd979bdSMark Fasheh ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno, 2554ccd979bdSMark Fasheh &bg_start_bit); 2555ccd979bdSMark Fasheh 2556b0697053SMark Fasheh mlog(0, "want to free %u clusters starting at block %llu\n", 2557b0697053SMark Fasheh num_clusters, (unsigned long long)start_blk); 2558b0697053SMark Fasheh mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n", 2559b0697053SMark Fasheh (unsigned long long)bg_blkno, bg_start_bit); 2560ccd979bdSMark Fasheh 2561b4414eeaSMark Fasheh status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh, 2562ccd979bdSMark Fasheh bg_start_bit, bg_blkno, 2563b4414eeaSMark Fasheh num_clusters, undo_fn); 25649c7af40bSMark Fasheh if (status < 0) { 2565ccd979bdSMark Fasheh mlog_errno(status); 25669c7af40bSMark Fasheh goto out; 25679c7af40bSMark Fasheh } 2568ccd979bdSMark Fasheh 25699c7af40bSMark Fasheh ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb), 25709c7af40bSMark Fasheh num_clusters); 25719c7af40bSMark Fasheh 25729c7af40bSMark Fasheh out: 2573ccd979bdSMark Fasheh mlog_exit(status); 2574ccd979bdSMark Fasheh return status; 2575ccd979bdSMark Fasheh } 2576ccd979bdSMark Fasheh 2577b4414eeaSMark Fasheh int ocfs2_free_clusters(handle_t *handle, 2578b4414eeaSMark Fasheh struct inode *bitmap_inode, 2579b4414eeaSMark Fasheh struct buffer_head *bitmap_bh, 2580b4414eeaSMark Fasheh u64 start_blk, 2581b4414eeaSMark Fasheh unsigned int num_clusters) 2582b4414eeaSMark Fasheh { 2583b4414eeaSMark Fasheh return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh, 2584b4414eeaSMark Fasheh start_blk, num_clusters, 2585b4414eeaSMark Fasheh _ocfs2_set_bit); 2586b4414eeaSMark Fasheh } 2587b4414eeaSMark Fasheh 2588b4414eeaSMark Fasheh /* 2589b4414eeaSMark Fasheh * Give never-used clusters back to the global bitmap. We don't need 2590b4414eeaSMark Fasheh * to protect these bits in the undo buffer. 2591b4414eeaSMark Fasheh */ 2592b4414eeaSMark Fasheh int ocfs2_release_clusters(handle_t *handle, 2593b4414eeaSMark Fasheh struct inode *bitmap_inode, 2594b4414eeaSMark Fasheh struct buffer_head *bitmap_bh, 2595b4414eeaSMark Fasheh u64 start_blk, 2596b4414eeaSMark Fasheh unsigned int num_clusters) 2597b4414eeaSMark Fasheh { 2598b4414eeaSMark Fasheh return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh, 2599b4414eeaSMark Fasheh start_blk, num_clusters, 2600b4414eeaSMark Fasheh _ocfs2_clear_bit); 2601b4414eeaSMark Fasheh } 2602b4414eeaSMark Fasheh 2603ccd979bdSMark Fasheh static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg) 2604ccd979bdSMark Fasheh { 2605ccd979bdSMark Fasheh printk("Block Group:\n"); 2606ccd979bdSMark Fasheh printk("bg_signature: %s\n", bg->bg_signature); 2607ccd979bdSMark Fasheh printk("bg_size: %u\n", bg->bg_size); 2608ccd979bdSMark Fasheh printk("bg_bits: %u\n", bg->bg_bits); 2609ccd979bdSMark Fasheh printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count); 2610ccd979bdSMark Fasheh printk("bg_chain: %u\n", bg->bg_chain); 2611ccd979bdSMark Fasheh printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation)); 2612b0697053SMark Fasheh printk("bg_next_group: %llu\n", 2613b0697053SMark Fasheh (unsigned long long)bg->bg_next_group); 2614b0697053SMark Fasheh printk("bg_parent_dinode: %llu\n", 2615b0697053SMark Fasheh (unsigned long long)bg->bg_parent_dinode); 2616b0697053SMark Fasheh printk("bg_blkno: %llu\n", 2617b0697053SMark Fasheh (unsigned long long)bg->bg_blkno); 2618ccd979bdSMark Fasheh } 2619ccd979bdSMark Fasheh 2620ccd979bdSMark Fasheh static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe) 2621ccd979bdSMark Fasheh { 2622ccd979bdSMark Fasheh int i; 2623ccd979bdSMark Fasheh 2624b0697053SMark Fasheh printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno); 2625ccd979bdSMark Fasheh printk("i_signature: %s\n", fe->i_signature); 2626b0697053SMark Fasheh printk("i_size: %llu\n", 2627b0697053SMark Fasheh (unsigned long long)fe->i_size); 2628ccd979bdSMark Fasheh printk("i_clusters: %u\n", fe->i_clusters); 2629ccd979bdSMark Fasheh printk("i_generation: %u\n", 2630ccd979bdSMark Fasheh le32_to_cpu(fe->i_generation)); 2631ccd979bdSMark Fasheh printk("id1.bitmap1.i_used: %u\n", 2632ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_used)); 2633ccd979bdSMark Fasheh printk("id1.bitmap1.i_total: %u\n", 2634ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_total)); 2635ccd979bdSMark Fasheh printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg); 2636ccd979bdSMark Fasheh printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc); 2637ccd979bdSMark Fasheh printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count); 2638ccd979bdSMark Fasheh printk("id2.i_chain.cl_next_free_rec: %u\n", 2639ccd979bdSMark Fasheh fe->id2.i_chain.cl_next_free_rec); 2640ccd979bdSMark Fasheh for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) { 2641ccd979bdSMark Fasheh printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i, 2642ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[i].c_free); 2643ccd979bdSMark Fasheh printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i, 2644ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[i].c_total); 2645b0697053SMark Fasheh printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i, 2646b0697053SMark Fasheh (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno); 2647ccd979bdSMark Fasheh } 2648ccd979bdSMark Fasheh } 2649e7d4cb6bSTao Ma 2650e7d4cb6bSTao Ma /* 2651e7d4cb6bSTao Ma * For a given allocation, determine which allocators will need to be 2652e7d4cb6bSTao Ma * accessed, and lock them, reserving the appropriate number of bits. 2653e7d4cb6bSTao Ma * 2654e7d4cb6bSTao Ma * Sparse file systems call this from ocfs2_write_begin_nolock() 2655e7d4cb6bSTao Ma * and ocfs2_allocate_unwritten_extents(). 2656e7d4cb6bSTao Ma * 2657e7d4cb6bSTao Ma * File systems which don't support holes call this from 2658e7d4cb6bSTao Ma * ocfs2_extend_allocation(). 2659e7d4cb6bSTao Ma */ 2660f99b9b7cSJoel Becker int ocfs2_lock_allocators(struct inode *inode, 2661f99b9b7cSJoel Becker struct ocfs2_extent_tree *et, 2662e7d4cb6bSTao Ma u32 clusters_to_add, u32 extents_to_split, 2663e7d4cb6bSTao Ma struct ocfs2_alloc_context **data_ac, 2664f99b9b7cSJoel Becker struct ocfs2_alloc_context **meta_ac) 2665e7d4cb6bSTao Ma { 2666e7d4cb6bSTao Ma int ret = 0, num_free_extents; 2667e7d4cb6bSTao Ma unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split; 2668e7d4cb6bSTao Ma struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2669e7d4cb6bSTao Ma 2670e7d4cb6bSTao Ma *meta_ac = NULL; 2671e7d4cb6bSTao Ma if (data_ac) 2672e7d4cb6bSTao Ma *data_ac = NULL; 2673e7d4cb6bSTao Ma 2674e7d4cb6bSTao Ma BUG_ON(clusters_to_add != 0 && data_ac == NULL); 2675e7d4cb6bSTao Ma 26763d03a305SJoel Becker num_free_extents = ocfs2_num_free_extents(osb, et); 2677e7d4cb6bSTao Ma if (num_free_extents < 0) { 2678e7d4cb6bSTao Ma ret = num_free_extents; 2679e7d4cb6bSTao Ma mlog_errno(ret); 2680e7d4cb6bSTao Ma goto out; 2681e7d4cb6bSTao Ma } 2682e7d4cb6bSTao Ma 2683e7d4cb6bSTao Ma /* 2684e7d4cb6bSTao Ma * Sparse allocation file systems need to be more conservative 2685e7d4cb6bSTao Ma * with reserving room for expansion - the actual allocation 2686e7d4cb6bSTao Ma * happens while we've got a journal handle open so re-taking 2687e7d4cb6bSTao Ma * a cluster lock (because we ran out of room for another 2688e7d4cb6bSTao Ma * extent) will violate ordering rules. 2689e7d4cb6bSTao Ma * 2690e7d4cb6bSTao Ma * Most of the time we'll only be seeing this 1 cluster at a time 2691e7d4cb6bSTao Ma * anyway. 2692e7d4cb6bSTao Ma * 2693e7d4cb6bSTao Ma * Always lock for any unwritten extents - we might want to 2694e7d4cb6bSTao Ma * add blocks during a split. 2695e7d4cb6bSTao Ma */ 2696e7d4cb6bSTao Ma if (!num_free_extents || 2697e7d4cb6bSTao Ma (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) { 2698f99b9b7cSJoel Becker ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac); 2699e7d4cb6bSTao Ma if (ret < 0) { 2700e7d4cb6bSTao Ma if (ret != -ENOSPC) 2701e7d4cb6bSTao Ma mlog_errno(ret); 2702e7d4cb6bSTao Ma goto out; 2703e7d4cb6bSTao Ma } 2704e7d4cb6bSTao Ma } 2705e7d4cb6bSTao Ma 2706e7d4cb6bSTao Ma if (clusters_to_add == 0) 2707e7d4cb6bSTao Ma goto out; 2708e7d4cb6bSTao Ma 2709e7d4cb6bSTao Ma ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac); 2710e7d4cb6bSTao Ma if (ret < 0) { 2711e7d4cb6bSTao Ma if (ret != -ENOSPC) 2712e7d4cb6bSTao Ma mlog_errno(ret); 2713e7d4cb6bSTao Ma goto out; 2714e7d4cb6bSTao Ma } 2715e7d4cb6bSTao Ma 2716e7d4cb6bSTao Ma out: 2717e7d4cb6bSTao Ma if (ret) { 2718e7d4cb6bSTao Ma if (*meta_ac) { 2719e7d4cb6bSTao Ma ocfs2_free_alloc_context(*meta_ac); 2720e7d4cb6bSTao Ma *meta_ac = NULL; 2721e7d4cb6bSTao Ma } 2722e7d4cb6bSTao Ma 2723e7d4cb6bSTao Ma /* 2724e7d4cb6bSTao Ma * We cannot have an error and a non null *data_ac. 2725e7d4cb6bSTao Ma */ 2726e7d4cb6bSTao Ma } 2727e7d4cb6bSTao Ma 2728e7d4cb6bSTao Ma return ret; 2729e7d4cb6bSTao Ma } 27306ca497a8Swengang wang 27316ca497a8Swengang wang /* 27326ca497a8Swengang wang * Read the inode specified by blkno to get suballoc_slot and 27336ca497a8Swengang wang * suballoc_bit. 27346ca497a8Swengang wang */ 27356ca497a8Swengang wang static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno, 2736889f004aSTao Ma u16 *suballoc_slot, u64 *group_blkno, 2737889f004aSTao Ma u16 *suballoc_bit) 27386ca497a8Swengang wang { 27396ca497a8Swengang wang int status; 27406ca497a8Swengang wang struct buffer_head *inode_bh = NULL; 27416ca497a8Swengang wang struct ocfs2_dinode *inode_fe; 27426ca497a8Swengang wang 27435b09b507SJoel Becker mlog_entry("blkno: %llu\n", (unsigned long long)blkno); 27446ca497a8Swengang wang 27456ca497a8Swengang wang /* dirty read disk */ 27466ca497a8Swengang wang status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh); 27476ca497a8Swengang wang if (status < 0) { 27485b09b507SJoel Becker mlog(ML_ERROR, "read block %llu failed %d\n", 27495b09b507SJoel Becker (unsigned long long)blkno, status); 27506ca497a8Swengang wang goto bail; 27516ca497a8Swengang wang } 27526ca497a8Swengang wang 27536ca497a8Swengang wang inode_fe = (struct ocfs2_dinode *) inode_bh->b_data; 27546ca497a8Swengang wang if (!OCFS2_IS_VALID_DINODE(inode_fe)) { 27555b09b507SJoel Becker mlog(ML_ERROR, "invalid inode %llu requested\n", 27565b09b507SJoel Becker (unsigned long long)blkno); 27576ca497a8Swengang wang status = -EINVAL; 27586ca497a8Swengang wang goto bail; 27596ca497a8Swengang wang } 27606ca497a8Swengang wang 27610fba8137STao Ma if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT && 27626ca497a8Swengang wang (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) { 27636ca497a8Swengang wang mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n", 27645b09b507SJoel Becker (unsigned long long)blkno, 27655b09b507SJoel Becker (u32)le16_to_cpu(inode_fe->i_suballoc_slot)); 27666ca497a8Swengang wang status = -EINVAL; 27676ca497a8Swengang wang goto bail; 27686ca497a8Swengang wang } 27696ca497a8Swengang wang 27706ca497a8Swengang wang if (suballoc_slot) 27716ca497a8Swengang wang *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot); 27726ca497a8Swengang wang if (suballoc_bit) 27736ca497a8Swengang wang *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit); 2774889f004aSTao Ma if (group_blkno) 2775889f004aSTao Ma *group_blkno = le64_to_cpu(inode_fe->i_suballoc_loc); 27766ca497a8Swengang wang 27776ca497a8Swengang wang bail: 27786ca497a8Swengang wang brelse(inode_bh); 27796ca497a8Swengang wang 27806ca497a8Swengang wang mlog_exit(status); 27816ca497a8Swengang wang return status; 27826ca497a8Swengang wang } 27836ca497a8Swengang wang 27846ca497a8Swengang wang /* 27856ca497a8Swengang wang * test whether bit is SET in allocator bitmap or not. on success, 0 27866ca497a8Swengang wang * is returned and *res is 1 for SET; 0 otherwise. when fails, errno 27876ca497a8Swengang wang * is returned and *res is meaningless. Call this after you have 27886ca497a8Swengang wang * cluster locked against suballoc, or you may get a result based on 27896ca497a8Swengang wang * non-up2date contents 27906ca497a8Swengang wang */ 27916ca497a8Swengang wang static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb, 27926ca497a8Swengang wang struct inode *suballoc, 2793889f004aSTao Ma struct buffer_head *alloc_bh, 2794889f004aSTao Ma u64 group_blkno, u64 blkno, 27956ca497a8Swengang wang u16 bit, int *res) 27966ca497a8Swengang wang { 2797abf1b3cbSTao Ma struct ocfs2_dinode *alloc_di; 27986ca497a8Swengang wang struct ocfs2_group_desc *group; 27996ca497a8Swengang wang struct buffer_head *group_bh = NULL; 28006ca497a8Swengang wang u64 bg_blkno; 28016ca497a8Swengang wang int status; 28026ca497a8Swengang wang 28035b09b507SJoel Becker mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno, 28045b09b507SJoel Becker (unsigned int)bit); 28056ca497a8Swengang wang 2806abf1b3cbSTao Ma alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data; 2807abf1b3cbSTao Ma if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) { 28086ca497a8Swengang wang mlog(ML_ERROR, "suballoc bit %u out of range of %u\n", 28096ca497a8Swengang wang (unsigned int)bit, 2810abf1b3cbSTao Ma ocfs2_bits_per_group(&alloc_di->id2.i_chain)); 28116ca497a8Swengang wang status = -EINVAL; 28126ca497a8Swengang wang goto bail; 28136ca497a8Swengang wang } 28146ca497a8Swengang wang 2815889f004aSTao Ma bg_blkno = group_blkno ? group_blkno : 2816889f004aSTao Ma ocfs2_which_suballoc_group(blkno, bit); 2817abf1b3cbSTao Ma status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno, 28186ca497a8Swengang wang &group_bh); 28196ca497a8Swengang wang if (status < 0) { 28205b09b507SJoel Becker mlog(ML_ERROR, "read group %llu failed %d\n", 28215b09b507SJoel Becker (unsigned long long)bg_blkno, status); 28226ca497a8Swengang wang goto bail; 28236ca497a8Swengang wang } 28246ca497a8Swengang wang 28256ca497a8Swengang wang group = (struct ocfs2_group_desc *) group_bh->b_data; 28266ca497a8Swengang wang *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap); 28276ca497a8Swengang wang 28286ca497a8Swengang wang bail: 28296ca497a8Swengang wang brelse(group_bh); 28306ca497a8Swengang wang 28316ca497a8Swengang wang mlog_exit(status); 28326ca497a8Swengang wang return status; 28336ca497a8Swengang wang } 28346ca497a8Swengang wang 28356ca497a8Swengang wang /* 28366ca497a8Swengang wang * Test if the bit representing this inode (blkno) is set in the 28376ca497a8Swengang wang * suballocator. 28386ca497a8Swengang wang * 28396ca497a8Swengang wang * On success, 0 is returned and *res is 1 for SET; 0 otherwise. 28406ca497a8Swengang wang * 28416ca497a8Swengang wang * In the event of failure, a negative value is returned and *res is 28426ca497a8Swengang wang * meaningless. 28436ca497a8Swengang wang * 28446ca497a8Swengang wang * Callers must make sure to hold nfs_sync_lock to prevent 28456ca497a8Swengang wang * ocfs2_delete_inode() on another node from accessing the same 28466ca497a8Swengang wang * suballocator concurrently. 28476ca497a8Swengang wang */ 28486ca497a8Swengang wang int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res) 28496ca497a8Swengang wang { 28506ca497a8Swengang wang int status; 2851889f004aSTao Ma u64 group_blkno = 0; 28526ca497a8Swengang wang u16 suballoc_bit = 0, suballoc_slot = 0; 28536ca497a8Swengang wang struct inode *inode_alloc_inode; 28546ca497a8Swengang wang struct buffer_head *alloc_bh = NULL; 28556ca497a8Swengang wang 28565b09b507SJoel Becker mlog_entry("blkno: %llu", (unsigned long long)blkno); 28576ca497a8Swengang wang 28586ca497a8Swengang wang status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot, 2859889f004aSTao Ma &group_blkno, &suballoc_bit); 28606ca497a8Swengang wang if (status < 0) { 28616ca497a8Swengang wang mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status); 28626ca497a8Swengang wang goto bail; 28636ca497a8Swengang wang } 28646ca497a8Swengang wang 28656ca497a8Swengang wang inode_alloc_inode = 28666ca497a8Swengang wang ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE, 28676ca497a8Swengang wang suballoc_slot); 28686ca497a8Swengang wang if (!inode_alloc_inode) { 28696ca497a8Swengang wang /* the error code could be inaccurate, but we are not able to 28706ca497a8Swengang wang * get the correct one. */ 28716ca497a8Swengang wang status = -EINVAL; 28726ca497a8Swengang wang mlog(ML_ERROR, "unable to get alloc inode in slot %u\n", 28736ca497a8Swengang wang (u32)suballoc_slot); 28746ca497a8Swengang wang goto bail; 28756ca497a8Swengang wang } 28766ca497a8Swengang wang 28776ca497a8Swengang wang mutex_lock(&inode_alloc_inode->i_mutex); 28786ca497a8Swengang wang status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0); 28796ca497a8Swengang wang if (status < 0) { 28806ca497a8Swengang wang mutex_unlock(&inode_alloc_inode->i_mutex); 28816ca497a8Swengang wang mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n", 28826ca497a8Swengang wang (u32)suballoc_slot, status); 28836ca497a8Swengang wang goto bail; 28846ca497a8Swengang wang } 28856ca497a8Swengang wang 28866ca497a8Swengang wang status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh, 2887889f004aSTao Ma group_blkno, blkno, suballoc_bit, res); 28886ca497a8Swengang wang if (status < 0) 28896ca497a8Swengang wang mlog(ML_ERROR, "test suballoc bit failed %d\n", status); 28906ca497a8Swengang wang 28916ca497a8Swengang wang ocfs2_inode_unlock(inode_alloc_inode, 0); 28926ca497a8Swengang wang mutex_unlock(&inode_alloc_inode->i_mutex); 28936ca497a8Swengang wang 28946ca497a8Swengang wang iput(inode_alloc_inode); 28956ca497a8Swengang wang brelse(alloc_bh); 28966ca497a8Swengang wang bail: 28976ca497a8Swengang wang mlog_exit(status); 28986ca497a8Swengang wang return status; 28996ca497a8Swengang wang } 2900