10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0 230f712c9SDave Chinner /* 330f712c9SDave Chinner * Copyright (c) 2013 Jie Liu. 430f712c9SDave Chinner * All Rights Reserved. 530f712c9SDave Chinner */ 630f712c9SDave Chinner #include "xfs.h" 730f712c9SDave Chinner #include "xfs_fs.h" 830f712c9SDave Chinner #include "xfs_shared.h" 930f712c9SDave Chinner #include "xfs_format.h" 1030f712c9SDave Chinner #include "xfs_log_format.h" 1130f712c9SDave Chinner #include "xfs_trans_resv.h" 1230f712c9SDave Chinner #include "xfs_mount.h" 1330f712c9SDave Chinner #include "xfs_da_format.h" 1430f712c9SDave Chinner #include "xfs_trans_space.h" 1530f712c9SDave Chinner #include "xfs_da_btree.h" 1630f712c9SDave Chinner #include "xfs_bmap_btree.h" 1752d8ea4fSDarrick J. Wong #include "xfs_trace.h" 1830f712c9SDave Chinner 1930f712c9SDave Chinner /* 207ea816caSDarrick J. Wong * Shortly after enabling the large extents count feature in 2023, longstanding 217ea816caSDarrick J. Wong * bugs were found in the code that computes the minimum log size. Luckily, 227ea816caSDarrick J. Wong * the bugs resulted in over-estimates of that size, so there's no impact to 237ea816caSDarrick J. Wong * existing users. However, we don't want to reduce the minimum log size 247ea816caSDarrick J. Wong * because that can create the situation where a newer mkfs writes a new 257ea816caSDarrick J. Wong * filesystem that an older kernel won't mount. 267ea816caSDarrick J. Wong * 27*6ed858c7SDarrick J. Wong * Several years prior, we also discovered that the transaction reservations 28*6ed858c7SDarrick J. Wong * for rmap and reflink operations were unnecessarily large. That was fixed, 29*6ed858c7SDarrick J. Wong * but the minimum log size computation was left alone to avoid the 30*6ed858c7SDarrick J. Wong * compatibility problems noted above. Fix that too. 31*6ed858c7SDarrick J. Wong * 327ea816caSDarrick J. Wong * Therefore, we only may correct the computation starting with filesystem 337ea816caSDarrick J. Wong * features that didn't exist in 2023. In other words, only turn this on if 347ea816caSDarrick J. Wong * the filesystem has parent pointers. 357ea816caSDarrick J. Wong * 367ea816caSDarrick J. Wong * This function can be called before the XFS_HAS_* flags have been set up, 377ea816caSDarrick J. Wong * (e.g. mkfs) so we must check the ondisk superblock. 387ea816caSDarrick J. Wong */ 397ea816caSDarrick J. Wong static inline bool 407ea816caSDarrick J. Wong xfs_want_minlogsize_fixes( 417ea816caSDarrick J. Wong struct xfs_sb *sb) 427ea816caSDarrick J. Wong { 437ea816caSDarrick J. Wong return xfs_sb_is_v5(sb) && 447ea816caSDarrick J. Wong xfs_sb_has_incompat_feature(sb, XFS_SB_FEAT_INCOMPAT_PARENT); 457ea816caSDarrick J. Wong } 467ea816caSDarrick J. Wong 477ea816caSDarrick J. Wong /* 4830f712c9SDave Chinner * Calculate the maximum length in bytes that would be required for a local 4930f712c9SDave Chinner * attribute value as large attributes out of line are not logged. 5030f712c9SDave Chinner */ 5130f712c9SDave Chinner STATIC int 5230f712c9SDave Chinner xfs_log_calc_max_attrsetm_res( 5330f712c9SDave Chinner struct xfs_mount *mp) 5430f712c9SDave Chinner { 5530f712c9SDave Chinner int size; 5630f712c9SDave Chinner int nblks; 5730f712c9SDave Chinner 5830f712c9SDave Chinner size = xfs_attr_leaf_entsize_local_max(mp->m_attr_geo->blksize) - 5930f712c9SDave Chinner MAXNAMELEN - 1; 6030f712c9SDave Chinner nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK); 6130f712c9SDave Chinner nblks += XFS_B_TO_FSB(mp, size); 627ea816caSDarrick J. Wong 637ea816caSDarrick J. Wong /* 647ea816caSDarrick J. Wong * If the feature set is new enough, correct a unit conversion error in 657ea816caSDarrick J. Wong * the xattr transaction reservation code that resulted in oversized 667ea816caSDarrick J. Wong * minimum log size computations. 677ea816caSDarrick J. Wong */ 687ea816caSDarrick J. Wong if (xfs_want_minlogsize_fixes(&mp->m_sb)) 697ea816caSDarrick J. Wong size = XFS_B_TO_FSB(mp, size); 707ea816caSDarrick J. Wong 7130f712c9SDave Chinner nblks += XFS_NEXTENTADD_SPACE_RES(mp, size, XFS_ATTR_FORK); 7230f712c9SDave Chinner 7330f712c9SDave Chinner return M_RES(mp)->tr_attrsetm.tr_logres + 7430f712c9SDave Chinner M_RES(mp)->tr_attrsetrt.tr_logres * nblks; 7530f712c9SDave Chinner } 7630f712c9SDave Chinner 7730f712c9SDave Chinner /* 784ecf9e7cSDarrick J. Wong * Compute an alternate set of log reservation sizes for use exclusively with 794ecf9e7cSDarrick J. Wong * minimum log size calculations. 804ecf9e7cSDarrick J. Wong */ 814ecf9e7cSDarrick J. Wong static void 824ecf9e7cSDarrick J. Wong xfs_log_calc_trans_resv_for_minlogblocks( 834ecf9e7cSDarrick J. Wong struct xfs_mount *mp, 844ecf9e7cSDarrick J. Wong struct xfs_trans_resv *resv) 854ecf9e7cSDarrick J. Wong { 864ecf9e7cSDarrick J. Wong unsigned int rmap_maxlevels = mp->m_rmap_maxlevels; 874ecf9e7cSDarrick J. Wong 884ecf9e7cSDarrick J. Wong /* 89*6ed858c7SDarrick J. Wong * If the feature set is new enough, drop the oversized minimum log 90*6ed858c7SDarrick J. Wong * size computation introduced by the original reflink code. 91*6ed858c7SDarrick J. Wong */ 92*6ed858c7SDarrick J. Wong if (xfs_want_minlogsize_fixes(&mp->m_sb)) { 93*6ed858c7SDarrick J. Wong xfs_trans_resv_calc(mp, resv); 94*6ed858c7SDarrick J. Wong return; 95*6ed858c7SDarrick J. Wong } 96*6ed858c7SDarrick J. Wong 97*6ed858c7SDarrick J. Wong /* 984ecf9e7cSDarrick J. Wong * In the early days of rmap+reflink, we always set the rmap maxlevels 994ecf9e7cSDarrick J. Wong * to 9 even if the AG was small enough that it would never grow to 1004ecf9e7cSDarrick J. Wong * that height. Transaction reservation sizes influence the minimum 1014ecf9e7cSDarrick J. Wong * log size calculation, which influences the size of the log that mkfs 1024ecf9e7cSDarrick J. Wong * creates. Use the old value here to ensure that newly formatted 1034ecf9e7cSDarrick J. Wong * small filesystems will mount on older kernels. 1044ecf9e7cSDarrick J. Wong */ 1054ecf9e7cSDarrick J. Wong if (xfs_has_rmapbt(mp) && xfs_has_reflink(mp)) 1064ecf9e7cSDarrick J. Wong mp->m_rmap_maxlevels = XFS_OLD_REFLINK_RMAP_MAXLEVELS; 1074ecf9e7cSDarrick J. Wong 1084ecf9e7cSDarrick J. Wong xfs_trans_resv_calc(mp, resv); 1094ecf9e7cSDarrick J. Wong 1104ecf9e7cSDarrick J. Wong if (xfs_has_reflink(mp)) { 1114ecf9e7cSDarrick J. Wong /* 1124ecf9e7cSDarrick J. Wong * In the early days of reflink, typical log operation counts 1134ecf9e7cSDarrick J. Wong * were greatly overestimated. 1144ecf9e7cSDarrick J. Wong */ 1154ecf9e7cSDarrick J. Wong resv->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK; 1164ecf9e7cSDarrick J. Wong resv->tr_itruncate.tr_logcount = 1174ecf9e7cSDarrick J. Wong XFS_ITRUNCATE_LOG_COUNT_REFLINK; 1184ecf9e7cSDarrick J. Wong resv->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK; 1194ecf9e7cSDarrick J. Wong } else if (xfs_has_rmapbt(mp)) { 1204ecf9e7cSDarrick J. Wong /* 1214ecf9e7cSDarrick J. Wong * In the early days of non-reflink rmap, the impact of rmapbt 1224ecf9e7cSDarrick J. Wong * updates on log counts were not taken into account at all. 1234ecf9e7cSDarrick J. Wong */ 1244ecf9e7cSDarrick J. Wong resv->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT; 1254ecf9e7cSDarrick J. Wong resv->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT; 1264ecf9e7cSDarrick J. Wong resv->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT; 1274ecf9e7cSDarrick J. Wong } 1284ecf9e7cSDarrick J. Wong 129b037c4eeSDarrick J. Wong /* 130b037c4eeSDarrick J. Wong * In the early days of reflink, we did not use deferred refcount 131b037c4eeSDarrick J. Wong * update log items, so log reservations must be recomputed using the 132b037c4eeSDarrick J. Wong * old calculations. 133b037c4eeSDarrick J. Wong */ 134b037c4eeSDarrick J. Wong resv->tr_write.tr_logres = 135b037c4eeSDarrick J. Wong xfs_calc_write_reservation_minlogsize(mp); 136b037c4eeSDarrick J. Wong resv->tr_itruncate.tr_logres = 137b037c4eeSDarrick J. Wong xfs_calc_itruncate_reservation_minlogsize(mp); 138b037c4eeSDarrick J. Wong resv->tr_qm_dqalloc.tr_logres = 139b037c4eeSDarrick J. Wong xfs_calc_qm_dqalloc_reservation_minlogsize(mp); 140b037c4eeSDarrick J. Wong 1414ecf9e7cSDarrick J. Wong /* Put everything back the way it was. This goes at the end. */ 1424ecf9e7cSDarrick J. Wong mp->m_rmap_maxlevels = rmap_maxlevels; 1434ecf9e7cSDarrick J. Wong } 1444ecf9e7cSDarrick J. Wong 1454ecf9e7cSDarrick J. Wong /* 14630f712c9SDave Chinner * Iterate over the log space reservation table to figure out and return 14730f712c9SDave Chinner * the maximum one in terms of the pre-calculated values which were done 14830f712c9SDave Chinner * at mount time. 14930f712c9SDave Chinner */ 150b872af2cSDarrick J. Wong void 15130f712c9SDave Chinner xfs_log_get_max_trans_res( 15230f712c9SDave Chinner struct xfs_mount *mp, 15330f712c9SDave Chinner struct xfs_trans_res *max_resp) 15430f712c9SDave Chinner { 1554ecf9e7cSDarrick J. Wong struct xfs_trans_resv resv = {}; 15630f712c9SDave Chinner struct xfs_trans_res *resp; 15730f712c9SDave Chinner struct xfs_trans_res *end_resp; 15852d8ea4fSDarrick J. Wong unsigned int i; 15930f712c9SDave Chinner int log_space = 0; 16030f712c9SDave Chinner int attr_space; 16130f712c9SDave Chinner 16230f712c9SDave Chinner attr_space = xfs_log_calc_max_attrsetm_res(mp); 16330f712c9SDave Chinner 1644ecf9e7cSDarrick J. Wong xfs_log_calc_trans_resv_for_minlogblocks(mp, &resv); 16552d8ea4fSDarrick J. Wong 16652d8ea4fSDarrick J. Wong resp = (struct xfs_trans_res *)&resv; 16752d8ea4fSDarrick J. Wong end_resp = (struct xfs_trans_res *)(&resv + 1); 16852d8ea4fSDarrick J. Wong for (i = 0; resp < end_resp; i++, resp++) { 16930f712c9SDave Chinner int tmp = resp->tr_logcount > 1 ? 17030f712c9SDave Chinner resp->tr_logres * resp->tr_logcount : 17130f712c9SDave Chinner resp->tr_logres; 17252d8ea4fSDarrick J. Wong 17352d8ea4fSDarrick J. Wong trace_xfs_trans_resv_calc_minlogsize(mp, i, resp); 17430f712c9SDave Chinner if (log_space < tmp) { 17530f712c9SDave Chinner log_space = tmp; 17630f712c9SDave Chinner *max_resp = *resp; /* struct copy */ 17730f712c9SDave Chinner } 17830f712c9SDave Chinner } 17930f712c9SDave Chinner 18030f712c9SDave Chinner if (attr_space > log_space) { 18152d8ea4fSDarrick J. Wong *max_resp = resv.tr_attrsetm; /* struct copy */ 18230f712c9SDave Chinner max_resp->tr_logres = attr_space; 18330f712c9SDave Chinner } 184918247ceSDarrick J. Wong trace_xfs_log_get_max_trans_res(mp, max_resp); 18530f712c9SDave Chinner } 18630f712c9SDave Chinner 18730f712c9SDave Chinner /* 18830f712c9SDave Chinner * Calculate the minimum valid log size for the given superblock configuration. 18930f712c9SDave Chinner * Used to calculate the minimum log size at mkfs time, and to determine if 19030f712c9SDave Chinner * the log is large enough or not at mount time. Returns the minimum size in 19130f712c9SDave Chinner * filesystem block size units. 19230f712c9SDave Chinner */ 19330f712c9SDave Chinner int 19430f712c9SDave Chinner xfs_log_calc_minimum_size( 19530f712c9SDave Chinner struct xfs_mount *mp) 19630f712c9SDave Chinner { 19730f712c9SDave Chinner struct xfs_trans_res tres = {0}; 19830f712c9SDave Chinner int max_logres; 19930f712c9SDave Chinner int min_logblks = 0; 20030f712c9SDave Chinner int lsunit = 0; 20130f712c9SDave Chinner 20230f712c9SDave Chinner xfs_log_get_max_trans_res(mp, &tres); 20330f712c9SDave Chinner 20430f712c9SDave Chinner max_logres = xfs_log_calc_unit_res(mp, tres.tr_logres); 20530f712c9SDave Chinner if (tres.tr_logcount > 1) 20630f712c9SDave Chinner max_logres *= tres.tr_logcount; 20730f712c9SDave Chinner 20838c26bfdSDave Chinner if (xfs_has_logv2(mp) && mp->m_sb.sb_logsunit > 1) 20930f712c9SDave Chinner lsunit = BTOBB(mp->m_sb.sb_logsunit); 21030f712c9SDave Chinner 21130f712c9SDave Chinner /* 21230f712c9SDave Chinner * Two factors should be taken into account for calculating the minimum 21330f712c9SDave Chinner * log space. 21430f712c9SDave Chinner * 1) The fundamental limitation is that no single transaction can be 21530f712c9SDave Chinner * larger than half size of the log. 21630f712c9SDave Chinner * 21730f712c9SDave Chinner * From mkfs.xfs, this is considered by the XFS_MIN_LOG_FACTOR 21830f712c9SDave Chinner * define, which is set to 3. That means we can definitely fit 21930f712c9SDave Chinner * maximally sized 2 transactions in the log. We'll use this same 22030f712c9SDave Chinner * value here. 22130f712c9SDave Chinner * 22230f712c9SDave Chinner * 2) If the lsunit option is specified, a transaction requires 2 LSU 22330f712c9SDave Chinner * for the reservation because there are two log writes that can 22430f712c9SDave Chinner * require padding - the transaction data and the commit record which 22530f712c9SDave Chinner * are written separately and both can require padding to the LSU. 22630f712c9SDave Chinner * Consider that we can have an active CIL reservation holding 2*LSU, 22730f712c9SDave Chinner * but the CIL is not over a push threshold, in this case, if we 22830f712c9SDave Chinner * don't have enough log space for at one new transaction, which 22930f712c9SDave Chinner * includes another 2*LSU in the reservation, we will run into dead 23030f712c9SDave Chinner * loop situation in log space grant procedure. i.e. 23130f712c9SDave Chinner * xlog_grant_head_wait(). 23230f712c9SDave Chinner * 23330f712c9SDave Chinner * Hence the log size needs to be able to contain two maximally sized 23430f712c9SDave Chinner * and padded transactions, which is (2 * (2 * LSU + maxlres)). 23530f712c9SDave Chinner * 23630f712c9SDave Chinner * Also, the log size should be a multiple of the log stripe unit, round 23730f712c9SDave Chinner * it up to lsunit boundary if lsunit is specified. 23830f712c9SDave Chinner */ 23930f712c9SDave Chinner if (lsunit) { 24030f712c9SDave Chinner min_logblks = roundup_64(BTOBB(max_logres), lsunit) + 24130f712c9SDave Chinner 2 * lsunit; 24230f712c9SDave Chinner } else 24330f712c9SDave Chinner min_logblks = BTOBB(max_logres) + 2 * BBSIZE; 24430f712c9SDave Chinner min_logblks *= XFS_MIN_LOG_FACTOR; 24530f712c9SDave Chinner 24630f712c9SDave Chinner return XFS_BB_TO_FSB(mp, min_logblks); 24730f712c9SDave Chinner } 248