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; /* perag reference count */
36 char pagf_init; /* this agf's entry is initialized */
37 char pagi_init; /* this agi's entry is initialized */
38 char pagf_metadata; /* the agf is preferred to be metadata */
39 char pagi_inodeok; /* The agi is ok for inodes */
40 uint8_t pagf_levels[XFS_BTNUM_AGF];
41 /* # of levels in bno & cnt btree */
42 bool pagf_agflreset; /* agfl requires reset before use */
43 uint32_t pagf_flcount; /* count of blocks in freelist */
44 xfs_extlen_t pagf_freeblks; /* total free blocks */
45 xfs_extlen_t pagf_longest; /* longest free space */
46 uint32_t pagf_btreeblks; /* # of blocks held in AGF btrees */
47 xfs_agino_t pagi_freecount; /* number of free inodes */
48 xfs_agino_t pagi_count; /* number of allocated inodes */
51 * Inode allocation search lookup optimisation.
52 * If the pagino matches, the search for new inodes
53 * doesn't need to search the near ones again straight away
55 xfs_agino_t pagl_pagino;
56 xfs_agino_t pagl_leftrec;
57 xfs_agino_t pagl_rightrec;
59 int pagb_count; /* pagb slots in use */
60 uint8_t pagf_refcount_level; /* recount btree height */
62 /* Blocks reserved for all kinds of metadata. */
63 struct xfs_ag_resv pag_meta_resv;
64 /* Blocks reserved for the reverse mapping btree. */
65 struct xfs_ag_resv pag_rmapbt_resv;
67 /* for rcu-safe freeing */
68 struct rcu_head rcu_head;
70 /* Precalculated geometry info */
71 xfs_agblock_t block_count;
72 xfs_agblock_t min_block;
73 xfs_agino_t agino_min;
74 xfs_agino_t agino_max;
77 /* -- kernel only structures below this line -- */
80 * Bitsets of per-ag metadata that have been checked and/or are sick.
81 * Callers should hold pag_state_lock before accessing this field.
85 spinlock_t pag_state_lock;
87 spinlock_t pagb_lock; /* lock for pagb_tree */
88 struct rb_root pagb_tree; /* ordered tree of busy extents */
89 unsigned int pagb_gen; /* generation count for pagb_tree */
90 wait_queue_head_t pagb_wait; /* woken when pagb_gen changes */
92 atomic_t pagf_fstrms; /* # of filestreams active in this AG */
94 spinlock_t pag_ici_lock; /* incore inode cache lock */
95 struct radix_tree_root pag_ici_root; /* incore inode cache root */
96 int pag_ici_reclaimable; /* reclaimable inodes */
97 unsigned long pag_ici_reclaim_cursor; /* reclaim restart point */
99 /* buffer cache index */
100 spinlock_t pag_buf_lock; /* lock for pag_buf_hash */
101 struct rhashtable pag_buf_hash;
103 /* background prealloc block trimming */
104 struct delayed_work pag_blockgc_work;
106 #endif /* __KERNEL__ */
109 int xfs_initialize_perag(struct xfs_mount *mp, xfs_agnumber_t agcount,
110 xfs_rfsblock_t dcount, xfs_agnumber_t *maxagi);
111 int xfs_initialize_perag_data(struct xfs_mount *mp, xfs_agnumber_t agno);
112 void xfs_free_perag(struct xfs_mount *mp);
114 struct xfs_perag *xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno);
115 struct xfs_perag *xfs_perag_get_tag(struct xfs_mount *mp, xfs_agnumber_t agno,
117 void xfs_perag_put(struct xfs_perag *pag);
120 * Per-ag geometry infomation and validation
122 xfs_agblock_t xfs_ag_block_count(struct xfs_mount *mp, xfs_agnumber_t agno);
123 void xfs_agino_range(struct xfs_mount *mp, xfs_agnumber_t agno,
124 xfs_agino_t *first, xfs_agino_t *last);
127 xfs_verify_agbno(struct xfs_perag *pag, xfs_agblock_t agbno)
129 if (agbno >= pag->block_count)
131 if (agbno <= pag->min_block)
138 struct xfs_perag *pag,
142 if (agbno + len <= agbno)
145 if (!xfs_verify_agbno(pag, agbno))
148 return xfs_verify_agbno(pag, agbno + len - 1);
152 * Verify that an AG inode number pointer neither points outside the AG
153 * nor points at static metadata.
156 xfs_verify_agino(struct xfs_perag *pag, xfs_agino_t agino)
158 if (agino < pag->agino_min)
160 if (agino > pag->agino_max)
166 * Verify that an AG inode number pointer neither points outside the AG
167 * nor points at static metadata, or is NULLAGINO.
170 xfs_verify_agino_or_null(struct xfs_perag *pag, xfs_agino_t agino)
172 if (agino == NULLAGINO)
174 return xfs_verify_agino(pag, agino);
178 xfs_ag_contains_log(struct xfs_mount *mp, xfs_agnumber_t agno)
180 return mp->m_sb.sb_logstart > 0 &&
181 agno == XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart);
185 * Perag iteration APIs
187 static inline struct xfs_perag *
189 struct xfs_perag *pag,
190 xfs_agnumber_t *agno,
191 xfs_agnumber_t end_agno)
193 struct xfs_mount *mp = pag->pag_mount;
195 *agno = pag->pag_agno + 1;
197 if (*agno > end_agno)
199 return xfs_perag_get(mp, *agno);
202 #define for_each_perag_range(mp, agno, end_agno, pag) \
203 for ((pag) = xfs_perag_get((mp), (agno)); \
205 (pag) = xfs_perag_next((pag), &(agno), (end_agno)))
207 #define for_each_perag_from(mp, agno, pag) \
208 for_each_perag_range((mp), (agno), (mp)->m_sb.sb_agcount - 1, (pag))
211 #define for_each_perag(mp, agno, pag) \
213 for_each_perag_from((mp), (agno), (pag))
215 #define for_each_perag_tag(mp, agno, pag, tag) \
216 for ((agno) = 0, (pag) = xfs_perag_get_tag((mp), 0, (tag)); \
218 (agno) = (pag)->pag_agno + 1, \
219 xfs_perag_put(pag), \
220 (pag) = xfs_perag_get_tag((mp), (agno), (tag)))
222 struct aghdr_init_data {
224 xfs_agblock_t agno; /* ag to init */
225 xfs_extlen_t agsize; /* new AG size */
226 struct list_head buffer_list; /* buffer writeback list */
227 xfs_rfsblock_t nfree; /* cumulative new free space */
229 /* per header data */
230 xfs_daddr_t daddr; /* header location */
231 size_t numblks; /* size of header */
232 xfs_btnum_t type; /* type of btree root block */
235 int xfs_ag_init_headers(struct xfs_mount *mp, struct aghdr_init_data *id);
236 int xfs_ag_shrink_space(struct xfs_perag *pag, struct xfs_trans **tpp,
238 int xfs_ag_extend_space(struct xfs_perag *pag, struct xfs_trans *tp,
240 int xfs_ag_get_geometry(struct xfs_perag *pag, struct xfs_ag_geometry *ageo);
242 #endif /* __LIBXFS_AG_H */