1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2018 Red Hat, Inc.
8 #define __LIBXFS_AG_H 1
15 * Per-ag infrastructure
18 /* per-AG block reservation data structures*/
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;
29 * Per-ag incore structure, copies of information in agf and agi, to improve the
30 * performance of allocation group selection.
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_levels[XFS_BTNUM_AGF];
40 /* # of levels in bno & cnt btree */
41 uint32_t pagf_flcount; /* count of blocks in freelist */
42 xfs_extlen_t pagf_freeblks; /* total free blocks */
43 xfs_extlen_t pagf_longest; /* longest free space */
44 uint32_t pagf_btreeblks; /* # of blocks held in AGF btrees */
45 xfs_agino_t pagi_freecount; /* number of free inodes */
46 xfs_agino_t pagi_count; /* number of allocated inodes */
49 * Inode allocation search lookup optimisation.
50 * If the pagino matches, the search for new inodes
51 * doesn't need to search the near ones again straight away
53 xfs_agino_t pagl_pagino;
54 xfs_agino_t pagl_leftrec;
55 xfs_agino_t pagl_rightrec;
57 int pagb_count; /* pagb slots in use */
58 uint8_t pagf_refcount_level; /* recount btree height */
60 /* Blocks reserved for all kinds of metadata. */
61 struct xfs_ag_resv pag_meta_resv;
62 /* Blocks reserved for the reverse mapping btree. */
63 struct xfs_ag_resv pag_rmapbt_resv;
65 /* for rcu-safe freeing */
66 struct rcu_head rcu_head;
68 /* Precalculated geometry info */
69 xfs_agblock_t block_count;
70 xfs_agblock_t min_block;
71 xfs_agino_t agino_min;
72 xfs_agino_t agino_max;
75 /* -- kernel only structures below this line -- */
78 * Bitsets of per-ag metadata that have been checked and/or are sick.
79 * Callers should hold pag_state_lock before accessing this field.
84 #ifdef CONFIG_XFS_ONLINE_REPAIR
86 * Alternate btree heights so that online repair won't trip the write
87 * verifiers while rebuilding the AG btrees.
89 uint8_t pagf_repair_levels[XFS_BTNUM_AGF];
90 uint8_t pagf_repair_refcount_level;
93 spinlock_t pag_state_lock;
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 */
100 atomic_t pagf_fstrms; /* # of filestreams active in this AG */
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 */
107 /* buffer cache index */
108 spinlock_t pag_buf_lock; /* lock for pag_buf_hash */
109 struct rhashtable pag_buf_hash;
111 /* background prealloc block trimming */
112 struct delayed_work pag_blockgc_work;
115 * We use xfs_drain to track the number of deferred log intent items
116 * that have been queued (but not yet processed) so that waiters (e.g.
117 * scrub) will not lock resources when other threads are in the middle
118 * of processing a chain of intent items only to find momentary
121 struct xfs_defer_drain pag_intents_drain;
122 #endif /* __KERNEL__ */
126 * Per-AG operational state. These are atomic flag bits.
128 #define XFS_AGSTATE_AGF_INIT 0
129 #define XFS_AGSTATE_AGI_INIT 1
130 #define XFS_AGSTATE_PREFERS_METADATA 2
131 #define XFS_AGSTATE_ALLOWS_INODES 3
132 #define XFS_AGSTATE_AGFL_NEEDS_RESET 4
134 #define __XFS_AG_OPSTATE(name, NAME) \
135 static inline bool xfs_perag_ ## name (struct xfs_perag *pag) \
137 return test_bit(XFS_AGSTATE_ ## NAME, &pag->pag_opstate); \
140 __XFS_AG_OPSTATE(initialised_agf, AGF_INIT)
141 __XFS_AG_OPSTATE(initialised_agi, AGI_INIT)
142 __XFS_AG_OPSTATE(prefers_metadata, PREFERS_METADATA)
143 __XFS_AG_OPSTATE(allows_inodes, ALLOWS_INODES)
144 __XFS_AG_OPSTATE(agfl_needs_reset, AGFL_NEEDS_RESET)
146 void xfs_free_unused_perag_range(struct xfs_mount *mp, xfs_agnumber_t agstart,
147 xfs_agnumber_t agend);
148 int xfs_initialize_perag(struct xfs_mount *mp, xfs_agnumber_t agcount,
149 xfs_rfsblock_t dcount, xfs_agnumber_t *maxagi);
150 int xfs_initialize_perag_data(struct xfs_mount *mp, xfs_agnumber_t agno);
151 void xfs_free_perag(struct xfs_mount *mp);
153 /* Passive AG references */
154 struct xfs_perag *xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno);
155 struct xfs_perag *xfs_perag_get_tag(struct xfs_mount *mp, xfs_agnumber_t agno,
157 struct xfs_perag *xfs_perag_hold(struct xfs_perag *pag);
158 void xfs_perag_put(struct xfs_perag *pag);
160 /* Active AG references */
161 struct xfs_perag *xfs_perag_grab(struct xfs_mount *, xfs_agnumber_t);
162 struct xfs_perag *xfs_perag_grab_tag(struct xfs_mount *, xfs_agnumber_t,
164 void xfs_perag_rele(struct xfs_perag *pag);
167 * Per-ag geometry infomation and validation
169 xfs_agblock_t xfs_ag_block_count(struct xfs_mount *mp, xfs_agnumber_t agno);
170 void xfs_agino_range(struct xfs_mount *mp, xfs_agnumber_t agno,
171 xfs_agino_t *first, xfs_agino_t *last);
174 xfs_verify_agbno(struct xfs_perag *pag, xfs_agblock_t agbno)
176 if (agbno >= pag->block_count)
178 if (agbno <= pag->min_block)
185 struct xfs_perag *pag,
189 if (agbno + len <= agbno)
192 if (!xfs_verify_agbno(pag, agbno))
195 return xfs_verify_agbno(pag, agbno + len - 1);
199 * Verify that an AG inode number pointer neither points outside the AG
200 * nor points at static metadata.
203 xfs_verify_agino(struct xfs_perag *pag, xfs_agino_t agino)
205 if (agino < pag->agino_min)
207 if (agino > pag->agino_max)
213 * Verify that an AG inode number pointer neither points outside the AG
214 * nor points at static metadata, or is NULLAGINO.
217 xfs_verify_agino_or_null(struct xfs_perag *pag, xfs_agino_t agino)
219 if (agino == NULLAGINO)
221 return xfs_verify_agino(pag, agino);
225 xfs_ag_contains_log(struct xfs_mount *mp, xfs_agnumber_t agno)
227 return mp->m_sb.sb_logstart > 0 &&
228 agno == XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart);
232 * Perag iteration APIs
234 static inline struct xfs_perag *
236 struct xfs_perag *pag,
237 xfs_agnumber_t *agno,
238 xfs_agnumber_t end_agno)
240 struct xfs_mount *mp = pag->pag_mount;
242 *agno = pag->pag_agno + 1;
244 while (*agno <= end_agno) {
245 pag = xfs_perag_grab(mp, *agno);
253 #define for_each_perag_range(mp, agno, end_agno, pag) \
254 for ((pag) = xfs_perag_grab((mp), (agno)); \
256 (pag) = xfs_perag_next((pag), &(agno), (end_agno)))
258 #define for_each_perag_from(mp, agno, pag) \
259 for_each_perag_range((mp), (agno), (mp)->m_sb.sb_agcount - 1, (pag))
261 #define for_each_perag(mp, agno, pag) \
263 for_each_perag_from((mp), (agno), (pag))
265 #define for_each_perag_tag(mp, agno, pag, tag) \
266 for ((agno) = 0, (pag) = xfs_perag_grab_tag((mp), 0, (tag)); \
268 (agno) = (pag)->pag_agno + 1, \
269 xfs_perag_rele(pag), \
270 (pag) = xfs_perag_grab_tag((mp), (agno), (tag)))
272 static inline struct xfs_perag *
274 struct xfs_perag *pag,
275 xfs_agnumber_t *agno,
276 xfs_agnumber_t stop_agno,
277 xfs_agnumber_t restart_agno,
278 xfs_agnumber_t wrap_agno)
280 struct xfs_mount *mp = pag->pag_mount;
282 *agno = pag->pag_agno + 1;
284 while (*agno != stop_agno) {
285 if (*agno >= wrap_agno) {
286 if (restart_agno >= stop_agno)
288 *agno = restart_agno;
291 pag = xfs_perag_grab(mp, *agno);
300 * Iterate all AGs from start_agno through wrap_agno, then restart_agno through
303 #define for_each_perag_wrap_range(mp, start_agno, restart_agno, wrap_agno, agno, pag) \
304 for ((agno) = (start_agno), (pag) = xfs_perag_grab((mp), (agno)); \
306 (pag) = xfs_perag_next_wrap((pag), &(agno), (start_agno), \
307 (restart_agno), (wrap_agno)))
309 * Iterate all AGs from start_agno through wrap_agno, then 0 through
312 #define for_each_perag_wrap_at(mp, start_agno, wrap_agno, agno, pag) \
313 for_each_perag_wrap_range((mp), (start_agno), 0, (wrap_agno), (agno), (pag))
316 * Iterate all AGs from start_agno through to the end of the filesystem, then 0
317 * through (start_agno - 1).
319 #define for_each_perag_wrap(mp, start_agno, agno, pag) \
320 for_each_perag_wrap_at((mp), (start_agno), (mp)->m_sb.sb_agcount, \
324 struct aghdr_init_data {
326 xfs_agblock_t agno; /* ag to init */
327 xfs_extlen_t agsize; /* new AG size */
328 struct list_head buffer_list; /* buffer writeback list */
329 xfs_rfsblock_t nfree; /* cumulative new free space */
331 /* per header data */
332 xfs_daddr_t daddr; /* header location */
333 size_t numblks; /* size of header */
334 xfs_btnum_t type; /* type of btree root block */
337 int xfs_ag_init_headers(struct xfs_mount *mp, struct aghdr_init_data *id);
338 int xfs_ag_shrink_space(struct xfs_perag *pag, struct xfs_trans **tpp,
340 int xfs_ag_extend_space(struct xfs_perag *pag, struct xfs_trans *tp,
342 int xfs_ag_get_geometry(struct xfs_perag *pag, struct xfs_ag_geometry *ageo);
344 #endif /* __LIBXFS_AG_H */