1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Allocation reservations implementation
8 * Some code borrowed from fs/ext3/balloc.c and is:
10 * Copyright (C) 1992, 1993, 1994, 1995
11 * Remy Card (card@masi.ibp.fr)
12 * Laboratoire MASI - Institut Blaise Pascal
13 * Universite Pierre et Marie Curie (Paris VI)
15 * The rest is copyright (C) 2010 Novell. All rights reserved.
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public
19 * License version 2 as published by the Free Software Foundation.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
31 #include <linux/bitops.h>
32 #include <linux/list.h>
34 #define MLOG_MASK_PREFIX ML_RESERVATIONS
35 #include <cluster/masklog.h>
39 #ifdef CONFIG_OCFS2_DEBUG_FS
40 #define OCFS2_CHECK_RESERVATIONS
43 DEFINE_SPINLOCK(resv_lock);
45 #define OCFS2_MIN_RESV_WINDOW_BITS 8
46 #define OCFS2_MAX_RESV_WINDOW_BITS 1024
48 static unsigned int ocfs2_resv_window_bits(struct ocfs2_reservation_map *resmap,
49 struct ocfs2_alloc_reservation *resv)
51 struct ocfs2_super *osb = resmap->m_osb;
54 /* 8, 16, 32, 64, 128, 256, 512, 1024 */
55 bits = 4 << osb->osb_resv_level;
60 static inline unsigned int ocfs2_resv_end(struct ocfs2_alloc_reservation *resv)
63 return resv->r_start + resv->r_len - 1;
67 static inline int ocfs2_resv_empty(struct ocfs2_alloc_reservation *resv)
69 return !!(resv->r_len == 0);
72 static inline int ocfs2_resmap_disabled(struct ocfs2_reservation_map *resmap)
74 if (resmap->m_osb->osb_resv_level == 0)
79 static void ocfs2_dump_resv(struct ocfs2_reservation_map *resmap)
81 struct ocfs2_super *osb = resmap->m_osb;
83 struct ocfs2_alloc_reservation *resv;
86 mlog(ML_NOTICE, "Dumping resmap for device %s. Bitmap length: %u\n",
87 osb->dev_str, resmap->m_bitmap_len);
89 node = rb_first(&resmap->m_reservations);
91 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
93 mlog(ML_NOTICE, "start: %u\tend: %u\tlen: %u\tlast_start: %u"
94 "\tlast_len: %u\n", resv->r_start,
95 ocfs2_resv_end(resv), resv->r_len, resv->r_last_start,
102 mlog(ML_NOTICE, "%d reservations found. LRU follows\n", i);
105 list_for_each_entry(resv, &resmap->m_lru, r_lru) {
106 mlog(ML_NOTICE, "LRU(%d) start: %u\tend: %u\tlen: %u\t"
107 "last_start: %u\tlast_len: %u\n", i, resv->r_start,
108 ocfs2_resv_end(resv), resv->r_len, resv->r_last_start,
115 #ifdef OCFS2_CHECK_RESERVATIONS
116 static int ocfs2_validate_resmap_bits(struct ocfs2_reservation_map *resmap,
118 struct ocfs2_alloc_reservation *resv)
120 char *disk_bitmap = resmap->m_disk_bitmap;
121 unsigned int start = resv->r_start;
122 unsigned int end = ocfs2_resv_end(resv);
124 while (start <= end) {
125 if (ocfs2_test_bit(start, disk_bitmap)) {
127 "reservation %d covers an allocated area "
128 "starting at bit %u!\n", i, start);
137 static void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
139 unsigned int off = 0;
141 struct rb_node *node;
142 struct ocfs2_alloc_reservation *resv;
144 node = rb_first(&resmap->m_reservations);
146 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
148 if (i > 0 && resv->r_start <= off) {
149 mlog(ML_ERROR, "reservation %d has bad start off!\n",
154 if (resv->r_len == 0) {
155 mlog(ML_ERROR, "reservation %d has no length!\n",
160 if (resv->r_start > ocfs2_resv_end(resv)) {
161 mlog(ML_ERROR, "reservation %d has invalid range!\n",
166 if (ocfs2_resv_end(resv) >= resmap->m_bitmap_len) {
167 mlog(ML_ERROR, "reservation %d extends past bitmap!\n",
172 if (ocfs2_validate_resmap_bits(resmap, i, resv))
175 off = ocfs2_resv_end(resv);
176 node = rb_next(node);
183 ocfs2_dump_resv(resmap);
187 static inline void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
193 void ocfs2_resv_init_once(struct ocfs2_alloc_reservation *resv)
195 memset(resv, 0, sizeof(*resv));
196 INIT_LIST_HEAD(&resv->r_lru);
199 void ocfs2_resv_set_type(struct ocfs2_alloc_reservation *resv,
202 BUG_ON(flags & ~OCFS2_RESV_TYPES);
204 resv->r_flags |= flags;
207 int ocfs2_resmap_init(struct ocfs2_super *osb,
208 struct ocfs2_reservation_map *resmap)
210 memset(resmap, 0, sizeof(*resmap));
213 resmap->m_reservations = RB_ROOT;
214 /* m_bitmap_len is initialized to zero by the above memset. */
215 INIT_LIST_HEAD(&resmap->m_lru);
220 static void ocfs2_resv_mark_lru(struct ocfs2_reservation_map *resmap,
221 struct ocfs2_alloc_reservation *resv)
223 assert_spin_locked(&resv_lock);
225 if (!list_empty(&resv->r_lru))
226 list_del_init(&resv->r_lru);
228 list_add_tail(&resv->r_lru, &resmap->m_lru);
231 static void __ocfs2_resv_trunc(struct ocfs2_alloc_reservation *resv)
237 static void ocfs2_resv_remove(struct ocfs2_reservation_map *resmap,
238 struct ocfs2_alloc_reservation *resv)
240 if (resv->r_flags & OCFS2_RESV_FLAG_INUSE) {
241 list_del_init(&resv->r_lru);
242 rb_erase(&resv->r_node, &resmap->m_reservations);
243 resv->r_flags &= ~OCFS2_RESV_FLAG_INUSE;
247 static void __ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
248 struct ocfs2_alloc_reservation *resv)
250 assert_spin_locked(&resv_lock);
252 __ocfs2_resv_trunc(resv);
254 * last_len and last_start no longer make sense if
255 * we're changing the range of our allocations.
257 resv->r_last_len = resv->r_last_start = 0;
259 ocfs2_resv_remove(resmap, resv);
262 /* does nothing if 'resv' is null */
263 void ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
264 struct ocfs2_alloc_reservation *resv)
267 spin_lock(&resv_lock);
268 __ocfs2_resv_discard(resmap, resv);
269 spin_unlock(&resv_lock);
273 static void ocfs2_resmap_clear_all_resv(struct ocfs2_reservation_map *resmap)
275 struct rb_node *node;
276 struct ocfs2_alloc_reservation *resv;
278 assert_spin_locked(&resv_lock);
280 while ((node = rb_last(&resmap->m_reservations)) != NULL) {
281 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
283 __ocfs2_resv_discard(resmap, resv);
287 void ocfs2_resmap_restart(struct ocfs2_reservation_map *resmap,
288 unsigned int clen, char *disk_bitmap)
290 if (ocfs2_resmap_disabled(resmap))
293 spin_lock(&resv_lock);
295 ocfs2_resmap_clear_all_resv(resmap);
296 resmap->m_bitmap_len = clen;
297 resmap->m_disk_bitmap = disk_bitmap;
299 spin_unlock(&resv_lock);
302 void ocfs2_resmap_uninit(struct ocfs2_reservation_map *resmap)
304 /* Does nothing for now. Keep this around for API symmetry */
307 static void ocfs2_resv_insert(struct ocfs2_reservation_map *resmap,
308 struct ocfs2_alloc_reservation *new)
310 struct rb_root *root = &resmap->m_reservations;
311 struct rb_node *parent = NULL;
312 struct rb_node **p = &root->rb_node;
313 struct ocfs2_alloc_reservation *tmp;
315 assert_spin_locked(&resv_lock);
317 mlog(0, "Insert reservation start: %u len: %u\n", new->r_start,
323 tmp = rb_entry(parent, struct ocfs2_alloc_reservation, r_node);
325 if (new->r_start < tmp->r_start) {
329 * This is a good place to check for
330 * overlapping reservations.
332 BUG_ON(ocfs2_resv_end(new) >= tmp->r_start);
333 } else if (new->r_start > ocfs2_resv_end(tmp)) {
336 /* This should never happen! */
337 mlog(ML_ERROR, "Duplicate reservation window!\n");
342 rb_link_node(&new->r_node, parent, p);
343 rb_insert_color(&new->r_node, root);
344 new->r_flags |= OCFS2_RESV_FLAG_INUSE;
346 ocfs2_resv_mark_lru(resmap, new);
348 ocfs2_check_resmap(resmap);
352 * ocfs2_find_resv_lhs() - find the window which contains goal
353 * @resmap: reservation map to search
354 * @goal: which bit to search for
356 * If a window containing that goal is not found, we return the window
357 * which comes before goal. Returns NULL on empty rbtree or no window
360 static struct ocfs2_alloc_reservation *
361 ocfs2_find_resv_lhs(struct ocfs2_reservation_map *resmap, unsigned int goal)
363 struct ocfs2_alloc_reservation *resv = NULL;
364 struct ocfs2_alloc_reservation *prev_resv = NULL;
365 struct rb_node *node = resmap->m_reservations.rb_node;
366 struct rb_node *prev = NULL;
368 assert_spin_locked(&resv_lock);
373 node = rb_first(&resmap->m_reservations);
375 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
377 if (resv->r_start <= goal && ocfs2_resv_end(resv) >= goal)
380 /* Check if we overshot the reservation just before goal? */
381 if (resv->r_start > goal) {
388 node = rb_next(node);
395 * We are given a range within the bitmap, which corresponds to a gap
396 * inside the reservations tree (search_start, search_len). The range
397 * can be anything from the whole bitmap, to a gap between
400 * The start value of *rstart is insignificant.
402 * This function searches the bitmap range starting at search_start
403 * with length csearch_len for a set of contiguous free bits. We try
404 * to find up to 'wanted' bits, but can sometimes return less.
406 * Returns the length of allocation, 0 if no free bits are found.
408 * *cstart and *clen will also be populated with the result.
410 static int ocfs2_resmap_find_free_bits(struct ocfs2_reservation_map *resmap,
412 unsigned int search_start,
413 unsigned int search_len,
414 unsigned int *rstart,
417 void *bitmap = resmap->m_disk_bitmap;
418 unsigned int best_start, best_len = 0;
419 int offset, start, found;
421 mlog(0, "Find %u bits within range (%u, len %u) resmap len: %u\n",
422 wanted, search_start, search_len, resmap->m_bitmap_len);
424 found = best_start = best_len = 0;
426 start = search_start;
427 while ((offset = ocfs2_find_next_zero_bit(bitmap, resmap->m_bitmap_len,
429 /* Search reached end of the region */
430 if (offset >= (search_start + search_len))
433 if (offset == start) {
434 /* we found a zero */
436 /* move start to the next bit to test */
439 /* got a zero after some ones */
443 if (found > best_len) {
445 best_start = start - found;
455 if (best_len >= wanted)
459 *rstart = best_start;
461 mlog(0, "Found start: %u len: %u\n", best_start, best_len);
466 static void __ocfs2_resv_find_window(struct ocfs2_reservation_map *resmap,
467 struct ocfs2_alloc_reservation *resv,
468 unsigned int goal, unsigned int wanted)
470 struct rb_root *root = &resmap->m_reservations;
471 unsigned int gap_start, gap_end, gap_len;
472 struct ocfs2_alloc_reservation *prev_resv, *next_resv;
473 struct rb_node *prev, *next;
474 unsigned int cstart, clen;
475 unsigned int best_start = 0, best_len = 0;
478 * Nasty cases to consider:
481 * - our window should be first in all reservations
482 * - our window should be last in all reservations
483 * - need to make sure we don't go past end of bitmap
486 mlog(0, "resv start: %u resv end: %u goal: %u wanted: %u\n",
487 resv->r_start, ocfs2_resv_end(resv), goal, wanted);
489 assert_spin_locked(&resv_lock);
491 if (RB_EMPTY_ROOT(root)) {
493 * Easiest case - empty tree. We can just take
494 * whatever window of free bits we want.
497 mlog(0, "Empty root\n");
499 clen = ocfs2_resmap_find_free_bits(resmap, wanted, goal,
500 resmap->m_bitmap_len - goal,
504 * This should never happen - the local alloc window
505 * will always have free bits when we're called.
507 BUG_ON(goal == 0 && clen == 0);
512 resv->r_start = cstart;
515 ocfs2_resv_insert(resmap, resv);
519 prev_resv = ocfs2_find_resv_lhs(resmap, goal);
521 if (prev_resv == NULL) {
522 mlog(0, "Goal on LHS of leftmost window\n");
525 * A NULL here means that the search code couldn't
526 * find a window that starts before goal.
528 * However, we can take the first window after goal,
529 * which is also by definition, the leftmost window in
530 * the entire tree. If we can find free bits in the
531 * gap between goal and the LHS window, then the
532 * reservation can safely be placed there.
534 * Otherwise we fall back to a linear search, checking
535 * the gaps in between windows for a place to
539 next = rb_first(root);
540 next_resv = rb_entry(next, struct ocfs2_alloc_reservation,
544 * The search should never return such a window. (see
547 if (next_resv->r_start <= goal) {
548 mlog(ML_ERROR, "goal: %u next_resv: start %u len %u\n",
549 goal, next_resv->r_start, next_resv->r_len);
550 ocfs2_dump_resv(resmap);
554 clen = ocfs2_resmap_find_free_bits(resmap, wanted, goal,
555 next_resv->r_start - goal,
560 if (best_len == wanted)
564 prev_resv = next_resv;
568 prev = &prev_resv->r_node;
570 /* Now we do a linear search for a window, starting at 'prev_rsv' */
572 next = rb_next(prev);
574 mlog(0, "One more resv found in linear search\n");
575 next_resv = rb_entry(next,
576 struct ocfs2_alloc_reservation,
579 gap_start = ocfs2_resv_end(prev_resv) + 1;
580 gap_end = next_resv->r_start - 1;
581 gap_len = gap_end - gap_start + 1;
583 mlog(0, "No next node\n");
585 * We're at the rightmost edge of the
586 * tree. See if a reservation between this
587 * window and the end of the bitmap will work.
589 gap_start = ocfs2_resv_end(prev_resv) + 1;
590 gap_len = resmap->m_bitmap_len - gap_start;
591 gap_end = resmap->m_bitmap_len - 1;
595 * No need to check this gap if we have already found
596 * a larger region of free bits.
598 if (gap_len <= best_len)
601 clen = ocfs2_resmap_find_free_bits(resmap, wanted, gap_start,
602 gap_len, &cstart, &clen);
603 if (clen == wanted) {
607 } else if (clen > best_len) {
617 prev_resv = rb_entry(prev, struct ocfs2_alloc_reservation,
623 resv->r_start = best_start;
624 resv->r_len = best_len;
625 ocfs2_resv_insert(resmap, resv);
629 static void ocfs2_cannibalize_resv(struct ocfs2_reservation_map *resmap,
630 struct ocfs2_alloc_reservation *resv,
633 struct ocfs2_alloc_reservation *lru_resv;
634 int tmpwindow = !!(resv->r_flags & OCFS2_RESV_FLAG_TMP);
635 unsigned int min_bits;
638 min_bits = ocfs2_resv_window_bits(resmap, resv) >> 1;
640 min_bits = wanted; /* We at know the temp window will use all
644 * Take the first reservation off the LRU as our 'target'. We
645 * don't try to be smart about it. There might be a case for
646 * searching based on size but I don't have enough data to be
647 * sure. --Mark (3/16/2010)
649 lru_resv = list_first_entry(&resmap->m_lru,
650 struct ocfs2_alloc_reservation, r_lru);
652 mlog(0, "lru resv: start: %u len: %u end: %u\n", lru_resv->r_start,
653 lru_resv->r_len, ocfs2_resv_end(lru_resv));
656 * Cannibalize (some or all) of the target reservation and
657 * feed it to the current window.
659 if (lru_resv->r_len <= min_bits) {
661 * Discard completely if size is less than or equal to a
662 * reasonable threshold - 50% of window bits for non temporary
665 resv->r_start = lru_resv->r_start;
666 resv->r_len = lru_resv->r_len;
668 __ocfs2_resv_discard(resmap, lru_resv);
674 shrink = lru_resv->r_len / 2;
676 lru_resv->r_len -= shrink;
678 resv->r_start = ocfs2_resv_end(lru_resv) + 1;
679 resv->r_len = shrink;
682 mlog(0, "Reservation now looks like: r_start: %u r_end: %u "
683 "r_len: %u r_last_start: %u r_last_len: %u\n",
684 resv->r_start, ocfs2_resv_end(resv), resv->r_len,
685 resv->r_last_start, resv->r_last_len);
687 ocfs2_resv_insert(resmap, resv);
690 static void ocfs2_resv_find_window(struct ocfs2_reservation_map *resmap,
691 struct ocfs2_alloc_reservation *resv,
694 unsigned int goal = 0;
696 BUG_ON(!ocfs2_resv_empty(resv));
699 * Begin by trying to get a window as close to the previous
700 * one as possible. Using the most recent allocation as a
701 * start goal makes sense.
703 if (resv->r_last_len) {
704 goal = resv->r_last_start + resv->r_last_len;
705 if (goal >= resmap->m_bitmap_len)
709 __ocfs2_resv_find_window(resmap, resv, goal, wanted);
711 /* Search from last alloc didn't work, try once more from beginning. */
712 if (ocfs2_resv_empty(resv) && goal != 0)
713 __ocfs2_resv_find_window(resmap, resv, 0, wanted);
715 if (ocfs2_resv_empty(resv)) {
717 * Still empty? Pull oldest one off the LRU, remove it from
718 * tree, put this one in it's place.
720 ocfs2_cannibalize_resv(resmap, resv, wanted);
723 BUG_ON(ocfs2_resv_empty(resv));
726 int ocfs2_resmap_resv_bits(struct ocfs2_reservation_map *resmap,
727 struct ocfs2_alloc_reservation *resv,
728 int *cstart, int *clen)
730 unsigned int wanted = *clen;
732 if (resv == NULL || ocfs2_resmap_disabled(resmap))
735 spin_lock(&resv_lock);
738 * We don't want to over-allocate for temporary
739 * windows. Otherwise, we run the risk of fragmenting the
742 wanted = ocfs2_resv_window_bits(resmap, resv);
743 if ((resv->r_flags & OCFS2_RESV_FLAG_TMP) || wanted < *clen)
746 if (ocfs2_resv_empty(resv)) {
747 mlog(0, "empty reservation, find new window\n");
750 * Try to get a window here. If it works, we must fall
751 * through and test the bitmap . This avoids some
752 * ping-ponging of windows due to non-reserved space
753 * being allocation before we initialize a window for
756 ocfs2_resv_find_window(resmap, resv, wanted);
759 BUG_ON(ocfs2_resv_empty(resv));
761 *cstart = resv->r_start;
764 spin_unlock(&resv_lock);
769 ocfs2_adjust_resv_from_alloc(struct ocfs2_reservation_map *resmap,
770 struct ocfs2_alloc_reservation *resv,
771 unsigned int start, unsigned int end)
773 unsigned int lhs = 0, rhs = 0;
775 BUG_ON(start < resv->r_start);
778 * Completely used? We can remove it then.
780 if (ocfs2_resv_end(resv) <= end && resv->r_start >= start) {
781 __ocfs2_resv_discard(resmap, resv);
785 if (end < ocfs2_resv_end(resv))
786 rhs = end - ocfs2_resv_end(resv);
788 if (start > resv->r_start)
789 lhs = start - resv->r_start;
792 * This should have been trapped above. At the very least, rhs
793 * should be non zero.
795 BUG_ON(rhs == 0 && lhs == 0);
798 unsigned int old_end = ocfs2_resv_end(resv);
800 resv->r_start = end + 1;
801 resv->r_len = old_end - resv->r_start + 1;
803 resv->r_len = start - resv->r_start;
807 void ocfs2_resmap_claimed_bits(struct ocfs2_reservation_map *resmap,
808 struct ocfs2_alloc_reservation *resv,
809 u32 cstart, u32 clen)
811 unsigned int cend = cstart + clen - 1;
813 if (resmap == NULL || ocfs2_resmap_disabled(resmap))
819 spin_lock(&resv_lock);
821 mlog(0, "claim bits: cstart: %u cend: %u clen: %u r_start: %u "
822 "r_end: %u r_len: %u, r_last_start: %u r_last_len: %u\n",
823 cstart, cend, clen, resv->r_start, ocfs2_resv_end(resv),
824 resv->r_len, resv->r_last_start, resv->r_last_len);
826 BUG_ON(cstart < resv->r_start);
827 BUG_ON(cstart > ocfs2_resv_end(resv));
828 BUG_ON(cend > ocfs2_resv_end(resv));
830 ocfs2_adjust_resv_from_alloc(resmap, resv, cstart, cend);
831 resv->r_last_start = cstart;
832 resv->r_last_len = clen;
835 * May have been discarded above from
836 * ocfs2_adjust_resv_from_alloc().
838 if (!ocfs2_resv_empty(resv))
839 ocfs2_resv_mark_lru(resmap, resv);
841 mlog(0, "Reservation now looks like: r_start: %u r_end: %u "
842 "r_len: %u r_last_start: %u r_last_len: %u\n",
843 resv->r_start, ocfs2_resv_end(resv), resv->r_len,
844 resv->r_last_start, resv->r_last_len);
846 ocfs2_check_resmap(resmap);
848 spin_unlock(&resv_lock);