1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2013 Jie Liu.
4*4882a593Smuzhiyun * All Rights Reserved.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun #include "xfs.h"
7*4882a593Smuzhiyun #include "xfs_fs.h"
8*4882a593Smuzhiyun #include "xfs_shared.h"
9*4882a593Smuzhiyun #include "xfs_format.h"
10*4882a593Smuzhiyun #include "xfs_log_format.h"
11*4882a593Smuzhiyun #include "xfs_trans_resv.h"
12*4882a593Smuzhiyun #include "xfs_mount.h"
13*4882a593Smuzhiyun #include "xfs_da_format.h"
14*4882a593Smuzhiyun #include "xfs_trans_space.h"
15*4882a593Smuzhiyun #include "xfs_da_btree.h"
16*4882a593Smuzhiyun #include "xfs_bmap_btree.h"
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun * Calculate the maximum length in bytes that would be required for a local
20*4882a593Smuzhiyun * attribute value as large attributes out of line are not logged.
21*4882a593Smuzhiyun */
22*4882a593Smuzhiyun STATIC int
xfs_log_calc_max_attrsetm_res(struct xfs_mount * mp)23*4882a593Smuzhiyun xfs_log_calc_max_attrsetm_res(
24*4882a593Smuzhiyun struct xfs_mount *mp)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun int size;
27*4882a593Smuzhiyun int nblks;
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun size = xfs_attr_leaf_entsize_local_max(mp->m_attr_geo->blksize) -
30*4882a593Smuzhiyun MAXNAMELEN - 1;
31*4882a593Smuzhiyun nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
32*4882a593Smuzhiyun nblks += XFS_B_TO_FSB(mp, size);
33*4882a593Smuzhiyun nblks += XFS_NEXTENTADD_SPACE_RES(mp, size, XFS_ATTR_FORK);
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun return M_RES(mp)->tr_attrsetm.tr_logres +
36*4882a593Smuzhiyun M_RES(mp)->tr_attrsetrt.tr_logres * nblks;
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun /*
40*4882a593Smuzhiyun * Iterate over the log space reservation table to figure out and return
41*4882a593Smuzhiyun * the maximum one in terms of the pre-calculated values which were done
42*4882a593Smuzhiyun * at mount time.
43*4882a593Smuzhiyun */
44*4882a593Smuzhiyun void
xfs_log_get_max_trans_res(struct xfs_mount * mp,struct xfs_trans_res * max_resp)45*4882a593Smuzhiyun xfs_log_get_max_trans_res(
46*4882a593Smuzhiyun struct xfs_mount *mp,
47*4882a593Smuzhiyun struct xfs_trans_res *max_resp)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun struct xfs_trans_res *resp;
50*4882a593Smuzhiyun struct xfs_trans_res *end_resp;
51*4882a593Smuzhiyun int log_space = 0;
52*4882a593Smuzhiyun int attr_space;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun attr_space = xfs_log_calc_max_attrsetm_res(mp);
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun resp = (struct xfs_trans_res *)M_RES(mp);
57*4882a593Smuzhiyun end_resp = (struct xfs_trans_res *)(M_RES(mp) + 1);
58*4882a593Smuzhiyun for (; resp < end_resp; resp++) {
59*4882a593Smuzhiyun int tmp = resp->tr_logcount > 1 ?
60*4882a593Smuzhiyun resp->tr_logres * resp->tr_logcount :
61*4882a593Smuzhiyun resp->tr_logres;
62*4882a593Smuzhiyun if (log_space < tmp) {
63*4882a593Smuzhiyun log_space = tmp;
64*4882a593Smuzhiyun *max_resp = *resp; /* struct copy */
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun if (attr_space > log_space) {
69*4882a593Smuzhiyun *max_resp = M_RES(mp)->tr_attrsetm; /* struct copy */
70*4882a593Smuzhiyun max_resp->tr_logres = attr_space;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /*
75*4882a593Smuzhiyun * Calculate the minimum valid log size for the given superblock configuration.
76*4882a593Smuzhiyun * Used to calculate the minimum log size at mkfs time, and to determine if
77*4882a593Smuzhiyun * the log is large enough or not at mount time. Returns the minimum size in
78*4882a593Smuzhiyun * filesystem block size units.
79*4882a593Smuzhiyun */
80*4882a593Smuzhiyun int
xfs_log_calc_minimum_size(struct xfs_mount * mp)81*4882a593Smuzhiyun xfs_log_calc_minimum_size(
82*4882a593Smuzhiyun struct xfs_mount *mp)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun struct xfs_trans_res tres = {0};
85*4882a593Smuzhiyun int max_logres;
86*4882a593Smuzhiyun int min_logblks = 0;
87*4882a593Smuzhiyun int lsunit = 0;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun xfs_log_get_max_trans_res(mp, &tres);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun max_logres = xfs_log_calc_unit_res(mp, tres.tr_logres);
92*4882a593Smuzhiyun if (tres.tr_logcount > 1)
93*4882a593Smuzhiyun max_logres *= tres.tr_logcount;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun if (xfs_sb_version_haslogv2(&mp->m_sb) && mp->m_sb.sb_logsunit > 1)
96*4882a593Smuzhiyun lsunit = BTOBB(mp->m_sb.sb_logsunit);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /*
99*4882a593Smuzhiyun * Two factors should be taken into account for calculating the minimum
100*4882a593Smuzhiyun * log space.
101*4882a593Smuzhiyun * 1) The fundamental limitation is that no single transaction can be
102*4882a593Smuzhiyun * larger than half size of the log.
103*4882a593Smuzhiyun *
104*4882a593Smuzhiyun * From mkfs.xfs, this is considered by the XFS_MIN_LOG_FACTOR
105*4882a593Smuzhiyun * define, which is set to 3. That means we can definitely fit
106*4882a593Smuzhiyun * maximally sized 2 transactions in the log. We'll use this same
107*4882a593Smuzhiyun * value here.
108*4882a593Smuzhiyun *
109*4882a593Smuzhiyun * 2) If the lsunit option is specified, a transaction requires 2 LSU
110*4882a593Smuzhiyun * for the reservation because there are two log writes that can
111*4882a593Smuzhiyun * require padding - the transaction data and the commit record which
112*4882a593Smuzhiyun * are written separately and both can require padding to the LSU.
113*4882a593Smuzhiyun * Consider that we can have an active CIL reservation holding 2*LSU,
114*4882a593Smuzhiyun * but the CIL is not over a push threshold, in this case, if we
115*4882a593Smuzhiyun * don't have enough log space for at one new transaction, which
116*4882a593Smuzhiyun * includes another 2*LSU in the reservation, we will run into dead
117*4882a593Smuzhiyun * loop situation in log space grant procedure. i.e.
118*4882a593Smuzhiyun * xlog_grant_head_wait().
119*4882a593Smuzhiyun *
120*4882a593Smuzhiyun * Hence the log size needs to be able to contain two maximally sized
121*4882a593Smuzhiyun * and padded transactions, which is (2 * (2 * LSU + maxlres)).
122*4882a593Smuzhiyun *
123*4882a593Smuzhiyun * Also, the log size should be a multiple of the log stripe unit, round
124*4882a593Smuzhiyun * it up to lsunit boundary if lsunit is specified.
125*4882a593Smuzhiyun */
126*4882a593Smuzhiyun if (lsunit) {
127*4882a593Smuzhiyun min_logblks = roundup_64(BTOBB(max_logres), lsunit) +
128*4882a593Smuzhiyun 2 * lsunit;
129*4882a593Smuzhiyun } else
130*4882a593Smuzhiyun min_logblks = BTOBB(max_logres) + 2 * BBSIZE;
131*4882a593Smuzhiyun min_logblks *= XFS_MIN_LOG_FACTOR;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun return XFS_BB_TO_FSB(mp, min_logblks);
134*4882a593Smuzhiyun }
135