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