]> Git Repo - linux.git/blob - fs/xfs/libxfs/xfs_ag.h
xfs: repair free space btrees
[linux.git] / fs / xfs / libxfs / xfs_ag.h
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_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 */
47
48         /*
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
52          */
53         xfs_agino_t     pagl_pagino;
54         xfs_agino_t     pagl_leftrec;
55         xfs_agino_t     pagl_rightrec;
56
57         int             pagb_count;     /* pagb slots in use */
58         uint8_t         pagf_refcount_level; /* recount btree height */
59
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;
64
65         /* for rcu-safe freeing */
66         struct rcu_head rcu_head;
67
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;
73
74 #ifdef __KERNEL__
75         /* -- kernel only structures below this line -- */
76
77         /*
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.
80          */
81         uint16_t        pag_checked;
82         uint16_t        pag_sick;
83
84 #ifdef CONFIG_XFS_ONLINE_REPAIR
85         /*
86          * Alternate btree heights so that online repair won't trip the write
87          * verifiers while rebuilding the AG btrees.
88          */
89         uint8_t         pagf_repair_levels[XFS_BTNUM_AGF];
90 #endif
91
92         spinlock_t      pag_state_lock;
93
94         spinlock_t      pagb_lock;      /* lock for pagb_tree */
95         struct rb_root  pagb_tree;      /* ordered tree of busy extents */
96         unsigned int    pagb_gen;       /* generation count for pagb_tree */
97         wait_queue_head_t pagb_wait;    /* woken when pagb_gen changes */
98
99         atomic_t        pagf_fstrms;    /* # of filestreams active in this AG */
100
101         spinlock_t      pag_ici_lock;   /* incore inode cache lock */
102         struct radix_tree_root pag_ici_root;    /* incore inode cache root */
103         int             pag_ici_reclaimable;    /* reclaimable inodes */
104         unsigned long   pag_ici_reclaim_cursor; /* reclaim restart point */
105
106         /* buffer cache index */
107         spinlock_t      pag_buf_lock;   /* lock for pag_buf_hash */
108         struct rhashtable pag_buf_hash;
109
110         /* background prealloc block trimming */
111         struct delayed_work     pag_blockgc_work;
112
113         /*
114          * We use xfs_drain to track the number of deferred log intent items
115          * that have been queued (but not yet processed) so that waiters (e.g.
116          * scrub) will not lock resources when other threads are in the middle
117          * of processing a chain of intent items only to find momentary
118          * inconsistencies.
119          */
120         struct xfs_defer_drain  pag_intents_drain;
121 #endif /* __KERNEL__ */
122 };
123
124 /*
125  * Per-AG operational state. These are atomic flag bits.
126  */
127 #define XFS_AGSTATE_AGF_INIT            0
128 #define XFS_AGSTATE_AGI_INIT            1
129 #define XFS_AGSTATE_PREFERS_METADATA    2
130 #define XFS_AGSTATE_ALLOWS_INODES       3
131 #define XFS_AGSTATE_AGFL_NEEDS_RESET    4
132
133 #define __XFS_AG_OPSTATE(name, NAME) \
134 static inline bool xfs_perag_ ## name (struct xfs_perag *pag) \
135 { \
136         return test_bit(XFS_AGSTATE_ ## NAME, &pag->pag_opstate); \
137 }
138
139 __XFS_AG_OPSTATE(initialised_agf, AGF_INIT)
140 __XFS_AG_OPSTATE(initialised_agi, AGI_INIT)
141 __XFS_AG_OPSTATE(prefers_metadata, PREFERS_METADATA)
142 __XFS_AG_OPSTATE(allows_inodes, ALLOWS_INODES)
143 __XFS_AG_OPSTATE(agfl_needs_reset, AGFL_NEEDS_RESET)
144
145 int xfs_initialize_perag(struct xfs_mount *mp, xfs_agnumber_t agcount,
146                         xfs_rfsblock_t dcount, xfs_agnumber_t *maxagi);
147 int xfs_initialize_perag_data(struct xfs_mount *mp, xfs_agnumber_t agno);
148 void xfs_free_perag(struct xfs_mount *mp);
149
150 /* Passive AG references */
151 struct xfs_perag *xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno);
152 struct xfs_perag *xfs_perag_get_tag(struct xfs_mount *mp, xfs_agnumber_t agno,
153                 unsigned int tag);
154 struct xfs_perag *xfs_perag_hold(struct xfs_perag *pag);
155 void xfs_perag_put(struct xfs_perag *pag);
156
157 /* Active AG references */
158 struct xfs_perag *xfs_perag_grab(struct xfs_mount *, xfs_agnumber_t);
159 struct xfs_perag *xfs_perag_grab_tag(struct xfs_mount *, xfs_agnumber_t,
160                                    int tag);
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 #define for_each_perag_tag(mp, agno, pag, tag) \
263         for ((agno) = 0, (pag) = xfs_perag_grab_tag((mp), 0, (tag)); \
264                 (pag) != NULL; \
265                 (agno) = (pag)->pag_agno + 1, \
266                 xfs_perag_rele(pag), \
267                 (pag) = xfs_perag_grab_tag((mp), (agno), (tag)))
268
269 static inline struct xfs_perag *
270 xfs_perag_next_wrap(
271         struct xfs_perag        *pag,
272         xfs_agnumber_t          *agno,
273         xfs_agnumber_t          stop_agno,
274         xfs_agnumber_t          restart_agno,
275         xfs_agnumber_t          wrap_agno)
276 {
277         struct xfs_mount        *mp = pag->pag_mount;
278
279         *agno = pag->pag_agno + 1;
280         xfs_perag_rele(pag);
281         while (*agno != stop_agno) {
282                 if (*agno >= wrap_agno) {
283                         if (restart_agno >= stop_agno)
284                                 break;
285                         *agno = restart_agno;
286                 }
287
288                 pag = xfs_perag_grab(mp, *agno);
289                 if (pag)
290                         return pag;
291                 (*agno)++;
292         }
293         return NULL;
294 }
295
296 /*
297  * Iterate all AGs from start_agno through wrap_agno, then restart_agno through
298  * (start_agno - 1).
299  */
300 #define for_each_perag_wrap_range(mp, start_agno, restart_agno, wrap_agno, agno, pag) \
301         for ((agno) = (start_agno), (pag) = xfs_perag_grab((mp), (agno)); \
302                 (pag) != NULL; \
303                 (pag) = xfs_perag_next_wrap((pag), &(agno), (start_agno), \
304                                 (restart_agno), (wrap_agno)))
305 /*
306  * Iterate all AGs from start_agno through wrap_agno, then 0 through
307  * (start_agno - 1).
308  */
309 #define for_each_perag_wrap_at(mp, start_agno, wrap_agno, agno, pag) \
310         for_each_perag_wrap_range((mp), (start_agno), 0, (wrap_agno), (agno), (pag))
311
312 /*
313  * Iterate all AGs from start_agno through to the end of the filesystem, then 0
314  * through (start_agno - 1).
315  */
316 #define for_each_perag_wrap(mp, start_agno, agno, pag) \
317         for_each_perag_wrap_at((mp), (start_agno), (mp)->m_sb.sb_agcount, \
318                                 (agno), (pag))
319
320
321 struct aghdr_init_data {
322         /* per ag data */
323         xfs_agblock_t           agno;           /* ag to init */
324         xfs_extlen_t            agsize;         /* new AG size */
325         struct list_head        buffer_list;    /* buffer writeback list */
326         xfs_rfsblock_t          nfree;          /* cumulative new free space */
327
328         /* per header data */
329         xfs_daddr_t             daddr;          /* header location */
330         size_t                  numblks;        /* size of header */
331         xfs_btnum_t             type;           /* type of btree root block */
332 };
333
334 int xfs_ag_init_headers(struct xfs_mount *mp, struct aghdr_init_data *id);
335 int xfs_ag_shrink_space(struct xfs_perag *pag, struct xfs_trans **tpp,
336                         xfs_extlen_t delta);
337 int xfs_ag_extend_space(struct xfs_perag *pag, struct xfs_trans *tp,
338                         xfs_extlen_t len);
339 int xfs_ag_get_geometry(struct xfs_perag *pag, struct xfs_ag_geometry *ageo);
340
341 #endif /* __LIBXFS_AG_H */
This page took 0.056947 seconds and 4 git commands to generate.