1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
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.
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>
41 #include "localalloc.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,
58 static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc);
60 static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
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,
73 struct ocfs2_alloc_context *ac);
75 static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
76 struct inode *local_alloc_inode);
79 * Determine how large our local alloc window should be, in bits.
81 * These values (and the behavior in ocfs2_alloc_should_use_local) have
82 * been chosen so that most allocations, including new block groups go
83 * through local alloc.
85 static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb)
87 BUG_ON(osb->s_clustersize_bits < 12);
89 return 2048 >> (osb->s_clustersize_bits - 12);
93 * Tell us whether a given allocation should use the local alloc
94 * file. Otherwise, it has to go to the main bitmap.
96 int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
98 int la_bits = ocfs2_local_alloc_window_bits(osb);
100 if (osb->local_alloc_state != OCFS2_LA_ENABLED)
103 /* la_bits should be at least twice the size (in clusters) of
104 * a new block group. We want to be sure block group
105 * allocations go through the local alloc, so allow an
106 * allocation to take up to half the bitmap. */
107 if (bits > (la_bits / 2))
113 int ocfs2_load_local_alloc(struct ocfs2_super *osb)
116 struct ocfs2_dinode *alloc = NULL;
117 struct buffer_head *alloc_bh = NULL;
119 struct inode *inode = NULL;
120 struct ocfs2_local_alloc *la;
124 /* read the alloc off disk */
125 inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
133 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
134 &alloc_bh, 0, inode);
140 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
141 la = OCFS2_LOCAL_ALLOC(alloc);
143 if (!(le32_to_cpu(alloc->i_flags) &
144 (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
145 mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
146 (unsigned long long)OCFS2_I(inode)->ip_blkno);
151 if ((la->la_size == 0) ||
152 (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
153 mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
154 le16_to_cpu(la->la_size));
159 /* do a little verification. */
160 num_used = ocfs2_local_alloc_count_bits(alloc);
162 /* hopefully the local alloc has always been recovered before
165 || alloc->id1.bitmap1.i_used
166 || alloc->id1.bitmap1.i_total
168 mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
169 "found = %u, set = %u, taken = %u, off = %u\n",
170 num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
171 le32_to_cpu(alloc->id1.bitmap1.i_total),
172 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
174 osb->local_alloc_bh = alloc_bh;
175 osb->local_alloc_state = OCFS2_LA_ENABLED;
189 * return any unused bits to the bitmap and write out a clean
192 * local_alloc_bh is optional. If not passed, we will simply use the
193 * one off osb. If you do pass it however, be warned that it *will* be
194 * returned brelse'd and NULL'd out.*/
195 void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
199 struct inode *local_alloc_inode = NULL;
200 struct buffer_head *bh = NULL;
201 struct buffer_head *main_bm_bh = NULL;
202 struct inode *main_bm_inode = NULL;
203 struct ocfs2_dinode *alloc_copy = NULL;
204 struct ocfs2_dinode *alloc = NULL;
208 if (osb->local_alloc_state == OCFS2_LA_UNUSED)
212 ocfs2_get_system_file_inode(osb,
213 LOCAL_ALLOC_SYSTEM_INODE,
215 if (!local_alloc_inode) {
221 osb->local_alloc_state = OCFS2_LA_DISABLED;
223 main_bm_inode = ocfs2_get_system_file_inode(osb,
224 GLOBAL_BITMAP_SYSTEM_INODE,
226 if (!main_bm_inode) {
232 mutex_lock(&main_bm_inode->i_mutex);
234 status = ocfs2_meta_lock(main_bm_inode, &main_bm_bh, 1);
240 /* WINDOW_MOVE_CREDITS is a bit heavy... */
241 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
242 if (IS_ERR(handle)) {
243 mlog_errno(PTR_ERR(handle));
248 bh = osb->local_alloc_bh;
249 alloc = (struct ocfs2_dinode *) bh->b_data;
251 alloc_copy = kmalloc(bh->b_size, GFP_KERNEL);
256 memcpy(alloc_copy, alloc, bh->b_size);
258 status = ocfs2_journal_access(handle, local_alloc_inode, bh,
259 OCFS2_JOURNAL_ACCESS_WRITE);
265 ocfs2_clear_local_alloc(alloc);
267 status = ocfs2_journal_dirty(handle, bh);
274 osb->local_alloc_bh = NULL;
275 osb->local_alloc_state = OCFS2_LA_UNUSED;
277 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
278 main_bm_inode, main_bm_bh);
283 ocfs2_commit_trans(osb, handle);
289 ocfs2_meta_unlock(main_bm_inode, 1);
292 mutex_unlock(&main_bm_inode->i_mutex);
296 if (local_alloc_inode)
297 iput(local_alloc_inode);
306 * We want to free the bitmap bits outside of any recovery context as
307 * we'll need a cluster lock to do so, but we must clear the local
308 * alloc before giving up the recovered nodes journal. To solve this,
309 * we kmalloc a copy of the local alloc before it's change for the
310 * caller to process with ocfs2_complete_local_alloc_recovery
312 int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
314 struct ocfs2_dinode **alloc_copy)
317 struct buffer_head *alloc_bh = NULL;
318 struct inode *inode = NULL;
319 struct ocfs2_dinode *alloc;
321 mlog_entry("(slot_num = %d)\n", slot_num);
325 inode = ocfs2_get_system_file_inode(osb,
326 LOCAL_ALLOC_SYSTEM_INODE,
334 mutex_lock(&inode->i_mutex);
336 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
337 &alloc_bh, 0, inode);
343 *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
344 if (!(*alloc_copy)) {
348 memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
350 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
351 ocfs2_clear_local_alloc(alloc);
353 status = ocfs2_write_block(osb, alloc_bh, inode);
358 if ((status < 0) && (*alloc_copy)) {
367 mutex_unlock(&inode->i_mutex);
376 * Step 2: By now, we've completed the journal recovery, we've stamped
377 * a clean local alloc on disk and dropped the node out of the
378 * recovery map. Dlm locks will no longer stall, so lets clear out the
381 int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
382 struct ocfs2_dinode *alloc)
386 struct buffer_head *main_bm_bh = NULL;
387 struct inode *main_bm_inode;
391 main_bm_inode = ocfs2_get_system_file_inode(osb,
392 GLOBAL_BITMAP_SYSTEM_INODE,
394 if (!main_bm_inode) {
400 mutex_lock(&main_bm_inode->i_mutex);
402 status = ocfs2_meta_lock(main_bm_inode, &main_bm_bh, 1);
408 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
409 if (IS_ERR(handle)) {
410 status = PTR_ERR(handle);
416 /* we want the bitmap change to be recorded on disk asap */
419 status = ocfs2_sync_local_to_main(osb, handle, alloc,
420 main_bm_inode, main_bm_bh);
424 ocfs2_commit_trans(osb, handle);
427 ocfs2_meta_unlock(main_bm_inode, 1);
430 mutex_unlock(&main_bm_inode->i_mutex);
443 * make sure we've got at least bitswanted contiguous bits in the
444 * local alloc. You lose them when you drop i_mutex.
446 * We will add ourselves to the transaction passed in, but may start
447 * our own in order to shift windows.
449 int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
451 struct ocfs2_alloc_context *ac)
454 struct ocfs2_dinode *alloc;
455 struct inode *local_alloc_inode;
456 unsigned int free_bits;
463 ocfs2_get_system_file_inode(osb,
464 LOCAL_ALLOC_SYSTEM_INODE,
466 if (!local_alloc_inode) {
472 mutex_lock(&local_alloc_inode->i_mutex);
474 ac->ac_inode = local_alloc_inode;
475 ac->ac_which = OCFS2_AC_USE_LOCAL;
477 if (osb->local_alloc_state != OCFS2_LA_ENABLED) {
482 if (bits_wanted > ocfs2_local_alloc_window_bits(osb)) {
483 mlog(0, "Asking for more than my max window size!\n");
488 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
490 if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
491 ocfs2_local_alloc_count_bits(alloc)) {
492 ocfs2_error(osb->sb, "local alloc inode %llu says it has "
493 "%u free bits, but a count shows %u",
494 (unsigned long long)le64_to_cpu(alloc->i_blkno),
495 le32_to_cpu(alloc->id1.bitmap1.i_used),
496 ocfs2_local_alloc_count_bits(alloc));
501 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
502 le32_to_cpu(alloc->id1.bitmap1.i_used);
503 if (bits_wanted > free_bits) {
504 /* uhoh, window change time. */
506 ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
508 if (status != -ENOSPC)
514 get_bh(osb->local_alloc_bh);
515 ac->ac_bh = osb->local_alloc_bh;
523 int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
525 struct ocfs2_alloc_context *ac,
531 struct inode *local_alloc_inode;
534 struct ocfs2_dinode *alloc;
535 struct ocfs2_local_alloc *la;
538 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
540 bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
541 local_alloc_inode = ac->ac_inode;
542 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
543 la = OCFS2_LOCAL_ALLOC(alloc);
545 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
547 /* TODO: Shouldn't we just BUG here? */
553 bitmap = la->la_bitmap;
554 *bit_off = le32_to_cpu(la->la_bm_off) + start;
555 /* local alloc is always contiguous by nature -- we never
556 * delete bits from it! */
557 *num_bits = bits_wanted;
559 status = ocfs2_journal_access(handle, local_alloc_inode,
561 OCFS2_JOURNAL_ACCESS_WRITE);
568 ocfs2_set_bit(start++, bitmap);
570 alloc->id1.bitmap1.i_used = cpu_to_le32(*num_bits +
571 le32_to_cpu(alloc->id1.bitmap1.i_used));
573 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
585 static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
590 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
594 buffer = la->la_bitmap;
595 for (i = 0; i < le16_to_cpu(la->la_size); i++)
596 count += hweight8(buffer[i]);
602 static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
603 struct ocfs2_dinode *alloc,
606 int numfound, bitoff, left, startoff, lastzero;
609 mlog_entry("(numbits wanted = %u)\n", numbits);
611 if (!alloc->id1.bitmap1.i_total) {
612 mlog(0, "No bits in my window!\n");
617 bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
619 numfound = bitoff = startoff = 0;
621 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
622 while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
623 if (bitoff == left) {
624 /* mlog(0, "bitoff (%d) == left", bitoff); */
627 /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
628 "numfound = %d\n", bitoff, startoff, numfound);*/
630 /* Ok, we found a zero bit... is it contig. or do we
632 if (bitoff == startoff) {
633 /* we found a zero */
637 /* got a zero after some ones */
641 /* we got everything we needed */
642 if (numfound == numbits) {
643 /* mlog(0, "Found it all!\n"); */
648 mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
651 if (numfound == numbits)
652 bitoff = startoff - numfound;
661 static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
663 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
667 alloc->id1.bitmap1.i_total = 0;
668 alloc->id1.bitmap1.i_used = 0;
670 for(i = 0; i < le16_to_cpu(la->la_size); i++)
671 la->la_bitmap[i] = 0;
677 /* turn this on and uncomment below to aid debugging window shifts. */
678 static void ocfs2_verify_zero_bits(unsigned long *bitmap,
682 unsigned int tmp = count;
684 if (ocfs2_test_bit(start + tmp, bitmap)) {
685 printk("ocfs2_verify_zero_bits: start = %u, count = "
686 "%u\n", start, count);
687 printk("ocfs2_verify_zero_bits: bit %u is set!",
696 * sync the local alloc to main bitmap.
698 * assumes you've already locked the main bitmap -- the bitmap inode
699 * passed is used for caching.
701 static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
703 struct ocfs2_dinode *alloc,
704 struct inode *main_bm_inode,
705 struct buffer_head *main_bm_bh)
708 int bit_off, left, count, start;
712 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
714 mlog_entry("total = %u, COUNT = %u, used = %u\n",
715 le32_to_cpu(alloc->id1.bitmap1.i_total),
716 ocfs2_local_alloc_count_bits(alloc),
717 le32_to_cpu(alloc->id1.bitmap1.i_used));
719 if (!alloc->id1.bitmap1.i_total) {
720 mlog(0, "nothing to sync!\n");
724 if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
725 le32_to_cpu(alloc->id1.bitmap1.i_total)) {
726 mlog(0, "all bits were taken!\n");
730 la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
731 le32_to_cpu(la->la_bm_off));
732 bitmap = la->la_bitmap;
733 start = count = bit_off = 0;
734 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
736 while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
738 if ((bit_off < left) && (bit_off == start)) {
744 blkno = la_start_blk +
745 ocfs2_clusters_to_blocks(osb->sb,
748 mlog(0, "freeing %u bits starting at local alloc bit "
749 "%u (la_start_blk = %llu, blkno = %llu)\n",
750 count, start - count,
751 (unsigned long long)la_start_blk,
752 (unsigned long long)blkno);
754 status = ocfs2_free_clusters(handle, main_bm_inode,
755 main_bm_bh, blkno, count);
772 static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
773 struct ocfs2_alloc_context **ac,
774 struct inode **bitmap_inode,
775 struct buffer_head **bitmap_bh)
779 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
786 (*ac)->ac_bits_wanted = ocfs2_local_alloc_window_bits(osb);
788 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
790 if (status != -ENOSPC)
795 *bitmap_inode = (*ac)->ac_inode;
796 igrab(*bitmap_inode);
797 *bitmap_bh = (*ac)->ac_bh;
801 if ((status < 0) && *ac) {
802 ocfs2_free_alloc_context(*ac);
811 * pass it the bitmap lock in lock_bh if you have it.
813 static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
815 struct ocfs2_alloc_context *ac)
818 u32 cluster_off, cluster_count;
819 struct ocfs2_dinode *alloc = NULL;
820 struct ocfs2_local_alloc *la;
824 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
825 la = OCFS2_LOCAL_ALLOC(alloc);
827 if (alloc->id1.bitmap1.i_total)
828 mlog(0, "asking me to alloc a new window over a non-empty "
831 mlog(0, "Allocating %u clusters for a new window.\n",
832 ocfs2_local_alloc_window_bits(osb));
834 /* Instruct the allocation code to try the most recently used
835 * cluster group. We'll re-record the group used this pass
837 ac->ac_last_group = osb->la_last_gd;
839 /* we used the generic suballoc reserve function, but we set
840 * everything up nicely, so there's no reason why we can't use
841 * the more specific cluster api to claim bits. */
842 status = ocfs2_claim_clusters(osb, handle, ac,
843 ocfs2_local_alloc_window_bits(osb),
844 &cluster_off, &cluster_count);
846 if (status != -ENOSPC)
851 osb->la_last_gd = ac->ac_last_group;
853 la->la_bm_off = cpu_to_le32(cluster_off);
854 alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
855 /* just in case... In the future when we find space ourselves,
856 * we don't have to get all contiguous -- but we'll have to
857 * set all previously used bits in bitmap and update
858 * la_bits_set before setting the bits in the main bitmap. */
859 alloc->id1.bitmap1.i_used = 0;
860 memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
861 le16_to_cpu(la->la_size));
863 mlog(0, "New window allocated:\n");
864 mlog(0, "window la_bm_off = %u\n",
865 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
866 mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
873 /* Note that we do *NOT* lock the local alloc inode here as
874 * it's been locked already for us. */
875 static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
876 struct inode *local_alloc_inode)
879 struct buffer_head *main_bm_bh = NULL;
880 struct inode *main_bm_inode = NULL;
881 handle_t *handle = NULL;
882 struct ocfs2_dinode *alloc;
883 struct ocfs2_dinode *alloc_copy = NULL;
884 struct ocfs2_alloc_context *ac = NULL;
888 /* This will lock the main bitmap for us. */
889 status = ocfs2_local_alloc_reserve_for_window(osb,
894 if (status != -ENOSPC)
899 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
900 if (IS_ERR(handle)) {
901 status = PTR_ERR(handle);
907 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
909 /* We want to clear the local alloc before doing anything
910 * else, so that if we error later during this operation,
911 * local alloc shutdown won't try to double free main bitmap
912 * bits. Make a copy so the sync function knows which bits to
914 alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_KERNEL);
920 memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
922 status = ocfs2_journal_access(handle, local_alloc_inode,
924 OCFS2_JOURNAL_ACCESS_WRITE);
930 ocfs2_clear_local_alloc(alloc);
932 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
938 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
939 main_bm_inode, main_bm_bh);
945 status = ocfs2_local_alloc_new_window(osb, handle, ac);
947 if (status != -ENOSPC)
952 atomic_inc(&osb->alloc_stats.moves);
957 ocfs2_commit_trans(osb, handle);
969 ocfs2_free_alloc_context(ac);