xfs_log_rlimit.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. * Calculate the maximum length in bytes that would be required for a local
  20. * attribute value as large attributes out of line are not logged.
  21. */
  22. STATIC int
  23. xfs_log_calc_max_attrsetm_res(
  24. struct xfs_mount *mp)
  25. {
  26. int size;
  27. int nblks;
  28. size = xfs_attr_leaf_entsize_local_max(mp->m_attr_geo->blksize) -
  29. MAXNAMELEN - 1;
  30. nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
  31. nblks += XFS_B_TO_FSB(mp, size);
  32. nblks += XFS_NEXTENTADD_SPACE_RES(mp, size, XFS_ATTR_FORK);
  33. return M_RES(mp)->tr_attrsetm.tr_logres +
  34. M_RES(mp)->tr_attrsetrt.tr_logres * nblks;
  35. }
  36. /*
  37. * Compute an alternate set of log reservation sizes for use exclusively with
  38. * minimum log size calculations.
  39. */
  40. static void
  41. xfs_log_calc_trans_resv_for_minlogblocks(
  42. struct xfs_mount *mp,
  43. struct xfs_trans_resv *resv)
  44. {
  45. unsigned int rmap_maxlevels = mp->m_rmap_maxlevels;
  46. /*
  47. * In the early days of rmap+reflink, we always set the rmap maxlevels
  48. * to 9 even if the AG was small enough that it would never grow to
  49. * that height. Transaction reservation sizes influence the minimum
  50. * log size calculation, which influences the size of the log that mkfs
  51. * creates. Use the old value here to ensure that newly formatted
  52. * small filesystems will mount on older kernels.
  53. */
  54. if (xfs_has_rmapbt(mp) && xfs_has_reflink(mp))
  55. mp->m_rmap_maxlevels = XFS_OLD_REFLINK_RMAP_MAXLEVELS;
  56. xfs_trans_resv_calc(mp, resv);
  57. if (xfs_has_reflink(mp)) {
  58. /*
  59. * In the early days of reflink, typical log operation counts
  60. * were greatly overestimated.
  61. */
  62. resv->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK;
  63. resv->tr_itruncate.tr_logcount =
  64. XFS_ITRUNCATE_LOG_COUNT_REFLINK;
  65. resv->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK;
  66. } else if (xfs_has_rmapbt(mp)) {
  67. /*
  68. * In the early days of non-reflink rmap, the impact of rmapbt
  69. * updates on log counts were not taken into account at all.
  70. */
  71. resv->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT;
  72. resv->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT;
  73. resv->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT;
  74. }
  75. /*
  76. * In the early days of reflink, we did not use deferred refcount
  77. * update log items, so log reservations must be recomputed using the
  78. * old calculations.
  79. */
  80. resv->tr_write.tr_logres =
  81. xfs_calc_write_reservation_minlogsize(mp);
  82. resv->tr_itruncate.tr_logres =
  83. xfs_calc_itruncate_reservation_minlogsize(mp);
  84. resv->tr_qm_dqalloc.tr_logres =
  85. xfs_calc_qm_dqalloc_reservation_minlogsize(mp);
  86. /* Put everything back the way it was. This goes at the end. */
  87. mp->m_rmap_maxlevels = rmap_maxlevels;
  88. }
  89. /*
  90. * Iterate over the log space reservation table to figure out and return
  91. * the maximum one in terms of the pre-calculated values which were done
  92. * at mount time.
  93. */
  94. void
  95. xfs_log_get_max_trans_res(
  96. struct xfs_mount *mp,
  97. struct xfs_trans_res *max_resp)
  98. {
  99. struct xfs_trans_resv resv = {};
  100. struct xfs_trans_res *resp;
  101. struct xfs_trans_res *end_resp;
  102. unsigned int i;
  103. int log_space = 0;
  104. int attr_space;
  105. attr_space = xfs_log_calc_max_attrsetm_res(mp);
  106. xfs_log_calc_trans_resv_for_minlogblocks(mp, &resv);
  107. resp = (struct xfs_trans_res *)&resv;
  108. end_resp = (struct xfs_trans_res *)(&resv + 1);
  109. for (i = 0; resp < end_resp; i++, resp++) {
  110. int tmp = resp->tr_logcount > 1 ?
  111. resp->tr_logres * resp->tr_logcount :
  112. resp->tr_logres;
  113. trace_xfs_trans_resv_calc_minlogsize(mp, i, resp);
  114. if (log_space < tmp) {
  115. log_space = tmp;
  116. *max_resp = *resp; /* struct copy */
  117. }
  118. }
  119. if (attr_space > log_space) {
  120. *max_resp = resv.tr_attrsetm; /* struct copy */
  121. max_resp->tr_logres = attr_space;
  122. }
  123. trace_xfs_log_get_max_trans_res(mp, max_resp);
  124. }
  125. /*
  126. * Calculate the minimum valid log size for the given superblock configuration.
  127. * Used to calculate the minimum log size at mkfs time, and to determine if
  128. * the log is large enough or not at mount time. Returns the minimum size in
  129. * filesystem block size units.
  130. */
  131. int
  132. xfs_log_calc_minimum_size(
  133. struct xfs_mount *mp)
  134. {
  135. struct xfs_trans_res tres = {0};
  136. int max_logres;
  137. int min_logblks = 0;
  138. int lsunit = 0;
  139. xfs_log_get_max_trans_res(mp, &tres);
  140. max_logres = xfs_log_calc_unit_res(mp, tres.tr_logres);
  141. if (tres.tr_logcount > 1)
  142. max_logres *= tres.tr_logcount;
  143. if (xfs_has_logv2(mp) && mp->m_sb.sb_logsunit > 1)
  144. lsunit = BTOBB(mp->m_sb.sb_logsunit);
  145. /*
  146. * Two factors should be taken into account for calculating the minimum
  147. * log space.
  148. * 1) The fundamental limitation is that no single transaction can be
  149. * larger than half size of the log.
  150. *
  151. * From mkfs.xfs, this is considered by the XFS_MIN_LOG_FACTOR
  152. * define, which is set to 3. That means we can definitely fit
  153. * maximally sized 2 transactions in the log. We'll use this same
  154. * value here.
  155. *
  156. * 2) If the lsunit option is specified, a transaction requires 2 LSU
  157. * for the reservation because there are two log writes that can
  158. * require padding - the transaction data and the commit record which
  159. * are written separately and both can require padding to the LSU.
  160. * Consider that we can have an active CIL reservation holding 2*LSU,
  161. * but the CIL is not over a push threshold, in this case, if we
  162. * don't have enough log space for at one new transaction, which
  163. * includes another 2*LSU in the reservation, we will run into dead
  164. * loop situation in log space grant procedure. i.e.
  165. * xlog_grant_head_wait().
  166. *
  167. * Hence the log size needs to be able to contain two maximally sized
  168. * and padded transactions, which is (2 * (2 * LSU + maxlres)).
  169. *
  170. * Also, the log size should be a multiple of the log stripe unit, round
  171. * it up to lsunit boundary if lsunit is specified.
  172. */
  173. if (lsunit) {
  174. min_logblks = roundup_64(BTOBB(max_logres), lsunit) +
  175. 2 * lsunit;
  176. } else
  177. min_logblks = BTOBB(max_logres) + 2 * BBSIZE;
  178. min_logblks *= XFS_MIN_LOG_FACTOR;
  179. return XFS_BB_TO_FSB(mp, min_logblks);
  180. }