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