]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/ocfs2/reservations.c
ocfs2: make ocfs2_adjust_resv_from_alloc simple.
[karo-tx-linux.git] / fs / ocfs2 / reservations.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * reservations.c
5  *
6  * Allocation reservations implementation
7  *
8  * Some code borrowed from fs/ext3/balloc.c and is:
9  *
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)
14  *
15  * The rest is copyright (C) 2010 Novell.  All rights reserved.
16  *
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.
20  *
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.
25  */
26
27 #include <linux/fs.h>
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>
33
34 #define MLOG_MASK_PREFIX ML_RESERVATIONS
35 #include <cluster/masklog.h>
36
37 #include "ocfs2.h"
38
39 #ifdef CONFIG_OCFS2_DEBUG_FS
40 #define OCFS2_CHECK_RESERVATIONS
41 #endif
42
43 DEFINE_SPINLOCK(resv_lock);
44
45 #define OCFS2_MIN_RESV_WINDOW_BITS      8
46 #define OCFS2_MAX_RESV_WINDOW_BITS      1024
47
48 int ocfs2_dir_resv_allowed(struct ocfs2_super *osb)
49 {
50         return (osb->osb_resv_level && osb->osb_dir_resv_level);
51 }
52
53 static unsigned int ocfs2_resv_window_bits(struct ocfs2_reservation_map *resmap,
54                                            struct ocfs2_alloc_reservation *resv)
55 {
56         struct ocfs2_super *osb = resmap->m_osb;
57         unsigned int bits;
58
59         if (!(resv->r_flags & OCFS2_RESV_FLAG_DIR)) {
60                 /* 8, 16, 32, 64, 128, 256, 512, 1024 */
61                 bits = 4 << osb->osb_resv_level;
62         } else {
63                 bits = 4 << osb->osb_dir_resv_level;
64         }
65         return bits;
66 }
67
68 static inline unsigned int ocfs2_resv_end(struct ocfs2_alloc_reservation *resv)
69 {
70         if (resv->r_len)
71                 return resv->r_start + resv->r_len - 1;
72         return resv->r_start;
73 }
74
75 static inline int ocfs2_resv_empty(struct ocfs2_alloc_reservation *resv)
76 {
77         return !!(resv->r_len == 0);
78 }
79
80 static inline int ocfs2_resmap_disabled(struct ocfs2_reservation_map *resmap)
81 {
82         if (resmap->m_osb->osb_resv_level == 0)
83                 return 1;
84         return 0;
85 }
86
87 static void ocfs2_dump_resv(struct ocfs2_reservation_map *resmap)
88 {
89         struct ocfs2_super *osb = resmap->m_osb;
90         struct rb_node *node;
91         struct ocfs2_alloc_reservation *resv;
92         int i = 0;
93
94         mlog(ML_NOTICE, "Dumping resmap for device %s. Bitmap length: %u\n",
95              osb->dev_str, resmap->m_bitmap_len);
96
97         node = rb_first(&resmap->m_reservations);
98         while (node) {
99                 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
100
101                 mlog(ML_NOTICE, "start: %u\tend: %u\tlen: %u\tlast_start: %u"
102                      "\tlast_len: %u\n", resv->r_start,
103                      ocfs2_resv_end(resv), resv->r_len, resv->r_last_start,
104                      resv->r_last_len);
105
106                 node = rb_next(node);
107                 i++;
108         }
109
110         mlog(ML_NOTICE, "%d reservations found. LRU follows\n", i);
111
112         i = 0;
113         list_for_each_entry(resv, &resmap->m_lru, r_lru) {
114                 mlog(ML_NOTICE, "LRU(%d) start: %u\tend: %u\tlen: %u\t"
115                      "last_start: %u\tlast_len: %u\n", i, resv->r_start,
116                      ocfs2_resv_end(resv), resv->r_len, resv->r_last_start,
117                      resv->r_last_len);
118
119                 i++;
120         }
121 }
122
123 #ifdef OCFS2_CHECK_RESERVATIONS
124 static int ocfs2_validate_resmap_bits(struct ocfs2_reservation_map *resmap,
125                                       int i,
126                                       struct ocfs2_alloc_reservation *resv)
127 {
128         char *disk_bitmap = resmap->m_disk_bitmap;
129         unsigned int start = resv->r_start;
130         unsigned int end = ocfs2_resv_end(resv);
131
132         while (start <= end) {
133                 if (ocfs2_test_bit(start, disk_bitmap)) {
134                         mlog(ML_ERROR,
135                              "reservation %d covers an allocated area "
136                              "starting at bit %u!\n", i, start);
137                         return 1;
138                 }
139
140                 start++;
141         }
142         return 0;
143 }
144
145 static void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
146 {
147         unsigned int off = 0;
148         int i = 0;
149         struct rb_node *node;
150         struct ocfs2_alloc_reservation *resv;
151
152         node = rb_first(&resmap->m_reservations);
153         while (node) {
154                 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
155
156                 if (i > 0 && resv->r_start <= off) {
157                         mlog(ML_ERROR, "reservation %d has bad start off!\n",
158                              i);
159                         goto bad;
160                 }
161
162                 if (resv->r_len == 0) {
163                         mlog(ML_ERROR, "reservation %d has no length!\n",
164                              i);
165                         goto bad;
166                 }
167
168                 if (resv->r_start > ocfs2_resv_end(resv)) {
169                         mlog(ML_ERROR, "reservation %d has invalid range!\n",
170                              i);
171                         goto bad;
172                 }
173
174                 if (ocfs2_resv_end(resv) >= resmap->m_bitmap_len) {
175                         mlog(ML_ERROR, "reservation %d extends past bitmap!\n",
176                              i);
177                         goto bad;
178                 }
179
180                 if (ocfs2_validate_resmap_bits(resmap, i, resv))
181                         goto bad;
182
183                 off = ocfs2_resv_end(resv);
184                 node = rb_next(node);
185
186                 i++;
187         }
188         return;
189
190 bad:
191         ocfs2_dump_resv(resmap);
192         BUG();
193 }
194 #else
195 static inline void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
196 {
197
198 }
199 #endif
200
201 void ocfs2_resv_init_once(struct ocfs2_alloc_reservation *resv)
202 {
203         memset(resv, 0, sizeof(*resv));
204         INIT_LIST_HEAD(&resv->r_lru);
205 }
206
207 void ocfs2_resv_set_type(struct ocfs2_alloc_reservation *resv,
208                          unsigned int flags)
209 {
210         BUG_ON(flags & ~OCFS2_RESV_TYPES);
211
212         resv->r_flags |= flags;
213 }
214
215 int ocfs2_resmap_init(struct ocfs2_super *osb,
216                       struct ocfs2_reservation_map *resmap)
217 {
218         memset(resmap, 0, sizeof(*resmap));
219
220         resmap->m_osb = osb;
221         resmap->m_reservations = RB_ROOT;
222         /* m_bitmap_len is initialized to zero by the above memset. */
223         INIT_LIST_HEAD(&resmap->m_lru);
224
225         return 0;
226 }
227
228 static void ocfs2_resv_mark_lru(struct ocfs2_reservation_map *resmap,
229                                 struct ocfs2_alloc_reservation *resv)
230 {
231         assert_spin_locked(&resv_lock);
232
233         if (!list_empty(&resv->r_lru))
234                 list_del_init(&resv->r_lru);
235
236         list_add_tail(&resv->r_lru, &resmap->m_lru);
237 }
238
239 static void __ocfs2_resv_trunc(struct ocfs2_alloc_reservation *resv)
240 {
241         resv->r_len = 0;
242         resv->r_start = 0;
243 }
244
245 static void ocfs2_resv_remove(struct ocfs2_reservation_map *resmap,
246                               struct ocfs2_alloc_reservation *resv)
247 {
248         if (resv->r_flags & OCFS2_RESV_FLAG_INUSE) {
249                 list_del_init(&resv->r_lru);
250                 rb_erase(&resv->r_node, &resmap->m_reservations);
251                 resv->r_flags &= ~OCFS2_RESV_FLAG_INUSE;
252         }
253 }
254
255 static void __ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
256                                  struct ocfs2_alloc_reservation *resv)
257 {
258         assert_spin_locked(&resv_lock);
259
260         __ocfs2_resv_trunc(resv);
261         /*
262          * last_len and last_start no longer make sense if
263          * we're changing the range of our allocations.
264          */
265         resv->r_last_len = resv->r_last_start = 0;
266
267         ocfs2_resv_remove(resmap, resv);
268 }
269
270 /* does nothing if 'resv' is null */
271 void ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
272                         struct ocfs2_alloc_reservation *resv)
273 {
274         if (resv) {
275                 spin_lock(&resv_lock);
276                 __ocfs2_resv_discard(resmap, resv);
277                 spin_unlock(&resv_lock);
278         }
279 }
280
281 static void ocfs2_resmap_clear_all_resv(struct ocfs2_reservation_map *resmap)
282 {
283         struct rb_node *node;
284         struct ocfs2_alloc_reservation *resv;
285
286         assert_spin_locked(&resv_lock);
287
288         while ((node = rb_last(&resmap->m_reservations)) != NULL) {
289                 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
290
291                 __ocfs2_resv_discard(resmap, resv);
292         }
293 }
294
295 void ocfs2_resmap_restart(struct ocfs2_reservation_map *resmap,
296                           unsigned int clen, char *disk_bitmap)
297 {
298         if (ocfs2_resmap_disabled(resmap))
299                 return;
300
301         spin_lock(&resv_lock);
302
303         ocfs2_resmap_clear_all_resv(resmap);
304         resmap->m_bitmap_len = clen;
305         resmap->m_disk_bitmap = disk_bitmap;
306
307         spin_unlock(&resv_lock);
308 }
309
310 void ocfs2_resmap_uninit(struct ocfs2_reservation_map *resmap)
311 {
312         /* Does nothing for now. Keep this around for API symmetry */
313 }
314
315 static void ocfs2_resv_insert(struct ocfs2_reservation_map *resmap,
316                               struct ocfs2_alloc_reservation *new)
317 {
318         struct rb_root *root = &resmap->m_reservations;
319         struct rb_node *parent = NULL;
320         struct rb_node **p = &root->rb_node;
321         struct ocfs2_alloc_reservation *tmp;
322
323         assert_spin_locked(&resv_lock);
324
325         mlog(0, "Insert reservation start: %u len: %u\n", new->r_start,
326              new->r_len);
327
328         while (*p) {
329                 parent = *p;
330
331                 tmp = rb_entry(parent, struct ocfs2_alloc_reservation, r_node);
332
333                 if (new->r_start < tmp->r_start) {
334                         p = &(*p)->rb_left;
335
336                         /*
337                          * This is a good place to check for
338                          * overlapping reservations.
339                          */
340                         BUG_ON(ocfs2_resv_end(new) >= tmp->r_start);
341                 } else if (new->r_start > ocfs2_resv_end(tmp)) {
342                         p = &(*p)->rb_right;
343                 } else {
344                         /* This should never happen! */
345                         mlog(ML_ERROR, "Duplicate reservation window!\n");
346                         BUG();
347                 }
348         }
349
350         rb_link_node(&new->r_node, parent, p);
351         rb_insert_color(&new->r_node, root);
352         new->r_flags |= OCFS2_RESV_FLAG_INUSE;
353
354         ocfs2_resv_mark_lru(resmap, new);
355
356         ocfs2_check_resmap(resmap);
357 }
358
359 /**
360  * ocfs2_find_resv_lhs() - find the window which contains goal
361  * @resmap: reservation map to search
362  * @goal: which bit to search for
363  *
364  * If a window containing that goal is not found, we return the window
365  * which comes before goal. Returns NULL on empty rbtree or no window
366  * before goal.
367  */
368 static struct ocfs2_alloc_reservation *
369 ocfs2_find_resv_lhs(struct ocfs2_reservation_map *resmap, unsigned int goal)
370 {
371         struct ocfs2_alloc_reservation *resv = NULL;
372         struct ocfs2_alloc_reservation *prev_resv = NULL;
373         struct rb_node *node = resmap->m_reservations.rb_node;
374         struct rb_node *prev = NULL;
375
376         assert_spin_locked(&resv_lock);
377
378         if (!node)
379                 return NULL;
380
381         node = rb_first(&resmap->m_reservations);
382         while (node) {
383                 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
384
385                 if (resv->r_start <= goal && ocfs2_resv_end(resv) >= goal)
386                         break;
387
388                 /* Check if we overshot the reservation just before goal? */
389                 if (resv->r_start > goal) {
390                         resv = prev_resv;
391                         break;
392                 }
393
394                 prev_resv = resv;
395                 prev = node;
396                 node = rb_next(node);
397         }
398
399         return resv;
400 }
401
402 /*
403  * We are given a range within the bitmap, which corresponds to a gap
404  * inside the reservations tree (search_start, search_len). The range
405  * can be anything from the whole bitmap, to a gap between
406  * reservations.
407  *
408  * The start value of *rstart is insignificant.
409  *
410  * This function searches the bitmap range starting at search_start
411  * with length search_len for a set of contiguous free bits. We try
412  * to find up to 'wanted' bits, but can sometimes return less.
413  *
414  * Returns the length of allocation, 0 if no free bits are found.
415  *
416  * *cstart and *clen will also be populated with the result.
417  */
418 static int ocfs2_resmap_find_free_bits(struct ocfs2_reservation_map *resmap,
419                                        unsigned int wanted,
420                                        unsigned int search_start,
421                                        unsigned int search_len,
422                                        unsigned int *rstart,
423                                        unsigned int *rlen)
424 {
425         void *bitmap = resmap->m_disk_bitmap;
426         unsigned int best_start, best_len = 0;
427         int offset, start, found;
428
429         mlog(0, "Find %u bits within range (%u, len %u) resmap len: %u\n",
430              wanted, search_start, search_len, resmap->m_bitmap_len);
431
432         found = best_start = best_len = 0;
433
434         start = search_start;
435         while ((offset = ocfs2_find_next_zero_bit(bitmap, resmap->m_bitmap_len,
436                                                  start)) != -1) {
437                 /* Search reached end of the region */
438                 if (offset >= (search_start + search_len))
439                         break;
440
441                 if (offset == start) {
442                         /* we found a zero */
443                         found++;
444                         /* move start to the next bit to test */
445                         start++;
446                 } else {
447                         /* got a zero after some ones */
448                         found = 1;
449                         start = offset + 1;
450                 }
451                 if (found > best_len) {
452                         best_len = found;
453                         best_start = start - found;
454                 }
455
456                 if (found >= wanted)
457                         break;
458         }
459
460         if (best_len == 0)
461                 return 0;
462
463         if (best_len >= wanted)
464                 best_len = wanted;
465
466         *rlen = best_len;
467         *rstart = best_start;
468
469         mlog(0, "Found start: %u len: %u\n", best_start, best_len);
470
471         return *rlen;
472 }
473
474 static void __ocfs2_resv_find_window(struct ocfs2_reservation_map *resmap,
475                                      struct ocfs2_alloc_reservation *resv,
476                                      unsigned int goal, unsigned int wanted)
477 {
478         struct rb_root *root = &resmap->m_reservations;
479         unsigned int gap_start, gap_end, gap_len;
480         struct ocfs2_alloc_reservation *prev_resv, *next_resv;
481         struct rb_node *prev, *next;
482         unsigned int cstart, clen;
483         unsigned int best_start = 0, best_len = 0;
484
485         /*
486          * Nasty cases to consider:
487          *
488          * - rbtree is empty
489          * - our window should be first in all reservations
490          * - our window should be last in all reservations
491          * - need to make sure we don't go past end of bitmap
492          */
493
494         mlog(0, "resv start: %u resv end: %u goal: %u wanted: %u\n",
495              resv->r_start, ocfs2_resv_end(resv), goal, wanted);
496
497         assert_spin_locked(&resv_lock);
498
499         if (RB_EMPTY_ROOT(root)) {
500                 /*
501                  * Easiest case - empty tree. We can just take
502                  * whatever window of free bits we want.
503                  */
504
505                 mlog(0, "Empty root\n");
506
507                 clen = ocfs2_resmap_find_free_bits(resmap, wanted, goal,
508                                                    resmap->m_bitmap_len - goal,
509                                                    &cstart, &clen);
510
511                 /*
512                  * This should never happen - the local alloc window
513                  * will always have free bits when we're called.
514                  */
515                 BUG_ON(goal == 0 && clen == 0);
516
517                 if (clen == 0)
518                         return;
519
520                 resv->r_start = cstart;
521                 resv->r_len = clen;
522
523                 ocfs2_resv_insert(resmap, resv);
524                 return;
525         }
526
527         prev_resv = ocfs2_find_resv_lhs(resmap, goal);
528
529         if (prev_resv == NULL) {
530                 mlog(0, "Goal on LHS of leftmost window\n");
531
532                 /*
533                  * A NULL here means that the search code couldn't
534                  * find a window that starts before goal.
535                  *
536                  * However, we can take the first window after goal,
537                  * which is also by definition, the leftmost window in
538                  * the entire tree. If we can find free bits in the
539                  * gap between goal and the LHS window, then the
540                  * reservation can safely be placed there.
541                  *
542                  * Otherwise we fall back to a linear search, checking
543                  * the gaps in between windows for a place to
544                  * allocate.
545                  */
546
547                 next = rb_first(root);
548                 next_resv = rb_entry(next, struct ocfs2_alloc_reservation,
549                                      r_node);
550
551                 /*
552                  * The search should never return such a window. (see
553                  * comment above
554                  */
555                 if (next_resv->r_start <= goal) {
556                         mlog(ML_ERROR, "goal: %u next_resv: start %u len %u\n",
557                              goal, next_resv->r_start, next_resv->r_len);
558                         ocfs2_dump_resv(resmap);
559                         BUG();
560                 }
561
562                 clen = ocfs2_resmap_find_free_bits(resmap, wanted, goal,
563                                                    next_resv->r_start - goal,
564                                                    &cstart, &clen);
565                 if (clen) {
566                         best_len = clen;
567                         best_start = cstart;
568                         if (best_len == wanted)
569                                 goto out_insert;
570                 }
571
572                 prev_resv = next_resv;
573                 next_resv = NULL;
574         }
575
576         prev = &prev_resv->r_node;
577
578         /* Now we do a linear search for a window, starting at 'prev_rsv' */
579         while (1) {
580                 next = rb_next(prev);
581                 if (next) {
582                         mlog(0, "One more resv found in linear search\n");
583                         next_resv = rb_entry(next,
584                                              struct ocfs2_alloc_reservation,
585                                              r_node);
586
587                         gap_start = ocfs2_resv_end(prev_resv) + 1;
588                         gap_end = next_resv->r_start - 1;
589                         gap_len = gap_end - gap_start + 1;
590                 } else {
591                         mlog(0, "No next node\n");
592                         /*
593                          * We're at the rightmost edge of the
594                          * tree. See if a reservation between this
595                          * window and the end of the bitmap will work.
596                          */
597                         gap_start = ocfs2_resv_end(prev_resv) + 1;
598                         gap_len = resmap->m_bitmap_len - gap_start;
599                         gap_end = resmap->m_bitmap_len - 1;
600                 }
601
602                 /*
603                  * No need to check this gap if we have already found
604                  * a larger region of free bits.
605                  */
606                 if (gap_len <= best_len)
607                         goto next_resv;
608
609                 clen = ocfs2_resmap_find_free_bits(resmap, wanted, gap_start,
610                                                    gap_len, &cstart, &clen);
611                 if (clen == wanted) {
612                         best_len = clen;
613                         best_start = cstart;
614                         goto out_insert;
615                 } else if (clen > best_len) {
616                         best_len = clen;
617                         best_start = cstart;
618                 }
619
620 next_resv:
621                 if (!next)
622                         break;
623
624                 prev = next;
625                 prev_resv = rb_entry(prev, struct ocfs2_alloc_reservation,
626                                      r_node);
627         }
628
629 out_insert:
630         if (best_len) {
631                 resv->r_start = best_start;
632                 resv->r_len = best_len;
633                 ocfs2_resv_insert(resmap, resv);
634         }
635 }
636
637 static void ocfs2_cannibalize_resv(struct ocfs2_reservation_map *resmap,
638                                    struct ocfs2_alloc_reservation *resv,
639                                    unsigned int wanted)
640 {
641         struct ocfs2_alloc_reservation *lru_resv;
642         int tmpwindow = !!(resv->r_flags & OCFS2_RESV_FLAG_TMP);
643         unsigned int min_bits;
644
645         if (!tmpwindow)
646                 min_bits = ocfs2_resv_window_bits(resmap, resv) >> 1;
647         else
648                 min_bits = wanted; /* We at know the temp window will use all
649                                     * of these bits */
650
651         /*
652          * Take the first reservation off the LRU as our 'target'. We
653          * don't try to be smart about it. There might be a case for
654          * searching based on size but I don't have enough data to be
655          * sure. --Mark (3/16/2010)
656          */
657         lru_resv = list_first_entry(&resmap->m_lru,
658                                     struct ocfs2_alloc_reservation, r_lru);
659
660         mlog(0, "lru resv: start: %u len: %u end: %u\n", lru_resv->r_start,
661              lru_resv->r_len, ocfs2_resv_end(lru_resv));
662
663         /*
664          * Cannibalize (some or all) of the target reservation and
665          * feed it to the current window.
666          */
667         if (lru_resv->r_len <= min_bits) {
668                 /*
669                  * Discard completely if size is less than or equal to a
670                  * reasonable threshold - 50% of window bits for non temporary
671                  * windows.
672                  */
673                 resv->r_start = lru_resv->r_start;
674                 resv->r_len = lru_resv->r_len;
675
676                 __ocfs2_resv_discard(resmap, lru_resv);
677         } else {
678                 unsigned int shrink;
679                 if (tmpwindow)
680                         shrink = min_bits;
681                 else
682                         shrink = lru_resv->r_len / 2;
683
684                 lru_resv->r_len -= shrink;
685
686                 resv->r_start = ocfs2_resv_end(lru_resv) + 1;
687                 resv->r_len = shrink;
688         }
689
690         mlog(0, "Reservation now looks like: r_start: %u r_end: %u "
691              "r_len: %u r_last_start: %u r_last_len: %u\n",
692              resv->r_start, ocfs2_resv_end(resv), resv->r_len,
693              resv->r_last_start, resv->r_last_len);
694
695         ocfs2_resv_insert(resmap, resv);
696 }
697
698 static void ocfs2_resv_find_window(struct ocfs2_reservation_map *resmap,
699                                    struct ocfs2_alloc_reservation *resv,
700                                    unsigned int wanted)
701 {
702         unsigned int goal = 0;
703
704         BUG_ON(!ocfs2_resv_empty(resv));
705
706         /*
707          * Begin by trying to get a window as close to the previous
708          * one as possible. Using the most recent allocation as a
709          * start goal makes sense.
710          */
711         if (resv->r_last_len) {
712                 goal = resv->r_last_start + resv->r_last_len;
713                 if (goal >= resmap->m_bitmap_len)
714                         goal = 0;
715         }
716
717         __ocfs2_resv_find_window(resmap, resv, goal, wanted);
718
719         /* Search from last alloc didn't work, try once more from beginning. */
720         if (ocfs2_resv_empty(resv) && goal != 0)
721                 __ocfs2_resv_find_window(resmap, resv, 0, wanted);
722
723         if (ocfs2_resv_empty(resv)) {
724                 /*
725                  * Still empty? Pull oldest one off the LRU, remove it from
726                  * tree, put this one in it's place.
727                  */
728                 ocfs2_cannibalize_resv(resmap, resv, wanted);
729         }
730
731         BUG_ON(ocfs2_resv_empty(resv));
732 }
733
734 int ocfs2_resmap_resv_bits(struct ocfs2_reservation_map *resmap,
735                            struct ocfs2_alloc_reservation *resv,
736                            int *cstart, int *clen)
737 {
738         unsigned int wanted = *clen;
739
740         if (resv == NULL || ocfs2_resmap_disabled(resmap))
741                 return -ENOSPC;
742
743         spin_lock(&resv_lock);
744
745         /*
746          * We don't want to over-allocate for temporary
747          * windows. Otherwise, we run the risk of fragmenting the
748          * allocation space.
749          */
750         wanted = ocfs2_resv_window_bits(resmap, resv);
751         if ((resv->r_flags & OCFS2_RESV_FLAG_TMP) || wanted < *clen)
752                 wanted = *clen;
753
754         if (ocfs2_resv_empty(resv)) {
755                 mlog(0, "empty reservation, find new window\n");
756
757                 /*
758                  * Try to get a window here. If it works, we must fall
759                  * through and test the bitmap . This avoids some
760                  * ping-ponging of windows due to non-reserved space
761                  * being allocation before we initialize a window for
762                  * that inode.
763                  */
764                 ocfs2_resv_find_window(resmap, resv, wanted);
765         }
766
767         BUG_ON(ocfs2_resv_empty(resv));
768
769         *cstart = resv->r_start;
770         *clen = resv->r_len;
771
772         spin_unlock(&resv_lock);
773         return 0;
774 }
775
776 static void
777         ocfs2_adjust_resv_from_alloc(struct ocfs2_reservation_map *resmap,
778                                      struct ocfs2_alloc_reservation *resv,
779                                      unsigned int start, unsigned int end)
780 {
781         unsigned int rhs = 0;
782         unsigned int old_end = ocfs2_resv_end(resv);
783
784         BUG_ON(start != resv->r_start || old_end < end);
785
786         /*
787          * Completely used? We can remove it then.
788          */
789         if (old_end == end) {
790                 __ocfs2_resv_discard(resmap, resv);
791                 return;
792         }
793
794         rhs = old_end - end;
795
796         /*
797          * This should have been trapped above.
798          */
799         BUG_ON(rhs == 0);
800
801         resv->r_start = end + 1;
802         resv->r_len = old_end - resv->r_start + 1;
803 }
804
805 void ocfs2_resmap_claimed_bits(struct ocfs2_reservation_map *resmap,
806                                struct ocfs2_alloc_reservation *resv,
807                                u32 cstart, u32 clen)
808 {
809         unsigned int cend = cstart + clen - 1;
810
811         if (resmap == NULL || ocfs2_resmap_disabled(resmap))
812                 return;
813
814         if (resv == NULL)
815                 return;
816
817         BUG_ON(cstart != resv->r_start);
818
819         spin_lock(&resv_lock);
820
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);
825
826         BUG_ON(cstart < resv->r_start);
827         BUG_ON(cstart > ocfs2_resv_end(resv));
828         BUG_ON(cend > ocfs2_resv_end(resv));
829
830         ocfs2_adjust_resv_from_alloc(resmap, resv, cstart, cend);
831         resv->r_last_start = cstart;
832         resv->r_last_len = clen;
833
834         /*
835          * May have been discarded above from
836          * ocfs2_adjust_resv_from_alloc().
837          */
838         if (!ocfs2_resv_empty(resv))
839                 ocfs2_resv_mark_lru(resmap, resv);
840
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);
845
846         ocfs2_check_resmap(resmap);
847
848         spin_unlock(&resv_lock);
849 }