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 | |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 114 | 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, |
Tao Ma | 811f933 | 2008-08-18 17:38:43 +0800 | [diff] [blame] | 497 | struct ocfs2_extent_list *root_el, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 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 | |
Tao Ma | 811f933 | 2008-08-18 17:38:43 +0800 | [diff] [blame] | 510 | (*ac)->ac_bits_wanted = ocfs2_extend_meta_needed(root_el); |
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; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
| 692 | if (status == -ENOSPC) { |
| 693 | status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac); |
| 694 | if (status < 0) { |
| 695 | if (status != -ENOSPC) |
| 696 | mlog_errno(status); |
| 697 | goto bail; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | status = 0; |
| 702 | bail: |
| 703 | if ((status < 0) && *ac) { |
| 704 | ocfs2_free_alloc_context(*ac); |
| 705 | *ac = NULL; |
| 706 | } |
| 707 | |
| 708 | mlog_exit(status); |
| 709 | return status; |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * More or less lifted from ext3. I'll leave their description below: |
| 714 | * |
| 715 | * "For ext3 allocations, we must not reuse any blocks which are |
| 716 | * allocated in the bitmap buffer's "last committed data" copy. This |
| 717 | * prevents deletes from freeing up the page for reuse until we have |
| 718 | * committed the delete transaction. |
| 719 | * |
| 720 | * If we didn't do this, then deleting something and reallocating it as |
| 721 | * data would allow the old block to be overwritten before the |
| 722 | * transaction committed (because we force data to disk before commit). |
| 723 | * This would lead to corruption if we crashed between overwriting the |
| 724 | * data and committing the delete. |
| 725 | * |
| 726 | * @@@ We may want to make this allocation behaviour conditional on |
| 727 | * data-writes at some point, and disable it for metadata allocations or |
| 728 | * sync-data inodes." |
| 729 | * |
| 730 | * Note: OCFS2 already does this differently for metadata vs data |
Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 731 | * allocations, as those bitmaps are separate and undo access is never |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 732 | * called on a metadata group descriptor. |
| 733 | */ |
| 734 | static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh, |
| 735 | int nr) |
| 736 | { |
| 737 | struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; |
| 738 | |
| 739 | if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap)) |
| 740 | return 0; |
| 741 | if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data) |
| 742 | return 1; |
| 743 | |
| 744 | bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data; |
| 745 | return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap); |
| 746 | } |
| 747 | |
| 748 | static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb, |
| 749 | struct buffer_head *bg_bh, |
| 750 | unsigned int bits_wanted, |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 751 | unsigned int total_bits, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 752 | u16 *bit_off, |
| 753 | u16 *bits_found) |
| 754 | { |
| 755 | void *bitmap; |
| 756 | u16 best_offset, best_size; |
| 757 | int offset, start, found, status = 0; |
| 758 | struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; |
| 759 | |
| 760 | if (!OCFS2_IS_VALID_GROUP_DESC(bg)) { |
| 761 | OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg); |
| 762 | return -EIO; |
| 763 | } |
| 764 | |
| 765 | found = start = best_offset = best_size = 0; |
| 766 | bitmap = bg->bg_bitmap; |
| 767 | |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 768 | while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) { |
| 769 | if (offset == total_bits) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 770 | break; |
| 771 | |
| 772 | if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) { |
| 773 | /* We found a zero, but we can't use it as it |
| 774 | * hasn't been put to disk yet! */ |
| 775 | found = 0; |
| 776 | start = offset + 1; |
| 777 | } else if (offset == start) { |
| 778 | /* we found a zero */ |
| 779 | found++; |
| 780 | /* move start to the next bit to test */ |
| 781 | start++; |
| 782 | } else { |
| 783 | /* got a zero after some ones */ |
| 784 | found = 1; |
| 785 | start = offset + 1; |
| 786 | } |
| 787 | if (found > best_size) { |
| 788 | best_size = found; |
| 789 | best_offset = start - found; |
| 790 | } |
| 791 | /* we got everything we needed */ |
| 792 | if (found == bits_wanted) { |
| 793 | /* mlog(0, "Found it all!\n"); */ |
| 794 | break; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /* XXX: I think the first clause is equivalent to the second |
| 799 | * - jlbec */ |
| 800 | if (found == bits_wanted) { |
| 801 | *bit_off = start - found; |
| 802 | *bits_found = found; |
| 803 | } else if (best_size) { |
| 804 | *bit_off = best_offset; |
| 805 | *bits_found = best_size; |
| 806 | } else { |
| 807 | status = -ENOSPC; |
| 808 | /* No error log here -- see the comment above |
| 809 | * ocfs2_test_bg_bit_allocatable */ |
| 810 | } |
| 811 | |
| 812 | return status; |
| 813 | } |
| 814 | |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 815 | static inline int ocfs2_block_group_set_bits(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 816 | struct inode *alloc_inode, |
| 817 | struct ocfs2_group_desc *bg, |
| 818 | struct buffer_head *group_bh, |
| 819 | unsigned int bit_off, |
| 820 | unsigned int num_bits) |
| 821 | { |
| 822 | int status; |
| 823 | void *bitmap = bg->bg_bitmap; |
| 824 | int journal_type = OCFS2_JOURNAL_ACCESS_WRITE; |
| 825 | |
| 826 | mlog_entry_void(); |
| 827 | |
| 828 | if (!OCFS2_IS_VALID_GROUP_DESC(bg)) { |
| 829 | OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg); |
| 830 | status = -EIO; |
| 831 | goto bail; |
| 832 | } |
| 833 | BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits); |
| 834 | |
| 835 | mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off, |
| 836 | num_bits); |
| 837 | |
| 838 | if (ocfs2_is_cluster_bitmap(alloc_inode)) |
| 839 | journal_type = OCFS2_JOURNAL_ACCESS_UNDO; |
| 840 | |
| 841 | status = ocfs2_journal_access(handle, |
| 842 | alloc_inode, |
| 843 | group_bh, |
| 844 | journal_type); |
| 845 | if (status < 0) { |
| 846 | mlog_errno(status); |
| 847 | goto bail; |
| 848 | } |
| 849 | |
| 850 | le16_add_cpu(&bg->bg_free_bits_count, -num_bits); |
| 851 | |
| 852 | while(num_bits--) |
| 853 | ocfs2_set_bit(bit_off++, bitmap); |
| 854 | |
| 855 | status = ocfs2_journal_dirty(handle, |
| 856 | group_bh); |
| 857 | if (status < 0) { |
| 858 | mlog_errno(status); |
| 859 | goto bail; |
| 860 | } |
| 861 | |
| 862 | bail: |
| 863 | mlog_exit(status); |
| 864 | return status; |
| 865 | } |
| 866 | |
| 867 | /* find the one with the most empty bits */ |
| 868 | static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl) |
| 869 | { |
| 870 | u16 curr, best; |
| 871 | |
| 872 | BUG_ON(!cl->cl_next_free_rec); |
| 873 | |
| 874 | best = curr = 0; |
| 875 | while (curr < le16_to_cpu(cl->cl_next_free_rec)) { |
| 876 | if (le32_to_cpu(cl->cl_recs[curr].c_free) > |
| 877 | le32_to_cpu(cl->cl_recs[best].c_free)) |
| 878 | best = curr; |
| 879 | curr++; |
| 880 | } |
| 881 | |
| 882 | BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec)); |
| 883 | return best; |
| 884 | } |
| 885 | |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 886 | static int ocfs2_relink_block_group(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 887 | struct inode *alloc_inode, |
| 888 | struct buffer_head *fe_bh, |
| 889 | struct buffer_head *bg_bh, |
| 890 | struct buffer_head *prev_bg_bh, |
| 891 | u16 chain) |
| 892 | { |
| 893 | int status; |
| 894 | /* there is a really tiny chance the journal calls could fail, |
| 895 | * but we wouldn't want inconsistent blocks in *any* case. */ |
| 896 | u64 fe_ptr, bg_ptr, prev_bg_ptr; |
| 897 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 898 | struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; |
| 899 | struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data; |
| 900 | |
| 901 | if (!OCFS2_IS_VALID_DINODE(fe)) { |
| 902 | OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe); |
| 903 | status = -EIO; |
| 904 | goto out; |
| 905 | } |
| 906 | if (!OCFS2_IS_VALID_GROUP_DESC(bg)) { |
| 907 | OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg); |
| 908 | status = -EIO; |
| 909 | goto out; |
| 910 | } |
| 911 | if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) { |
| 912 | OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg); |
| 913 | status = -EIO; |
| 914 | goto out; |
| 915 | } |
| 916 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 917 | 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] | 918 | (unsigned long long)le64_to_cpu(fe->i_blkno), chain, |
| 919 | (unsigned long long)le64_to_cpu(bg->bg_blkno), |
| 920 | (unsigned long long)le64_to_cpu(prev_bg->bg_blkno)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 921 | |
| 922 | fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno); |
| 923 | bg_ptr = le64_to_cpu(bg->bg_next_group); |
| 924 | prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group); |
| 925 | |
| 926 | status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh, |
| 927 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 928 | if (status < 0) { |
| 929 | mlog_errno(status); |
| 930 | goto out_rollback; |
| 931 | } |
| 932 | |
| 933 | prev_bg->bg_next_group = bg->bg_next_group; |
| 934 | |
| 935 | status = ocfs2_journal_dirty(handle, prev_bg_bh); |
| 936 | if (status < 0) { |
| 937 | mlog_errno(status); |
| 938 | goto out_rollback; |
| 939 | } |
| 940 | |
| 941 | status = ocfs2_journal_access(handle, alloc_inode, bg_bh, |
| 942 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 943 | if (status < 0) { |
| 944 | mlog_errno(status); |
| 945 | goto out_rollback; |
| 946 | } |
| 947 | |
| 948 | bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno; |
| 949 | |
| 950 | status = ocfs2_journal_dirty(handle, bg_bh); |
| 951 | if (status < 0) { |
| 952 | mlog_errno(status); |
| 953 | goto out_rollback; |
| 954 | } |
| 955 | |
| 956 | status = ocfs2_journal_access(handle, alloc_inode, fe_bh, |
| 957 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 958 | if (status < 0) { |
| 959 | mlog_errno(status); |
| 960 | goto out_rollback; |
| 961 | } |
| 962 | |
| 963 | fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno; |
| 964 | |
| 965 | status = ocfs2_journal_dirty(handle, fe_bh); |
| 966 | if (status < 0) { |
| 967 | mlog_errno(status); |
| 968 | goto out_rollback; |
| 969 | } |
| 970 | |
| 971 | status = 0; |
| 972 | out_rollback: |
| 973 | if (status < 0) { |
| 974 | fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr); |
| 975 | bg->bg_next_group = cpu_to_le64(bg_ptr); |
| 976 | prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr); |
| 977 | } |
| 978 | out: |
| 979 | mlog_exit(status); |
| 980 | return status; |
| 981 | } |
| 982 | |
| 983 | static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg, |
| 984 | u32 wanted) |
| 985 | { |
| 986 | return le16_to_cpu(bg->bg_free_bits_count) > wanted; |
| 987 | } |
| 988 | |
| 989 | /* return 0 on success, -ENOSPC to keep searching and any other < 0 |
| 990 | * value on error. */ |
| 991 | static int ocfs2_cluster_group_search(struct inode *inode, |
| 992 | struct buffer_head *group_bh, |
| 993 | u32 bits_wanted, u32 min_bits, |
| 994 | u16 *bit_off, u16 *bits_found) |
| 995 | { |
| 996 | int search = -ENOSPC; |
| 997 | int ret; |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 998 | 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] | 999 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1000 | u16 tmp_off, tmp_found; |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 1001 | unsigned int max_bits, gd_cluster_off; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1002 | |
| 1003 | BUG_ON(!ocfs2_is_cluster_bitmap(inode)); |
| 1004 | |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 1005 | if (gd->bg_free_bits_count) { |
| 1006 | max_bits = le16_to_cpu(gd->bg_bits); |
| 1007 | |
| 1008 | /* Tail groups in cluster bitmaps which aren't cpg |
| 1009 | * aligned are prone to partial extention by a failed |
| 1010 | * fs resize. If the file system resize never got to |
| 1011 | * update the dinode cluster count, then we don't want |
| 1012 | * to trust any clusters past it, regardless of what |
| 1013 | * the group descriptor says. */ |
| 1014 | gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb, |
| 1015 | le64_to_cpu(gd->bg_blkno)); |
| 1016 | if ((gd_cluster_off + max_bits) > |
| 1017 | OCFS2_I(inode)->ip_clusters) { |
| 1018 | max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off; |
| 1019 | mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n", |
| 1020 | (unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 1021 | le16_to_cpu(gd->bg_bits), |
| 1022 | OCFS2_I(inode)->ip_clusters, max_bits); |
| 1023 | } |
| 1024 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1025 | ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb), |
| 1026 | group_bh, bits_wanted, |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 1027 | max_bits, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1028 | &tmp_off, &tmp_found); |
| 1029 | if (ret) |
| 1030 | return ret; |
| 1031 | |
| 1032 | /* ocfs2_block_group_find_clear_bits() might |
| 1033 | * return success, but we still want to return |
| 1034 | * -ENOSPC unless it found the minimum number |
| 1035 | * of bits. */ |
| 1036 | if (min_bits <= tmp_found) { |
| 1037 | *bit_off = tmp_off; |
| 1038 | *bits_found = tmp_found; |
| 1039 | search = 0; /* success */ |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 1040 | } else if (tmp_found) { |
| 1041 | /* |
| 1042 | * Don't show bits which we'll be returning |
| 1043 | * for allocation to the local alloc bitmap. |
| 1044 | */ |
| 1045 | ocfs2_local_alloc_seen_free_bits(osb, tmp_found); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | return search; |
| 1050 | } |
| 1051 | |
| 1052 | static int ocfs2_block_group_search(struct inode *inode, |
| 1053 | struct buffer_head *group_bh, |
| 1054 | u32 bits_wanted, u32 min_bits, |
| 1055 | u16 *bit_off, u16 *bits_found) |
| 1056 | { |
| 1057 | int ret = -ENOSPC; |
| 1058 | struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data; |
| 1059 | |
| 1060 | BUG_ON(min_bits != 1); |
| 1061 | BUG_ON(ocfs2_is_cluster_bitmap(inode)); |
| 1062 | |
| 1063 | if (bg->bg_free_bits_count) |
| 1064 | ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb), |
| 1065 | group_bh, bits_wanted, |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 1066 | le16_to_cpu(bg->bg_bits), |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1067 | bit_off, bits_found); |
| 1068 | |
| 1069 | return ret; |
| 1070 | } |
| 1071 | |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1072 | static int ocfs2_alloc_dinode_update_counts(struct inode *inode, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1073 | handle_t *handle, |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1074 | struct buffer_head *di_bh, |
| 1075 | u32 num_bits, |
| 1076 | u16 chain) |
| 1077 | { |
| 1078 | int ret; |
| 1079 | u32 tmp_used; |
| 1080 | struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; |
| 1081 | struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain; |
| 1082 | |
| 1083 | ret = ocfs2_journal_access(handle, inode, di_bh, |
| 1084 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1085 | if (ret < 0) { |
| 1086 | mlog_errno(ret); |
| 1087 | goto out; |
| 1088 | } |
| 1089 | |
| 1090 | tmp_used = le32_to_cpu(di->id1.bitmap1.i_used); |
| 1091 | di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used); |
| 1092 | le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits); |
| 1093 | |
| 1094 | ret = ocfs2_journal_dirty(handle, di_bh); |
| 1095 | if (ret < 0) |
| 1096 | mlog_errno(ret); |
| 1097 | |
| 1098 | out: |
| 1099 | return ret; |
| 1100 | } |
| 1101 | |
| 1102 | static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1103 | handle_t *handle, |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1104 | u32 bits_wanted, |
| 1105 | u32 min_bits, |
| 1106 | u16 *bit_off, |
| 1107 | unsigned int *num_bits, |
| 1108 | u64 gd_blkno, |
| 1109 | u16 *bits_left) |
| 1110 | { |
| 1111 | int ret; |
| 1112 | u16 found; |
| 1113 | struct buffer_head *group_bh = NULL; |
| 1114 | struct ocfs2_group_desc *gd; |
| 1115 | struct inode *alloc_inode = ac->ac_inode; |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1116 | |
| 1117 | ret = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), gd_blkno, |
| 1118 | &group_bh, OCFS2_BH_CACHED, alloc_inode); |
| 1119 | if (ret < 0) { |
| 1120 | mlog_errno(ret); |
| 1121 | return ret; |
| 1122 | } |
| 1123 | |
| 1124 | gd = (struct ocfs2_group_desc *) group_bh->b_data; |
| 1125 | if (!OCFS2_IS_VALID_GROUP_DESC(gd)) { |
| 1126 | OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, gd); |
| 1127 | ret = -EIO; |
| 1128 | goto out; |
| 1129 | } |
| 1130 | |
| 1131 | ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits, |
| 1132 | bit_off, &found); |
| 1133 | if (ret < 0) { |
| 1134 | if (ret != -ENOSPC) |
| 1135 | mlog_errno(ret); |
| 1136 | goto out; |
| 1137 | } |
| 1138 | |
| 1139 | *num_bits = found; |
| 1140 | |
| 1141 | ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh, |
| 1142 | *num_bits, |
| 1143 | le16_to_cpu(gd->bg_chain)); |
| 1144 | if (ret < 0) { |
| 1145 | mlog_errno(ret); |
| 1146 | goto out; |
| 1147 | } |
| 1148 | |
| 1149 | ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh, |
| 1150 | *bit_off, *num_bits); |
| 1151 | if (ret < 0) |
| 1152 | mlog_errno(ret); |
| 1153 | |
| 1154 | *bits_left = le16_to_cpu(gd->bg_free_bits_count); |
| 1155 | |
| 1156 | out: |
| 1157 | brelse(group_bh); |
| 1158 | |
| 1159 | return ret; |
| 1160 | } |
| 1161 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1162 | static int ocfs2_search_chain(struct ocfs2_alloc_context *ac, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1163 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1164 | u32 bits_wanted, |
| 1165 | u32 min_bits, |
| 1166 | u16 *bit_off, |
| 1167 | unsigned int *num_bits, |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1168 | u64 *bg_blkno, |
| 1169 | u16 *bits_left) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1170 | { |
| 1171 | int status; |
| 1172 | u16 chain, tmp_bits; |
| 1173 | u32 tmp_used; |
| 1174 | u64 next_group; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1175 | struct inode *alloc_inode = ac->ac_inode; |
| 1176 | struct buffer_head *group_bh = NULL; |
| 1177 | struct buffer_head *prev_group_bh = NULL; |
| 1178 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data; |
| 1179 | struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain; |
| 1180 | struct ocfs2_group_desc *bg; |
| 1181 | |
| 1182 | chain = ac->ac_chain; |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1183 | mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n", |
| 1184 | bits_wanted, chain, |
| 1185 | (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1186 | |
| 1187 | status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), |
| 1188 | le64_to_cpu(cl->cl_recs[chain].c_blkno), |
| 1189 | &group_bh, OCFS2_BH_CACHED, alloc_inode); |
| 1190 | if (status < 0) { |
| 1191 | mlog_errno(status); |
| 1192 | goto bail; |
| 1193 | } |
| 1194 | bg = (struct ocfs2_group_desc *) group_bh->b_data; |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 1195 | status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg); |
| 1196 | if (status) { |
| 1197 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1198 | goto bail; |
| 1199 | } |
| 1200 | |
| 1201 | status = -ENOSPC; |
| 1202 | /* for now, the chain search is a bit simplistic. We just use |
| 1203 | * the 1st group with any empty bits. */ |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 1204 | while ((status = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, |
| 1205 | min_bits, bit_off, &tmp_bits)) == -ENOSPC) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1206 | if (!bg->bg_next_group) |
| 1207 | break; |
| 1208 | |
| 1209 | if (prev_group_bh) { |
| 1210 | brelse(prev_group_bh); |
| 1211 | prev_group_bh = NULL; |
| 1212 | } |
| 1213 | next_group = le64_to_cpu(bg->bg_next_group); |
| 1214 | prev_group_bh = group_bh; |
| 1215 | group_bh = NULL; |
| 1216 | status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), |
| 1217 | next_group, &group_bh, |
| 1218 | OCFS2_BH_CACHED, alloc_inode); |
| 1219 | if (status < 0) { |
| 1220 | mlog_errno(status); |
| 1221 | goto bail; |
| 1222 | } |
| 1223 | bg = (struct ocfs2_group_desc *) group_bh->b_data; |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 1224 | status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg); |
| 1225 | if (status) { |
| 1226 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1227 | goto bail; |
| 1228 | } |
| 1229 | } |
| 1230 | if (status < 0) { |
| 1231 | if (status != -ENOSPC) |
| 1232 | mlog_errno(status); |
| 1233 | goto bail; |
| 1234 | } |
| 1235 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1236 | 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] | 1237 | tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1238 | |
| 1239 | *num_bits = tmp_bits; |
| 1240 | |
| 1241 | BUG_ON(*num_bits == 0); |
| 1242 | |
| 1243 | /* |
| 1244 | * Keep track of previous block descriptor read. When |
| 1245 | * we find a target, if we have read more than X |
| 1246 | * number of descriptors, and the target is reasonably |
| 1247 | * empty, relink him to top of his chain. |
| 1248 | * |
| 1249 | * We've read 0 extra blocks and only send one more to |
| 1250 | * the transaction, yet the next guy to search has a |
| 1251 | * much easier time. |
| 1252 | * |
| 1253 | * Do this *after* figuring out how many bits we're taking out |
| 1254 | * of our target group. |
| 1255 | */ |
| 1256 | if (ac->ac_allow_chain_relink && |
| 1257 | (prev_group_bh) && |
| 1258 | (ocfs2_block_group_reasonably_empty(bg, *num_bits))) { |
| 1259 | status = ocfs2_relink_block_group(handle, alloc_inode, |
| 1260 | ac->ac_bh, group_bh, |
| 1261 | prev_group_bh, chain); |
| 1262 | if (status < 0) { |
| 1263 | mlog_errno(status); |
| 1264 | goto bail; |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | /* Ok, claim our bits now: set the info on dinode, chainlist |
| 1269 | * and then the group */ |
| 1270 | status = ocfs2_journal_access(handle, |
| 1271 | alloc_inode, |
| 1272 | ac->ac_bh, |
| 1273 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1274 | if (status < 0) { |
| 1275 | mlog_errno(status); |
| 1276 | goto bail; |
| 1277 | } |
| 1278 | |
| 1279 | tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used); |
| 1280 | fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used); |
| 1281 | le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits)); |
| 1282 | |
| 1283 | status = ocfs2_journal_dirty(handle, |
| 1284 | ac->ac_bh); |
| 1285 | if (status < 0) { |
| 1286 | mlog_errno(status); |
| 1287 | goto bail; |
| 1288 | } |
| 1289 | |
| 1290 | status = ocfs2_block_group_set_bits(handle, |
| 1291 | alloc_inode, |
| 1292 | bg, |
| 1293 | group_bh, |
| 1294 | *bit_off, |
| 1295 | *num_bits); |
| 1296 | if (status < 0) { |
| 1297 | mlog_errno(status); |
| 1298 | goto bail; |
| 1299 | } |
| 1300 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1301 | mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits, |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 1302 | (unsigned long long)le64_to_cpu(fe->i_blkno)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1303 | |
| 1304 | *bg_blkno = le64_to_cpu(bg->bg_blkno); |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1305 | *bits_left = le16_to_cpu(bg->bg_free_bits_count); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1306 | bail: |
| 1307 | if (group_bh) |
| 1308 | brelse(group_bh); |
| 1309 | if (prev_group_bh) |
| 1310 | brelse(prev_group_bh); |
| 1311 | |
| 1312 | mlog_exit(status); |
| 1313 | return status; |
| 1314 | } |
| 1315 | |
| 1316 | /* will give out up to bits_wanted contiguous bits. */ |
| 1317 | static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb, |
| 1318 | struct ocfs2_alloc_context *ac, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1319 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1320 | u32 bits_wanted, |
| 1321 | u32 min_bits, |
| 1322 | u16 *bit_off, |
| 1323 | unsigned int *num_bits, |
| 1324 | u64 *bg_blkno) |
| 1325 | { |
| 1326 | int status; |
| 1327 | u16 victim, i; |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1328 | u16 bits_left = 0; |
| 1329 | u64 hint_blkno = ac->ac_last_group; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1330 | struct ocfs2_chain_list *cl; |
| 1331 | struct ocfs2_dinode *fe; |
| 1332 | |
| 1333 | mlog_entry_void(); |
| 1334 | |
| 1335 | BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted); |
| 1336 | BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given)); |
| 1337 | BUG_ON(!ac->ac_bh); |
| 1338 | |
| 1339 | fe = (struct ocfs2_dinode *) ac->ac_bh->b_data; |
| 1340 | if (!OCFS2_IS_VALID_DINODE(fe)) { |
| 1341 | OCFS2_RO_ON_INVALID_DINODE(osb->sb, fe); |
| 1342 | status = -EIO; |
| 1343 | goto bail; |
| 1344 | } |
| 1345 | if (le32_to_cpu(fe->id1.bitmap1.i_used) >= |
| 1346 | le32_to_cpu(fe->id1.bitmap1.i_total)) { |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1347 | ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used " |
| 1348 | "bits but only %u total.", |
| 1349 | (unsigned long long)le64_to_cpu(fe->i_blkno), |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1350 | le32_to_cpu(fe->id1.bitmap1.i_used), |
| 1351 | le32_to_cpu(fe->id1.bitmap1.i_total)); |
| 1352 | status = -EIO; |
| 1353 | goto bail; |
| 1354 | } |
| 1355 | |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1356 | if (hint_blkno) { |
| 1357 | /* Attempt to short-circuit the usual search mechanism |
| 1358 | * by jumping straight to the most recently used |
| 1359 | * allocation group. This helps us mantain some |
| 1360 | * contiguousness across allocations. */ |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1361 | status = ocfs2_search_one_group(ac, handle, bits_wanted, |
| 1362 | min_bits, bit_off, num_bits, |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1363 | hint_blkno, &bits_left); |
| 1364 | if (!status) { |
| 1365 | /* Be careful to update *bg_blkno here as the |
| 1366 | * caller is expecting it to be filled in, and |
| 1367 | * ocfs2_search_one_group() won't do that for |
| 1368 | * us. */ |
| 1369 | *bg_blkno = hint_blkno; |
| 1370 | goto set_hint; |
| 1371 | } |
| 1372 | if (status < 0 && status != -ENOSPC) { |
| 1373 | mlog_errno(status); |
| 1374 | goto bail; |
| 1375 | } |
| 1376 | } |
| 1377 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1378 | cl = (struct ocfs2_chain_list *) &fe->id2.i_chain; |
| 1379 | |
| 1380 | victim = ocfs2_find_victim_chain(cl); |
| 1381 | ac->ac_chain = victim; |
| 1382 | ac->ac_allow_chain_relink = 1; |
| 1383 | |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1384 | status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off, |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1385 | num_bits, bg_blkno, &bits_left); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1386 | if (!status) |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1387 | goto set_hint; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1388 | if (status < 0 && status != -ENOSPC) { |
| 1389 | mlog_errno(status); |
| 1390 | goto bail; |
| 1391 | } |
| 1392 | |
| 1393 | mlog(0, "Search of victim chain %u came up with nothing, " |
| 1394 | "trying all chains now.\n", victim); |
| 1395 | |
| 1396 | /* If we didn't pick a good victim, then just default to |
| 1397 | * searching each chain in order. Don't allow chain relinking |
| 1398 | * because we only calculate enough journal credits for one |
| 1399 | * relink per alloc. */ |
| 1400 | ac->ac_allow_chain_relink = 0; |
| 1401 | for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) { |
| 1402 | if (i == victim) |
| 1403 | continue; |
| 1404 | if (!cl->cl_recs[i].c_free) |
| 1405 | continue; |
| 1406 | |
| 1407 | ac->ac_chain = i; |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1408 | status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1409 | bit_off, num_bits, bg_blkno, |
| 1410 | &bits_left); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1411 | if (!status) |
| 1412 | break; |
| 1413 | if (status < 0 && status != -ENOSPC) { |
| 1414 | mlog_errno(status); |
| 1415 | goto bail; |
| 1416 | } |
| 1417 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1418 | |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1419 | set_hint: |
| 1420 | if (status != -ENOSPC) { |
| 1421 | /* If the next search of this group is not likely to |
| 1422 | * yield a suitable extent, then we reset the last |
| 1423 | * group hint so as to not waste a disk read */ |
| 1424 | if (bits_left < min_bits) |
| 1425 | ac->ac_last_group = 0; |
| 1426 | else |
| 1427 | ac->ac_last_group = *bg_blkno; |
| 1428 | } |
| 1429 | |
| 1430 | bail: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1431 | mlog_exit(status); |
| 1432 | return status; |
| 1433 | } |
| 1434 | |
| 1435 | int ocfs2_claim_metadata(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1436 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1437 | struct ocfs2_alloc_context *ac, |
| 1438 | u32 bits_wanted, |
| 1439 | u16 *suballoc_bit_start, |
| 1440 | unsigned int *num_bits, |
| 1441 | u64 *blkno_start) |
| 1442 | { |
| 1443 | int status; |
| 1444 | u64 bg_blkno; |
| 1445 | |
| 1446 | BUG_ON(!ac); |
| 1447 | BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted)); |
| 1448 | BUG_ON(ac->ac_which != OCFS2_AC_USE_META); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1449 | |
| 1450 | status = ocfs2_claim_suballoc_bits(osb, |
| 1451 | ac, |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1452 | handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1453 | bits_wanted, |
| 1454 | 1, |
| 1455 | suballoc_bit_start, |
| 1456 | num_bits, |
| 1457 | &bg_blkno); |
| 1458 | if (status < 0) { |
| 1459 | mlog_errno(status); |
| 1460 | goto bail; |
| 1461 | } |
| 1462 | atomic_inc(&osb->alloc_stats.bg_allocs); |
| 1463 | |
| 1464 | *blkno_start = bg_blkno + (u64) *suballoc_bit_start; |
| 1465 | ac->ac_bits_given += (*num_bits); |
| 1466 | status = 0; |
| 1467 | bail: |
| 1468 | mlog_exit(status); |
| 1469 | return status; |
| 1470 | } |
| 1471 | |
| 1472 | int ocfs2_claim_new_inode(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1473 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1474 | struct ocfs2_alloc_context *ac, |
| 1475 | u16 *suballoc_bit, |
| 1476 | u64 *fe_blkno) |
| 1477 | { |
| 1478 | int status; |
| 1479 | unsigned int num_bits; |
| 1480 | u64 bg_blkno; |
| 1481 | |
| 1482 | mlog_entry_void(); |
| 1483 | |
| 1484 | BUG_ON(!ac); |
| 1485 | BUG_ON(ac->ac_bits_given != 0); |
| 1486 | BUG_ON(ac->ac_bits_wanted != 1); |
| 1487 | BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1488 | |
| 1489 | status = ocfs2_claim_suballoc_bits(osb, |
| 1490 | ac, |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1491 | handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1492 | 1, |
| 1493 | 1, |
| 1494 | suballoc_bit, |
| 1495 | &num_bits, |
| 1496 | &bg_blkno); |
| 1497 | if (status < 0) { |
| 1498 | mlog_errno(status); |
| 1499 | goto bail; |
| 1500 | } |
| 1501 | atomic_inc(&osb->alloc_stats.bg_allocs); |
| 1502 | |
| 1503 | BUG_ON(num_bits != 1); |
| 1504 | |
| 1505 | *fe_blkno = bg_blkno + (u64) (*suballoc_bit); |
| 1506 | ac->ac_bits_given++; |
| 1507 | status = 0; |
| 1508 | bail: |
| 1509 | mlog_exit(status); |
| 1510 | return status; |
| 1511 | } |
| 1512 | |
| 1513 | /* translate a group desc. blkno and it's bitmap offset into |
| 1514 | * disk cluster offset. */ |
| 1515 | static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode, |
| 1516 | u64 bg_blkno, |
| 1517 | u16 bg_bit_off) |
| 1518 | { |
| 1519 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1520 | u32 cluster = 0; |
| 1521 | |
| 1522 | BUG_ON(!ocfs2_is_cluster_bitmap(inode)); |
| 1523 | |
| 1524 | if (bg_blkno != osb->first_cluster_group_blkno) |
| 1525 | cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno); |
| 1526 | cluster += (u32) bg_bit_off; |
| 1527 | return cluster; |
| 1528 | } |
| 1529 | |
| 1530 | /* given a cluster offset, calculate which block group it belongs to |
| 1531 | * and return that block offset. */ |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 1532 | u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1533 | { |
| 1534 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1535 | u32 group_no; |
| 1536 | |
| 1537 | BUG_ON(!ocfs2_is_cluster_bitmap(inode)); |
| 1538 | |
| 1539 | group_no = cluster / osb->bitmap_cpg; |
| 1540 | if (!group_no) |
| 1541 | return osb->first_cluster_group_blkno; |
| 1542 | return ocfs2_clusters_to_blocks(inode->i_sb, |
| 1543 | group_no * osb->bitmap_cpg); |
| 1544 | } |
| 1545 | |
| 1546 | /* given the block number of a cluster start, calculate which cluster |
| 1547 | * group and descriptor bitmap offset that corresponds to. */ |
| 1548 | static inline void ocfs2_block_to_cluster_group(struct inode *inode, |
| 1549 | u64 data_blkno, |
| 1550 | u64 *bg_blkno, |
| 1551 | u16 *bg_bit_off) |
| 1552 | { |
| 1553 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1554 | u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno); |
| 1555 | |
| 1556 | BUG_ON(!ocfs2_is_cluster_bitmap(inode)); |
| 1557 | |
| 1558 | *bg_blkno = ocfs2_which_cluster_group(inode, |
| 1559 | data_cluster); |
| 1560 | |
| 1561 | if (*bg_blkno == osb->first_cluster_group_blkno) |
| 1562 | *bg_bit_off = (u16) data_cluster; |
| 1563 | else |
| 1564 | *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb, |
| 1565 | data_blkno - *bg_blkno); |
| 1566 | } |
| 1567 | |
| 1568 | /* |
| 1569 | * min_bits - minimum contiguous chunk from this total allocation we |
| 1570 | * can handle. set to what we asked for originally for a full |
| 1571 | * contig. allocation, set to '1' to indicate we can deal with extents |
| 1572 | * of any size. |
| 1573 | */ |
Mark Fasheh | 415cb80 | 2007-09-16 20:10:16 -0700 | [diff] [blame] | 1574 | int __ocfs2_claim_clusters(struct ocfs2_super *osb, |
| 1575 | handle_t *handle, |
| 1576 | struct ocfs2_alloc_context *ac, |
| 1577 | u32 min_clusters, |
| 1578 | u32 max_clusters, |
| 1579 | u32 *cluster_start, |
| 1580 | u32 *num_clusters) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1581 | { |
| 1582 | int status; |
Mark Fasheh | 415cb80 | 2007-09-16 20:10:16 -0700 | [diff] [blame] | 1583 | unsigned int bits_wanted = max_clusters; |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1584 | u64 bg_blkno = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1585 | u16 bg_bit_off; |
| 1586 | |
| 1587 | mlog_entry_void(); |
| 1588 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1589 | BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted); |
| 1590 | |
| 1591 | BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL |
| 1592 | && ac->ac_which != OCFS2_AC_USE_MAIN); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1593 | |
| 1594 | if (ac->ac_which == OCFS2_AC_USE_LOCAL) { |
| 1595 | status = ocfs2_claim_local_alloc_bits(osb, |
| 1596 | handle, |
| 1597 | ac, |
| 1598 | bits_wanted, |
| 1599 | cluster_start, |
| 1600 | num_clusters); |
| 1601 | if (!status) |
| 1602 | atomic_inc(&osb->alloc_stats.local_data); |
| 1603 | } else { |
| 1604 | if (min_clusters > (osb->bitmap_cpg - 1)) { |
| 1605 | /* The only paths asking for contiguousness |
| 1606 | * should know about this already. */ |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 1607 | mlog(ML_ERROR, "minimum allocation requested %u exceeds " |
| 1608 | "group bitmap size %u!\n", min_clusters, |
| 1609 | osb->bitmap_cpg); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1610 | status = -ENOSPC; |
| 1611 | goto bail; |
| 1612 | } |
| 1613 | /* clamp the current request down to a realistic size. */ |
| 1614 | if (bits_wanted > (osb->bitmap_cpg - 1)) |
| 1615 | bits_wanted = osb->bitmap_cpg - 1; |
| 1616 | |
| 1617 | status = ocfs2_claim_suballoc_bits(osb, |
| 1618 | ac, |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1619 | handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1620 | bits_wanted, |
| 1621 | min_clusters, |
| 1622 | &bg_bit_off, |
| 1623 | num_clusters, |
| 1624 | &bg_blkno); |
| 1625 | if (!status) { |
| 1626 | *cluster_start = |
| 1627 | ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode, |
| 1628 | bg_blkno, |
| 1629 | bg_bit_off); |
| 1630 | atomic_inc(&osb->alloc_stats.bitmap_data); |
| 1631 | } |
| 1632 | } |
| 1633 | if (status < 0) { |
| 1634 | if (status != -ENOSPC) |
| 1635 | mlog_errno(status); |
| 1636 | goto bail; |
| 1637 | } |
| 1638 | |
| 1639 | ac->ac_bits_given += *num_clusters; |
| 1640 | |
| 1641 | bail: |
| 1642 | mlog_exit(status); |
| 1643 | return status; |
| 1644 | } |
| 1645 | |
Mark Fasheh | 415cb80 | 2007-09-16 20:10:16 -0700 | [diff] [blame] | 1646 | int ocfs2_claim_clusters(struct ocfs2_super *osb, |
| 1647 | handle_t *handle, |
| 1648 | struct ocfs2_alloc_context *ac, |
| 1649 | u32 min_clusters, |
| 1650 | u32 *cluster_start, |
| 1651 | u32 *num_clusters) |
| 1652 | { |
| 1653 | unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given; |
| 1654 | |
| 1655 | return __ocfs2_claim_clusters(osb, handle, ac, min_clusters, |
| 1656 | bits_wanted, cluster_start, num_clusters); |
| 1657 | } |
| 1658 | |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1659 | static inline int ocfs2_block_group_clear_bits(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1660 | struct inode *alloc_inode, |
| 1661 | struct ocfs2_group_desc *bg, |
| 1662 | struct buffer_head *group_bh, |
| 1663 | unsigned int bit_off, |
| 1664 | unsigned int num_bits) |
| 1665 | { |
| 1666 | int status; |
| 1667 | unsigned int tmp; |
| 1668 | int journal_type = OCFS2_JOURNAL_ACCESS_WRITE; |
| 1669 | struct ocfs2_group_desc *undo_bg = NULL; |
| 1670 | |
| 1671 | mlog_entry_void(); |
| 1672 | |
| 1673 | if (!OCFS2_IS_VALID_GROUP_DESC(bg)) { |
| 1674 | OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg); |
| 1675 | status = -EIO; |
| 1676 | goto bail; |
| 1677 | } |
| 1678 | |
| 1679 | mlog(0, "off = %u, num = %u\n", bit_off, num_bits); |
| 1680 | |
| 1681 | if (ocfs2_is_cluster_bitmap(alloc_inode)) |
| 1682 | journal_type = OCFS2_JOURNAL_ACCESS_UNDO; |
| 1683 | |
| 1684 | status = ocfs2_journal_access(handle, alloc_inode, group_bh, |
| 1685 | journal_type); |
| 1686 | if (status < 0) { |
| 1687 | mlog_errno(status); |
| 1688 | goto bail; |
| 1689 | } |
| 1690 | |
| 1691 | if (ocfs2_is_cluster_bitmap(alloc_inode)) |
| 1692 | undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data; |
| 1693 | |
| 1694 | tmp = num_bits; |
| 1695 | while(tmp--) { |
| 1696 | ocfs2_clear_bit((bit_off + tmp), |
| 1697 | (unsigned long *) bg->bg_bitmap); |
| 1698 | if (ocfs2_is_cluster_bitmap(alloc_inode)) |
| 1699 | ocfs2_set_bit(bit_off + tmp, |
| 1700 | (unsigned long *) undo_bg->bg_bitmap); |
| 1701 | } |
| 1702 | le16_add_cpu(&bg->bg_free_bits_count, num_bits); |
| 1703 | |
| 1704 | status = ocfs2_journal_dirty(handle, group_bh); |
| 1705 | if (status < 0) |
| 1706 | mlog_errno(status); |
| 1707 | bail: |
| 1708 | return status; |
| 1709 | } |
| 1710 | |
| 1711 | /* |
| 1712 | * expects the suballoc inode to already be locked. |
| 1713 | */ |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 1714 | int ocfs2_free_suballoc_bits(handle_t *handle, |
| 1715 | struct inode *alloc_inode, |
| 1716 | struct buffer_head *alloc_bh, |
| 1717 | unsigned int start_bit, |
| 1718 | u64 bg_blkno, |
| 1719 | unsigned int count) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1720 | { |
| 1721 | int status = 0; |
| 1722 | u32 tmp_used; |
| 1723 | struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb); |
| 1724 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data; |
| 1725 | struct ocfs2_chain_list *cl = &fe->id2.i_chain; |
| 1726 | struct buffer_head *group_bh = NULL; |
| 1727 | struct ocfs2_group_desc *group; |
| 1728 | |
| 1729 | mlog_entry_void(); |
| 1730 | |
| 1731 | if (!OCFS2_IS_VALID_DINODE(fe)) { |
| 1732 | OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe); |
| 1733 | status = -EIO; |
| 1734 | goto bail; |
| 1735 | } |
| 1736 | BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl)); |
| 1737 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1738 | mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n", |
| 1739 | (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count, |
| 1740 | (unsigned long long)bg_blkno, start_bit); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1741 | |
| 1742 | status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED, |
| 1743 | alloc_inode); |
| 1744 | if (status < 0) { |
| 1745 | mlog_errno(status); |
| 1746 | goto bail; |
| 1747 | } |
| 1748 | |
| 1749 | group = (struct ocfs2_group_desc *) group_bh->b_data; |
Mark Fasheh | 7bf72ed | 2006-05-03 17:46:50 -0700 | [diff] [blame] | 1750 | status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, group); |
| 1751 | if (status) { |
| 1752 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1753 | goto bail; |
| 1754 | } |
| 1755 | BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits)); |
| 1756 | |
| 1757 | status = ocfs2_block_group_clear_bits(handle, alloc_inode, |
| 1758 | group, group_bh, |
| 1759 | start_bit, count); |
| 1760 | if (status < 0) { |
| 1761 | mlog_errno(status); |
| 1762 | goto bail; |
| 1763 | } |
| 1764 | |
| 1765 | status = ocfs2_journal_access(handle, alloc_inode, alloc_bh, |
| 1766 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1767 | if (status < 0) { |
| 1768 | mlog_errno(status); |
| 1769 | goto bail; |
| 1770 | } |
| 1771 | |
| 1772 | le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free, |
| 1773 | count); |
| 1774 | tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used); |
| 1775 | fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count); |
| 1776 | |
| 1777 | status = ocfs2_journal_dirty(handle, alloc_bh); |
| 1778 | if (status < 0) { |
| 1779 | mlog_errno(status); |
| 1780 | goto bail; |
| 1781 | } |
| 1782 | |
| 1783 | bail: |
| 1784 | if (group_bh) |
| 1785 | brelse(group_bh); |
| 1786 | |
| 1787 | mlog_exit(status); |
| 1788 | return status; |
| 1789 | } |
| 1790 | |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1791 | int ocfs2_free_dinode(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1792 | struct inode *inode_alloc_inode, |
| 1793 | struct buffer_head *inode_alloc_bh, |
| 1794 | struct ocfs2_dinode *di) |
| 1795 | { |
| 1796 | u64 blk = le64_to_cpu(di->i_blkno); |
| 1797 | u16 bit = le16_to_cpu(di->i_suballoc_bit); |
| 1798 | u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit); |
| 1799 | |
| 1800 | return ocfs2_free_suballoc_bits(handle, inode_alloc_inode, |
| 1801 | inode_alloc_bh, bit, bg_blkno, 1); |
| 1802 | } |
| 1803 | |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1804 | int ocfs2_free_clusters(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1805 | struct inode *bitmap_inode, |
| 1806 | struct buffer_head *bitmap_bh, |
| 1807 | u64 start_blk, |
| 1808 | unsigned int num_clusters) |
| 1809 | { |
| 1810 | int status; |
| 1811 | u16 bg_start_bit; |
| 1812 | u64 bg_blkno; |
| 1813 | struct ocfs2_dinode *fe; |
| 1814 | |
| 1815 | /* You can't ever have a contiguous set of clusters |
| 1816 | * bigger than a block group bitmap so we never have to worry |
| 1817 | * about looping on them. */ |
| 1818 | |
| 1819 | mlog_entry_void(); |
| 1820 | |
| 1821 | /* This is expensive. We can safely remove once this stuff has |
| 1822 | * gotten tested really well. */ |
| 1823 | BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk))); |
| 1824 | |
| 1825 | fe = (struct ocfs2_dinode *) bitmap_bh->b_data; |
| 1826 | |
| 1827 | ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno, |
| 1828 | &bg_start_bit); |
| 1829 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1830 | mlog(0, "want to free %u clusters starting at block %llu\n", |
| 1831 | num_clusters, (unsigned long long)start_blk); |
| 1832 | mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n", |
| 1833 | (unsigned long long)bg_blkno, bg_start_bit); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1834 | |
| 1835 | status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh, |
| 1836 | bg_start_bit, bg_blkno, |
| 1837 | num_clusters); |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 1838 | if (status < 0) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1839 | mlog_errno(status); |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 1840 | goto out; |
| 1841 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1842 | |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 1843 | ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb), |
| 1844 | num_clusters); |
| 1845 | |
| 1846 | out: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1847 | mlog_exit(status); |
| 1848 | return status; |
| 1849 | } |
| 1850 | |
| 1851 | static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg) |
| 1852 | { |
| 1853 | printk("Block Group:\n"); |
| 1854 | printk("bg_signature: %s\n", bg->bg_signature); |
| 1855 | printk("bg_size: %u\n", bg->bg_size); |
| 1856 | printk("bg_bits: %u\n", bg->bg_bits); |
| 1857 | printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count); |
| 1858 | printk("bg_chain: %u\n", bg->bg_chain); |
| 1859 | printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation)); |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1860 | printk("bg_next_group: %llu\n", |
| 1861 | (unsigned long long)bg->bg_next_group); |
| 1862 | printk("bg_parent_dinode: %llu\n", |
| 1863 | (unsigned long long)bg->bg_parent_dinode); |
| 1864 | printk("bg_blkno: %llu\n", |
| 1865 | (unsigned long long)bg->bg_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe) |
| 1869 | { |
| 1870 | int i; |
| 1871 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1872 | printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1873 | printk("i_signature: %s\n", fe->i_signature); |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1874 | printk("i_size: %llu\n", |
| 1875 | (unsigned long long)fe->i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1876 | printk("i_clusters: %u\n", fe->i_clusters); |
| 1877 | printk("i_generation: %u\n", |
| 1878 | le32_to_cpu(fe->i_generation)); |
| 1879 | printk("id1.bitmap1.i_used: %u\n", |
| 1880 | le32_to_cpu(fe->id1.bitmap1.i_used)); |
| 1881 | printk("id1.bitmap1.i_total: %u\n", |
| 1882 | le32_to_cpu(fe->id1.bitmap1.i_total)); |
| 1883 | printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg); |
| 1884 | printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc); |
| 1885 | printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count); |
| 1886 | printk("id2.i_chain.cl_next_free_rec: %u\n", |
| 1887 | fe->id2.i_chain.cl_next_free_rec); |
| 1888 | for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) { |
| 1889 | printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i, |
| 1890 | fe->id2.i_chain.cl_recs[i].c_free); |
| 1891 | printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i, |
| 1892 | fe->id2.i_chain.cl_recs[i].c_total); |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1893 | printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i, |
| 1894 | (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1895 | } |
| 1896 | } |
Tao Ma | e7d4cb6 | 2008-08-18 17:38:44 +0800 | [diff] [blame^] | 1897 | |
| 1898 | /* |
| 1899 | * For a given allocation, determine which allocators will need to be |
| 1900 | * accessed, and lock them, reserving the appropriate number of bits. |
| 1901 | * |
| 1902 | * Sparse file systems call this from ocfs2_write_begin_nolock() |
| 1903 | * and ocfs2_allocate_unwritten_extents(). |
| 1904 | * |
| 1905 | * File systems which don't support holes call this from |
| 1906 | * ocfs2_extend_allocation(). |
| 1907 | */ |
| 1908 | int ocfs2_lock_allocators(struct inode *inode, struct buffer_head *root_bh, |
| 1909 | struct ocfs2_extent_list *root_el, |
| 1910 | u32 clusters_to_add, u32 extents_to_split, |
| 1911 | struct ocfs2_alloc_context **data_ac, |
| 1912 | struct ocfs2_alloc_context **meta_ac) |
| 1913 | { |
| 1914 | int ret = 0, num_free_extents; |
| 1915 | unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split; |
| 1916 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1917 | |
| 1918 | *meta_ac = NULL; |
| 1919 | if (data_ac) |
| 1920 | *data_ac = NULL; |
| 1921 | |
| 1922 | BUG_ON(clusters_to_add != 0 && data_ac == NULL); |
| 1923 | |
| 1924 | num_free_extents = ocfs2_num_free_extents(osb, inode, root_bh, |
| 1925 | OCFS2_DINODE_EXTENT); |
| 1926 | if (num_free_extents < 0) { |
| 1927 | ret = num_free_extents; |
| 1928 | mlog_errno(ret); |
| 1929 | goto out; |
| 1930 | } |
| 1931 | |
| 1932 | /* |
| 1933 | * Sparse allocation file systems need to be more conservative |
| 1934 | * with reserving room for expansion - the actual allocation |
| 1935 | * happens while we've got a journal handle open so re-taking |
| 1936 | * a cluster lock (because we ran out of room for another |
| 1937 | * extent) will violate ordering rules. |
| 1938 | * |
| 1939 | * Most of the time we'll only be seeing this 1 cluster at a time |
| 1940 | * anyway. |
| 1941 | * |
| 1942 | * Always lock for any unwritten extents - we might want to |
| 1943 | * add blocks during a split. |
| 1944 | */ |
| 1945 | if (!num_free_extents || |
| 1946 | (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) { |
| 1947 | ret = ocfs2_reserve_new_metadata(osb, root_el, meta_ac); |
| 1948 | if (ret < 0) { |
| 1949 | if (ret != -ENOSPC) |
| 1950 | mlog_errno(ret); |
| 1951 | goto out; |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | if (clusters_to_add == 0) |
| 1956 | goto out; |
| 1957 | |
| 1958 | ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac); |
| 1959 | if (ret < 0) { |
| 1960 | if (ret != -ENOSPC) |
| 1961 | mlog_errno(ret); |
| 1962 | goto out; |
| 1963 | } |
| 1964 | |
| 1965 | out: |
| 1966 | if (ret) { |
| 1967 | if (*meta_ac) { |
| 1968 | ocfs2_free_alloc_context(*meta_ac); |
| 1969 | *meta_ac = NULL; |
| 1970 | } |
| 1971 | |
| 1972 | /* |
| 1973 | * We cannot have an error and a non null *data_ac. |
| 1974 | */ |
| 1975 | } |
| 1976 | |
| 1977 | return ret; |
| 1978 | } |