| 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, resp: resv); |
| 94 | resv->tr_atomic_ioend = M_RES(mp)->tr_atomic_ioend; |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * In the early days of rmap+reflink, we always set the rmap maxlevels |
| 100 | * to 9 even if the AG was small enough that it would never grow to |
| 101 | * that height. Transaction reservation sizes influence the minimum |
| 102 | * log size calculation, which influences the size of the log that mkfs |
| 103 | * creates. Use the old value here to ensure that newly formatted |
| 104 | * small filesystems will mount on older kernels. |
| 105 | */ |
| 106 | if (xfs_has_rmapbt(mp) && xfs_has_reflink(mp)) |
| 107 | mp->m_rmap_maxlevels = XFS_OLD_REFLINK_RMAP_MAXLEVELS; |
| 108 | |
| 109 | xfs_trans_resv_calc(mp, resp: resv); |
| 110 | |
| 111 | /* Copy the dynamic transaction reservation types from the running fs */ |
| 112 | resv->tr_atomic_ioend = M_RES(mp)->tr_atomic_ioend; |
| 113 | |
| 114 | if (xfs_has_reflink(mp)) { |
| 115 | /* |
| 116 | * In the early days of reflink, typical log operation counts |
| 117 | * were greatly overestimated. |
| 118 | */ |
| 119 | resv->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK; |
| 120 | resv->tr_itruncate.tr_logcount = |
| 121 | XFS_ITRUNCATE_LOG_COUNT_REFLINK; |
| 122 | resv->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK; |
| 123 | } else if (xfs_has_rmapbt(mp)) { |
| 124 | /* |
| 125 | * In the early days of non-reflink rmap, the impact of rmapbt |
| 126 | * updates on log counts were not taken into account at all. |
| 127 | */ |
| 128 | resv->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT; |
| 129 | resv->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT; |
| 130 | resv->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * In the early days of reflink, we did not use deferred refcount |
| 135 | * update log items, so log reservations must be recomputed using the |
| 136 | * old calculations. |
| 137 | */ |
| 138 | resv->tr_write.tr_logres = |
| 139 | xfs_calc_write_reservation_minlogsize(mp); |
| 140 | resv->tr_itruncate.tr_logres = |
| 141 | xfs_calc_itruncate_reservation_minlogsize(mp); |
| 142 | resv->tr_qm_dqalloc.tr_logres = |
| 143 | xfs_calc_qm_dqalloc_reservation_minlogsize(mp); |
| 144 | |
| 145 | /* Put everything back the way it was. This goes at the end. */ |
| 146 | mp->m_rmap_maxlevels = rmap_maxlevels; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Iterate over the log space reservation table to figure out and return |
| 151 | * the maximum one in terms of the pre-calculated values which were done |
| 152 | * at mount time. |
| 153 | */ |
| 154 | void |
| 155 | xfs_log_get_max_trans_res( |
| 156 | struct xfs_mount *mp, |
| 157 | struct xfs_trans_res *max_resp) |
| 158 | { |
| 159 | struct xfs_trans_resv resv = {}; |
| 160 | struct xfs_trans_res *resp; |
| 161 | struct xfs_trans_res *end_resp; |
| 162 | unsigned int i; |
| 163 | int log_space = 0; |
| 164 | int attr_space; |
| 165 | |
| 166 | attr_space = xfs_log_calc_max_attrsetm_res(mp); |
| 167 | |
| 168 | xfs_log_calc_trans_resv_for_minlogblocks(mp, resv: &resv); |
| 169 | |
| 170 | resp = (struct xfs_trans_res *)&resv; |
| 171 | end_resp = (struct xfs_trans_res *)(&resv + 1); |
| 172 | for (i = 0; resp < end_resp; i++, resp++) { |
| 173 | int tmp = resp->tr_logcount > 1 ? |
| 174 | resp->tr_logres * resp->tr_logcount : |
| 175 | resp->tr_logres; |
| 176 | |
| 177 | trace_xfs_trans_resv_calc_minlogsize(mp, i, resp); |
| 178 | if (log_space < tmp) { |
| 179 | log_space = tmp; |
| 180 | *max_resp = *resp; /* struct copy */ |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (attr_space > log_space) { |
| 185 | *max_resp = resv.tr_attrsetm; /* struct copy */ |
| 186 | max_resp->tr_logres = attr_space; |
| 187 | } |
| 188 | trace_xfs_log_get_max_trans_res(mp, max_resp); |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Calculate the minimum valid log size for the given superblock configuration. |
| 193 | * Used to calculate the minimum log size at mkfs time, and to determine if |
| 194 | * the log is large enough or not at mount time. Returns the minimum size in |
| 195 | * filesystem block size units. |
| 196 | */ |
| 197 | int |
| 198 | xfs_log_calc_minimum_size( |
| 199 | struct xfs_mount *mp) |
| 200 | { |
| 201 | struct xfs_trans_res tres = {0}; |
| 202 | int max_logres; |
| 203 | int min_logblks = 0; |
| 204 | int lsunit = 0; |
| 205 | |
| 206 | xfs_log_get_max_trans_res(mp, max_resp: &tres); |
| 207 | |
| 208 | max_logres = xfs_log_calc_unit_res(mp, unit_bytes: tres.tr_logres); |
| 209 | if (tres.tr_logcount > 1) |
| 210 | max_logres *= tres.tr_logcount; |
| 211 | |
| 212 | if (xfs_has_logv2(mp) && mp->m_sb.sb_logsunit > 1) |
| 213 | lsunit = BTOBB(mp->m_sb.sb_logsunit); |
| 214 | |
| 215 | /* |
| 216 | * Two factors should be taken into account for calculating the minimum |
| 217 | * log space. |
| 218 | * 1) The fundamental limitation is that no single transaction can be |
| 219 | * larger than half size of the log. |
| 220 | * |
| 221 | * From mkfs.xfs, this is considered by the XFS_MIN_LOG_FACTOR |
| 222 | * define, which is set to 3. That means we can definitely fit |
| 223 | * maximally sized 2 transactions in the log. We'll use this same |
| 224 | * value here. |
| 225 | * |
| 226 | * 2) If the lsunit option is specified, a transaction requires 2 LSU |
| 227 | * for the reservation because there are two log writes that can |
| 228 | * require padding - the transaction data and the commit record which |
| 229 | * are written separately and both can require padding to the LSU. |
| 230 | * Consider that we can have an active CIL reservation holding 2*LSU, |
| 231 | * but the CIL is not over a push threshold, in this case, if we |
| 232 | * don't have enough log space for at one new transaction, which |
| 233 | * includes another 2*LSU in the reservation, we will run into dead |
| 234 | * loop situation in log space grant procedure. i.e. |
| 235 | * xlog_grant_head_wait(). |
| 236 | * |
| 237 | * Hence the log size needs to be able to contain two maximally sized |
| 238 | * and padded transactions, which is (2 * (2 * LSU + maxlres)). |
| 239 | * |
| 240 | * Also, the log size should be a multiple of the log stripe unit, round |
| 241 | * it up to lsunit boundary if lsunit is specified. |
| 242 | */ |
| 243 | if (lsunit) { |
| 244 | min_logblks = roundup_64(BTOBB(max_logres), lsunit) + |
| 245 | 2 * lsunit; |
| 246 | } else |
| 247 | min_logblks = BTOBB(max_logres) + 2 * BBSIZE; |
| 248 | min_logblks *= XFS_MIN_LOG_FACTOR; |
| 249 | |
| 250 | return XFS_BB_TO_FSB(mp, min_logblks); |
| 251 | } |
| 252 | |