1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/fs.h>
28#include <linux/types.h>
29#include <linux/highmem.h>
30#include <linux/bitops.h>
31#include <linux/list.h>
32
33#define MLOG_MASK_PREFIX ML_RESERVATIONS
34#include <cluster/masklog.h>
35
36#include "ocfs2.h"
37
38#ifdef CONFIG_OCFS2_DEBUG_FS
39#define OCFS2_CHECK_RESERVATIONS
40#endif
41
42DEFINE_SPINLOCK(resv_lock);
43
44#define OCFS2_MIN_RESV_WINDOW_BITS 8
45#define OCFS2_MAX_RESV_WINDOW_BITS 1024
46
47int ocfs2_dir_resv_allowed(struct ocfs2_super *osb)
48{
49 return (osb->osb_resv_level && osb->osb_dir_resv_level);
50}
51
52static unsigned int ocfs2_resv_window_bits(struct ocfs2_reservation_map *resmap,
53 struct ocfs2_alloc_reservation *resv)
54{
55 struct ocfs2_super *osb = resmap->m_osb;
56 unsigned int bits;
57
58 if (!(resv->r_flags & OCFS2_RESV_FLAG_DIR)) {
59
60 bits = 4 << osb->osb_resv_level;
61 } else {
62 bits = 4 << osb->osb_dir_resv_level;
63 }
64 return bits;
65}
66
67static inline unsigned int ocfs2_resv_end(struct ocfs2_alloc_reservation *resv)
68{
69 if (resv->r_len)
70 return resv->r_start + resv->r_len - 1;
71 return resv->r_start;
72}
73
74static inline int ocfs2_resv_empty(struct ocfs2_alloc_reservation *resv)
75{
76 return !!(resv->r_len == 0);
77}
78
79static inline int ocfs2_resmap_disabled(struct ocfs2_reservation_map *resmap)
80{
81 if (resmap->m_osb->osb_resv_level == 0)
82 return 1;
83 return 0;
84}
85
86static void ocfs2_dump_resv(struct ocfs2_reservation_map *resmap)
87{
88 struct ocfs2_super *osb = resmap->m_osb;
89 struct rb_node *node;
90 struct ocfs2_alloc_reservation *resv;
91 int i = 0;
92
93 mlog(ML_NOTICE, "Dumping resmap for device %s. Bitmap length: %u\n",
94 osb->dev_str, resmap->m_bitmap_len);
95
96 node = rb_first(&resmap->m_reservations);
97 while (node) {
98 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
99
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,
103 resv->r_last_len);
104
105 node = rb_next(node);
106 i++;
107 }
108
109 mlog(ML_NOTICE, "%d reservations found. LRU follows\n", i);
110
111 i = 0;
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,
116 resv->r_last_len);
117
118 i++;
119 }
120}
121
122#ifdef OCFS2_CHECK_RESERVATIONS
123static int ocfs2_validate_resmap_bits(struct ocfs2_reservation_map *resmap,
124 int i,
125 struct ocfs2_alloc_reservation *resv)
126{
127 char *disk_bitmap = resmap->m_disk_bitmap;
128 unsigned int start = resv->r_start;
129 unsigned int end = ocfs2_resv_end(resv);
130
131 while (start <= end) {
132 if (ocfs2_test_bit(start, disk_bitmap)) {
133 mlog(ML_ERROR,
134 "reservation %d covers an allocated area "
135 "starting at bit %u!\n", i, start);
136 return 1;
137 }
138
139 start++;
140 }
141 return 0;
142}
143
144static void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
145{
146 unsigned int off = 0;
147 int i = 0;
148 struct rb_node *node;
149 struct ocfs2_alloc_reservation *resv;
150
151 node = rb_first(&resmap->m_reservations);
152 while (node) {
153 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
154
155 if (i > 0 && resv->r_start <= off) {
156 mlog(ML_ERROR, "reservation %d has bad start off!\n",
157 i);
158 goto bad;
159 }
160
161 if (resv->r_len == 0) {
162 mlog(ML_ERROR, "reservation %d has no length!\n",
163 i);
164 goto bad;
165 }
166
167 if (resv->r_start > ocfs2_resv_end(resv)) {
168 mlog(ML_ERROR, "reservation %d has invalid range!\n",
169 i);
170 goto bad;
171 }
172
173 if (ocfs2_resv_end(resv) >= resmap->m_bitmap_len) {
174 mlog(ML_ERROR, "reservation %d extends past bitmap!\n",
175 i);
176 goto bad;
177 }
178
179 if (ocfs2_validate_resmap_bits(resmap, i, resv))
180 goto bad;
181
182 off = ocfs2_resv_end(resv);
183 node = rb_next(node);
184
185 i++;
186 }
187 return;
188
189bad:
190 ocfs2_dump_resv(resmap);
191 BUG();
192}
193#else
194static inline void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
195{
196
197}
198#endif
199
200void ocfs2_resv_init_once(struct ocfs2_alloc_reservation *resv)
201{
202 memset(resv, 0, sizeof(*resv));
203 INIT_LIST_HEAD(&resv->r_lru);
204}
205
206void ocfs2_resv_set_type(struct ocfs2_alloc_reservation *resv,
207 unsigned int flags)
208{
209 BUG_ON(flags & ~OCFS2_RESV_TYPES);
210
211 resv->r_flags |= flags;
212}
213
214int ocfs2_resmap_init(struct ocfs2_super *osb,
215 struct ocfs2_reservation_map *resmap)
216{
217 memset(resmap, 0, sizeof(*resmap));
218
219 resmap->m_osb = osb;
220 resmap->m_reservations = RB_ROOT;
221
222 INIT_LIST_HEAD(&resmap->m_lru);
223
224 return 0;
225}
226
227static void ocfs2_resv_mark_lru(struct ocfs2_reservation_map *resmap,
228 struct ocfs2_alloc_reservation *resv)
229{
230 assert_spin_locked(&resv_lock);
231
232 if (!list_empty(&resv->r_lru))
233 list_del_init(&resv->r_lru);
234
235 list_add_tail(&resv->r_lru, &resmap->m_lru);
236}
237
238static void __ocfs2_resv_trunc(struct ocfs2_alloc_reservation *resv)
239{
240 resv->r_len = 0;
241 resv->r_start = 0;
242}
243
244static void ocfs2_resv_remove(struct ocfs2_reservation_map *resmap,
245 struct ocfs2_alloc_reservation *resv)
246{
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;
251 }
252}
253
254static void __ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
255 struct ocfs2_alloc_reservation *resv)
256{
257 assert_spin_locked(&resv_lock);
258
259 __ocfs2_resv_trunc(resv);
260
261
262
263
264 resv->r_last_len = resv->r_last_start = 0;
265
266 ocfs2_resv_remove(resmap, resv);
267}
268
269
270void ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
271 struct ocfs2_alloc_reservation *resv)
272{
273 if (resv) {
274 spin_lock(&resv_lock);
275 __ocfs2_resv_discard(resmap, resv);
276 spin_unlock(&resv_lock);
277 }
278}
279
280static void ocfs2_resmap_clear_all_resv(struct ocfs2_reservation_map *resmap)
281{
282 struct rb_node *node;
283 struct ocfs2_alloc_reservation *resv;
284
285 assert_spin_locked(&resv_lock);
286
287 while ((node = rb_last(&resmap->m_reservations)) != NULL) {
288 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
289
290 __ocfs2_resv_discard(resmap, resv);
291 }
292}
293
294void ocfs2_resmap_restart(struct ocfs2_reservation_map *resmap,
295 unsigned int clen, char *disk_bitmap)
296{
297 if (ocfs2_resmap_disabled(resmap))
298 return;
299
300 spin_lock(&resv_lock);
301
302 ocfs2_resmap_clear_all_resv(resmap);
303 resmap->m_bitmap_len = clen;
304 resmap->m_disk_bitmap = disk_bitmap;
305
306 spin_unlock(&resv_lock);
307}
308
309void ocfs2_resmap_uninit(struct ocfs2_reservation_map *resmap)
310{
311
312}
313
314static void ocfs2_resv_insert(struct ocfs2_reservation_map *resmap,
315 struct ocfs2_alloc_reservation *new)
316{
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;
321
322 assert_spin_locked(&resv_lock);
323
324 mlog(0, "Insert reservation start: %u len: %u\n", new->r_start,
325 new->r_len);
326
327 while (*p) {
328 parent = *p;
329
330 tmp = rb_entry(parent, struct ocfs2_alloc_reservation, r_node);
331
332 if (new->r_start < tmp->r_start) {
333 p = &(*p)->rb_left;
334
335
336
337
338
339 BUG_ON(ocfs2_resv_end(new) >= tmp->r_start);
340 } else if (new->r_start > ocfs2_resv_end(tmp)) {
341 p = &(*p)->rb_right;
342 } else {
343
344 mlog(ML_ERROR, "Duplicate reservation window!\n");
345 BUG();
346 }
347 }
348
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;
352
353 ocfs2_resv_mark_lru(resmap, new);
354
355 ocfs2_check_resmap(resmap);
356}
357
358
359
360
361
362
363
364
365
366
367static struct ocfs2_alloc_reservation *
368ocfs2_find_resv_lhs(struct ocfs2_reservation_map *resmap, unsigned int goal)
369{
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;
373
374 assert_spin_locked(&resv_lock);
375
376 if (!node)
377 return NULL;
378
379 node = rb_first(&resmap->m_reservations);
380 while (node) {
381 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
382
383 if (resv->r_start <= goal && ocfs2_resv_end(resv) >= goal)
384 break;
385
386
387 if (resv->r_start > goal) {
388 resv = prev_resv;
389 break;
390 }
391
392 prev_resv = resv;
393 node = rb_next(node);
394 }
395
396 return resv;
397}
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415static int ocfs2_resmap_find_free_bits(struct ocfs2_reservation_map *resmap,
416 unsigned int wanted,
417 unsigned int search_start,
418 unsigned int search_len,
419 unsigned int *rstart,
420 unsigned int *rlen)
421{
422 void *bitmap = resmap->m_disk_bitmap;
423 unsigned int best_start, best_len = 0;
424 int offset, start, found;
425
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);
428
429 found = best_start = best_len = 0;
430
431 start = search_start;
432 while ((offset = ocfs2_find_next_zero_bit(bitmap, resmap->m_bitmap_len,
433 start)) != -1) {
434
435 if (offset >= (search_start + search_len))
436 break;
437
438 if (offset == start) {
439
440 found++;
441
442 start++;
443 } else {
444
445 found = 1;
446 start = offset + 1;
447 }
448 if (found > best_len) {
449 best_len = found;
450 best_start = start - found;
451 }
452
453 if (found >= wanted)
454 break;
455 }
456
457 if (best_len == 0)
458 return 0;
459
460 if (best_len >= wanted)
461 best_len = wanted;
462
463 *rlen = best_len;
464 *rstart = best_start;
465
466 mlog(0, "Found start: %u len: %u\n", best_start, best_len);
467
468 return *rlen;
469}
470
471static void __ocfs2_resv_find_window(struct ocfs2_reservation_map *resmap,
472 struct ocfs2_alloc_reservation *resv,
473 unsigned int goal, unsigned int wanted)
474{
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;
481
482
483
484
485
486
487
488
489
490
491 mlog(0, "resv start: %u resv end: %u goal: %u wanted: %u\n",
492 resv->r_start, ocfs2_resv_end(resv), goal, wanted);
493
494 assert_spin_locked(&resv_lock);
495
496 if (RB_EMPTY_ROOT(root)) {
497
498
499
500
501
502 mlog(0, "Empty root\n");
503
504 clen = ocfs2_resmap_find_free_bits(resmap, wanted, goal,
505 resmap->m_bitmap_len - goal,
506 &cstart, &clen);
507
508
509
510
511
512 BUG_ON(goal == 0 && clen == 0);
513
514 if (clen == 0)
515 return;
516
517 resv->r_start = cstart;
518 resv->r_len = clen;
519
520 ocfs2_resv_insert(resmap, resv);
521 return;
522 }
523
524 prev_resv = ocfs2_find_resv_lhs(resmap, goal);
525
526 if (prev_resv == NULL) {
527 mlog(0, "Goal on LHS of leftmost window\n");
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544 next = rb_first(root);
545 next_resv = rb_entry(next, struct ocfs2_alloc_reservation,
546 r_node);
547
548
549
550
551
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);
556 BUG();
557 }
558
559 clen = ocfs2_resmap_find_free_bits(resmap, wanted, goal,
560 next_resv->r_start - goal,
561 &cstart, &clen);
562 if (clen) {
563 best_len = clen;
564 best_start = cstart;
565 if (best_len == wanted)
566 goto out_insert;
567 }
568
569 prev_resv = next_resv;
570 next_resv = NULL;
571 }
572
573 prev = &prev_resv->r_node;
574
575
576 while (1) {
577 next = rb_next(prev);
578 if (next) {
579 mlog(0, "One more resv found in linear search\n");
580 next_resv = rb_entry(next,
581 struct ocfs2_alloc_reservation,
582 r_node);
583
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;
587 } else {
588 mlog(0, "No next node\n");
589
590
591
592
593
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;
597 }
598
599
600
601
602
603 if (gap_len <= best_len)
604 goto next_resv;
605
606 clen = ocfs2_resmap_find_free_bits(resmap, wanted, gap_start,
607 gap_len, &cstart, &clen);
608 if (clen == wanted) {
609 best_len = clen;
610 best_start = cstart;
611 goto out_insert;
612 } else if (clen > best_len) {
613 best_len = clen;
614 best_start = cstart;
615 }
616
617next_resv:
618 if (!next)
619 break;
620
621 prev = next;
622 prev_resv = rb_entry(prev, struct ocfs2_alloc_reservation,
623 r_node);
624 }
625
626out_insert:
627 if (best_len) {
628 resv->r_start = best_start;
629 resv->r_len = best_len;
630 ocfs2_resv_insert(resmap, resv);
631 }
632}
633
634static void ocfs2_cannibalize_resv(struct ocfs2_reservation_map *resmap,
635 struct ocfs2_alloc_reservation *resv,
636 unsigned int wanted)
637{
638 struct ocfs2_alloc_reservation *lru_resv;
639 int tmpwindow = !!(resv->r_flags & OCFS2_RESV_FLAG_TMP);
640 unsigned int min_bits;
641
642 if (!tmpwindow)
643 min_bits = ocfs2_resv_window_bits(resmap, resv) >> 1;
644 else
645 min_bits = wanted;
646
647
648
649
650
651
652
653
654 lru_resv = list_first_entry(&resmap->m_lru,
655 struct ocfs2_alloc_reservation, r_lru);
656
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));
659
660
661
662
663
664 if (lru_resv->r_len <= min_bits) {
665
666
667
668
669
670 resv->r_start = lru_resv->r_start;
671 resv->r_len = lru_resv->r_len;
672
673 __ocfs2_resv_discard(resmap, lru_resv);
674 } else {
675 unsigned int shrink;
676 if (tmpwindow)
677 shrink = min_bits;
678 else
679 shrink = lru_resv->r_len / 2;
680
681 lru_resv->r_len -= shrink;
682
683 resv->r_start = ocfs2_resv_end(lru_resv) + 1;
684 resv->r_len = shrink;
685 }
686
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);
691
692 ocfs2_resv_insert(resmap, resv);
693}
694
695static void ocfs2_resv_find_window(struct ocfs2_reservation_map *resmap,
696 struct ocfs2_alloc_reservation *resv,
697 unsigned int wanted)
698{
699 unsigned int goal = 0;
700
701 BUG_ON(!ocfs2_resv_empty(resv));
702
703
704
705
706
707
708 if (resv->r_last_len) {
709 goal = resv->r_last_start + resv->r_last_len;
710 if (goal >= resmap->m_bitmap_len)
711 goal = 0;
712 }
713
714 __ocfs2_resv_find_window(resmap, resv, goal, wanted);
715
716
717 if (ocfs2_resv_empty(resv) && goal != 0)
718 __ocfs2_resv_find_window(resmap, resv, 0, wanted);
719
720 if (ocfs2_resv_empty(resv)) {
721
722
723
724
725 ocfs2_cannibalize_resv(resmap, resv, wanted);
726 }
727
728 BUG_ON(ocfs2_resv_empty(resv));
729}
730
731int ocfs2_resmap_resv_bits(struct ocfs2_reservation_map *resmap,
732 struct ocfs2_alloc_reservation *resv,
733 int *cstart, int *clen)
734{
735 if (resv == NULL || ocfs2_resmap_disabled(resmap))
736 return -ENOSPC;
737
738 spin_lock(&resv_lock);
739
740 if (ocfs2_resv_empty(resv)) {
741
742
743
744
745
746 unsigned int wanted = ocfs2_resv_window_bits(resmap, resv);
747
748 if ((resv->r_flags & OCFS2_RESV_FLAG_TMP) || wanted < *clen)
749 wanted = *clen;
750
751 mlog(0, "empty reservation, find new window\n");
752
753
754
755
756
757
758
759 ocfs2_resv_find_window(resmap, resv, wanted);
760 }
761
762 BUG_ON(ocfs2_resv_empty(resv));
763
764 *cstart = resv->r_start;
765 *clen = resv->r_len;
766
767 spin_unlock(&resv_lock);
768 return 0;
769}
770
771static void
772 ocfs2_adjust_resv_from_alloc(struct ocfs2_reservation_map *resmap,
773 struct ocfs2_alloc_reservation *resv,
774 unsigned int start, unsigned int end)
775{
776 unsigned int rhs = 0;
777 unsigned int old_end = ocfs2_resv_end(resv);
778
779 BUG_ON(start != resv->r_start || old_end < end);
780
781
782
783
784 if (old_end == end) {
785 __ocfs2_resv_discard(resmap, resv);
786 return;
787 }
788
789 rhs = old_end - end;
790
791
792
793
794 BUG_ON(rhs == 0);
795
796 resv->r_start = end + 1;
797 resv->r_len = old_end - resv->r_start + 1;
798}
799
800void ocfs2_resmap_claimed_bits(struct ocfs2_reservation_map *resmap,
801 struct ocfs2_alloc_reservation *resv,
802 u32 cstart, u32 clen)
803{
804 unsigned int cend = cstart + clen - 1;
805
806 if (resmap == NULL || ocfs2_resmap_disabled(resmap))
807 return;
808
809 if (resv == NULL)
810 return;
811
812 BUG_ON(cstart != resv->r_start);
813
814 spin_lock(&resv_lock);
815
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);
820
821 BUG_ON(cstart < resv->r_start);
822 BUG_ON(cstart > ocfs2_resv_end(resv));
823 BUG_ON(cend > ocfs2_resv_end(resv));
824
825 ocfs2_adjust_resv_from_alloc(resmap, resv, cstart, cend);
826 resv->r_last_start = cstart;
827 resv->r_last_len = clen;
828
829
830
831
832
833 if (!ocfs2_resv_empty(resv))
834 ocfs2_resv_mark_lru(resmap, resv);
835
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);
840
841 ocfs2_check_resmap(resmap);
842
843 spin_unlock(&resv_lock);
844}
845