^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.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Allocation reservations function prototypes and structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (C) 2010 Novell. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #ifndef OCFS2_RESERVATIONS_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define OCFS2_RESERVATIONS_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/rbtree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define OCFS2_DEFAULT_RESV_LEVEL 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define OCFS2_MAX_RESV_LEVEL 9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define OCFS2_MIN_RESV_LEVEL 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct ocfs2_alloc_reservation {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct rb_node r_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) unsigned int r_start; /* Beginning of current window */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) unsigned int r_len; /* Length of the window */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned int r_last_len; /* Length of most recent alloc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned int r_last_start; /* Start of most recent alloc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct list_head r_lru; /* LRU list head */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unsigned int r_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define OCFS2_RESV_FLAG_INUSE 0x01 /* Set when r_node is part of a btree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define OCFS2_RESV_FLAG_TMP 0x02 /* Temporary reservation, will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * destroyed immedately after use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define OCFS2_RESV_FLAG_DIR 0x04 /* Reservation is for an unindexed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * directory btree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct ocfs2_reservation_map {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct rb_root m_reservations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) char *m_disk_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct ocfs2_super *m_osb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* The following are not initialized to meaningful values until a disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * bitmap is provided. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) u32 m_bitmap_len; /* Number of valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * bits available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct list_head m_lru; /* LRU of reservations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * structures. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^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) void ocfs2_resv_init_once(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) #define OCFS2_RESV_TYPES (OCFS2_RESV_FLAG_TMP|OCFS2_RESV_FLAG_DIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) void ocfs2_resv_set_type(struct ocfs2_alloc_reservation *resv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) unsigned int flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int ocfs2_dir_resv_allowed(struct ocfs2_super *osb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * ocfs2_resv_discard() - truncate a reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * @resmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * @resv: the reservation to truncate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * After this function is called, the reservation will be empty, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * unlinked from the rbtree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) void ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct ocfs2_alloc_reservation *resv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * ocfs2_resmap_init() - Initialize fields of a reservations bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * @resmap: struct ocfs2_reservation_map to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * @obj: unused for now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * @ops: unused for now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * @max_bitmap_bytes: Maximum size of the bitmap (typically blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * Only possible return value other than '0' is -ENOMEM for failure to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * allocation mirror bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int ocfs2_resmap_init(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct ocfs2_reservation_map *resmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * ocfs2_resmap_restart() - "restart" a reservation bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * @resmap: reservations bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * @clen: Number of valid bits in the bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * @disk_bitmap: the disk bitmap this resmap should refer to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * Re-initialize the parameters of a reservation bitmap. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * useful for local alloc window slides.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * This function will call ocfs2_trunc_resv against all existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * reservations. A future version will recalculate existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * reservations based on the new bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) void ocfs2_resmap_restart(struct ocfs2_reservation_map *resmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) unsigned int clen, char *disk_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * ocfs2_resmap_uninit() - uninitialize a reservation bitmap structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * @resmap: the struct ocfs2_reservation_map to uninitialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) void ocfs2_resmap_uninit(struct ocfs2_reservation_map *resmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * ocfs2_resmap_resv_bits() - Return still-valid reservation bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * @resmap: reservations bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * @resv: reservation to base search from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * @cstart: start of proposed allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * @clen: length (in clusters) of proposed allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * Using the reservation data from resv, this function will compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * resmap and resmap->m_disk_bitmap to determine what part (if any) of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * the reservation window is still clear to use. If resv is empty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * this function will try to allocate a window for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * On success, zero is returned and the valid allocation area is set in cstart
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * and clen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * Returns -ENOSPC if reservations are disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int ocfs2_resmap_resv_bits(struct ocfs2_reservation_map *resmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct ocfs2_alloc_reservation *resv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int *cstart, int *clen);
^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) * ocfs2_resmap_claimed_bits() - Tell the reservation code that bits were used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * @resmap: reservations bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * @resv: optional reservation to recalulate based on new bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * @cstart: start of allocation in clusters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * @clen: end of allocation in clusters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * Tell the reservation code that bits were used to fulfill allocation in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * resmap. The bits don't have to have been part of any existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * reservation. But we must always call this function when bits are claimed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * Internally, the reservations code will use this information to mark the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * reservations bitmap. If resv is passed, it's next allocation window will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * calculated. It also expects that 'cstart' is the same as we passed back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * from ocfs2_resmap_resv_bits().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) void ocfs2_resmap_claimed_bits(struct ocfs2_reservation_map *resmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct ocfs2_alloc_reservation *resv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) u32 cstart, u32 clen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #endif /* OCFS2_RESERVATIONS_H */