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/highmem.h>
30 #include <linux/bitops.h>
31 #include <linux/list.h>
33 #define MLOG_MASK_PREFIX ML_RESERVATIONS
34 #include <cluster/masklog.h>
38 #ifdef CONFIG_OCFS2_DEBUG_FS
39 #define OCFS2_CHECK_RESERVATIONS
42 DEFINE_SPINLOCK(resv_lock
);
44 #define OCFS2_MIN_RESV_WINDOW_BITS 8
45 #define OCFS2_MAX_RESV_WINDOW_BITS 1024
47 int ocfs2_dir_resv_allowed(struct ocfs2_super
*osb
)
49 return (osb
->osb_resv_level
&& osb
->osb_dir_resv_level
);
52 static unsigned int ocfs2_resv_window_bits(struct ocfs2_reservation_map
*resmap
,
53 struct ocfs2_alloc_reservation
*resv
)
55 struct ocfs2_super
*osb
= resmap
->m_osb
;
58 if (!(resv
->r_flags
& OCFS2_RESV_FLAG_DIR
)) {
59 /* 8, 16, 32, 64, 128, 256, 512, 1024 */
60 bits
= 4 << osb
->osb_resv_level
;
62 bits
= 4 << osb
->osb_dir_resv_level
;
67 static inline unsigned int ocfs2_resv_end(struct ocfs2_alloc_reservation
*resv
)
70 return resv
->r_start
+ resv
->r_len
- 1;
74 static inline int ocfs2_resv_empty(struct ocfs2_alloc_reservation
*resv
)
76 return !!(resv
->r_len
== 0);
79 static inline int ocfs2_resmap_disabled(struct ocfs2_reservation_map
*resmap
)
81 if (resmap
->m_osb
->osb_resv_level
== 0)
86 static void ocfs2_dump_resv(struct ocfs2_reservation_map
*resmap
)
88 struct ocfs2_super
*osb
= resmap
->m_osb
;
90 struct ocfs2_alloc_reservation
*resv
;
93 mlog(ML_NOTICE
, "Dumping resmap for device %s. Bitmap length: %u\n",
94 osb
->dev_str
, resmap
->m_bitmap_len
);
96 node
= rb_first(&resmap
->m_reservations
);
98 resv
= rb_entry(node
, struct ocfs2_alloc_reservation
, r_node
);
100 mlog(ML_NOTICE
, "start: %u\tend: %u\tlen: %u\tlast_start: %u"
101 "\tlast_len: %u\n", resv
->r_start
,
102 ocfs2_resv_end(resv
), resv
->r_len
, resv
->r_last_start
,
105 node
= rb_next(node
);
109 mlog(ML_NOTICE
, "%d reservations found. LRU follows\n", i
);
112 list_for_each_entry(resv
, &resmap
->m_lru
, r_lru
) {
113 mlog(ML_NOTICE
, "LRU(%d) start: %u\tend: %u\tlen: %u\t"
114 "last_start: %u\tlast_len: %u\n", i
, resv
->r_start
,
115 ocfs2_resv_end(resv
), resv
->r_len
, resv
->r_last_start
,
122 #ifdef OCFS2_CHECK_RESERVATIONS
123 static int ocfs2_validate_resmap_bits(struct ocfs2_reservation_map
*resmap
,
125 struct ocfs2_alloc_reservation
*resv
)
127 char *disk_bitmap
= resmap
->m_disk_bitmap
;
128 unsigned int start
= resv
->r_start
;
129 unsigned int end
= ocfs2_resv_end(resv
);
131 while (start
<= end
) {
132 if (ocfs2_test_bit(start
, disk_bitmap
)) {
134 "reservation %d covers an allocated area "
135 "starting at bit %u!\n", i
, start
);
144 static void ocfs2_check_resmap(struct ocfs2_reservation_map
*resmap
)
146 unsigned int off
= 0;
148 struct rb_node
*node
;
149 struct ocfs2_alloc_reservation
*resv
;
151 node
= rb_first(&resmap
->m_reservations
);
153 resv
= rb_entry(node
, struct ocfs2_alloc_reservation
, r_node
);
155 if (i
> 0 && resv
->r_start
<= off
) {
156 mlog(ML_ERROR
, "reservation %d has bad start off!\n",
161 if (resv
->r_len
== 0) {
162 mlog(ML_ERROR
, "reservation %d has no length!\n",
167 if (resv
->r_start
> ocfs2_resv_end(resv
)) {
168 mlog(ML_ERROR
, "reservation %d has invalid range!\n",
173 if (ocfs2_resv_end(resv
) >= resmap
->m_bitmap_len
) {
174 mlog(ML_ERROR
, "reservation %d extends past bitmap!\n",
179 if (ocfs2_validate_resmap_bits(resmap
, i
, resv
))
182 off
= ocfs2_resv_end(resv
);
183 node
= rb_next(node
);
190 ocfs2_dump_resv(resmap
);
194 static inline void ocfs2_check_resmap(struct ocfs2_reservation_map
*resmap
)
200 void ocfs2_resv_init_once(struct ocfs2_alloc_reservation
*resv
)
202 memset(resv
, 0, sizeof(*resv
));
203 INIT_LIST_HEAD(&resv
->r_lru
);
206 void ocfs2_resv_set_type(struct ocfs2_alloc_reservation
*resv
,
209 BUG_ON(flags
& ~OCFS2_RESV_TYPES
);
211 resv
->r_flags
|= flags
;
214 int ocfs2_resmap_init(struct ocfs2_super
*osb
,
215 struct ocfs2_reservation_map
*resmap
)
217 memset(resmap
, 0, sizeof(*resmap
));
220 resmap
->m_reservations
= RB_ROOT
;
221 /* m_bitmap_len is initialized to zero by the above memset. */
222 INIT_LIST_HEAD(&resmap
->m_lru
);
227 static void ocfs2_resv_mark_lru(struct ocfs2_reservation_map
*resmap
,
228 struct ocfs2_alloc_reservation
*resv
)
230 assert_spin_locked(&resv_lock
);
232 if (!list_empty(&resv
->r_lru
))
233 list_del_init(&resv
->r_lru
);
235 list_add_tail(&resv
->r_lru
, &resmap
->m_lru
);
238 static void __ocfs2_resv_trunc(struct ocfs2_alloc_reservation
*resv
)
244 static void ocfs2_resv_remove(struct ocfs2_reservation_map
*resmap
,
245 struct ocfs2_alloc_reservation
*resv
)
247 if (resv
->r_flags
& OCFS2_RESV_FLAG_INUSE
) {
248 list_del_init(&resv
->r_lru
);
249 rb_erase(&resv
->r_node
, &resmap
->m_reservations
);
250 resv
->r_flags
&= ~OCFS2_RESV_FLAG_INUSE
;
254 static void __ocfs2_resv_discard(struct ocfs2_reservation_map
*resmap
,
255 struct ocfs2_alloc_reservation
*resv
)
257 assert_spin_locked(&resv_lock
);
259 __ocfs2_resv_trunc(resv
);
261 * last_len and last_start no longer make sense if
262 * we're changing the range of our allocations.
264 resv
->r_last_len
= resv
->r_last_start
= 0;
266 ocfs2_resv_remove(resmap
, resv
);
269 /* does nothing if 'resv' is null */
270 void ocfs2_resv_discard(struct ocfs2_reservation_map
*resmap
,
271 struct ocfs2_alloc_reservation
*resv
)
274 spin_lock(&resv_lock
);
275 __ocfs2_resv_discard(resmap
, resv
);
276 spin_unlock(&resv_lock
);
280 static void ocfs2_resmap_clear_all_resv(struct ocfs2_reservation_map
*resmap
)
282 struct rb_node
*node
;
283 struct ocfs2_alloc_reservation
*resv
;
285 assert_spin_locked(&resv_lock
);
287 while ((node
= rb_last(&resmap
->m_reservations
)) != NULL
) {
288 resv
= rb_entry(node
, struct ocfs2_alloc_reservation
, r_node
);
290 __ocfs2_resv_discard(resmap
, resv
);
294 void ocfs2_resmap_restart(struct ocfs2_reservation_map
*resmap
,
295 unsigned int clen
, char *disk_bitmap
)
297 if (ocfs2_resmap_disabled(resmap
))
300 spin_lock(&resv_lock
);
302 ocfs2_resmap_clear_all_resv(resmap
);
303 resmap
->m_bitmap_len
= clen
;
304 resmap
->m_disk_bitmap
= disk_bitmap
;
306 spin_unlock(&resv_lock
);
309 void ocfs2_resmap_uninit(struct ocfs2_reservation_map
*resmap
)
311 /* Does nothing for now. Keep this around for API symmetry */
314 static void ocfs2_resv_insert(struct ocfs2_reservation_map
*resmap
,
315 struct ocfs2_alloc_reservation
*new)
317 struct rb_root
*root
= &resmap
->m_reservations
;
318 struct rb_node
*parent
= NULL
;
319 struct rb_node
**p
= &root
->rb_node
;
320 struct ocfs2_alloc_reservation
*tmp
;
322 assert_spin_locked(&resv_lock
);
324 mlog(0, "Insert reservation start: %u len: %u\n", new->r_start
,
330 tmp
= rb_entry(parent
, struct ocfs2_alloc_reservation
, r_node
);
332 if (new->r_start
< tmp
->r_start
) {
336 * This is a good place to check for
337 * overlapping reservations.
339 BUG_ON(ocfs2_resv_end(new) >= tmp
->r_start
);
340 } else if (new->r_start
> ocfs2_resv_end(tmp
)) {
343 /* This should never happen! */
344 mlog(ML_ERROR
, "Duplicate reservation window!\n");
349 rb_link_node(&new->r_node
, parent
, p
);
350 rb_insert_color(&new->r_node
, root
);
351 new->r_flags
|= OCFS2_RESV_FLAG_INUSE
;
353 ocfs2_resv_mark_lru(resmap
, new);
355 ocfs2_check_resmap(resmap
);
359 * ocfs2_find_resv_lhs() - find the window which contains goal
360 * @resmap: reservation map to search
361 * @goal: which bit to search for
363 * If a window containing that goal is not found, we return the window
364 * which comes before goal. Returns NULL on empty rbtree or no window
367 static struct ocfs2_alloc_reservation
*
368 ocfs2_find_resv_lhs(struct ocfs2_reservation_map
*resmap
, unsigned int goal
)
370 struct ocfs2_alloc_reservation
*resv
= NULL
;
371 struct ocfs2_alloc_reservation
*prev_resv
= NULL
;
372 struct rb_node
*node
= resmap
->m_reservations
.rb_node
;
374 assert_spin_locked(&resv_lock
);
379 node
= rb_first(&resmap
->m_reservations
);
381 resv
= rb_entry(node
, struct ocfs2_alloc_reservation
, r_node
);
383 if (resv
->r_start
<= goal
&& ocfs2_resv_end(resv
) >= goal
)
386 /* Check if we overshot the reservation just before goal? */
387 if (resv
->r_start
> goal
) {
393 node
= rb_next(node
);
400 * We are given a range within the bitmap, which corresponds to a gap
401 * inside the reservations tree (search_start, search_len). The range
402 * can be anything from the whole bitmap, to a gap between
405 * The start value of *rstart is insignificant.
407 * This function searches the bitmap range starting at search_start
408 * with length search_len for a set of contiguous free bits. We try
409 * to find up to 'wanted' bits, but can sometimes return less.
411 * Returns the length of allocation, 0 if no free bits are found.
413 * *cstart and *clen will also be populated with the result.
415 static int ocfs2_resmap_find_free_bits(struct ocfs2_reservation_map
*resmap
,
417 unsigned int search_start
,
418 unsigned int search_len
,
419 unsigned int *rstart
,
422 void *bitmap
= resmap
->m_disk_bitmap
;
423 unsigned int best_start
, best_len
= 0;
424 int offset
, start
, found
;
426 mlog(0, "Find %u bits within range (%u, len %u) resmap len: %u\n",
427 wanted
, search_start
, search_len
, resmap
->m_bitmap_len
);
429 found
= best_start
= best_len
= 0;
431 start
= search_start
;
432 while ((offset
= ocfs2_find_next_zero_bit(bitmap
, resmap
->m_bitmap_len
,
434 /* Search reached end of the region */
435 if (offset
>= (search_start
+ search_len
))
438 if (offset
== start
) {
439 /* we found a zero */
441 /* move start to the next bit to test */
444 /* got a zero after some ones */
448 if (found
> best_len
) {
450 best_start
= start
- found
;
460 if (best_len
>= wanted
)
464 *rstart
= best_start
;
466 mlog(0, "Found start: %u len: %u\n", best_start
, best_len
);
471 static void __ocfs2_resv_find_window(struct ocfs2_reservation_map
*resmap
,
472 struct ocfs2_alloc_reservation
*resv
,
473 unsigned int goal
, unsigned int wanted
)
475 struct rb_root
*root
= &resmap
->m_reservations
;
476 unsigned int gap_start
, gap_end
, gap_len
;
477 struct ocfs2_alloc_reservation
*prev_resv
, *next_resv
;
478 struct rb_node
*prev
, *next
;
479 unsigned int cstart
, clen
;
480 unsigned int best_start
= 0, best_len
= 0;
483 * Nasty cases to consider:
486 * - our window should be first in all reservations
487 * - our window should be last in all reservations
488 * - need to make sure we don't go past end of bitmap
491 mlog(0, "resv start: %u resv end: %u goal: %u wanted: %u\n",
492 resv
->r_start
, ocfs2_resv_end(resv
), goal
, wanted
);
494 assert_spin_locked(&resv_lock
);
496 if (RB_EMPTY_ROOT(root
)) {
498 * Easiest case - empty tree. We can just take
499 * whatever window of free bits we want.
502 mlog(0, "Empty root\n");
504 clen
= ocfs2_resmap_find_free_bits(resmap
, wanted
, goal
,
505 resmap
->m_bitmap_len
- goal
,
509 * This should never happen - the local alloc window
510 * will always have free bits when we're called.
512 BUG_ON(goal
== 0 && clen
== 0);
517 resv
->r_start
= cstart
;
520 ocfs2_resv_insert(resmap
, resv
);
524 prev_resv
= ocfs2_find_resv_lhs(resmap
, goal
);
526 if (prev_resv
== NULL
) {
527 mlog(0, "Goal on LHS of leftmost window\n");
530 * A NULL here means that the search code couldn't
531 * find a window that starts before goal.
533 * However, we can take the first window after goal,
534 * which is also by definition, the leftmost window in
535 * the entire tree. If we can find free bits in the
536 * gap between goal and the LHS window, then the
537 * reservation can safely be placed there.
539 * Otherwise we fall back to a linear search, checking
540 * the gaps in between windows for a place to
544 next
= rb_first(root
);
545 next_resv
= rb_entry(next
, struct ocfs2_alloc_reservation
,
549 * The search should never return such a window. (see
552 if (next_resv
->r_start
<= goal
) {
553 mlog(ML_ERROR
, "goal: %u next_resv: start %u len %u\n",
554 goal
, next_resv
->r_start
, next_resv
->r_len
);
555 ocfs2_dump_resv(resmap
);
559 clen
= ocfs2_resmap_find_free_bits(resmap
, wanted
, goal
,
560 next_resv
->r_start
- goal
,
565 if (best_len
== wanted
)
569 prev_resv
= next_resv
;
573 prev
= &prev_resv
->r_node
;
575 /* Now we do a linear search for a window, starting at 'prev_rsv' */
577 next
= rb_next(prev
);
579 mlog(0, "One more resv found in linear search\n");
580 next_resv
= rb_entry(next
,
581 struct ocfs2_alloc_reservation
,
584 gap_start
= ocfs2_resv_end(prev_resv
) + 1;
585 gap_end
= next_resv
->r_start
- 1;
586 gap_len
= gap_end
- gap_start
+ 1;
588 mlog(0, "No next node\n");
590 * We're at the rightmost edge of the
591 * tree. See if a reservation between this
592 * window and the end of the bitmap will work.
594 gap_start
= ocfs2_resv_end(prev_resv
) + 1;
595 gap_len
= resmap
->m_bitmap_len
- gap_start
;
596 gap_end
= resmap
->m_bitmap_len
- 1;
600 * No need to check this gap if we have already found
601 * a larger region of free bits.
603 if (gap_len
<= best_len
)
606 clen
= ocfs2_resmap_find_free_bits(resmap
, wanted
, gap_start
,
607 gap_len
, &cstart
, &clen
);
608 if (clen
== wanted
) {
612 } else if (clen
> best_len
) {
622 prev_resv
= rb_entry(prev
, struct ocfs2_alloc_reservation
,
628 resv
->r_start
= best_start
;
629 resv
->r_len
= best_len
;
630 ocfs2_resv_insert(resmap
, resv
);
634 static void ocfs2_cannibalize_resv(struct ocfs2_reservation_map
*resmap
,
635 struct ocfs2_alloc_reservation
*resv
,
638 struct ocfs2_alloc_reservation
*lru_resv
;
639 int tmpwindow
= !!(resv
->r_flags
& OCFS2_RESV_FLAG_TMP
);
640 unsigned int min_bits
;
643 min_bits
= ocfs2_resv_window_bits(resmap
, resv
) >> 1;
645 min_bits
= wanted
; /* We at know the temp window will use all
649 * Take the first reservation off the LRU as our 'target'. We
650 * don't try to be smart about it. There might be a case for
651 * searching based on size but I don't have enough data to be
652 * sure. --Mark (3/16/2010)
654 lru_resv
= list_first_entry(&resmap
->m_lru
,
655 struct ocfs2_alloc_reservation
, r_lru
);
657 mlog(0, "lru resv: start: %u len: %u end: %u\n", lru_resv
->r_start
,
658 lru_resv
->r_len
, ocfs2_resv_end(lru_resv
));
661 * Cannibalize (some or all) of the target reservation and
662 * feed it to the current window.
664 if (lru_resv
->r_len
<= min_bits
) {
666 * Discard completely if size is less than or equal to a
667 * reasonable threshold - 50% of window bits for non temporary
670 resv
->r_start
= lru_resv
->r_start
;
671 resv
->r_len
= lru_resv
->r_len
;
673 __ocfs2_resv_discard(resmap
, lru_resv
);
679 shrink
= lru_resv
->r_len
/ 2;
681 lru_resv
->r_len
-= shrink
;
683 resv
->r_start
= ocfs2_resv_end(lru_resv
) + 1;
684 resv
->r_len
= shrink
;
687 mlog(0, "Reservation now looks like: r_start: %u r_end: %u "
688 "r_len: %u r_last_start: %u r_last_len: %u\n",
689 resv
->r_start
, ocfs2_resv_end(resv
), resv
->r_len
,
690 resv
->r_last_start
, resv
->r_last_len
);
692 ocfs2_resv_insert(resmap
, resv
);
695 static void ocfs2_resv_find_window(struct ocfs2_reservation_map
*resmap
,
696 struct ocfs2_alloc_reservation
*resv
,
699 unsigned int goal
= 0;
701 BUG_ON(!ocfs2_resv_empty(resv
));
704 * Begin by trying to get a window as close to the previous
705 * one as possible. Using the most recent allocation as a
706 * start goal makes sense.
708 if (resv
->r_last_len
) {
709 goal
= resv
->r_last_start
+ resv
->r_last_len
;
710 if (goal
>= resmap
->m_bitmap_len
)
714 __ocfs2_resv_find_window(resmap
, resv
, goal
, wanted
);
716 /* Search from last alloc didn't work, try once more from beginning. */
717 if (ocfs2_resv_empty(resv
) && goal
!= 0)
718 __ocfs2_resv_find_window(resmap
, resv
, 0, wanted
);
720 if (ocfs2_resv_empty(resv
)) {
722 * Still empty? Pull oldest one off the LRU, remove it from
723 * tree, put this one in it's place.
725 ocfs2_cannibalize_resv(resmap
, resv
, wanted
);
728 BUG_ON(ocfs2_resv_empty(resv
));
731 int ocfs2_resmap_resv_bits(struct ocfs2_reservation_map
*resmap
,
732 struct ocfs2_alloc_reservation
*resv
,
733 int *cstart
, int *clen
)
735 if (resv
== NULL
|| ocfs2_resmap_disabled(resmap
))
738 spin_lock(&resv_lock
);
740 if (ocfs2_resv_empty(resv
)) {
742 * We don't want to over-allocate for temporary
743 * windows. Otherwise, we run the risk of fragmenting the
746 unsigned int wanted
= ocfs2_resv_window_bits(resmap
, resv
);
748 if ((resv
->r_flags
& OCFS2_RESV_FLAG_TMP
) || wanted
< *clen
)
751 mlog(0, "empty reservation, find new window\n");
753 * Try to get a window here. If it works, we must fall
754 * through and test the bitmap . This avoids some
755 * ping-ponging of windows due to non-reserved space
756 * being allocation before we initialize a window for
759 ocfs2_resv_find_window(resmap
, resv
, wanted
);
762 BUG_ON(ocfs2_resv_empty(resv
));
764 *cstart
= resv
->r_start
;
767 spin_unlock(&resv_lock
);
772 ocfs2_adjust_resv_from_alloc(struct ocfs2_reservation_map
*resmap
,
773 struct ocfs2_alloc_reservation
*resv
,
774 unsigned int start
, unsigned int end
)
776 unsigned int rhs
= 0;
777 unsigned int old_end
= ocfs2_resv_end(resv
);
779 BUG_ON(start
!= resv
->r_start
|| old_end
< end
);
782 * Completely used? We can remove it then.
784 if (old_end
== end
) {
785 __ocfs2_resv_discard(resmap
, resv
);
792 * This should have been trapped above.
796 resv
->r_start
= end
+ 1;
797 resv
->r_len
= old_end
- resv
->r_start
+ 1;
800 void ocfs2_resmap_claimed_bits(struct ocfs2_reservation_map
*resmap
,
801 struct ocfs2_alloc_reservation
*resv
,
802 u32 cstart
, u32 clen
)
804 unsigned int cend
= cstart
+ clen
- 1;
806 if (resmap
== NULL
|| ocfs2_resmap_disabled(resmap
))
812 BUG_ON(cstart
!= resv
->r_start
);
814 spin_lock(&resv_lock
);
816 mlog(0, "claim bits: cstart: %u cend: %u clen: %u r_start: %u "
817 "r_end: %u r_len: %u, r_last_start: %u r_last_len: %u\n",
818 cstart
, cend
, clen
, resv
->r_start
, ocfs2_resv_end(resv
),
819 resv
->r_len
, resv
->r_last_start
, resv
->r_last_len
);
821 BUG_ON(cstart
< resv
->r_start
);
822 BUG_ON(cstart
> ocfs2_resv_end(resv
));
823 BUG_ON(cend
> ocfs2_resv_end(resv
));
825 ocfs2_adjust_resv_from_alloc(resmap
, resv
, cstart
, cend
);
826 resv
->r_last_start
= cstart
;
827 resv
->r_last_len
= clen
;
830 * May have been discarded above from
831 * ocfs2_adjust_resv_from_alloc().
833 if (!ocfs2_resv_empty(resv
))
834 ocfs2_resv_mark_lru(resmap
, resv
);
836 mlog(0, "Reservation now looks like: r_start: %u r_end: %u "
837 "r_len: %u r_last_start: %u r_last_len: %u\n",
838 resv
->r_start
, ocfs2_resv_end(resv
), resv
->r_len
,
839 resv
->r_last_start
, resv
->r_last_len
);
841 ocfs2_check_resmap(resmap
);
843 spin_unlock(&resv_lock
);