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);
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);
95 if (osb->local_alloc_state != OCFS2_LA_ENABLED)
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))
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);
112 int ocfs2_load_local_alloc(struct ocfs2_super *osb)
115 struct ocfs2_dinode *alloc = NULL;
116 struct buffer_head *alloc_bh = NULL;
118 struct inode *inode = NULL;
119 struct ocfs2_local_alloc *la;
123 if (osb->local_alloc_size == 0)
126 if (ocfs2_local_alloc_window_bits(osb) >= osb->bitmap_cpg) {
127 mlog(ML_NOTICE, "Requested local alloc window %d is larger "
128 "than max possible %u. Using defaults.\n",
129 ocfs2_local_alloc_window_bits(osb), (osb->bitmap_cpg - 1));
130 osb->local_alloc_size = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
133 /* read the alloc off disk */
134 inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
142 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
143 &alloc_bh, 0, inode);
149 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
150 la = OCFS2_LOCAL_ALLOC(alloc);
152 if (!(le32_to_cpu(alloc->i_flags) &
153 (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
154 mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
155 (unsigned long long)OCFS2_I(inode)->ip_blkno);
160 if ((la->la_size == 0) ||
161 (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
162 mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
163 le16_to_cpu(la->la_size));
168 /* do a little verification. */
169 num_used = ocfs2_local_alloc_count_bits(alloc);
171 /* hopefully the local alloc has always been recovered before
174 || alloc->id1.bitmap1.i_used
175 || alloc->id1.bitmap1.i_total
177 mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
178 "found = %u, set = %u, taken = %u, off = %u\n",
179 num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
180 le32_to_cpu(alloc->id1.bitmap1.i_total),
181 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
183 osb->local_alloc_bh = alloc_bh;
184 osb->local_alloc_state = OCFS2_LA_ENABLED;
193 mlog(0, "Local alloc window bits = %d\n",
194 ocfs2_local_alloc_window_bits(osb));
201 * return any unused bits to the bitmap and write out a clean
204 * local_alloc_bh is optional. If not passed, we will simply use the
205 * one off osb. If you do pass it however, be warned that it *will* be
206 * returned brelse'd and NULL'd out.*/
207 void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
211 struct inode *local_alloc_inode = NULL;
212 struct buffer_head *bh = NULL;
213 struct buffer_head *main_bm_bh = NULL;
214 struct inode *main_bm_inode = NULL;
215 struct ocfs2_dinode *alloc_copy = NULL;
216 struct ocfs2_dinode *alloc = NULL;
220 if (osb->local_alloc_state == OCFS2_LA_UNUSED)
224 ocfs2_get_system_file_inode(osb,
225 LOCAL_ALLOC_SYSTEM_INODE,
227 if (!local_alloc_inode) {
233 osb->local_alloc_state = OCFS2_LA_DISABLED;
235 main_bm_inode = ocfs2_get_system_file_inode(osb,
236 GLOBAL_BITMAP_SYSTEM_INODE,
238 if (!main_bm_inode) {
244 mutex_lock(&main_bm_inode->i_mutex);
246 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
252 /* WINDOW_MOVE_CREDITS is a bit heavy... */
253 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
254 if (IS_ERR(handle)) {
255 mlog_errno(PTR_ERR(handle));
260 bh = osb->local_alloc_bh;
261 alloc = (struct ocfs2_dinode *) bh->b_data;
263 alloc_copy = kmalloc(bh->b_size, GFP_KERNEL);
268 memcpy(alloc_copy, alloc, bh->b_size);
270 status = ocfs2_journal_access(handle, local_alloc_inode, bh,
271 OCFS2_JOURNAL_ACCESS_WRITE);
277 ocfs2_clear_local_alloc(alloc);
279 status = ocfs2_journal_dirty(handle, bh);
286 osb->local_alloc_bh = NULL;
287 osb->local_alloc_state = OCFS2_LA_UNUSED;
289 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
290 main_bm_inode, main_bm_bh);
295 ocfs2_commit_trans(osb, handle);
301 ocfs2_inode_unlock(main_bm_inode, 1);
304 mutex_unlock(&main_bm_inode->i_mutex);
308 if (local_alloc_inode)
309 iput(local_alloc_inode);
318 * We want to free the bitmap bits outside of any recovery context as
319 * we'll need a cluster lock to do so, but we must clear the local
320 * alloc before giving up the recovered nodes journal. To solve this,
321 * we kmalloc a copy of the local alloc before it's change for the
322 * caller to process with ocfs2_complete_local_alloc_recovery
324 int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
326 struct ocfs2_dinode **alloc_copy)
329 struct buffer_head *alloc_bh = NULL;
330 struct inode *inode = NULL;
331 struct ocfs2_dinode *alloc;
333 mlog_entry("(slot_num = %d)\n", slot_num);
337 inode = ocfs2_get_system_file_inode(osb,
338 LOCAL_ALLOC_SYSTEM_INODE,
346 mutex_lock(&inode->i_mutex);
348 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
349 &alloc_bh, 0, inode);
355 *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
356 if (!(*alloc_copy)) {
360 memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
362 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
363 ocfs2_clear_local_alloc(alloc);
365 status = ocfs2_write_block(osb, alloc_bh, inode);
370 if ((status < 0) && (*alloc_copy)) {
379 mutex_unlock(&inode->i_mutex);
388 * Step 2: By now, we've completed the journal recovery, we've stamped
389 * a clean local alloc on disk and dropped the node out of the
390 * recovery map. Dlm locks will no longer stall, so lets clear out the
393 int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
394 struct ocfs2_dinode *alloc)
398 struct buffer_head *main_bm_bh = NULL;
399 struct inode *main_bm_inode;
403 main_bm_inode = ocfs2_get_system_file_inode(osb,
404 GLOBAL_BITMAP_SYSTEM_INODE,
406 if (!main_bm_inode) {
412 mutex_lock(&main_bm_inode->i_mutex);
414 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
420 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
421 if (IS_ERR(handle)) {
422 status = PTR_ERR(handle);
428 /* we want the bitmap change to be recorded on disk asap */
431 status = ocfs2_sync_local_to_main(osb, handle, alloc,
432 main_bm_inode, main_bm_bh);
436 ocfs2_commit_trans(osb, handle);
439 ocfs2_inode_unlock(main_bm_inode, 1);
442 mutex_unlock(&main_bm_inode->i_mutex);
455 * make sure we've got at least bitswanted contiguous bits in the
456 * local alloc. You lose them when you drop i_mutex.
458 * We will add ourselves to the transaction passed in, but may start
459 * our own in order to shift windows.
461 int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
463 struct ocfs2_alloc_context *ac)
466 struct ocfs2_dinode *alloc;
467 struct inode *local_alloc_inode;
468 unsigned int free_bits;
475 ocfs2_get_system_file_inode(osb,
476 LOCAL_ALLOC_SYSTEM_INODE,
478 if (!local_alloc_inode) {
484 mutex_lock(&local_alloc_inode->i_mutex);
486 if (osb->local_alloc_state != OCFS2_LA_ENABLED) {
491 if (bits_wanted > ocfs2_local_alloc_window_bits(osb)) {
492 mlog(0, "Asking for more than my max window size!\n");
497 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
499 #ifdef OCFS2_DEBUG_FS
500 if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
501 ocfs2_local_alloc_count_bits(alloc)) {
502 ocfs2_error(osb->sb, "local alloc inode %llu says it has "
503 "%u free bits, but a count shows %u",
504 (unsigned long long)le64_to_cpu(alloc->i_blkno),
505 le32_to_cpu(alloc->id1.bitmap1.i_used),
506 ocfs2_local_alloc_count_bits(alloc));
512 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
513 le32_to_cpu(alloc->id1.bitmap1.i_used);
514 if (bits_wanted > free_bits) {
515 /* uhoh, window change time. */
517 ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
519 if (status != -ENOSPC)
525 ac->ac_inode = local_alloc_inode;
526 ac->ac_which = OCFS2_AC_USE_LOCAL;
527 get_bh(osb->local_alloc_bh);
528 ac->ac_bh = osb->local_alloc_bh;
531 if (status < 0 && local_alloc_inode) {
532 mutex_unlock(&local_alloc_inode->i_mutex);
533 iput(local_alloc_inode);
536 mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
543 int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
545 struct ocfs2_alloc_context *ac,
551 struct inode *local_alloc_inode;
553 struct ocfs2_dinode *alloc;
554 struct ocfs2_local_alloc *la;
557 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
559 local_alloc_inode = ac->ac_inode;
560 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
561 la = OCFS2_LOCAL_ALLOC(alloc);
563 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
565 /* TODO: Shouldn't we just BUG here? */
571 bitmap = la->la_bitmap;
572 *bit_off = le32_to_cpu(la->la_bm_off) + start;
573 /* local alloc is always contiguous by nature -- we never
574 * delete bits from it! */
575 *num_bits = bits_wanted;
577 status = ocfs2_journal_access(handle, local_alloc_inode,
579 OCFS2_JOURNAL_ACCESS_WRITE);
586 ocfs2_set_bit(start++, bitmap);
588 alloc->id1.bitmap1.i_used = cpu_to_le32(*num_bits +
589 le32_to_cpu(alloc->id1.bitmap1.i_used));
591 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
603 static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
608 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
612 buffer = la->la_bitmap;
613 for (i = 0; i < le16_to_cpu(la->la_size); i++)
614 count += hweight8(buffer[i]);
620 static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
621 struct ocfs2_dinode *alloc,
624 int numfound, bitoff, left, startoff, lastzero;
627 mlog_entry("(numbits wanted = %u)\n", numbits);
629 if (!alloc->id1.bitmap1.i_total) {
630 mlog(0, "No bits in my window!\n");
635 bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
637 numfound = bitoff = startoff = 0;
639 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
640 while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
641 if (bitoff == left) {
642 /* mlog(0, "bitoff (%d) == left", bitoff); */
645 /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
646 "numfound = %d\n", bitoff, startoff, numfound);*/
648 /* Ok, we found a zero bit... is it contig. or do we
650 if (bitoff == startoff) {
651 /* we found a zero */
655 /* got a zero after some ones */
659 /* we got everything we needed */
660 if (numfound == numbits) {
661 /* mlog(0, "Found it all!\n"); */
666 mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
669 if (numfound == numbits)
670 bitoff = startoff - numfound;
679 static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
681 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
685 alloc->id1.bitmap1.i_total = 0;
686 alloc->id1.bitmap1.i_used = 0;
688 for(i = 0; i < le16_to_cpu(la->la_size); i++)
689 la->la_bitmap[i] = 0;
695 /* turn this on and uncomment below to aid debugging window shifts. */
696 static void ocfs2_verify_zero_bits(unsigned long *bitmap,
700 unsigned int tmp = count;
702 if (ocfs2_test_bit(start + tmp, bitmap)) {
703 printk("ocfs2_verify_zero_bits: start = %u, count = "
704 "%u\n", start, count);
705 printk("ocfs2_verify_zero_bits: bit %u is set!",
714 * sync the local alloc to main bitmap.
716 * assumes you've already locked the main bitmap -- the bitmap inode
717 * passed is used for caching.
719 static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
721 struct ocfs2_dinode *alloc,
722 struct inode *main_bm_inode,
723 struct buffer_head *main_bm_bh)
726 int bit_off, left, count, start;
730 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
732 mlog_entry("total = %u, used = %u\n",
733 le32_to_cpu(alloc->id1.bitmap1.i_total),
734 le32_to_cpu(alloc->id1.bitmap1.i_used));
736 if (!alloc->id1.bitmap1.i_total) {
737 mlog(0, "nothing to sync!\n");
741 if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
742 le32_to_cpu(alloc->id1.bitmap1.i_total)) {
743 mlog(0, "all bits were taken!\n");
747 la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
748 le32_to_cpu(la->la_bm_off));
749 bitmap = la->la_bitmap;
750 start = count = bit_off = 0;
751 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
753 while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
755 if ((bit_off < left) && (bit_off == start)) {
761 blkno = la_start_blk +
762 ocfs2_clusters_to_blocks(osb->sb,
765 mlog(0, "freeing %u bits starting at local alloc bit "
766 "%u (la_start_blk = %llu, blkno = %llu)\n",
767 count, start - count,
768 (unsigned long long)la_start_blk,
769 (unsigned long long)blkno);
771 status = ocfs2_free_clusters(handle, main_bm_inode,
772 main_bm_bh, blkno, count);
789 static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
790 struct ocfs2_alloc_context **ac,
791 struct inode **bitmap_inode,
792 struct buffer_head **bitmap_bh)
796 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
803 (*ac)->ac_bits_wanted = ocfs2_local_alloc_window_bits(osb);
805 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
807 if (status != -ENOSPC)
812 *bitmap_inode = (*ac)->ac_inode;
813 igrab(*bitmap_inode);
814 *bitmap_bh = (*ac)->ac_bh;
818 if ((status < 0) && *ac) {
819 ocfs2_free_alloc_context(*ac);
828 * pass it the bitmap lock in lock_bh if you have it.
830 static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
832 struct ocfs2_alloc_context *ac)
835 u32 cluster_off, cluster_count;
836 struct ocfs2_dinode *alloc = NULL;
837 struct ocfs2_local_alloc *la;
841 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
842 la = OCFS2_LOCAL_ALLOC(alloc);
844 if (alloc->id1.bitmap1.i_total)
845 mlog(0, "asking me to alloc a new window over a non-empty "
848 mlog(0, "Allocating %u clusters for a new window.\n",
849 ocfs2_local_alloc_window_bits(osb));
851 /* Instruct the allocation code to try the most recently used
852 * cluster group. We'll re-record the group used this pass
854 ac->ac_last_group = osb->la_last_gd;
856 /* we used the generic suballoc reserve function, but we set
857 * everything up nicely, so there's no reason why we can't use
858 * the more specific cluster api to claim bits. */
859 status = ocfs2_claim_clusters(osb, handle, ac,
860 ocfs2_local_alloc_window_bits(osb),
861 &cluster_off, &cluster_count);
863 if (status != -ENOSPC)
868 osb->la_last_gd = ac->ac_last_group;
870 la->la_bm_off = cpu_to_le32(cluster_off);
871 alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
872 /* just in case... In the future when we find space ourselves,
873 * we don't have to get all contiguous -- but we'll have to
874 * set all previously used bits in bitmap and update
875 * la_bits_set before setting the bits in the main bitmap. */
876 alloc->id1.bitmap1.i_used = 0;
877 memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
878 le16_to_cpu(la->la_size));
880 mlog(0, "New window allocated:\n");
881 mlog(0, "window la_bm_off = %u\n",
882 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
883 mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
890 /* Note that we do *NOT* lock the local alloc inode here as
891 * it's been locked already for us. */
892 static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
893 struct inode *local_alloc_inode)
896 struct buffer_head *main_bm_bh = NULL;
897 struct inode *main_bm_inode = NULL;
898 handle_t *handle = NULL;
899 struct ocfs2_dinode *alloc;
900 struct ocfs2_dinode *alloc_copy = NULL;
901 struct ocfs2_alloc_context *ac = NULL;
905 /* This will lock the main bitmap for us. */
906 status = ocfs2_local_alloc_reserve_for_window(osb,
911 if (status != -ENOSPC)
916 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
917 if (IS_ERR(handle)) {
918 status = PTR_ERR(handle);
924 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
926 /* We want to clear the local alloc before doing anything
927 * else, so that if we error later during this operation,
928 * local alloc shutdown won't try to double free main bitmap
929 * bits. Make a copy so the sync function knows which bits to
931 alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_KERNEL);
937 memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
939 status = ocfs2_journal_access(handle, local_alloc_inode,
941 OCFS2_JOURNAL_ACCESS_WRITE);
947 ocfs2_clear_local_alloc(alloc);
949 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
955 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
956 main_bm_inode, main_bm_bh);
962 status = ocfs2_local_alloc_new_window(osb, handle, ac);
964 if (status != -ENOSPC)
969 atomic_inc(&osb->alloc_stats.moves);
974 ocfs2_commit_trans(osb, handle);
986 ocfs2_free_alloc_context(ac);