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