xref: /linux/fs/xfs/libxfs/xfs_ag.h (revision 06a130e42a5bfc84795464bff023bff4c16f58c5)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2018 Red Hat, Inc.
4  * All rights reserved.
5  */
6 
7 #ifndef __LIBXFS_AG_H
8 #define __LIBXFS_AG_H 1
9 
10 struct xfs_mount;
11 struct xfs_trans;
12 struct xfs_perag;
13 
14 /*
15  * Per-ag infrastructure
16  */
17 
18 /* per-AG block reservation data structures*/
19 struct xfs_ag_resv {
20 	/* number of blocks originally reserved here */
21 	xfs_extlen_t			ar_orig_reserved;
22 	/* number of blocks reserved here */
23 	xfs_extlen_t			ar_reserved;
24 	/* number of blocks originally asked for */
25 	xfs_extlen_t			ar_asked;
26 };
27 
28 /*
29  * Per-ag incore structure, copies of information in agf and agi, to improve the
30  * performance of allocation group selection.
31  */
32 struct xfs_perag {
33 	struct xfs_mount *pag_mount;	/* owner filesystem */
34 	xfs_agnumber_t	pag_agno;	/* AG this structure belongs to */
35 	atomic_t	pag_ref;	/* passive reference count */
36 	atomic_t	pag_active_ref;	/* active reference count */
37 	wait_queue_head_t pag_active_wq;/* woken active_ref falls to zero */
38 	unsigned long	pag_opstate;
39 	uint8_t		pagf_bno_level;	/* # of levels in bno btree */
40 	uint8_t		pagf_cnt_level;	/* # of levels in cnt btree */
41 	uint8_t		pagf_rmap_level;/* # of levels in rmap btree */
42 	uint32_t	pagf_flcount;	/* count of blocks in freelist */
43 	xfs_extlen_t	pagf_freeblks;	/* total free blocks */
44 	xfs_extlen_t	pagf_longest;	/* longest free space */
45 	uint32_t	pagf_btreeblks;	/* # of blocks held in AGF btrees */
46 	xfs_agino_t	pagi_freecount;	/* number of free inodes */
47 	xfs_agino_t	pagi_count;	/* number of allocated inodes */
48 
49 	/*
50 	 * Inode allocation search lookup optimisation.
51 	 * If the pagino matches, the search for new inodes
52 	 * doesn't need to search the near ones again straight away
53 	 */
54 	xfs_agino_t	pagl_pagino;
55 	xfs_agino_t	pagl_leftrec;
56 	xfs_agino_t	pagl_rightrec;
57 
58 	int		pagb_count;	/* pagb slots in use */
59 	uint8_t		pagf_refcount_level; /* recount btree height */
60 
61 	/* Blocks reserved for all kinds of metadata. */
62 	struct xfs_ag_resv	pag_meta_resv;
63 	/* Blocks reserved for the reverse mapping btree. */
64 	struct xfs_ag_resv	pag_rmapbt_resv;
65 
66 	/* Precalculated geometry info */
67 	xfs_agblock_t		block_count;
68 	xfs_agblock_t		min_block;
69 	xfs_agino_t		agino_min;
70 	xfs_agino_t		agino_max;
71 
72 #ifdef __KERNEL__
73 	/* -- kernel only structures below this line -- */
74 
75 	/*
76 	 * Bitsets of per-ag metadata that have been checked and/or are sick.
77 	 * Callers should hold pag_state_lock before accessing this field.
78 	 */
79 	uint16_t	pag_checked;
80 	uint16_t	pag_sick;
81 
82 #ifdef CONFIG_XFS_ONLINE_REPAIR
83 	/*
84 	 * Alternate btree heights so that online repair won't trip the write
85 	 * verifiers while rebuilding the AG btrees.
86 	 */
87 	uint8_t		pagf_repair_bno_level;
88 	uint8_t		pagf_repair_cnt_level;
89 	uint8_t		pagf_repair_refcount_level;
90 	uint8_t		pagf_repair_rmap_level;
91 #endif
92 
93 	spinlock_t	pag_state_lock;
94 
95 	spinlock_t	pagb_lock;	/* lock for pagb_tree */
96 	struct rb_root	pagb_tree;	/* ordered tree of busy extents */
97 	unsigned int	pagb_gen;	/* generation count for pagb_tree */
98 	wait_queue_head_t pagb_wait;	/* woken when pagb_gen changes */
99 
100 	atomic_t        pagf_fstrms;    /* # of filestreams active in this AG */
101 
102 	spinlock_t	pag_ici_lock;	/* incore inode cache lock */
103 	struct radix_tree_root pag_ici_root;	/* incore inode cache root */
104 	int		pag_ici_reclaimable;	/* reclaimable inodes */
105 	unsigned long	pag_ici_reclaim_cursor;	/* reclaim restart point */
106 
107 	struct xfs_buf_cache	pag_bcache;
108 
109 	/* background prealloc block trimming */
110 	struct delayed_work	pag_blockgc_work;
111 
112 	/*
113 	 * We use xfs_drain to track the number of deferred log intent items
114 	 * that have been queued (but not yet processed) so that waiters (e.g.
115 	 * scrub) will not lock resources when other threads are in the middle
116 	 * of processing a chain of intent items only to find momentary
117 	 * inconsistencies.
118 	 */
119 	struct xfs_defer_drain	pag_intents_drain;
120 
121 	/* Hook to feed rmapbt updates to an active online repair. */
122 	struct xfs_hooks	pag_rmap_update_hooks;
123 #endif /* __KERNEL__ */
124 };
125 
126 /*
127  * Per-AG operational state. These are atomic flag bits.
128  */
129 #define XFS_AGSTATE_AGF_INIT		0
130 #define XFS_AGSTATE_AGI_INIT		1
131 #define XFS_AGSTATE_PREFERS_METADATA	2
132 #define XFS_AGSTATE_ALLOWS_INODES	3
133 #define XFS_AGSTATE_AGFL_NEEDS_RESET	4
134 
135 #define __XFS_AG_OPSTATE(name, NAME) \
136 static inline bool xfs_perag_ ## name (struct xfs_perag *pag) \
137 { \
138 	return test_bit(XFS_AGSTATE_ ## NAME, &pag->pag_opstate); \
139 }
140 
141 __XFS_AG_OPSTATE(initialised_agf, AGF_INIT)
142 __XFS_AG_OPSTATE(initialised_agi, AGI_INIT)
143 __XFS_AG_OPSTATE(prefers_metadata, PREFERS_METADATA)
144 __XFS_AG_OPSTATE(allows_inodes, ALLOWS_INODES)
145 __XFS_AG_OPSTATE(agfl_needs_reset, AGFL_NEEDS_RESET)
146 
147 void xfs_free_unused_perag_range(struct xfs_mount *mp, xfs_agnumber_t agstart,
148 			xfs_agnumber_t agend);
149 int xfs_initialize_perag(struct xfs_mount *mp, xfs_agnumber_t agcount,
150 			xfs_rfsblock_t dcount, xfs_agnumber_t *maxagi);
151 int xfs_initialize_perag_data(struct xfs_mount *mp, xfs_agnumber_t agno);
152 void xfs_free_perag(struct xfs_mount *mp);
153 
154 /* Passive AG references */
155 struct xfs_perag *xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno);
156 struct xfs_perag *xfs_perag_hold(struct xfs_perag *pag);
157 void xfs_perag_put(struct xfs_perag *pag);
158 
159 /* Active AG references */
160 struct xfs_perag *xfs_perag_grab(struct xfs_mount *, xfs_agnumber_t);
161 void xfs_perag_rele(struct xfs_perag *pag);
162 
163 /*
164  * Per-ag geometry infomation and validation
165  */
166 xfs_agblock_t xfs_ag_block_count(struct xfs_mount *mp, xfs_agnumber_t agno);
167 void xfs_agino_range(struct xfs_mount *mp, xfs_agnumber_t agno,
168 		xfs_agino_t *first, xfs_agino_t *last);
169 
170 static inline bool
171 xfs_verify_agbno(struct xfs_perag *pag, xfs_agblock_t agbno)
172 {
173 	if (agbno >= pag->block_count)
174 		return false;
175 	if (agbno <= pag->min_block)
176 		return false;
177 	return true;
178 }
179 
180 static inline bool
181 xfs_verify_agbext(
182 	struct xfs_perag	*pag,
183 	xfs_agblock_t		agbno,
184 	xfs_agblock_t		len)
185 {
186 	if (agbno + len <= agbno)
187 		return false;
188 
189 	if (!xfs_verify_agbno(pag, agbno))
190 		return false;
191 
192 	return xfs_verify_agbno(pag, agbno + len - 1);
193 }
194 
195 /*
196  * Verify that an AG inode number pointer neither points outside the AG
197  * nor points at static metadata.
198  */
199 static inline bool
200 xfs_verify_agino(struct xfs_perag *pag, xfs_agino_t agino)
201 {
202 	if (agino < pag->agino_min)
203 		return false;
204 	if (agino > pag->agino_max)
205 		return false;
206 	return true;
207 }
208 
209 /*
210  * Verify that an AG inode number pointer neither points outside the AG
211  * nor points at static metadata, or is NULLAGINO.
212  */
213 static inline bool
214 xfs_verify_agino_or_null(struct xfs_perag *pag, xfs_agino_t agino)
215 {
216 	if (agino == NULLAGINO)
217 		return true;
218 	return xfs_verify_agino(pag, agino);
219 }
220 
221 static inline bool
222 xfs_ag_contains_log(struct xfs_mount *mp, xfs_agnumber_t agno)
223 {
224 	return mp->m_sb.sb_logstart > 0 &&
225 	       agno == XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart);
226 }
227 
228 /*
229  * Perag iteration APIs
230  */
231 static inline struct xfs_perag *
232 xfs_perag_next(
233 	struct xfs_perag	*pag,
234 	xfs_agnumber_t		*agno,
235 	xfs_agnumber_t		end_agno)
236 {
237 	struct xfs_mount	*mp = pag->pag_mount;
238 
239 	*agno = pag->pag_agno + 1;
240 	xfs_perag_rele(pag);
241 	while (*agno <= end_agno) {
242 		pag = xfs_perag_grab(mp, *agno);
243 		if (pag)
244 			return pag;
245 		(*agno)++;
246 	}
247 	return NULL;
248 }
249 
250 #define for_each_perag_range(mp, agno, end_agno, pag) \
251 	for ((pag) = xfs_perag_grab((mp), (agno)); \
252 		(pag) != NULL; \
253 		(pag) = xfs_perag_next((pag), &(agno), (end_agno)))
254 
255 #define for_each_perag_from(mp, agno, pag) \
256 	for_each_perag_range((mp), (agno), (mp)->m_sb.sb_agcount - 1, (pag))
257 
258 #define for_each_perag(mp, agno, pag) \
259 	(agno) = 0; \
260 	for_each_perag_from((mp), (agno), (pag))
261 
262 static inline struct xfs_perag *
263 xfs_perag_next_wrap(
264 	struct xfs_perag	*pag,
265 	xfs_agnumber_t		*agno,
266 	xfs_agnumber_t		stop_agno,
267 	xfs_agnumber_t		restart_agno,
268 	xfs_agnumber_t		wrap_agno)
269 {
270 	struct xfs_mount	*mp = pag->pag_mount;
271 
272 	*agno = pag->pag_agno + 1;
273 	xfs_perag_rele(pag);
274 	while (*agno != stop_agno) {
275 		if (*agno >= wrap_agno) {
276 			if (restart_agno >= stop_agno)
277 				break;
278 			*agno = restart_agno;
279 		}
280 
281 		pag = xfs_perag_grab(mp, *agno);
282 		if (pag)
283 			return pag;
284 		(*agno)++;
285 	}
286 	return NULL;
287 }
288 
289 /*
290  * Iterate all AGs from start_agno through wrap_agno, then restart_agno through
291  * (start_agno - 1).
292  */
293 #define for_each_perag_wrap_range(mp, start_agno, restart_agno, wrap_agno, agno, pag) \
294 	for ((agno) = (start_agno), (pag) = xfs_perag_grab((mp), (agno)); \
295 		(pag) != NULL; \
296 		(pag) = xfs_perag_next_wrap((pag), &(agno), (start_agno), \
297 				(restart_agno), (wrap_agno)))
298 /*
299  * Iterate all AGs from start_agno through wrap_agno, then 0 through
300  * (start_agno - 1).
301  */
302 #define for_each_perag_wrap_at(mp, start_agno, wrap_agno, agno, pag) \
303 	for_each_perag_wrap_range((mp), (start_agno), 0, (wrap_agno), (agno), (pag))
304 
305 /*
306  * Iterate all AGs from start_agno through to the end of the filesystem, then 0
307  * through (start_agno - 1).
308  */
309 #define for_each_perag_wrap(mp, start_agno, agno, pag) \
310 	for_each_perag_wrap_at((mp), (start_agno), (mp)->m_sb.sb_agcount, \
311 				(agno), (pag))
312 
313 
314 struct aghdr_init_data {
315 	/* per ag data */
316 	xfs_agblock_t		agno;		/* ag to init */
317 	xfs_extlen_t		agsize;		/* new AG size */
318 	struct list_head	buffer_list;	/* buffer writeback list */
319 	xfs_rfsblock_t		nfree;		/* cumulative new free space */
320 
321 	/* per header data */
322 	xfs_daddr_t		daddr;		/* header location */
323 	size_t			numblks;	/* size of header */
324 	const struct xfs_btree_ops *bc_ops;	/* btree ops */
325 };
326 
327 int xfs_ag_init_headers(struct xfs_mount *mp, struct aghdr_init_data *id);
328 int xfs_ag_shrink_space(struct xfs_perag *pag, struct xfs_trans **tpp,
329 			xfs_extlen_t delta);
330 int xfs_ag_extend_space(struct xfs_perag *pag, struct xfs_trans *tp,
331 			xfs_extlen_t len);
332 int xfs_ag_get_geometry(struct xfs_perag *pag, struct xfs_ag_geometry *ageo);
333 
334 #endif /* __LIBXFS_AG_H */
335