aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ocfs2/localalloc.c
diff options
context:
space:
mode:
authorMark Fasheh <mfasheh@suse.com>2008-07-28 21:02:53 -0400
committerMark Fasheh <mfasheh@suse.com>2008-10-13 16:57:57 -0400
commit9c7af40b210e87f8fddd97b0badc0a352862234a (patch)
treedeb248e5295439b96ad9bd1789e8512f93803142 /fs/ocfs2/localalloc.c
parentebcee4b5c9136096f64ee6f691a013d7c0a4bc34 (diff)
ocfs2: throttle back local alloc when low on disk space
Ocfs2's local allocator disables itself for the duration of a mount point when it has trouble allocating a large enough area from the primary bitmap. That can cause performance problems, especially for disks which were only temporarily full or fragmented. This patch allows for the allocator to shrink it's window first, before being disabled. Later, it can also be re-enabled so that any performance drop is minimized. To do this, we allow the value of osb->local_alloc_bits to be shrunk when needed. The default value is recorded in a mostly read-only variable so that we can re-initialize when required. Locking had to be updated so that we could protect changes to local_alloc_bits. Mostly this involves protecting various local alloc values with the osb spinlock. A new state is also added, OCFS2_LA_THROTTLED, which is used when the local allocator is has shrunk, but is not disabled. If the available space dips below 1 megabyte, the local alloc file is disabled. In either case, local alloc is re-enabled 30 seconds after the event, or when an appropriate amount of bits is seen in the primary bitmap. Signed-off-by: Mark Fasheh <mfasheh@suse.com>
Diffstat (limited to 'fs/ocfs2/localalloc.c')
-rw-r--r--fs/ocfs2/localalloc.c198
1 files changed, 186 insertions, 12 deletions
diff --git a/fs/ocfs2/localalloc.c b/fs/ocfs2/localalloc.c
index b05ce6642919..f71658adddb5 100644
--- a/fs/ocfs2/localalloc.c
+++ b/fs/ocfs2/localalloc.c
@@ -73,16 +73,51 @@ static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
73static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb, 73static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
74 struct inode *local_alloc_inode); 74 struct inode *local_alloc_inode);
75 75
76static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb)
77{
78 return (osb->local_alloc_state == OCFS2_LA_THROTTLED ||
79 osb->local_alloc_state == OCFS2_LA_ENABLED);
80}
81
82void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb,
83 unsigned int num_clusters)
84{
85 spin_lock(&osb->osb_lock);
86 if (osb->local_alloc_state == OCFS2_LA_DISABLED ||
87 osb->local_alloc_state == OCFS2_LA_THROTTLED)
88 if (num_clusters >= osb->local_alloc_default_bits) {
89 cancel_delayed_work(&osb->la_enable_wq);
90 osb->local_alloc_state = OCFS2_LA_ENABLED;
91 }
92 spin_unlock(&osb->osb_lock);
93}
94
95void ocfs2_la_enable_worker(struct work_struct *work)
96{
97 struct ocfs2_super *osb =
98 container_of(work, struct ocfs2_super,
99 la_enable_wq.work);
100 spin_lock(&osb->osb_lock);
101 osb->local_alloc_state = OCFS2_LA_ENABLED;
102 spin_unlock(&osb->osb_lock);
103}
104
76/* 105/*
77 * Tell us whether a given allocation should use the local alloc 106 * Tell us whether a given allocation should use the local alloc
78 * file. Otherwise, it has to go to the main bitmap. 107 * file. Otherwise, it has to go to the main bitmap.
108 *
109 * This function does semi-dirty reads of local alloc size and state!
110 * This is ok however, as the values are re-checked once under mutex.
79 */ 111 */
80int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits) 112int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
81{ 113{
82 int la_bits = osb->local_alloc_bits;
83 int ret = 0; 114 int ret = 0;
115 int la_bits;
116
117 spin_lock(&osb->osb_lock);
118 la_bits = osb->local_alloc_bits;
84 119
85 if (osb->local_alloc_state != OCFS2_LA_ENABLED) 120 if (!ocfs2_la_state_enabled(osb))
86 goto bail; 121 goto bail;
87 122
88 /* la_bits should be at least twice the size (in clusters) of 123 /* la_bits should be at least twice the size (in clusters) of
@@ -96,6 +131,7 @@ int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
96bail: 131bail:
97 mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n", 132 mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n",
98 osb->local_alloc_state, (unsigned long long)bits, la_bits, ret); 133 osb->local_alloc_state, (unsigned long long)bits, la_bits, ret);
134 spin_unlock(&osb->osb_lock);
99 return ret; 135 return ret;
100} 136}
101 137
@@ -208,6 +244,9 @@ void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
208 244
209 mlog_entry_void(); 245 mlog_entry_void();
210 246
247 cancel_delayed_work(&osb->la_enable_wq);
248 flush_workqueue(ocfs2_wq);
249
211 if (osb->local_alloc_state == OCFS2_LA_UNUSED) 250 if (osb->local_alloc_state == OCFS2_LA_UNUSED)
212 goto out; 251 goto out;
213 252
@@ -445,7 +484,7 @@ out:
445} 484}
446 485
447/* 486/*
448 * make sure we've got at least bitswanted contiguous bits in the 487 * make sure we've got at least bits_wanted contiguous bits in the
449 * local alloc. You lose them when you drop i_mutex. 488 * local alloc. You lose them when you drop i_mutex.
450 * 489 *
451 * We will add ourselves to the transaction passed in, but may start 490 * We will add ourselves to the transaction passed in, but may start
@@ -476,16 +515,18 @@ int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
476 515
477 mutex_lock(&local_alloc_inode->i_mutex); 516 mutex_lock(&local_alloc_inode->i_mutex);
478 517
479 if (osb->local_alloc_state != OCFS2_LA_ENABLED) { 518 /*
480 status = -ENOSPC; 519 * We must double check state and allocator bits because
481 goto bail; 520 * another process may have changed them while holding i_mutex.
482 } 521 */
483 522 spin_lock(&osb->osb_lock);
484 if (bits_wanted > osb->local_alloc_bits) { 523 if (!ocfs2_la_state_enabled(osb) ||
485 mlog(0, "Asking for more than my max window size!\n"); 524 (bits_wanted > osb->local_alloc_bits)) {
525 spin_unlock(&osb->osb_lock);
486 status = -ENOSPC; 526 status = -ENOSPC;
487 goto bail; 527 goto bail;
488 } 528 }
529 spin_unlock(&osb->osb_lock);
489 530
490 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; 531 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
491 532
@@ -513,6 +554,21 @@ int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
513 mlog_errno(status); 554 mlog_errno(status);
514 goto bail; 555 goto bail;
515 } 556 }
557
558 /*
559 * Under certain conditions, the window slide code
560 * might have reduced the number of bits available or
561 * disabled the the local alloc entirely. Re-check
562 * here and return -ENOSPC if necessary.
563 */
564 status = -ENOSPC;
565 if (!ocfs2_la_state_enabled(osb))
566 goto bail;
567
568 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
569 le32_to_cpu(alloc->id1.bitmap1.i_used);
570 if (bits_wanted > free_bits)
571 goto bail;
516 } 572 }
517 573
518 ac->ac_inode = local_alloc_inode; 574 ac->ac_inode = local_alloc_inode;
@@ -780,6 +836,85 @@ bail:
780 return status; 836 return status;
781} 837}
782 838
839enum ocfs2_la_event {
840 OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */
841 OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has
842 * enough bits theoretically
843 * free, but a contiguous
844 * allocation could not be
845 * found. */
846 OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have
847 * enough bits free to satisfy
848 * our request. */
849};
850#define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
851/*
852 * Given an event, calculate the size of our next local alloc window.
853 *
854 * This should always be called under i_mutex of the local alloc inode
855 * so that local alloc disabling doesn't race with processes trying to
856 * use the allocator.
857 *
858 * Returns the state which the local alloc was left in. This value can
859 * be ignored by some paths.
860 */
861static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
862 enum ocfs2_la_event event)
863{
864 unsigned int bits;
865 int state;
866
867 spin_lock(&osb->osb_lock);
868 if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
869 WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
870 goto out_unlock;
871 }
872
873 /*
874 * ENOSPC and fragmentation are treated similarly for now.
875 */
876 if (event == OCFS2_LA_EVENT_ENOSPC ||
877 event == OCFS2_LA_EVENT_FRAGMENTED) {
878 /*
879 * We ran out of contiguous space in the primary
880 * bitmap. Drastically reduce the number of bits used
881 * by local alloc until we have to disable it.
882 */
883 bits = osb->local_alloc_bits >> 1;
884 if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
885 /*
886 * By setting state to THROTTLED, we'll keep
887 * the number of local alloc bits used down
888 * until an event occurs which would give us
889 * reason to assume the bitmap situation might
890 * have changed.
891 */
892 osb->local_alloc_state = OCFS2_LA_THROTTLED;
893 osb->local_alloc_bits = bits;
894 } else {
895 osb->local_alloc_state = OCFS2_LA_DISABLED;
896 }
897 queue_delayed_work(ocfs2_wq, &osb->la_enable_wq,
898 OCFS2_LA_ENABLE_INTERVAL);
899 goto out_unlock;
900 }
901
902 /*
903 * Don't increase the size of the local alloc window until we
904 * know we might be able to fulfill the request. Otherwise, we
905 * risk bouncing around the global bitmap during periods of
906 * low space.
907 */
908 if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
909 osb->local_alloc_bits = osb->local_alloc_default_bits;
910
911out_unlock:
912 state = osb->local_alloc_state;
913 spin_unlock(&osb->osb_lock);
914
915 return state;
916}
917
783static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb, 918static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
784 struct ocfs2_alloc_context **ac, 919 struct ocfs2_alloc_context **ac,
785 struct inode **bitmap_inode, 920 struct inode **bitmap_inode,
@@ -794,12 +929,21 @@ static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
794 goto bail; 929 goto bail;
795 } 930 }
796 931
932retry_enospc:
797 (*ac)->ac_bits_wanted = osb->local_alloc_bits; 933 (*ac)->ac_bits_wanted = osb->local_alloc_bits;
798 934
799 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac); 935 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
936 if (status == -ENOSPC) {
937 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
938 OCFS2_LA_DISABLED)
939 goto bail;
940
941 ocfs2_free_ac_resource(*ac);
942 memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
943 goto retry_enospc;
944 }
800 if (status < 0) { 945 if (status < 0) {
801 if (status != -ENOSPC) 946 mlog_errno(status);
802 mlog_errno(status);
803 goto bail; 947 goto bail;
804 } 948 }
805 949
@@ -852,6 +996,34 @@ static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
852 * the more specific cluster api to claim bits. */ 996 * the more specific cluster api to claim bits. */
853 status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits, 997 status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits,
854 &cluster_off, &cluster_count); 998 &cluster_off, &cluster_count);
999 if (status == -ENOSPC) {
1000retry_enospc:
1001 /*
1002 * Note: We could also try syncing the journal here to
1003 * allow use of any free bits which the current
1004 * transaction can't give us access to. --Mark
1005 */
1006 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
1007 OCFS2_LA_DISABLED)
1008 goto bail;
1009
1010 status = ocfs2_claim_clusters(osb, handle, ac,
1011 osb->local_alloc_bits,
1012 &cluster_off,
1013 &cluster_count);
1014 if (status == -ENOSPC)
1015 goto retry_enospc;
1016 /*
1017 * We only shrunk the *minimum* number of in our
1018 * request - it's entirely possible that the allocator
1019 * might give us more than we asked for.
1020 */
1021 if (status == 0) {
1022 spin_lock(&osb->osb_lock);
1023 osb->local_alloc_bits = cluster_count;
1024 spin_unlock(&osb->osb_lock);
1025 }
1026 }
855 if (status < 0) { 1027 if (status < 0) {
856 if (status != -ENOSPC) 1028 if (status != -ENOSPC)
857 mlog_errno(status); 1029 mlog_errno(status);
@@ -895,6 +1067,8 @@ static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
895 1067
896 mlog_entry_void(); 1068 mlog_entry_void();
897 1069
1070 ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
1071
898 /* This will lock the main bitmap for us. */ 1072 /* This will lock the main bitmap for us. */
899 status = ocfs2_local_alloc_reserve_for_window(osb, 1073 status = ocfs2_local_alloc_reserve_for_window(osb,
900 &ac, 1074 &ac,