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