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