blob: f71658adddb51c7e7a62b5bdf99eb6d4f3bac01b [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * localalloc.c
5 *
6 * Node local data allocation
7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/fs.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/highmem.h>
30#include <linux/bitops.h>
31
32#define MLOG_MASK_PREFIX ML_DISK_ALLOC
33#include <cluster/masklog.h>
34
35#include "ocfs2.h"
36
37#include "alloc.h"
38#include "dlmglue.h"
39#include "inode.h"
40#include "journal.h"
41#include "localalloc.h"
42#include "suballoc.h"
43#include "super.h"
44#include "sysfile.h"
45
46#include "buffer_head_io.h"
47
48#define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab))
49
Mark Fashehccd979b2005-12-15 14:31:24 -080050static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc);
51
52static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
53 struct ocfs2_dinode *alloc,
54 u32 numbits);
55
56static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc);
57
58static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -070059 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080060 struct ocfs2_dinode *alloc,
61 struct inode *main_bm_inode,
62 struct buffer_head *main_bm_bh);
63
64static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -080065 struct ocfs2_alloc_context **ac,
66 struct inode **bitmap_inode,
67 struct buffer_head **bitmap_bh);
68
69static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -070070 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080071 struct ocfs2_alloc_context *ac);
72
73static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
74 struct inode *local_alloc_inode);
75
Mark Fasheh9c7af402008-07-28 18:02:53 -070076static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb)
77{
78 return (osb->local_alloc_state == OCFS2_LA_THROTTLED ||
79 osb->local_alloc_state == OCFS2_LA_ENABLED);
80}
81
82void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb,
83 unsigned int num_clusters)
84{
85 spin_lock(&osb->osb_lock);
86 if (osb->local_alloc_state == OCFS2_LA_DISABLED ||
87 osb->local_alloc_state == OCFS2_LA_THROTTLED)
88 if (num_clusters >= osb->local_alloc_default_bits) {
89 cancel_delayed_work(&osb->la_enable_wq);
90 osb->local_alloc_state = OCFS2_LA_ENABLED;
91 }
92 spin_unlock(&osb->osb_lock);
93}
94
95void ocfs2_la_enable_worker(struct work_struct *work)
96{
97 struct ocfs2_super *osb =
98 container_of(work, struct ocfs2_super,
99 la_enable_wq.work);
100 spin_lock(&osb->osb_lock);
101 osb->local_alloc_state = OCFS2_LA_ENABLED;
102 spin_unlock(&osb->osb_lock);
103}
104
Mark Fashehccd979b2005-12-15 14:31:24 -0800105/*
106 * Tell us whether a given allocation should use the local alloc
107 * file. Otherwise, it has to go to the main bitmap.
Mark Fasheh9c7af402008-07-28 18:02:53 -0700108 *
109 * This function does semi-dirty reads of local alloc size and state!
110 * This is ok however, as the values are re-checked once under mutex.
Mark Fashehccd979b2005-12-15 14:31:24 -0800111 */
112int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
113{
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800114 int ret = 0;
Mark Fasheh9c7af402008-07-28 18:02:53 -0700115 int la_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800116
Mark Fasheh9c7af402008-07-28 18:02:53 -0700117 spin_lock(&osb->osb_lock);
118 la_bits = osb->local_alloc_bits;
119
120 if (!ocfs2_la_state_enabled(osb))
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800121 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800122
123 /* la_bits should be at least twice the size (in clusters) of
124 * a new block group. We want to be sure block group
125 * allocations go through the local alloc, so allow an
126 * allocation to take up to half the bitmap. */
127 if (bits > (la_bits / 2))
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800128 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800129
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800130 ret = 1;
131bail:
132 mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n",
133 osb->local_alloc_state, (unsigned long long)bits, la_bits, ret);
Mark Fasheh9c7af402008-07-28 18:02:53 -0700134 spin_unlock(&osb->osb_lock);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800135 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800136}
137
138int ocfs2_load_local_alloc(struct ocfs2_super *osb)
139{
140 int status = 0;
141 struct ocfs2_dinode *alloc = NULL;
142 struct buffer_head *alloc_bh = NULL;
143 u32 num_used;
144 struct inode *inode = NULL;
145 struct ocfs2_local_alloc *la;
146
147 mlog_entry_void();
148
Mark Fashehebcee4b2008-07-28 14:55:20 -0700149 if (osb->local_alloc_bits == 0)
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800150 goto bail;
151
Mark Fashehebcee4b2008-07-28 14:55:20 -0700152 if (osb->local_alloc_bits >= osb->bitmap_cpg) {
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800153 mlog(ML_NOTICE, "Requested local alloc window %d is larger "
154 "than max possible %u. Using defaults.\n",
Mark Fashehebcee4b2008-07-28 14:55:20 -0700155 osb->local_alloc_bits, (osb->bitmap_cpg - 1));
156 osb->local_alloc_bits =
157 ocfs2_megabytes_to_clusters(osb->sb,
158 OCFS2_DEFAULT_LOCAL_ALLOC_SIZE);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800159 }
160
Mark Fashehccd979b2005-12-15 14:31:24 -0800161 /* read the alloc off disk */
162 inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
163 osb->slot_num);
164 if (!inode) {
165 status = -EINVAL;
166 mlog_errno(status);
167 goto bail;
168 }
169
170 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
171 &alloc_bh, 0, inode);
172 if (status < 0) {
173 mlog_errno(status);
174 goto bail;
175 }
176
177 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
178 la = OCFS2_LOCAL_ALLOC(alloc);
179
180 if (!(le32_to_cpu(alloc->i_flags) &
181 (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800182 mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
183 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800184 status = -EINVAL;
185 goto bail;
186 }
187
188 if ((la->la_size == 0) ||
189 (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
190 mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
191 le16_to_cpu(la->la_size));
192 status = -EINVAL;
193 goto bail;
194 }
195
196 /* do a little verification. */
197 num_used = ocfs2_local_alloc_count_bits(alloc);
198
199 /* hopefully the local alloc has always been recovered before
200 * we load it. */
201 if (num_used
202 || alloc->id1.bitmap1.i_used
203 || alloc->id1.bitmap1.i_total
204 || la->la_bm_off)
205 mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
206 "found = %u, set = %u, taken = %u, off = %u\n",
207 num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
208 le32_to_cpu(alloc->id1.bitmap1.i_total),
209 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
210
211 osb->local_alloc_bh = alloc_bh;
212 osb->local_alloc_state = OCFS2_LA_ENABLED;
213
214bail:
215 if (status < 0)
216 if (alloc_bh)
217 brelse(alloc_bh);
218 if (inode)
219 iput(inode);
220
Mark Fashehebcee4b2008-07-28 14:55:20 -0700221 mlog(0, "Local alloc window bits = %d\n", osb->local_alloc_bits);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800222
Mark Fashehccd979b2005-12-15 14:31:24 -0800223 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.*/
234void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
235{
236 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700237 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800238 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 Fasheh9c7af402008-07-28 18:02:53 -0700247 cancel_delayed_work(&osb->la_enable_wq);
248 flush_workqueue(ocfs2_wq);
249
Mark Fashehccd979b2005-12-15 14:31:24 -0800250 if (osb->local_alloc_state == OCFS2_LA_UNUSED)
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700251 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800252
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 Fasheh8898a5a2006-10-05 15:42:08 -0700260 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800261 }
262
263 osb->local_alloc_state = OCFS2_LA_DISABLED;
264
Mark Fashehccd979b2005-12-15 14:31:24 -0800265 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 Fasheh8898a5a2006-10-05 15:42:08 -0700271 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800272 }
273
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700274 mutex_lock(&main_bm_inode->i_mutex);
275
Mark Fashehe63aecb62007-10-18 15:30:42 -0700276 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800277 if (status < 0) {
278 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700279 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -0800280 }
281
282 /* WINDOW_MOVE_CREDITS is a bit heavy... */
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700283 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800284 if (IS_ERR(handle)) {
285 mlog_errno(PTR_ERR(handle));
286 handle = NULL;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700287 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800288 }
289
290 bh = osb->local_alloc_bh;
291 alloc = (struct ocfs2_dinode *) bh->b_data;
292
Sunil Mushran4ba1c5b2008-04-18 15:03:59 -0700293 alloc_copy = kmalloc(bh->b_size, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800294 if (!alloc_copy) {
295 status = -ENOMEM;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700296 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800297 }
298 memcpy(alloc_copy, alloc, bh->b_size);
299
300 status = ocfs2_journal_access(handle, local_alloc_inode, bh,
301 OCFS2_JOURNAL_ACCESS_WRITE);
302 if (status < 0) {
303 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700304 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800305 }
306
307 ocfs2_clear_local_alloc(alloc);
308
309 status = ocfs2_journal_dirty(handle, bh);
310 if (status < 0) {
311 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700312 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800313 }
314
315 brelse(bh);
316 osb->local_alloc_bh = NULL;
317 osb->local_alloc_state = OCFS2_LA_UNUSED;
318
319 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
320 main_bm_inode, main_bm_bh);
321 if (status < 0)
322 mlog_errno(status);
323
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700324out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700325 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800326
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700327out_unlock:
Mark Fashehccd979b2005-12-15 14:31:24 -0800328 if (main_bm_bh)
329 brelse(main_bm_bh);
330
Mark Fashehe63aecb62007-10-18 15:30:42 -0700331 ocfs2_inode_unlock(main_bm_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800332
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700333out_mutex:
334 mutex_unlock(&main_bm_inode->i_mutex);
335 iput(main_bm_inode);
336
337out:
Mark Fashehccd979b2005-12-15 14:31:24 -0800338 if (local_alloc_inode)
339 iput(local_alloc_inode);
340
341 if (alloc_copy)
342 kfree(alloc_copy);
343
344 mlog_exit_void();
345}
346
347/*
348 * We want to free the bitmap bits outside of any recovery context as
349 * we'll need a cluster lock to do so, but we must clear the local
350 * alloc before giving up the recovered nodes journal. To solve this,
351 * we kmalloc a copy of the local alloc before it's change for the
352 * caller to process with ocfs2_complete_local_alloc_recovery
353 */
354int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
355 int slot_num,
356 struct ocfs2_dinode **alloc_copy)
357{
358 int status = 0;
359 struct buffer_head *alloc_bh = NULL;
360 struct inode *inode = NULL;
361 struct ocfs2_dinode *alloc;
362
363 mlog_entry("(slot_num = %d)\n", slot_num);
364
365 *alloc_copy = NULL;
366
367 inode = ocfs2_get_system_file_inode(osb,
368 LOCAL_ALLOC_SYSTEM_INODE,
369 slot_num);
370 if (!inode) {
371 status = -EINVAL;
372 mlog_errno(status);
373 goto bail;
374 }
375
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800376 mutex_lock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800377
378 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
379 &alloc_bh, 0, inode);
380 if (status < 0) {
381 mlog_errno(status);
382 goto bail;
383 }
384
385 *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
386 if (!(*alloc_copy)) {
387 status = -ENOMEM;
388 goto bail;
389 }
390 memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
391
392 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
393 ocfs2_clear_local_alloc(alloc);
394
395 status = ocfs2_write_block(osb, alloc_bh, inode);
396 if (status < 0)
397 mlog_errno(status);
398
399bail:
400 if ((status < 0) && (*alloc_copy)) {
401 kfree(*alloc_copy);
402 *alloc_copy = NULL;
403 }
404
405 if (alloc_bh)
406 brelse(alloc_bh);
407
408 if (inode) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800409 mutex_unlock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800410 iput(inode);
411 }
412
413 mlog_exit(status);
414 return status;
415}
416
417/*
418 * Step 2: By now, we've completed the journal recovery, we've stamped
419 * a clean local alloc on disk and dropped the node out of the
420 * recovery map. Dlm locks will no longer stall, so lets clear out the
421 * main bitmap.
422 */
423int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
424 struct ocfs2_dinode *alloc)
425{
426 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700427 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800428 struct buffer_head *main_bm_bh = NULL;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700429 struct inode *main_bm_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800430
431 mlog_entry_void();
432
Mark Fashehccd979b2005-12-15 14:31:24 -0800433 main_bm_inode = ocfs2_get_system_file_inode(osb,
434 GLOBAL_BITMAP_SYSTEM_INODE,
435 OCFS2_INVALID_SLOT);
436 if (!main_bm_inode) {
437 status = -EINVAL;
438 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700439 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800440 }
441
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700442 mutex_lock(&main_bm_inode->i_mutex);
443
Mark Fashehe63aecb62007-10-18 15:30:42 -0700444 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800445 if (status < 0) {
446 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700447 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -0800448 }
449
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700450 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800451 if (IS_ERR(handle)) {
452 status = PTR_ERR(handle);
453 handle = NULL;
454 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700455 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800456 }
457
458 /* we want the bitmap change to be recorded on disk asap */
Mark Fasheh1fabe142006-10-09 18:11:45 -0700459 handle->h_sync = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800460
461 status = ocfs2_sync_local_to_main(osb, handle, alloc,
462 main_bm_inode, main_bm_bh);
463 if (status < 0)
464 mlog_errno(status);
465
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700466 ocfs2_commit_trans(osb, handle);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700467
468out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700469 ocfs2_inode_unlock(main_bm_inode, 1);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700470
471out_mutex:
472 mutex_unlock(&main_bm_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800473
474 if (main_bm_bh)
475 brelse(main_bm_bh);
476
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700477 iput(main_bm_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800478
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700479out:
Tao Ma4d0ddb22008-03-05 16:11:46 +0800480 if (!status)
481 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800482 mlog_exit(status);
483 return status;
484}
485
486/*
Mark Fasheh9c7af402008-07-28 18:02:53 -0700487 * make sure we've got at least bits_wanted contiguous bits in the
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800488 * local alloc. You lose them when you drop i_mutex.
Mark Fashehccd979b2005-12-15 14:31:24 -0800489 *
490 * We will add ourselves to the transaction passed in, but may start
491 * our own in order to shift windows.
492 */
493int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800494 u32 bits_wanted,
495 struct ocfs2_alloc_context *ac)
496{
497 int status;
498 struct ocfs2_dinode *alloc;
499 struct inode *local_alloc_inode;
500 unsigned int free_bits;
501
502 mlog_entry_void();
503
Mark Fashehccd979b2005-12-15 14:31:24 -0800504 BUG_ON(!ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800505
506 local_alloc_inode =
507 ocfs2_get_system_file_inode(osb,
508 LOCAL_ALLOC_SYSTEM_INODE,
509 osb->slot_num);
510 if (!local_alloc_inode) {
511 status = -ENOENT;
512 mlog_errno(status);
513 goto bail;
514 }
Mark Fashehda5cbf22006-10-06 18:34:35 -0700515
516 mutex_lock(&local_alloc_inode->i_mutex);
517
Mark Fasheh9c7af402008-07-28 18:02:53 -0700518 /*
519 * We must double check state and allocator bits because
520 * another process may have changed them while holding i_mutex.
521 */
522 spin_lock(&osb->osb_lock);
523 if (!ocfs2_la_state_enabled(osb) ||
524 (bits_wanted > osb->local_alloc_bits)) {
525 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800526 status = -ENOSPC;
527 goto bail;
528 }
Mark Fasheh9c7af402008-07-28 18:02:53 -0700529 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800530
531 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
532
Joel Beckere407e392008-06-12 22:35:39 -0700533#ifdef CONFIG_OCFS2_DEBUG_FS
Mark Fashehccd979b2005-12-15 14:31:24 -0800534 if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
535 ocfs2_local_alloc_count_bits(alloc)) {
Mark Fashehb06970532006-03-03 10:24:33 -0800536 ocfs2_error(osb->sb, "local alloc inode %llu says it has "
Mark Fashehccd979b2005-12-15 14:31:24 -0800537 "%u free bits, but a count shows %u",
Mark Fashehb06970532006-03-03 10:24:33 -0800538 (unsigned long long)le64_to_cpu(alloc->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -0800539 le32_to_cpu(alloc->id1.bitmap1.i_used),
540 ocfs2_local_alloc_count_bits(alloc));
541 status = -EIO;
542 goto bail;
543 }
Jan Kara5a58c3e2007-11-13 19:59:33 +0100544#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800545
546 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
547 le32_to_cpu(alloc->id1.bitmap1.i_used);
548 if (bits_wanted > free_bits) {
549 /* uhoh, window change time. */
550 status =
551 ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
552 if (status < 0) {
553 if (status != -ENOSPC)
554 mlog_errno(status);
555 goto bail;
556 }
Mark Fasheh9c7af402008-07-28 18:02:53 -0700557
558 /*
559 * Under certain conditions, the window slide code
560 * might have reduced the number of bits available or
561 * disabled the the local alloc entirely. Re-check
562 * here and return -ENOSPC if necessary.
563 */
564 status = -ENOSPC;
565 if (!ocfs2_la_state_enabled(osb))
566 goto bail;
567
568 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
569 le32_to_cpu(alloc->id1.bitmap1.i_used);
570 if (bits_wanted > free_bits)
571 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800572 }
573
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700574 ac->ac_inode = local_alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800575 /* We should never use localalloc from another slot */
576 ac->ac_alloc_slot = osb->slot_num;
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700577 ac->ac_which = OCFS2_AC_USE_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800578 get_bh(osb->local_alloc_bh);
579 ac->ac_bh = osb->local_alloc_bh;
Mark Fashehccd979b2005-12-15 14:31:24 -0800580 status = 0;
581bail:
Sunil Mushranbda02332007-09-21 11:41:43 -0700582 if (status < 0 && local_alloc_inode) {
583 mutex_unlock(&local_alloc_inode->i_mutex);
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700584 iput(local_alloc_inode);
Sunil Mushranbda02332007-09-21 11:41:43 -0700585 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800586
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800587 mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
588 status);
589
Mark Fashehccd979b2005-12-15 14:31:24 -0800590 mlog_exit(status);
591 return status;
592}
593
594int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700595 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800596 struct ocfs2_alloc_context *ac,
Mark Fasheh415cb802007-09-16 20:10:16 -0700597 u32 bits_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -0800598 u32 *bit_off,
599 u32 *num_bits)
600{
601 int status, start;
602 struct inode *local_alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800603 void *bitmap;
604 struct ocfs2_dinode *alloc;
605 struct ocfs2_local_alloc *la;
606
607 mlog_entry_void();
608 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
609
Mark Fashehccd979b2005-12-15 14:31:24 -0800610 local_alloc_inode = ac->ac_inode;
611 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
612 la = OCFS2_LOCAL_ALLOC(alloc);
613
614 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
615 if (start == -1) {
616 /* TODO: Shouldn't we just BUG here? */
617 status = -ENOSPC;
618 mlog_errno(status);
619 goto bail;
620 }
621
622 bitmap = la->la_bitmap;
623 *bit_off = le32_to_cpu(la->la_bm_off) + start;
624 /* local alloc is always contiguous by nature -- we never
625 * delete bits from it! */
626 *num_bits = bits_wanted;
627
628 status = ocfs2_journal_access(handle, local_alloc_inode,
629 osb->local_alloc_bh,
630 OCFS2_JOURNAL_ACCESS_WRITE);
631 if (status < 0) {
632 mlog_errno(status);
633 goto bail;
634 }
635
636 while(bits_wanted--)
637 ocfs2_set_bit(start++, bitmap);
638
Marcin Slusarz0dd32562008-02-13 00:06:18 +0100639 le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800640
641 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
642 if (status < 0) {
643 mlog_errno(status);
644 goto bail;
645 }
646
647 status = 0;
648bail:
649 mlog_exit(status);
650 return status;
651}
652
653static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
654{
655 int i;
656 u8 *buffer;
657 u32 count = 0;
658 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
659
660 mlog_entry_void();
661
662 buffer = la->la_bitmap;
663 for (i = 0; i < le16_to_cpu(la->la_size); i++)
664 count += hweight8(buffer[i]);
665
666 mlog_exit(count);
667 return count;
668}
669
670static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
671 struct ocfs2_dinode *alloc,
672 u32 numbits)
673{
674 int numfound, bitoff, left, startoff, lastzero;
675 void *bitmap = NULL;
676
677 mlog_entry("(numbits wanted = %u)\n", numbits);
678
679 if (!alloc->id1.bitmap1.i_total) {
680 mlog(0, "No bits in my window!\n");
681 bitoff = -1;
682 goto bail;
683 }
684
685 bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
686
687 numfound = bitoff = startoff = 0;
688 lastzero = -1;
689 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
690 while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
691 if (bitoff == left) {
692 /* mlog(0, "bitoff (%d) == left", bitoff); */
693 break;
694 }
695 /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
696 "numfound = %d\n", bitoff, startoff, numfound);*/
697
698 /* Ok, we found a zero bit... is it contig. or do we
699 * start over?*/
700 if (bitoff == startoff) {
701 /* we found a zero */
702 numfound++;
703 startoff++;
704 } else {
705 /* got a zero after some ones */
706 numfound = 1;
707 startoff = bitoff+1;
708 }
709 /* we got everything we needed */
710 if (numfound == numbits) {
711 /* mlog(0, "Found it all!\n"); */
712 break;
713 }
714 }
715
716 mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
717 numfound);
718
719 if (numfound == numbits)
720 bitoff = startoff - numfound;
721 else
722 bitoff = -1;
723
724bail:
725 mlog_exit(bitoff);
726 return bitoff;
727}
728
729static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
730{
731 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
732 int i;
733 mlog_entry_void();
734
735 alloc->id1.bitmap1.i_total = 0;
736 alloc->id1.bitmap1.i_used = 0;
737 la->la_bm_off = 0;
738 for(i = 0; i < le16_to_cpu(la->la_size); i++)
739 la->la_bitmap[i] = 0;
740
741 mlog_exit_void();
742}
743
744#if 0
745/* turn this on and uncomment below to aid debugging window shifts. */
746static void ocfs2_verify_zero_bits(unsigned long *bitmap,
747 unsigned int start,
748 unsigned int count)
749{
750 unsigned int tmp = count;
751 while(tmp--) {
752 if (ocfs2_test_bit(start + tmp, bitmap)) {
753 printk("ocfs2_verify_zero_bits: start = %u, count = "
754 "%u\n", start, count);
755 printk("ocfs2_verify_zero_bits: bit %u is set!",
756 start + tmp);
757 BUG();
758 }
759 }
760}
761#endif
762
763/*
764 * sync the local alloc to main bitmap.
765 *
766 * assumes you've already locked the main bitmap -- the bitmap inode
767 * passed is used for caching.
768 */
769static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700770 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800771 struct ocfs2_dinode *alloc,
772 struct inode *main_bm_inode,
773 struct buffer_head *main_bm_bh)
774{
775 int status = 0;
776 int bit_off, left, count, start;
777 u64 la_start_blk;
778 u64 blkno;
779 void *bitmap;
780 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
781
Jan Kara5a58c3e2007-11-13 19:59:33 +0100782 mlog_entry("total = %u, used = %u\n",
Mark Fashehccd979b2005-12-15 14:31:24 -0800783 le32_to_cpu(alloc->id1.bitmap1.i_total),
Mark Fashehccd979b2005-12-15 14:31:24 -0800784 le32_to_cpu(alloc->id1.bitmap1.i_used));
785
786 if (!alloc->id1.bitmap1.i_total) {
787 mlog(0, "nothing to sync!\n");
788 goto bail;
789 }
790
791 if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
792 le32_to_cpu(alloc->id1.bitmap1.i_total)) {
793 mlog(0, "all bits were taken!\n");
794 goto bail;
795 }
796
797 la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
798 le32_to_cpu(la->la_bm_off));
799 bitmap = la->la_bitmap;
800 start = count = bit_off = 0;
801 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
802
803 while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
804 != -1) {
805 if ((bit_off < left) && (bit_off == start)) {
806 count++;
807 start++;
808 continue;
809 }
810 if (count) {
811 blkno = la_start_blk +
812 ocfs2_clusters_to_blocks(osb->sb,
813 start - count);
814
Mark Fashehb06970532006-03-03 10:24:33 -0800815 mlog(0, "freeing %u bits starting at local alloc bit "
816 "%u (la_start_blk = %llu, blkno = %llu)\n",
817 count, start - count,
818 (unsigned long long)la_start_blk,
819 (unsigned long long)blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800820
821 status = ocfs2_free_clusters(handle, main_bm_inode,
822 main_bm_bh, blkno, count);
823 if (status < 0) {
824 mlog_errno(status);
825 goto bail;
826 }
827 }
828 if (bit_off >= left)
829 break;
830 count = 1;
831 start = bit_off + 1;
832 }
833
834bail:
835 mlog_exit(status);
836 return status;
837}
838
Mark Fasheh9c7af402008-07-28 18:02:53 -0700839enum ocfs2_la_event {
840 OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */
841 OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has
842 * enough bits theoretically
843 * free, but a contiguous
844 * allocation could not be
845 * found. */
846 OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have
847 * enough bits free to satisfy
848 * our request. */
849};
850#define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
851/*
852 * Given an event, calculate the size of our next local alloc window.
853 *
854 * This should always be called under i_mutex of the local alloc inode
855 * so that local alloc disabling doesn't race with processes trying to
856 * use the allocator.
857 *
858 * Returns the state which the local alloc was left in. This value can
859 * be ignored by some paths.
860 */
861static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
862 enum ocfs2_la_event event)
863{
864 unsigned int bits;
865 int state;
866
867 spin_lock(&osb->osb_lock);
868 if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
869 WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
870 goto out_unlock;
871 }
872
873 /*
874 * ENOSPC and fragmentation are treated similarly for now.
875 */
876 if (event == OCFS2_LA_EVENT_ENOSPC ||
877 event == OCFS2_LA_EVENT_FRAGMENTED) {
878 /*
879 * We ran out of contiguous space in the primary
880 * bitmap. Drastically reduce the number of bits used
881 * by local alloc until we have to disable it.
882 */
883 bits = osb->local_alloc_bits >> 1;
884 if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
885 /*
886 * By setting state to THROTTLED, we'll keep
887 * the number of local alloc bits used down
888 * until an event occurs which would give us
889 * reason to assume the bitmap situation might
890 * have changed.
891 */
892 osb->local_alloc_state = OCFS2_LA_THROTTLED;
893 osb->local_alloc_bits = bits;
894 } else {
895 osb->local_alloc_state = OCFS2_LA_DISABLED;
896 }
897 queue_delayed_work(ocfs2_wq, &osb->la_enable_wq,
898 OCFS2_LA_ENABLE_INTERVAL);
899 goto out_unlock;
900 }
901
902 /*
903 * Don't increase the size of the local alloc window until we
904 * know we might be able to fulfill the request. Otherwise, we
905 * risk bouncing around the global bitmap during periods of
906 * low space.
907 */
908 if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
909 osb->local_alloc_bits = osb->local_alloc_default_bits;
910
911out_unlock:
912 state = osb->local_alloc_state;
913 spin_unlock(&osb->osb_lock);
914
915 return state;
916}
917
Mark Fashehccd979b2005-12-15 14:31:24 -0800918static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800919 struct ocfs2_alloc_context **ac,
920 struct inode **bitmap_inode,
921 struct buffer_head **bitmap_bh)
922{
923 int status;
924
Robert P. J. Daycd861282006-12-13 00:34:52 -0800925 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800926 if (!(*ac)) {
927 status = -ENOMEM;
928 mlog_errno(status);
929 goto bail;
930 }
931
Mark Fasheh9c7af402008-07-28 18:02:53 -0700932retry_enospc:
Mark Fashehebcee4b2008-07-28 14:55:20 -0700933 (*ac)->ac_bits_wanted = osb->local_alloc_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800934
935 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
Mark Fasheh9c7af402008-07-28 18:02:53 -0700936 if (status == -ENOSPC) {
937 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
938 OCFS2_LA_DISABLED)
939 goto bail;
940
941 ocfs2_free_ac_resource(*ac);
942 memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
943 goto retry_enospc;
944 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800945 if (status < 0) {
Mark Fasheh9c7af402008-07-28 18:02:53 -0700946 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800947 goto bail;
948 }
949
950 *bitmap_inode = (*ac)->ac_inode;
951 igrab(*bitmap_inode);
952 *bitmap_bh = (*ac)->ac_bh;
953 get_bh(*bitmap_bh);
954 status = 0;
955bail:
956 if ((status < 0) && *ac) {
957 ocfs2_free_alloc_context(*ac);
958 *ac = NULL;
959 }
960
961 mlog_exit(status);
962 return status;
963}
964
965/*
966 * pass it the bitmap lock in lock_bh if you have it.
967 */
968static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700969 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800970 struct ocfs2_alloc_context *ac)
971{
972 int status = 0;
973 u32 cluster_off, cluster_count;
974 struct ocfs2_dinode *alloc = NULL;
975 struct ocfs2_local_alloc *la;
976
977 mlog_entry_void();
978
979 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
980 la = OCFS2_LOCAL_ALLOC(alloc);
981
982 if (alloc->id1.bitmap1.i_total)
983 mlog(0, "asking me to alloc a new window over a non-empty "
984 "one\n");
985
986 mlog(0, "Allocating %u clusters for a new window.\n",
Mark Fashehebcee4b2008-07-28 14:55:20 -0700987 osb->local_alloc_bits);
Mark Fasheh883d4ca2006-06-05 16:41:00 -0400988
989 /* Instruct the allocation code to try the most recently used
990 * cluster group. We'll re-record the group used this pass
991 * below. */
992 ac->ac_last_group = osb->la_last_gd;
993
Mark Fashehccd979b2005-12-15 14:31:24 -0800994 /* we used the generic suballoc reserve function, but we set
995 * everything up nicely, so there's no reason why we can't use
996 * the more specific cluster api to claim bits. */
Mark Fashehebcee4b2008-07-28 14:55:20 -0700997 status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -0800998 &cluster_off, &cluster_count);
Mark Fasheh9c7af402008-07-28 18:02:53 -0700999 if (status == -ENOSPC) {
1000retry_enospc:
1001 /*
1002 * Note: We could also try syncing the journal here to
1003 * allow use of any free bits which the current
1004 * transaction can't give us access to. --Mark
1005 */
1006 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
1007 OCFS2_LA_DISABLED)
1008 goto bail;
1009
1010 status = ocfs2_claim_clusters(osb, handle, ac,
1011 osb->local_alloc_bits,
1012 &cluster_off,
1013 &cluster_count);
1014 if (status == -ENOSPC)
1015 goto retry_enospc;
1016 /*
1017 * We only shrunk the *minimum* number of in our
1018 * request - it's entirely possible that the allocator
1019 * might give us more than we asked for.
1020 */
1021 if (status == 0) {
1022 spin_lock(&osb->osb_lock);
1023 osb->local_alloc_bits = cluster_count;
1024 spin_unlock(&osb->osb_lock);
1025 }
1026 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001027 if (status < 0) {
1028 if (status != -ENOSPC)
1029 mlog_errno(status);
1030 goto bail;
1031 }
1032
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001033 osb->la_last_gd = ac->ac_last_group;
1034
Mark Fashehccd979b2005-12-15 14:31:24 -08001035 la->la_bm_off = cpu_to_le32(cluster_off);
1036 alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
1037 /* just in case... In the future when we find space ourselves,
1038 * we don't have to get all contiguous -- but we'll have to
1039 * set all previously used bits in bitmap and update
1040 * la_bits_set before setting the bits in the main bitmap. */
1041 alloc->id1.bitmap1.i_used = 0;
1042 memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
1043 le16_to_cpu(la->la_size));
1044
1045 mlog(0, "New window allocated:\n");
1046 mlog(0, "window la_bm_off = %u\n",
1047 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
1048 mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
1049
1050bail:
1051 mlog_exit(status);
1052 return status;
1053}
1054
1055/* Note that we do *NOT* lock the local alloc inode here as
1056 * it's been locked already for us. */
1057static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
1058 struct inode *local_alloc_inode)
1059{
1060 int status = 0;
1061 struct buffer_head *main_bm_bh = NULL;
1062 struct inode *main_bm_inode = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001063 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001064 struct ocfs2_dinode *alloc;
1065 struct ocfs2_dinode *alloc_copy = NULL;
1066 struct ocfs2_alloc_context *ac = NULL;
1067
1068 mlog_entry_void();
1069
Mark Fasheh9c7af402008-07-28 18:02:53 -07001070 ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
1071
Mark Fashehccd979b2005-12-15 14:31:24 -08001072 /* This will lock the main bitmap for us. */
1073 status = ocfs2_local_alloc_reserve_for_window(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001074 &ac,
1075 &main_bm_inode,
1076 &main_bm_bh);
1077 if (status < 0) {
1078 if (status != -ENOSPC)
1079 mlog_errno(status);
1080 goto bail;
1081 }
1082
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001083 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001084 if (IS_ERR(handle)) {
1085 status = PTR_ERR(handle);
1086 handle = NULL;
1087 mlog_errno(status);
1088 goto bail;
1089 }
1090
1091 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1092
1093 /* We want to clear the local alloc before doing anything
1094 * else, so that if we error later during this operation,
1095 * local alloc shutdown won't try to double free main bitmap
1096 * bits. Make a copy so the sync function knows which bits to
1097 * free. */
Sunil Mushran4ba1c5b2008-04-18 15:03:59 -07001098 alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001099 if (!alloc_copy) {
1100 status = -ENOMEM;
1101 mlog_errno(status);
1102 goto bail;
1103 }
1104 memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
1105
1106 status = ocfs2_journal_access(handle, local_alloc_inode,
1107 osb->local_alloc_bh,
1108 OCFS2_JOURNAL_ACCESS_WRITE);
1109 if (status < 0) {
1110 mlog_errno(status);
1111 goto bail;
1112 }
1113
1114 ocfs2_clear_local_alloc(alloc);
1115
1116 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
1117 if (status < 0) {
1118 mlog_errno(status);
1119 goto bail;
1120 }
1121
1122 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
1123 main_bm_inode, main_bm_bh);
1124 if (status < 0) {
1125 mlog_errno(status);
1126 goto bail;
1127 }
1128
1129 status = ocfs2_local_alloc_new_window(osb, handle, ac);
1130 if (status < 0) {
1131 if (status != -ENOSPC)
1132 mlog_errno(status);
1133 goto bail;
1134 }
1135
1136 atomic_inc(&osb->alloc_stats.moves);
1137
1138 status = 0;
1139bail:
1140 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001141 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001142
1143 if (main_bm_bh)
1144 brelse(main_bm_bh);
1145
1146 if (main_bm_inode)
1147 iput(main_bm_inode);
1148
1149 if (alloc_copy)
1150 kfree(alloc_copy);
1151
1152 if (ac)
1153 ocfs2_free_alloc_context(ac);
1154
1155 mlog_exit(status);
1156 return status;
1157}
1158