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