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