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;	/* 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 */
49 
50 	/*
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
54 	 */
55 	xfs_agino_t	pagl_pagino;
56 	xfs_agino_t	pagl_leftrec;
57 	xfs_agino_t	pagl_rightrec;
58 
59 	int		pagb_count;	/* pagb slots in use */
60 	uint8_t		pagf_refcount_level; /* recount btree height */
61 
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;
66 
67 	/* for rcu-safe freeing */
68 	struct rcu_head	rcu_head;
69 
70 #ifdef __KERNEL__
71 	/* -- kernel only structures below this line -- */
72 
73 	/*
74 	 * Bitsets of per-ag metadata that have been checked and/or are sick.
75 	 * Callers should hold pag_state_lock before accessing this field.
76 	 */
77 	uint16_t	pag_checked;
78 	uint16_t	pag_sick;
79 	spinlock_t	pag_state_lock;
80 
81 	spinlock_t	pagb_lock;	/* lock for pagb_tree */
82 	struct rb_root	pagb_tree;	/* ordered tree of busy extents */
83 	unsigned int	pagb_gen;	/* generation count for pagb_tree */
84 	wait_queue_head_t pagb_wait;	/* woken when pagb_gen changes */
85 
86 	atomic_t        pagf_fstrms;    /* # of filestreams active in this AG */
87 
88 	spinlock_t	pag_ici_lock;	/* incore inode cache lock */
89 	struct radix_tree_root pag_ici_root;	/* incore inode cache root */
90 	int		pag_ici_reclaimable;	/* reclaimable inodes */
91 	unsigned long	pag_ici_reclaim_cursor;	/* reclaim restart point */
92 
93 	/* buffer cache index */
94 	spinlock_t	pag_buf_lock;	/* lock for pag_buf_hash */
95 	struct rhashtable pag_buf_hash;
96 
97 	/* background prealloc block trimming */
98 	struct delayed_work	pag_blockgc_work;
99 
100 	/*
101 	 * Unlinked inode information.  This incore information reflects
102 	 * data stored in the AGI, so callers must hold the AGI buffer lock
103 	 * or have some other means to control concurrency.
104 	 */
105 	struct rhashtable	pagi_unlinked_hash;
106 #endif /* __KERNEL__ */
107 };
108 
109 int xfs_initialize_perag(struct xfs_mount *mp, xfs_agnumber_t agcount,
110 			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);
113 
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,
116 		unsigned int tag);
117 void xfs_perag_put(struct xfs_perag *pag);
118 
119 /*
120  * Perag iteration APIs
121  */
122 static inline struct xfs_perag *
xfs_perag_next(struct xfs_perag * pag,xfs_agnumber_t * agno,xfs_agnumber_t end_agno)123 xfs_perag_next(
124 	struct xfs_perag	*pag,
125 	xfs_agnumber_t		*agno,
126 	xfs_agnumber_t		end_agno)
127 {
128 	struct xfs_mount	*mp = pag->pag_mount;
129 
130 	*agno = pag->pag_agno + 1;
131 	xfs_perag_put(pag);
132 	if (*agno > end_agno)
133 		return NULL;
134 	return xfs_perag_get(mp, *agno);
135 }
136 
137 #define for_each_perag_range(mp, agno, end_agno, pag) \
138 	for ((pag) = xfs_perag_get((mp), (agno)); \
139 		(pag) != NULL; \
140 		(pag) = xfs_perag_next((pag), &(agno), (end_agno)))
141 
142 #define for_each_perag_from(mp, agno, pag) \
143 	for_each_perag_range((mp), (agno), (mp)->m_sb.sb_agcount - 1, (pag))
144 
145 
146 #define for_each_perag(mp, agno, pag) \
147 	(agno) = 0; \
148 	for_each_perag_from((mp), (agno), (pag))
149 
150 #define for_each_perag_tag(mp, agno, pag, tag) \
151 	for ((agno) = 0, (pag) = xfs_perag_get_tag((mp), 0, (tag)); \
152 		(pag) != NULL; \
153 		(agno) = (pag)->pag_agno + 1, \
154 		xfs_perag_put(pag), \
155 		(pag) = xfs_perag_get_tag((mp), (agno), (tag)))
156 
157 struct aghdr_init_data {
158 	/* per ag data */
159 	xfs_agblock_t		agno;		/* ag to init */
160 	xfs_extlen_t		agsize;		/* new AG size */
161 	struct list_head	buffer_list;	/* buffer writeback list */
162 	xfs_rfsblock_t		nfree;		/* cumulative new free space */
163 
164 	/* per header data */
165 	xfs_daddr_t		daddr;		/* header location */
166 	size_t			numblks;	/* size of header */
167 	xfs_btnum_t		type;		/* type of btree root block */
168 };
169 
170 int xfs_ag_init_headers(struct xfs_mount *mp, struct aghdr_init_data *id);
171 int xfs_ag_shrink_space(struct xfs_mount *mp, struct xfs_trans **tpp,
172 			xfs_agnumber_t agno, xfs_extlen_t delta);
173 int xfs_ag_extend_space(struct xfs_mount *mp, struct xfs_trans *tp,
174 			struct aghdr_init_data *id, xfs_extlen_t len);
175 int xfs_ag_get_geometry(struct xfs_mount *mp, xfs_agnumber_t agno,
176 			struct xfs_ag_geometry *ageo);
177 
178 #endif /* __LIBXFS_AG_H */
179