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>
38 #include "blockcheck.h"
42 #include "localalloc.h"
47 #include "buffer_head_io.h"
49 #define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab))
51 static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc);
53 static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
54 struct ocfs2_dinode *alloc,
56 struct ocfs2_alloc_reservation *resv);
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 void ocfs2_la_set_sizes(struct ocfs2_super *osb, int requested_mb)
80 struct super_block *sb = osb->sb;
81 unsigned int la_default_mb = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
82 unsigned int la_max_mb;
84 la_max_mb = ocfs2_clusters_to_megabytes(sb,
85 ocfs2_local_alloc_size(sb) * 8);
87 mlog(0, "requested: %dM, max: %uM, default: %uM\n",
88 requested_mb, la_max_mb, la_default_mb);
90 if (requested_mb == -1) {
91 /* No user request - use defaults */
92 osb->local_alloc_default_bits =
93 ocfs2_megabytes_to_clusters(sb, la_default_mb);
94 } else if (requested_mb > la_max_mb) {
95 /* Request is too big, we give the maximum available */
96 osb->local_alloc_default_bits =
97 ocfs2_megabytes_to_clusters(sb, la_max_mb);
99 osb->local_alloc_default_bits =
100 ocfs2_megabytes_to_clusters(sb, requested_mb);
103 osb->local_alloc_bits = osb->local_alloc_default_bits;
106 static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb)
108 return (osb->local_alloc_state == OCFS2_LA_THROTTLED ||
109 osb->local_alloc_state == OCFS2_LA_ENABLED);
112 void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb,
113 unsigned int num_clusters)
115 spin_lock(&osb->osb_lock);
116 if (osb->local_alloc_state == OCFS2_LA_DISABLED ||
117 osb->local_alloc_state == OCFS2_LA_THROTTLED)
118 if (num_clusters >= osb->local_alloc_default_bits) {
119 cancel_delayed_work(&osb->la_enable_wq);
120 osb->local_alloc_state = OCFS2_LA_ENABLED;
122 spin_unlock(&osb->osb_lock);
125 void ocfs2_la_enable_worker(struct work_struct *work)
127 struct ocfs2_super *osb =
128 container_of(work, struct ocfs2_super,
130 spin_lock(&osb->osb_lock);
131 osb->local_alloc_state = OCFS2_LA_ENABLED;
132 spin_unlock(&osb->osb_lock);
136 * Tell us whether a given allocation should use the local alloc
137 * file. Otherwise, it has to go to the main bitmap.
139 * This function does semi-dirty reads of local alloc size and state!
140 * This is ok however, as the values are re-checked once under mutex.
142 int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
147 spin_lock(&osb->osb_lock);
148 la_bits = osb->local_alloc_bits;
150 if (!ocfs2_la_state_enabled(osb))
153 /* la_bits should be at least twice the size (in clusters) of
154 * a new block group. We want to be sure block group
155 * allocations go through the local alloc, so allow an
156 * allocation to take up to half the bitmap. */
157 if (bits > (la_bits / 2))
162 mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n",
163 osb->local_alloc_state, (unsigned long long)bits, la_bits, ret);
164 spin_unlock(&osb->osb_lock);
168 int ocfs2_load_local_alloc(struct ocfs2_super *osb)
171 struct ocfs2_dinode *alloc = NULL;
172 struct buffer_head *alloc_bh = NULL;
174 struct inode *inode = NULL;
175 struct ocfs2_local_alloc *la;
179 if (osb->local_alloc_bits == 0)
182 if (osb->local_alloc_bits >= osb->bitmap_cpg) {
183 mlog(ML_NOTICE, "Requested local alloc window %d is larger "
184 "than max possible %u. Using defaults.\n",
185 osb->local_alloc_bits, (osb->bitmap_cpg - 1));
186 osb->local_alloc_bits =
187 ocfs2_megabytes_to_clusters(osb->sb,
188 OCFS2_DEFAULT_LOCAL_ALLOC_SIZE);
191 /* read the alloc off disk */
192 inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
200 status = ocfs2_read_inode_block_full(inode, &alloc_bh,
201 OCFS2_BH_IGNORE_CACHE);
207 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
208 la = OCFS2_LOCAL_ALLOC(alloc);
210 if (!(le32_to_cpu(alloc->i_flags) &
211 (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
212 mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
213 (unsigned long long)OCFS2_I(inode)->ip_blkno);
218 if ((la->la_size == 0) ||
219 (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
220 mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
221 le16_to_cpu(la->la_size));
226 /* do a little verification. */
227 num_used = ocfs2_local_alloc_count_bits(alloc);
229 /* hopefully the local alloc has always been recovered before
232 || alloc->id1.bitmap1.i_used
233 || alloc->id1.bitmap1.i_total
235 mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
236 "found = %u, set = %u, taken = %u, off = %u\n",
237 num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
238 le32_to_cpu(alloc->id1.bitmap1.i_total),
239 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
241 osb->local_alloc_bh = alloc_bh;
242 osb->local_alloc_state = OCFS2_LA_ENABLED;
250 mlog(0, "Local alloc window bits = %d\n", osb->local_alloc_bits);
257 * return any unused bits to the bitmap and write out a clean
260 * local_alloc_bh is optional. If not passed, we will simply use the
261 * one off osb. If you do pass it however, be warned that it *will* be
262 * returned brelse'd and NULL'd out.*/
263 void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
267 struct inode *local_alloc_inode = NULL;
268 struct buffer_head *bh = NULL;
269 struct buffer_head *main_bm_bh = NULL;
270 struct inode *main_bm_inode = NULL;
271 struct ocfs2_dinode *alloc_copy = NULL;
272 struct ocfs2_dinode *alloc = NULL;
276 cancel_delayed_work(&osb->la_enable_wq);
277 flush_workqueue(ocfs2_wq);
279 if (osb->local_alloc_state == OCFS2_LA_UNUSED)
283 ocfs2_get_system_file_inode(osb,
284 LOCAL_ALLOC_SYSTEM_INODE,
286 if (!local_alloc_inode) {
292 osb->local_alloc_state = OCFS2_LA_DISABLED;
294 ocfs2_resmap_uninit(&osb->osb_la_resmap);
296 main_bm_inode = ocfs2_get_system_file_inode(osb,
297 GLOBAL_BITMAP_SYSTEM_INODE,
299 if (!main_bm_inode) {
305 mutex_lock(&main_bm_inode->i_mutex);
307 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
313 /* WINDOW_MOVE_CREDITS is a bit heavy... */
314 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
315 if (IS_ERR(handle)) {
316 mlog_errno(PTR_ERR(handle));
321 bh = osb->local_alloc_bh;
322 alloc = (struct ocfs2_dinode *) bh->b_data;
324 alloc_copy = kmalloc(bh->b_size, GFP_NOFS);
329 memcpy(alloc_copy, alloc, bh->b_size);
331 status = ocfs2_journal_access_di(handle, INODE_CACHE(local_alloc_inode),
332 bh, OCFS2_JOURNAL_ACCESS_WRITE);
338 ocfs2_clear_local_alloc(alloc);
339 ocfs2_journal_dirty(handle, bh);
342 osb->local_alloc_bh = NULL;
343 osb->local_alloc_state = OCFS2_LA_UNUSED;
345 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
346 main_bm_inode, main_bm_bh);
351 ocfs2_commit_trans(osb, handle);
356 ocfs2_inode_unlock(main_bm_inode, 1);
359 mutex_unlock(&main_bm_inode->i_mutex);
363 if (local_alloc_inode)
364 iput(local_alloc_inode);
373 * We want to free the bitmap bits outside of any recovery context as
374 * we'll need a cluster lock to do so, but we must clear the local
375 * alloc before giving up the recovered nodes journal. To solve this,
376 * we kmalloc a copy of the local alloc before it's change for the
377 * caller to process with ocfs2_complete_local_alloc_recovery
379 int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
381 struct ocfs2_dinode **alloc_copy)
384 struct buffer_head *alloc_bh = NULL;
385 struct inode *inode = NULL;
386 struct ocfs2_dinode *alloc;
388 mlog_entry("(slot_num = %d)\n", slot_num);
392 inode = ocfs2_get_system_file_inode(osb,
393 LOCAL_ALLOC_SYSTEM_INODE,
401 mutex_lock(&inode->i_mutex);
403 status = ocfs2_read_inode_block_full(inode, &alloc_bh,
404 OCFS2_BH_IGNORE_CACHE);
410 *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
411 if (!(*alloc_copy)) {
415 memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
417 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
418 ocfs2_clear_local_alloc(alloc);
420 ocfs2_compute_meta_ecc(osb->sb, alloc_bh->b_data, &alloc->i_check);
421 status = ocfs2_write_block(osb, alloc_bh, INODE_CACHE(inode));
426 if ((status < 0) && (*alloc_copy)) {
434 mutex_unlock(&inode->i_mutex);
443 * Step 2: By now, we've completed the journal recovery, we've stamped
444 * a clean local alloc on disk and dropped the node out of the
445 * recovery map. Dlm locks will no longer stall, so lets clear out the
448 int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
449 struct ocfs2_dinode *alloc)
453 struct buffer_head *main_bm_bh = NULL;
454 struct inode *main_bm_inode;
458 main_bm_inode = ocfs2_get_system_file_inode(osb,
459 GLOBAL_BITMAP_SYSTEM_INODE,
461 if (!main_bm_inode) {
467 mutex_lock(&main_bm_inode->i_mutex);
469 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
475 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
476 if (IS_ERR(handle)) {
477 status = PTR_ERR(handle);
483 /* we want the bitmap change to be recorded on disk asap */
486 status = ocfs2_sync_local_to_main(osb, handle, alloc,
487 main_bm_inode, main_bm_bh);
491 ocfs2_commit_trans(osb, handle);
494 ocfs2_inode_unlock(main_bm_inode, 1);
497 mutex_unlock(&main_bm_inode->i_mutex);
505 ocfs2_init_steal_slots(osb);
511 * make sure we've got at least bits_wanted contiguous bits in the
512 * local alloc. You lose them when you drop i_mutex.
514 * We will add ourselves to the transaction passed in, but may start
515 * our own in order to shift windows.
517 int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
519 struct ocfs2_alloc_context *ac)
522 struct ocfs2_dinode *alloc;
523 struct inode *local_alloc_inode;
524 unsigned int free_bits;
531 ocfs2_get_system_file_inode(osb,
532 LOCAL_ALLOC_SYSTEM_INODE,
534 if (!local_alloc_inode) {
540 mutex_lock(&local_alloc_inode->i_mutex);
543 * We must double check state and allocator bits because
544 * another process may have changed them while holding i_mutex.
546 spin_lock(&osb->osb_lock);
547 if (!ocfs2_la_state_enabled(osb) ||
548 (bits_wanted > osb->local_alloc_bits)) {
549 spin_unlock(&osb->osb_lock);
553 spin_unlock(&osb->osb_lock);
555 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
557 #ifdef CONFIG_OCFS2_DEBUG_FS
558 if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
559 ocfs2_local_alloc_count_bits(alloc)) {
560 ocfs2_error(osb->sb, "local alloc inode %llu says it has "
561 "%u free bits, but a count shows %u",
562 (unsigned long long)le64_to_cpu(alloc->i_blkno),
563 le32_to_cpu(alloc->id1.bitmap1.i_used),
564 ocfs2_local_alloc_count_bits(alloc));
570 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
571 le32_to_cpu(alloc->id1.bitmap1.i_used);
572 if (bits_wanted > free_bits) {
573 /* uhoh, window change time. */
575 ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
577 if (status != -ENOSPC)
583 * Under certain conditions, the window slide code
584 * might have reduced the number of bits available or
585 * disabled the the local alloc entirely. Re-check
586 * here and return -ENOSPC if necessary.
589 if (!ocfs2_la_state_enabled(osb))
592 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
593 le32_to_cpu(alloc->id1.bitmap1.i_used);
594 if (bits_wanted > free_bits)
598 if (ac->ac_max_block)
599 mlog(0, "Calling in_range for max block %llu\n",
600 (unsigned long long)ac->ac_max_block);
602 ac->ac_inode = local_alloc_inode;
603 /* We should never use localalloc from another slot */
604 ac->ac_alloc_slot = osb->slot_num;
605 ac->ac_which = OCFS2_AC_USE_LOCAL;
606 get_bh(osb->local_alloc_bh);
607 ac->ac_bh = osb->local_alloc_bh;
610 if (status < 0 && local_alloc_inode) {
611 mutex_unlock(&local_alloc_inode->i_mutex);
612 iput(local_alloc_inode);
615 mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
622 int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
624 struct ocfs2_alloc_context *ac,
630 struct inode *local_alloc_inode;
632 struct ocfs2_dinode *alloc;
633 struct ocfs2_local_alloc *la;
636 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
638 local_alloc_inode = ac->ac_inode;
639 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
640 la = OCFS2_LOCAL_ALLOC(alloc);
642 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, &bits_wanted,
645 /* TODO: Shouldn't we just BUG here? */
651 bitmap = la->la_bitmap;
652 *bit_off = le32_to_cpu(la->la_bm_off) + start;
653 *num_bits = bits_wanted;
655 status = ocfs2_journal_access_di(handle,
656 INODE_CACHE(local_alloc_inode),
658 OCFS2_JOURNAL_ACCESS_WRITE);
664 ocfs2_resmap_claimed_bits(&osb->osb_la_resmap, ac->ac_resv, start,
668 ocfs2_set_bit(start++, bitmap);
670 le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
671 ocfs2_journal_dirty(handle, osb->local_alloc_bh);
678 static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
683 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
687 buffer = la->la_bitmap;
688 for (i = 0; i < le16_to_cpu(la->la_size); i++)
689 count += hweight8(buffer[i]);
695 static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
696 struct ocfs2_dinode *alloc,
698 struct ocfs2_alloc_reservation *resv)
700 int numfound, bitoff, left, startoff, lastzero;
702 struct ocfs2_alloc_reservation r;
704 struct ocfs2_reservation_map *resmap = &osb->osb_la_resmap;
706 mlog_entry("(numbits wanted = %u)\n", *numbits);
708 if (!alloc->id1.bitmap1.i_total) {
709 mlog(0, "No bits in my window!\n");
716 ocfs2_resv_init_once(&r);
717 ocfs2_resv_set_type(&r, OCFS2_RESV_FLAG_TMP);
722 if (ocfs2_resmap_resv_bits(resmap, resv, &bitoff, &numfound) == 0) {
723 if (numfound < *numbits)
729 * Code error. While reservations are enabled, local
730 * allocation should _always_ go through them.
732 BUG_ON(osb->osb_resv_level != 0);
735 * Reservations are disabled. Handle this the old way.
738 bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
740 numfound = bitoff = startoff = 0;
742 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
743 while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
744 if (bitoff == left) {
745 /* mlog(0, "bitoff (%d) == left", bitoff); */
748 /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
749 "numfound = %d\n", bitoff, startoff, numfound);*/
751 /* Ok, we found a zero bit... is it contig. or do we
753 if (bitoff == startoff) {
754 /* we found a zero */
758 /* got a zero after some ones */
762 /* we got everything we needed */
763 if (numfound == *numbits) {
764 /* mlog(0, "Found it all!\n"); */
769 mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
772 if (numfound == *numbits) {
773 bitoff = startoff - numfound;
782 ocfs2_resv_discard(resmap, resv);
788 static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
790 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
794 alloc->id1.bitmap1.i_total = 0;
795 alloc->id1.bitmap1.i_used = 0;
797 for(i = 0; i < le16_to_cpu(la->la_size); i++)
798 la->la_bitmap[i] = 0;
804 /* turn this on and uncomment below to aid debugging window shifts. */
805 static void ocfs2_verify_zero_bits(unsigned long *bitmap,
809 unsigned int tmp = count;
811 if (ocfs2_test_bit(start + tmp, bitmap)) {
812 printk("ocfs2_verify_zero_bits: start = %u, count = "
813 "%u\n", start, count);
814 printk("ocfs2_verify_zero_bits: bit %u is set!",
823 * sync the local alloc to main bitmap.
825 * assumes you've already locked the main bitmap -- the bitmap inode
826 * passed is used for caching.
828 static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
830 struct ocfs2_dinode *alloc,
831 struct inode *main_bm_inode,
832 struct buffer_head *main_bm_bh)
835 int bit_off, left, count, start;
839 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
841 mlog_entry("total = %u, used = %u\n",
842 le32_to_cpu(alloc->id1.bitmap1.i_total),
843 le32_to_cpu(alloc->id1.bitmap1.i_used));
845 if (!alloc->id1.bitmap1.i_total) {
846 mlog(0, "nothing to sync!\n");
850 if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
851 le32_to_cpu(alloc->id1.bitmap1.i_total)) {
852 mlog(0, "all bits were taken!\n");
856 la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
857 le32_to_cpu(la->la_bm_off));
858 bitmap = la->la_bitmap;
859 start = count = bit_off = 0;
860 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
862 while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
864 if ((bit_off < left) && (bit_off == start)) {
870 blkno = la_start_blk +
871 ocfs2_clusters_to_blocks(osb->sb,
874 mlog(0, "freeing %u bits starting at local alloc bit "
875 "%u (la_start_blk = %llu, blkno = %llu)\n",
876 count, start - count,
877 (unsigned long long)la_start_blk,
878 (unsigned long long)blkno);
880 status = ocfs2_release_clusters(handle,
900 enum ocfs2_la_event {
901 OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */
902 OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has
903 * enough bits theoretically
904 * free, but a contiguous
905 * allocation could not be
907 OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have
908 * enough bits free to satisfy
911 #define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
913 * Given an event, calculate the size of our next local alloc window.
915 * This should always be called under i_mutex of the local alloc inode
916 * so that local alloc disabling doesn't race with processes trying to
919 * Returns the state which the local alloc was left in. This value can
920 * be ignored by some paths.
922 static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
923 enum ocfs2_la_event event)
928 spin_lock(&osb->osb_lock);
929 if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
930 WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
935 * ENOSPC and fragmentation are treated similarly for now.
937 if (event == OCFS2_LA_EVENT_ENOSPC ||
938 event == OCFS2_LA_EVENT_FRAGMENTED) {
940 * We ran out of contiguous space in the primary
941 * bitmap. Drastically reduce the number of bits used
942 * by local alloc until we have to disable it.
944 bits = osb->local_alloc_bits >> 1;
945 if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
947 * By setting state to THROTTLED, we'll keep
948 * the number of local alloc bits used down
949 * until an event occurs which would give us
950 * reason to assume the bitmap situation might
953 osb->local_alloc_state = OCFS2_LA_THROTTLED;
954 osb->local_alloc_bits = bits;
956 osb->local_alloc_state = OCFS2_LA_DISABLED;
958 queue_delayed_work(ocfs2_wq, &osb->la_enable_wq,
959 OCFS2_LA_ENABLE_INTERVAL);
964 * Don't increase the size of the local alloc window until we
965 * know we might be able to fulfill the request. Otherwise, we
966 * risk bouncing around the global bitmap during periods of
969 if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
970 osb->local_alloc_bits = osb->local_alloc_default_bits;
973 state = osb->local_alloc_state;
974 spin_unlock(&osb->osb_lock);
979 static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
980 struct ocfs2_alloc_context **ac,
981 struct inode **bitmap_inode,
982 struct buffer_head **bitmap_bh)
986 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
994 (*ac)->ac_bits_wanted = osb->local_alloc_default_bits;
995 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
996 if (status == -ENOSPC) {
997 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
1001 ocfs2_free_ac_resource(*ac);
1002 memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
1010 *bitmap_inode = (*ac)->ac_inode;
1011 igrab(*bitmap_inode);
1012 *bitmap_bh = (*ac)->ac_bh;
1016 if ((status < 0) && *ac) {
1017 ocfs2_free_alloc_context(*ac);
1026 * pass it the bitmap lock in lock_bh if you have it.
1028 static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
1030 struct ocfs2_alloc_context *ac)
1033 u32 cluster_off, cluster_count;
1034 struct ocfs2_dinode *alloc = NULL;
1035 struct ocfs2_local_alloc *la;
1039 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1040 la = OCFS2_LOCAL_ALLOC(alloc);
1042 if (alloc->id1.bitmap1.i_total)
1043 mlog(0, "asking me to alloc a new window over a non-empty "
1046 mlog(0, "Allocating %u clusters for a new window.\n",
1047 osb->local_alloc_bits);
1049 /* Instruct the allocation code to try the most recently used
1050 * cluster group. We'll re-record the group used this pass
1052 ac->ac_last_group = osb->la_last_gd;
1054 /* we used the generic suballoc reserve function, but we set
1055 * everything up nicely, so there's no reason why we can't use
1056 * the more specific cluster api to claim bits. */
1057 status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits,
1058 &cluster_off, &cluster_count);
1059 if (status == -ENOSPC) {
1062 * Note: We could also try syncing the journal here to
1063 * allow use of any free bits which the current
1064 * transaction can't give us access to. --Mark
1066 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
1070 ac->ac_bits_wanted = osb->local_alloc_default_bits;
1071 status = ocfs2_claim_clusters(osb, handle, ac,
1072 osb->local_alloc_bits,
1075 if (status == -ENOSPC)
1078 * We only shrunk the *minimum* number of in our
1079 * request - it's entirely possible that the allocator
1080 * might give us more than we asked for.
1083 spin_lock(&osb->osb_lock);
1084 osb->local_alloc_bits = cluster_count;
1085 spin_unlock(&osb->osb_lock);
1089 if (status != -ENOSPC)
1094 osb->la_last_gd = ac->ac_last_group;
1096 la->la_bm_off = cpu_to_le32(cluster_off);
1097 alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
1098 /* just in case... In the future when we find space ourselves,
1099 * we don't have to get all contiguous -- but we'll have to
1100 * set all previously used bits in bitmap and update
1101 * la_bits_set before setting the bits in the main bitmap. */
1102 alloc->id1.bitmap1.i_used = 0;
1103 memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
1104 le16_to_cpu(la->la_size));
1106 ocfs2_resmap_restart(&osb->osb_la_resmap, cluster_count,
1107 OCFS2_LOCAL_ALLOC(alloc)->la_bitmap);
1109 mlog(0, "New window allocated:\n");
1110 mlog(0, "window la_bm_off = %u\n",
1111 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
1112 mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
1119 /* Note that we do *NOT* lock the local alloc inode here as
1120 * it's been locked already for us. */
1121 static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
1122 struct inode *local_alloc_inode)
1125 struct buffer_head *main_bm_bh = NULL;
1126 struct inode *main_bm_inode = NULL;
1127 handle_t *handle = NULL;
1128 struct ocfs2_dinode *alloc;
1129 struct ocfs2_dinode *alloc_copy = NULL;
1130 struct ocfs2_alloc_context *ac = NULL;
1134 ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
1136 /* This will lock the main bitmap for us. */
1137 status = ocfs2_local_alloc_reserve_for_window(osb,
1142 if (status != -ENOSPC)
1147 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
1148 if (IS_ERR(handle)) {
1149 status = PTR_ERR(handle);
1155 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1157 /* We want to clear the local alloc before doing anything
1158 * else, so that if we error later during this operation,
1159 * local alloc shutdown won't try to double free main bitmap
1160 * bits. Make a copy so the sync function knows which bits to
1162 alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
1168 memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
1170 status = ocfs2_journal_access_di(handle,
1171 INODE_CACHE(local_alloc_inode),
1172 osb->local_alloc_bh,
1173 OCFS2_JOURNAL_ACCESS_WRITE);
1179 ocfs2_clear_local_alloc(alloc);
1180 ocfs2_journal_dirty(handle, osb->local_alloc_bh);
1182 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
1183 main_bm_inode, main_bm_bh);
1189 status = ocfs2_local_alloc_new_window(osb, handle, ac);
1191 if (status != -ENOSPC)
1196 atomic_inc(&osb->alloc_stats.moves);
1200 ocfs2_commit_trans(osb, handle);
1205 iput(main_bm_inode);
1211 ocfs2_free_alloc_context(ac);