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 char pagf_init
; /* this agf's entry is initialized */
39 char pagi_init
; /* this agi's entry is initialized */
40 char pagf_metadata
; /* the agf is preferred to be metadata */
41 char pagi_inodeok
; /* The agi is ok for inodes */
42 uint8_t pagf_levels
[XFS_BTNUM_AGF
];
43 /* # of levels in bno & cnt btree */
44 bool pagf_agflreset
; /* agfl requires reset before use */
45 uint32_t pagf_flcount
; /* count of blocks in freelist */
46 xfs_extlen_t pagf_freeblks
; /* total free blocks */
47 xfs_extlen_t pagf_longest
; /* longest free space */
48 uint32_t pagf_btreeblks
; /* # of blocks held in AGF btrees */
49 xfs_agino_t pagi_freecount
; /* number of free inodes */
50 xfs_agino_t pagi_count
; /* number of allocated inodes */
53 * Inode allocation search lookup optimisation.
54 * If the pagino matches, the search for new inodes
55 * doesn't need to search the near ones again straight away
57 xfs_agino_t pagl_pagino
;
58 xfs_agino_t pagl_leftrec
;
59 xfs_agino_t pagl_rightrec
;
61 int pagb_count
; /* pagb slots in use */
62 uint8_t pagf_refcount_level
; /* recount btree height */
64 /* Blocks reserved for all kinds of metadata. */
65 struct xfs_ag_resv pag_meta_resv
;
66 /* Blocks reserved for the reverse mapping btree. */
67 struct xfs_ag_resv pag_rmapbt_resv
;
69 /* for rcu-safe freeing */
70 struct rcu_head rcu_head
;
72 /* Precalculated geometry info */
73 xfs_agblock_t block_count
;
74 xfs_agblock_t min_block
;
75 xfs_agino_t agino_min
;
76 xfs_agino_t agino_max
;
79 /* -- kernel only structures below this line -- */
82 * Bitsets of per-ag metadata that have been checked and/or are sick.
83 * Callers should hold pag_state_lock before accessing this field.
87 spinlock_t pag_state_lock
;
89 spinlock_t pagb_lock
; /* lock for pagb_tree */
90 struct rb_root pagb_tree
; /* ordered tree of busy extents */
91 unsigned int pagb_gen
; /* generation count for pagb_tree */
92 wait_queue_head_t pagb_wait
; /* woken when pagb_gen changes */
94 atomic_t pagf_fstrms
; /* # of filestreams active in this AG */
96 spinlock_t pag_ici_lock
; /* incore inode cache lock */
97 struct radix_tree_root pag_ici_root
; /* incore inode cache root */
98 int pag_ici_reclaimable
; /* reclaimable inodes */
99 unsigned long pag_ici_reclaim_cursor
; /* reclaim restart point */
101 /* buffer cache index */
102 spinlock_t pag_buf_lock
; /* lock for pag_buf_hash */
103 struct rhashtable pag_buf_hash
;
105 /* background prealloc block trimming */
106 struct delayed_work pag_blockgc_work
;
108 #endif /* __KERNEL__ */
111 int xfs_initialize_perag(struct xfs_mount
*mp
, xfs_agnumber_t agcount
,
112 xfs_rfsblock_t dcount
, xfs_agnumber_t
*maxagi
);
113 int xfs_initialize_perag_data(struct xfs_mount
*mp
, xfs_agnumber_t agno
);
114 void xfs_free_perag(struct xfs_mount
*mp
);
116 /* Passive AG references */
117 struct xfs_perag
*xfs_perag_get(struct xfs_mount
*mp
, xfs_agnumber_t agno
);
118 struct xfs_perag
*xfs_perag_get_tag(struct xfs_mount
*mp
, xfs_agnumber_t agno
,
120 void xfs_perag_put(struct xfs_perag
*pag
);
122 /* Active AG references */
123 struct xfs_perag
*xfs_perag_grab(struct xfs_mount
*, xfs_agnumber_t
);
124 struct xfs_perag
*xfs_perag_grab_tag(struct xfs_mount
*, xfs_agnumber_t
,
126 void xfs_perag_rele(struct xfs_perag
*pag
);
129 * Per-ag geometry infomation and validation
131 xfs_agblock_t
xfs_ag_block_count(struct xfs_mount
*mp
, xfs_agnumber_t agno
);
132 void xfs_agino_range(struct xfs_mount
*mp
, xfs_agnumber_t agno
,
133 xfs_agino_t
*first
, xfs_agino_t
*last
);
136 xfs_verify_agbno(struct xfs_perag
*pag
, xfs_agblock_t agbno
)
138 if (agbno
>= pag
->block_count
)
140 if (agbno
<= pag
->min_block
)
147 struct xfs_perag
*pag
,
151 if (agbno
+ len
<= agbno
)
154 if (!xfs_verify_agbno(pag
, agbno
))
157 return xfs_verify_agbno(pag
, agbno
+ len
- 1);
161 * Verify that an AG inode number pointer neither points outside the AG
162 * nor points at static metadata.
165 xfs_verify_agino(struct xfs_perag
*pag
, xfs_agino_t agino
)
167 if (agino
< pag
->agino_min
)
169 if (agino
> pag
->agino_max
)
175 * Verify that an AG inode number pointer neither points outside the AG
176 * nor points at static metadata, or is NULLAGINO.
179 xfs_verify_agino_or_null(struct xfs_perag
*pag
, xfs_agino_t agino
)
181 if (agino
== NULLAGINO
)
183 return xfs_verify_agino(pag
, agino
);
187 xfs_ag_contains_log(struct xfs_mount
*mp
, xfs_agnumber_t agno
)
189 return mp
->m_sb
.sb_logstart
> 0 &&
190 agno
== XFS_FSB_TO_AGNO(mp
, mp
->m_sb
.sb_logstart
);
194 * Perag iteration APIs
196 static inline struct xfs_perag
*
198 struct xfs_perag
*pag
,
199 xfs_agnumber_t
*agno
,
200 xfs_agnumber_t end_agno
)
202 struct xfs_mount
*mp
= pag
->pag_mount
;
204 *agno
= pag
->pag_agno
+ 1;
206 while (*agno
<= end_agno
) {
207 pag
= xfs_perag_grab(mp
, *agno
);
215 #define for_each_perag_range(mp, agno, end_agno, pag) \
216 for ((pag) = xfs_perag_grab((mp), (agno)); \
218 (pag) = xfs_perag_next((pag), &(agno), (end_agno)))
220 #define for_each_perag_from(mp, agno, pag) \
221 for_each_perag_range((mp), (agno), (mp)->m_sb.sb_agcount - 1, (pag))
224 #define for_each_perag(mp, agno, pag) \
226 for_each_perag_from((mp), (agno), (pag))
228 #define for_each_perag_tag(mp, agno, pag, tag) \
229 for ((agno) = 0, (pag) = xfs_perag_grab_tag((mp), 0, (tag)); \
231 (agno) = (pag)->pag_agno + 1, \
232 xfs_perag_rele(pag), \
233 (pag) = xfs_perag_grab_tag((mp), (agno), (tag)))
235 struct aghdr_init_data
{
237 xfs_agblock_t agno
; /* ag to init */
238 xfs_extlen_t agsize
; /* new AG size */
239 struct list_head buffer_list
; /* buffer writeback list */
240 xfs_rfsblock_t nfree
; /* cumulative new free space */
242 /* per header data */
243 xfs_daddr_t daddr
; /* header location */
244 size_t numblks
; /* size of header */
245 xfs_btnum_t type
; /* type of btree root block */
248 int xfs_ag_init_headers(struct xfs_mount
*mp
, struct aghdr_init_data
*id
);
249 int xfs_ag_shrink_space(struct xfs_perag
*pag
, struct xfs_trans
**tpp
,
251 int xfs_ag_extend_space(struct xfs_perag
*pag
, struct xfs_trans
*tp
,
253 int xfs_ag_get_geometry(struct xfs_perag
*pag
, struct xfs_ag_geometry
*ageo
);
255 #endif /* __LIBXFS_AG_H */