18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 28c2ecf20Sopenharmony_ci/* -*- mode: c; c-basic-offset: 8; -*- 38c2ecf20Sopenharmony_ci * vim: noexpandtab sw=8 ts=8 sts=0: 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * reservations.h 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Allocation reservations function prototypes and structures. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Copyright (C) 2010 Novell. All rights reserved. 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#ifndef OCFS2_RESERVATIONS_H 138c2ecf20Sopenharmony_ci#define OCFS2_RESERVATIONS_H 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include <linux/rbtree.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#define OCFS2_DEFAULT_RESV_LEVEL 2 188c2ecf20Sopenharmony_ci#define OCFS2_MAX_RESV_LEVEL 9 198c2ecf20Sopenharmony_ci#define OCFS2_MIN_RESV_LEVEL 0 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistruct ocfs2_alloc_reservation { 228c2ecf20Sopenharmony_ci struct rb_node r_node; 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci unsigned int r_start; /* Beginning of current window */ 258c2ecf20Sopenharmony_ci unsigned int r_len; /* Length of the window */ 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci unsigned int r_last_len; /* Length of most recent alloc */ 288c2ecf20Sopenharmony_ci unsigned int r_last_start; /* Start of most recent alloc */ 298c2ecf20Sopenharmony_ci struct list_head r_lru; /* LRU list head */ 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci unsigned int r_flags; 328c2ecf20Sopenharmony_ci}; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci#define OCFS2_RESV_FLAG_INUSE 0x01 /* Set when r_node is part of a btree */ 358c2ecf20Sopenharmony_ci#define OCFS2_RESV_FLAG_TMP 0x02 /* Temporary reservation, will be 368c2ecf20Sopenharmony_ci * destroyed immedately after use */ 378c2ecf20Sopenharmony_ci#define OCFS2_RESV_FLAG_DIR 0x04 /* Reservation is for an unindexed 388c2ecf20Sopenharmony_ci * directory btree */ 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistruct ocfs2_reservation_map { 418c2ecf20Sopenharmony_ci struct rb_root m_reservations; 428c2ecf20Sopenharmony_ci char *m_disk_bitmap; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci struct ocfs2_super *m_osb; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci /* The following are not initialized to meaningful values until a disk 478c2ecf20Sopenharmony_ci * bitmap is provided. */ 488c2ecf20Sopenharmony_ci u32 m_bitmap_len; /* Number of valid 498c2ecf20Sopenharmony_ci * bits available */ 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci struct list_head m_lru; /* LRU of reservations 528c2ecf20Sopenharmony_ci * structures. */ 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci}; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_civoid ocfs2_resv_init_once(struct ocfs2_alloc_reservation *resv); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#define OCFS2_RESV_TYPES (OCFS2_RESV_FLAG_TMP|OCFS2_RESV_FLAG_DIR) 598c2ecf20Sopenharmony_civoid ocfs2_resv_set_type(struct ocfs2_alloc_reservation *resv, 608c2ecf20Sopenharmony_ci unsigned int flags); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ciint ocfs2_dir_resv_allowed(struct ocfs2_super *osb); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci/** 658c2ecf20Sopenharmony_ci * ocfs2_resv_discard() - truncate a reservation 668c2ecf20Sopenharmony_ci * @resmap: 678c2ecf20Sopenharmony_ci * @resv: the reservation to truncate. 688c2ecf20Sopenharmony_ci * 698c2ecf20Sopenharmony_ci * After this function is called, the reservation will be empty, and 708c2ecf20Sopenharmony_ci * unlinked from the rbtree. 718c2ecf20Sopenharmony_ci */ 728c2ecf20Sopenharmony_civoid ocfs2_resv_discard(struct ocfs2_reservation_map *resmap, 738c2ecf20Sopenharmony_ci struct ocfs2_alloc_reservation *resv); 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci/** 778c2ecf20Sopenharmony_ci * ocfs2_resmap_init() - Initialize fields of a reservations bitmap 788c2ecf20Sopenharmony_ci * @resmap: struct ocfs2_reservation_map to initialize 798c2ecf20Sopenharmony_ci * @obj: unused for now 808c2ecf20Sopenharmony_ci * @ops: unused for now 818c2ecf20Sopenharmony_ci * @max_bitmap_bytes: Maximum size of the bitmap (typically blocksize) 828c2ecf20Sopenharmony_ci * 838c2ecf20Sopenharmony_ci * Only possible return value other than '0' is -ENOMEM for failure to 848c2ecf20Sopenharmony_ci * allocation mirror bitmap. 858c2ecf20Sopenharmony_ci */ 868c2ecf20Sopenharmony_ciint ocfs2_resmap_init(struct ocfs2_super *osb, 878c2ecf20Sopenharmony_ci struct ocfs2_reservation_map *resmap); 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci/** 908c2ecf20Sopenharmony_ci * ocfs2_resmap_restart() - "restart" a reservation bitmap 918c2ecf20Sopenharmony_ci * @resmap: reservations bitmap 928c2ecf20Sopenharmony_ci * @clen: Number of valid bits in the bitmap 938c2ecf20Sopenharmony_ci * @disk_bitmap: the disk bitmap this resmap should refer to. 948c2ecf20Sopenharmony_ci * 958c2ecf20Sopenharmony_ci * Re-initialize the parameters of a reservation bitmap. This is 968c2ecf20Sopenharmony_ci * useful for local alloc window slides. 978c2ecf20Sopenharmony_ci * 988c2ecf20Sopenharmony_ci * This function will call ocfs2_trunc_resv against all existing 998c2ecf20Sopenharmony_ci * reservations. A future version will recalculate existing 1008c2ecf20Sopenharmony_ci * reservations based on the new bitmap. 1018c2ecf20Sopenharmony_ci */ 1028c2ecf20Sopenharmony_civoid ocfs2_resmap_restart(struct ocfs2_reservation_map *resmap, 1038c2ecf20Sopenharmony_ci unsigned int clen, char *disk_bitmap); 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci/** 1068c2ecf20Sopenharmony_ci * ocfs2_resmap_uninit() - uninitialize a reservation bitmap structure 1078c2ecf20Sopenharmony_ci * @resmap: the struct ocfs2_reservation_map to uninitialize 1088c2ecf20Sopenharmony_ci */ 1098c2ecf20Sopenharmony_civoid ocfs2_resmap_uninit(struct ocfs2_reservation_map *resmap); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci/** 1128c2ecf20Sopenharmony_ci * ocfs2_resmap_resv_bits() - Return still-valid reservation bits 1138c2ecf20Sopenharmony_ci * @resmap: reservations bitmap 1148c2ecf20Sopenharmony_ci * @resv: reservation to base search from 1158c2ecf20Sopenharmony_ci * @cstart: start of proposed allocation 1168c2ecf20Sopenharmony_ci * @clen: length (in clusters) of proposed allocation 1178c2ecf20Sopenharmony_ci * 1188c2ecf20Sopenharmony_ci * Using the reservation data from resv, this function will compare 1198c2ecf20Sopenharmony_ci * resmap and resmap->m_disk_bitmap to determine what part (if any) of 1208c2ecf20Sopenharmony_ci * the reservation window is still clear to use. If resv is empty, 1218c2ecf20Sopenharmony_ci * this function will try to allocate a window for it. 1228c2ecf20Sopenharmony_ci * 1238c2ecf20Sopenharmony_ci * On success, zero is returned and the valid allocation area is set in cstart 1248c2ecf20Sopenharmony_ci * and clen. 1258c2ecf20Sopenharmony_ci * 1268c2ecf20Sopenharmony_ci * Returns -ENOSPC if reservations are disabled. 1278c2ecf20Sopenharmony_ci */ 1288c2ecf20Sopenharmony_ciint ocfs2_resmap_resv_bits(struct ocfs2_reservation_map *resmap, 1298c2ecf20Sopenharmony_ci struct ocfs2_alloc_reservation *resv, 1308c2ecf20Sopenharmony_ci int *cstart, int *clen); 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci/** 1338c2ecf20Sopenharmony_ci * ocfs2_resmap_claimed_bits() - Tell the reservation code that bits were used. 1348c2ecf20Sopenharmony_ci * @resmap: reservations bitmap 1358c2ecf20Sopenharmony_ci * @resv: optional reservation to recalulate based on new bitmap 1368c2ecf20Sopenharmony_ci * @cstart: start of allocation in clusters 1378c2ecf20Sopenharmony_ci * @clen: end of allocation in clusters. 1388c2ecf20Sopenharmony_ci * 1398c2ecf20Sopenharmony_ci * Tell the reservation code that bits were used to fulfill allocation in 1408c2ecf20Sopenharmony_ci * resmap. The bits don't have to have been part of any existing 1418c2ecf20Sopenharmony_ci * reservation. But we must always call this function when bits are claimed. 1428c2ecf20Sopenharmony_ci * Internally, the reservations code will use this information to mark the 1438c2ecf20Sopenharmony_ci * reservations bitmap. If resv is passed, it's next allocation window will be 1448c2ecf20Sopenharmony_ci * calculated. It also expects that 'cstart' is the same as we passed back 1458c2ecf20Sopenharmony_ci * from ocfs2_resmap_resv_bits(). 1468c2ecf20Sopenharmony_ci */ 1478c2ecf20Sopenharmony_civoid ocfs2_resmap_claimed_bits(struct ocfs2_reservation_map *resmap, 1488c2ecf20Sopenharmony_ci struct ocfs2_alloc_reservation *resv, 1498c2ecf20Sopenharmony_ci u32 cstart, u32 clen); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci#endif /* OCFS2_RESERVATIONS_H */ 152