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