28 #include <linux/types.h>
30 #include <linux/bitops.h>
31 #include <linux/list.h>
38 #ifdef CONFIG_OCFS2_DEBUG_FS
39 #define OCFS2_CHECK_RESERVATIONS
44 #define OCFS2_MIN_RESV_WINDOW_BITS 8
45 #define OCFS2_MAX_RESV_WINDOW_BITS 1024
76 return !!(resv->
r_len == 0);
81 if (resmap->
m_osb->osb_resv_level == 0)
93 mlog(
ML_NOTICE,
"Dumping resmap for device %s. Bitmap length: %u\n",
100 mlog(
ML_NOTICE,
"start: %u\tend: %u\tlen: %u\tlast_start: %u"
101 "\tlast_len: %u\n", resv->
r_start,
114 "last_start: %u\tlast_len: %u\n", i, resv->
r_start,
122 #ifdef OCFS2_CHECK_RESERVATIONS
129 unsigned int end = ocfs2_resv_end(resv);
131 while (start <= end) {
134 "reservation %d covers an allocated area "
135 "starting at bit %u!\n", i, start);
146 unsigned int off = 0;
155 if (i > 0 && resv->
r_start <= off) {
161 if (resv->
r_len == 0) {
167 if (resv->
r_start > ocfs2_resv_end(resv)) {
179 if (ocfs2_validate_resmap_bits(resmap, i, resv))
182 off = ocfs2_resv_end(resv);
190 ocfs2_dump_resv(resmap);
202 memset(resv, 0,
sizeof(*resv));
203 INIT_LIST_HEAD(&resv->
r_lru);
217 memset(resmap, 0,
sizeof(*resmap));
222 INIT_LIST_HEAD(&resmap->
m_lru);
232 if (!list_empty(&resv->
r_lru))
233 list_del_init(&resv->
r_lru);
248 list_del_init(&resv->
r_lru);
259 __ocfs2_resv_trunc(resv);
266 ocfs2_resv_remove(resmap, resv);
274 spin_lock(&resv_lock);
275 __ocfs2_resv_discard(resmap, resv);
276 spin_unlock(&resv_lock);
290 __ocfs2_resv_discard(resmap, resv);
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);
306 spin_unlock(&resv_lock);
324 trace_ocfs2_resv_insert(new->r_start, new->r_len);
331 if (new->r_start < tmp->
r_start) {
339 }
else if (new->r_start > ocfs2_resv_end(tmp)) {
348 rb_link_node(&new->r_node, parent, p);
352 ocfs2_resv_mark_lru(resmap,
new);
354 ocfs2_check_resmap(resmap);
382 if (resv->
r_start <= goal && ocfs2_resv_end(resv) >= goal)
416 unsigned int search_start,
417 unsigned int search_len,
418 unsigned int *rstart,
422 unsigned int best_start, best_len = 0;
425 trace_ocfs2_resmap_find_free_bits_begin(search_start, search_len,
428 found = best_start = best_len = 0;
430 start = search_start;
434 if (offset >= (search_start + search_len))
437 if (offset == start) {
447 if (found > best_len) {
449 best_start = start - found;
459 if (best_len >= wanted)
463 *rstart = best_start;
465 trace_ocfs2_resmap_find_free_bits_end(best_start, best_len);
472 unsigned int goal,
unsigned int wanted)
475 unsigned int gap_start, gap_end, gap_len;
478 unsigned int cstart,
clen;
479 unsigned int best_start = 0, best_len = 0;
489 trace_ocfs2_resv_find_window_begin(resv->
r_start, ocfs2_resv_end(resv),
499 clen = ocfs2_resmap_find_free_bits(resmap, wanted, goal,
507 BUG_ON(goal == 0 && clen == 0);
515 ocfs2_resv_insert(resmap, resv);
519 prev_resv = ocfs2_find_resv_lhs(resmap, goal);
521 if (prev_resv ==
NULL) {
545 if (next_resv->
r_start <= goal) {
548 ocfs2_dump_resv(resmap);
552 clen = ocfs2_resmap_find_free_bits(resmap, wanted, goal,
558 if (best_len == wanted)
562 prev_resv = next_resv;
566 trace_ocfs2_resv_find_window_prev(prev_resv->
r_start,
567 ocfs2_resv_end(prev_resv));
569 prev = &prev_resv->
r_node;
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;
588 gap_start = ocfs2_resv_end(prev_resv) + 1;
593 trace_ocfs2_resv_find_window_next(next ? next_resv->
r_start: -1,
594 next ? ocfs2_resv_end(next_resv) : -1);
599 if (gap_len <= best_len)
602 clen = ocfs2_resmap_find_free_bits(resmap, wanted, gap_start,
603 gap_len, &cstart, &clen);
604 if (clen == wanted) {
608 }
else if (clen > best_len) {
625 resv->
r_len = best_len;
626 ocfs2_resv_insert(resmap, resv);
636 unsigned int min_bits;
639 min_bits = ocfs2_resv_window_bits(resmap, resv) >> 1;
653 trace_ocfs2_cannibalize_resv_begin(lru_resv->
r_start,
655 ocfs2_resv_end(lru_resv));
661 if (lru_resv->
r_len <= min_bits) {
670 __ocfs2_resv_discard(resmap, lru_resv);
676 shrink = lru_resv->
r_len / 2;
680 resv->
r_start = ocfs2_resv_end(lru_resv) + 1;
684 trace_ocfs2_cannibalize_resv_end(resv->
r_start, ocfs2_resv_end(resv),
688 ocfs2_resv_insert(resmap, resv);
695 unsigned int goal = 0;
697 BUG_ON(!ocfs2_resv_empty(resv));
710 __ocfs2_resv_find_window(resmap, resv, goal, wanted);
713 if (ocfs2_resv_empty(resv) && goal != 0)
714 __ocfs2_resv_find_window(resmap, resv, 0, wanted);
716 if (ocfs2_resv_empty(resv)) {
721 ocfs2_cannibalize_resv(resmap, resv, wanted);
724 BUG_ON(ocfs2_resv_empty(resv));
729 int *cstart,
int *clen)
731 if (resv ==
NULL || ocfs2_resmap_disabled(resmap))
734 spin_lock(&resv_lock);
736 if (ocfs2_resv_empty(resv)) {
742 unsigned int wanted = ocfs2_resv_window_bits(resmap, resv);
754 ocfs2_resv_find_window(resmap, resv, wanted);
755 trace_ocfs2_resmap_resv_bits(resv->
r_start, resv->
r_len);
758 BUG_ON(ocfs2_resv_empty(resv));
763 spin_unlock(&resv_lock);
770 unsigned int start,
unsigned int end)
772 unsigned int rhs = 0;
773 unsigned int old_end = ocfs2_resv_end(resv);
780 if (old_end == end) {
781 __ocfs2_resv_discard(resmap, resv);
800 unsigned int cend = cstart + clen - 1;
802 if (resmap ==
NULL || ocfs2_resmap_disabled(resmap))
810 spin_lock(&resv_lock);
812 trace_ocfs2_resmap_claimed_bits_begin(cstart, cend, clen, resv->
r_start,
813 ocfs2_resv_end(resv), resv->
r_len,
818 BUG_ON(cstart > ocfs2_resv_end(resv));
819 BUG_ON(cend > ocfs2_resv_end(resv));
821 ocfs2_adjust_resv_from_alloc(resmap, resv, cstart, cend);
829 if (!ocfs2_resv_empty(resv))
830 ocfs2_resv_mark_lru(resmap, resv);
832 trace_ocfs2_resmap_claimed_bits_end(resv->
r_start, ocfs2_resv_end(resv),
836 ocfs2_check_resmap(resmap);
838 spin_unlock(&resv_lock);