Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * reservations.c
0004  *
0005  * Allocation reservations implementation
0006  *
0007  * Some code borrowed from fs/ext3/balloc.c and is:
0008  *
0009  * Copyright (C) 1992, 1993, 1994, 1995
0010  * Remy Card (card@masi.ibp.fr)
0011  * Laboratoire MASI - Institut Blaise Pascal
0012  * Universite Pierre et Marie Curie (Paris VI)
0013  *
0014  * The rest is copyright (C) 2010 Novell.  All rights reserved.
0015  */
0016 
0017 #include <linux/fs.h>
0018 #include <linux/types.h>
0019 #include <linux/highmem.h>
0020 #include <linux/bitops.h>
0021 #include <linux/list.h>
0022 
0023 #include <cluster/masklog.h>
0024 
0025 #include "ocfs2.h"
0026 #include "ocfs2_trace.h"
0027 
0028 #ifdef CONFIG_OCFS2_DEBUG_FS
0029 #define OCFS2_CHECK_RESERVATIONS
0030 #endif
0031 
0032 static DEFINE_SPINLOCK(resv_lock);
0033 
0034 int ocfs2_dir_resv_allowed(struct ocfs2_super *osb)
0035 {
0036     return (osb->osb_resv_level && osb->osb_dir_resv_level);
0037 }
0038 
0039 static unsigned int ocfs2_resv_window_bits(struct ocfs2_reservation_map *resmap,
0040                        struct ocfs2_alloc_reservation *resv)
0041 {
0042     struct ocfs2_super *osb = resmap->m_osb;
0043     unsigned int bits;
0044 
0045     if (!(resv->r_flags & OCFS2_RESV_FLAG_DIR)) {
0046         /* 8, 16, 32, 64, 128, 256, 512, 1024 */
0047         bits = 4 << osb->osb_resv_level;
0048     } else {
0049         bits = 4 << osb->osb_dir_resv_level;
0050     }
0051     return bits;
0052 }
0053 
0054 static inline unsigned int ocfs2_resv_end(struct ocfs2_alloc_reservation *resv)
0055 {
0056     if (resv->r_len)
0057         return resv->r_start + resv->r_len - 1;
0058     return resv->r_start;
0059 }
0060 
0061 static inline int ocfs2_resv_empty(struct ocfs2_alloc_reservation *resv)
0062 {
0063     return !!(resv->r_len == 0);
0064 }
0065 
0066 static inline int ocfs2_resmap_disabled(struct ocfs2_reservation_map *resmap)
0067 {
0068     if (resmap->m_osb->osb_resv_level == 0)
0069         return 1;
0070     return 0;
0071 }
0072 
0073 static void ocfs2_dump_resv(struct ocfs2_reservation_map *resmap)
0074 {
0075     struct ocfs2_super *osb = resmap->m_osb;
0076     struct rb_node *node;
0077     struct ocfs2_alloc_reservation *resv;
0078     int i = 0;
0079 
0080     mlog(ML_NOTICE, "Dumping resmap for device %s. Bitmap length: %u\n",
0081          osb->dev_str, resmap->m_bitmap_len);
0082 
0083     node = rb_first(&resmap->m_reservations);
0084     while (node) {
0085         resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
0086 
0087         mlog(ML_NOTICE, "start: %u\tend: %u\tlen: %u\tlast_start: %u"
0088              "\tlast_len: %u\n", resv->r_start,
0089              ocfs2_resv_end(resv), resv->r_len, resv->r_last_start,
0090              resv->r_last_len);
0091 
0092         node = rb_next(node);
0093         i++;
0094     }
0095 
0096     mlog(ML_NOTICE, "%d reservations found. LRU follows\n", i);
0097 
0098     i = 0;
0099     list_for_each_entry(resv, &resmap->m_lru, r_lru) {
0100         mlog(ML_NOTICE, "LRU(%d) start: %u\tend: %u\tlen: %u\t"
0101              "last_start: %u\tlast_len: %u\n", i, resv->r_start,
0102              ocfs2_resv_end(resv), resv->r_len, resv->r_last_start,
0103              resv->r_last_len);
0104 
0105         i++;
0106     }
0107 }
0108 
0109 #ifdef OCFS2_CHECK_RESERVATIONS
0110 static int ocfs2_validate_resmap_bits(struct ocfs2_reservation_map *resmap,
0111                       int i,
0112                       struct ocfs2_alloc_reservation *resv)
0113 {
0114     char *disk_bitmap = resmap->m_disk_bitmap;
0115     unsigned int start = resv->r_start;
0116     unsigned int end = ocfs2_resv_end(resv);
0117 
0118     while (start <= end) {
0119         if (ocfs2_test_bit(start, disk_bitmap)) {
0120             mlog(ML_ERROR,
0121                  "reservation %d covers an allocated area "
0122                  "starting at bit %u!\n", i, start);
0123             return 1;
0124         }
0125 
0126         start++;
0127     }
0128     return 0;
0129 }
0130 
0131 static void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
0132 {
0133     unsigned int off = 0;
0134     int i = 0;
0135     struct rb_node *node;
0136     struct ocfs2_alloc_reservation *resv;
0137 
0138     node = rb_first(&resmap->m_reservations);
0139     while (node) {
0140         resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
0141 
0142         if (i > 0 && resv->r_start <= off) {
0143             mlog(ML_ERROR, "reservation %d has bad start off!\n",
0144                  i);
0145             goto bad;
0146         }
0147 
0148         if (resv->r_len == 0) {
0149             mlog(ML_ERROR, "reservation %d has no length!\n",
0150                  i);
0151             goto bad;
0152         }
0153 
0154         if (resv->r_start > ocfs2_resv_end(resv)) {
0155             mlog(ML_ERROR, "reservation %d has invalid range!\n",
0156                  i);
0157             goto bad;
0158         }
0159 
0160         if (ocfs2_resv_end(resv) >= resmap->m_bitmap_len) {
0161             mlog(ML_ERROR, "reservation %d extends past bitmap!\n",
0162                  i);
0163             goto bad;
0164         }
0165 
0166         if (ocfs2_validate_resmap_bits(resmap, i, resv))
0167             goto bad;
0168 
0169         off = ocfs2_resv_end(resv);
0170         node = rb_next(node);
0171 
0172         i++;
0173     }
0174     return;
0175 
0176 bad:
0177     ocfs2_dump_resv(resmap);
0178     BUG();
0179 }
0180 #else
0181 static inline void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
0182 {
0183 
0184 }
0185 #endif
0186 
0187 void ocfs2_resv_init_once(struct ocfs2_alloc_reservation *resv)
0188 {
0189     memset(resv, 0, sizeof(*resv));
0190     INIT_LIST_HEAD(&resv->r_lru);
0191 }
0192 
0193 void ocfs2_resv_set_type(struct ocfs2_alloc_reservation *resv,
0194              unsigned int flags)
0195 {
0196     BUG_ON(flags & ~OCFS2_RESV_TYPES);
0197 
0198     resv->r_flags |= flags;
0199 }
0200 
0201 void ocfs2_resmap_init(struct ocfs2_super *osb,
0202               struct ocfs2_reservation_map *resmap)
0203 {
0204     memset(resmap, 0, sizeof(*resmap));
0205 
0206     resmap->m_osb = osb;
0207     resmap->m_reservations = RB_ROOT;
0208     /* m_bitmap_len is initialized to zero by the above memset. */
0209     INIT_LIST_HEAD(&resmap->m_lru);
0210 }
0211 
0212 static void ocfs2_resv_mark_lru(struct ocfs2_reservation_map *resmap,
0213                 struct ocfs2_alloc_reservation *resv)
0214 {
0215     assert_spin_locked(&resv_lock);
0216 
0217     if (!list_empty(&resv->r_lru))
0218         list_del_init(&resv->r_lru);
0219 
0220     list_add_tail(&resv->r_lru, &resmap->m_lru);
0221 }
0222 
0223 static void __ocfs2_resv_trunc(struct ocfs2_alloc_reservation *resv)
0224 {
0225     resv->r_len = 0;
0226     resv->r_start = 0;
0227 }
0228 
0229 static void ocfs2_resv_remove(struct ocfs2_reservation_map *resmap,
0230                   struct ocfs2_alloc_reservation *resv)
0231 {
0232     if (resv->r_flags & OCFS2_RESV_FLAG_INUSE) {
0233         list_del_init(&resv->r_lru);
0234         rb_erase(&resv->r_node, &resmap->m_reservations);
0235         resv->r_flags &= ~OCFS2_RESV_FLAG_INUSE;
0236     }
0237 }
0238 
0239 static void __ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
0240                  struct ocfs2_alloc_reservation *resv)
0241 {
0242     assert_spin_locked(&resv_lock);
0243 
0244     __ocfs2_resv_trunc(resv);
0245     /*
0246      * last_len and last_start no longer make sense if
0247      * we're changing the range of our allocations.
0248      */
0249     resv->r_last_len = resv->r_last_start = 0;
0250 
0251     ocfs2_resv_remove(resmap, resv);
0252 }
0253 
0254 /* does nothing if 'resv' is null */
0255 void ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
0256             struct ocfs2_alloc_reservation *resv)
0257 {
0258     if (resv) {
0259         spin_lock(&resv_lock);
0260         __ocfs2_resv_discard(resmap, resv);
0261         spin_unlock(&resv_lock);
0262     }
0263 }
0264 
0265 static void ocfs2_resmap_clear_all_resv(struct ocfs2_reservation_map *resmap)
0266 {
0267     struct rb_node *node;
0268     struct ocfs2_alloc_reservation *resv;
0269 
0270     assert_spin_locked(&resv_lock);
0271 
0272     while ((node = rb_last(&resmap->m_reservations)) != NULL) {
0273         resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
0274 
0275         __ocfs2_resv_discard(resmap, resv);
0276     }
0277 }
0278 
0279 void ocfs2_resmap_restart(struct ocfs2_reservation_map *resmap,
0280               unsigned int clen, char *disk_bitmap)
0281 {
0282     if (ocfs2_resmap_disabled(resmap))
0283         return;
0284 
0285     spin_lock(&resv_lock);
0286 
0287     ocfs2_resmap_clear_all_resv(resmap);
0288     resmap->m_bitmap_len = clen;
0289     resmap->m_disk_bitmap = disk_bitmap;
0290 
0291     spin_unlock(&resv_lock);
0292 }
0293 
0294 void ocfs2_resmap_uninit(struct ocfs2_reservation_map *resmap)
0295 {
0296     /* Does nothing for now. Keep this around for API symmetry */
0297 }
0298 
0299 static void ocfs2_resv_insert(struct ocfs2_reservation_map *resmap,
0300                   struct ocfs2_alloc_reservation *new)
0301 {
0302     struct rb_root *root = &resmap->m_reservations;
0303     struct rb_node *parent = NULL;
0304     struct rb_node **p = &root->rb_node;
0305     struct ocfs2_alloc_reservation *tmp;
0306 
0307     assert_spin_locked(&resv_lock);
0308 
0309     trace_ocfs2_resv_insert(new->r_start, new->r_len);
0310 
0311     while (*p) {
0312         parent = *p;
0313 
0314         tmp = rb_entry(parent, struct ocfs2_alloc_reservation, r_node);
0315 
0316         if (new->r_start < tmp->r_start) {
0317             p = &(*p)->rb_left;
0318 
0319             /*
0320              * This is a good place to check for
0321              * overlapping reservations.
0322              */
0323             BUG_ON(ocfs2_resv_end(new) >= tmp->r_start);
0324         } else if (new->r_start > ocfs2_resv_end(tmp)) {
0325             p = &(*p)->rb_right;
0326         } else {
0327             /* This should never happen! */
0328             mlog(ML_ERROR, "Duplicate reservation window!\n");
0329             BUG();
0330         }
0331     }
0332 
0333     rb_link_node(&new->r_node, parent, p);
0334     rb_insert_color(&new->r_node, root);
0335     new->r_flags |= OCFS2_RESV_FLAG_INUSE;
0336 
0337     ocfs2_resv_mark_lru(resmap, new);
0338 
0339     ocfs2_check_resmap(resmap);
0340 }
0341 
0342 /**
0343  * ocfs2_find_resv_lhs() - find the window which contains goal
0344  * @resmap: reservation map to search
0345  * @goal: which bit to search for
0346  *
0347  * If a window containing that goal is not found, we return the window
0348  * which comes before goal. Returns NULL on empty rbtree or no window
0349  * before goal.
0350  */
0351 static struct ocfs2_alloc_reservation *
0352 ocfs2_find_resv_lhs(struct ocfs2_reservation_map *resmap, unsigned int goal)
0353 {
0354     struct ocfs2_alloc_reservation *resv = NULL;
0355     struct ocfs2_alloc_reservation *prev_resv = NULL;
0356     struct rb_node *node = resmap->m_reservations.rb_node;
0357 
0358     assert_spin_locked(&resv_lock);
0359 
0360     if (!node)
0361         return NULL;
0362 
0363     node = rb_first(&resmap->m_reservations);
0364     while (node) {
0365         resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
0366 
0367         if (resv->r_start <= goal && ocfs2_resv_end(resv) >= goal)
0368             break;
0369 
0370         /* Check if we overshot the reservation just before goal? */
0371         if (resv->r_start > goal) {
0372             resv = prev_resv;
0373             break;
0374         }
0375 
0376         prev_resv = resv;
0377         node = rb_next(node);
0378     }
0379 
0380     return resv;
0381 }
0382 
0383 /*
0384  * We are given a range within the bitmap, which corresponds to a gap
0385  * inside the reservations tree (search_start, search_len). The range
0386  * can be anything from the whole bitmap, to a gap between
0387  * reservations.
0388  *
0389  * The start value of *rstart is insignificant.
0390  *
0391  * This function searches the bitmap range starting at search_start
0392  * with length search_len for a set of contiguous free bits. We try
0393  * to find up to 'wanted' bits, but can sometimes return less.
0394  *
0395  * Returns the length of allocation, 0 if no free bits are found.
0396  *
0397  * *cstart and *clen will also be populated with the result.
0398  */
0399 static int ocfs2_resmap_find_free_bits(struct ocfs2_reservation_map *resmap,
0400                        unsigned int wanted,
0401                        unsigned int search_start,
0402                        unsigned int search_len,
0403                        unsigned int *rstart,
0404                        unsigned int *rlen)
0405 {
0406     void *bitmap = resmap->m_disk_bitmap;
0407     unsigned int best_start, best_len = 0;
0408     int offset, start, found;
0409 
0410     trace_ocfs2_resmap_find_free_bits_begin(search_start, search_len,
0411                         wanted, resmap->m_bitmap_len);
0412 
0413     found = best_start = best_len = 0;
0414 
0415     start = search_start;
0416     while ((offset = ocfs2_find_next_zero_bit(bitmap, resmap->m_bitmap_len,
0417                          start)) != -1) {
0418         /* Search reached end of the region */
0419         if (offset >= (search_start + search_len))
0420             break;
0421 
0422         if (offset == start) {
0423             /* we found a zero */
0424             found++;
0425             /* move start to the next bit to test */
0426             start++;
0427         } else {
0428             /* got a zero after some ones */
0429             found = 1;
0430             start = offset + 1;
0431         }
0432         if (found > best_len) {
0433             best_len = found;
0434             best_start = start - found;
0435         }
0436 
0437         if (found >= wanted)
0438             break;
0439     }
0440 
0441     if (best_len == 0)
0442         return 0;
0443 
0444     if (best_len >= wanted)
0445         best_len = wanted;
0446 
0447     *rlen = best_len;
0448     *rstart = best_start;
0449 
0450     trace_ocfs2_resmap_find_free_bits_end(best_start, best_len);
0451 
0452     return *rlen;
0453 }
0454 
0455 static void __ocfs2_resv_find_window(struct ocfs2_reservation_map *resmap,
0456                      struct ocfs2_alloc_reservation *resv,
0457                      unsigned int goal, unsigned int wanted)
0458 {
0459     struct rb_root *root = &resmap->m_reservations;
0460     unsigned int gap_start, gap_end, gap_len;
0461     struct ocfs2_alloc_reservation *prev_resv, *next_resv;
0462     struct rb_node *prev, *next;
0463     unsigned int cstart, clen;
0464     unsigned int best_start = 0, best_len = 0;
0465 
0466     /*
0467      * Nasty cases to consider:
0468      *
0469      * - rbtree is empty
0470      * - our window should be first in all reservations
0471      * - our window should be last in all reservations
0472      * - need to make sure we don't go past end of bitmap
0473      */
0474     trace_ocfs2_resv_find_window_begin(resv->r_start, ocfs2_resv_end(resv),
0475                        goal, wanted, RB_EMPTY_ROOT(root));
0476 
0477     assert_spin_locked(&resv_lock);
0478 
0479     if (RB_EMPTY_ROOT(root)) {
0480         /*
0481          * Easiest case - empty tree. We can just take
0482          * whatever window of free bits we want.
0483          */
0484         clen = ocfs2_resmap_find_free_bits(resmap, wanted, goal,
0485                            resmap->m_bitmap_len - goal,
0486                            &cstart, &clen);
0487 
0488         /*
0489          * This should never happen - the local alloc window
0490          * will always have free bits when we're called.
0491          */
0492         BUG_ON(goal == 0 && clen == 0);
0493 
0494         if (clen == 0)
0495             return;
0496 
0497         resv->r_start = cstart;
0498         resv->r_len = clen;
0499 
0500         ocfs2_resv_insert(resmap, resv);
0501         return;
0502     }
0503 
0504     prev_resv = ocfs2_find_resv_lhs(resmap, goal);
0505 
0506     if (prev_resv == NULL) {
0507         /*
0508          * A NULL here means that the search code couldn't
0509          * find a window that starts before goal.
0510          *
0511          * However, we can take the first window after goal,
0512          * which is also by definition, the leftmost window in
0513          * the entire tree. If we can find free bits in the
0514          * gap between goal and the LHS window, then the
0515          * reservation can safely be placed there.
0516          *
0517          * Otherwise we fall back to a linear search, checking
0518          * the gaps in between windows for a place to
0519          * allocate.
0520          */
0521 
0522         next = rb_first(root);
0523         next_resv = rb_entry(next, struct ocfs2_alloc_reservation,
0524                      r_node);
0525 
0526         /*
0527          * The search should never return such a window. (see
0528          * comment above
0529          */
0530         if (next_resv->r_start <= goal) {
0531             mlog(ML_ERROR, "goal: %u next_resv: start %u len %u\n",
0532                  goal, next_resv->r_start, next_resv->r_len);
0533             ocfs2_dump_resv(resmap);
0534             BUG();
0535         }
0536 
0537         clen = ocfs2_resmap_find_free_bits(resmap, wanted, goal,
0538                            next_resv->r_start - goal,
0539                            &cstart, &clen);
0540         if (clen) {
0541             best_len = clen;
0542             best_start = cstart;
0543             if (best_len == wanted)
0544                 goto out_insert;
0545         }
0546 
0547         prev_resv = next_resv;
0548         next_resv = NULL;
0549     }
0550 
0551     trace_ocfs2_resv_find_window_prev(prev_resv->r_start,
0552                       ocfs2_resv_end(prev_resv));
0553 
0554     prev = &prev_resv->r_node;
0555 
0556     /* Now we do a linear search for a window, starting at 'prev_rsv' */
0557     while (1) {
0558         next = rb_next(prev);
0559         if (next) {
0560             next_resv = rb_entry(next,
0561                          struct ocfs2_alloc_reservation,
0562                          r_node);
0563 
0564             gap_start = ocfs2_resv_end(prev_resv) + 1;
0565             gap_end = next_resv->r_start - 1;
0566             gap_len = gap_end - gap_start + 1;
0567         } else {
0568             /*
0569              * We're at the rightmost edge of the
0570              * tree. See if a reservation between this
0571              * window and the end of the bitmap will work.
0572              */
0573             gap_start = ocfs2_resv_end(prev_resv) + 1;
0574             gap_len = resmap->m_bitmap_len - gap_start;
0575             gap_end = resmap->m_bitmap_len - 1;
0576         }
0577 
0578         trace_ocfs2_resv_find_window_next(next ? next_resv->r_start: -1,
0579                     next ? ocfs2_resv_end(next_resv) : -1);
0580         /*
0581          * No need to check this gap if we have already found
0582          * a larger region of free bits.
0583          */
0584         if (gap_len <= best_len)
0585             goto next_resv;
0586 
0587         clen = ocfs2_resmap_find_free_bits(resmap, wanted, gap_start,
0588                            gap_len, &cstart, &clen);
0589         if (clen == wanted) {
0590             best_len = clen;
0591             best_start = cstart;
0592             goto out_insert;
0593         } else if (clen > best_len) {
0594             best_len = clen;
0595             best_start = cstart;
0596         }
0597 
0598 next_resv:
0599         if (!next)
0600             break;
0601 
0602         prev = next;
0603         prev_resv = rb_entry(prev, struct ocfs2_alloc_reservation,
0604                      r_node);
0605     }
0606 
0607 out_insert:
0608     if (best_len) {
0609         resv->r_start = best_start;
0610         resv->r_len = best_len;
0611         ocfs2_resv_insert(resmap, resv);
0612     }
0613 }
0614 
0615 static void ocfs2_cannibalize_resv(struct ocfs2_reservation_map *resmap,
0616                    struct ocfs2_alloc_reservation *resv,
0617                    unsigned int wanted)
0618 {
0619     struct ocfs2_alloc_reservation *lru_resv;
0620     int tmpwindow = !!(resv->r_flags & OCFS2_RESV_FLAG_TMP);
0621     unsigned int min_bits;
0622 
0623     if (!tmpwindow)
0624         min_bits = ocfs2_resv_window_bits(resmap, resv) >> 1;
0625     else
0626         min_bits = wanted; /* We at know the temp window will use all
0627                     * of these bits */
0628 
0629     /*
0630      * Take the first reservation off the LRU as our 'target'. We
0631      * don't try to be smart about it. There might be a case for
0632      * searching based on size but I don't have enough data to be
0633      * sure. --Mark (3/16/2010)
0634      */
0635     lru_resv = list_first_entry(&resmap->m_lru,
0636                     struct ocfs2_alloc_reservation, r_lru);
0637 
0638     trace_ocfs2_cannibalize_resv_begin(lru_resv->r_start,
0639                        lru_resv->r_len,
0640                        ocfs2_resv_end(lru_resv));
0641 
0642     /*
0643      * Cannibalize (some or all) of the target reservation and
0644      * feed it to the current window.
0645      */
0646     if (lru_resv->r_len <= min_bits) {
0647         /*
0648          * Discard completely if size is less than or equal to a
0649          * reasonable threshold - 50% of window bits for non temporary
0650          * windows.
0651          */
0652         resv->r_start = lru_resv->r_start;
0653         resv->r_len = lru_resv->r_len;
0654 
0655         __ocfs2_resv_discard(resmap, lru_resv);
0656     } else {
0657         unsigned int shrink;
0658         if (tmpwindow)
0659             shrink = min_bits;
0660         else
0661             shrink = lru_resv->r_len / 2;
0662 
0663         lru_resv->r_len -= shrink;
0664 
0665         resv->r_start = ocfs2_resv_end(lru_resv) + 1;
0666         resv->r_len = shrink;
0667     }
0668 
0669     trace_ocfs2_cannibalize_resv_end(resv->r_start, ocfs2_resv_end(resv),
0670                      resv->r_len, resv->r_last_start,
0671                      resv->r_last_len);
0672 
0673     ocfs2_resv_insert(resmap, resv);
0674 }
0675 
0676 static void ocfs2_resv_find_window(struct ocfs2_reservation_map *resmap,
0677                    struct ocfs2_alloc_reservation *resv,
0678                    unsigned int wanted)
0679 {
0680     unsigned int goal = 0;
0681 
0682     BUG_ON(!ocfs2_resv_empty(resv));
0683 
0684     /*
0685      * Begin by trying to get a window as close to the previous
0686      * one as possible. Using the most recent allocation as a
0687      * start goal makes sense.
0688      */
0689     if (resv->r_last_len) {
0690         goal = resv->r_last_start + resv->r_last_len;
0691         if (goal >= resmap->m_bitmap_len)
0692             goal = 0;
0693     }
0694 
0695     __ocfs2_resv_find_window(resmap, resv, goal, wanted);
0696 
0697     /* Search from last alloc didn't work, try once more from beginning. */
0698     if (ocfs2_resv_empty(resv) && goal != 0)
0699         __ocfs2_resv_find_window(resmap, resv, 0, wanted);
0700 
0701     if (ocfs2_resv_empty(resv)) {
0702         /*
0703          * Still empty? Pull oldest one off the LRU, remove it from
0704          * tree, put this one in it's place.
0705          */
0706         ocfs2_cannibalize_resv(resmap, resv, wanted);
0707     }
0708 
0709     BUG_ON(ocfs2_resv_empty(resv));
0710 }
0711 
0712 int ocfs2_resmap_resv_bits(struct ocfs2_reservation_map *resmap,
0713                struct ocfs2_alloc_reservation *resv,
0714                int *cstart, int *clen)
0715 {
0716     if (resv == NULL || ocfs2_resmap_disabled(resmap))
0717         return -ENOSPC;
0718 
0719     spin_lock(&resv_lock);
0720 
0721     if (ocfs2_resv_empty(resv)) {
0722         /*
0723          * We don't want to over-allocate for temporary
0724          * windows. Otherwise, we run the risk of fragmenting the
0725          * allocation space.
0726          */
0727         unsigned int wanted = ocfs2_resv_window_bits(resmap, resv);
0728 
0729         if ((resv->r_flags & OCFS2_RESV_FLAG_TMP) || wanted < *clen)
0730             wanted = *clen;
0731 
0732         /*
0733          * Try to get a window here. If it works, we must fall
0734          * through and test the bitmap . This avoids some
0735          * ping-ponging of windows due to non-reserved space
0736          * being allocation before we initialize a window for
0737          * that inode.
0738          */
0739         ocfs2_resv_find_window(resmap, resv, wanted);
0740         trace_ocfs2_resmap_resv_bits(resv->r_start, resv->r_len);
0741     }
0742 
0743     BUG_ON(ocfs2_resv_empty(resv));
0744 
0745     *cstart = resv->r_start;
0746     *clen = resv->r_len;
0747 
0748     spin_unlock(&resv_lock);
0749     return 0;
0750 }
0751 
0752 static void
0753     ocfs2_adjust_resv_from_alloc(struct ocfs2_reservation_map *resmap,
0754                      struct ocfs2_alloc_reservation *resv,
0755                      unsigned int start, unsigned int end)
0756 {
0757     unsigned int rhs = 0;
0758     unsigned int old_end = ocfs2_resv_end(resv);
0759 
0760     BUG_ON(start != resv->r_start || old_end < end);
0761 
0762     /*
0763      * Completely used? We can remove it then.
0764      */
0765     if (old_end == end) {
0766         __ocfs2_resv_discard(resmap, resv);
0767         return;
0768     }
0769 
0770     rhs = old_end - end;
0771 
0772     /*
0773      * This should have been trapped above.
0774      */
0775     BUG_ON(rhs == 0);
0776 
0777     resv->r_start = end + 1;
0778     resv->r_len = old_end - resv->r_start + 1;
0779 }
0780 
0781 void ocfs2_resmap_claimed_bits(struct ocfs2_reservation_map *resmap,
0782                    struct ocfs2_alloc_reservation *resv,
0783                    u32 cstart, u32 clen)
0784 {
0785     unsigned int cend = cstart + clen - 1;
0786 
0787     if (resmap == NULL || ocfs2_resmap_disabled(resmap))
0788         return;
0789 
0790     if (resv == NULL)
0791         return;
0792 
0793     BUG_ON(cstart != resv->r_start);
0794 
0795     spin_lock(&resv_lock);
0796 
0797     trace_ocfs2_resmap_claimed_bits_begin(cstart, cend, clen, resv->r_start,
0798                           ocfs2_resv_end(resv), resv->r_len,
0799                           resv->r_last_start,
0800                           resv->r_last_len);
0801 
0802     BUG_ON(cstart < resv->r_start);
0803     BUG_ON(cstart > ocfs2_resv_end(resv));
0804     BUG_ON(cend > ocfs2_resv_end(resv));
0805 
0806     ocfs2_adjust_resv_from_alloc(resmap, resv, cstart, cend);
0807     resv->r_last_start = cstart;
0808     resv->r_last_len = clen;
0809 
0810     /*
0811      * May have been discarded above from
0812      * ocfs2_adjust_resv_from_alloc().
0813      */
0814     if (!ocfs2_resv_empty(resv))
0815         ocfs2_resv_mark_lru(resmap, resv);
0816 
0817     trace_ocfs2_resmap_claimed_bits_end(resv->r_start, ocfs2_resv_end(resv),
0818                         resv->r_len, resv->r_last_start,
0819                         resv->r_last_len);
0820 
0821     ocfs2_check_resmap(resmap);
0822 
0823     spin_unlock(&resv_lock);
0824 }