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 | * localalloc.c |
| 5 | * |
| 6 | * Node local data allocation |
| 7 | * |
| 8 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public |
| 21 | * License along with this program; if not, write to the |
| 22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | * Boston, MA 021110-1307, USA. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/fs.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/highmem.h> |
| 30 | #include <linux/bitops.h> |
| 31 | |
| 32 | #define MLOG_MASK_PREFIX ML_DISK_ALLOC |
| 33 | #include <cluster/masklog.h> |
| 34 | |
| 35 | #include "ocfs2.h" |
| 36 | |
| 37 | #include "alloc.h" |
| 38 | #include "dlmglue.h" |
| 39 | #include "inode.h" |
| 40 | #include "journal.h" |
| 41 | #include "localalloc.h" |
| 42 | #include "suballoc.h" |
| 43 | #include "super.h" |
| 44 | #include "sysfile.h" |
| 45 | |
| 46 | #include "buffer_head_io.h" |
| 47 | |
| 48 | #define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab)) |
| 49 | |
| 50 | static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb); |
| 51 | |
| 52 | static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc); |
| 53 | |
| 54 | static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb, |
| 55 | struct ocfs2_dinode *alloc, |
| 56 | u32 numbits); |
| 57 | |
| 58 | static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc); |
| 59 | |
| 60 | static int ocfs2_sync_local_to_main(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 61 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 62 | struct ocfs2_dinode *alloc, |
| 63 | struct inode *main_bm_inode, |
| 64 | struct buffer_head *main_bm_bh); |
| 65 | |
| 66 | static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 67 | struct ocfs2_alloc_context **ac, |
| 68 | struct inode **bitmap_inode, |
| 69 | struct buffer_head **bitmap_bh); |
| 70 | |
| 71 | static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 72 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 73 | struct ocfs2_alloc_context *ac); |
| 74 | |
| 75 | static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb, |
| 76 | struct inode *local_alloc_inode); |
| 77 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 78 | static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb) |
| 79 | { |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 80 | BUG_ON(osb->s_clustersize_bits > 20); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 81 | |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 82 | /* Size local alloc windows by the megabyte */ |
| 83 | return osb->local_alloc_size << (20 - osb->s_clustersize_bits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Tell us whether a given allocation should use the local alloc |
| 88 | * file. Otherwise, it has to go to the main bitmap. |
| 89 | */ |
| 90 | int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits) |
| 91 | { |
| 92 | int la_bits = ocfs2_local_alloc_window_bits(osb); |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 93 | int ret = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 94 | |
| 95 | if (osb->local_alloc_state != OCFS2_LA_ENABLED) |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 96 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 97 | |
| 98 | /* la_bits should be at least twice the size (in clusters) of |
| 99 | * a new block group. We want to be sure block group |
| 100 | * allocations go through the local alloc, so allow an |
| 101 | * allocation to take up to half the bitmap. */ |
| 102 | if (bits > (la_bits / 2)) |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 103 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 104 | |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 105 | ret = 1; |
| 106 | bail: |
| 107 | mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n", |
| 108 | osb->local_alloc_state, (unsigned long long)bits, la_bits, ret); |
| 109 | return ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int ocfs2_load_local_alloc(struct ocfs2_super *osb) |
| 113 | { |
| 114 | int status = 0; |
| 115 | struct ocfs2_dinode *alloc = NULL; |
| 116 | struct buffer_head *alloc_bh = NULL; |
| 117 | u32 num_used; |
| 118 | struct inode *inode = NULL; |
| 119 | struct ocfs2_local_alloc *la; |
| 120 | |
| 121 | mlog_entry_void(); |
| 122 | |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 123 | if (osb->local_alloc_size == 0) |
| 124 | goto bail; |
| 125 | |
| 126 | if (ocfs2_local_alloc_window_bits(osb) >= osb->bitmap_cpg) { |
| 127 | mlog(ML_NOTICE, "Requested local alloc window %d is larger " |
| 128 | "than max possible %u. Using defaults.\n", |
| 129 | ocfs2_local_alloc_window_bits(osb), (osb->bitmap_cpg - 1)); |
| 130 | osb->local_alloc_size = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE; |
| 131 | } |
| 132 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 133 | /* read the alloc off disk */ |
| 134 | inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE, |
| 135 | osb->slot_num); |
| 136 | if (!inode) { |
| 137 | status = -EINVAL; |
| 138 | mlog_errno(status); |
| 139 | goto bail; |
| 140 | } |
| 141 | |
| 142 | status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, |
| 143 | &alloc_bh, 0, inode); |
| 144 | if (status < 0) { |
| 145 | mlog_errno(status); |
| 146 | goto bail; |
| 147 | } |
| 148 | |
| 149 | alloc = (struct ocfs2_dinode *) alloc_bh->b_data; |
| 150 | la = OCFS2_LOCAL_ALLOC(alloc); |
| 151 | |
| 152 | if (!(le32_to_cpu(alloc->i_flags) & |
| 153 | (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 154 | mlog(ML_ERROR, "Invalid local alloc inode, %llu\n", |
| 155 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 156 | status = -EINVAL; |
| 157 | goto bail; |
| 158 | } |
| 159 | |
| 160 | if ((la->la_size == 0) || |
| 161 | (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) { |
| 162 | mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n", |
| 163 | le16_to_cpu(la->la_size)); |
| 164 | status = -EINVAL; |
| 165 | goto bail; |
| 166 | } |
| 167 | |
| 168 | /* do a little verification. */ |
| 169 | num_used = ocfs2_local_alloc_count_bits(alloc); |
| 170 | |
| 171 | /* hopefully the local alloc has always been recovered before |
| 172 | * we load it. */ |
| 173 | if (num_used |
| 174 | || alloc->id1.bitmap1.i_used |
| 175 | || alloc->id1.bitmap1.i_total |
| 176 | || la->la_bm_off) |
| 177 | mlog(ML_ERROR, "Local alloc hasn't been recovered!\n" |
| 178 | "found = %u, set = %u, taken = %u, off = %u\n", |
| 179 | num_used, le32_to_cpu(alloc->id1.bitmap1.i_used), |
| 180 | le32_to_cpu(alloc->id1.bitmap1.i_total), |
| 181 | OCFS2_LOCAL_ALLOC(alloc)->la_bm_off); |
| 182 | |
| 183 | osb->local_alloc_bh = alloc_bh; |
| 184 | osb->local_alloc_state = OCFS2_LA_ENABLED; |
| 185 | |
| 186 | bail: |
| 187 | if (status < 0) |
| 188 | if (alloc_bh) |
| 189 | brelse(alloc_bh); |
| 190 | if (inode) |
| 191 | iput(inode); |
| 192 | |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 193 | mlog(0, "Local alloc window bits = %d\n", |
| 194 | ocfs2_local_alloc_window_bits(osb)); |
| 195 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 196 | mlog_exit(status); |
| 197 | return status; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * return any unused bits to the bitmap and write out a clean |
| 202 | * local_alloc. |
| 203 | * |
| 204 | * local_alloc_bh is optional. If not passed, we will simply use the |
| 205 | * one off osb. If you do pass it however, be warned that it *will* be |
| 206 | * returned brelse'd and NULL'd out.*/ |
| 207 | void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb) |
| 208 | { |
| 209 | int status; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 210 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 211 | struct inode *local_alloc_inode = NULL; |
| 212 | struct buffer_head *bh = NULL; |
| 213 | struct buffer_head *main_bm_bh = NULL; |
| 214 | struct inode *main_bm_inode = NULL; |
| 215 | struct ocfs2_dinode *alloc_copy = NULL; |
| 216 | struct ocfs2_dinode *alloc = NULL; |
| 217 | |
| 218 | mlog_entry_void(); |
| 219 | |
| 220 | if (osb->local_alloc_state == OCFS2_LA_UNUSED) |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 221 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 222 | |
| 223 | local_alloc_inode = |
| 224 | ocfs2_get_system_file_inode(osb, |
| 225 | LOCAL_ALLOC_SYSTEM_INODE, |
| 226 | osb->slot_num); |
| 227 | if (!local_alloc_inode) { |
| 228 | status = -ENOENT; |
| 229 | mlog_errno(status); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 230 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | osb->local_alloc_state = OCFS2_LA_DISABLED; |
| 234 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 235 | main_bm_inode = ocfs2_get_system_file_inode(osb, |
| 236 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 237 | OCFS2_INVALID_SLOT); |
| 238 | if (!main_bm_inode) { |
| 239 | status = -EINVAL; |
| 240 | mlog_errno(status); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 241 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 244 | mutex_lock(&main_bm_inode->i_mutex); |
| 245 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 246 | status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 247 | if (status < 0) { |
| 248 | mlog_errno(status); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 249 | goto out_mutex; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* WINDOW_MOVE_CREDITS is a bit heavy... */ |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 253 | handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 254 | if (IS_ERR(handle)) { |
| 255 | mlog_errno(PTR_ERR(handle)); |
| 256 | handle = NULL; |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 257 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | bh = osb->local_alloc_bh; |
| 261 | alloc = (struct ocfs2_dinode *) bh->b_data; |
| 262 | |
| 263 | alloc_copy = kmalloc(bh->b_size, GFP_KERNEL); |
| 264 | if (!alloc_copy) { |
| 265 | status = -ENOMEM; |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 266 | goto out_commit; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 267 | } |
| 268 | memcpy(alloc_copy, alloc, bh->b_size); |
| 269 | |
| 270 | status = ocfs2_journal_access(handle, local_alloc_inode, bh, |
| 271 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 272 | if (status < 0) { |
| 273 | mlog_errno(status); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 274 | goto out_commit; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | ocfs2_clear_local_alloc(alloc); |
| 278 | |
| 279 | status = ocfs2_journal_dirty(handle, bh); |
| 280 | if (status < 0) { |
| 281 | mlog_errno(status); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 282 | goto out_commit; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | brelse(bh); |
| 286 | osb->local_alloc_bh = NULL; |
| 287 | osb->local_alloc_state = OCFS2_LA_UNUSED; |
| 288 | |
| 289 | status = ocfs2_sync_local_to_main(osb, handle, alloc_copy, |
| 290 | main_bm_inode, main_bm_bh); |
| 291 | if (status < 0) |
| 292 | mlog_errno(status); |
| 293 | |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 294 | out_commit: |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 295 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 296 | |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 297 | out_unlock: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 298 | if (main_bm_bh) |
| 299 | brelse(main_bm_bh); |
| 300 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 301 | ocfs2_inode_unlock(main_bm_inode, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 302 | |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 303 | out_mutex: |
| 304 | mutex_unlock(&main_bm_inode->i_mutex); |
| 305 | iput(main_bm_inode); |
| 306 | |
| 307 | out: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 308 | if (local_alloc_inode) |
| 309 | iput(local_alloc_inode); |
| 310 | |
| 311 | if (alloc_copy) |
| 312 | kfree(alloc_copy); |
| 313 | |
| 314 | mlog_exit_void(); |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * We want to free the bitmap bits outside of any recovery context as |
| 319 | * we'll need a cluster lock to do so, but we must clear the local |
| 320 | * alloc before giving up the recovered nodes journal. To solve this, |
| 321 | * we kmalloc a copy of the local alloc before it's change for the |
| 322 | * caller to process with ocfs2_complete_local_alloc_recovery |
| 323 | */ |
| 324 | int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb, |
| 325 | int slot_num, |
| 326 | struct ocfs2_dinode **alloc_copy) |
| 327 | { |
| 328 | int status = 0; |
| 329 | struct buffer_head *alloc_bh = NULL; |
| 330 | struct inode *inode = NULL; |
| 331 | struct ocfs2_dinode *alloc; |
| 332 | |
| 333 | mlog_entry("(slot_num = %d)\n", slot_num); |
| 334 | |
| 335 | *alloc_copy = NULL; |
| 336 | |
| 337 | inode = ocfs2_get_system_file_inode(osb, |
| 338 | LOCAL_ALLOC_SYSTEM_INODE, |
| 339 | slot_num); |
| 340 | if (!inode) { |
| 341 | status = -EINVAL; |
| 342 | mlog_errno(status); |
| 343 | goto bail; |
| 344 | } |
| 345 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 346 | mutex_lock(&inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 347 | |
| 348 | status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, |
| 349 | &alloc_bh, 0, inode); |
| 350 | if (status < 0) { |
| 351 | mlog_errno(status); |
| 352 | goto bail; |
| 353 | } |
| 354 | |
| 355 | *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL); |
| 356 | if (!(*alloc_copy)) { |
| 357 | status = -ENOMEM; |
| 358 | goto bail; |
| 359 | } |
| 360 | memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size); |
| 361 | |
| 362 | alloc = (struct ocfs2_dinode *) alloc_bh->b_data; |
| 363 | ocfs2_clear_local_alloc(alloc); |
| 364 | |
| 365 | status = ocfs2_write_block(osb, alloc_bh, inode); |
| 366 | if (status < 0) |
| 367 | mlog_errno(status); |
| 368 | |
| 369 | bail: |
| 370 | if ((status < 0) && (*alloc_copy)) { |
| 371 | kfree(*alloc_copy); |
| 372 | *alloc_copy = NULL; |
| 373 | } |
| 374 | |
| 375 | if (alloc_bh) |
| 376 | brelse(alloc_bh); |
| 377 | |
| 378 | if (inode) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 379 | mutex_unlock(&inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 380 | iput(inode); |
| 381 | } |
| 382 | |
| 383 | mlog_exit(status); |
| 384 | return status; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Step 2: By now, we've completed the journal recovery, we've stamped |
| 389 | * a clean local alloc on disk and dropped the node out of the |
| 390 | * recovery map. Dlm locks will no longer stall, so lets clear out the |
| 391 | * main bitmap. |
| 392 | */ |
| 393 | int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb, |
| 394 | struct ocfs2_dinode *alloc) |
| 395 | { |
| 396 | int status; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 397 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 398 | struct buffer_head *main_bm_bh = NULL; |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 399 | struct inode *main_bm_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 400 | |
| 401 | mlog_entry_void(); |
| 402 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 403 | main_bm_inode = ocfs2_get_system_file_inode(osb, |
| 404 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 405 | OCFS2_INVALID_SLOT); |
| 406 | if (!main_bm_inode) { |
| 407 | status = -EINVAL; |
| 408 | mlog_errno(status); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 409 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 412 | mutex_lock(&main_bm_inode->i_mutex); |
| 413 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 414 | status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 415 | if (status < 0) { |
| 416 | mlog_errno(status); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 417 | goto out_mutex; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 420 | handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 421 | if (IS_ERR(handle)) { |
| 422 | status = PTR_ERR(handle); |
| 423 | handle = NULL; |
| 424 | mlog_errno(status); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 425 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /* we want the bitmap change to be recorded on disk asap */ |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 429 | handle->h_sync = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 430 | |
| 431 | status = ocfs2_sync_local_to_main(osb, handle, alloc, |
| 432 | main_bm_inode, main_bm_bh); |
| 433 | if (status < 0) |
| 434 | mlog_errno(status); |
| 435 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 436 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 437 | |
| 438 | out_unlock: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 439 | ocfs2_inode_unlock(main_bm_inode, 1); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 440 | |
| 441 | out_mutex: |
| 442 | mutex_unlock(&main_bm_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 443 | |
| 444 | if (main_bm_bh) |
| 445 | brelse(main_bm_bh); |
| 446 | |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 447 | iput(main_bm_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 448 | |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 449 | out: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 450 | mlog_exit(status); |
| 451 | return status; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * make sure we've got at least bitswanted contiguous bits in the |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 456 | * local alloc. You lose them when you drop i_mutex. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 457 | * |
| 458 | * We will add ourselves to the transaction passed in, but may start |
| 459 | * our own in order to shift windows. |
| 460 | */ |
| 461 | int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 462 | u32 bits_wanted, |
| 463 | struct ocfs2_alloc_context *ac) |
| 464 | { |
| 465 | int status; |
| 466 | struct ocfs2_dinode *alloc; |
| 467 | struct inode *local_alloc_inode; |
| 468 | unsigned int free_bits; |
| 469 | |
| 470 | mlog_entry_void(); |
| 471 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 472 | BUG_ON(!ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 473 | |
| 474 | local_alloc_inode = |
| 475 | ocfs2_get_system_file_inode(osb, |
| 476 | LOCAL_ALLOC_SYSTEM_INODE, |
| 477 | osb->slot_num); |
| 478 | if (!local_alloc_inode) { |
| 479 | status = -ENOENT; |
| 480 | mlog_errno(status); |
| 481 | goto bail; |
| 482 | } |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 483 | |
| 484 | mutex_lock(&local_alloc_inode->i_mutex); |
| 485 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 486 | if (osb->local_alloc_state != OCFS2_LA_ENABLED) { |
| 487 | status = -ENOSPC; |
| 488 | goto bail; |
| 489 | } |
| 490 | |
| 491 | if (bits_wanted > ocfs2_local_alloc_window_bits(osb)) { |
| 492 | mlog(0, "Asking for more than my max window size!\n"); |
| 493 | status = -ENOSPC; |
| 494 | goto bail; |
| 495 | } |
| 496 | |
| 497 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; |
| 498 | |
Jan Kara | 5a58c3e | 2007-11-13 19:59:33 +0100 | [diff] [blame] | 499 | #ifdef OCFS2_DEBUG_FS |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 500 | if (le32_to_cpu(alloc->id1.bitmap1.i_used) != |
| 501 | ocfs2_local_alloc_count_bits(alloc)) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 502 | ocfs2_error(osb->sb, "local alloc inode %llu says it has " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 503 | "%u free bits, but a count shows %u", |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 504 | (unsigned long long)le64_to_cpu(alloc->i_blkno), |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 505 | le32_to_cpu(alloc->id1.bitmap1.i_used), |
| 506 | ocfs2_local_alloc_count_bits(alloc)); |
| 507 | status = -EIO; |
| 508 | goto bail; |
| 509 | } |
Jan Kara | 5a58c3e | 2007-11-13 19:59:33 +0100 | [diff] [blame] | 510 | #endif |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 511 | |
| 512 | free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) - |
| 513 | le32_to_cpu(alloc->id1.bitmap1.i_used); |
| 514 | if (bits_wanted > free_bits) { |
| 515 | /* uhoh, window change time. */ |
| 516 | status = |
| 517 | ocfs2_local_alloc_slide_window(osb, local_alloc_inode); |
| 518 | if (status < 0) { |
| 519 | if (status != -ENOSPC) |
| 520 | mlog_errno(status); |
| 521 | goto bail; |
| 522 | } |
| 523 | } |
| 524 | |
Mark Fasheh | 8fccfc8 | 2007-05-09 17:34:26 -0700 | [diff] [blame] | 525 | ac->ac_inode = local_alloc_inode; |
Tao Ma | a4a4891 | 2008-03-03 17:12:30 +0800 | [diff] [blame^] | 526 | /* We should never use localalloc from another slot */ |
| 527 | ac->ac_alloc_slot = osb->slot_num; |
Mark Fasheh | 8fccfc8 | 2007-05-09 17:34:26 -0700 | [diff] [blame] | 528 | ac->ac_which = OCFS2_AC_USE_LOCAL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 529 | get_bh(osb->local_alloc_bh); |
| 530 | ac->ac_bh = osb->local_alloc_bh; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 531 | status = 0; |
| 532 | bail: |
Sunil Mushran | bda0233 | 2007-09-21 11:41:43 -0700 | [diff] [blame] | 533 | if (status < 0 && local_alloc_inode) { |
| 534 | mutex_unlock(&local_alloc_inode->i_mutex); |
Mark Fasheh | 8fccfc8 | 2007-05-09 17:34:26 -0700 | [diff] [blame] | 535 | iput(local_alloc_inode); |
Sunil Mushran | bda0233 | 2007-09-21 11:41:43 -0700 | [diff] [blame] | 536 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 537 | |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 538 | mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num, |
| 539 | status); |
| 540 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 541 | mlog_exit(status); |
| 542 | return status; |
| 543 | } |
| 544 | |
| 545 | int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 546 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 547 | struct ocfs2_alloc_context *ac, |
Mark Fasheh | 415cb80 | 2007-09-16 20:10:16 -0700 | [diff] [blame] | 548 | u32 bits_wanted, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 549 | u32 *bit_off, |
| 550 | u32 *num_bits) |
| 551 | { |
| 552 | int status, start; |
| 553 | struct inode *local_alloc_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 554 | void *bitmap; |
| 555 | struct ocfs2_dinode *alloc; |
| 556 | struct ocfs2_local_alloc *la; |
| 557 | |
| 558 | mlog_entry_void(); |
| 559 | BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL); |
| 560 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 561 | local_alloc_inode = ac->ac_inode; |
| 562 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; |
| 563 | la = OCFS2_LOCAL_ALLOC(alloc); |
| 564 | |
| 565 | start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted); |
| 566 | if (start == -1) { |
| 567 | /* TODO: Shouldn't we just BUG here? */ |
| 568 | status = -ENOSPC; |
| 569 | mlog_errno(status); |
| 570 | goto bail; |
| 571 | } |
| 572 | |
| 573 | bitmap = la->la_bitmap; |
| 574 | *bit_off = le32_to_cpu(la->la_bm_off) + start; |
| 575 | /* local alloc is always contiguous by nature -- we never |
| 576 | * delete bits from it! */ |
| 577 | *num_bits = bits_wanted; |
| 578 | |
| 579 | status = ocfs2_journal_access(handle, local_alloc_inode, |
| 580 | osb->local_alloc_bh, |
| 581 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 582 | if (status < 0) { |
| 583 | mlog_errno(status); |
| 584 | goto bail; |
| 585 | } |
| 586 | |
| 587 | while(bits_wanted--) |
| 588 | ocfs2_set_bit(start++, bitmap); |
| 589 | |
Marcin Slusarz | 0dd3256 | 2008-02-13 00:06:18 +0100 | [diff] [blame] | 590 | le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 591 | |
| 592 | status = ocfs2_journal_dirty(handle, osb->local_alloc_bh); |
| 593 | if (status < 0) { |
| 594 | mlog_errno(status); |
| 595 | goto bail; |
| 596 | } |
| 597 | |
| 598 | status = 0; |
| 599 | bail: |
| 600 | mlog_exit(status); |
| 601 | return status; |
| 602 | } |
| 603 | |
| 604 | static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc) |
| 605 | { |
| 606 | int i; |
| 607 | u8 *buffer; |
| 608 | u32 count = 0; |
| 609 | struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc); |
| 610 | |
| 611 | mlog_entry_void(); |
| 612 | |
| 613 | buffer = la->la_bitmap; |
| 614 | for (i = 0; i < le16_to_cpu(la->la_size); i++) |
| 615 | count += hweight8(buffer[i]); |
| 616 | |
| 617 | mlog_exit(count); |
| 618 | return count; |
| 619 | } |
| 620 | |
| 621 | static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb, |
| 622 | struct ocfs2_dinode *alloc, |
| 623 | u32 numbits) |
| 624 | { |
| 625 | int numfound, bitoff, left, startoff, lastzero; |
| 626 | void *bitmap = NULL; |
| 627 | |
| 628 | mlog_entry("(numbits wanted = %u)\n", numbits); |
| 629 | |
| 630 | if (!alloc->id1.bitmap1.i_total) { |
| 631 | mlog(0, "No bits in my window!\n"); |
| 632 | bitoff = -1; |
| 633 | goto bail; |
| 634 | } |
| 635 | |
| 636 | bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap; |
| 637 | |
| 638 | numfound = bitoff = startoff = 0; |
| 639 | lastzero = -1; |
| 640 | left = le32_to_cpu(alloc->id1.bitmap1.i_total); |
| 641 | while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) { |
| 642 | if (bitoff == left) { |
| 643 | /* mlog(0, "bitoff (%d) == left", bitoff); */ |
| 644 | break; |
| 645 | } |
| 646 | /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, " |
| 647 | "numfound = %d\n", bitoff, startoff, numfound);*/ |
| 648 | |
| 649 | /* Ok, we found a zero bit... is it contig. or do we |
| 650 | * start over?*/ |
| 651 | if (bitoff == startoff) { |
| 652 | /* we found a zero */ |
| 653 | numfound++; |
| 654 | startoff++; |
| 655 | } else { |
| 656 | /* got a zero after some ones */ |
| 657 | numfound = 1; |
| 658 | startoff = bitoff+1; |
| 659 | } |
| 660 | /* we got everything we needed */ |
| 661 | if (numfound == numbits) { |
| 662 | /* mlog(0, "Found it all!\n"); */ |
| 663 | break; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff, |
| 668 | numfound); |
| 669 | |
| 670 | if (numfound == numbits) |
| 671 | bitoff = startoff - numfound; |
| 672 | else |
| 673 | bitoff = -1; |
| 674 | |
| 675 | bail: |
| 676 | mlog_exit(bitoff); |
| 677 | return bitoff; |
| 678 | } |
| 679 | |
| 680 | static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc) |
| 681 | { |
| 682 | struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc); |
| 683 | int i; |
| 684 | mlog_entry_void(); |
| 685 | |
| 686 | alloc->id1.bitmap1.i_total = 0; |
| 687 | alloc->id1.bitmap1.i_used = 0; |
| 688 | la->la_bm_off = 0; |
| 689 | for(i = 0; i < le16_to_cpu(la->la_size); i++) |
| 690 | la->la_bitmap[i] = 0; |
| 691 | |
| 692 | mlog_exit_void(); |
| 693 | } |
| 694 | |
| 695 | #if 0 |
| 696 | /* turn this on and uncomment below to aid debugging window shifts. */ |
| 697 | static void ocfs2_verify_zero_bits(unsigned long *bitmap, |
| 698 | unsigned int start, |
| 699 | unsigned int count) |
| 700 | { |
| 701 | unsigned int tmp = count; |
| 702 | while(tmp--) { |
| 703 | if (ocfs2_test_bit(start + tmp, bitmap)) { |
| 704 | printk("ocfs2_verify_zero_bits: start = %u, count = " |
| 705 | "%u\n", start, count); |
| 706 | printk("ocfs2_verify_zero_bits: bit %u is set!", |
| 707 | start + tmp); |
| 708 | BUG(); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | #endif |
| 713 | |
| 714 | /* |
| 715 | * sync the local alloc to main bitmap. |
| 716 | * |
| 717 | * assumes you've already locked the main bitmap -- the bitmap inode |
| 718 | * passed is used for caching. |
| 719 | */ |
| 720 | static int ocfs2_sync_local_to_main(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 721 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 722 | struct ocfs2_dinode *alloc, |
| 723 | struct inode *main_bm_inode, |
| 724 | struct buffer_head *main_bm_bh) |
| 725 | { |
| 726 | int status = 0; |
| 727 | int bit_off, left, count, start; |
| 728 | u64 la_start_blk; |
| 729 | u64 blkno; |
| 730 | void *bitmap; |
| 731 | struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc); |
| 732 | |
Jan Kara | 5a58c3e | 2007-11-13 19:59:33 +0100 | [diff] [blame] | 733 | mlog_entry("total = %u, used = %u\n", |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 734 | le32_to_cpu(alloc->id1.bitmap1.i_total), |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 735 | le32_to_cpu(alloc->id1.bitmap1.i_used)); |
| 736 | |
| 737 | if (!alloc->id1.bitmap1.i_total) { |
| 738 | mlog(0, "nothing to sync!\n"); |
| 739 | goto bail; |
| 740 | } |
| 741 | |
| 742 | if (le32_to_cpu(alloc->id1.bitmap1.i_used) == |
| 743 | le32_to_cpu(alloc->id1.bitmap1.i_total)) { |
| 744 | mlog(0, "all bits were taken!\n"); |
| 745 | goto bail; |
| 746 | } |
| 747 | |
| 748 | la_start_blk = ocfs2_clusters_to_blocks(osb->sb, |
| 749 | le32_to_cpu(la->la_bm_off)); |
| 750 | bitmap = la->la_bitmap; |
| 751 | start = count = bit_off = 0; |
| 752 | left = le32_to_cpu(alloc->id1.bitmap1.i_total); |
| 753 | |
| 754 | while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start)) |
| 755 | != -1) { |
| 756 | if ((bit_off < left) && (bit_off == start)) { |
| 757 | count++; |
| 758 | start++; |
| 759 | continue; |
| 760 | } |
| 761 | if (count) { |
| 762 | blkno = la_start_blk + |
| 763 | ocfs2_clusters_to_blocks(osb->sb, |
| 764 | start - count); |
| 765 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 766 | mlog(0, "freeing %u bits starting at local alloc bit " |
| 767 | "%u (la_start_blk = %llu, blkno = %llu)\n", |
| 768 | count, start - count, |
| 769 | (unsigned long long)la_start_blk, |
| 770 | (unsigned long long)blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 771 | |
| 772 | status = ocfs2_free_clusters(handle, main_bm_inode, |
| 773 | main_bm_bh, blkno, count); |
| 774 | if (status < 0) { |
| 775 | mlog_errno(status); |
| 776 | goto bail; |
| 777 | } |
| 778 | } |
| 779 | if (bit_off >= left) |
| 780 | break; |
| 781 | count = 1; |
| 782 | start = bit_off + 1; |
| 783 | } |
| 784 | |
| 785 | bail: |
| 786 | mlog_exit(status); |
| 787 | return status; |
| 788 | } |
| 789 | |
| 790 | static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 791 | struct ocfs2_alloc_context **ac, |
| 792 | struct inode **bitmap_inode, |
| 793 | struct buffer_head **bitmap_bh) |
| 794 | { |
| 795 | int status; |
| 796 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 797 | *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 798 | if (!(*ac)) { |
| 799 | status = -ENOMEM; |
| 800 | mlog_errno(status); |
| 801 | goto bail; |
| 802 | } |
| 803 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 804 | (*ac)->ac_bits_wanted = ocfs2_local_alloc_window_bits(osb); |
| 805 | |
| 806 | status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac); |
| 807 | if (status < 0) { |
| 808 | if (status != -ENOSPC) |
| 809 | mlog_errno(status); |
| 810 | goto bail; |
| 811 | } |
| 812 | |
| 813 | *bitmap_inode = (*ac)->ac_inode; |
| 814 | igrab(*bitmap_inode); |
| 815 | *bitmap_bh = (*ac)->ac_bh; |
| 816 | get_bh(*bitmap_bh); |
| 817 | status = 0; |
| 818 | bail: |
| 819 | if ((status < 0) && *ac) { |
| 820 | ocfs2_free_alloc_context(*ac); |
| 821 | *ac = NULL; |
| 822 | } |
| 823 | |
| 824 | mlog_exit(status); |
| 825 | return status; |
| 826 | } |
| 827 | |
| 828 | /* |
| 829 | * pass it the bitmap lock in lock_bh if you have it. |
| 830 | */ |
| 831 | static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 832 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 833 | struct ocfs2_alloc_context *ac) |
| 834 | { |
| 835 | int status = 0; |
| 836 | u32 cluster_off, cluster_count; |
| 837 | struct ocfs2_dinode *alloc = NULL; |
| 838 | struct ocfs2_local_alloc *la; |
| 839 | |
| 840 | mlog_entry_void(); |
| 841 | |
| 842 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; |
| 843 | la = OCFS2_LOCAL_ALLOC(alloc); |
| 844 | |
| 845 | if (alloc->id1.bitmap1.i_total) |
| 846 | mlog(0, "asking me to alloc a new window over a non-empty " |
| 847 | "one\n"); |
| 848 | |
| 849 | mlog(0, "Allocating %u clusters for a new window.\n", |
| 850 | ocfs2_local_alloc_window_bits(osb)); |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 851 | |
| 852 | /* Instruct the allocation code to try the most recently used |
| 853 | * cluster group. We'll re-record the group used this pass |
| 854 | * below. */ |
| 855 | ac->ac_last_group = osb->la_last_gd; |
| 856 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 857 | /* we used the generic suballoc reserve function, but we set |
| 858 | * everything up nicely, so there's no reason why we can't use |
| 859 | * the more specific cluster api to claim bits. */ |
| 860 | status = ocfs2_claim_clusters(osb, handle, ac, |
| 861 | ocfs2_local_alloc_window_bits(osb), |
| 862 | &cluster_off, &cluster_count); |
| 863 | if (status < 0) { |
| 864 | if (status != -ENOSPC) |
| 865 | mlog_errno(status); |
| 866 | goto bail; |
| 867 | } |
| 868 | |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 869 | osb->la_last_gd = ac->ac_last_group; |
| 870 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 871 | la->la_bm_off = cpu_to_le32(cluster_off); |
| 872 | alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count); |
| 873 | /* just in case... In the future when we find space ourselves, |
| 874 | * we don't have to get all contiguous -- but we'll have to |
| 875 | * set all previously used bits in bitmap and update |
| 876 | * la_bits_set before setting the bits in the main bitmap. */ |
| 877 | alloc->id1.bitmap1.i_used = 0; |
| 878 | memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0, |
| 879 | le16_to_cpu(la->la_size)); |
| 880 | |
| 881 | mlog(0, "New window allocated:\n"); |
| 882 | mlog(0, "window la_bm_off = %u\n", |
| 883 | OCFS2_LOCAL_ALLOC(alloc)->la_bm_off); |
| 884 | mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total)); |
| 885 | |
| 886 | bail: |
| 887 | mlog_exit(status); |
| 888 | return status; |
| 889 | } |
| 890 | |
| 891 | /* Note that we do *NOT* lock the local alloc inode here as |
| 892 | * it's been locked already for us. */ |
| 893 | static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb, |
| 894 | struct inode *local_alloc_inode) |
| 895 | { |
| 896 | int status = 0; |
| 897 | struct buffer_head *main_bm_bh = NULL; |
| 898 | struct inode *main_bm_inode = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 899 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 900 | struct ocfs2_dinode *alloc; |
| 901 | struct ocfs2_dinode *alloc_copy = NULL; |
| 902 | struct ocfs2_alloc_context *ac = NULL; |
| 903 | |
| 904 | mlog_entry_void(); |
| 905 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 906 | /* This will lock the main bitmap for us. */ |
| 907 | status = ocfs2_local_alloc_reserve_for_window(osb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 908 | &ac, |
| 909 | &main_bm_inode, |
| 910 | &main_bm_bh); |
| 911 | if (status < 0) { |
| 912 | if (status != -ENOSPC) |
| 913 | mlog_errno(status); |
| 914 | goto bail; |
| 915 | } |
| 916 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 917 | handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 918 | if (IS_ERR(handle)) { |
| 919 | status = PTR_ERR(handle); |
| 920 | handle = NULL; |
| 921 | mlog_errno(status); |
| 922 | goto bail; |
| 923 | } |
| 924 | |
| 925 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; |
| 926 | |
| 927 | /* We want to clear the local alloc before doing anything |
| 928 | * else, so that if we error later during this operation, |
| 929 | * local alloc shutdown won't try to double free main bitmap |
| 930 | * bits. Make a copy so the sync function knows which bits to |
| 931 | * free. */ |
| 932 | alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_KERNEL); |
| 933 | if (!alloc_copy) { |
| 934 | status = -ENOMEM; |
| 935 | mlog_errno(status); |
| 936 | goto bail; |
| 937 | } |
| 938 | memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size); |
| 939 | |
| 940 | status = ocfs2_journal_access(handle, local_alloc_inode, |
| 941 | osb->local_alloc_bh, |
| 942 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 943 | if (status < 0) { |
| 944 | mlog_errno(status); |
| 945 | goto bail; |
| 946 | } |
| 947 | |
| 948 | ocfs2_clear_local_alloc(alloc); |
| 949 | |
| 950 | status = ocfs2_journal_dirty(handle, osb->local_alloc_bh); |
| 951 | if (status < 0) { |
| 952 | mlog_errno(status); |
| 953 | goto bail; |
| 954 | } |
| 955 | |
| 956 | status = ocfs2_sync_local_to_main(osb, handle, alloc_copy, |
| 957 | main_bm_inode, main_bm_bh); |
| 958 | if (status < 0) { |
| 959 | mlog_errno(status); |
| 960 | goto bail; |
| 961 | } |
| 962 | |
| 963 | status = ocfs2_local_alloc_new_window(osb, handle, ac); |
| 964 | if (status < 0) { |
| 965 | if (status != -ENOSPC) |
| 966 | mlog_errno(status); |
| 967 | goto bail; |
| 968 | } |
| 969 | |
| 970 | atomic_inc(&osb->alloc_stats.moves); |
| 971 | |
| 972 | status = 0; |
| 973 | bail: |
| 974 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 975 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 976 | |
| 977 | if (main_bm_bh) |
| 978 | brelse(main_bm_bh); |
| 979 | |
| 980 | if (main_bm_inode) |
| 981 | iput(main_bm_inode); |
| 982 | |
| 983 | if (alloc_copy) |
| 984 | kfree(alloc_copy); |
| 985 | |
| 986 | if (ac) |
| 987 | ocfs2_free_alloc_context(ac); |
| 988 | |
| 989 | mlog_exit(status); |
| 990 | return status; |
| 991 | } |
| 992 | |