xfs_log_rlimit.c 4.1 KB

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