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