]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/ocfs2/reservations.c
ocfs2: change default reservation window sizes
[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 #define OCFS2_RESV_DIR_WINDOW_BITS      OCFS2_MIN_RESV_WINDOW_BITS
48
49 static unsigned int ocfs2_resv_window_bits(struct ocfs2_reservation_map *resmap,
50                                            struct ocfs2_alloc_reservation *resv)
51 {
52         struct ocfs2_super *osb = resmap->m_osb;
53         unsigned int bits;
54
55         if (!(resv->r_flags & OCFS2_RESV_FLAG_DIR)) {
56                 /* 8, 16, 32, 64, 128, 256, 512, 1024 */
57                 bits = 4 << osb->osb_resv_level;
58         } else {
59                 /* For now, treat directories the same as files. */
60                 bits = 4 << osb->osb_resv_level;
61         }
62         return bits;
63 }
64
65 static inline unsigned int ocfs2_resv_end(struct ocfs2_alloc_reservation *resv)
66 {
67         if (resv->r_len)
68                 return resv->r_start + resv->r_len - 1;
69         return resv->r_start;
70 }
71
72 static inline int ocfs2_resv_empty(struct ocfs2_alloc_reservation *resv)
73 {
74         return !!(resv->r_len == 0);
75 }
76
77 static inline int ocfs2_resmap_disabled(struct ocfs2_reservation_map *resmap)
78 {
79         if (resmap->m_osb->osb_resv_level == 0)
80                 return 1;
81         return 0;
82 }
83
84 static void ocfs2_dump_resv(struct ocfs2_reservation_map *resmap)
85 {
86         struct ocfs2_super *osb = resmap->m_osb;
87         struct rb_node *node;
88         struct ocfs2_alloc_reservation *resv;
89         int i = 0;
90
91         mlog(ML_NOTICE, "Dumping resmap for device %s. Bitmap length: %u\n",
92              osb->dev_str, resmap->m_bitmap_len);
93
94         node = rb_first(&resmap->m_reservations);
95         while (node) {
96                 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
97
98                 mlog(ML_NOTICE, "start: %u\tend: %u\tlen: %u\tlast_start: %u"
99                      "\tlast_len: %u\n", resv->r_start,
100                      ocfs2_resv_end(resv), resv->r_len, resv->r_last_start,
101                      resv->r_last_len);
102
103                 node = rb_next(node);
104                 i++;
105         }
106
107         mlog(ML_NOTICE, "%d reservations found. LRU follows\n", i);
108
109         i = 0;
110         list_for_each_entry(resv, &resmap->m_lru, r_lru) {
111                 mlog(ML_NOTICE, "LRU(%d) start: %u\tend: %u\tlen: %u\t"
112                      "last_start: %u\tlast_len: %u\n", i, resv->r_start,
113                      ocfs2_resv_end(resv), resv->r_len, resv->r_last_start,
114                      resv->r_last_len);
115
116                 i++;
117         }
118 }
119
120 #ifdef OCFS2_CHECK_RESERVATIONS
121 static int ocfs2_validate_resmap_bits(struct ocfs2_reservation_map *resmap,
122                                       int i,
123                                       struct ocfs2_alloc_reservation *resv)
124 {
125         char *disk_bitmap = resmap->m_disk_bitmap;
126         unsigned int start = resv->r_start;
127         unsigned int end = ocfs2_resv_end(resv);
128
129         while (start <= end) {
130                 if (ocfs2_test_bit(start, disk_bitmap)) {
131                         mlog(ML_ERROR,
132                              "reservation %d covers an allocated area "
133                              "starting at bit %u!\n", i, start);
134                         return 1;
135                 }
136
137                 start++;
138         }
139         return 0;
140 }
141
142 static void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
143 {
144         unsigned int off = 0;
145         int i = 0;
146         struct rb_node *node;
147         struct ocfs2_alloc_reservation *resv;
148
149         node = rb_first(&resmap->m_reservations);
150         while (node) {
151                 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
152
153                 if (i > 0 && resv->r_start <= off) {
154                         mlog(ML_ERROR, "reservation %d has bad start off!\n",
155                              i);
156                         goto bad;
157                 }
158
159                 if (resv->r_len == 0) {
160                         mlog(ML_ERROR, "reservation %d has no length!\n",
161                              i);
162                         goto bad;
163                 }
164
165                 if (resv->r_start > ocfs2_resv_end(resv)) {
166                         mlog(ML_ERROR, "reservation %d has invalid range!\n",
167                              i);
168                         goto bad;
169                 }
170
171                 if (ocfs2_resv_end(resv) >= resmap->m_bitmap_len) {
172                         mlog(ML_ERROR, "reservation %d extends past bitmap!\n",
173                              i);
174                         goto bad;
175                 }
176
177                 if (ocfs2_validate_resmap_bits(resmap, i, resv))
178                         goto bad;
179
180                 off = ocfs2_resv_end(resv);
181                 node = rb_next(node);
182
183                 i++;
184         }
185         return;
186
187 bad:
188         ocfs2_dump_resv(resmap);
189         BUG();
190 }
191 #else
192 static inline void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
193 {
194
195 }
196 #endif
197
198 void ocfs2_resv_init_once(struct ocfs2_alloc_reservation *resv)
199 {
200         memset(resv, 0, sizeof(*resv));
201         INIT_LIST_HEAD(&resv->r_lru);
202 }
203
204 void ocfs2_resv_set_type(struct ocfs2_alloc_reservation *resv,
205                          unsigned int flags)
206 {
207         BUG_ON(flags & ~OCFS2_RESV_TYPES);
208
209         resv->r_flags |= flags;
210 }
211
212 int ocfs2_resmap_init(struct ocfs2_super *osb,
213                       struct ocfs2_reservation_map *resmap)
214 {
215         memset(resmap, 0, sizeof(*resmap));
216
217         resmap->m_osb = osb;
218         resmap->m_reservations = RB_ROOT;
219         /* m_bitmap_len is initialized to zero by the above memset. */
220         INIT_LIST_HEAD(&resmap->m_lru);
221
222         return 0;
223 }
224
225 static void ocfs2_resv_mark_lru(struct ocfs2_reservation_map *resmap,
226                                 struct ocfs2_alloc_reservation *resv)
227 {
228         assert_spin_locked(&resv_lock);
229
230         if (!list_empty(&resv->r_lru))
231                 list_del_init(&resv->r_lru);
232
233         list_add_tail(&resv->r_lru, &resmap->m_lru);
234 }
235
236 static void __ocfs2_resv_trunc(struct ocfs2_alloc_reservation *resv)
237 {
238         resv->r_len = 0;
239         resv->r_start = 0;
240 }
241
242 static void ocfs2_resv_remove(struct ocfs2_reservation_map *resmap,
243                               struct ocfs2_alloc_reservation *resv)
244 {
245         if (resv->r_flags & OCFS2_RESV_FLAG_INUSE) {
246                 list_del_init(&resv->r_lru);
247                 rb_erase(&resv->r_node, &resmap->m_reservations);
248                 resv->r_flags &= ~OCFS2_RESV_FLAG_INUSE;
249         }
250 }
251
252 static void __ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
253                                  struct ocfs2_alloc_reservation *resv)
254 {
255         assert_spin_locked(&resv_lock);
256
257         __ocfs2_resv_trunc(resv);
258         /*
259          * last_len and last_start no longer make sense if
260          * we're changing the range of our allocations.
261          */
262         resv->r_last_len = resv->r_last_start = 0;
263
264         ocfs2_resv_remove(resmap, resv);
265 }
266
267 /* does nothing if 'resv' is null */
268 void ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
269                         struct ocfs2_alloc_reservation *resv)
270 {
271         if (resv) {
272                 spin_lock(&resv_lock);
273                 __ocfs2_resv_discard(resmap, resv);
274                 spin_unlock(&resv_lock);
275         }
276 }
277
278 static void ocfs2_resmap_clear_all_resv(struct ocfs2_reservation_map *resmap)
279 {
280         struct rb_node *node;
281         struct ocfs2_alloc_reservation *resv;
282
283         assert_spin_locked(&resv_lock);
284
285         while ((node = rb_last(&resmap->m_reservations)) != NULL) {
286                 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
287
288                 __ocfs2_resv_discard(resmap, resv);
289         }
290 }
291
292 void ocfs2_resmap_restart(struct ocfs2_reservation_map *resmap,
293                           unsigned int clen, char *disk_bitmap)
294 {
295         if (ocfs2_resmap_disabled(resmap))
296                 return;
297
298         spin_lock(&resv_lock);
299
300         ocfs2_resmap_clear_all_resv(resmap);
301         resmap->m_bitmap_len = clen;
302         resmap->m_disk_bitmap = disk_bitmap;
303
304         spin_unlock(&resv_lock);
305 }
306
307 void ocfs2_resmap_uninit(struct ocfs2_reservation_map *resmap)
308 {
309         /* Does nothing for now. Keep this around for API symmetry */
310 }
311
312 static void ocfs2_resv_insert(struct ocfs2_reservation_map *resmap,
313                               struct ocfs2_alloc_reservation *new)
314 {
315         struct rb_root *root = &resmap->m_reservations;
316         struct rb_node *parent = NULL;
317         struct rb_node **p = &root->rb_node;
318         struct ocfs2_alloc_reservation *tmp;
319
320         assert_spin_locked(&resv_lock);
321
322         mlog(0, "Insert reservation start: %u len: %u\n", new->r_start,
323              new->r_len);
324
325         while (*p) {
326                 parent = *p;
327
328                 tmp = rb_entry(parent, struct ocfs2_alloc_reservation, r_node);
329
330                 if (new->r_start < tmp->r_start) {
331                         p = &(*p)->rb_left;
332
333                         /*
334                          * This is a good place to check for
335                          * overlapping reservations.
336                          */
337                         BUG_ON(ocfs2_resv_end(new) >= tmp->r_start);
338                 } else if (new->r_start > ocfs2_resv_end(tmp)) {
339                         p = &(*p)->rb_right;
340                 } else {
341                         /* This should never happen! */
342                         mlog(ML_ERROR, "Duplicate reservation window!\n");
343                         BUG();
344                 }
345         }
346
347         rb_link_node(&new->r_node, parent, p);
348         rb_insert_color(&new->r_node, root);
349         new->r_flags |= OCFS2_RESV_FLAG_INUSE;
350
351         ocfs2_resv_mark_lru(resmap, new);
352
353         ocfs2_check_resmap(resmap);
354 }
355
356 /**
357  * ocfs2_find_resv_lhs() - find the window which contains goal
358  * @resmap: reservation map to search
359  * @goal: which bit to search for
360  *
361  * If a window containing that goal is not found, we return the window
362  * which comes before goal. Returns NULL on empty rbtree or no window
363  * before goal.
364  */
365 static struct ocfs2_alloc_reservation *
366 ocfs2_find_resv_lhs(struct ocfs2_reservation_map *resmap, unsigned int goal)
367 {
368         struct ocfs2_alloc_reservation *resv = NULL;
369         struct ocfs2_alloc_reservation *prev_resv = NULL;
370         struct rb_node *node = resmap->m_reservations.rb_node;
371         struct rb_node *prev = NULL;
372
373         assert_spin_locked(&resv_lock);
374
375         if (!node)
376                 return NULL;
377
378         node = rb_first(&resmap->m_reservations);
379         while (node) {
380                 resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
381
382                 if (resv->r_start <= goal && ocfs2_resv_end(resv) >= goal)
383                         break;
384
385                 /* Check if we overshot the reservation just before goal? */
386                 if (resv->r_start > goal) {
387                         resv = prev_resv;
388                         break;
389                 }
390
391                 prev_resv = resv;
392                 prev = node;
393                 node = rb_next(node);
394         }
395
396         return resv;
397 }
398
399 /*
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
403  * reservations.
404  *
405  * The start value of *rstart is insignificant.
406  *
407  * This function searches the bitmap range starting at search_start
408  * with length csearch_len for a set of contiguous free bits. We try
409  * to find up to 'wanted' bits, but can sometimes return less.
410  *
411  * Returns the length of allocation, 0 if no free bits are found.
412  *
413  * *cstart and *clen will also be populated with the result.
414  */
415 static 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                 /* Search reached end of the region */
435                 if (offset >= (search_start + search_len))
436                         break;
437
438                 if (offset == start) {
439                         /* we found a zero */
440                         found++;
441                         /* move start to the next bit to test */
442                         start++;
443                 } else {
444                         /* got a zero after some ones */
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
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)
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          * Nasty cases to consider:
484          *
485          * - rbtree is empty
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
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                  * Easiest case - empty tree. We can just take
499                  * whatever window of free bits we want.
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                  * This should never happen - the local alloc window
510                  * will always have free bits when we're called.
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                  * A NULL here means that the search code couldn't
531                  * find a window that starts before goal.
532                  *
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.
538                  *
539                  * Otherwise we fall back to a linear search, checking
540                  * the gaps in between windows for a place to
541                  * allocate.
542                  */
543
544                 next = rb_first(root);
545                 next_resv = rb_entry(next, struct ocfs2_alloc_reservation,
546                                      r_node);
547
548                 /*
549                  * The search should never return such a window. (see
550                  * comment above
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         /* Now we do a linear search for a window, starting at 'prev_rsv' */
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                          * 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.
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                  * No need to check this gap if we have already found
601                  * a larger region of free bits.
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
617 next_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
626 out_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
634 static 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; /* We at know the temp window will use all
646                                     * of these bits */
647
648         /*
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)
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          * Cannibalize (some or all) of the target reservation and
662          * feed it to the current window.
663          */
664         if (lru_resv->r_len <= min_bits) {
665                 /*
666                  * Discard completely if size is less than or equal to a
667                  * reasonable threshold - 50% of window bits for non temporary
668                  * windows.
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
695 static 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          * 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.
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         /* 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);
719
720         if (ocfs2_resv_empty(resv)) {
721                 /*
722                  * Still empty? Pull oldest one off the LRU, remove it from
723                  * tree, put this one in it's place.
724                  */
725                 ocfs2_cannibalize_resv(resmap, resv, wanted);
726         }
727
728         BUG_ON(ocfs2_resv_empty(resv));
729 }
730
731 int ocfs2_resmap_resv_bits(struct ocfs2_reservation_map *resmap,
732                            struct ocfs2_alloc_reservation *resv,
733                            int *cstart, int *clen)
734 {
735         unsigned int wanted = *clen;
736
737         if (resv == NULL || ocfs2_resmap_disabled(resmap))
738                 return -ENOSPC;
739
740         spin_lock(&resv_lock);
741
742         /*
743          * We don't want to over-allocate for temporary
744          * windows. Otherwise, we run the risk of fragmenting the
745          * allocation space.
746          */
747         wanted = ocfs2_resv_window_bits(resmap, resv);
748         if ((resv->r_flags & OCFS2_RESV_FLAG_TMP) || wanted < *clen)
749                 wanted = *clen;
750
751         if (ocfs2_resv_empty(resv)) {
752                 mlog(0, "empty reservation, find new window\n");
753
754                 /*
755                  * Try to get a window here. If it works, we must fall
756                  * through and test the bitmap . This avoids some
757                  * ping-ponging of windows due to non-reserved space
758                  * being allocation before we initialize a window for
759                  * that inode.
760                  */
761                 ocfs2_resv_find_window(resmap, resv, wanted);
762         }
763
764         BUG_ON(ocfs2_resv_empty(resv));
765
766         *cstart = resv->r_start;
767         *clen = resv->r_len;
768
769         spin_unlock(&resv_lock);
770         return 0;
771 }
772
773 static void
774         ocfs2_adjust_resv_from_alloc(struct ocfs2_reservation_map *resmap,
775                                      struct ocfs2_alloc_reservation *resv,
776                                      unsigned int start, unsigned int end)
777 {
778         unsigned int lhs = 0, rhs = 0;
779
780         BUG_ON(start < resv->r_start);
781
782         /*
783          * Completely used? We can remove it then.
784          */
785         if (ocfs2_resv_end(resv) <= end && resv->r_start >= start) {
786                 __ocfs2_resv_discard(resmap, resv);
787                 return;
788         }
789
790         if (end < ocfs2_resv_end(resv))
791                 rhs = end - ocfs2_resv_end(resv);
792
793         if (start > resv->r_start)
794                 lhs = start - resv->r_start;
795
796         /*
797          * This should have been trapped above. At the very least, rhs
798          * should be non zero.
799          */
800         BUG_ON(rhs == 0 && lhs == 0);
801
802         if (rhs >= lhs) {
803                 unsigned int old_end = ocfs2_resv_end(resv);
804
805                 resv->r_start = end + 1;
806                 resv->r_len = old_end - resv->r_start + 1;
807         } else {
808                 resv->r_len = start - resv->r_start;
809         }
810 }
811
812 void ocfs2_resmap_claimed_bits(struct ocfs2_reservation_map *resmap,
813                                struct ocfs2_alloc_reservation *resv,
814                                u32 cstart, u32 clen)
815 {
816         unsigned int cend = cstart + clen - 1;
817
818         if (resmap == NULL || ocfs2_resmap_disabled(resmap))
819                 return;
820
821         if (resv == NULL)
822                 return;
823
824         spin_lock(&resv_lock);
825
826         mlog(0, "claim bits: cstart: %u cend: %u clen: %u r_start: %u "
827              "r_end: %u r_len: %u, r_last_start: %u r_last_len: %u\n",
828              cstart, cend, clen, resv->r_start, ocfs2_resv_end(resv),
829              resv->r_len, resv->r_last_start, resv->r_last_len);
830
831         BUG_ON(cstart < resv->r_start);
832         BUG_ON(cstart > ocfs2_resv_end(resv));
833         BUG_ON(cend > ocfs2_resv_end(resv));
834
835         ocfs2_adjust_resv_from_alloc(resmap, resv, cstart, cend);
836         resv->r_last_start = cstart;
837         resv->r_last_len = clen;
838
839         /*
840          * May have been discarded above from
841          * ocfs2_adjust_resv_from_alloc().
842          */
843         if (!ocfs2_resv_empty(resv))
844                 ocfs2_resv_mark_lru(resmap, resv);
845
846         mlog(0, "Reservation now looks like: r_start: %u r_end: %u "
847              "r_len: %u r_last_start: %u r_last_len: %u\n",
848              resv->r_start, ocfs2_resv_end(resv), resv->r_len,
849              resv->r_last_start, resv->r_last_len);
850
851         ocfs2_check_resmap(resmap);
852
853         spin_unlock(&resv_lock);
854 }