xfs_ag_resv.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (C) 2016 Oracle. All Rights Reserved.
  3. *
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it would be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write the Free Software Foundation,
  18. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include "xfs.h"
  21. #include "xfs_fs.h"
  22. #include "xfs_shared.h"
  23. #include "xfs_format.h"
  24. #include "xfs_log_format.h"
  25. #include "xfs_trans_resv.h"
  26. #include "xfs_sb.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_defer.h"
  29. #include "xfs_alloc.h"
  30. #include "xfs_error.h"
  31. #include "xfs_trace.h"
  32. #include "xfs_cksum.h"
  33. #include "xfs_trans.h"
  34. #include "xfs_bit.h"
  35. #include "xfs_bmap.h"
  36. #include "xfs_bmap_btree.h"
  37. #include "xfs_ag_resv.h"
  38. #include "xfs_trans_space.h"
  39. #include "xfs_rmap_btree.h"
  40. #include "xfs_btree.h"
  41. #include "xfs_refcount_btree.h"
  42. /*
  43. * Per-AG Block Reservations
  44. *
  45. * For some kinds of allocation group metadata structures, it is advantageous
  46. * to reserve a small number of blocks in each AG so that future expansions of
  47. * that data structure do not encounter ENOSPC because errors during a btree
  48. * split cause the filesystem to go offline.
  49. *
  50. * Prior to the introduction of reflink, this wasn't an issue because the free
  51. * space btrees maintain a reserve of space (the AGFL) to handle any expansion
  52. * that may be necessary; and allocations of other metadata (inodes, BMBT,
  53. * dir/attr) aren't restricted to a single AG. However, with reflink it is
  54. * possible to allocate all the space in an AG, have subsequent reflink/CoW
  55. * activity expand the refcount btree, and discover that there's no space left
  56. * to handle that expansion. Since we can calculate the maximum size of the
  57. * refcount btree, we can reserve space for it and avoid ENOSPC.
  58. *
  59. * Handling per-AG reservations consists of three changes to the allocator's
  60. * behavior: First, because these reservations are always needed, we decrease
  61. * the ag_max_usable counter to reflect the size of the AG after the reserved
  62. * blocks are taken. Second, the reservations must be reflected in the
  63. * fdblocks count to maintain proper accounting. Third, each AG must maintain
  64. * its own reserved block counter so that we can calculate the amount of space
  65. * that must remain free to maintain the reservations. Fourth, the "remaining
  66. * reserved blocks" count must be used when calculating the length of the
  67. * longest free extent in an AG and to clamp maxlen in the per-AG allocation
  68. * functions. In other words, we maintain a virtual allocation via in-core
  69. * accounting tricks so that we don't have to clean up after a crash. :)
  70. *
  71. * Reserved blocks can be managed by passing one of the enum xfs_ag_resv_type
  72. * values via struct xfs_alloc_arg or directly to the xfs_free_extent
  73. * function. It might seem a little funny to maintain a reservoir of blocks
  74. * to feed another reservoir, but the AGFL only holds enough blocks to get
  75. * through the next transaction. The per-AG reservation is to ensure (we
  76. * hope) that each AG never runs out of blocks. Each data structure wanting
  77. * to use the reservation system should update ask/used in xfs_ag_resv_init.
  78. */
  79. /*
  80. * Are we critically low on blocks? For now we'll define that as the number
  81. * of blocks we can get our hands on being less than 10% of what we reserved
  82. * or less than some arbitrary number (maximum btree height).
  83. */
  84. bool
  85. xfs_ag_resv_critical(
  86. struct xfs_perag *pag,
  87. enum xfs_ag_resv_type type)
  88. {
  89. xfs_extlen_t avail;
  90. xfs_extlen_t orig;
  91. switch (type) {
  92. case XFS_AG_RESV_METADATA:
  93. avail = pag->pagf_freeblks - pag->pag_agfl_resv.ar_reserved;
  94. orig = pag->pag_meta_resv.ar_asked;
  95. break;
  96. case XFS_AG_RESV_AGFL:
  97. avail = pag->pagf_freeblks + pag->pagf_flcount -
  98. pag->pag_meta_resv.ar_reserved;
  99. orig = pag->pag_agfl_resv.ar_asked;
  100. break;
  101. default:
  102. ASSERT(0);
  103. return false;
  104. }
  105. trace_xfs_ag_resv_critical(pag, type, avail);
  106. /* Critically low if less than 10% or max btree height remains. */
  107. return XFS_TEST_ERROR(avail < orig / 10 || avail < XFS_BTREE_MAXLEVELS,
  108. pag->pag_mount, XFS_ERRTAG_AG_RESV_CRITICAL,
  109. XFS_RANDOM_AG_RESV_CRITICAL);
  110. }
  111. /*
  112. * How many blocks are reserved but not used, and therefore must not be
  113. * allocated away?
  114. */
  115. xfs_extlen_t
  116. xfs_ag_resv_needed(
  117. struct xfs_perag *pag,
  118. enum xfs_ag_resv_type type)
  119. {
  120. xfs_extlen_t len;
  121. len = pag->pag_meta_resv.ar_reserved + pag->pag_agfl_resv.ar_reserved;
  122. switch (type) {
  123. case XFS_AG_RESV_METADATA:
  124. case XFS_AG_RESV_AGFL:
  125. len -= xfs_perag_resv(pag, type)->ar_reserved;
  126. break;
  127. case XFS_AG_RESV_NONE:
  128. /* empty */
  129. break;
  130. default:
  131. ASSERT(0);
  132. }
  133. trace_xfs_ag_resv_needed(pag, type, len);
  134. return len;
  135. }
  136. /* Clean out a reservation */
  137. static int
  138. __xfs_ag_resv_free(
  139. struct xfs_perag *pag,
  140. enum xfs_ag_resv_type type)
  141. {
  142. struct xfs_ag_resv *resv;
  143. xfs_extlen_t oldresv;
  144. int error;
  145. trace_xfs_ag_resv_free(pag, type, 0);
  146. resv = xfs_perag_resv(pag, type);
  147. pag->pag_mount->m_ag_max_usable += resv->ar_asked;
  148. /*
  149. * AGFL blocks are always considered "free", so whatever
  150. * was reserved at mount time must be given back at umount.
  151. */
  152. if (type == XFS_AG_RESV_AGFL)
  153. oldresv = resv->ar_orig_reserved;
  154. else
  155. oldresv = resv->ar_reserved;
  156. error = xfs_mod_fdblocks(pag->pag_mount, oldresv, true);
  157. resv->ar_reserved = 0;
  158. resv->ar_asked = 0;
  159. if (error)
  160. trace_xfs_ag_resv_free_error(pag->pag_mount, pag->pag_agno,
  161. error, _RET_IP_);
  162. return error;
  163. }
  164. /* Free a per-AG reservation. */
  165. int
  166. xfs_ag_resv_free(
  167. struct xfs_perag *pag)
  168. {
  169. int error;
  170. int err2;
  171. error = __xfs_ag_resv_free(pag, XFS_AG_RESV_AGFL);
  172. err2 = __xfs_ag_resv_free(pag, XFS_AG_RESV_METADATA);
  173. if (err2 && !error)
  174. error = err2;
  175. return error;
  176. }
  177. static int
  178. __xfs_ag_resv_init(
  179. struct xfs_perag *pag,
  180. enum xfs_ag_resv_type type,
  181. xfs_extlen_t ask,
  182. xfs_extlen_t used)
  183. {
  184. struct xfs_mount *mp = pag->pag_mount;
  185. struct xfs_ag_resv *resv;
  186. int error;
  187. resv = xfs_perag_resv(pag, type);
  188. if (used > ask)
  189. ask = used;
  190. resv->ar_asked = ask;
  191. resv->ar_reserved = resv->ar_orig_reserved = ask - used;
  192. mp->m_ag_max_usable -= ask;
  193. trace_xfs_ag_resv_init(pag, type, ask);
  194. error = xfs_mod_fdblocks(mp, -(int64_t)resv->ar_reserved, true);
  195. if (error)
  196. trace_xfs_ag_resv_init_error(pag->pag_mount, pag->pag_agno,
  197. error, _RET_IP_);
  198. return error;
  199. }
  200. /* Create a per-AG block reservation. */
  201. int
  202. xfs_ag_resv_init(
  203. struct xfs_perag *pag)
  204. {
  205. xfs_extlen_t ask;
  206. xfs_extlen_t used;
  207. int error = 0;
  208. /* Create the metadata reservation. */
  209. if (pag->pag_meta_resv.ar_asked == 0) {
  210. ask = used = 0;
  211. error = xfs_refcountbt_calc_reserves(pag->pag_mount,
  212. pag->pag_agno, &ask, &used);
  213. if (error)
  214. goto out;
  215. error = __xfs_ag_resv_init(pag, XFS_AG_RESV_METADATA,
  216. ask, used);
  217. if (error)
  218. goto out;
  219. }
  220. /* Create the AGFL metadata reservation */
  221. if (pag->pag_agfl_resv.ar_asked == 0) {
  222. ask = used = 0;
  223. error = xfs_rmapbt_calc_reserves(pag->pag_mount, pag->pag_agno,
  224. &ask, &used);
  225. if (error)
  226. goto out;
  227. error = __xfs_ag_resv_init(pag, XFS_AG_RESV_AGFL, ask, used);
  228. if (error)
  229. goto out;
  230. }
  231. ASSERT(xfs_perag_resv(pag, XFS_AG_RESV_METADATA)->ar_reserved +
  232. xfs_perag_resv(pag, XFS_AG_RESV_AGFL)->ar_reserved <=
  233. pag->pagf_freeblks + pag->pagf_flcount);
  234. out:
  235. return error;
  236. }
  237. /* Allocate a block from the reservation. */
  238. void
  239. xfs_ag_resv_alloc_extent(
  240. struct xfs_perag *pag,
  241. enum xfs_ag_resv_type type,
  242. struct xfs_alloc_arg *args)
  243. {
  244. struct xfs_ag_resv *resv;
  245. xfs_extlen_t len;
  246. uint field;
  247. trace_xfs_ag_resv_alloc_extent(pag, type, args->len);
  248. switch (type) {
  249. case XFS_AG_RESV_METADATA:
  250. case XFS_AG_RESV_AGFL:
  251. resv = xfs_perag_resv(pag, type);
  252. break;
  253. default:
  254. ASSERT(0);
  255. /* fall through */
  256. case XFS_AG_RESV_NONE:
  257. field = args->wasdel ? XFS_TRANS_SB_RES_FDBLOCKS :
  258. XFS_TRANS_SB_FDBLOCKS;
  259. xfs_trans_mod_sb(args->tp, field, -(int64_t)args->len);
  260. return;
  261. }
  262. len = min_t(xfs_extlen_t, args->len, resv->ar_reserved);
  263. resv->ar_reserved -= len;
  264. if (type == XFS_AG_RESV_AGFL)
  265. return;
  266. /* Allocations of reserved blocks only need on-disk sb updates... */
  267. xfs_trans_mod_sb(args->tp, XFS_TRANS_SB_RES_FDBLOCKS, -(int64_t)len);
  268. /* ...but non-reserved blocks need in-core and on-disk updates. */
  269. if (args->len > len)
  270. xfs_trans_mod_sb(args->tp, XFS_TRANS_SB_FDBLOCKS,
  271. -((int64_t)args->len - len));
  272. }
  273. /* Free a block to the reservation. */
  274. void
  275. xfs_ag_resv_free_extent(
  276. struct xfs_perag *pag,
  277. enum xfs_ag_resv_type type,
  278. struct xfs_trans *tp,
  279. xfs_extlen_t len)
  280. {
  281. xfs_extlen_t leftover;
  282. struct xfs_ag_resv *resv;
  283. trace_xfs_ag_resv_free_extent(pag, type, len);
  284. switch (type) {
  285. case XFS_AG_RESV_METADATA:
  286. case XFS_AG_RESV_AGFL:
  287. resv = xfs_perag_resv(pag, type);
  288. break;
  289. default:
  290. ASSERT(0);
  291. /* fall through */
  292. case XFS_AG_RESV_NONE:
  293. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (int64_t)len);
  294. return;
  295. }
  296. leftover = min_t(xfs_extlen_t, len, resv->ar_asked - resv->ar_reserved);
  297. resv->ar_reserved += leftover;
  298. if (type == XFS_AG_RESV_AGFL)
  299. return;
  300. /* Freeing into the reserved pool only requires on-disk update... */
  301. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FDBLOCKS, len);
  302. /* ...but freeing beyond that requires in-core and on-disk update. */
  303. if (len > leftover)
  304. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, len - leftover);
  305. }