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