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