Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 2 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 3 | * |
| 4 | * suballoc.c |
| 5 | * |
| 6 | * metadata alloc and free |
| 7 | * Inspired by ext3 block groups. |
| 8 | * |
| 9 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public |
| 13 | * License as published by the Free Software Foundation; either |
| 14 | * version 2 of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public |
| 22 | * License along with this program; if not, write to the |
| 23 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 24 | * Boston, MA 021110-1307, USA. |
| 25 | */ |
| 26 | |
| 27 | #include <linux/fs.h> |
| 28 | #include <linux/types.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/highmem.h> |
| 31 | |
| 32 | #define MLOG_MASK_PREFIX ML_DISK_ALLOC |
| 33 | #include <cluster/masklog.h> |
| 34 | |
| 35 | #include "ocfs2.h" |
| 36 | |
| 37 | #include "alloc.h" |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 38 | #include "blockcheck.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 39 | #include "dlmglue.h" |
| 40 | #include "inode.h" |
| 41 | #include "journal.h" |
| 42 | #include "localalloc.h" |
| 43 | #include "suballoc.h" |
| 44 | #include "super.h" |
| 45 | #include "sysfile.h" |
| 46 | #include "uptodate.h" |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 47 | #include "ocfs2_trace.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 48 | |
| 49 | #include "buffer_head_io.h" |
| 50 | |
Tao Ma | ffda89a | 2008-03-03 17:12:09 +0800 | [diff] [blame] | 51 | #define NOT_ALLOC_NEW_GROUP 0 |
Tao Ma | 60ca81e | 2009-02-25 00:53:24 +0800 | [diff] [blame] | 52 | #define ALLOC_NEW_GROUP 0x1 |
| 53 | #define ALLOC_GROUPS_FROM_GLOBAL 0x2 |
Tao Ma | ffda89a | 2008-03-03 17:12:09 +0800 | [diff] [blame] | 54 | |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 55 | #define OCFS2_MAX_TO_STEAL 1024 |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 56 | |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 57 | struct ocfs2_suballoc_result { |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 58 | u64 sr_bg_blkno; /* The bg we allocated from. Set |
| 59 | to 0 when a block group is |
| 60 | contiguous. */ |
Mark Fasheh | e49e276 | 2010-08-13 15:15:17 -0700 | [diff] [blame] | 61 | u64 sr_bg_stable_blkno; /* |
| 62 | * Doesn't change, always |
| 63 | * set to target block |
| 64 | * group descriptor |
| 65 | * block. |
| 66 | */ |
Joel Becker | ba20663 | 2010-03-26 10:08:59 +0800 | [diff] [blame] | 67 | u64 sr_blkno; /* The first allocated block */ |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 68 | unsigned int sr_bit_offset; /* The bit in the bg */ |
| 69 | unsigned int sr_bits; /* How many bits we claimed */ |
| 70 | }; |
| 71 | |
Mark Fasheh | b2b6ebf | 2010-08-26 13:06:50 -0700 | [diff] [blame] | 72 | static u64 ocfs2_group_from_res(struct ocfs2_suballoc_result *res) |
| 73 | { |
| 74 | if (res->sr_blkno == 0) |
| 75 | return 0; |
| 76 | |
| 77 | if (res->sr_bg_blkno) |
| 78 | return res->sr_bg_blkno; |
| 79 | |
| 80 | return ocfs2_which_suballoc_group(res->sr_blkno, res->sr_bit_offset); |
| 81 | } |
| 82 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 83 | static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg); |
| 84 | static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe); |
| 85 | static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl); |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 86 | static int ocfs2_block_group_fill(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 87 | struct inode *alloc_inode, |
| 88 | struct buffer_head *bg_bh, |
| 89 | u64 group_blkno, |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 90 | unsigned int group_clusters, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 91 | u16 my_chain, |
| 92 | struct ocfs2_chain_list *cl); |
| 93 | static int ocfs2_block_group_alloc(struct ocfs2_super *osb, |
| 94 | struct inode *alloc_inode, |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 95 | struct buffer_head *bh, |
Tao Ma | 60ca81e | 2009-02-25 00:53:24 +0800 | [diff] [blame] | 96 | u64 max_block, |
Tao Ma | feb473a | 2009-02-25 00:53:25 +0800 | [diff] [blame] | 97 | u64 *last_alloc_group, |
Tao Ma | 60ca81e | 2009-02-25 00:53:24 +0800 | [diff] [blame] | 98 | int flags); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 99 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 100 | static int ocfs2_cluster_group_search(struct inode *inode, |
| 101 | struct buffer_head *group_bh, |
| 102 | u32 bits_wanted, u32 min_bits, |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 103 | u64 max_block, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 104 | struct ocfs2_suballoc_result *res); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 105 | static int ocfs2_block_group_search(struct inode *inode, |
| 106 | struct buffer_head *group_bh, |
| 107 | u32 bits_wanted, u32 min_bits, |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 108 | u64 max_block, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 109 | struct ocfs2_suballoc_result *res); |
Joel Becker | aa8f8e9 | 2010-03-26 10:08:07 +0800 | [diff] [blame] | 110 | static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 111 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 112 | u32 bits_wanted, |
| 113 | u32 min_bits, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 114 | struct ocfs2_suballoc_result *res); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 115 | static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh, |
| 116 | int nr); |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 117 | static inline int ocfs2_block_group_set_bits(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 118 | struct inode *alloc_inode, |
| 119 | struct ocfs2_group_desc *bg, |
| 120 | struct buffer_head *group_bh, |
| 121 | unsigned int bit_off, |
| 122 | unsigned int num_bits); |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 123 | static int ocfs2_relink_block_group(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 124 | struct inode *alloc_inode, |
| 125 | struct buffer_head *fe_bh, |
| 126 | struct buffer_head *bg_bh, |
| 127 | struct buffer_head *prev_bg_bh, |
| 128 | u16 chain); |
| 129 | static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg, |
| 130 | u32 wanted); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 131 | static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode, |
| 132 | u64 bg_blkno, |
| 133 | u16 bg_bit_off); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 134 | static inline void ocfs2_block_to_cluster_group(struct inode *inode, |
| 135 | u64 data_blkno, |
| 136 | u64 *bg_blkno, |
| 137 | u16 *bg_bit_off); |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 138 | static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb, |
| 139 | u32 bits_wanted, u64 max_block, |
Tao Ma | 60ca81e | 2009-02-25 00:53:24 +0800 | [diff] [blame] | 140 | int flags, |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 141 | struct ocfs2_alloc_context **ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 142 | |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 143 | void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 144 | { |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 145 | struct inode *inode = ac->ac_inode; |
| 146 | |
| 147 | if (inode) { |
| 148 | if (ac->ac_which != OCFS2_AC_USE_LOCAL) |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 149 | ocfs2_inode_unlock(inode, 1); |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 150 | |
| 151 | mutex_unlock(&inode->i_mutex); |
| 152 | |
| 153 | iput(inode); |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 154 | ac->ac_inode = NULL; |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 155 | } |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 156 | brelse(ac->ac_bh); |
| 157 | ac->ac_bh = NULL; |
Mark Fasheh | e3b4a97 | 2009-12-07 13:16:07 -0800 | [diff] [blame] | 158 | ac->ac_resv = NULL; |
Mark Fasheh | e49e276 | 2010-08-13 15:15:17 -0700 | [diff] [blame] | 159 | if (ac->ac_find_loc_priv) { |
| 160 | kfree(ac->ac_find_loc_priv); |
| 161 | ac->ac_find_loc_priv = NULL; |
| 162 | } |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac) |
| 166 | { |
| 167 | ocfs2_free_ac_resource(ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 168 | kfree(ac); |
| 169 | } |
| 170 | |
| 171 | static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl) |
| 172 | { |
| 173 | return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc); |
| 174 | } |
| 175 | |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 176 | #define do_error(fmt, ...) \ |
| 177 | do{ \ |
Tao Ma | 78c37eb | 2010-03-03 11:26:27 +0800 | [diff] [blame] | 178 | if (resize) \ |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 179 | mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \ |
| 180 | else \ |
| 181 | ocfs2_error(sb, fmt, ##__VA_ARGS__); \ |
| 182 | } while (0) |
| 183 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 184 | static int ocfs2_validate_gd_self(struct super_block *sb, |
| 185 | struct buffer_head *bh, |
Tao Ma | 78c37eb | 2010-03-03 11:26:27 +0800 | [diff] [blame] | 186 | int resize) |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 187 | { |
| 188 | struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; |
| 189 | |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 190 | if (!OCFS2_IS_VALID_GROUP_DESC(gd)) { |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 191 | do_error("Group descriptor #%llu has bad signature %.*s", |
| 192 | (unsigned long long)bh->b_blocknr, 7, |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 193 | gd->bg_signature); |
| 194 | return -EINVAL; |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 197 | if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) { |
| 198 | do_error("Group descriptor #%llu has an invalid bg_blkno " |
| 199 | "of %llu", |
| 200 | (unsigned long long)bh->b_blocknr, |
| 201 | (unsigned long long)le64_to_cpu(gd->bg_blkno)); |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | |
| 205 | if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) { |
| 206 | do_error("Group descriptor #%llu has an invalid " |
| 207 | "fs_generation of #%u", |
| 208 | (unsigned long long)bh->b_blocknr, |
| 209 | le32_to_cpu(gd->bg_generation)); |
| 210 | return -EINVAL; |
| 211 | } |
| 212 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 213 | if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) { |
| 214 | do_error("Group descriptor #%llu has bit count %u but " |
| 215 | "claims that %u are free", |
| 216 | (unsigned long long)bh->b_blocknr, |
| 217 | le16_to_cpu(gd->bg_bits), |
| 218 | le16_to_cpu(gd->bg_free_bits_count)); |
| 219 | return -EINVAL; |
| 220 | } |
| 221 | |
| 222 | if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) { |
| 223 | do_error("Group descriptor #%llu has bit count %u but " |
| 224 | "max bitmap bits of %u", |
| 225 | (unsigned long long)bh->b_blocknr, |
| 226 | le16_to_cpu(gd->bg_bits), |
| 227 | 8 * le16_to_cpu(gd->bg_size)); |
| 228 | return -EINVAL; |
| 229 | } |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static int ocfs2_validate_gd_parent(struct super_block *sb, |
| 235 | struct ocfs2_dinode *di, |
| 236 | struct buffer_head *bh, |
Tao Ma | 78c37eb | 2010-03-03 11:26:27 +0800 | [diff] [blame] | 237 | int resize) |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 238 | { |
| 239 | unsigned int max_bits; |
| 240 | struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; |
| 241 | |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 242 | if (di->i_blkno != gd->bg_parent_dinode) { |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 243 | do_error("Group descriptor #%llu has bad parent " |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 244 | "pointer (%llu, expected %llu)", |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 245 | (unsigned long long)bh->b_blocknr, |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 246 | (unsigned long long)le64_to_cpu(gd->bg_parent_dinode), |
| 247 | (unsigned long long)le64_to_cpu(di->i_blkno)); |
| 248 | return -EINVAL; |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc); |
| 252 | if (le16_to_cpu(gd->bg_bits) > max_bits) { |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 253 | do_error("Group descriptor #%llu has bit count of %u", |
| 254 | (unsigned long long)bh->b_blocknr, |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 255 | le16_to_cpu(gd->bg_bits)); |
| 256 | return -EINVAL; |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Tao Ma | 78c37eb | 2010-03-03 11:26:27 +0800 | [diff] [blame] | 259 | /* In resize, we may meet the case bg_chain == cl_next_free_rec. */ |
| 260 | if ((le16_to_cpu(gd->bg_chain) > |
| 261 | le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) || |
| 262 | ((le16_to_cpu(gd->bg_chain) == |
| 263 | le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) { |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 264 | do_error("Group descriptor #%llu has bad chain %u", |
| 265 | (unsigned long long)bh->b_blocknr, |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 266 | le16_to_cpu(gd->bg_chain)); |
| 267 | return -EINVAL; |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 270 | return 0; |
| 271 | } |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 272 | |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 273 | #undef do_error |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 274 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 275 | /* |
| 276 | * This version only prints errors. It does not fail the filesystem, and |
| 277 | * exists only for resize. |
| 278 | */ |
| 279 | int ocfs2_check_group_descriptor(struct super_block *sb, |
| 280 | struct ocfs2_dinode *di, |
| 281 | struct buffer_head *bh) |
| 282 | { |
| 283 | int rc; |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 284 | struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 285 | |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 286 | BUG_ON(!buffer_uptodate(bh)); |
| 287 | |
| 288 | /* |
| 289 | * If the ecc fails, we return the error but otherwise |
| 290 | * leave the filesystem running. We know any error is |
| 291 | * local to this block. |
| 292 | */ |
| 293 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 294 | if (rc) { |
| 295 | mlog(ML_ERROR, |
| 296 | "Checksum failed for group descriptor %llu\n", |
| 297 | (unsigned long long)bh->b_blocknr); |
| 298 | } else |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 299 | rc = ocfs2_validate_gd_self(sb, bh, 1); |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 300 | if (!rc) |
| 301 | rc = ocfs2_validate_gd_parent(sb, di, bh, 1); |
| 302 | |
| 303 | return rc; |
| 304 | } |
| 305 | |
| 306 | static int ocfs2_validate_group_descriptor(struct super_block *sb, |
| 307 | struct buffer_head *bh) |
| 308 | { |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 309 | int rc; |
| 310 | struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; |
| 311 | |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 312 | trace_ocfs2_validate_group_descriptor( |
| 313 | (unsigned long long)bh->b_blocknr); |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 314 | |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 315 | BUG_ON(!buffer_uptodate(bh)); |
| 316 | |
| 317 | /* |
| 318 | * If the ecc fails, we return the error but otherwise |
| 319 | * leave the filesystem running. We know any error is |
| 320 | * local to this block. |
| 321 | */ |
| 322 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check); |
| 323 | if (rc) |
| 324 | return rc; |
| 325 | |
| 326 | /* |
| 327 | * Errors after here are fatal. |
| 328 | */ |
| 329 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 330 | return ocfs2_validate_gd_self(sb, bh, 0); |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 333 | int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di, |
| 334 | u64 gd_blkno, struct buffer_head **bh) |
| 335 | { |
| 336 | int rc; |
| 337 | struct buffer_head *tmp = *bh; |
| 338 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 339 | rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp, |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 340 | ocfs2_validate_group_descriptor); |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 341 | if (rc) |
| 342 | goto out; |
| 343 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 344 | rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0); |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 345 | if (rc) { |
| 346 | brelse(tmp); |
| 347 | goto out; |
| 348 | } |
| 349 | |
| 350 | /* If ocfs2_read_block() got us a new bh, pass it up. */ |
| 351 | if (!*bh) |
| 352 | *bh = tmp; |
| 353 | |
| 354 | out: |
| 355 | return rc; |
| 356 | } |
| 357 | |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 358 | static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb, |
| 359 | struct ocfs2_group_desc *bg, |
| 360 | struct ocfs2_chain_list *cl, |
Tao Ma | 47dea42 | 2010-09-13 15:13:50 +0800 | [diff] [blame] | 361 | u64 p_blkno, unsigned int clusters) |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 362 | { |
| 363 | struct ocfs2_extent_list *el = &bg->bg_list; |
| 364 | struct ocfs2_extent_rec *rec; |
| 365 | |
Tao Ma | 4711954 | 2010-04-22 14:09:15 +0800 | [diff] [blame] | 366 | BUG_ON(!ocfs2_supports_discontig_bg(osb)); |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 367 | if (!el->l_next_free_rec) |
| 368 | el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb)); |
| 369 | rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)]; |
Tao Ma | 4711954 | 2010-04-22 14:09:15 +0800 | [diff] [blame] | 370 | rec->e_blkno = cpu_to_le64(p_blkno); |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 371 | rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) / |
| 372 | le16_to_cpu(cl->cl_bpc)); |
Tao Ma | 47dea42 | 2010-09-13 15:13:50 +0800 | [diff] [blame] | 373 | rec->e_leaf_clusters = cpu_to_le16(clusters); |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 374 | le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc)); |
Tao Ma | 4711954 | 2010-04-22 14:09:15 +0800 | [diff] [blame] | 375 | le16_add_cpu(&bg->bg_free_bits_count, |
| 376 | clusters * le16_to_cpu(cl->cl_bpc)); |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 377 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 378 | } |
| 379 | |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 380 | static int ocfs2_block_group_fill(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 381 | struct inode *alloc_inode, |
| 382 | struct buffer_head *bg_bh, |
| 383 | u64 group_blkno, |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 384 | unsigned int group_clusters, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 385 | u16 my_chain, |
| 386 | struct ocfs2_chain_list *cl) |
| 387 | { |
| 388 | int status = 0; |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 389 | struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 390 | struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; |
| 391 | struct super_block * sb = alloc_inode->i_sb; |
| 392 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 393 | if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 394 | ocfs2_error(alloc_inode->i_sb, "group block (%llu) != " |
| 395 | "b_blocknr (%llu)", |
| 396 | (unsigned long long)group_blkno, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 397 | (unsigned long long) bg_bh->b_blocknr); |
| 398 | status = -EIO; |
| 399 | goto bail; |
| 400 | } |
| 401 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 402 | status = ocfs2_journal_access_gd(handle, |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 403 | INODE_CACHE(alloc_inode), |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 404 | bg_bh, |
| 405 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 406 | if (status < 0) { |
| 407 | mlog_errno(status); |
| 408 | goto bail; |
| 409 | } |
| 410 | |
| 411 | memset(bg, 0, sb->s_blocksize); |
| 412 | strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE); |
| 413 | bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation); |
Tao Ma | 8571882 | 2010-04-13 14:38:06 +0800 | [diff] [blame] | 414 | bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1, |
| 415 | osb->s_feature_incompat)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 416 | bg->bg_chain = cpu_to_le16(my_chain); |
| 417 | bg->bg_next_group = cl->cl_recs[my_chain].c_blkno; |
| 418 | bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno); |
| 419 | bg->bg_blkno = cpu_to_le64(group_blkno); |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 420 | if (group_clusters == le16_to_cpu(cl->cl_cpg)) |
| 421 | bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl)); |
| 422 | else |
Tao Ma | 4711954 | 2010-04-22 14:09:15 +0800 | [diff] [blame] | 423 | ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno, |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 424 | group_clusters); |
| 425 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 426 | /* set the 1st bit in the bitmap to account for the descriptor block */ |
| 427 | ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap); |
| 428 | bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1); |
| 429 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 430 | ocfs2_journal_dirty(handle, bg_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 431 | |
| 432 | /* There is no need to zero out or otherwise initialize the |
| 433 | * other blocks in a group - All valid FS metadata in a block |
| 434 | * group stores the superblock fs_generation value at |
| 435 | * allocation time. */ |
| 436 | |
| 437 | bail: |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 438 | if (status) |
| 439 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 440 | return status; |
| 441 | } |
| 442 | |
| 443 | static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl) |
| 444 | { |
| 445 | u16 curr, best; |
| 446 | |
| 447 | best = curr = 0; |
| 448 | while (curr < le16_to_cpu(cl->cl_count)) { |
| 449 | if (le32_to_cpu(cl->cl_recs[best].c_total) > |
| 450 | le32_to_cpu(cl->cl_recs[curr].c_total)) |
| 451 | best = curr; |
| 452 | curr++; |
| 453 | } |
| 454 | return best; |
| 455 | } |
| 456 | |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 457 | static struct buffer_head * |
| 458 | ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle, |
| 459 | struct inode *alloc_inode, |
| 460 | struct ocfs2_alloc_context *ac, |
| 461 | struct ocfs2_chain_list *cl) |
| 462 | { |
| 463 | int status; |
| 464 | u32 bit_off, num_bits; |
| 465 | u64 bg_blkno; |
| 466 | struct buffer_head *bg_bh; |
| 467 | unsigned int alloc_rec = ocfs2_find_smallest_chain(cl); |
| 468 | |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 469 | status = ocfs2_claim_clusters(handle, ac, |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 470 | le16_to_cpu(cl->cl_cpg), &bit_off, |
| 471 | &num_bits); |
| 472 | if (status < 0) { |
| 473 | if (status != -ENOSPC) |
| 474 | mlog_errno(status); |
| 475 | goto bail; |
| 476 | } |
| 477 | |
| 478 | /* setup the group */ |
| 479 | bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off); |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 480 | trace_ocfs2_block_group_alloc_contig( |
| 481 | (unsigned long long)bg_blkno, alloc_rec); |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 482 | |
| 483 | bg_bh = sb_getblk(osb->sb, bg_blkno); |
| 484 | if (!bg_bh) { |
| 485 | status = -EIO; |
| 486 | mlog_errno(status); |
| 487 | goto bail; |
| 488 | } |
| 489 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh); |
| 490 | |
| 491 | status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh, |
| 492 | bg_blkno, num_bits, alloc_rec, cl); |
| 493 | if (status < 0) { |
| 494 | brelse(bg_bh); |
| 495 | mlog_errno(status); |
| 496 | } |
| 497 | |
| 498 | bail: |
| 499 | return status ? ERR_PTR(status) : bg_bh; |
| 500 | } |
| 501 | |
| 502 | static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb, |
| 503 | handle_t *handle, |
| 504 | struct ocfs2_alloc_context *ac, |
| 505 | unsigned int min_bits, |
| 506 | u32 *bit_off, u32 *num_bits) |
| 507 | { |
Joel Becker | 18d3a98 | 2010-05-18 16:47:55 -0700 | [diff] [blame] | 508 | int status = 0; |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 509 | |
| 510 | while (min_bits) { |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 511 | status = ocfs2_claim_clusters(handle, ac, min_bits, |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 512 | bit_off, num_bits); |
| 513 | if (status != -ENOSPC) |
| 514 | break; |
| 515 | |
| 516 | min_bits >>= 1; |
| 517 | } |
| 518 | |
| 519 | return status; |
| 520 | } |
| 521 | |
| 522 | static int ocfs2_block_group_grow_discontig(handle_t *handle, |
| 523 | struct inode *alloc_inode, |
| 524 | struct buffer_head *bg_bh, |
| 525 | struct ocfs2_alloc_context *ac, |
| 526 | struct ocfs2_chain_list *cl, |
| 527 | unsigned int min_bits) |
| 528 | { |
| 529 | int status; |
| 530 | struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); |
| 531 | struct ocfs2_group_desc *bg = |
| 532 | (struct ocfs2_group_desc *)bg_bh->b_data; |
Tao Ma | 4711954 | 2010-04-22 14:09:15 +0800 | [diff] [blame] | 533 | unsigned int needed = le16_to_cpu(cl->cl_cpg) - |
| 534 | le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc); |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 535 | u32 p_cpos, clusters; |
| 536 | u64 p_blkno; |
| 537 | struct ocfs2_extent_list *el = &bg->bg_list; |
| 538 | |
| 539 | status = ocfs2_journal_access_gd(handle, |
| 540 | INODE_CACHE(alloc_inode), |
| 541 | bg_bh, |
| 542 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 543 | if (status < 0) { |
| 544 | mlog_errno(status); |
| 545 | goto bail; |
| 546 | } |
| 547 | |
| 548 | while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) < |
| 549 | le16_to_cpu(el->l_count))) { |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 550 | if (min_bits > needed) |
| 551 | min_bits = needed; |
| 552 | status = ocfs2_block_group_claim_bits(osb, handle, ac, |
| 553 | min_bits, &p_cpos, |
| 554 | &clusters); |
| 555 | if (status < 0) { |
| 556 | if (status != -ENOSPC) |
| 557 | mlog_errno(status); |
| 558 | goto bail; |
| 559 | } |
| 560 | p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos); |
| 561 | ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno, |
| 562 | clusters); |
| 563 | |
| 564 | min_bits = clusters; |
Tao Ma | 4711954 | 2010-04-22 14:09:15 +0800 | [diff] [blame] | 565 | needed = le16_to_cpu(cl->cl_cpg) - |
| 566 | le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc); |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | if (needed > 0) { |
Tao Ma | 4711954 | 2010-04-22 14:09:15 +0800 | [diff] [blame] | 570 | /* |
| 571 | * We have used up all the extent rec but can't fill up |
| 572 | * the cpg. So bail out. |
| 573 | */ |
| 574 | status = -ENOSPC; |
| 575 | goto bail; |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | ocfs2_journal_dirty(handle, bg_bh); |
| 579 | |
| 580 | bail: |
| 581 | return status; |
| 582 | } |
| 583 | |
Joel Becker | 8b06bc5 | 2010-03-26 10:09:29 +0800 | [diff] [blame] | 584 | static void ocfs2_bg_alloc_cleanup(handle_t *handle, |
| 585 | struct ocfs2_alloc_context *cluster_ac, |
| 586 | struct inode *alloc_inode, |
| 587 | struct buffer_head *bg_bh) |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 588 | { |
Joel Becker | 8b06bc5 | 2010-03-26 10:09:29 +0800 | [diff] [blame] | 589 | int i, ret; |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 590 | struct ocfs2_group_desc *bg; |
| 591 | struct ocfs2_extent_list *el; |
| 592 | struct ocfs2_extent_rec *rec; |
| 593 | |
| 594 | if (!bg_bh) |
| 595 | return; |
| 596 | |
| 597 | bg = (struct ocfs2_group_desc *)bg_bh->b_data; |
| 598 | el = &bg->bg_list; |
| 599 | for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { |
| 600 | rec = &el->l_recs[i]; |
Joel Becker | 8b06bc5 | 2010-03-26 10:09:29 +0800 | [diff] [blame] | 601 | ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode, |
| 602 | cluster_ac->ac_bh, |
| 603 | le64_to_cpu(rec->e_blkno), |
| 604 | le32_to_cpu(rec->e_leaf_clusters)); |
| 605 | if (ret) |
| 606 | mlog_errno(ret); |
| 607 | /* Try all the clusters to free */ |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh); |
| 611 | brelse(bg_bh); |
| 612 | } |
| 613 | |
| 614 | static struct buffer_head * |
| 615 | ocfs2_block_group_alloc_discontig(handle_t *handle, |
| 616 | struct inode *alloc_inode, |
| 617 | struct ocfs2_alloc_context *ac, |
Joel Becker | 8b06bc5 | 2010-03-26 10:09:29 +0800 | [diff] [blame] | 618 | struct ocfs2_chain_list *cl) |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 619 | { |
| 620 | int status; |
| 621 | u32 bit_off, num_bits; |
| 622 | u64 bg_blkno; |
| 623 | unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1; |
| 624 | struct buffer_head *bg_bh = NULL; |
| 625 | unsigned int alloc_rec = ocfs2_find_smallest_chain(cl); |
| 626 | struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); |
| 627 | |
Tao Ma | 4711954 | 2010-04-22 14:09:15 +0800 | [diff] [blame] | 628 | if (!ocfs2_supports_discontig_bg(osb)) { |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 629 | status = -ENOSPC; |
| 630 | goto bail; |
| 631 | } |
| 632 | |
Joel Becker | 8b06bc5 | 2010-03-26 10:09:29 +0800 | [diff] [blame] | 633 | status = ocfs2_extend_trans(handle, |
| 634 | ocfs2_calc_bg_discontig_credits(osb->sb)); |
| 635 | if (status) { |
| 636 | mlog_errno(status); |
| 637 | goto bail; |
| 638 | } |
| 639 | |
Joel Becker | 95ec0ad | 2010-03-26 10:10:08 +0800 | [diff] [blame] | 640 | /* |
| 641 | * We're going to be grabbing from multiple cluster groups. |
| 642 | * We don't have enough credits to relink them all, and the |
| 643 | * cluster groups will be staying in cache for the duration of |
| 644 | * this operation. |
| 645 | */ |
| 646 | ac->ac_allow_chain_relink = 0; |
| 647 | |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 648 | /* Claim the first region */ |
| 649 | status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits, |
| 650 | &bit_off, &num_bits); |
| 651 | if (status < 0) { |
| 652 | if (status != -ENOSPC) |
| 653 | mlog_errno(status); |
| 654 | goto bail; |
| 655 | } |
| 656 | min_bits = num_bits; |
| 657 | |
| 658 | /* setup the group */ |
| 659 | bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off); |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 660 | trace_ocfs2_block_group_alloc_discontig( |
| 661 | (unsigned long long)bg_blkno, alloc_rec); |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 662 | |
| 663 | bg_bh = sb_getblk(osb->sb, bg_blkno); |
| 664 | if (!bg_bh) { |
| 665 | status = -EIO; |
| 666 | mlog_errno(status); |
| 667 | goto bail; |
| 668 | } |
| 669 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh); |
| 670 | |
| 671 | status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh, |
| 672 | bg_blkno, num_bits, alloc_rec, cl); |
| 673 | if (status < 0) { |
| 674 | mlog_errno(status); |
| 675 | goto bail; |
| 676 | } |
| 677 | |
| 678 | status = ocfs2_block_group_grow_discontig(handle, alloc_inode, |
| 679 | bg_bh, ac, cl, min_bits); |
| 680 | if (status) |
| 681 | mlog_errno(status); |
| 682 | |
| 683 | bail: |
| 684 | if (status) |
Joel Becker | 8b06bc5 | 2010-03-26 10:09:29 +0800 | [diff] [blame] | 685 | ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh); |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 686 | return status ? ERR_PTR(status) : bg_bh; |
| 687 | } |
| 688 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 689 | /* |
| 690 | * We expect the block group allocator to already be locked. |
| 691 | */ |
| 692 | static int ocfs2_block_group_alloc(struct ocfs2_super *osb, |
| 693 | struct inode *alloc_inode, |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 694 | struct buffer_head *bh, |
Tao Ma | 60ca81e | 2009-02-25 00:53:24 +0800 | [diff] [blame] | 695 | u64 max_block, |
Tao Ma | feb473a | 2009-02-25 00:53:25 +0800 | [diff] [blame] | 696 | u64 *last_alloc_group, |
Tao Ma | 60ca81e | 2009-02-25 00:53:24 +0800 | [diff] [blame] | 697 | int flags) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 698 | { |
| 699 | int status, credits; |
| 700 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data; |
| 701 | struct ocfs2_chain_list *cl; |
| 702 | struct ocfs2_alloc_context *ac = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 703 | handle_t *handle = NULL; |
Tao Ma | 4711954 | 2010-04-22 14:09:15 +0800 | [diff] [blame] | 704 | u16 alloc_rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 705 | struct buffer_head *bg_bh = NULL; |
| 706 | struct ocfs2_group_desc *bg; |
| 707 | |
| 708 | BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode)); |
| 709 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 710 | cl = &fe->id2.i_chain; |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 711 | status = ocfs2_reserve_clusters_with_limit(osb, |
| 712 | le16_to_cpu(cl->cl_cpg), |
Tao Ma | 60ca81e | 2009-02-25 00:53:24 +0800 | [diff] [blame] | 713 | max_block, flags, &ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 714 | if (status < 0) { |
| 715 | if (status != -ENOSPC) |
| 716 | mlog_errno(status); |
| 717 | goto bail; |
| 718 | } |
| 719 | |
| 720 | credits = ocfs2_calc_group_alloc_credits(osb->sb, |
| 721 | le16_to_cpu(cl->cl_cpg)); |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 722 | handle = ocfs2_start_trans(osb, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 723 | if (IS_ERR(handle)) { |
| 724 | status = PTR_ERR(handle); |
| 725 | handle = NULL; |
| 726 | mlog_errno(status); |
| 727 | goto bail; |
| 728 | } |
| 729 | |
Tao Ma | feb473a | 2009-02-25 00:53:25 +0800 | [diff] [blame] | 730 | if (last_alloc_group && *last_alloc_group != 0) { |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 731 | trace_ocfs2_block_group_alloc( |
| 732 | (unsigned long long)*last_alloc_group); |
Tao Ma | feb473a | 2009-02-25 00:53:25 +0800 | [diff] [blame] | 733 | ac->ac_last_group = *last_alloc_group; |
| 734 | } |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 735 | |
| 736 | bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode, |
| 737 | ac, cl); |
| 738 | if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC)) |
| 739 | bg_bh = ocfs2_block_group_alloc_discontig(handle, |
| 740 | alloc_inode, |
Joel Becker | 8b06bc5 | 2010-03-26 10:09:29 +0800 | [diff] [blame] | 741 | ac, cl); |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 742 | if (IS_ERR(bg_bh)) { |
| 743 | status = PTR_ERR(bg_bh); |
| 744 | bg_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 745 | if (status != -ENOSPC) |
| 746 | mlog_errno(status); |
| 747 | goto bail; |
| 748 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 749 | bg = (struct ocfs2_group_desc *) bg_bh->b_data; |
| 750 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 751 | status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 752 | bh, OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 753 | if (status < 0) { |
| 754 | mlog_errno(status); |
| 755 | goto bail; |
| 756 | } |
| 757 | |
Tao Ma | 4711954 | 2010-04-22 14:09:15 +0800 | [diff] [blame] | 758 | alloc_rec = le16_to_cpu(bg->bg_chain); |
| 759 | le32_add_cpu(&cl->cl_recs[alloc_rec].c_free, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 760 | le16_to_cpu(bg->bg_free_bits_count)); |
Tao Ma | 4711954 | 2010-04-22 14:09:15 +0800 | [diff] [blame] | 761 | le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, |
Joel Becker | 798db35 | 2010-04-13 14:26:32 +0800 | [diff] [blame] | 762 | le16_to_cpu(bg->bg_bits)); |
Tao Ma | 0a463b7 | 2010-07-08 11:11:11 +0800 | [diff] [blame] | 763 | cl->cl_recs[alloc_rec].c_blkno = bg->bg_blkno; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 764 | if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count)) |
| 765 | le16_add_cpu(&cl->cl_next_free_rec, 1); |
| 766 | |
| 767 | le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) - |
| 768 | le16_to_cpu(bg->bg_free_bits_count)); |
| 769 | le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits)); |
| 770 | le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg)); |
| 771 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 772 | ocfs2_journal_dirty(handle, bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 773 | |
| 774 | spin_lock(&OCFS2_I(alloc_inode)->ip_lock); |
| 775 | OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters); |
| 776 | fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb, |
| 777 | le32_to_cpu(fe->i_clusters))); |
| 778 | spin_unlock(&OCFS2_I(alloc_inode)->ip_lock); |
| 779 | i_size_write(alloc_inode, le64_to_cpu(fe->i_size)); |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 780 | alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 781 | |
| 782 | status = 0; |
Tao Ma | feb473a | 2009-02-25 00:53:25 +0800 | [diff] [blame] | 783 | |
| 784 | /* save the new last alloc group so that the caller can cache it. */ |
| 785 | if (last_alloc_group) |
| 786 | *last_alloc_group = ac->ac_last_group; |
| 787 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 788 | bail: |
| 789 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 790 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 791 | |
| 792 | if (ac) |
| 793 | ocfs2_free_alloc_context(ac); |
| 794 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 795 | brelse(bg_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 796 | |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 797 | if (status) |
| 798 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 799 | return status; |
| 800 | } |
| 801 | |
| 802 | static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb, |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 803 | struct ocfs2_alloc_context *ac, |
| 804 | int type, |
Tao Ma | ffda89a | 2008-03-03 17:12:09 +0800 | [diff] [blame] | 805 | u32 slot, |
Tao Ma | feb473a | 2009-02-25 00:53:25 +0800 | [diff] [blame] | 806 | u64 *last_alloc_group, |
Tao Ma | 60ca81e | 2009-02-25 00:53:24 +0800 | [diff] [blame] | 807 | int flags) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 808 | { |
| 809 | int status; |
| 810 | u32 bits_wanted = ac->ac_bits_wanted; |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 811 | struct inode *alloc_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 812 | struct buffer_head *bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 813 | struct ocfs2_dinode *fe; |
| 814 | u32 free_bits; |
| 815 | |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 816 | alloc_inode = ocfs2_get_system_file_inode(osb, type, slot); |
| 817 | if (!alloc_inode) { |
| 818 | mlog_errno(-EINVAL); |
| 819 | return -EINVAL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 822 | mutex_lock(&alloc_inode->i_mutex); |
| 823 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 824 | status = ocfs2_inode_lock(alloc_inode, &bh, 1); |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 825 | if (status < 0) { |
| 826 | mutex_unlock(&alloc_inode->i_mutex); |
| 827 | iput(alloc_inode); |
| 828 | |
| 829 | mlog_errno(status); |
| 830 | return status; |
| 831 | } |
| 832 | |
| 833 | ac->ac_inode = alloc_inode; |
Tao Ma | a4a4891 | 2008-03-03 17:12:30 +0800 | [diff] [blame] | 834 | ac->ac_alloc_slot = slot; |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 835 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 836 | fe = (struct ocfs2_dinode *) bh->b_data; |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 837 | |
| 838 | /* The bh was validated by the inode read inside |
| 839 | * ocfs2_inode_lock(). Any corruption is a code bug. */ |
| 840 | BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); |
| 841 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 842 | if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 843 | ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu", |
| 844 | (unsigned long long)le64_to_cpu(fe->i_blkno)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 845 | status = -EIO; |
| 846 | goto bail; |
| 847 | } |
| 848 | |
| 849 | free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) - |
| 850 | le32_to_cpu(fe->id1.bitmap1.i_used); |
| 851 | |
| 852 | if (bits_wanted > free_bits) { |
| 853 | /* cluster bitmap never grows */ |
| 854 | if (ocfs2_is_cluster_bitmap(alloc_inode)) { |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 855 | trace_ocfs2_reserve_suballoc_bits_nospc(bits_wanted, |
| 856 | free_bits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 857 | status = -ENOSPC; |
| 858 | goto bail; |
| 859 | } |
| 860 | |
Tao Ma | 60ca81e | 2009-02-25 00:53:24 +0800 | [diff] [blame] | 861 | if (!(flags & ALLOC_NEW_GROUP)) { |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 862 | trace_ocfs2_reserve_suballoc_bits_no_new_group( |
| 863 | slot, bits_wanted, free_bits); |
Tao Ma | ffda89a | 2008-03-03 17:12:09 +0800 | [diff] [blame] | 864 | status = -ENOSPC; |
| 865 | goto bail; |
| 866 | } |
| 867 | |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 868 | status = ocfs2_block_group_alloc(osb, alloc_inode, bh, |
Tao Ma | feb473a | 2009-02-25 00:53:25 +0800 | [diff] [blame] | 869 | ac->ac_max_block, |
| 870 | last_alloc_group, flags); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 871 | if (status < 0) { |
| 872 | if (status != -ENOSPC) |
| 873 | mlog_errno(status); |
| 874 | goto bail; |
| 875 | } |
| 876 | atomic_inc(&osb->alloc_stats.bg_extends); |
| 877 | |
| 878 | /* You should never ask for this much metadata */ |
| 879 | BUG_ON(bits_wanted > |
| 880 | (le32_to_cpu(fe->id1.bitmap1.i_total) |
| 881 | - le32_to_cpu(fe->id1.bitmap1.i_used))); |
| 882 | } |
| 883 | |
| 884 | get_bh(bh); |
| 885 | ac->ac_bh = bh; |
| 886 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 887 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 888 | |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 889 | if (status) |
| 890 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 891 | return status; |
| 892 | } |
| 893 | |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 894 | static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb) |
| 895 | { |
| 896 | spin_lock(&osb->osb_lock); |
| 897 | osb->s_inode_steal_slot = OCFS2_INVALID_SLOT; |
| 898 | spin_unlock(&osb->osb_lock); |
| 899 | atomic_set(&osb->s_num_inodes_stolen, 0); |
| 900 | } |
| 901 | |
| 902 | static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb) |
| 903 | { |
| 904 | spin_lock(&osb->osb_lock); |
| 905 | osb->s_meta_steal_slot = OCFS2_INVALID_SLOT; |
| 906 | spin_unlock(&osb->osb_lock); |
| 907 | atomic_set(&osb->s_num_meta_stolen, 0); |
| 908 | } |
| 909 | |
| 910 | void ocfs2_init_steal_slots(struct ocfs2_super *osb) |
| 911 | { |
| 912 | ocfs2_init_inode_steal_slot(osb); |
| 913 | ocfs2_init_meta_steal_slot(osb); |
| 914 | } |
| 915 | |
| 916 | static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type) |
| 917 | { |
| 918 | spin_lock(&osb->osb_lock); |
| 919 | if (type == INODE_ALLOC_SYSTEM_INODE) |
| 920 | osb->s_inode_steal_slot = slot; |
| 921 | else if (type == EXTENT_ALLOC_SYSTEM_INODE) |
| 922 | osb->s_meta_steal_slot = slot; |
| 923 | spin_unlock(&osb->osb_lock); |
| 924 | } |
| 925 | |
| 926 | static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type) |
| 927 | { |
| 928 | int slot = OCFS2_INVALID_SLOT; |
| 929 | |
| 930 | spin_lock(&osb->osb_lock); |
| 931 | if (type == INODE_ALLOC_SYSTEM_INODE) |
| 932 | slot = osb->s_inode_steal_slot; |
| 933 | else if (type == EXTENT_ALLOC_SYSTEM_INODE) |
| 934 | slot = osb->s_meta_steal_slot; |
| 935 | spin_unlock(&osb->osb_lock); |
| 936 | |
| 937 | return slot; |
| 938 | } |
| 939 | |
| 940 | static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb) |
| 941 | { |
| 942 | return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE); |
| 943 | } |
| 944 | |
| 945 | static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb) |
| 946 | { |
| 947 | return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE); |
| 948 | } |
| 949 | |
| 950 | static int ocfs2_steal_resource(struct ocfs2_super *osb, |
| 951 | struct ocfs2_alloc_context *ac, |
| 952 | int type) |
| 953 | { |
| 954 | int i, status = -ENOSPC; |
| 955 | int slot = __ocfs2_get_steal_slot(osb, type); |
| 956 | |
| 957 | /* Start to steal resource from the first slot after ours. */ |
| 958 | if (slot == OCFS2_INVALID_SLOT) |
| 959 | slot = osb->slot_num + 1; |
| 960 | |
| 961 | for (i = 0; i < osb->max_slots; i++, slot++) { |
| 962 | if (slot == osb->max_slots) |
| 963 | slot = 0; |
| 964 | |
| 965 | if (slot == osb->slot_num) |
| 966 | continue; |
| 967 | |
| 968 | status = ocfs2_reserve_suballoc_bits(osb, ac, |
| 969 | type, |
| 970 | (u32)slot, NULL, |
| 971 | NOT_ALLOC_NEW_GROUP); |
| 972 | if (status >= 0) { |
| 973 | __ocfs2_set_steal_slot(osb, slot, type); |
| 974 | break; |
| 975 | } |
| 976 | |
| 977 | ocfs2_free_ac_resource(ac); |
| 978 | } |
| 979 | |
| 980 | return status; |
| 981 | } |
| 982 | |
| 983 | static int ocfs2_steal_inode(struct ocfs2_super *osb, |
| 984 | struct ocfs2_alloc_context *ac) |
| 985 | { |
| 986 | return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE); |
| 987 | } |
| 988 | |
| 989 | static int ocfs2_steal_meta(struct ocfs2_super *osb, |
| 990 | struct ocfs2_alloc_context *ac) |
| 991 | { |
| 992 | return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE); |
| 993 | } |
| 994 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 995 | int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb, |
| 996 | int blocks, |
| 997 | struct ocfs2_alloc_context **ac) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 998 | { |
| 999 | int status; |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 1000 | int slot = ocfs2_get_meta_steal_slot(osb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1001 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 1002 | *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1003 | if (!(*ac)) { |
| 1004 | status = -ENOMEM; |
| 1005 | mlog_errno(status); |
| 1006 | goto bail; |
| 1007 | } |
| 1008 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1009 | (*ac)->ac_bits_wanted = blocks; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1010 | (*ac)->ac_which = OCFS2_AC_USE_META; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1011 | (*ac)->ac_group_search = ocfs2_block_group_search; |
| 1012 | |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 1013 | if (slot != OCFS2_INVALID_SLOT && |
| 1014 | atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL) |
| 1015 | goto extent_steal; |
| 1016 | |
| 1017 | atomic_set(&osb->s_num_meta_stolen, 0); |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1018 | status = ocfs2_reserve_suballoc_bits(osb, (*ac), |
Tao Ma | ffda89a | 2008-03-03 17:12:09 +0800 | [diff] [blame] | 1019 | EXTENT_ALLOC_SYSTEM_INODE, |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 1020 | (u32)osb->slot_num, NULL, |
Mark Fasheh | 33d5d38 | 2010-02-24 13:34:09 -0800 | [diff] [blame] | 1021 | ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP); |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 1022 | |
| 1023 | |
| 1024 | if (status >= 0) { |
| 1025 | status = 0; |
| 1026 | if (slot != OCFS2_INVALID_SLOT) |
| 1027 | ocfs2_init_meta_steal_slot(osb); |
| 1028 | goto bail; |
| 1029 | } else if (status < 0 && status != -ENOSPC) { |
| 1030 | mlog_errno(status); |
| 1031 | goto bail; |
| 1032 | } |
| 1033 | |
| 1034 | ocfs2_free_ac_resource(*ac); |
| 1035 | |
| 1036 | extent_steal: |
| 1037 | status = ocfs2_steal_meta(osb, *ac); |
| 1038 | atomic_inc(&osb->s_num_meta_stolen); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1039 | if (status < 0) { |
| 1040 | if (status != -ENOSPC) |
| 1041 | mlog_errno(status); |
| 1042 | goto bail; |
| 1043 | } |
| 1044 | |
| 1045 | status = 0; |
| 1046 | bail: |
| 1047 | if ((status < 0) && *ac) { |
| 1048 | ocfs2_free_alloc_context(*ac); |
| 1049 | *ac = NULL; |
| 1050 | } |
| 1051 | |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 1052 | if (status) |
| 1053 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1054 | return status; |
| 1055 | } |
| 1056 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1057 | int ocfs2_reserve_new_metadata(struct ocfs2_super *osb, |
| 1058 | struct ocfs2_extent_list *root_el, |
| 1059 | struct ocfs2_alloc_context **ac) |
| 1060 | { |
| 1061 | return ocfs2_reserve_new_metadata_blocks(osb, |
| 1062 | ocfs2_extend_meta_needed(root_el), |
| 1063 | ac); |
| 1064 | } |
| 1065 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1066 | int ocfs2_reserve_new_inode(struct ocfs2_super *osb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1067 | struct ocfs2_alloc_context **ac) |
| 1068 | { |
| 1069 | int status; |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 1070 | int slot = ocfs2_get_inode_steal_slot(osb); |
Tao Ma | feb473a | 2009-02-25 00:53:25 +0800 | [diff] [blame] | 1071 | u64 alloc_group; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1072 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 1073 | *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1074 | if (!(*ac)) { |
| 1075 | status = -ENOMEM; |
| 1076 | mlog_errno(status); |
| 1077 | goto bail; |
| 1078 | } |
| 1079 | |
| 1080 | (*ac)->ac_bits_wanted = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1081 | (*ac)->ac_which = OCFS2_AC_USE_INODE; |
| 1082 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1083 | (*ac)->ac_group_search = ocfs2_block_group_search; |
| 1084 | |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 1085 | /* |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1086 | * stat(2) can't handle i_ino > 32bits, so we tell the |
| 1087 | * lower levels not to allocate us a block group past that |
Joel Becker | 12462f1 | 2008-09-03 20:03:40 -0700 | [diff] [blame] | 1088 | * limit. The 'inode64' mount option avoids this behavior. |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1089 | */ |
Joel Becker | 12462f1 | 2008-09-03 20:03:40 -0700 | [diff] [blame] | 1090 | if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64)) |
| 1091 | (*ac)->ac_max_block = (u32)~0U; |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1092 | |
| 1093 | /* |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 1094 | * slot is set when we successfully steal inode from other nodes. |
| 1095 | * It is reset in 3 places: |
| 1096 | * 1. when we flush the truncate log |
| 1097 | * 2. when we complete local alloc recovery. |
| 1098 | * 3. when we successfully allocate from our own slot. |
| 1099 | * After it is set, we will go on stealing inodes until we find the |
| 1100 | * need to check our slots to see whether there is some space for us. |
| 1101 | */ |
| 1102 | if (slot != OCFS2_INVALID_SLOT && |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 1103 | atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL) |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 1104 | goto inode_steal; |
| 1105 | |
| 1106 | atomic_set(&osb->s_num_inodes_stolen, 0); |
Tao Ma | feb473a | 2009-02-25 00:53:25 +0800 | [diff] [blame] | 1107 | alloc_group = osb->osb_inode_alloc_group; |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1108 | status = ocfs2_reserve_suballoc_bits(osb, *ac, |
| 1109 | INODE_ALLOC_SYSTEM_INODE, |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 1110 | (u32)osb->slot_num, |
Tao Ma | feb473a | 2009-02-25 00:53:25 +0800 | [diff] [blame] | 1111 | &alloc_group, |
Tao Ma | 60ca81e | 2009-02-25 00:53:24 +0800 | [diff] [blame] | 1112 | ALLOC_NEW_GROUP | |
| 1113 | ALLOC_GROUPS_FROM_GLOBAL); |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 1114 | if (status >= 0) { |
| 1115 | status = 0; |
| 1116 | |
Tao Ma | feb473a | 2009-02-25 00:53:25 +0800 | [diff] [blame] | 1117 | spin_lock(&osb->osb_lock); |
| 1118 | osb->osb_inode_alloc_group = alloc_group; |
| 1119 | spin_unlock(&osb->osb_lock); |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 1120 | trace_ocfs2_reserve_new_inode_new_group( |
| 1121 | (unsigned long long)alloc_group); |
Tao Ma | feb473a | 2009-02-25 00:53:25 +0800 | [diff] [blame] | 1122 | |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 1123 | /* |
| 1124 | * Some inodes must be freed by us, so try to allocate |
| 1125 | * from our own next time. |
| 1126 | */ |
| 1127 | if (slot != OCFS2_INVALID_SLOT) |
| 1128 | ocfs2_init_inode_steal_slot(osb); |
| 1129 | goto bail; |
| 1130 | } else if (status < 0 && status != -ENOSPC) { |
| 1131 | mlog_errno(status); |
| 1132 | goto bail; |
| 1133 | } |
| 1134 | |
| 1135 | ocfs2_free_ac_resource(*ac); |
| 1136 | |
| 1137 | inode_steal: |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 1138 | status = ocfs2_steal_inode(osb, *ac); |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 1139 | atomic_inc(&osb->s_num_inodes_stolen); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1140 | if (status < 0) { |
| 1141 | if (status != -ENOSPC) |
| 1142 | mlog_errno(status); |
| 1143 | goto bail; |
| 1144 | } |
| 1145 | |
| 1146 | status = 0; |
| 1147 | bail: |
| 1148 | if ((status < 0) && *ac) { |
| 1149 | ocfs2_free_alloc_context(*ac); |
| 1150 | *ac = NULL; |
| 1151 | } |
| 1152 | |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 1153 | if (status) |
| 1154 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1155 | return status; |
| 1156 | } |
| 1157 | |
| 1158 | /* local alloc code has to do the same thing, so rather than do this |
| 1159 | * twice.. */ |
| 1160 | int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb, |
| 1161 | struct ocfs2_alloc_context *ac) |
| 1162 | { |
| 1163 | int status; |
| 1164 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1165 | ac->ac_which = OCFS2_AC_USE_MAIN; |
| 1166 | ac->ac_group_search = ocfs2_cluster_group_search; |
| 1167 | |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1168 | status = ocfs2_reserve_suballoc_bits(osb, ac, |
| 1169 | GLOBAL_BITMAP_SYSTEM_INODE, |
Tao Ma | feb473a | 2009-02-25 00:53:25 +0800 | [diff] [blame] | 1170 | OCFS2_INVALID_SLOT, NULL, |
Tao Ma | ffda89a | 2008-03-03 17:12:09 +0800 | [diff] [blame] | 1171 | ALLOC_NEW_GROUP); |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1172 | if (status < 0 && status != -ENOSPC) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1173 | mlog_errno(status); |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1174 | goto bail; |
| 1175 | } |
| 1176 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1177 | bail: |
| 1178 | return status; |
| 1179 | } |
| 1180 | |
| 1181 | /* Callers don't need to care which bitmap (local alloc or main) to |
| 1182 | * use so we figure it out for them, but unfortunately this clutters |
| 1183 | * things a bit. */ |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1184 | static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb, |
| 1185 | u32 bits_wanted, u64 max_block, |
Tao Ma | 60ca81e | 2009-02-25 00:53:24 +0800 | [diff] [blame] | 1186 | int flags, |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1187 | struct ocfs2_alloc_context **ac) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1188 | { |
| 1189 | int status; |
| 1190 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 1191 | *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1192 | if (!(*ac)) { |
| 1193 | status = -ENOMEM; |
| 1194 | mlog_errno(status); |
| 1195 | goto bail; |
| 1196 | } |
| 1197 | |
| 1198 | (*ac)->ac_bits_wanted = bits_wanted; |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1199 | (*ac)->ac_max_block = max_block; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1200 | |
| 1201 | status = -ENOSPC; |
Tao Ma | 60ca81e | 2009-02-25 00:53:24 +0800 | [diff] [blame] | 1202 | if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) && |
| 1203 | ocfs2_alloc_should_use_local(osb, bits_wanted)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1204 | status = ocfs2_reserve_local_alloc_bits(osb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1205 | bits_wanted, |
| 1206 | *ac); |
Mark Fasheh | a57c8fd | 2010-03-16 21:01:00 -0700 | [diff] [blame] | 1207 | if ((status < 0) && (status != -ENOSPC)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1208 | mlog_errno(status); |
| 1209 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | if (status == -ENOSPC) { |
| 1214 | status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac); |
| 1215 | if (status < 0) { |
| 1216 | if (status != -ENOSPC) |
| 1217 | mlog_errno(status); |
| 1218 | goto bail; |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | status = 0; |
| 1223 | bail: |
| 1224 | if ((status < 0) && *ac) { |
| 1225 | ocfs2_free_alloc_context(*ac); |
| 1226 | *ac = NULL; |
| 1227 | } |
| 1228 | |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 1229 | if (status) |
| 1230 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1231 | return status; |
| 1232 | } |
| 1233 | |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1234 | int ocfs2_reserve_clusters(struct ocfs2_super *osb, |
| 1235 | u32 bits_wanted, |
| 1236 | struct ocfs2_alloc_context **ac) |
| 1237 | { |
Tao Ma | 60ca81e | 2009-02-25 00:53:24 +0800 | [diff] [blame] | 1238 | return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0, |
| 1239 | ALLOC_NEW_GROUP, ac); |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1242 | /* |
| 1243 | * More or less lifted from ext3. I'll leave their description below: |
| 1244 | * |
| 1245 | * "For ext3 allocations, we must not reuse any blocks which are |
| 1246 | * allocated in the bitmap buffer's "last committed data" copy. This |
| 1247 | * prevents deletes from freeing up the page for reuse until we have |
| 1248 | * committed the delete transaction. |
| 1249 | * |
| 1250 | * If we didn't do this, then deleting something and reallocating it as |
| 1251 | * data would allow the old block to be overwritten before the |
| 1252 | * transaction committed (because we force data to disk before commit). |
| 1253 | * This would lead to corruption if we crashed between overwriting the |
| 1254 | * data and committing the delete. |
| 1255 | * |
| 1256 | * @@@ We may want to make this allocation behaviour conditional on |
| 1257 | * data-writes at some point, and disable it for metadata allocations or |
| 1258 | * sync-data inodes." |
| 1259 | * |
| 1260 | * Note: OCFS2 already does this differently for metadata vs data |
Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 1261 | * allocations, as those bitmaps are separate and undo access is never |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1262 | * called on a metadata group descriptor. |
| 1263 | */ |
| 1264 | static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh, |
| 1265 | int nr) |
| 1266 | { |
| 1267 | struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; |
Sunil Mushran | 94e41ec | 2009-06-19 14:45:54 -0700 | [diff] [blame] | 1268 | int ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1269 | |
| 1270 | if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap)) |
| 1271 | return 0; |
Sunil Mushran | 94e41ec | 2009-06-19 14:45:54 -0700 | [diff] [blame] | 1272 | |
| 1273 | if (!buffer_jbd(bg_bh)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1274 | return 1; |
| 1275 | |
Sunil Mushran | 94e41ec | 2009-06-19 14:45:54 -0700 | [diff] [blame] | 1276 | jbd_lock_bh_state(bg_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1277 | bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data; |
Sunil Mushran | 94e41ec | 2009-06-19 14:45:54 -0700 | [diff] [blame] | 1278 | if (bg) |
| 1279 | ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap); |
| 1280 | else |
| 1281 | ret = 1; |
| 1282 | jbd_unlock_bh_state(bg_bh); |
| 1283 | |
| 1284 | return ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb, |
| 1288 | struct buffer_head *bg_bh, |
| 1289 | unsigned int bits_wanted, |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 1290 | unsigned int total_bits, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1291 | struct ocfs2_suballoc_result *res) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1292 | { |
| 1293 | void *bitmap; |
| 1294 | u16 best_offset, best_size; |
| 1295 | int offset, start, found, status = 0; |
| 1296 | struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; |
| 1297 | |
Joel Becker | 4203530 | 2008-11-13 14:49:15 -0800 | [diff] [blame] | 1298 | /* Callers got this descriptor from |
| 1299 | * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ |
| 1300 | BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1301 | |
| 1302 | found = start = best_offset = best_size = 0; |
| 1303 | bitmap = bg->bg_bitmap; |
| 1304 | |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 1305 | while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) { |
| 1306 | if (offset == total_bits) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1307 | break; |
| 1308 | |
| 1309 | if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) { |
| 1310 | /* We found a zero, but we can't use it as it |
| 1311 | * hasn't been put to disk yet! */ |
| 1312 | found = 0; |
| 1313 | start = offset + 1; |
| 1314 | } else if (offset == start) { |
| 1315 | /* we found a zero */ |
| 1316 | found++; |
| 1317 | /* move start to the next bit to test */ |
| 1318 | start++; |
| 1319 | } else { |
| 1320 | /* got a zero after some ones */ |
| 1321 | found = 1; |
| 1322 | start = offset + 1; |
| 1323 | } |
| 1324 | if (found > best_size) { |
| 1325 | best_size = found; |
| 1326 | best_offset = start - found; |
| 1327 | } |
| 1328 | /* we got everything we needed */ |
| 1329 | if (found == bits_wanted) { |
| 1330 | /* mlog(0, "Found it all!\n"); */ |
| 1331 | break; |
| 1332 | } |
| 1333 | } |
| 1334 | |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1335 | if (best_size) { |
| 1336 | res->sr_bit_offset = best_offset; |
| 1337 | res->sr_bits = best_size; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1338 | } else { |
| 1339 | status = -ENOSPC; |
| 1340 | /* No error log here -- see the comment above |
| 1341 | * ocfs2_test_bg_bit_allocatable */ |
| 1342 | } |
| 1343 | |
| 1344 | return status; |
| 1345 | } |
| 1346 | |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1347 | static inline int ocfs2_block_group_set_bits(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1348 | struct inode *alloc_inode, |
| 1349 | struct ocfs2_group_desc *bg, |
| 1350 | struct buffer_head *group_bh, |
| 1351 | unsigned int bit_off, |
| 1352 | unsigned int num_bits) |
| 1353 | { |
| 1354 | int status; |
| 1355 | void *bitmap = bg->bg_bitmap; |
| 1356 | int journal_type = OCFS2_JOURNAL_ACCESS_WRITE; |
| 1357 | |
Joel Becker | 4203530 | 2008-11-13 14:49:15 -0800 | [diff] [blame] | 1358 | /* All callers get the descriptor via |
| 1359 | * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ |
| 1360 | BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1361 | BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits); |
| 1362 | |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 1363 | trace_ocfs2_block_group_set_bits(bit_off, num_bits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1364 | |
| 1365 | if (ocfs2_is_cluster_bitmap(alloc_inode)) |
| 1366 | journal_type = OCFS2_JOURNAL_ACCESS_UNDO; |
| 1367 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1368 | status = ocfs2_journal_access_gd(handle, |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1369 | INODE_CACHE(alloc_inode), |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1370 | group_bh, |
| 1371 | journal_type); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1372 | if (status < 0) { |
| 1373 | mlog_errno(status); |
| 1374 | goto bail; |
| 1375 | } |
| 1376 | |
| 1377 | le16_add_cpu(&bg->bg_free_bits_count, -num_bits); |
Srinivas Eeda | 9b5cd10 | 2010-10-05 15:53:06 -0700 | [diff] [blame] | 1378 | if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) { |
| 1379 | ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit" |
| 1380 | " count %u but claims %u are freed. num_bits %d", |
| 1381 | (unsigned long long)le64_to_cpu(bg->bg_blkno), |
| 1382 | le16_to_cpu(bg->bg_bits), |
| 1383 | le16_to_cpu(bg->bg_free_bits_count), num_bits); |
| 1384 | return -EROFS; |
| 1385 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1386 | while(num_bits--) |
| 1387 | ocfs2_set_bit(bit_off++, bitmap); |
| 1388 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1389 | ocfs2_journal_dirty(handle, group_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1390 | |
| 1391 | bail: |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 1392 | if (status) |
| 1393 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1394 | return status; |
| 1395 | } |
| 1396 | |
| 1397 | /* find the one with the most empty bits */ |
| 1398 | static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl) |
| 1399 | { |
| 1400 | u16 curr, best; |
| 1401 | |
| 1402 | BUG_ON(!cl->cl_next_free_rec); |
| 1403 | |
| 1404 | best = curr = 0; |
| 1405 | while (curr < le16_to_cpu(cl->cl_next_free_rec)) { |
| 1406 | if (le32_to_cpu(cl->cl_recs[curr].c_free) > |
| 1407 | le32_to_cpu(cl->cl_recs[best].c_free)) |
| 1408 | best = curr; |
| 1409 | curr++; |
| 1410 | } |
| 1411 | |
| 1412 | BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec)); |
| 1413 | return best; |
| 1414 | } |
| 1415 | |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1416 | static int ocfs2_relink_block_group(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1417 | struct inode *alloc_inode, |
| 1418 | struct buffer_head *fe_bh, |
| 1419 | struct buffer_head *bg_bh, |
| 1420 | struct buffer_head *prev_bg_bh, |
| 1421 | u16 chain) |
| 1422 | { |
| 1423 | int status; |
| 1424 | /* there is a really tiny chance the journal calls could fail, |
| 1425 | * but we wouldn't want inconsistent blocks in *any* case. */ |
| 1426 | u64 fe_ptr, bg_ptr, prev_bg_ptr; |
| 1427 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 1428 | struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; |
| 1429 | struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data; |
| 1430 | |
Joel Becker | 4203530 | 2008-11-13 14:49:15 -0800 | [diff] [blame] | 1431 | /* The caller got these descriptors from |
| 1432 | * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ |
| 1433 | BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); |
| 1434 | BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1435 | |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 1436 | trace_ocfs2_relink_block_group( |
| 1437 | (unsigned long long)le64_to_cpu(fe->i_blkno), chain, |
| 1438 | (unsigned long long)le64_to_cpu(bg->bg_blkno), |
| 1439 | (unsigned long long)le64_to_cpu(prev_bg->bg_blkno)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1440 | |
| 1441 | fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno); |
| 1442 | bg_ptr = le64_to_cpu(bg->bg_next_group); |
| 1443 | prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group); |
| 1444 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1445 | status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), |
| 1446 | prev_bg_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1447 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1448 | if (status < 0) { |
| 1449 | mlog_errno(status); |
| 1450 | goto out_rollback; |
| 1451 | } |
| 1452 | |
| 1453 | prev_bg->bg_next_group = bg->bg_next_group; |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1454 | ocfs2_journal_dirty(handle, prev_bg_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1455 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1456 | status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), |
| 1457 | bg_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1458 | if (status < 0) { |
| 1459 | mlog_errno(status); |
| 1460 | goto out_rollback; |
| 1461 | } |
| 1462 | |
| 1463 | bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno; |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1464 | ocfs2_journal_dirty(handle, bg_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1465 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1466 | status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), |
| 1467 | fe_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1468 | if (status < 0) { |
| 1469 | mlog_errno(status); |
| 1470 | goto out_rollback; |
| 1471 | } |
| 1472 | |
| 1473 | fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno; |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1474 | ocfs2_journal_dirty(handle, fe_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1475 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1476 | out_rollback: |
| 1477 | if (status < 0) { |
| 1478 | fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr); |
| 1479 | bg->bg_next_group = cpu_to_le64(bg_ptr); |
| 1480 | prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr); |
| 1481 | } |
Joel Becker | 4203530 | 2008-11-13 14:49:15 -0800 | [diff] [blame] | 1482 | |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 1483 | if (status) |
| 1484 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1485 | return status; |
| 1486 | } |
| 1487 | |
| 1488 | static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg, |
| 1489 | u32 wanted) |
| 1490 | { |
| 1491 | return le16_to_cpu(bg->bg_free_bits_count) > wanted; |
| 1492 | } |
| 1493 | |
| 1494 | /* return 0 on success, -ENOSPC to keep searching and any other < 0 |
| 1495 | * value on error. */ |
| 1496 | static int ocfs2_cluster_group_search(struct inode *inode, |
| 1497 | struct buffer_head *group_bh, |
| 1498 | u32 bits_wanted, u32 min_bits, |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1499 | u64 max_block, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1500 | struct ocfs2_suballoc_result *res) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1501 | { |
| 1502 | int search = -ENOSPC; |
| 1503 | int ret; |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1504 | u64 blkoff; |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 1505 | struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data; |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 1506 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 1507 | unsigned int max_bits, gd_cluster_off; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1508 | |
| 1509 | BUG_ON(!ocfs2_is_cluster_bitmap(inode)); |
| 1510 | |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 1511 | if (gd->bg_free_bits_count) { |
| 1512 | max_bits = le16_to_cpu(gd->bg_bits); |
| 1513 | |
| 1514 | /* Tail groups in cluster bitmaps which aren't cpg |
| 1515 | * aligned are prone to partial extention by a failed |
| 1516 | * fs resize. If the file system resize never got to |
| 1517 | * update the dinode cluster count, then we don't want |
| 1518 | * to trust any clusters past it, regardless of what |
| 1519 | * the group descriptor says. */ |
| 1520 | gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb, |
| 1521 | le64_to_cpu(gd->bg_blkno)); |
| 1522 | if ((gd_cluster_off + max_bits) > |
| 1523 | OCFS2_I(inode)->ip_clusters) { |
| 1524 | max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off; |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 1525 | trace_ocfs2_cluster_group_search_wrong_max_bits( |
| 1526 | (unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 1527 | le16_to_cpu(gd->bg_bits), |
| 1528 | OCFS2_I(inode)->ip_clusters, max_bits); |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1531 | ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb), |
| 1532 | group_bh, bits_wanted, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1533 | max_bits, res); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1534 | if (ret) |
| 1535 | return ret; |
| 1536 | |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1537 | if (max_block) { |
| 1538 | blkoff = ocfs2_clusters_to_blocks(inode->i_sb, |
| 1539 | gd_cluster_off + |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1540 | res->sr_bit_offset + |
| 1541 | res->sr_bits); |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 1542 | trace_ocfs2_cluster_group_search_max_block( |
| 1543 | (unsigned long long)blkoff, |
| 1544 | (unsigned long long)max_block); |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1545 | if (blkoff > max_block) |
| 1546 | return -ENOSPC; |
| 1547 | } |
| 1548 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1549 | /* ocfs2_block_group_find_clear_bits() might |
| 1550 | * return success, but we still want to return |
| 1551 | * -ENOSPC unless it found the minimum number |
| 1552 | * of bits. */ |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1553 | if (min_bits <= res->sr_bits) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1554 | search = 0; /* success */ |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1555 | else if (res->sr_bits) { |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 1556 | /* |
| 1557 | * Don't show bits which we'll be returning |
| 1558 | * for allocation to the local alloc bitmap. |
| 1559 | */ |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1560 | ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | return search; |
| 1565 | } |
| 1566 | |
| 1567 | static int ocfs2_block_group_search(struct inode *inode, |
| 1568 | struct buffer_head *group_bh, |
| 1569 | u32 bits_wanted, u32 min_bits, |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1570 | u64 max_block, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1571 | struct ocfs2_suballoc_result *res) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1572 | { |
| 1573 | int ret = -ENOSPC; |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1574 | u64 blkoff; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1575 | struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data; |
| 1576 | |
| 1577 | BUG_ON(min_bits != 1); |
| 1578 | BUG_ON(ocfs2_is_cluster_bitmap(inode)); |
| 1579 | |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1580 | if (bg->bg_free_bits_count) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1581 | ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb), |
| 1582 | group_bh, bits_wanted, |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 1583 | le16_to_cpu(bg->bg_bits), |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1584 | res); |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1585 | if (!ret && max_block) { |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1586 | blkoff = le64_to_cpu(bg->bg_blkno) + |
| 1587 | res->sr_bit_offset + res->sr_bits; |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 1588 | trace_ocfs2_block_group_search_max_block( |
| 1589 | (unsigned long long)blkoff, |
| 1590 | (unsigned long long)max_block); |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1591 | if (blkoff > max_block) |
| 1592 | ret = -ENOSPC; |
| 1593 | } |
| 1594 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1595 | |
| 1596 | return ret; |
| 1597 | } |
| 1598 | |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1599 | static int ocfs2_alloc_dinode_update_counts(struct inode *inode, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1600 | handle_t *handle, |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1601 | struct buffer_head *di_bh, |
| 1602 | u32 num_bits, |
| 1603 | u16 chain) |
| 1604 | { |
| 1605 | int ret; |
| 1606 | u32 tmp_used; |
| 1607 | struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; |
| 1608 | struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain; |
| 1609 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1610 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1611 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1612 | if (ret < 0) { |
| 1613 | mlog_errno(ret); |
| 1614 | goto out; |
| 1615 | } |
| 1616 | |
| 1617 | tmp_used = le32_to_cpu(di->id1.bitmap1.i_used); |
| 1618 | di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used); |
| 1619 | le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits); |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1620 | ocfs2_journal_dirty(handle, di_bh); |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1621 | |
| 1622 | out: |
| 1623 | return ret; |
| 1624 | } |
| 1625 | |
Joel Becker | ba20663 | 2010-03-26 10:08:59 +0800 | [diff] [blame] | 1626 | static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res, |
| 1627 | struct ocfs2_extent_rec *rec, |
| 1628 | struct ocfs2_chain_list *cl) |
Joel Becker | 13e434c | 2010-03-26 10:08:27 +0800 | [diff] [blame] | 1629 | { |
| 1630 | unsigned int bpc = le16_to_cpu(cl->cl_bpc); |
| 1631 | unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc; |
| 1632 | unsigned int bitcount = le32_to_cpu(rec->e_leaf_clusters) * bpc; |
| 1633 | |
| 1634 | if (res->sr_bit_offset < bitoff) |
| 1635 | return 0; |
| 1636 | if (res->sr_bit_offset >= (bitoff + bitcount)) |
| 1637 | return 0; |
Joel Becker | ba20663 | 2010-03-26 10:08:59 +0800 | [diff] [blame] | 1638 | res->sr_blkno = le64_to_cpu(rec->e_blkno) + |
| 1639 | (res->sr_bit_offset - bitoff); |
Joel Becker | 13e434c | 2010-03-26 10:08:27 +0800 | [diff] [blame] | 1640 | if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount)) |
| 1641 | res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset; |
| 1642 | return 1; |
| 1643 | } |
| 1644 | |
Joel Becker | ba20663 | 2010-03-26 10:08:59 +0800 | [diff] [blame] | 1645 | static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac, |
| 1646 | struct ocfs2_group_desc *bg, |
| 1647 | struct ocfs2_suballoc_result *res) |
Joel Becker | 13e434c | 2010-03-26 10:08:27 +0800 | [diff] [blame] | 1648 | { |
| 1649 | int i; |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 1650 | u64 bg_blkno = res->sr_bg_blkno; /* Save off */ |
Joel Becker | 13e434c | 2010-03-26 10:08:27 +0800 | [diff] [blame] | 1651 | struct ocfs2_extent_rec *rec; |
| 1652 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data; |
| 1653 | struct ocfs2_chain_list *cl = &di->id2.i_chain; |
| 1654 | |
Joel Becker | ba20663 | 2010-03-26 10:08:59 +0800 | [diff] [blame] | 1655 | if (ocfs2_is_cluster_bitmap(ac->ac_inode)) { |
| 1656 | res->sr_blkno = 0; |
Joel Becker | 13e434c | 2010-03-26 10:08:27 +0800 | [diff] [blame] | 1657 | return; |
Joel Becker | ba20663 | 2010-03-26 10:08:59 +0800 | [diff] [blame] | 1658 | } |
Joel Becker | 13e434c | 2010-03-26 10:08:27 +0800 | [diff] [blame] | 1659 | |
Joel Becker | ba20663 | 2010-03-26 10:08:59 +0800 | [diff] [blame] | 1660 | res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset; |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 1661 | res->sr_bg_blkno = 0; /* Clear it for contig block groups */ |
Tao Ma | 4711954 | 2010-04-22 14:09:15 +0800 | [diff] [blame] | 1662 | if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) || |
Joel Becker | ba20663 | 2010-03-26 10:08:59 +0800 | [diff] [blame] | 1663 | !bg->bg_list.l_next_free_rec) |
Joel Becker | 13e434c | 2010-03-26 10:08:27 +0800 | [diff] [blame] | 1664 | return; |
| 1665 | |
| 1666 | for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) { |
| 1667 | rec = &bg->bg_list.l_recs[i]; |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 1668 | if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) { |
| 1669 | res->sr_bg_blkno = bg_blkno; /* Restore */ |
Joel Becker | 13e434c | 2010-03-26 10:08:27 +0800 | [diff] [blame] | 1670 | break; |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 1671 | } |
Joel Becker | 13e434c | 2010-03-26 10:08:27 +0800 | [diff] [blame] | 1672 | } |
| 1673 | } |
| 1674 | |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1675 | static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1676 | handle_t *handle, |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1677 | u32 bits_wanted, |
| 1678 | u32 min_bits, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1679 | struct ocfs2_suballoc_result *res, |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1680 | u16 *bits_left) |
| 1681 | { |
| 1682 | int ret; |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1683 | struct buffer_head *group_bh = NULL; |
| 1684 | struct ocfs2_group_desc *gd; |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 1685 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data; |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1686 | struct inode *alloc_inode = ac->ac_inode; |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1687 | |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1688 | ret = ocfs2_read_group_descriptor(alloc_inode, di, |
| 1689 | res->sr_bg_blkno, &group_bh); |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1690 | if (ret < 0) { |
| 1691 | mlog_errno(ret); |
| 1692 | return ret; |
| 1693 | } |
| 1694 | |
| 1695 | gd = (struct ocfs2_group_desc *) group_bh->b_data; |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1696 | ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1697 | ac->ac_max_block, res); |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1698 | if (ret < 0) { |
| 1699 | if (ret != -ENOSPC) |
| 1700 | mlog_errno(ret); |
| 1701 | goto out; |
| 1702 | } |
| 1703 | |
Joel Becker | 13e434c | 2010-03-26 10:08:27 +0800 | [diff] [blame] | 1704 | if (!ret) |
Joel Becker | ba20663 | 2010-03-26 10:08:59 +0800 | [diff] [blame] | 1705 | ocfs2_bg_discontig_fix_result(ac, gd, res); |
Joel Becker | 13e434c | 2010-03-26 10:08:27 +0800 | [diff] [blame] | 1706 | |
Mark Fasheh | e49e276 | 2010-08-13 15:15:17 -0700 | [diff] [blame] | 1707 | /* |
| 1708 | * sr_bg_blkno might have been changed by |
| 1709 | * ocfs2_bg_discontig_fix_result |
| 1710 | */ |
| 1711 | res->sr_bg_stable_blkno = group_bh->b_blocknr; |
| 1712 | |
| 1713 | if (ac->ac_find_loc_only) |
| 1714 | goto out_loc_only; |
| 1715 | |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1716 | ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1717 | res->sr_bits, |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1718 | le16_to_cpu(gd->bg_chain)); |
| 1719 | if (ret < 0) { |
| 1720 | mlog_errno(ret); |
| 1721 | goto out; |
| 1722 | } |
| 1723 | |
| 1724 | ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1725 | res->sr_bit_offset, res->sr_bits); |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1726 | if (ret < 0) |
| 1727 | mlog_errno(ret); |
| 1728 | |
Mark Fasheh | e49e276 | 2010-08-13 15:15:17 -0700 | [diff] [blame] | 1729 | out_loc_only: |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1730 | *bits_left = le16_to_cpu(gd->bg_free_bits_count); |
| 1731 | |
| 1732 | out: |
| 1733 | brelse(group_bh); |
| 1734 | |
| 1735 | return ret; |
| 1736 | } |
| 1737 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1738 | static int ocfs2_search_chain(struct ocfs2_alloc_context *ac, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1739 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1740 | u32 bits_wanted, |
| 1741 | u32 min_bits, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1742 | struct ocfs2_suballoc_result *res, |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1743 | u16 *bits_left) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1744 | { |
| 1745 | int status; |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1746 | u16 chain; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1747 | u64 next_group; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1748 | struct inode *alloc_inode = ac->ac_inode; |
| 1749 | struct buffer_head *group_bh = NULL; |
| 1750 | struct buffer_head *prev_group_bh = NULL; |
| 1751 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data; |
| 1752 | struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain; |
| 1753 | struct ocfs2_group_desc *bg; |
| 1754 | |
| 1755 | chain = ac->ac_chain; |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 1756 | trace_ocfs2_search_chain_begin( |
| 1757 | (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, |
| 1758 | bits_wanted, chain); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1759 | |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 1760 | status = ocfs2_read_group_descriptor(alloc_inode, fe, |
| 1761 | le64_to_cpu(cl->cl_recs[chain].c_blkno), |
| 1762 | &group_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1763 | if (status < 0) { |
| 1764 | mlog_errno(status); |
| 1765 | goto bail; |
| 1766 | } |
| 1767 | bg = (struct ocfs2_group_desc *) group_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1768 | |
| 1769 | status = -ENOSPC; |
| 1770 | /* for now, the chain search is a bit simplistic. We just use |
| 1771 | * the 1st group with any empty bits. */ |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 1772 | while ((status = ac->ac_group_search(alloc_inode, group_bh, |
| 1773 | bits_wanted, min_bits, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1774 | ac->ac_max_block, |
| 1775 | res)) == -ENOSPC) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1776 | if (!bg->bg_next_group) |
| 1777 | break; |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1778 | |
| 1779 | brelse(prev_group_bh); |
| 1780 | prev_group_bh = NULL; |
| 1781 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1782 | next_group = le64_to_cpu(bg->bg_next_group); |
| 1783 | prev_group_bh = group_bh; |
| 1784 | group_bh = NULL; |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 1785 | status = ocfs2_read_group_descriptor(alloc_inode, fe, |
| 1786 | next_group, &group_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1787 | if (status < 0) { |
| 1788 | mlog_errno(status); |
| 1789 | goto bail; |
| 1790 | } |
| 1791 | bg = (struct ocfs2_group_desc *) group_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1792 | } |
| 1793 | if (status < 0) { |
| 1794 | if (status != -ENOSPC) |
| 1795 | mlog_errno(status); |
| 1796 | goto bail; |
| 1797 | } |
| 1798 | |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 1799 | trace_ocfs2_search_chain_succ( |
| 1800 | (unsigned long long)le64_to_cpu(bg->bg_blkno), res->sr_bits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1801 | |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1802 | res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1803 | |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1804 | BUG_ON(res->sr_bits == 0); |
Joel Becker | 13e434c | 2010-03-26 10:08:27 +0800 | [diff] [blame] | 1805 | if (!status) |
Joel Becker | ba20663 | 2010-03-26 10:08:59 +0800 | [diff] [blame] | 1806 | ocfs2_bg_discontig_fix_result(ac, bg, res); |
Joel Becker | 13e434c | 2010-03-26 10:08:27 +0800 | [diff] [blame] | 1807 | |
Mark Fasheh | e49e276 | 2010-08-13 15:15:17 -0700 | [diff] [blame] | 1808 | /* |
| 1809 | * sr_bg_blkno might have been changed by |
| 1810 | * ocfs2_bg_discontig_fix_result |
| 1811 | */ |
| 1812 | res->sr_bg_stable_blkno = group_bh->b_blocknr; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1813 | |
| 1814 | /* |
| 1815 | * Keep track of previous block descriptor read. When |
| 1816 | * we find a target, if we have read more than X |
| 1817 | * number of descriptors, and the target is reasonably |
| 1818 | * empty, relink him to top of his chain. |
| 1819 | * |
| 1820 | * We've read 0 extra blocks and only send one more to |
| 1821 | * the transaction, yet the next guy to search has a |
| 1822 | * much easier time. |
| 1823 | * |
| 1824 | * Do this *after* figuring out how many bits we're taking out |
| 1825 | * of our target group. |
| 1826 | */ |
| 1827 | if (ac->ac_allow_chain_relink && |
| 1828 | (prev_group_bh) && |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1829 | (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1830 | status = ocfs2_relink_block_group(handle, alloc_inode, |
| 1831 | ac->ac_bh, group_bh, |
| 1832 | prev_group_bh, chain); |
| 1833 | if (status < 0) { |
| 1834 | mlog_errno(status); |
| 1835 | goto bail; |
| 1836 | } |
| 1837 | } |
| 1838 | |
Mark Fasheh | e49e276 | 2010-08-13 15:15:17 -0700 | [diff] [blame] | 1839 | if (ac->ac_find_loc_only) |
| 1840 | goto out_loc_only; |
| 1841 | |
Mark Fasheh | d513498 | 2010-08-13 15:15:16 -0700 | [diff] [blame] | 1842 | status = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, |
| 1843 | ac->ac_bh, res->sr_bits, |
| 1844 | chain); |
| 1845 | if (status) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1846 | mlog_errno(status); |
| 1847 | goto bail; |
| 1848 | } |
| 1849 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1850 | status = ocfs2_block_group_set_bits(handle, |
| 1851 | alloc_inode, |
| 1852 | bg, |
| 1853 | group_bh, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1854 | res->sr_bit_offset, |
| 1855 | res->sr_bits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1856 | if (status < 0) { |
| 1857 | mlog_errno(status); |
| 1858 | goto bail; |
| 1859 | } |
| 1860 | |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 1861 | trace_ocfs2_search_chain_end( |
| 1862 | (unsigned long long)le64_to_cpu(fe->i_blkno), |
| 1863 | res->sr_bits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1864 | |
Mark Fasheh | e49e276 | 2010-08-13 15:15:17 -0700 | [diff] [blame] | 1865 | out_loc_only: |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1866 | *bits_left = le16_to_cpu(bg->bg_free_bits_count); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1867 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1868 | brelse(group_bh); |
| 1869 | brelse(prev_group_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1870 | |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 1871 | if (status) |
| 1872 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1873 | return status; |
| 1874 | } |
| 1875 | |
| 1876 | /* will give out up to bits_wanted contiguous bits. */ |
Joel Becker | aa8f8e9 | 2010-03-26 10:08:07 +0800 | [diff] [blame] | 1877 | static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1878 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1879 | u32 bits_wanted, |
| 1880 | u32 min_bits, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1881 | struct ocfs2_suballoc_result *res) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1882 | { |
| 1883 | int status; |
| 1884 | u16 victim, i; |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1885 | u16 bits_left = 0; |
Mark Fasheh | b2b6ebf | 2010-08-26 13:06:50 -0700 | [diff] [blame] | 1886 | u64 hint = ac->ac_last_group; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1887 | struct ocfs2_chain_list *cl; |
| 1888 | struct ocfs2_dinode *fe; |
| 1889 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1890 | BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted); |
| 1891 | BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given)); |
| 1892 | BUG_ON(!ac->ac_bh); |
| 1893 | |
| 1894 | fe = (struct ocfs2_dinode *) ac->ac_bh->b_data; |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 1895 | |
| 1896 | /* The bh was validated by the inode read during |
| 1897 | * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */ |
| 1898 | BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); |
| 1899 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1900 | if (le32_to_cpu(fe->id1.bitmap1.i_used) >= |
| 1901 | le32_to_cpu(fe->id1.bitmap1.i_total)) { |
Joel Becker | aa8f8e9 | 2010-03-26 10:08:07 +0800 | [diff] [blame] | 1902 | ocfs2_error(ac->ac_inode->i_sb, |
| 1903 | "Chain allocator dinode %llu has %u used " |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1904 | "bits but only %u total.", |
| 1905 | (unsigned long long)le64_to_cpu(fe->i_blkno), |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1906 | le32_to_cpu(fe->id1.bitmap1.i_used), |
| 1907 | le32_to_cpu(fe->id1.bitmap1.i_total)); |
| 1908 | status = -EIO; |
| 1909 | goto bail; |
| 1910 | } |
| 1911 | |
Mark Fasheh | b2b6ebf | 2010-08-26 13:06:50 -0700 | [diff] [blame] | 1912 | res->sr_bg_blkno = hint; |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1913 | if (res->sr_bg_blkno) { |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1914 | /* Attempt to short-circuit the usual search mechanism |
| 1915 | * by jumping straight to the most recently used |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 1916 | * allocation group. This helps us maintain some |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1917 | * contiguousness across allocations. */ |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1918 | status = ocfs2_search_one_group(ac, handle, bits_wanted, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1919 | min_bits, res, &bits_left); |
| 1920 | if (!status) |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1921 | goto set_hint; |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1922 | if (status < 0 && status != -ENOSPC) { |
| 1923 | mlog_errno(status); |
| 1924 | goto bail; |
| 1925 | } |
| 1926 | } |
| 1927 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1928 | cl = (struct ocfs2_chain_list *) &fe->id2.i_chain; |
| 1929 | |
| 1930 | victim = ocfs2_find_victim_chain(cl); |
| 1931 | ac->ac_chain = victim; |
| 1932 | ac->ac_allow_chain_relink = 1; |
| 1933 | |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1934 | status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, |
| 1935 | res, &bits_left); |
Mark Fasheh | b2b6ebf | 2010-08-26 13:06:50 -0700 | [diff] [blame] | 1936 | if (!status) { |
| 1937 | hint = ocfs2_group_from_res(res); |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1938 | goto set_hint; |
Mark Fasheh | b2b6ebf | 2010-08-26 13:06:50 -0700 | [diff] [blame] | 1939 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1940 | if (status < 0 && status != -ENOSPC) { |
| 1941 | mlog_errno(status); |
| 1942 | goto bail; |
| 1943 | } |
| 1944 | |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 1945 | trace_ocfs2_claim_suballoc_bits(victim); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1946 | |
| 1947 | /* If we didn't pick a good victim, then just default to |
| 1948 | * searching each chain in order. Don't allow chain relinking |
| 1949 | * because we only calculate enough journal credits for one |
| 1950 | * relink per alloc. */ |
| 1951 | ac->ac_allow_chain_relink = 0; |
| 1952 | for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) { |
| 1953 | if (i == victim) |
| 1954 | continue; |
| 1955 | if (!cl->cl_recs[i].c_free) |
| 1956 | continue; |
| 1957 | |
| 1958 | ac->ac_chain = i; |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1959 | status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 1960 | res, &bits_left); |
Mark Fasheh | b2b6ebf | 2010-08-26 13:06:50 -0700 | [diff] [blame] | 1961 | if (!status) { |
| 1962 | hint = ocfs2_group_from_res(res); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1963 | break; |
Mark Fasheh | b2b6ebf | 2010-08-26 13:06:50 -0700 | [diff] [blame] | 1964 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1965 | if (status < 0 && status != -ENOSPC) { |
| 1966 | mlog_errno(status); |
| 1967 | goto bail; |
| 1968 | } |
| 1969 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1970 | |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1971 | set_hint: |
| 1972 | if (status != -ENOSPC) { |
| 1973 | /* If the next search of this group is not likely to |
| 1974 | * yield a suitable extent, then we reset the last |
| 1975 | * group hint so as to not waste a disk read */ |
| 1976 | if (bits_left < min_bits) |
| 1977 | ac->ac_last_group = 0; |
| 1978 | else |
Mark Fasheh | b2b6ebf | 2010-08-26 13:06:50 -0700 | [diff] [blame] | 1979 | ac->ac_last_group = hint; |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | bail: |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 1983 | if (status) |
| 1984 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1985 | return status; |
| 1986 | } |
| 1987 | |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 1988 | int ocfs2_claim_metadata(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1989 | struct ocfs2_alloc_context *ac, |
| 1990 | u32 bits_wanted, |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 1991 | u64 *suballoc_loc, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1992 | u16 *suballoc_bit_start, |
| 1993 | unsigned int *num_bits, |
| 1994 | u64 *blkno_start) |
| 1995 | { |
| 1996 | int status; |
Joel Becker | ba20663 | 2010-03-26 10:08:59 +0800 | [diff] [blame] | 1997 | struct ocfs2_suballoc_result res = { .sr_blkno = 0, }; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1998 | |
| 1999 | BUG_ON(!ac); |
| 2000 | BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted)); |
| 2001 | BUG_ON(ac->ac_which != OCFS2_AC_USE_META); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2002 | |
Joel Becker | aa8f8e9 | 2010-03-26 10:08:07 +0800 | [diff] [blame] | 2003 | status = ocfs2_claim_suballoc_bits(ac, |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 2004 | handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2005 | bits_wanted, |
| 2006 | 1, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 2007 | &res); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2008 | if (status < 0) { |
| 2009 | mlog_errno(status); |
| 2010 | goto bail; |
| 2011 | } |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 2012 | atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2013 | |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 2014 | *suballoc_loc = res.sr_bg_blkno; |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 2015 | *suballoc_bit_start = res.sr_bit_offset; |
Joel Becker | ba20663 | 2010-03-26 10:08:59 +0800 | [diff] [blame] | 2016 | *blkno_start = res.sr_blkno; |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 2017 | ac->ac_bits_given += res.sr_bits; |
| 2018 | *num_bits = res.sr_bits; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2019 | status = 0; |
| 2020 | bail: |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 2021 | if (status) |
| 2022 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2023 | return status; |
| 2024 | } |
| 2025 | |
Tao Ma | 1382115 | 2009-02-25 00:53:23 +0800 | [diff] [blame] | 2026 | static void ocfs2_init_inode_ac_group(struct inode *dir, |
Tao Ma | abf1b3c | 2010-04-27 08:30:36 +0800 | [diff] [blame] | 2027 | struct buffer_head *parent_di_bh, |
Tao Ma | 1382115 | 2009-02-25 00:53:23 +0800 | [diff] [blame] | 2028 | struct ocfs2_alloc_context *ac) |
| 2029 | { |
Tao Ma | abf1b3c | 2010-04-27 08:30:36 +0800 | [diff] [blame] | 2030 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data; |
Tao Ma | 1382115 | 2009-02-25 00:53:23 +0800 | [diff] [blame] | 2031 | /* |
| 2032 | * Try to allocate inodes from some specific group. |
| 2033 | * |
| 2034 | * If the parent dir has recorded the last group used in allocation, |
| 2035 | * cool, use it. Otherwise if we try to allocate new inode from the |
| 2036 | * same slot the parent dir belongs to, use the same chunk. |
| 2037 | * |
| 2038 | * We are very careful here to avoid the mistake of setting |
| 2039 | * ac_last_group to a group descriptor from a different (unlocked) slot. |
| 2040 | */ |
| 2041 | if (OCFS2_I(dir)->ip_last_used_group && |
| 2042 | OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot) |
| 2043 | ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group; |
Tao Ma | abf1b3c | 2010-04-27 08:30:36 +0800 | [diff] [blame] | 2044 | else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) { |
| 2045 | if (di->i_suballoc_loc) |
| 2046 | ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc); |
| 2047 | else |
| 2048 | ac->ac_last_group = ocfs2_which_suballoc_group( |
| 2049 | le64_to_cpu(di->i_blkno), |
| 2050 | le16_to_cpu(di->i_suballoc_bit)); |
| 2051 | } |
Tao Ma | 1382115 | 2009-02-25 00:53:23 +0800 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | static inline void ocfs2_save_inode_ac_group(struct inode *dir, |
| 2055 | struct ocfs2_alloc_context *ac) |
| 2056 | { |
| 2057 | OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group; |
| 2058 | OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot; |
| 2059 | } |
| 2060 | |
Mark Fasheh | e49e276 | 2010-08-13 15:15:17 -0700 | [diff] [blame] | 2061 | int ocfs2_find_new_inode_loc(struct inode *dir, |
| 2062 | struct buffer_head *parent_fe_bh, |
| 2063 | struct ocfs2_alloc_context *ac, |
| 2064 | u64 *fe_blkno) |
| 2065 | { |
| 2066 | int ret; |
| 2067 | handle_t *handle = NULL; |
| 2068 | struct ocfs2_suballoc_result *res; |
| 2069 | |
| 2070 | BUG_ON(!ac); |
| 2071 | BUG_ON(ac->ac_bits_given != 0); |
| 2072 | BUG_ON(ac->ac_bits_wanted != 1); |
| 2073 | BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE); |
| 2074 | |
| 2075 | res = kzalloc(sizeof(*res), GFP_NOFS); |
| 2076 | if (res == NULL) { |
| 2077 | ret = -ENOMEM; |
| 2078 | mlog_errno(ret); |
| 2079 | goto out; |
| 2080 | } |
| 2081 | |
| 2082 | ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac); |
| 2083 | |
| 2084 | /* |
| 2085 | * The handle started here is for chain relink. Alternatively, |
| 2086 | * we could just disable relink for these calls. |
| 2087 | */ |
| 2088 | handle = ocfs2_start_trans(OCFS2_SB(dir->i_sb), OCFS2_SUBALLOC_ALLOC); |
| 2089 | if (IS_ERR(handle)) { |
| 2090 | ret = PTR_ERR(handle); |
| 2091 | handle = NULL; |
| 2092 | mlog_errno(ret); |
| 2093 | goto out; |
| 2094 | } |
| 2095 | |
| 2096 | /* |
| 2097 | * This will instruct ocfs2_claim_suballoc_bits and |
| 2098 | * ocfs2_search_one_group to search but save actual allocation |
| 2099 | * for later. |
| 2100 | */ |
| 2101 | ac->ac_find_loc_only = 1; |
| 2102 | |
| 2103 | ret = ocfs2_claim_suballoc_bits(ac, handle, 1, 1, res); |
| 2104 | if (ret < 0) { |
| 2105 | mlog_errno(ret); |
| 2106 | goto out; |
| 2107 | } |
| 2108 | |
| 2109 | ac->ac_find_loc_priv = res; |
| 2110 | *fe_blkno = res->sr_blkno; |
| 2111 | |
| 2112 | out: |
| 2113 | if (handle) |
| 2114 | ocfs2_commit_trans(OCFS2_SB(dir->i_sb), handle); |
| 2115 | |
| 2116 | if (ret) |
| 2117 | kfree(res); |
| 2118 | |
| 2119 | return ret; |
| 2120 | } |
| 2121 | |
| 2122 | int ocfs2_claim_new_inode_at_loc(handle_t *handle, |
| 2123 | struct inode *dir, |
| 2124 | struct ocfs2_alloc_context *ac, |
| 2125 | u64 *suballoc_loc, |
| 2126 | u16 *suballoc_bit, |
| 2127 | u64 di_blkno) |
| 2128 | { |
| 2129 | int ret; |
| 2130 | u16 chain; |
| 2131 | struct ocfs2_suballoc_result *res = ac->ac_find_loc_priv; |
| 2132 | struct buffer_head *bg_bh = NULL; |
| 2133 | struct ocfs2_group_desc *bg; |
| 2134 | struct ocfs2_dinode *di = (struct ocfs2_dinode *) ac->ac_bh->b_data; |
| 2135 | |
| 2136 | /* |
| 2137 | * Since di_blkno is being passed back in, we check for any |
| 2138 | * inconsistencies which may have happened between |
| 2139 | * calls. These are code bugs as di_blkno is not expected to |
| 2140 | * change once returned from ocfs2_find_new_inode_loc() |
| 2141 | */ |
| 2142 | BUG_ON(res->sr_blkno != di_blkno); |
| 2143 | |
| 2144 | ret = ocfs2_read_group_descriptor(ac->ac_inode, di, |
| 2145 | res->sr_bg_stable_blkno, &bg_bh); |
| 2146 | if (ret) { |
| 2147 | mlog_errno(ret); |
| 2148 | goto out; |
| 2149 | } |
| 2150 | |
| 2151 | bg = (struct ocfs2_group_desc *) bg_bh->b_data; |
| 2152 | chain = le16_to_cpu(bg->bg_chain); |
| 2153 | |
| 2154 | ret = ocfs2_alloc_dinode_update_counts(ac->ac_inode, handle, |
| 2155 | ac->ac_bh, res->sr_bits, |
| 2156 | chain); |
| 2157 | if (ret) { |
| 2158 | mlog_errno(ret); |
| 2159 | goto out; |
| 2160 | } |
| 2161 | |
| 2162 | ret = ocfs2_block_group_set_bits(handle, |
| 2163 | ac->ac_inode, |
| 2164 | bg, |
| 2165 | bg_bh, |
| 2166 | res->sr_bit_offset, |
| 2167 | res->sr_bits); |
| 2168 | if (ret < 0) { |
| 2169 | mlog_errno(ret); |
| 2170 | goto out; |
| 2171 | } |
| 2172 | |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 2173 | trace_ocfs2_claim_new_inode_at_loc((unsigned long long)di_blkno, |
| 2174 | res->sr_bits); |
Mark Fasheh | e49e276 | 2010-08-13 15:15:17 -0700 | [diff] [blame] | 2175 | |
| 2176 | atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs); |
| 2177 | |
| 2178 | BUG_ON(res->sr_bits != 1); |
| 2179 | |
| 2180 | *suballoc_loc = res->sr_bg_blkno; |
| 2181 | *suballoc_bit = res->sr_bit_offset; |
| 2182 | ac->ac_bits_given++; |
| 2183 | ocfs2_save_inode_ac_group(dir, ac); |
| 2184 | |
| 2185 | out: |
| 2186 | brelse(bg_bh); |
| 2187 | |
| 2188 | return ret; |
| 2189 | } |
| 2190 | |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 2191 | int ocfs2_claim_new_inode(handle_t *handle, |
Tao Ma | 1382115 | 2009-02-25 00:53:23 +0800 | [diff] [blame] | 2192 | struct inode *dir, |
| 2193 | struct buffer_head *parent_fe_bh, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2194 | struct ocfs2_alloc_context *ac, |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 2195 | u64 *suballoc_loc, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2196 | u16 *suballoc_bit, |
| 2197 | u64 *fe_blkno) |
| 2198 | { |
| 2199 | int status; |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 2200 | struct ocfs2_suballoc_result res; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2201 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2202 | BUG_ON(!ac); |
| 2203 | BUG_ON(ac->ac_bits_given != 0); |
| 2204 | BUG_ON(ac->ac_bits_wanted != 1); |
| 2205 | BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2206 | |
Tao Ma | 1382115 | 2009-02-25 00:53:23 +0800 | [diff] [blame] | 2207 | ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac); |
| 2208 | |
Joel Becker | aa8f8e9 | 2010-03-26 10:08:07 +0800 | [diff] [blame] | 2209 | status = ocfs2_claim_suballoc_bits(ac, |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 2210 | handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2211 | 1, |
| 2212 | 1, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 2213 | &res); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2214 | if (status < 0) { |
| 2215 | mlog_errno(status); |
| 2216 | goto bail; |
| 2217 | } |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 2218 | atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2219 | |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 2220 | BUG_ON(res.sr_bits != 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2221 | |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 2222 | *suballoc_loc = res.sr_bg_blkno; |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 2223 | *suballoc_bit = res.sr_bit_offset; |
Joel Becker | ba20663 | 2010-03-26 10:08:59 +0800 | [diff] [blame] | 2224 | *fe_blkno = res.sr_blkno; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2225 | ac->ac_bits_given++; |
Tao Ma | 1382115 | 2009-02-25 00:53:23 +0800 | [diff] [blame] | 2226 | ocfs2_save_inode_ac_group(dir, ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2227 | status = 0; |
| 2228 | bail: |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 2229 | if (status) |
| 2230 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2231 | return status; |
| 2232 | } |
| 2233 | |
| 2234 | /* translate a group desc. blkno and it's bitmap offset into |
| 2235 | * disk cluster offset. */ |
| 2236 | static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode, |
| 2237 | u64 bg_blkno, |
| 2238 | u16 bg_bit_off) |
| 2239 | { |
| 2240 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 2241 | u32 cluster = 0; |
| 2242 | |
| 2243 | BUG_ON(!ocfs2_is_cluster_bitmap(inode)); |
| 2244 | |
| 2245 | if (bg_blkno != osb->first_cluster_group_blkno) |
| 2246 | cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno); |
| 2247 | cluster += (u32) bg_bit_off; |
| 2248 | return cluster; |
| 2249 | } |
| 2250 | |
| 2251 | /* given a cluster offset, calculate which block group it belongs to |
| 2252 | * and return that block offset. */ |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 2253 | u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2254 | { |
| 2255 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 2256 | u32 group_no; |
| 2257 | |
| 2258 | BUG_ON(!ocfs2_is_cluster_bitmap(inode)); |
| 2259 | |
| 2260 | group_no = cluster / osb->bitmap_cpg; |
| 2261 | if (!group_no) |
| 2262 | return osb->first_cluster_group_blkno; |
| 2263 | return ocfs2_clusters_to_blocks(inode->i_sb, |
| 2264 | group_no * osb->bitmap_cpg); |
| 2265 | } |
| 2266 | |
| 2267 | /* given the block number of a cluster start, calculate which cluster |
| 2268 | * group and descriptor bitmap offset that corresponds to. */ |
| 2269 | static inline void ocfs2_block_to_cluster_group(struct inode *inode, |
| 2270 | u64 data_blkno, |
| 2271 | u64 *bg_blkno, |
| 2272 | u16 *bg_bit_off) |
| 2273 | { |
| 2274 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 2275 | u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno); |
| 2276 | |
| 2277 | BUG_ON(!ocfs2_is_cluster_bitmap(inode)); |
| 2278 | |
| 2279 | *bg_blkno = ocfs2_which_cluster_group(inode, |
| 2280 | data_cluster); |
| 2281 | |
| 2282 | if (*bg_blkno == osb->first_cluster_group_blkno) |
| 2283 | *bg_bit_off = (u16) data_cluster; |
| 2284 | else |
| 2285 | *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb, |
| 2286 | data_blkno - *bg_blkno); |
| 2287 | } |
| 2288 | |
| 2289 | /* |
| 2290 | * min_bits - minimum contiguous chunk from this total allocation we |
| 2291 | * can handle. set to what we asked for originally for a full |
| 2292 | * contig. allocation, set to '1' to indicate we can deal with extents |
| 2293 | * of any size. |
| 2294 | */ |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 2295 | int __ocfs2_claim_clusters(handle_t *handle, |
Mark Fasheh | 415cb80 | 2007-09-16 20:10:16 -0700 | [diff] [blame] | 2296 | struct ocfs2_alloc_context *ac, |
| 2297 | u32 min_clusters, |
| 2298 | u32 max_clusters, |
| 2299 | u32 *cluster_start, |
| 2300 | u32 *num_clusters) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2301 | { |
| 2302 | int status; |
Mark Fasheh | 415cb80 | 2007-09-16 20:10:16 -0700 | [diff] [blame] | 2303 | unsigned int bits_wanted = max_clusters; |
Joel Becker | ba20663 | 2010-03-26 10:08:59 +0800 | [diff] [blame] | 2304 | struct ocfs2_suballoc_result res = { .sr_blkno = 0, }; |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 2305 | struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2306 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2307 | BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted); |
| 2308 | |
| 2309 | BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL |
| 2310 | && ac->ac_which != OCFS2_AC_USE_MAIN); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2311 | |
| 2312 | if (ac->ac_which == OCFS2_AC_USE_LOCAL) { |
Mark Fasheh | 33d5d38 | 2010-02-24 13:34:09 -0800 | [diff] [blame] | 2313 | WARN_ON(min_clusters > 1); |
| 2314 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2315 | status = ocfs2_claim_local_alloc_bits(osb, |
| 2316 | handle, |
| 2317 | ac, |
| 2318 | bits_wanted, |
| 2319 | cluster_start, |
| 2320 | num_clusters); |
| 2321 | if (!status) |
| 2322 | atomic_inc(&osb->alloc_stats.local_data); |
| 2323 | } else { |
| 2324 | if (min_clusters > (osb->bitmap_cpg - 1)) { |
| 2325 | /* The only paths asking for contiguousness |
| 2326 | * should know about this already. */ |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 2327 | mlog(ML_ERROR, "minimum allocation requested %u exceeds " |
| 2328 | "group bitmap size %u!\n", min_clusters, |
| 2329 | osb->bitmap_cpg); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2330 | status = -ENOSPC; |
| 2331 | goto bail; |
| 2332 | } |
| 2333 | /* clamp the current request down to a realistic size. */ |
| 2334 | if (bits_wanted > (osb->bitmap_cpg - 1)) |
| 2335 | bits_wanted = osb->bitmap_cpg - 1; |
| 2336 | |
Joel Becker | aa8f8e9 | 2010-03-26 10:08:07 +0800 | [diff] [blame] | 2337 | status = ocfs2_claim_suballoc_bits(ac, |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 2338 | handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2339 | bits_wanted, |
| 2340 | min_clusters, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 2341 | &res); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2342 | if (!status) { |
Joel Becker | ba20663 | 2010-03-26 10:08:59 +0800 | [diff] [blame] | 2343 | BUG_ON(res.sr_blkno); /* cluster alloc can't set */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2344 | *cluster_start = |
| 2345 | ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode, |
Joel Becker | 7d1fe09 | 2010-04-13 14:30:19 +0800 | [diff] [blame] | 2346 | res.sr_bg_blkno, |
| 2347 | res.sr_bit_offset); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2348 | atomic_inc(&osb->alloc_stats.bitmap_data); |
Tao Ma | 4711954 | 2010-04-22 14:09:15 +0800 | [diff] [blame] | 2349 | *num_clusters = res.sr_bits; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2350 | } |
| 2351 | } |
| 2352 | if (status < 0) { |
| 2353 | if (status != -ENOSPC) |
| 2354 | mlog_errno(status); |
| 2355 | goto bail; |
| 2356 | } |
| 2357 | |
Tao Ma | 4711954 | 2010-04-22 14:09:15 +0800 | [diff] [blame] | 2358 | ac->ac_bits_given += *num_clusters; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2359 | |
| 2360 | bail: |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 2361 | if (status) |
| 2362 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2363 | return status; |
| 2364 | } |
| 2365 | |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 2366 | int ocfs2_claim_clusters(handle_t *handle, |
Mark Fasheh | 415cb80 | 2007-09-16 20:10:16 -0700 | [diff] [blame] | 2367 | struct ocfs2_alloc_context *ac, |
| 2368 | u32 min_clusters, |
| 2369 | u32 *cluster_start, |
| 2370 | u32 *num_clusters) |
| 2371 | { |
| 2372 | unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given; |
| 2373 | |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 2374 | return __ocfs2_claim_clusters(handle, ac, min_clusters, |
Mark Fasheh | 415cb80 | 2007-09-16 20:10:16 -0700 | [diff] [blame] | 2375 | bits_wanted, cluster_start, num_clusters); |
| 2376 | } |
| 2377 | |
Mark Fasheh | b4414ee | 2010-03-11 18:31:09 -0800 | [diff] [blame] | 2378 | static int ocfs2_block_group_clear_bits(handle_t *handle, |
| 2379 | struct inode *alloc_inode, |
| 2380 | struct ocfs2_group_desc *bg, |
| 2381 | struct buffer_head *group_bh, |
| 2382 | unsigned int bit_off, |
| 2383 | unsigned int num_bits, |
| 2384 | void (*undo_fn)(unsigned int bit, |
| 2385 | unsigned long *bmap)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2386 | { |
| 2387 | int status; |
| 2388 | unsigned int tmp; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2389 | struct ocfs2_group_desc *undo_bg = NULL; |
| 2390 | |
Joel Becker | 4203530 | 2008-11-13 14:49:15 -0800 | [diff] [blame] | 2391 | /* The caller got this descriptor from |
| 2392 | * ocfs2_read_group_descriptor(). Any corruption is a code bug. */ |
| 2393 | BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2394 | |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 2395 | trace_ocfs2_block_group_clear_bits(bit_off, num_bits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2396 | |
Mark Fasheh | b4414ee | 2010-03-11 18:31:09 -0800 | [diff] [blame] | 2397 | BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode)); |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2398 | status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode), |
Mark Fasheh | b4414ee | 2010-03-11 18:31:09 -0800 | [diff] [blame] | 2399 | group_bh, |
| 2400 | undo_fn ? |
| 2401 | OCFS2_JOURNAL_ACCESS_UNDO : |
| 2402 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2403 | if (status < 0) { |
| 2404 | mlog_errno(status); |
| 2405 | goto bail; |
| 2406 | } |
| 2407 | |
Mark Fasheh | b4414ee | 2010-03-11 18:31:09 -0800 | [diff] [blame] | 2408 | if (undo_fn) { |
Sunil Mushran | 94e41ec | 2009-06-19 14:45:54 -0700 | [diff] [blame] | 2409 | jbd_lock_bh_state(group_bh); |
| 2410 | undo_bg = (struct ocfs2_group_desc *) |
| 2411 | bh2jh(group_bh)->b_committed_data; |
| 2412 | BUG_ON(!undo_bg); |
| 2413 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2414 | |
| 2415 | tmp = num_bits; |
| 2416 | while(tmp--) { |
| 2417 | ocfs2_clear_bit((bit_off + tmp), |
| 2418 | (unsigned long *) bg->bg_bitmap); |
Mark Fasheh | b4414ee | 2010-03-11 18:31:09 -0800 | [diff] [blame] | 2419 | if (undo_fn) |
| 2420 | undo_fn(bit_off + tmp, |
| 2421 | (unsigned long *) undo_bg->bg_bitmap); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2422 | } |
| 2423 | le16_add_cpu(&bg->bg_free_bits_count, num_bits); |
Srinivas Eeda | 9b5cd10 | 2010-10-05 15:53:06 -0700 | [diff] [blame] | 2424 | if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) { |
| 2425 | ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit" |
| 2426 | " count %u but claims %u are freed. num_bits %d", |
| 2427 | (unsigned long long)le64_to_cpu(bg->bg_blkno), |
| 2428 | le16_to_cpu(bg->bg_bits), |
| 2429 | le16_to_cpu(bg->bg_free_bits_count), num_bits); |
| 2430 | return -EROFS; |
| 2431 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2432 | |
Mark Fasheh | b4414ee | 2010-03-11 18:31:09 -0800 | [diff] [blame] | 2433 | if (undo_fn) |
Sunil Mushran | 94e41ec | 2009-06-19 14:45:54 -0700 | [diff] [blame] | 2434 | jbd_unlock_bh_state(group_bh); |
| 2435 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2436 | ocfs2_journal_dirty(handle, group_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2437 | bail: |
| 2438 | return status; |
| 2439 | } |
| 2440 | |
| 2441 | /* |
| 2442 | * expects the suballoc inode to already be locked. |
| 2443 | */ |
Mark Fasheh | b4414ee | 2010-03-11 18:31:09 -0800 | [diff] [blame] | 2444 | static int _ocfs2_free_suballoc_bits(handle_t *handle, |
| 2445 | struct inode *alloc_inode, |
| 2446 | struct buffer_head *alloc_bh, |
| 2447 | unsigned int start_bit, |
| 2448 | u64 bg_blkno, |
| 2449 | unsigned int count, |
| 2450 | void (*undo_fn)(unsigned int bit, |
| 2451 | unsigned long *bitmap)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2452 | { |
| 2453 | int status = 0; |
| 2454 | u32 tmp_used; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2455 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data; |
| 2456 | struct ocfs2_chain_list *cl = &fe->id2.i_chain; |
| 2457 | struct buffer_head *group_bh = NULL; |
| 2458 | struct ocfs2_group_desc *group; |
| 2459 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 2460 | /* The alloc_bh comes from ocfs2_free_dinode() or |
| 2461 | * ocfs2_free_clusters(). The callers have all locked the |
| 2462 | * allocator and gotten alloc_bh from the lock call. This |
| 2463 | * validates the dinode buffer. Any corruption that has happended |
| 2464 | * is a code bug. */ |
| 2465 | BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2466 | BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl)); |
| 2467 | |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 2468 | trace_ocfs2_free_suballoc_bits( |
| 2469 | (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, |
| 2470 | (unsigned long long)bg_blkno, |
| 2471 | start_bit, count); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2472 | |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 2473 | status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno, |
| 2474 | &group_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2475 | if (status < 0) { |
| 2476 | mlog_errno(status); |
| 2477 | goto bail; |
| 2478 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2479 | group = (struct ocfs2_group_desc *) group_bh->b_data; |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 2480 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2481 | BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits)); |
| 2482 | |
| 2483 | status = ocfs2_block_group_clear_bits(handle, alloc_inode, |
| 2484 | group, group_bh, |
Mark Fasheh | b4414ee | 2010-03-11 18:31:09 -0800 | [diff] [blame] | 2485 | start_bit, count, undo_fn); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2486 | if (status < 0) { |
| 2487 | mlog_errno(status); |
| 2488 | goto bail; |
| 2489 | } |
| 2490 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2491 | status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode), |
| 2492 | alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2493 | if (status < 0) { |
| 2494 | mlog_errno(status); |
| 2495 | goto bail; |
| 2496 | } |
| 2497 | |
| 2498 | le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free, |
| 2499 | count); |
| 2500 | tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used); |
| 2501 | fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count); |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2502 | ocfs2_journal_dirty(handle, alloc_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2503 | |
| 2504 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 2505 | brelse(group_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2506 | |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 2507 | if (status) |
| 2508 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2509 | return status; |
| 2510 | } |
| 2511 | |
Mark Fasheh | b4414ee | 2010-03-11 18:31:09 -0800 | [diff] [blame] | 2512 | int ocfs2_free_suballoc_bits(handle_t *handle, |
| 2513 | struct inode *alloc_inode, |
| 2514 | struct buffer_head *alloc_bh, |
| 2515 | unsigned int start_bit, |
| 2516 | u64 bg_blkno, |
| 2517 | unsigned int count) |
| 2518 | { |
| 2519 | return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh, |
| 2520 | start_bit, bg_blkno, count, NULL); |
| 2521 | } |
| 2522 | |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 2523 | int ocfs2_free_dinode(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2524 | struct inode *inode_alloc_inode, |
| 2525 | struct buffer_head *inode_alloc_bh, |
| 2526 | struct ocfs2_dinode *di) |
| 2527 | { |
| 2528 | u64 blk = le64_to_cpu(di->i_blkno); |
| 2529 | u16 bit = le16_to_cpu(di->i_suballoc_bit); |
| 2530 | u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit); |
| 2531 | |
Tao Ma | 74380c4 | 2010-03-22 14:20:18 +0800 | [diff] [blame] | 2532 | if (di->i_suballoc_loc) |
| 2533 | bg_blkno = le64_to_cpu(di->i_suballoc_loc); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2534 | return ocfs2_free_suballoc_bits(handle, inode_alloc_inode, |
| 2535 | inode_alloc_bh, bit, bg_blkno, 1); |
| 2536 | } |
| 2537 | |
Mark Fasheh | b4414ee | 2010-03-11 18:31:09 -0800 | [diff] [blame] | 2538 | static int _ocfs2_free_clusters(handle_t *handle, |
| 2539 | struct inode *bitmap_inode, |
| 2540 | struct buffer_head *bitmap_bh, |
| 2541 | u64 start_blk, |
| 2542 | unsigned int num_clusters, |
| 2543 | void (*undo_fn)(unsigned int bit, |
| 2544 | unsigned long *bitmap)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2545 | { |
| 2546 | int status; |
| 2547 | u16 bg_start_bit; |
| 2548 | u64 bg_blkno; |
| 2549 | struct ocfs2_dinode *fe; |
| 2550 | |
| 2551 | /* You can't ever have a contiguous set of clusters |
| 2552 | * bigger than a block group bitmap so we never have to worry |
Tao Ma | ef6b689 | 2011-02-21 11:10:44 +0800 | [diff] [blame] | 2553 | * about looping on them. |
| 2554 | * This is expensive. We can safely remove once this stuff has |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2555 | * gotten tested really well. */ |
| 2556 | BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk))); |
| 2557 | |
| 2558 | fe = (struct ocfs2_dinode *) bitmap_bh->b_data; |
| 2559 | |
| 2560 | ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno, |
| 2561 | &bg_start_bit); |
| 2562 | |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 2563 | trace_ocfs2_free_clusters((unsigned long long)bg_blkno, |
| 2564 | (unsigned long long)start_blk, |
| 2565 | bg_start_bit, num_clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2566 | |
Mark Fasheh | b4414ee | 2010-03-11 18:31:09 -0800 | [diff] [blame] | 2567 | status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh, |
| 2568 | bg_start_bit, bg_blkno, |
| 2569 | num_clusters, undo_fn); |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 2570 | if (status < 0) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2571 | mlog_errno(status); |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 2572 | goto out; |
| 2573 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2574 | |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 2575 | ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb), |
| 2576 | num_clusters); |
| 2577 | |
| 2578 | out: |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 2579 | if (status) |
| 2580 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2581 | return status; |
| 2582 | } |
| 2583 | |
Mark Fasheh | b4414ee | 2010-03-11 18:31:09 -0800 | [diff] [blame] | 2584 | int ocfs2_free_clusters(handle_t *handle, |
| 2585 | struct inode *bitmap_inode, |
| 2586 | struct buffer_head *bitmap_bh, |
| 2587 | u64 start_blk, |
| 2588 | unsigned int num_clusters) |
| 2589 | { |
| 2590 | return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh, |
| 2591 | start_blk, num_clusters, |
| 2592 | _ocfs2_set_bit); |
| 2593 | } |
| 2594 | |
| 2595 | /* |
| 2596 | * Give never-used clusters back to the global bitmap. We don't need |
| 2597 | * to protect these bits in the undo buffer. |
| 2598 | */ |
| 2599 | int ocfs2_release_clusters(handle_t *handle, |
| 2600 | struct inode *bitmap_inode, |
| 2601 | struct buffer_head *bitmap_bh, |
| 2602 | u64 start_blk, |
| 2603 | unsigned int num_clusters) |
| 2604 | { |
| 2605 | return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh, |
| 2606 | start_blk, num_clusters, |
| 2607 | _ocfs2_clear_bit); |
| 2608 | } |
| 2609 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2610 | static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg) |
| 2611 | { |
| 2612 | printk("Block Group:\n"); |
| 2613 | printk("bg_signature: %s\n", bg->bg_signature); |
| 2614 | printk("bg_size: %u\n", bg->bg_size); |
| 2615 | printk("bg_bits: %u\n", bg->bg_bits); |
| 2616 | printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count); |
| 2617 | printk("bg_chain: %u\n", bg->bg_chain); |
| 2618 | printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation)); |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 2619 | printk("bg_next_group: %llu\n", |
| 2620 | (unsigned long long)bg->bg_next_group); |
| 2621 | printk("bg_parent_dinode: %llu\n", |
| 2622 | (unsigned long long)bg->bg_parent_dinode); |
| 2623 | printk("bg_blkno: %llu\n", |
| 2624 | (unsigned long long)bg->bg_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe) |
| 2628 | { |
| 2629 | int i; |
| 2630 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 2631 | printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2632 | printk("i_signature: %s\n", fe->i_signature); |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 2633 | printk("i_size: %llu\n", |
| 2634 | (unsigned long long)fe->i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2635 | printk("i_clusters: %u\n", fe->i_clusters); |
| 2636 | printk("i_generation: %u\n", |
| 2637 | le32_to_cpu(fe->i_generation)); |
| 2638 | printk("id1.bitmap1.i_used: %u\n", |
| 2639 | le32_to_cpu(fe->id1.bitmap1.i_used)); |
| 2640 | printk("id1.bitmap1.i_total: %u\n", |
| 2641 | le32_to_cpu(fe->id1.bitmap1.i_total)); |
| 2642 | printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg); |
| 2643 | printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc); |
| 2644 | printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count); |
| 2645 | printk("id2.i_chain.cl_next_free_rec: %u\n", |
| 2646 | fe->id2.i_chain.cl_next_free_rec); |
| 2647 | for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) { |
| 2648 | printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i, |
| 2649 | fe->id2.i_chain.cl_recs[i].c_free); |
| 2650 | printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i, |
| 2651 | fe->id2.i_chain.cl_recs[i].c_total); |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 2652 | printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i, |
| 2653 | (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2654 | } |
| 2655 | } |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2656 | |
| 2657 | /* |
| 2658 | * For a given allocation, determine which allocators will need to be |
| 2659 | * accessed, and lock them, reserving the appropriate number of bits. |
| 2660 | * |
| 2661 | * Sparse file systems call this from ocfs2_write_begin_nolock() |
| 2662 | * and ocfs2_allocate_unwritten_extents(). |
| 2663 | * |
| 2664 | * File systems which don't support holes call this from |
| 2665 | * ocfs2_extend_allocation(). |
| 2666 | */ |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 2667 | int ocfs2_lock_allocators(struct inode *inode, |
| 2668 | struct ocfs2_extent_tree *et, |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2669 | u32 clusters_to_add, u32 extents_to_split, |
| 2670 | struct ocfs2_alloc_context **data_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 2671 | struct ocfs2_alloc_context **meta_ac) |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2672 | { |
| 2673 | int ret = 0, num_free_extents; |
| 2674 | unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split; |
| 2675 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 2676 | |
| 2677 | *meta_ac = NULL; |
| 2678 | if (data_ac) |
| 2679 | *data_ac = NULL; |
| 2680 | |
| 2681 | BUG_ON(clusters_to_add != 0 && data_ac == NULL); |
| 2682 | |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 2683 | num_free_extents = ocfs2_num_free_extents(osb, et); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2684 | if (num_free_extents < 0) { |
| 2685 | ret = num_free_extents; |
| 2686 | mlog_errno(ret); |
| 2687 | goto out; |
| 2688 | } |
| 2689 | |
| 2690 | /* |
| 2691 | * Sparse allocation file systems need to be more conservative |
| 2692 | * with reserving room for expansion - the actual allocation |
| 2693 | * happens while we've got a journal handle open so re-taking |
| 2694 | * a cluster lock (because we ran out of room for another |
| 2695 | * extent) will violate ordering rules. |
| 2696 | * |
| 2697 | * Most of the time we'll only be seeing this 1 cluster at a time |
| 2698 | * anyway. |
| 2699 | * |
| 2700 | * Always lock for any unwritten extents - we might want to |
| 2701 | * add blocks during a split. |
| 2702 | */ |
| 2703 | if (!num_free_extents || |
| 2704 | (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) { |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 2705 | ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac); |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame] | 2706 | if (ret < 0) { |
| 2707 | if (ret != -ENOSPC) |
| 2708 | mlog_errno(ret); |
| 2709 | goto out; |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | if (clusters_to_add == 0) |
| 2714 | goto out; |
| 2715 | |
| 2716 | ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac); |
| 2717 | if (ret < 0) { |
| 2718 | if (ret != -ENOSPC) |
| 2719 | mlog_errno(ret); |
| 2720 | goto out; |
| 2721 | } |
| 2722 | |
| 2723 | out: |
| 2724 | if (ret) { |
| 2725 | if (*meta_ac) { |
| 2726 | ocfs2_free_alloc_context(*meta_ac); |
| 2727 | *meta_ac = NULL; |
| 2728 | } |
| 2729 | |
| 2730 | /* |
| 2731 | * We cannot have an error and a non null *data_ac. |
| 2732 | */ |
| 2733 | } |
| 2734 | |
| 2735 | return ret; |
| 2736 | } |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2737 | |
| 2738 | /* |
| 2739 | * Read the inode specified by blkno to get suballoc_slot and |
| 2740 | * suballoc_bit. |
| 2741 | */ |
| 2742 | static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno, |
Tao Ma | 889f004 | 2010-09-02 13:10:10 +0800 | [diff] [blame] | 2743 | u16 *suballoc_slot, u64 *group_blkno, |
| 2744 | u16 *suballoc_bit) |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2745 | { |
| 2746 | int status; |
| 2747 | struct buffer_head *inode_bh = NULL; |
| 2748 | struct ocfs2_dinode *inode_fe; |
| 2749 | |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 2750 | trace_ocfs2_get_suballoc_slot_bit((unsigned long long)blkno); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2751 | |
| 2752 | /* dirty read disk */ |
| 2753 | status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh); |
| 2754 | if (status < 0) { |
Joel Becker | 5b09b50 | 2009-04-21 16:31:20 -0700 | [diff] [blame] | 2755 | mlog(ML_ERROR, "read block %llu failed %d\n", |
| 2756 | (unsigned long long)blkno, status); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2757 | goto bail; |
| 2758 | } |
| 2759 | |
| 2760 | inode_fe = (struct ocfs2_dinode *) inode_bh->b_data; |
| 2761 | if (!OCFS2_IS_VALID_DINODE(inode_fe)) { |
Joel Becker | 5b09b50 | 2009-04-21 16:31:20 -0700 | [diff] [blame] | 2762 | mlog(ML_ERROR, "invalid inode %llu requested\n", |
| 2763 | (unsigned long long)blkno); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2764 | status = -EINVAL; |
| 2765 | goto bail; |
| 2766 | } |
| 2767 | |
Tao Ma | 0fba813 | 2009-03-19 05:08:43 +0800 | [diff] [blame] | 2768 | if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT && |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2769 | (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) { |
| 2770 | mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n", |
Joel Becker | 5b09b50 | 2009-04-21 16:31:20 -0700 | [diff] [blame] | 2771 | (unsigned long long)blkno, |
| 2772 | (u32)le16_to_cpu(inode_fe->i_suballoc_slot)); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2773 | status = -EINVAL; |
| 2774 | goto bail; |
| 2775 | } |
| 2776 | |
| 2777 | if (suballoc_slot) |
| 2778 | *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot); |
| 2779 | if (suballoc_bit) |
| 2780 | *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit); |
Tao Ma | 889f004 | 2010-09-02 13:10:10 +0800 | [diff] [blame] | 2781 | if (group_blkno) |
| 2782 | *group_blkno = le64_to_cpu(inode_fe->i_suballoc_loc); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2783 | |
| 2784 | bail: |
| 2785 | brelse(inode_bh); |
| 2786 | |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 2787 | if (status) |
| 2788 | mlog_errno(status); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2789 | return status; |
| 2790 | } |
| 2791 | |
| 2792 | /* |
| 2793 | * test whether bit is SET in allocator bitmap or not. on success, 0 |
| 2794 | * is returned and *res is 1 for SET; 0 otherwise. when fails, errno |
| 2795 | * is returned and *res is meaningless. Call this after you have |
| 2796 | * cluster locked against suballoc, or you may get a result based on |
| 2797 | * non-up2date contents |
| 2798 | */ |
| 2799 | static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb, |
| 2800 | struct inode *suballoc, |
Tao Ma | 889f004 | 2010-09-02 13:10:10 +0800 | [diff] [blame] | 2801 | struct buffer_head *alloc_bh, |
| 2802 | u64 group_blkno, u64 blkno, |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2803 | u16 bit, int *res) |
| 2804 | { |
Tao Ma | abf1b3c | 2010-04-27 08:30:36 +0800 | [diff] [blame] | 2805 | struct ocfs2_dinode *alloc_di; |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2806 | struct ocfs2_group_desc *group; |
| 2807 | struct buffer_head *group_bh = NULL; |
| 2808 | u64 bg_blkno; |
| 2809 | int status; |
| 2810 | |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 2811 | trace_ocfs2_test_suballoc_bit((unsigned long long)blkno, |
| 2812 | (unsigned int)bit); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2813 | |
Tao Ma | abf1b3c | 2010-04-27 08:30:36 +0800 | [diff] [blame] | 2814 | alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data; |
| 2815 | if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) { |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2816 | mlog(ML_ERROR, "suballoc bit %u out of range of %u\n", |
| 2817 | (unsigned int)bit, |
Tao Ma | abf1b3c | 2010-04-27 08:30:36 +0800 | [diff] [blame] | 2818 | ocfs2_bits_per_group(&alloc_di->id2.i_chain)); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2819 | status = -EINVAL; |
| 2820 | goto bail; |
| 2821 | } |
| 2822 | |
Tao Ma | 889f004 | 2010-09-02 13:10:10 +0800 | [diff] [blame] | 2823 | bg_blkno = group_blkno ? group_blkno : |
| 2824 | ocfs2_which_suballoc_group(blkno, bit); |
Tao Ma | abf1b3c | 2010-04-27 08:30:36 +0800 | [diff] [blame] | 2825 | status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno, |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2826 | &group_bh); |
| 2827 | if (status < 0) { |
Joel Becker | 5b09b50 | 2009-04-21 16:31:20 -0700 | [diff] [blame] | 2828 | mlog(ML_ERROR, "read group %llu failed %d\n", |
| 2829 | (unsigned long long)bg_blkno, status); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2830 | goto bail; |
| 2831 | } |
| 2832 | |
| 2833 | group = (struct ocfs2_group_desc *) group_bh->b_data; |
| 2834 | *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap); |
| 2835 | |
| 2836 | bail: |
| 2837 | brelse(group_bh); |
| 2838 | |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 2839 | if (status) |
| 2840 | mlog_errno(status); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2841 | return status; |
| 2842 | } |
| 2843 | |
| 2844 | /* |
| 2845 | * Test if the bit representing this inode (blkno) is set in the |
| 2846 | * suballocator. |
| 2847 | * |
| 2848 | * On success, 0 is returned and *res is 1 for SET; 0 otherwise. |
| 2849 | * |
| 2850 | * In the event of failure, a negative value is returned and *res is |
| 2851 | * meaningless. |
| 2852 | * |
| 2853 | * Callers must make sure to hold nfs_sync_lock to prevent |
| 2854 | * ocfs2_delete_inode() on another node from accessing the same |
| 2855 | * suballocator concurrently. |
| 2856 | */ |
| 2857 | int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res) |
| 2858 | { |
| 2859 | int status; |
Tao Ma | 889f004 | 2010-09-02 13:10:10 +0800 | [diff] [blame] | 2860 | u64 group_blkno = 0; |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2861 | u16 suballoc_bit = 0, suballoc_slot = 0; |
| 2862 | struct inode *inode_alloc_inode; |
| 2863 | struct buffer_head *alloc_bh = NULL; |
| 2864 | |
Tao Ma | 2f73e13 | 2011-02-22 08:22:33 +0800 | [diff] [blame^] | 2865 | trace_ocfs2_test_inode_bit((unsigned long long)blkno); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2866 | |
| 2867 | status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot, |
Tao Ma | 889f004 | 2010-09-02 13:10:10 +0800 | [diff] [blame] | 2868 | &group_blkno, &suballoc_bit); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2869 | if (status < 0) { |
| 2870 | mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status); |
| 2871 | goto bail; |
| 2872 | } |
| 2873 | |
| 2874 | inode_alloc_inode = |
| 2875 | ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE, |
| 2876 | suballoc_slot); |
| 2877 | if (!inode_alloc_inode) { |
| 2878 | /* the error code could be inaccurate, but we are not able to |
| 2879 | * get the correct one. */ |
| 2880 | status = -EINVAL; |
| 2881 | mlog(ML_ERROR, "unable to get alloc inode in slot %u\n", |
| 2882 | (u32)suballoc_slot); |
| 2883 | goto bail; |
| 2884 | } |
| 2885 | |
| 2886 | mutex_lock(&inode_alloc_inode->i_mutex); |
| 2887 | status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0); |
| 2888 | if (status < 0) { |
| 2889 | mutex_unlock(&inode_alloc_inode->i_mutex); |
| 2890 | mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n", |
| 2891 | (u32)suballoc_slot, status); |
| 2892 | goto bail; |
| 2893 | } |
| 2894 | |
| 2895 | status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh, |
Tao Ma | 889f004 | 2010-09-02 13:10:10 +0800 | [diff] [blame] | 2896 | group_blkno, blkno, suballoc_bit, res); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2897 | if (status < 0) |
| 2898 | mlog(ML_ERROR, "test suballoc bit failed %d\n", status); |
| 2899 | |
| 2900 | ocfs2_inode_unlock(inode_alloc_inode, 0); |
| 2901 | mutex_unlock(&inode_alloc_inode->i_mutex); |
| 2902 | |
| 2903 | iput(inode_alloc_inode); |
| 2904 | brelse(alloc_bh); |
| 2905 | bail: |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 2906 | if (status) |
| 2907 | mlog_errno(status); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 2908 | return status; |
| 2909 | } |