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" |
Joel Becker | 13723d0 | 2008-10-17 19:25: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 | |
| 47 | #include "buffer_head_io.h" |
| 48 | |
| 49 | #define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab)) |
| 50 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 51 | static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc); |
| 52 | |
| 53 | static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb, |
| 54 | struct ocfs2_dinode *alloc, |
| 55 | u32 numbits); |
| 56 | |
| 57 | static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc); |
| 58 | |
| 59 | static int ocfs2_sync_local_to_main(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 60 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 61 | struct ocfs2_dinode *alloc, |
| 62 | struct inode *main_bm_inode, |
| 63 | struct buffer_head *main_bm_bh); |
| 64 | |
| 65 | static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 66 | struct ocfs2_alloc_context **ac, |
| 67 | struct inode **bitmap_inode, |
| 68 | struct buffer_head **bitmap_bh); |
| 69 | |
| 70 | static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 71 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 72 | struct ocfs2_alloc_context *ac); |
| 73 | |
| 74 | static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb, |
| 75 | struct inode *local_alloc_inode); |
| 76 | |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 77 | static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb) |
| 78 | { |
| 79 | return (osb->local_alloc_state == OCFS2_LA_THROTTLED || |
| 80 | osb->local_alloc_state == OCFS2_LA_ENABLED); |
| 81 | } |
| 82 | |
| 83 | void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb, |
| 84 | unsigned int num_clusters) |
| 85 | { |
| 86 | spin_lock(&osb->osb_lock); |
| 87 | if (osb->local_alloc_state == OCFS2_LA_DISABLED || |
| 88 | osb->local_alloc_state == OCFS2_LA_THROTTLED) |
| 89 | if (num_clusters >= osb->local_alloc_default_bits) { |
| 90 | cancel_delayed_work(&osb->la_enable_wq); |
| 91 | osb->local_alloc_state = OCFS2_LA_ENABLED; |
| 92 | } |
| 93 | spin_unlock(&osb->osb_lock); |
| 94 | } |
| 95 | |
| 96 | void ocfs2_la_enable_worker(struct work_struct *work) |
| 97 | { |
| 98 | struct ocfs2_super *osb = |
| 99 | container_of(work, struct ocfs2_super, |
| 100 | la_enable_wq.work); |
| 101 | spin_lock(&osb->osb_lock); |
| 102 | osb->local_alloc_state = OCFS2_LA_ENABLED; |
| 103 | spin_unlock(&osb->osb_lock); |
| 104 | } |
| 105 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 106 | /* |
| 107 | * Tell us whether a given allocation should use the local alloc |
| 108 | * file. Otherwise, it has to go to the main bitmap. |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 109 | * |
| 110 | * This function does semi-dirty reads of local alloc size and state! |
| 111 | * This is ok however, as the values are re-checked once under mutex. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 112 | */ |
| 113 | int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits) |
| 114 | { |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 115 | int ret = 0; |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 116 | int la_bits; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 117 | |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 118 | spin_lock(&osb->osb_lock); |
| 119 | la_bits = osb->local_alloc_bits; |
| 120 | |
| 121 | if (!ocfs2_la_state_enabled(osb)) |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 122 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 123 | |
| 124 | /* la_bits should be at least twice the size (in clusters) of |
| 125 | * a new block group. We want to be sure block group |
| 126 | * allocations go through the local alloc, so allow an |
| 127 | * allocation to take up to half the bitmap. */ |
| 128 | if (bits > (la_bits / 2)) |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 129 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 130 | |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 131 | ret = 1; |
| 132 | bail: |
| 133 | mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n", |
| 134 | osb->local_alloc_state, (unsigned long long)bits, la_bits, ret); |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 135 | spin_unlock(&osb->osb_lock); |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 136 | return ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | int ocfs2_load_local_alloc(struct ocfs2_super *osb) |
| 140 | { |
| 141 | int status = 0; |
| 142 | struct ocfs2_dinode *alloc = NULL; |
| 143 | struct buffer_head *alloc_bh = NULL; |
| 144 | u32 num_used; |
| 145 | struct inode *inode = NULL; |
| 146 | struct ocfs2_local_alloc *la; |
| 147 | |
| 148 | mlog_entry_void(); |
| 149 | |
Mark Fasheh | ebcee4b | 2008-07-28 14:55:20 -0700 | [diff] [blame] | 150 | if (osb->local_alloc_bits == 0) |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 151 | goto bail; |
| 152 | |
Mark Fasheh | ebcee4b | 2008-07-28 14:55:20 -0700 | [diff] [blame] | 153 | if (osb->local_alloc_bits >= osb->bitmap_cpg) { |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 154 | mlog(ML_NOTICE, "Requested local alloc window %d is larger " |
| 155 | "than max possible %u. Using defaults.\n", |
Mark Fasheh | ebcee4b | 2008-07-28 14:55:20 -0700 | [diff] [blame] | 156 | osb->local_alloc_bits, (osb->bitmap_cpg - 1)); |
| 157 | osb->local_alloc_bits = |
| 158 | ocfs2_megabytes_to_clusters(osb->sb, |
| 159 | OCFS2_DEFAULT_LOCAL_ALLOC_SIZE); |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 162 | /* read the alloc off disk */ |
| 163 | inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE, |
| 164 | osb->slot_num); |
| 165 | if (!inode) { |
| 166 | status = -EINVAL; |
| 167 | mlog_errno(status); |
| 168 | goto bail; |
| 169 | } |
| 170 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 171 | status = ocfs2_read_inode_block_full(inode, &alloc_bh, |
| 172 | OCFS2_BH_IGNORE_CACHE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 173 | if (status < 0) { |
| 174 | mlog_errno(status); |
| 175 | goto bail; |
| 176 | } |
| 177 | |
| 178 | alloc = (struct ocfs2_dinode *) alloc_bh->b_data; |
| 179 | la = OCFS2_LOCAL_ALLOC(alloc); |
| 180 | |
| 181 | if (!(le32_to_cpu(alloc->i_flags) & |
| 182 | (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 183 | mlog(ML_ERROR, "Invalid local alloc inode, %llu\n", |
| 184 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 185 | status = -EINVAL; |
| 186 | goto bail; |
| 187 | } |
| 188 | |
| 189 | if ((la->la_size == 0) || |
| 190 | (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) { |
| 191 | mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n", |
| 192 | le16_to_cpu(la->la_size)); |
| 193 | status = -EINVAL; |
| 194 | goto bail; |
| 195 | } |
| 196 | |
| 197 | /* do a little verification. */ |
| 198 | num_used = ocfs2_local_alloc_count_bits(alloc); |
| 199 | |
| 200 | /* hopefully the local alloc has always been recovered before |
| 201 | * we load it. */ |
| 202 | if (num_used |
| 203 | || alloc->id1.bitmap1.i_used |
| 204 | || alloc->id1.bitmap1.i_total |
| 205 | || la->la_bm_off) |
| 206 | mlog(ML_ERROR, "Local alloc hasn't been recovered!\n" |
| 207 | "found = %u, set = %u, taken = %u, off = %u\n", |
| 208 | num_used, le32_to_cpu(alloc->id1.bitmap1.i_used), |
| 209 | le32_to_cpu(alloc->id1.bitmap1.i_total), |
| 210 | OCFS2_LOCAL_ALLOC(alloc)->la_bm_off); |
| 211 | |
| 212 | osb->local_alloc_bh = alloc_bh; |
| 213 | osb->local_alloc_state = OCFS2_LA_ENABLED; |
| 214 | |
| 215 | bail: |
| 216 | if (status < 0) |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 217 | brelse(alloc_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 218 | if (inode) |
| 219 | iput(inode); |
| 220 | |
Mark Fasheh | ebcee4b | 2008-07-28 14:55:20 -0700 | [diff] [blame] | 221 | mlog(0, "Local alloc window bits = %d\n", osb->local_alloc_bits); |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 222 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 223 | mlog_exit(status); |
| 224 | return status; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * return any unused bits to the bitmap and write out a clean |
| 229 | * local_alloc. |
| 230 | * |
| 231 | * local_alloc_bh is optional. If not passed, we will simply use the |
| 232 | * one off osb. If you do pass it however, be warned that it *will* be |
| 233 | * returned brelse'd and NULL'd out.*/ |
| 234 | void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb) |
| 235 | { |
| 236 | int status; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 237 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 238 | struct inode *local_alloc_inode = NULL; |
| 239 | struct buffer_head *bh = NULL; |
| 240 | struct buffer_head *main_bm_bh = NULL; |
| 241 | struct inode *main_bm_inode = NULL; |
| 242 | struct ocfs2_dinode *alloc_copy = NULL; |
| 243 | struct ocfs2_dinode *alloc = NULL; |
| 244 | |
| 245 | mlog_entry_void(); |
| 246 | |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 247 | cancel_delayed_work(&osb->la_enable_wq); |
| 248 | flush_workqueue(ocfs2_wq); |
| 249 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 250 | if (osb->local_alloc_state == OCFS2_LA_UNUSED) |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 251 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 252 | |
| 253 | local_alloc_inode = |
| 254 | ocfs2_get_system_file_inode(osb, |
| 255 | LOCAL_ALLOC_SYSTEM_INODE, |
| 256 | osb->slot_num); |
| 257 | if (!local_alloc_inode) { |
| 258 | status = -ENOENT; |
| 259 | mlog_errno(status); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 260 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | osb->local_alloc_state = OCFS2_LA_DISABLED; |
| 264 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 265 | main_bm_inode = ocfs2_get_system_file_inode(osb, |
| 266 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 267 | OCFS2_INVALID_SLOT); |
| 268 | if (!main_bm_inode) { |
| 269 | status = -EINVAL; |
| 270 | mlog_errno(status); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 271 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 274 | mutex_lock(&main_bm_inode->i_mutex); |
| 275 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 276 | status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 277 | if (status < 0) { |
| 278 | mlog_errno(status); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 279 | goto out_mutex; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /* WINDOW_MOVE_CREDITS is a bit heavy... */ |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 283 | handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 284 | if (IS_ERR(handle)) { |
| 285 | mlog_errno(PTR_ERR(handle)); |
| 286 | handle = NULL; |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 287 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | bh = osb->local_alloc_bh; |
| 291 | alloc = (struct ocfs2_dinode *) bh->b_data; |
| 292 | |
Sunil Mushran | 4ba1c5b | 2008-04-18 15:03:59 -0700 | [diff] [blame] | 293 | alloc_copy = kmalloc(bh->b_size, GFP_NOFS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 294 | if (!alloc_copy) { |
| 295 | status = -ENOMEM; |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 296 | goto out_commit; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 297 | } |
| 298 | memcpy(alloc_copy, alloc, bh->b_size); |
| 299 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 300 | status = ocfs2_journal_access_di(handle, INODE_CACHE(local_alloc_inode), |
| 301 | bh, OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 302 | if (status < 0) { |
| 303 | mlog_errno(status); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 304 | goto out_commit; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | ocfs2_clear_local_alloc(alloc); |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame^] | 308 | ocfs2_journal_dirty(handle, bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 309 | |
| 310 | brelse(bh); |
| 311 | osb->local_alloc_bh = NULL; |
| 312 | osb->local_alloc_state = OCFS2_LA_UNUSED; |
| 313 | |
| 314 | status = ocfs2_sync_local_to_main(osb, handle, alloc_copy, |
| 315 | main_bm_inode, main_bm_bh); |
| 316 | if (status < 0) |
| 317 | mlog_errno(status); |
| 318 | |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 319 | out_commit: |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 320 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 321 | |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 322 | out_unlock: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 323 | brelse(main_bm_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 324 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 325 | ocfs2_inode_unlock(main_bm_inode, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 326 | |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 327 | out_mutex: |
| 328 | mutex_unlock(&main_bm_inode->i_mutex); |
| 329 | iput(main_bm_inode); |
| 330 | |
| 331 | out: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 332 | if (local_alloc_inode) |
| 333 | iput(local_alloc_inode); |
| 334 | |
| 335 | if (alloc_copy) |
| 336 | kfree(alloc_copy); |
| 337 | |
| 338 | mlog_exit_void(); |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * We want to free the bitmap bits outside of any recovery context as |
| 343 | * we'll need a cluster lock to do so, but we must clear the local |
| 344 | * alloc before giving up the recovered nodes journal. To solve this, |
| 345 | * we kmalloc a copy of the local alloc before it's change for the |
| 346 | * caller to process with ocfs2_complete_local_alloc_recovery |
| 347 | */ |
| 348 | int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb, |
| 349 | int slot_num, |
| 350 | struct ocfs2_dinode **alloc_copy) |
| 351 | { |
| 352 | int status = 0; |
| 353 | struct buffer_head *alloc_bh = NULL; |
| 354 | struct inode *inode = NULL; |
| 355 | struct ocfs2_dinode *alloc; |
| 356 | |
| 357 | mlog_entry("(slot_num = %d)\n", slot_num); |
| 358 | |
| 359 | *alloc_copy = NULL; |
| 360 | |
| 361 | inode = ocfs2_get_system_file_inode(osb, |
| 362 | LOCAL_ALLOC_SYSTEM_INODE, |
| 363 | slot_num); |
| 364 | if (!inode) { |
| 365 | status = -EINVAL; |
| 366 | mlog_errno(status); |
| 367 | goto bail; |
| 368 | } |
| 369 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 370 | mutex_lock(&inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 371 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 372 | status = ocfs2_read_inode_block_full(inode, &alloc_bh, |
| 373 | OCFS2_BH_IGNORE_CACHE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 374 | if (status < 0) { |
| 375 | mlog_errno(status); |
| 376 | goto bail; |
| 377 | } |
| 378 | |
| 379 | *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL); |
| 380 | if (!(*alloc_copy)) { |
| 381 | status = -ENOMEM; |
| 382 | goto bail; |
| 383 | } |
| 384 | memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size); |
| 385 | |
| 386 | alloc = (struct ocfs2_dinode *) alloc_bh->b_data; |
| 387 | ocfs2_clear_local_alloc(alloc); |
| 388 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 389 | ocfs2_compute_meta_ecc(osb->sb, alloc_bh->b_data, &alloc->i_check); |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 390 | status = ocfs2_write_block(osb, alloc_bh, INODE_CACHE(inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 391 | if (status < 0) |
| 392 | mlog_errno(status); |
| 393 | |
| 394 | bail: |
| 395 | if ((status < 0) && (*alloc_copy)) { |
| 396 | kfree(*alloc_copy); |
| 397 | *alloc_copy = NULL; |
| 398 | } |
| 399 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 400 | brelse(alloc_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 401 | |
| 402 | if (inode) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 403 | mutex_unlock(&inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 404 | iput(inode); |
| 405 | } |
| 406 | |
| 407 | mlog_exit(status); |
| 408 | return status; |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Step 2: By now, we've completed the journal recovery, we've stamped |
| 413 | * a clean local alloc on disk and dropped the node out of the |
| 414 | * recovery map. Dlm locks will no longer stall, so lets clear out the |
| 415 | * main bitmap. |
| 416 | */ |
| 417 | int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb, |
| 418 | struct ocfs2_dinode *alloc) |
| 419 | { |
| 420 | int status; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 421 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 422 | struct buffer_head *main_bm_bh = NULL; |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 423 | struct inode *main_bm_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 424 | |
| 425 | mlog_entry_void(); |
| 426 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 427 | main_bm_inode = ocfs2_get_system_file_inode(osb, |
| 428 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 429 | OCFS2_INVALID_SLOT); |
| 430 | if (!main_bm_inode) { |
| 431 | status = -EINVAL; |
| 432 | mlog_errno(status); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 433 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 436 | mutex_lock(&main_bm_inode->i_mutex); |
| 437 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 438 | status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 439 | if (status < 0) { |
| 440 | mlog_errno(status); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 441 | goto out_mutex; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 444 | handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 445 | if (IS_ERR(handle)) { |
| 446 | status = PTR_ERR(handle); |
| 447 | handle = NULL; |
| 448 | mlog_errno(status); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 449 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* we want the bitmap change to be recorded on disk asap */ |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 453 | handle->h_sync = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 454 | |
| 455 | status = ocfs2_sync_local_to_main(osb, handle, alloc, |
| 456 | main_bm_inode, main_bm_bh); |
| 457 | if (status < 0) |
| 458 | mlog_errno(status); |
| 459 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 460 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 461 | |
| 462 | out_unlock: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 463 | ocfs2_inode_unlock(main_bm_inode, 1); |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 464 | |
| 465 | out_mutex: |
| 466 | mutex_unlock(&main_bm_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 467 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 468 | brelse(main_bm_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 469 | |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 470 | iput(main_bm_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 471 | |
Mark Fasheh | 8898a5a | 2006-10-05 15:42:08 -0700 | [diff] [blame] | 472 | out: |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 473 | if (!status) |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 474 | ocfs2_init_steal_slots(osb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 475 | mlog_exit(status); |
| 476 | return status; |
| 477 | } |
| 478 | |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 479 | /* Check to see if the local alloc window is within ac->ac_max_block */ |
| 480 | static int ocfs2_local_alloc_in_range(struct inode *inode, |
| 481 | struct ocfs2_alloc_context *ac, |
| 482 | u32 bits_wanted) |
| 483 | { |
| 484 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 485 | struct ocfs2_dinode *alloc; |
| 486 | struct ocfs2_local_alloc *la; |
| 487 | int start; |
| 488 | u64 block_off; |
| 489 | |
| 490 | if (!ac->ac_max_block) |
| 491 | return 1; |
| 492 | |
| 493 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; |
| 494 | la = OCFS2_LOCAL_ALLOC(alloc); |
| 495 | |
| 496 | start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted); |
| 497 | if (start == -1) { |
| 498 | mlog_errno(-ENOSPC); |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * Converting (bm_off + start + bits_wanted) to blocks gives us |
| 504 | * the blkno just past our actual allocation. This is perfect |
| 505 | * to compare with ac_max_block. |
| 506 | */ |
| 507 | block_off = ocfs2_clusters_to_blocks(inode->i_sb, |
| 508 | le32_to_cpu(la->la_bm_off) + |
| 509 | start + bits_wanted); |
| 510 | mlog(0, "Checking %llu against %llu\n", |
| 511 | (unsigned long long)block_off, |
| 512 | (unsigned long long)ac->ac_max_block); |
| 513 | if (block_off > ac->ac_max_block) |
| 514 | return 0; |
| 515 | |
| 516 | return 1; |
| 517 | } |
| 518 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 519 | /* |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 520 | * make sure we've got at least bits_wanted contiguous bits in the |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 521 | * local alloc. You lose them when you drop i_mutex. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 522 | * |
| 523 | * We will add ourselves to the transaction passed in, but may start |
| 524 | * our own in order to shift windows. |
| 525 | */ |
| 526 | int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 527 | u32 bits_wanted, |
| 528 | struct ocfs2_alloc_context *ac) |
| 529 | { |
| 530 | int status; |
| 531 | struct ocfs2_dinode *alloc; |
| 532 | struct inode *local_alloc_inode; |
| 533 | unsigned int free_bits; |
| 534 | |
| 535 | mlog_entry_void(); |
| 536 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 537 | BUG_ON(!ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 538 | |
| 539 | local_alloc_inode = |
| 540 | ocfs2_get_system_file_inode(osb, |
| 541 | LOCAL_ALLOC_SYSTEM_INODE, |
| 542 | osb->slot_num); |
| 543 | if (!local_alloc_inode) { |
| 544 | status = -ENOENT; |
| 545 | mlog_errno(status); |
| 546 | goto bail; |
| 547 | } |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 548 | |
| 549 | mutex_lock(&local_alloc_inode->i_mutex); |
| 550 | |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 551 | /* |
| 552 | * We must double check state and allocator bits because |
| 553 | * another process may have changed them while holding i_mutex. |
| 554 | */ |
| 555 | spin_lock(&osb->osb_lock); |
| 556 | if (!ocfs2_la_state_enabled(osb) || |
| 557 | (bits_wanted > osb->local_alloc_bits)) { |
| 558 | spin_unlock(&osb->osb_lock); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 559 | status = -ENOSPC; |
| 560 | goto bail; |
| 561 | } |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 562 | spin_unlock(&osb->osb_lock); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 563 | |
| 564 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; |
| 565 | |
Joel Becker | e407e39 | 2008-06-12 22:35:39 -0700 | [diff] [blame] | 566 | #ifdef CONFIG_OCFS2_DEBUG_FS |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 567 | if (le32_to_cpu(alloc->id1.bitmap1.i_used) != |
| 568 | ocfs2_local_alloc_count_bits(alloc)) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 569 | ocfs2_error(osb->sb, "local alloc inode %llu says it has " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 570 | "%u free bits, but a count shows %u", |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 571 | (unsigned long long)le64_to_cpu(alloc->i_blkno), |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 572 | le32_to_cpu(alloc->id1.bitmap1.i_used), |
| 573 | ocfs2_local_alloc_count_bits(alloc)); |
| 574 | status = -EIO; |
| 575 | goto bail; |
| 576 | } |
Jan Kara | 5a58c3e | 2007-11-13 19:59:33 +0100 | [diff] [blame] | 577 | #endif |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 578 | |
| 579 | free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) - |
| 580 | le32_to_cpu(alloc->id1.bitmap1.i_used); |
| 581 | if (bits_wanted > free_bits) { |
| 582 | /* uhoh, window change time. */ |
| 583 | status = |
| 584 | ocfs2_local_alloc_slide_window(osb, local_alloc_inode); |
| 585 | if (status < 0) { |
| 586 | if (status != -ENOSPC) |
| 587 | mlog_errno(status); |
| 588 | goto bail; |
| 589 | } |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 590 | |
| 591 | /* |
| 592 | * Under certain conditions, the window slide code |
| 593 | * might have reduced the number of bits available or |
| 594 | * disabled the the local alloc entirely. Re-check |
| 595 | * here and return -ENOSPC if necessary. |
| 596 | */ |
| 597 | status = -ENOSPC; |
| 598 | if (!ocfs2_la_state_enabled(osb)) |
| 599 | goto bail; |
| 600 | |
| 601 | free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) - |
| 602 | le32_to_cpu(alloc->id1.bitmap1.i_used); |
| 603 | if (bits_wanted > free_bits) |
| 604 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 605 | } |
| 606 | |
Joel Becker | 1187c96 | 2008-09-03 20:03:39 -0700 | [diff] [blame] | 607 | if (ac->ac_max_block) |
| 608 | mlog(0, "Calling in_range for max block %llu\n", |
| 609 | (unsigned long long)ac->ac_max_block); |
| 610 | |
| 611 | if (!ocfs2_local_alloc_in_range(local_alloc_inode, ac, |
| 612 | bits_wanted)) { |
| 613 | /* |
| 614 | * The window is outside ac->ac_max_block. |
| 615 | * This errno tells the caller to keep localalloc enabled |
| 616 | * but to get the allocation from the main bitmap. |
| 617 | */ |
| 618 | status = -EFBIG; |
| 619 | goto bail; |
| 620 | } |
| 621 | |
Mark Fasheh | 8fccfc8 | 2007-05-09 17:34:26 -0700 | [diff] [blame] | 622 | ac->ac_inode = local_alloc_inode; |
Tao Ma | a4a4891 | 2008-03-03 17:12:30 +0800 | [diff] [blame] | 623 | /* We should never use localalloc from another slot */ |
| 624 | ac->ac_alloc_slot = osb->slot_num; |
Mark Fasheh | 8fccfc8 | 2007-05-09 17:34:26 -0700 | [diff] [blame] | 625 | ac->ac_which = OCFS2_AC_USE_LOCAL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 626 | get_bh(osb->local_alloc_bh); |
| 627 | ac->ac_bh = osb->local_alloc_bh; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 628 | status = 0; |
| 629 | bail: |
Sunil Mushran | bda0233 | 2007-09-21 11:41:43 -0700 | [diff] [blame] | 630 | if (status < 0 && local_alloc_inode) { |
| 631 | mutex_unlock(&local_alloc_inode->i_mutex); |
Mark Fasheh | 8fccfc8 | 2007-05-09 17:34:26 -0700 | [diff] [blame] | 632 | iput(local_alloc_inode); |
Sunil Mushran | bda0233 | 2007-09-21 11:41:43 -0700 | [diff] [blame] | 633 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 634 | |
Sunil Mushran | 2fbe8d1 | 2007-12-20 14:58:11 -0800 | [diff] [blame] | 635 | mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num, |
| 636 | status); |
| 637 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 638 | mlog_exit(status); |
| 639 | return status; |
| 640 | } |
| 641 | |
| 642 | int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 643 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 644 | struct ocfs2_alloc_context *ac, |
Mark Fasheh | 415cb80 | 2007-09-16 20:10:16 -0700 | [diff] [blame] | 645 | u32 bits_wanted, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 646 | u32 *bit_off, |
| 647 | u32 *num_bits) |
| 648 | { |
| 649 | int status, start; |
| 650 | struct inode *local_alloc_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 651 | void *bitmap; |
| 652 | struct ocfs2_dinode *alloc; |
| 653 | struct ocfs2_local_alloc *la; |
| 654 | |
| 655 | mlog_entry_void(); |
| 656 | BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL); |
| 657 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 658 | local_alloc_inode = ac->ac_inode; |
| 659 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; |
| 660 | la = OCFS2_LOCAL_ALLOC(alloc); |
| 661 | |
| 662 | start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted); |
| 663 | if (start == -1) { |
| 664 | /* TODO: Shouldn't we just BUG here? */ |
| 665 | status = -ENOSPC; |
| 666 | mlog_errno(status); |
| 667 | goto bail; |
| 668 | } |
| 669 | |
| 670 | bitmap = la->la_bitmap; |
| 671 | *bit_off = le32_to_cpu(la->la_bm_off) + start; |
| 672 | /* local alloc is always contiguous by nature -- we never |
| 673 | * delete bits from it! */ |
| 674 | *num_bits = bits_wanted; |
| 675 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 676 | status = ocfs2_journal_access_di(handle, |
| 677 | INODE_CACHE(local_alloc_inode), |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 678 | osb->local_alloc_bh, |
| 679 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 680 | if (status < 0) { |
| 681 | mlog_errno(status); |
| 682 | goto bail; |
| 683 | } |
| 684 | |
| 685 | while(bits_wanted--) |
| 686 | ocfs2_set_bit(start++, bitmap); |
| 687 | |
Marcin Slusarz | 0dd3256 | 2008-02-13 00:06:18 +0100 | [diff] [blame] | 688 | le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits); |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame^] | 689 | ocfs2_journal_dirty(handle, osb->local_alloc_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 690 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 691 | bail: |
| 692 | mlog_exit(status); |
| 693 | return status; |
| 694 | } |
| 695 | |
| 696 | static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc) |
| 697 | { |
| 698 | int i; |
| 699 | u8 *buffer; |
| 700 | u32 count = 0; |
| 701 | struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc); |
| 702 | |
| 703 | mlog_entry_void(); |
| 704 | |
| 705 | buffer = la->la_bitmap; |
| 706 | for (i = 0; i < le16_to_cpu(la->la_size); i++) |
| 707 | count += hweight8(buffer[i]); |
| 708 | |
| 709 | mlog_exit(count); |
| 710 | return count; |
| 711 | } |
| 712 | |
| 713 | static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb, |
| 714 | struct ocfs2_dinode *alloc, |
| 715 | u32 numbits) |
| 716 | { |
| 717 | int numfound, bitoff, left, startoff, lastzero; |
| 718 | void *bitmap = NULL; |
| 719 | |
| 720 | mlog_entry("(numbits wanted = %u)\n", numbits); |
| 721 | |
| 722 | if (!alloc->id1.bitmap1.i_total) { |
| 723 | mlog(0, "No bits in my window!\n"); |
| 724 | bitoff = -1; |
| 725 | goto bail; |
| 726 | } |
| 727 | |
| 728 | bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap; |
| 729 | |
| 730 | numfound = bitoff = startoff = 0; |
| 731 | lastzero = -1; |
| 732 | left = le32_to_cpu(alloc->id1.bitmap1.i_total); |
| 733 | while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) { |
| 734 | if (bitoff == left) { |
| 735 | /* mlog(0, "bitoff (%d) == left", bitoff); */ |
| 736 | break; |
| 737 | } |
| 738 | /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, " |
| 739 | "numfound = %d\n", bitoff, startoff, numfound);*/ |
| 740 | |
| 741 | /* Ok, we found a zero bit... is it contig. or do we |
| 742 | * start over?*/ |
| 743 | if (bitoff == startoff) { |
| 744 | /* we found a zero */ |
| 745 | numfound++; |
| 746 | startoff++; |
| 747 | } else { |
| 748 | /* got a zero after some ones */ |
| 749 | numfound = 1; |
| 750 | startoff = bitoff+1; |
| 751 | } |
| 752 | /* we got everything we needed */ |
| 753 | if (numfound == numbits) { |
| 754 | /* mlog(0, "Found it all!\n"); */ |
| 755 | break; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff, |
| 760 | numfound); |
| 761 | |
| 762 | if (numfound == numbits) |
| 763 | bitoff = startoff - numfound; |
| 764 | else |
| 765 | bitoff = -1; |
| 766 | |
| 767 | bail: |
| 768 | mlog_exit(bitoff); |
| 769 | return bitoff; |
| 770 | } |
| 771 | |
| 772 | static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc) |
| 773 | { |
| 774 | struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc); |
| 775 | int i; |
| 776 | mlog_entry_void(); |
| 777 | |
| 778 | alloc->id1.bitmap1.i_total = 0; |
| 779 | alloc->id1.bitmap1.i_used = 0; |
| 780 | la->la_bm_off = 0; |
| 781 | for(i = 0; i < le16_to_cpu(la->la_size); i++) |
| 782 | la->la_bitmap[i] = 0; |
| 783 | |
| 784 | mlog_exit_void(); |
| 785 | } |
| 786 | |
| 787 | #if 0 |
| 788 | /* turn this on and uncomment below to aid debugging window shifts. */ |
| 789 | static void ocfs2_verify_zero_bits(unsigned long *bitmap, |
| 790 | unsigned int start, |
| 791 | unsigned int count) |
| 792 | { |
| 793 | unsigned int tmp = count; |
| 794 | while(tmp--) { |
| 795 | if (ocfs2_test_bit(start + tmp, bitmap)) { |
| 796 | printk("ocfs2_verify_zero_bits: start = %u, count = " |
| 797 | "%u\n", start, count); |
| 798 | printk("ocfs2_verify_zero_bits: bit %u is set!", |
| 799 | start + tmp); |
| 800 | BUG(); |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | #endif |
| 805 | |
| 806 | /* |
| 807 | * sync the local alloc to main bitmap. |
| 808 | * |
| 809 | * assumes you've already locked the main bitmap -- the bitmap inode |
| 810 | * passed is used for caching. |
| 811 | */ |
| 812 | static int ocfs2_sync_local_to_main(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_dinode *alloc, |
| 815 | struct inode *main_bm_inode, |
| 816 | struct buffer_head *main_bm_bh) |
| 817 | { |
| 818 | int status = 0; |
| 819 | int bit_off, left, count, start; |
| 820 | u64 la_start_blk; |
| 821 | u64 blkno; |
| 822 | void *bitmap; |
| 823 | struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc); |
| 824 | |
Jan Kara | 5a58c3e | 2007-11-13 19:59:33 +0100 | [diff] [blame] | 825 | mlog_entry("total = %u, used = %u\n", |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 826 | le32_to_cpu(alloc->id1.bitmap1.i_total), |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 827 | le32_to_cpu(alloc->id1.bitmap1.i_used)); |
| 828 | |
| 829 | if (!alloc->id1.bitmap1.i_total) { |
| 830 | mlog(0, "nothing to sync!\n"); |
| 831 | goto bail; |
| 832 | } |
| 833 | |
| 834 | if (le32_to_cpu(alloc->id1.bitmap1.i_used) == |
| 835 | le32_to_cpu(alloc->id1.bitmap1.i_total)) { |
| 836 | mlog(0, "all bits were taken!\n"); |
| 837 | goto bail; |
| 838 | } |
| 839 | |
| 840 | la_start_blk = ocfs2_clusters_to_blocks(osb->sb, |
| 841 | le32_to_cpu(la->la_bm_off)); |
| 842 | bitmap = la->la_bitmap; |
| 843 | start = count = bit_off = 0; |
| 844 | left = le32_to_cpu(alloc->id1.bitmap1.i_total); |
| 845 | |
| 846 | while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start)) |
| 847 | != -1) { |
| 848 | if ((bit_off < left) && (bit_off == start)) { |
| 849 | count++; |
| 850 | start++; |
| 851 | continue; |
| 852 | } |
| 853 | if (count) { |
| 854 | blkno = la_start_blk + |
| 855 | ocfs2_clusters_to_blocks(osb->sb, |
| 856 | start - count); |
| 857 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 858 | mlog(0, "freeing %u bits starting at local alloc bit " |
| 859 | "%u (la_start_blk = %llu, blkno = %llu)\n", |
| 860 | count, start - count, |
| 861 | (unsigned long long)la_start_blk, |
| 862 | (unsigned long long)blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 863 | |
Mark Fasheh | b4414ee | 2010-03-11 18:31:09 -0800 | [diff] [blame] | 864 | status = ocfs2_release_clusters(handle, |
| 865 | main_bm_inode, |
| 866 | main_bm_bh, blkno, |
| 867 | count); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 868 | if (status < 0) { |
| 869 | mlog_errno(status); |
| 870 | goto bail; |
| 871 | } |
| 872 | } |
| 873 | if (bit_off >= left) |
| 874 | break; |
| 875 | count = 1; |
| 876 | start = bit_off + 1; |
| 877 | } |
| 878 | |
| 879 | bail: |
| 880 | mlog_exit(status); |
| 881 | return status; |
| 882 | } |
| 883 | |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 884 | enum ocfs2_la_event { |
| 885 | OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */ |
| 886 | OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has |
| 887 | * enough bits theoretically |
| 888 | * free, but a contiguous |
| 889 | * allocation could not be |
| 890 | * found. */ |
| 891 | OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have |
| 892 | * enough bits free to satisfy |
| 893 | * our request. */ |
| 894 | }; |
| 895 | #define OCFS2_LA_ENABLE_INTERVAL (30 * HZ) |
| 896 | /* |
| 897 | * Given an event, calculate the size of our next local alloc window. |
| 898 | * |
| 899 | * This should always be called under i_mutex of the local alloc inode |
| 900 | * so that local alloc disabling doesn't race with processes trying to |
| 901 | * use the allocator. |
| 902 | * |
| 903 | * Returns the state which the local alloc was left in. This value can |
| 904 | * be ignored by some paths. |
| 905 | */ |
| 906 | static int ocfs2_recalc_la_window(struct ocfs2_super *osb, |
| 907 | enum ocfs2_la_event event) |
| 908 | { |
| 909 | unsigned int bits; |
| 910 | int state; |
| 911 | |
| 912 | spin_lock(&osb->osb_lock); |
| 913 | if (osb->local_alloc_state == OCFS2_LA_DISABLED) { |
| 914 | WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED); |
| 915 | goto out_unlock; |
| 916 | } |
| 917 | |
| 918 | /* |
| 919 | * ENOSPC and fragmentation are treated similarly for now. |
| 920 | */ |
| 921 | if (event == OCFS2_LA_EVENT_ENOSPC || |
| 922 | event == OCFS2_LA_EVENT_FRAGMENTED) { |
| 923 | /* |
| 924 | * We ran out of contiguous space in the primary |
| 925 | * bitmap. Drastically reduce the number of bits used |
| 926 | * by local alloc until we have to disable it. |
| 927 | */ |
| 928 | bits = osb->local_alloc_bits >> 1; |
| 929 | if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) { |
| 930 | /* |
| 931 | * By setting state to THROTTLED, we'll keep |
| 932 | * the number of local alloc bits used down |
| 933 | * until an event occurs which would give us |
| 934 | * reason to assume the bitmap situation might |
| 935 | * have changed. |
| 936 | */ |
| 937 | osb->local_alloc_state = OCFS2_LA_THROTTLED; |
| 938 | osb->local_alloc_bits = bits; |
| 939 | } else { |
| 940 | osb->local_alloc_state = OCFS2_LA_DISABLED; |
| 941 | } |
| 942 | queue_delayed_work(ocfs2_wq, &osb->la_enable_wq, |
| 943 | OCFS2_LA_ENABLE_INTERVAL); |
| 944 | goto out_unlock; |
| 945 | } |
| 946 | |
| 947 | /* |
| 948 | * Don't increase the size of the local alloc window until we |
| 949 | * know we might be able to fulfill the request. Otherwise, we |
| 950 | * risk bouncing around the global bitmap during periods of |
| 951 | * low space. |
| 952 | */ |
| 953 | if (osb->local_alloc_state != OCFS2_LA_THROTTLED) |
| 954 | osb->local_alloc_bits = osb->local_alloc_default_bits; |
| 955 | |
| 956 | out_unlock: |
| 957 | state = osb->local_alloc_state; |
| 958 | spin_unlock(&osb->osb_lock); |
| 959 | |
| 960 | return state; |
| 961 | } |
| 962 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 963 | static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 964 | struct ocfs2_alloc_context **ac, |
| 965 | struct inode **bitmap_inode, |
| 966 | struct buffer_head **bitmap_bh) |
| 967 | { |
| 968 | int status; |
| 969 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 970 | *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 971 | if (!(*ac)) { |
| 972 | status = -ENOMEM; |
| 973 | mlog_errno(status); |
| 974 | goto bail; |
| 975 | } |
| 976 | |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 977 | retry_enospc: |
Mark Fasheh | b22b63e | 2010-03-11 18:43:46 -0800 | [diff] [blame] | 978 | (*ac)->ac_bits_wanted = osb->local_alloc_default_bits; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 979 | status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac); |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 980 | if (status == -ENOSPC) { |
| 981 | if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) == |
| 982 | OCFS2_LA_DISABLED) |
| 983 | goto bail; |
| 984 | |
| 985 | ocfs2_free_ac_resource(*ac); |
| 986 | memset(*ac, 0, sizeof(struct ocfs2_alloc_context)); |
| 987 | goto retry_enospc; |
| 988 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 989 | if (status < 0) { |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 990 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 991 | goto bail; |
| 992 | } |
| 993 | |
| 994 | *bitmap_inode = (*ac)->ac_inode; |
| 995 | igrab(*bitmap_inode); |
| 996 | *bitmap_bh = (*ac)->ac_bh; |
| 997 | get_bh(*bitmap_bh); |
| 998 | status = 0; |
| 999 | bail: |
| 1000 | if ((status < 0) && *ac) { |
| 1001 | ocfs2_free_alloc_context(*ac); |
| 1002 | *ac = NULL; |
| 1003 | } |
| 1004 | |
| 1005 | mlog_exit(status); |
| 1006 | return status; |
| 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | * pass it the bitmap lock in lock_bh if you have it. |
| 1011 | */ |
| 1012 | static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1013 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1014 | struct ocfs2_alloc_context *ac) |
| 1015 | { |
| 1016 | int status = 0; |
| 1017 | u32 cluster_off, cluster_count; |
| 1018 | struct ocfs2_dinode *alloc = NULL; |
| 1019 | struct ocfs2_local_alloc *la; |
| 1020 | |
| 1021 | mlog_entry_void(); |
| 1022 | |
| 1023 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; |
| 1024 | la = OCFS2_LOCAL_ALLOC(alloc); |
| 1025 | |
| 1026 | if (alloc->id1.bitmap1.i_total) |
| 1027 | mlog(0, "asking me to alloc a new window over a non-empty " |
| 1028 | "one\n"); |
| 1029 | |
| 1030 | mlog(0, "Allocating %u clusters for a new window.\n", |
Mark Fasheh | ebcee4b | 2008-07-28 14:55:20 -0700 | [diff] [blame] | 1031 | osb->local_alloc_bits); |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1032 | |
| 1033 | /* Instruct the allocation code to try the most recently used |
| 1034 | * cluster group. We'll re-record the group used this pass |
| 1035 | * below. */ |
| 1036 | ac->ac_last_group = osb->la_last_gd; |
| 1037 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1038 | /* we used the generic suballoc reserve function, but we set |
| 1039 | * everything up nicely, so there's no reason why we can't use |
| 1040 | * the more specific cluster api to claim bits. */ |
Mark Fasheh | ebcee4b | 2008-07-28 14:55:20 -0700 | [diff] [blame] | 1041 | status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1042 | &cluster_off, &cluster_count); |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 1043 | if (status == -ENOSPC) { |
| 1044 | retry_enospc: |
| 1045 | /* |
| 1046 | * Note: We could also try syncing the journal here to |
| 1047 | * allow use of any free bits which the current |
| 1048 | * transaction can't give us access to. --Mark |
| 1049 | */ |
| 1050 | if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) == |
| 1051 | OCFS2_LA_DISABLED) |
| 1052 | goto bail; |
| 1053 | |
Mark Fasheh | b22b63e | 2010-03-11 18:43:46 -0800 | [diff] [blame] | 1054 | ac->ac_bits_wanted = osb->local_alloc_default_bits; |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 1055 | status = ocfs2_claim_clusters(osb, handle, ac, |
| 1056 | osb->local_alloc_bits, |
| 1057 | &cluster_off, |
| 1058 | &cluster_count); |
| 1059 | if (status == -ENOSPC) |
| 1060 | goto retry_enospc; |
| 1061 | /* |
| 1062 | * We only shrunk the *minimum* number of in our |
| 1063 | * request - it's entirely possible that the allocator |
| 1064 | * might give us more than we asked for. |
| 1065 | */ |
| 1066 | if (status == 0) { |
| 1067 | spin_lock(&osb->osb_lock); |
| 1068 | osb->local_alloc_bits = cluster_count; |
| 1069 | spin_unlock(&osb->osb_lock); |
| 1070 | } |
| 1071 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1072 | if (status < 0) { |
| 1073 | if (status != -ENOSPC) |
| 1074 | mlog_errno(status); |
| 1075 | goto bail; |
| 1076 | } |
| 1077 | |
Mark Fasheh | 883d4ca | 2006-06-05 16:41:00 -0400 | [diff] [blame] | 1078 | osb->la_last_gd = ac->ac_last_group; |
| 1079 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1080 | la->la_bm_off = cpu_to_le32(cluster_off); |
| 1081 | alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count); |
| 1082 | /* just in case... In the future when we find space ourselves, |
| 1083 | * we don't have to get all contiguous -- but we'll have to |
| 1084 | * set all previously used bits in bitmap and update |
| 1085 | * la_bits_set before setting the bits in the main bitmap. */ |
| 1086 | alloc->id1.bitmap1.i_used = 0; |
| 1087 | memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0, |
| 1088 | le16_to_cpu(la->la_size)); |
| 1089 | |
| 1090 | mlog(0, "New window allocated:\n"); |
| 1091 | mlog(0, "window la_bm_off = %u\n", |
| 1092 | OCFS2_LOCAL_ALLOC(alloc)->la_bm_off); |
| 1093 | mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total)); |
| 1094 | |
| 1095 | bail: |
| 1096 | mlog_exit(status); |
| 1097 | return status; |
| 1098 | } |
| 1099 | |
| 1100 | /* Note that we do *NOT* lock the local alloc inode here as |
| 1101 | * it's been locked already for us. */ |
| 1102 | static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb, |
| 1103 | struct inode *local_alloc_inode) |
| 1104 | { |
| 1105 | int status = 0; |
| 1106 | struct buffer_head *main_bm_bh = NULL; |
| 1107 | struct inode *main_bm_inode = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1108 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1109 | struct ocfs2_dinode *alloc; |
| 1110 | struct ocfs2_dinode *alloc_copy = NULL; |
| 1111 | struct ocfs2_alloc_context *ac = NULL; |
| 1112 | |
| 1113 | mlog_entry_void(); |
| 1114 | |
Mark Fasheh | 9c7af40 | 2008-07-28 18:02:53 -0700 | [diff] [blame] | 1115 | ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE); |
| 1116 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1117 | /* This will lock the main bitmap for us. */ |
| 1118 | status = ocfs2_local_alloc_reserve_for_window(osb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1119 | &ac, |
| 1120 | &main_bm_inode, |
| 1121 | &main_bm_bh); |
| 1122 | if (status < 0) { |
| 1123 | if (status != -ENOSPC) |
| 1124 | mlog_errno(status); |
| 1125 | goto bail; |
| 1126 | } |
| 1127 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 1128 | handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1129 | if (IS_ERR(handle)) { |
| 1130 | status = PTR_ERR(handle); |
| 1131 | handle = NULL; |
| 1132 | mlog_errno(status); |
| 1133 | goto bail; |
| 1134 | } |
| 1135 | |
| 1136 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; |
| 1137 | |
| 1138 | /* We want to clear the local alloc before doing anything |
| 1139 | * else, so that if we error later during this operation, |
| 1140 | * local alloc shutdown won't try to double free main bitmap |
| 1141 | * bits. Make a copy so the sync function knows which bits to |
| 1142 | * free. */ |
Sunil Mushran | 4ba1c5b | 2008-04-18 15:03:59 -0700 | [diff] [blame] | 1143 | alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1144 | if (!alloc_copy) { |
| 1145 | status = -ENOMEM; |
| 1146 | mlog_errno(status); |
| 1147 | goto bail; |
| 1148 | } |
| 1149 | memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size); |
| 1150 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1151 | status = ocfs2_journal_access_di(handle, |
| 1152 | INODE_CACHE(local_alloc_inode), |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1153 | osb->local_alloc_bh, |
| 1154 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1155 | if (status < 0) { |
| 1156 | mlog_errno(status); |
| 1157 | goto bail; |
| 1158 | } |
| 1159 | |
| 1160 | ocfs2_clear_local_alloc(alloc); |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame^] | 1161 | ocfs2_journal_dirty(handle, osb->local_alloc_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1162 | |
| 1163 | status = ocfs2_sync_local_to_main(osb, handle, alloc_copy, |
| 1164 | main_bm_inode, main_bm_bh); |
| 1165 | if (status < 0) { |
| 1166 | mlog_errno(status); |
| 1167 | goto bail; |
| 1168 | } |
| 1169 | |
| 1170 | status = ocfs2_local_alloc_new_window(osb, handle, ac); |
| 1171 | if (status < 0) { |
| 1172 | if (status != -ENOSPC) |
| 1173 | mlog_errno(status); |
| 1174 | goto bail; |
| 1175 | } |
| 1176 | |
| 1177 | atomic_inc(&osb->alloc_stats.moves); |
| 1178 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1179 | bail: |
| 1180 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 1181 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1182 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1183 | brelse(main_bm_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1184 | |
| 1185 | if (main_bm_inode) |
| 1186 | iput(main_bm_inode); |
| 1187 | |
| 1188 | if (alloc_copy) |
| 1189 | kfree(alloc_copy); |
| 1190 | |
| 1191 | if (ac) |
| 1192 | ocfs2_free_alloc_context(ac); |
| 1193 | |
| 1194 | mlog_exit(status); |
| 1195 | return status; |
| 1196 | } |
| 1197 | |