162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * reservations.h
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Allocation reservations function prototypes and structures.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright (C) 2010 Novell.  All rights reserved.
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#ifndef	OCFS2_RESERVATIONS_H
1162306a36Sopenharmony_ci#define	OCFS2_RESERVATIONS_H
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/rbtree.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#define OCFS2_DEFAULT_RESV_LEVEL	2
1662306a36Sopenharmony_ci#define OCFS2_MAX_RESV_LEVEL	9
1762306a36Sopenharmony_ci#define OCFS2_MIN_RESV_LEVEL	0
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistruct ocfs2_alloc_reservation {
2062306a36Sopenharmony_ci	struct rb_node	r_node;
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci	unsigned int	r_start;	/* Beginning of current window */
2362306a36Sopenharmony_ci	unsigned int	r_len;		/* Length of the window */
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci	unsigned int	r_last_len;	/* Length of most recent alloc */
2662306a36Sopenharmony_ci	unsigned int	r_last_start;	/* Start of most recent alloc */
2762306a36Sopenharmony_ci	struct list_head	r_lru;	/* LRU list head */
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci	unsigned int	r_flags;
3062306a36Sopenharmony_ci};
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci#define	OCFS2_RESV_FLAG_INUSE	0x01	/* Set when r_node is part of a btree */
3362306a36Sopenharmony_ci#define	OCFS2_RESV_FLAG_TMP	0x02	/* Temporary reservation, will be
3462306a36Sopenharmony_ci					 * destroyed immedately after use */
3562306a36Sopenharmony_ci#define	OCFS2_RESV_FLAG_DIR	0x04	/* Reservation is for an unindexed
3662306a36Sopenharmony_ci					 * directory btree */
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistruct ocfs2_reservation_map {
3962306a36Sopenharmony_ci	struct rb_root		m_reservations;
4062306a36Sopenharmony_ci	char			*m_disk_bitmap;
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci	struct ocfs2_super	*m_osb;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	/* The following are not initialized to meaningful values until a disk
4562306a36Sopenharmony_ci	 * bitmap is provided. */
4662306a36Sopenharmony_ci	u32			m_bitmap_len;	/* Number of valid
4762306a36Sopenharmony_ci						 * bits available */
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	struct list_head	m_lru;		/* LRU of reservations
5062306a36Sopenharmony_ci						 * structures. */
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci};
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_civoid ocfs2_resv_init_once(struct ocfs2_alloc_reservation *resv);
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#define OCFS2_RESV_TYPES	(OCFS2_RESV_FLAG_TMP|OCFS2_RESV_FLAG_DIR)
5762306a36Sopenharmony_civoid ocfs2_resv_set_type(struct ocfs2_alloc_reservation *resv,
5862306a36Sopenharmony_ci			 unsigned int flags);
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ciint ocfs2_dir_resv_allowed(struct ocfs2_super *osb);
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci/**
6362306a36Sopenharmony_ci * ocfs2_resv_discard() - truncate a reservation
6462306a36Sopenharmony_ci * @resmap:
6562306a36Sopenharmony_ci * @resv: the reservation to truncate.
6662306a36Sopenharmony_ci *
6762306a36Sopenharmony_ci * After this function is called, the reservation will be empty, and
6862306a36Sopenharmony_ci * unlinked from the rbtree.
6962306a36Sopenharmony_ci */
7062306a36Sopenharmony_civoid ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
7162306a36Sopenharmony_ci			struct ocfs2_alloc_reservation *resv);
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci/**
7562306a36Sopenharmony_ci * ocfs2_resmap_init() - Initialize fields of a reservations bitmap
7662306a36Sopenharmony_ci * @osb: struct ocfs2_super to be saved in resmap
7762306a36Sopenharmony_ci * @resmap: struct ocfs2_reservation_map to initialize
7862306a36Sopenharmony_ci */
7962306a36Sopenharmony_civoid ocfs2_resmap_init(struct ocfs2_super *osb,
8062306a36Sopenharmony_ci		      struct ocfs2_reservation_map *resmap);
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci/**
8362306a36Sopenharmony_ci * ocfs2_resmap_restart() - "restart" a reservation bitmap
8462306a36Sopenharmony_ci * @resmap: reservations bitmap
8562306a36Sopenharmony_ci * @clen: Number of valid bits in the bitmap
8662306a36Sopenharmony_ci * @disk_bitmap: the disk bitmap this resmap should refer to.
8762306a36Sopenharmony_ci *
8862306a36Sopenharmony_ci * Re-initialize the parameters of a reservation bitmap. This is
8962306a36Sopenharmony_ci * useful for local alloc window slides.
9062306a36Sopenharmony_ci *
9162306a36Sopenharmony_ci * This function will call ocfs2_trunc_resv against all existing
9262306a36Sopenharmony_ci * reservations. A future version will recalculate existing
9362306a36Sopenharmony_ci * reservations based on the new bitmap.
9462306a36Sopenharmony_ci */
9562306a36Sopenharmony_civoid ocfs2_resmap_restart(struct ocfs2_reservation_map *resmap,
9662306a36Sopenharmony_ci			  unsigned int clen, char *disk_bitmap);
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci/**
9962306a36Sopenharmony_ci * ocfs2_resmap_uninit() - uninitialize a reservation bitmap structure
10062306a36Sopenharmony_ci * @resmap: the struct ocfs2_reservation_map to uninitialize
10162306a36Sopenharmony_ci */
10262306a36Sopenharmony_civoid ocfs2_resmap_uninit(struct ocfs2_reservation_map *resmap);
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci/**
10562306a36Sopenharmony_ci * ocfs2_resmap_resv_bits() - Return still-valid reservation bits
10662306a36Sopenharmony_ci * @resmap: reservations bitmap
10762306a36Sopenharmony_ci * @resv: reservation to base search from
10862306a36Sopenharmony_ci * @cstart: start of proposed allocation
10962306a36Sopenharmony_ci * @clen: length (in clusters) of proposed allocation
11062306a36Sopenharmony_ci *
11162306a36Sopenharmony_ci * Using the reservation data from resv, this function will compare
11262306a36Sopenharmony_ci * resmap and resmap->m_disk_bitmap to determine what part (if any) of
11362306a36Sopenharmony_ci * the reservation window is still clear to use. If resv is empty,
11462306a36Sopenharmony_ci * this function will try to allocate a window for it.
11562306a36Sopenharmony_ci *
11662306a36Sopenharmony_ci * On success, zero is returned and the valid allocation area is set in cstart
11762306a36Sopenharmony_ci * and clen.
11862306a36Sopenharmony_ci *
11962306a36Sopenharmony_ci * Returns -ENOSPC if reservations are disabled.
12062306a36Sopenharmony_ci */
12162306a36Sopenharmony_ciint ocfs2_resmap_resv_bits(struct ocfs2_reservation_map *resmap,
12262306a36Sopenharmony_ci			   struct ocfs2_alloc_reservation *resv,
12362306a36Sopenharmony_ci			   int *cstart, int *clen);
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci/**
12662306a36Sopenharmony_ci * ocfs2_resmap_claimed_bits() - Tell the reservation code that bits were used.
12762306a36Sopenharmony_ci * @resmap: reservations bitmap
12862306a36Sopenharmony_ci * @resv: optional reservation to recalulate based on new bitmap
12962306a36Sopenharmony_ci * @cstart: start of allocation in clusters
13062306a36Sopenharmony_ci * @clen: end of allocation in clusters.
13162306a36Sopenharmony_ci *
13262306a36Sopenharmony_ci * Tell the reservation code that bits were used to fulfill allocation in
13362306a36Sopenharmony_ci * resmap. The bits don't have to have been part of any existing
13462306a36Sopenharmony_ci * reservation. But we must always call this function when bits are claimed.
13562306a36Sopenharmony_ci * Internally, the reservations code will use this information to mark the
13662306a36Sopenharmony_ci * reservations bitmap. If resv is passed, it's next allocation window will be
13762306a36Sopenharmony_ci * calculated. It also expects that 'cstart' is the same as we passed back
13862306a36Sopenharmony_ci * from ocfs2_resmap_resv_bits().
13962306a36Sopenharmony_ci */
14062306a36Sopenharmony_civoid ocfs2_resmap_claimed_bits(struct ocfs2_reservation_map *resmap,
14162306a36Sopenharmony_ci			       struct ocfs2_alloc_reservation *resv,
14262306a36Sopenharmony_ci			       u32 cstart, u32 clen);
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci#endif	/* OCFS2_RESERVATIONS_H */
145