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