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