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