xref: /linux/fs/xfs/libxfs/xfs_log_rlimit.c (revision c5288cda69ee2d8607f5026bd599a5cebf0ee783)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2013 Jie Liu.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_da_format.h"
14 #include "xfs_trans_space.h"
15 #include "xfs_da_btree.h"
16 #include "xfs_bmap_btree.h"
17 #include "xfs_trace.h"
18 
19 /*
20  * Shortly after enabling the large extents count feature in 2023, longstanding
21  * bugs were found in the code that computes the minimum log size.  Luckily,
22  * the bugs resulted in over-estimates of that size, so there's no impact to
23  * existing users.  However, we don't want to reduce the minimum log size
24  * because that can create the situation where a newer mkfs writes a new
25  * filesystem that an older kernel won't mount.
26  *
27  * Several years prior, we also discovered that the transaction reservations
28  * for rmap and reflink operations were unnecessarily large.  That was fixed,
29  * but the minimum log size computation was left alone to avoid the
30  * compatibility problems noted above.  Fix that too.
31  *
32  * Therefore, we only may correct the computation starting with filesystem
33  * features that didn't exist in 2023.  In other words, only turn this on if
34  * the filesystem has parent pointers.
35  *
36  * This function can be called before the XFS_HAS_* flags have been set up,
37  * (e.g. mkfs) so we must check the ondisk superblock.
38  */
39 static inline bool
40 xfs_want_minlogsize_fixes(
41 	struct xfs_sb	*sb)
42 {
43 	return xfs_sb_is_v5(sb) &&
44 	       xfs_sb_has_incompat_feature(sb, XFS_SB_FEAT_INCOMPAT_PARENT);
45 }
46 
47 /*
48  * Calculate the maximum length in bytes that would be required for a local
49  * attribute value as large attributes out of line are not logged.
50  */
51 STATIC int
52 xfs_log_calc_max_attrsetm_res(
53 	struct xfs_mount	*mp)
54 {
55 	int			size;
56 	int			nblks;
57 
58 	size = xfs_attr_leaf_entsize_local_max(mp->m_attr_geo->blksize) -
59 	       MAXNAMELEN - 1;
60 	nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
61 	nblks += XFS_B_TO_FSB(mp, size);
62 
63 	/*
64 	 * If the feature set is new enough, correct a unit conversion error in
65 	 * the xattr transaction reservation code that resulted in oversized
66 	 * minimum log size computations.
67 	 */
68 	if (xfs_want_minlogsize_fixes(&mp->m_sb))
69 		size = XFS_B_TO_FSB(mp, size);
70 
71 	nblks += XFS_NEXTENTADD_SPACE_RES(mp, size, XFS_ATTR_FORK);
72 
73 	return  M_RES(mp)->tr_attrsetm.tr_logres +
74 		M_RES(mp)->tr_attrsetrt.tr_logres * nblks;
75 }
76 
77 /*
78  * Compute an alternate set of log reservation sizes for use exclusively with
79  * minimum log size calculations.
80  */
81 static void
82 xfs_log_calc_trans_resv_for_minlogblocks(
83 	struct xfs_mount	*mp,
84 	struct xfs_trans_resv	*resv)
85 {
86 	unsigned int		rmap_maxlevels = mp->m_rmap_maxlevels;
87 
88 	/*
89 	 * If the feature set is new enough, drop the oversized minimum log
90 	 * size computation introduced by the original reflink code.
91 	 */
92 	if (xfs_want_minlogsize_fixes(&mp->m_sb)) {
93 		xfs_trans_resv_calc(mp, resv);
94 		return;
95 	}
96 
97 	/*
98 	 * In the early days of rmap+reflink, we always set the rmap maxlevels
99 	 * to 9 even if the AG was small enough that it would never grow to
100 	 * that height.  Transaction reservation sizes influence the minimum
101 	 * log size calculation, which influences the size of the log that mkfs
102 	 * creates.  Use the old value here to ensure that newly formatted
103 	 * small filesystems will mount on older kernels.
104 	 */
105 	if (xfs_has_rmapbt(mp) && xfs_has_reflink(mp))
106 		mp->m_rmap_maxlevels = XFS_OLD_REFLINK_RMAP_MAXLEVELS;
107 
108 	xfs_trans_resv_calc(mp, resv);
109 
110 	if (xfs_has_reflink(mp)) {
111 		/*
112 		 * In the early days of reflink, typical log operation counts
113 		 * were greatly overestimated.
114 		 */
115 		resv->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK;
116 		resv->tr_itruncate.tr_logcount =
117 				XFS_ITRUNCATE_LOG_COUNT_REFLINK;
118 		resv->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK;
119 	} else if (xfs_has_rmapbt(mp)) {
120 		/*
121 		 * In the early days of non-reflink rmap, the impact of rmapbt
122 		 * updates on log counts were not taken into account at all.
123 		 */
124 		resv->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT;
125 		resv->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT;
126 		resv->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT;
127 	}
128 
129 	/*
130 	 * In the early days of reflink, we did not use deferred refcount
131 	 * update log items, so log reservations must be recomputed using the
132 	 * old calculations.
133 	 */
134 	resv->tr_write.tr_logres =
135 			xfs_calc_write_reservation_minlogsize(mp);
136 	resv->tr_itruncate.tr_logres =
137 			xfs_calc_itruncate_reservation_minlogsize(mp);
138 	resv->tr_qm_dqalloc.tr_logres =
139 			xfs_calc_qm_dqalloc_reservation_minlogsize(mp);
140 
141 	/* Put everything back the way it was.  This goes at the end. */
142 	mp->m_rmap_maxlevels = rmap_maxlevels;
143 }
144 
145 /*
146  * Iterate over the log space reservation table to figure out and return
147  * the maximum one in terms of the pre-calculated values which were done
148  * at mount time.
149  */
150 void
151 xfs_log_get_max_trans_res(
152 	struct xfs_mount	*mp,
153 	struct xfs_trans_res	*max_resp)
154 {
155 	struct xfs_trans_resv	resv = {};
156 	struct xfs_trans_res	*resp;
157 	struct xfs_trans_res	*end_resp;
158 	unsigned int		i;
159 	int			log_space = 0;
160 	int			attr_space;
161 
162 	attr_space = xfs_log_calc_max_attrsetm_res(mp);
163 
164 	xfs_log_calc_trans_resv_for_minlogblocks(mp, &resv);
165 
166 	resp = (struct xfs_trans_res *)&resv;
167 	end_resp = (struct xfs_trans_res *)(&resv + 1);
168 	for (i = 0; resp < end_resp; i++, resp++) {
169 		int		tmp = resp->tr_logcount > 1 ?
170 				      resp->tr_logres * resp->tr_logcount :
171 				      resp->tr_logres;
172 
173 		trace_xfs_trans_resv_calc_minlogsize(mp, i, resp);
174 		if (log_space < tmp) {
175 			log_space = tmp;
176 			*max_resp = *resp;		/* struct copy */
177 		}
178 	}
179 
180 	if (attr_space > log_space) {
181 		*max_resp = resv.tr_attrsetm;	/* struct copy */
182 		max_resp->tr_logres = attr_space;
183 	}
184 	trace_xfs_log_get_max_trans_res(mp, max_resp);
185 }
186 
187 /*
188  * Calculate the minimum valid log size for the given superblock configuration.
189  * Used to calculate the minimum log size at mkfs time, and to determine if
190  * the log is large enough or not at mount time. Returns the minimum size in
191  * filesystem block size units.
192  */
193 int
194 xfs_log_calc_minimum_size(
195 	struct xfs_mount	*mp)
196 {
197 	struct xfs_trans_res	tres = {0};
198 	int			max_logres;
199 	int			min_logblks = 0;
200 	int			lsunit = 0;
201 
202 	xfs_log_get_max_trans_res(mp, &tres);
203 
204 	max_logres = xfs_log_calc_unit_res(mp, tres.tr_logres);
205 	if (tres.tr_logcount > 1)
206 		max_logres *= tres.tr_logcount;
207 
208 	if (xfs_has_logv2(mp) && mp->m_sb.sb_logsunit > 1)
209 		lsunit = BTOBB(mp->m_sb.sb_logsunit);
210 
211 	/*
212 	 * Two factors should be taken into account for calculating the minimum
213 	 * log space.
214 	 * 1) The fundamental limitation is that no single transaction can be
215 	 *    larger than half size of the log.
216 	 *
217 	 *    From mkfs.xfs, this is considered by the XFS_MIN_LOG_FACTOR
218 	 *    define, which is set to 3. That means we can definitely fit
219 	 *    maximally sized 2 transactions in the log. We'll use this same
220 	 *    value here.
221 	 *
222 	 * 2) If the lsunit option is specified, a transaction requires 2 LSU
223 	 *    for the reservation because there are two log writes that can
224 	 *    require padding - the transaction data and the commit record which
225 	 *    are written separately and both can require padding to the LSU.
226 	 *    Consider that we can have an active CIL reservation holding 2*LSU,
227 	 *    but the CIL is not over a push threshold, in this case, if we
228 	 *    don't have enough log space for at one new transaction, which
229 	 *    includes another 2*LSU in the reservation, we will run into dead
230 	 *    loop situation in log space grant procedure. i.e.
231 	 *    xlog_grant_head_wait().
232 	 *
233 	 *    Hence the log size needs to be able to contain two maximally sized
234 	 *    and padded transactions, which is (2 * (2 * LSU + maxlres)).
235 	 *
236 	 * Also, the log size should be a multiple of the log stripe unit, round
237 	 * it up to lsunit boundary if lsunit is specified.
238 	 */
239 	if (lsunit) {
240 		min_logblks = roundup_64(BTOBB(max_logres), lsunit) +
241 			      2 * lsunit;
242 	} else
243 		min_logblks = BTOBB(max_logres) + 2 * BBSIZE;
244 	min_logblks *= XFS_MIN_LOG_FACTOR;
245 
246 	return XFS_BB_TO_FSB(mp, min_logblks);
247 }
248