x86: add PAGE_KERNEL_EXEC_NOCACHE
[wrt350n-kernel.git] / fs / ocfs2 / localalloc.c
blobadd1ffdc5c6c75bb2accdb65972466f2651e80c8
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * localalloc.c
6 * Node local data allocation
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
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.
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.
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.
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>
32 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33 #include <cluster/masklog.h>
35 #include "ocfs2.h"
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"
46 #include "buffer_head_io.h"
48 #define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab))
50 static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb);
52 static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc);
54 static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
55 struct ocfs2_dinode *alloc,
56 u32 numbits);
58 static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc);
60 static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
61 handle_t *handle,
62 struct ocfs2_dinode *alloc,
63 struct inode *main_bm_inode,
64 struct buffer_head *main_bm_bh);
66 static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
67 struct ocfs2_alloc_context **ac,
68 struct inode **bitmap_inode,
69 struct buffer_head **bitmap_bh);
71 static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
72 handle_t *handle,
73 struct ocfs2_alloc_context *ac);
75 static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
76 struct inode *local_alloc_inode);
78 static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb)
80 BUG_ON(osb->s_clustersize_bits > 20);
82 /* Size local alloc windows by the megabyte */
83 return osb->local_alloc_size << (20 - osb->s_clustersize_bits);
87 * Tell us whether a given allocation should use the local alloc
88 * file. Otherwise, it has to go to the main bitmap.
90 int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
92 int la_bits = ocfs2_local_alloc_window_bits(osb);
93 int ret = 0;
95 if (osb->local_alloc_state != OCFS2_LA_ENABLED)
96 goto bail;
98 /* la_bits should be at least twice the size (in clusters) of
99 * a new block group. We want to be sure block group
100 * allocations go through the local alloc, so allow an
101 * allocation to take up to half the bitmap. */
102 if (bits > (la_bits / 2))
103 goto bail;
105 ret = 1;
106 bail:
107 mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n",
108 osb->local_alloc_state, (unsigned long long)bits, la_bits, ret);
109 return ret;
112 int ocfs2_load_local_alloc(struct ocfs2_super *osb)
114 int status = 0;
115 struct ocfs2_dinode *alloc = NULL;
116 struct buffer_head *alloc_bh = NULL;
117 u32 num_used;
118 struct inode *inode = NULL;
119 struct ocfs2_local_alloc *la;
121 mlog_entry_void();
123 if (ocfs2_mount_local(osb))
124 goto bail;
126 if (osb->local_alloc_size == 0)
127 goto bail;
129 if (ocfs2_local_alloc_window_bits(osb) >= osb->bitmap_cpg) {
130 mlog(ML_NOTICE, "Requested local alloc window %d is larger "
131 "than max possible %u. Using defaults.\n",
132 ocfs2_local_alloc_window_bits(osb), (osb->bitmap_cpg - 1));
133 osb->local_alloc_size = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
136 /* read the alloc off disk */
137 inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
138 osb->slot_num);
139 if (!inode) {
140 status = -EINVAL;
141 mlog_errno(status);
142 goto bail;
145 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
146 &alloc_bh, 0, inode);
147 if (status < 0) {
148 mlog_errno(status);
149 goto bail;
152 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
153 la = OCFS2_LOCAL_ALLOC(alloc);
155 if (!(le32_to_cpu(alloc->i_flags) &
156 (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
157 mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
158 (unsigned long long)OCFS2_I(inode)->ip_blkno);
159 status = -EINVAL;
160 goto bail;
163 if ((la->la_size == 0) ||
164 (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
165 mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
166 le16_to_cpu(la->la_size));
167 status = -EINVAL;
168 goto bail;
171 /* do a little verification. */
172 num_used = ocfs2_local_alloc_count_bits(alloc);
174 /* hopefully the local alloc has always been recovered before
175 * we load it. */
176 if (num_used
177 || alloc->id1.bitmap1.i_used
178 || alloc->id1.bitmap1.i_total
179 || la->la_bm_off)
180 mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
181 "found = %u, set = %u, taken = %u, off = %u\n",
182 num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
183 le32_to_cpu(alloc->id1.bitmap1.i_total),
184 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
186 osb->local_alloc_bh = alloc_bh;
187 osb->local_alloc_state = OCFS2_LA_ENABLED;
189 bail:
190 if (status < 0)
191 if (alloc_bh)
192 brelse(alloc_bh);
193 if (inode)
194 iput(inode);
196 mlog(0, "Local alloc window bits = %d\n",
197 ocfs2_local_alloc_window_bits(osb));
199 mlog_exit(status);
200 return status;
204 * return any unused bits to the bitmap and write out a clean
205 * local_alloc.
207 * local_alloc_bh is optional. If not passed, we will simply use the
208 * one off osb. If you do pass it however, be warned that it *will* be
209 * returned brelse'd and NULL'd out.*/
210 void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
212 int status;
213 handle_t *handle;
214 struct inode *local_alloc_inode = NULL;
215 struct buffer_head *bh = NULL;
216 struct buffer_head *main_bm_bh = NULL;
217 struct inode *main_bm_inode = NULL;
218 struct ocfs2_dinode *alloc_copy = NULL;
219 struct ocfs2_dinode *alloc = NULL;
221 mlog_entry_void();
223 if (osb->local_alloc_state == OCFS2_LA_UNUSED)
224 goto out;
226 local_alloc_inode =
227 ocfs2_get_system_file_inode(osb,
228 LOCAL_ALLOC_SYSTEM_INODE,
229 osb->slot_num);
230 if (!local_alloc_inode) {
231 status = -ENOENT;
232 mlog_errno(status);
233 goto out;
236 osb->local_alloc_state = OCFS2_LA_DISABLED;
238 main_bm_inode = ocfs2_get_system_file_inode(osb,
239 GLOBAL_BITMAP_SYSTEM_INODE,
240 OCFS2_INVALID_SLOT);
241 if (!main_bm_inode) {
242 status = -EINVAL;
243 mlog_errno(status);
244 goto out;
247 mutex_lock(&main_bm_inode->i_mutex);
249 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
250 if (status < 0) {
251 mlog_errno(status);
252 goto out_mutex;
255 /* WINDOW_MOVE_CREDITS is a bit heavy... */
256 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
257 if (IS_ERR(handle)) {
258 mlog_errno(PTR_ERR(handle));
259 handle = NULL;
260 goto out_unlock;
263 bh = osb->local_alloc_bh;
264 alloc = (struct ocfs2_dinode *) bh->b_data;
266 alloc_copy = kmalloc(bh->b_size, GFP_KERNEL);
267 if (!alloc_copy) {
268 status = -ENOMEM;
269 goto out_commit;
271 memcpy(alloc_copy, alloc, bh->b_size);
273 status = ocfs2_journal_access(handle, local_alloc_inode, bh,
274 OCFS2_JOURNAL_ACCESS_WRITE);
275 if (status < 0) {
276 mlog_errno(status);
277 goto out_commit;
280 ocfs2_clear_local_alloc(alloc);
282 status = ocfs2_journal_dirty(handle, bh);
283 if (status < 0) {
284 mlog_errno(status);
285 goto out_commit;
288 brelse(bh);
289 osb->local_alloc_bh = NULL;
290 osb->local_alloc_state = OCFS2_LA_UNUSED;
292 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
293 main_bm_inode, main_bm_bh);
294 if (status < 0)
295 mlog_errno(status);
297 out_commit:
298 ocfs2_commit_trans(osb, handle);
300 out_unlock:
301 if (main_bm_bh)
302 brelse(main_bm_bh);
304 ocfs2_inode_unlock(main_bm_inode, 1);
306 out_mutex:
307 mutex_unlock(&main_bm_inode->i_mutex);
308 iput(main_bm_inode);
310 out:
311 if (local_alloc_inode)
312 iput(local_alloc_inode);
314 if (alloc_copy)
315 kfree(alloc_copy);
317 mlog_exit_void();
321 * We want to free the bitmap bits outside of any recovery context as
322 * we'll need a cluster lock to do so, but we must clear the local
323 * alloc before giving up the recovered nodes journal. To solve this,
324 * we kmalloc a copy of the local alloc before it's change for the
325 * caller to process with ocfs2_complete_local_alloc_recovery
327 int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
328 int slot_num,
329 struct ocfs2_dinode **alloc_copy)
331 int status = 0;
332 struct buffer_head *alloc_bh = NULL;
333 struct inode *inode = NULL;
334 struct ocfs2_dinode *alloc;
336 mlog_entry("(slot_num = %d)\n", slot_num);
338 *alloc_copy = NULL;
340 inode = ocfs2_get_system_file_inode(osb,
341 LOCAL_ALLOC_SYSTEM_INODE,
342 slot_num);
343 if (!inode) {
344 status = -EINVAL;
345 mlog_errno(status);
346 goto bail;
349 mutex_lock(&inode->i_mutex);
351 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
352 &alloc_bh, 0, inode);
353 if (status < 0) {
354 mlog_errno(status);
355 goto bail;
358 *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
359 if (!(*alloc_copy)) {
360 status = -ENOMEM;
361 goto bail;
363 memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
365 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
366 ocfs2_clear_local_alloc(alloc);
368 status = ocfs2_write_block(osb, alloc_bh, inode);
369 if (status < 0)
370 mlog_errno(status);
372 bail:
373 if ((status < 0) && (*alloc_copy)) {
374 kfree(*alloc_copy);
375 *alloc_copy = NULL;
378 if (alloc_bh)
379 brelse(alloc_bh);
381 if (inode) {
382 mutex_unlock(&inode->i_mutex);
383 iput(inode);
386 mlog_exit(status);
387 return status;
391 * Step 2: By now, we've completed the journal recovery, we've stamped
392 * a clean local alloc on disk and dropped the node out of the
393 * recovery map. Dlm locks will no longer stall, so lets clear out the
394 * main bitmap.
396 int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
397 struct ocfs2_dinode *alloc)
399 int status;
400 handle_t *handle;
401 struct buffer_head *main_bm_bh = NULL;
402 struct inode *main_bm_inode;
404 mlog_entry_void();
406 main_bm_inode = ocfs2_get_system_file_inode(osb,
407 GLOBAL_BITMAP_SYSTEM_INODE,
408 OCFS2_INVALID_SLOT);
409 if (!main_bm_inode) {
410 status = -EINVAL;
411 mlog_errno(status);
412 goto out;
415 mutex_lock(&main_bm_inode->i_mutex);
417 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
418 if (status < 0) {
419 mlog_errno(status);
420 goto out_mutex;
423 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
424 if (IS_ERR(handle)) {
425 status = PTR_ERR(handle);
426 handle = NULL;
427 mlog_errno(status);
428 goto out_unlock;
431 /* we want the bitmap change to be recorded on disk asap */
432 handle->h_sync = 1;
434 status = ocfs2_sync_local_to_main(osb, handle, alloc,
435 main_bm_inode, main_bm_bh);
436 if (status < 0)
437 mlog_errno(status);
439 ocfs2_commit_trans(osb, handle);
441 out_unlock:
442 ocfs2_inode_unlock(main_bm_inode, 1);
444 out_mutex:
445 mutex_unlock(&main_bm_inode->i_mutex);
447 if (main_bm_bh)
448 brelse(main_bm_bh);
450 iput(main_bm_inode);
452 out:
453 mlog_exit(status);
454 return status;
458 * make sure we've got at least bitswanted contiguous bits in the
459 * local alloc. You lose them when you drop i_mutex.
461 * We will add ourselves to the transaction passed in, but may start
462 * our own in order to shift windows.
464 int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
465 u32 bits_wanted,
466 struct ocfs2_alloc_context *ac)
468 int status;
469 struct ocfs2_dinode *alloc;
470 struct inode *local_alloc_inode;
471 unsigned int free_bits;
473 mlog_entry_void();
475 BUG_ON(!ac);
477 local_alloc_inode =
478 ocfs2_get_system_file_inode(osb,
479 LOCAL_ALLOC_SYSTEM_INODE,
480 osb->slot_num);
481 if (!local_alloc_inode) {
482 status = -ENOENT;
483 mlog_errno(status);
484 goto bail;
487 mutex_lock(&local_alloc_inode->i_mutex);
489 if (osb->local_alloc_state != OCFS2_LA_ENABLED) {
490 status = -ENOSPC;
491 goto bail;
494 if (bits_wanted > ocfs2_local_alloc_window_bits(osb)) {
495 mlog(0, "Asking for more than my max window size!\n");
496 status = -ENOSPC;
497 goto bail;
500 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
502 #ifdef OCFS2_DEBUG_FS
503 if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
504 ocfs2_local_alloc_count_bits(alloc)) {
505 ocfs2_error(osb->sb, "local alloc inode %llu says it has "
506 "%u free bits, but a count shows %u",
507 (unsigned long long)le64_to_cpu(alloc->i_blkno),
508 le32_to_cpu(alloc->id1.bitmap1.i_used),
509 ocfs2_local_alloc_count_bits(alloc));
510 status = -EIO;
511 goto bail;
513 #endif
515 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
516 le32_to_cpu(alloc->id1.bitmap1.i_used);
517 if (bits_wanted > free_bits) {
518 /* uhoh, window change time. */
519 status =
520 ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
521 if (status < 0) {
522 if (status != -ENOSPC)
523 mlog_errno(status);
524 goto bail;
528 ac->ac_inode = local_alloc_inode;
529 ac->ac_which = OCFS2_AC_USE_LOCAL;
530 get_bh(osb->local_alloc_bh);
531 ac->ac_bh = osb->local_alloc_bh;
532 status = 0;
533 bail:
534 if (status < 0 && local_alloc_inode) {
535 mutex_unlock(&local_alloc_inode->i_mutex);
536 iput(local_alloc_inode);
539 mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
540 status);
542 mlog_exit(status);
543 return status;
546 int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
547 handle_t *handle,
548 struct ocfs2_alloc_context *ac,
549 u32 bits_wanted,
550 u32 *bit_off,
551 u32 *num_bits)
553 int status, start;
554 struct inode *local_alloc_inode;
555 void *bitmap;
556 struct ocfs2_dinode *alloc;
557 struct ocfs2_local_alloc *la;
559 mlog_entry_void();
560 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
562 local_alloc_inode = ac->ac_inode;
563 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
564 la = OCFS2_LOCAL_ALLOC(alloc);
566 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
567 if (start == -1) {
568 /* TODO: Shouldn't we just BUG here? */
569 status = -ENOSPC;
570 mlog_errno(status);
571 goto bail;
574 bitmap = la->la_bitmap;
575 *bit_off = le32_to_cpu(la->la_bm_off) + start;
576 /* local alloc is always contiguous by nature -- we never
577 * delete bits from it! */
578 *num_bits = bits_wanted;
580 status = ocfs2_journal_access(handle, local_alloc_inode,
581 osb->local_alloc_bh,
582 OCFS2_JOURNAL_ACCESS_WRITE);
583 if (status < 0) {
584 mlog_errno(status);
585 goto bail;
588 while(bits_wanted--)
589 ocfs2_set_bit(start++, bitmap);
591 alloc->id1.bitmap1.i_used = cpu_to_le32(*num_bits +
592 le32_to_cpu(alloc->id1.bitmap1.i_used));
594 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
595 if (status < 0) {
596 mlog_errno(status);
597 goto bail;
600 status = 0;
601 bail:
602 mlog_exit(status);
603 return status;
606 static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
608 int i;
609 u8 *buffer;
610 u32 count = 0;
611 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
613 mlog_entry_void();
615 buffer = la->la_bitmap;
616 for (i = 0; i < le16_to_cpu(la->la_size); i++)
617 count += hweight8(buffer[i]);
619 mlog_exit(count);
620 return count;
623 static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
624 struct ocfs2_dinode *alloc,
625 u32 numbits)
627 int numfound, bitoff, left, startoff, lastzero;
628 void *bitmap = NULL;
630 mlog_entry("(numbits wanted = %u)\n", numbits);
632 if (!alloc->id1.bitmap1.i_total) {
633 mlog(0, "No bits in my window!\n");
634 bitoff = -1;
635 goto bail;
638 bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
640 numfound = bitoff = startoff = 0;
641 lastzero = -1;
642 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
643 while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
644 if (bitoff == left) {
645 /* mlog(0, "bitoff (%d) == left", bitoff); */
646 break;
648 /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
649 "numfound = %d\n", bitoff, startoff, numfound);*/
651 /* Ok, we found a zero bit... is it contig. or do we
652 * start over?*/
653 if (bitoff == startoff) {
654 /* we found a zero */
655 numfound++;
656 startoff++;
657 } else {
658 /* got a zero after some ones */
659 numfound = 1;
660 startoff = bitoff+1;
662 /* we got everything we needed */
663 if (numfound == numbits) {
664 /* mlog(0, "Found it all!\n"); */
665 break;
669 mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
670 numfound);
672 if (numfound == numbits)
673 bitoff = startoff - numfound;
674 else
675 bitoff = -1;
677 bail:
678 mlog_exit(bitoff);
679 return bitoff;
682 static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
684 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
685 int i;
686 mlog_entry_void();
688 alloc->id1.bitmap1.i_total = 0;
689 alloc->id1.bitmap1.i_used = 0;
690 la->la_bm_off = 0;
691 for(i = 0; i < le16_to_cpu(la->la_size); i++)
692 la->la_bitmap[i] = 0;
694 mlog_exit_void();
697 #if 0
698 /* turn this on and uncomment below to aid debugging window shifts. */
699 static void ocfs2_verify_zero_bits(unsigned long *bitmap,
700 unsigned int start,
701 unsigned int count)
703 unsigned int tmp = count;
704 while(tmp--) {
705 if (ocfs2_test_bit(start + tmp, bitmap)) {
706 printk("ocfs2_verify_zero_bits: start = %u, count = "
707 "%u\n", start, count);
708 printk("ocfs2_verify_zero_bits: bit %u is set!",
709 start + tmp);
710 BUG();
714 #endif
717 * sync the local alloc to main bitmap.
719 * assumes you've already locked the main bitmap -- the bitmap inode
720 * passed is used for caching.
722 static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
723 handle_t *handle,
724 struct ocfs2_dinode *alloc,
725 struct inode *main_bm_inode,
726 struct buffer_head *main_bm_bh)
728 int status = 0;
729 int bit_off, left, count, start;
730 u64 la_start_blk;
731 u64 blkno;
732 void *bitmap;
733 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
735 mlog_entry("total = %u, used = %u\n",
736 le32_to_cpu(alloc->id1.bitmap1.i_total),
737 le32_to_cpu(alloc->id1.bitmap1.i_used));
739 if (!alloc->id1.bitmap1.i_total) {
740 mlog(0, "nothing to sync!\n");
741 goto bail;
744 if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
745 le32_to_cpu(alloc->id1.bitmap1.i_total)) {
746 mlog(0, "all bits were taken!\n");
747 goto bail;
750 la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
751 le32_to_cpu(la->la_bm_off));
752 bitmap = la->la_bitmap;
753 start = count = bit_off = 0;
754 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
756 while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
757 != -1) {
758 if ((bit_off < left) && (bit_off == start)) {
759 count++;
760 start++;
761 continue;
763 if (count) {
764 blkno = la_start_blk +
765 ocfs2_clusters_to_blocks(osb->sb,
766 start - count);
768 mlog(0, "freeing %u bits starting at local alloc bit "
769 "%u (la_start_blk = %llu, blkno = %llu)\n",
770 count, start - count,
771 (unsigned long long)la_start_blk,
772 (unsigned long long)blkno);
774 status = ocfs2_free_clusters(handle, main_bm_inode,
775 main_bm_bh, blkno, count);
776 if (status < 0) {
777 mlog_errno(status);
778 goto bail;
781 if (bit_off >= left)
782 break;
783 count = 1;
784 start = bit_off + 1;
787 bail:
788 mlog_exit(status);
789 return status;
792 static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
793 struct ocfs2_alloc_context **ac,
794 struct inode **bitmap_inode,
795 struct buffer_head **bitmap_bh)
797 int status;
799 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
800 if (!(*ac)) {
801 status = -ENOMEM;
802 mlog_errno(status);
803 goto bail;
806 (*ac)->ac_bits_wanted = ocfs2_local_alloc_window_bits(osb);
808 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
809 if (status < 0) {
810 if (status != -ENOSPC)
811 mlog_errno(status);
812 goto bail;
815 *bitmap_inode = (*ac)->ac_inode;
816 igrab(*bitmap_inode);
817 *bitmap_bh = (*ac)->ac_bh;
818 get_bh(*bitmap_bh);
819 status = 0;
820 bail:
821 if ((status < 0) && *ac) {
822 ocfs2_free_alloc_context(*ac);
823 *ac = NULL;
826 mlog_exit(status);
827 return status;
831 * pass it the bitmap lock in lock_bh if you have it.
833 static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
834 handle_t *handle,
835 struct ocfs2_alloc_context *ac)
837 int status = 0;
838 u32 cluster_off, cluster_count;
839 struct ocfs2_dinode *alloc = NULL;
840 struct ocfs2_local_alloc *la;
842 mlog_entry_void();
844 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
845 la = OCFS2_LOCAL_ALLOC(alloc);
847 if (alloc->id1.bitmap1.i_total)
848 mlog(0, "asking me to alloc a new window over a non-empty "
849 "one\n");
851 mlog(0, "Allocating %u clusters for a new window.\n",
852 ocfs2_local_alloc_window_bits(osb));
854 /* Instruct the allocation code to try the most recently used
855 * cluster group. We'll re-record the group used this pass
856 * below. */
857 ac->ac_last_group = osb->la_last_gd;
859 /* we used the generic suballoc reserve function, but we set
860 * everything up nicely, so there's no reason why we can't use
861 * the more specific cluster api to claim bits. */
862 status = ocfs2_claim_clusters(osb, handle, ac,
863 ocfs2_local_alloc_window_bits(osb),
864 &cluster_off, &cluster_count);
865 if (status < 0) {
866 if (status != -ENOSPC)
867 mlog_errno(status);
868 goto bail;
871 osb->la_last_gd = ac->ac_last_group;
873 la->la_bm_off = cpu_to_le32(cluster_off);
874 alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
875 /* just in case... In the future when we find space ourselves,
876 * we don't have to get all contiguous -- but we'll have to
877 * set all previously used bits in bitmap and update
878 * la_bits_set before setting the bits in the main bitmap. */
879 alloc->id1.bitmap1.i_used = 0;
880 memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
881 le16_to_cpu(la->la_size));
883 mlog(0, "New window allocated:\n");
884 mlog(0, "window la_bm_off = %u\n",
885 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
886 mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
888 bail:
889 mlog_exit(status);
890 return status;
893 /* Note that we do *NOT* lock the local alloc inode here as
894 * it's been locked already for us. */
895 static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
896 struct inode *local_alloc_inode)
898 int status = 0;
899 struct buffer_head *main_bm_bh = NULL;
900 struct inode *main_bm_inode = NULL;
901 handle_t *handle = NULL;
902 struct ocfs2_dinode *alloc;
903 struct ocfs2_dinode *alloc_copy = NULL;
904 struct ocfs2_alloc_context *ac = NULL;
906 mlog_entry_void();
908 /* This will lock the main bitmap for us. */
909 status = ocfs2_local_alloc_reserve_for_window(osb,
910 &ac,
911 &main_bm_inode,
912 &main_bm_bh);
913 if (status < 0) {
914 if (status != -ENOSPC)
915 mlog_errno(status);
916 goto bail;
919 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
920 if (IS_ERR(handle)) {
921 status = PTR_ERR(handle);
922 handle = NULL;
923 mlog_errno(status);
924 goto bail;
927 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
929 /* We want to clear the local alloc before doing anything
930 * else, so that if we error later during this operation,
931 * local alloc shutdown won't try to double free main bitmap
932 * bits. Make a copy so the sync function knows which bits to
933 * free. */
934 alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_KERNEL);
935 if (!alloc_copy) {
936 status = -ENOMEM;
937 mlog_errno(status);
938 goto bail;
940 memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
942 status = ocfs2_journal_access(handle, local_alloc_inode,
943 osb->local_alloc_bh,
944 OCFS2_JOURNAL_ACCESS_WRITE);
945 if (status < 0) {
946 mlog_errno(status);
947 goto bail;
950 ocfs2_clear_local_alloc(alloc);
952 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
953 if (status < 0) {
954 mlog_errno(status);
955 goto bail;
958 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
959 main_bm_inode, main_bm_bh);
960 if (status < 0) {
961 mlog_errno(status);
962 goto bail;
965 status = ocfs2_local_alloc_new_window(osb, handle, ac);
966 if (status < 0) {
967 if (status != -ENOSPC)
968 mlog_errno(status);
969 goto bail;
972 atomic_inc(&osb->alloc_stats.moves);
974 status = 0;
975 bail:
976 if (handle)
977 ocfs2_commit_trans(osb, handle);
979 if (main_bm_bh)
980 brelse(main_bm_bh);
982 if (main_bm_inode)
983 iput(main_bm_inode);
985 if (alloc_copy)
986 kfree(alloc_copy);
988 if (ac)
989 ocfs2_free_alloc_context(ac);
991 mlog_exit(status);
992 return status;