xfs_ag_resv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #include "xfs_ialloc_btree.h"
  43. /*
  44. * Per-AG Block Reservations
  45. *
  46. * For some kinds of allocation group metadata structures, it is advantageous
  47. * to reserve a small number of blocks in each AG so that future expansions of
  48. * that data structure do not encounter ENOSPC because errors during a btree
  49. * split cause the filesystem to go offline.
  50. *
  51. * Prior to the introduction of reflink, this wasn't an issue because the free
  52. * space btrees maintain a reserve of space (the AGFL) to handle any expansion
  53. * that may be necessary; and allocations of other metadata (inodes, BMBT,
  54. * dir/attr) aren't restricted to a single AG. However, with reflink it is
  55. * possible to allocate all the space in an AG, have subsequent reflink/CoW
  56. * activity expand the refcount btree, and discover that there's no space left
  57. * to handle that expansion. Since we can calculate the maximum size of the
  58. * refcount btree, we can reserve space for it and avoid ENOSPC.
  59. *
  60. * Handling per-AG reservations consists of three changes to the allocator's
  61. * behavior: First, because these reservations are always needed, we decrease
  62. * the ag_max_usable counter to reflect the size of the AG after the reserved
  63. * blocks are taken. Second, the reservations must be reflected in the
  64. * fdblocks count to maintain proper accounting. Third, each AG must maintain
  65. * its own reserved block counter so that we can calculate the amount of space
  66. * that must remain free to maintain the reservations. Fourth, the "remaining
  67. * reserved blocks" count must be used when calculating the length of the
  68. * longest free extent in an AG and to clamp maxlen in the per-AG allocation
  69. * functions. In other words, we maintain a virtual allocation via in-core
  70. * accounting tricks so that we don't have to clean up after a crash. :)
  71. *
  72. * Reserved blocks can be managed by passing one of the enum xfs_ag_resv_type
  73. * values via struct xfs_alloc_arg or directly to the xfs_free_extent
  74. * function. It might seem a little funny to maintain a reservoir of blocks
  75. * to feed another reservoir, but the AGFL only holds enough blocks to get
  76. * through the next transaction. The per-AG reservation is to ensure (we
  77. * hope) that each AG never runs out of blocks. Each data structure wanting
  78. * to use the reservation system should update ask/used in xfs_ag_resv_init.
  79. */
  80. /*
  81. * Are we critically low on blocks? For now we'll define that as the number
  82. * of blocks we can get our hands on being less than 10% of what we reserved
  83. * or less than some arbitrary number (maximum btree height).
  84. */
  85. bool
  86. xfs_ag_resv_critical(
  87. struct xfs_perag *pag,
  88. enum xfs_ag_resv_type type)
  89. {
  90. xfs_extlen_t avail;
  91. xfs_extlen_t orig;
  92. switch (type) {
  93. case XFS_AG_RESV_METADATA:
  94. avail = pag->pagf_freeblks - pag->pag_agfl_resv.ar_reserved;
  95. orig = pag->pag_meta_resv.ar_asked;
  96. break;
  97. case XFS_AG_RESV_AGFL:
  98. avail = pag->pagf_freeblks + pag->pagf_flcount -
  99. pag->pag_meta_resv.ar_reserved;
  100. orig = pag->pag_agfl_resv.ar_asked;
  101. break;
  102. default:
  103. ASSERT(0);
  104. return false;
  105. }
  106. trace_xfs_ag_resv_critical(pag, type, avail);
  107. /* Critically low if less than 10% or max btree height remains. */
  108. return XFS_TEST_ERROR(avail < orig / 10 || avail < XFS_BTREE_MAXLEVELS,
  109. pag->pag_mount, XFS_ERRTAG_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. if (pag->pag_agno == 0)
  148. pag->pag_mount->m_ag_max_usable += resv->ar_asked;
  149. /*
  150. * AGFL blocks are always considered "free", so whatever
  151. * was reserved at mount time must be given back at umount.
  152. */
  153. if (type == XFS_AG_RESV_AGFL)
  154. oldresv = resv->ar_orig_reserved;
  155. else
  156. oldresv = resv->ar_reserved;
  157. error = xfs_mod_fdblocks(pag->pag_mount, oldresv, true);
  158. resv->ar_reserved = 0;
  159. resv->ar_asked = 0;
  160. if (error)
  161. trace_xfs_ag_resv_free_error(pag->pag_mount, pag->pag_agno,
  162. error, _RET_IP_);
  163. return error;
  164. }
  165. /* Free a per-AG reservation. */
  166. int
  167. xfs_ag_resv_free(
  168. struct xfs_perag *pag)
  169. {
  170. int error;
  171. int err2;
  172. error = __xfs_ag_resv_free(pag, XFS_AG_RESV_AGFL);
  173. err2 = __xfs_ag_resv_free(pag, XFS_AG_RESV_METADATA);
  174. if (err2 && !error)
  175. error = err2;
  176. return error;
  177. }
  178. static int
  179. __xfs_ag_resv_init(
  180. struct xfs_perag *pag,
  181. enum xfs_ag_resv_type type,
  182. xfs_extlen_t ask,
  183. xfs_extlen_t used)
  184. {
  185. struct xfs_mount *mp = pag->pag_mount;
  186. struct xfs_ag_resv *resv;
  187. int error;
  188. xfs_extlen_t reserved;
  189. if (used > ask)
  190. ask = used;
  191. reserved = ask - used;
  192. error = xfs_mod_fdblocks(mp, -(int64_t)reserved, true);
  193. if (error) {
  194. trace_xfs_ag_resv_init_error(pag->pag_mount, pag->pag_agno,
  195. error, _RET_IP_);
  196. xfs_warn(mp,
  197. "Per-AG reservation for AG %u failed. Filesystem may run out of space.",
  198. pag->pag_agno);
  199. return error;
  200. }
  201. /*
  202. * Reduce the maximum per-AG allocation length by however much we're
  203. * trying to reserve for an AG. Since this is a filesystem-wide
  204. * counter, we only make the adjustment for AG 0. This assumes that
  205. * there aren't any AGs hungrier for per-AG reservation than AG 0.
  206. */
  207. if (pag->pag_agno == 0)
  208. mp->m_ag_max_usable -= ask;
  209. resv = xfs_perag_resv(pag, type);
  210. resv->ar_asked = ask;
  211. resv->ar_reserved = resv->ar_orig_reserved = reserved;
  212. trace_xfs_ag_resv_init(pag, type, ask);
  213. return 0;
  214. }
  215. /* Create a per-AG block reservation. */
  216. int
  217. xfs_ag_resv_init(
  218. struct xfs_perag *pag)
  219. {
  220. struct xfs_mount *mp = pag->pag_mount;
  221. xfs_agnumber_t agno = pag->pag_agno;
  222. xfs_extlen_t ask;
  223. xfs_extlen_t used;
  224. int error = 0;
  225. /* Create the metadata reservation. */
  226. if (pag->pag_meta_resv.ar_asked == 0) {
  227. ask = used = 0;
  228. error = xfs_refcountbt_calc_reserves(mp, agno, &ask, &used);
  229. if (error)
  230. goto out;
  231. error = xfs_finobt_calc_reserves(mp, agno, &ask, &used);
  232. if (error)
  233. goto out;
  234. error = __xfs_ag_resv_init(pag, XFS_AG_RESV_METADATA,
  235. ask, used);
  236. if (error) {
  237. /*
  238. * Because we didn't have per-AG reservations when the
  239. * finobt feature was added we might not be able to
  240. * reserve all needed blocks. Warn and fall back to the
  241. * old and potentially buggy code in that case, but
  242. * ensure we do have the reservation for the refcountbt.
  243. */
  244. ask = used = 0;
  245. mp->m_inotbt_nores = true;
  246. error = xfs_refcountbt_calc_reserves(mp, agno, &ask,
  247. &used);
  248. if (error)
  249. goto out;
  250. error = __xfs_ag_resv_init(pag, XFS_AG_RESV_METADATA,
  251. ask, used);
  252. if (error)
  253. goto out;
  254. }
  255. }
  256. /* Create the AGFL metadata reservation */
  257. if (pag->pag_agfl_resv.ar_asked == 0) {
  258. ask = used = 0;
  259. error = xfs_rmapbt_calc_reserves(mp, agno, &ask, &used);
  260. if (error)
  261. goto out;
  262. error = __xfs_ag_resv_init(pag, XFS_AG_RESV_AGFL, ask, used);
  263. if (error)
  264. goto out;
  265. }
  266. #ifdef DEBUG
  267. /* need to read in the AGF for the ASSERT below to work */
  268. error = xfs_alloc_pagf_init(pag->pag_mount, NULL, pag->pag_agno, 0);
  269. if (error)
  270. return error;
  271. ASSERT(xfs_perag_resv(pag, XFS_AG_RESV_METADATA)->ar_reserved +
  272. xfs_perag_resv(pag, XFS_AG_RESV_AGFL)->ar_reserved <=
  273. pag->pagf_freeblks + pag->pagf_flcount);
  274. #endif
  275. out:
  276. return error;
  277. }
  278. /* Allocate a block from the reservation. */
  279. void
  280. xfs_ag_resv_alloc_extent(
  281. struct xfs_perag *pag,
  282. enum xfs_ag_resv_type type,
  283. struct xfs_alloc_arg *args)
  284. {
  285. struct xfs_ag_resv *resv;
  286. xfs_extlen_t len;
  287. uint field;
  288. trace_xfs_ag_resv_alloc_extent(pag, type, args->len);
  289. switch (type) {
  290. case XFS_AG_RESV_METADATA:
  291. case XFS_AG_RESV_AGFL:
  292. resv = xfs_perag_resv(pag, type);
  293. break;
  294. default:
  295. ASSERT(0);
  296. /* fall through */
  297. case XFS_AG_RESV_NONE:
  298. field = args->wasdel ? XFS_TRANS_SB_RES_FDBLOCKS :
  299. XFS_TRANS_SB_FDBLOCKS;
  300. xfs_trans_mod_sb(args->tp, field, -(int64_t)args->len);
  301. return;
  302. }
  303. len = min_t(xfs_extlen_t, args->len, resv->ar_reserved);
  304. resv->ar_reserved -= len;
  305. if (type == XFS_AG_RESV_AGFL)
  306. return;
  307. /* Allocations of reserved blocks only need on-disk sb updates... */
  308. xfs_trans_mod_sb(args->tp, XFS_TRANS_SB_RES_FDBLOCKS, -(int64_t)len);
  309. /* ...but non-reserved blocks need in-core and on-disk updates. */
  310. if (args->len > len)
  311. xfs_trans_mod_sb(args->tp, XFS_TRANS_SB_FDBLOCKS,
  312. -((int64_t)args->len - len));
  313. }
  314. /* Free a block to the reservation. */
  315. void
  316. xfs_ag_resv_free_extent(
  317. struct xfs_perag *pag,
  318. enum xfs_ag_resv_type type,
  319. struct xfs_trans *tp,
  320. xfs_extlen_t len)
  321. {
  322. xfs_extlen_t leftover;
  323. struct xfs_ag_resv *resv;
  324. trace_xfs_ag_resv_free_extent(pag, type, len);
  325. switch (type) {
  326. case XFS_AG_RESV_METADATA:
  327. case XFS_AG_RESV_AGFL:
  328. resv = xfs_perag_resv(pag, type);
  329. break;
  330. default:
  331. ASSERT(0);
  332. /* fall through */
  333. case XFS_AG_RESV_NONE:
  334. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (int64_t)len);
  335. return;
  336. }
  337. leftover = min_t(xfs_extlen_t, len, resv->ar_asked - resv->ar_reserved);
  338. resv->ar_reserved += leftover;
  339. if (type == XFS_AG_RESV_AGFL)
  340. return;
  341. /* Freeing into the reserved pool only requires on-disk update... */
  342. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FDBLOCKS, len);
  343. /* ...but freeing beyond that requires in-core and on-disk update. */
  344. if (len > leftover)
  345. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, len - leftover);
  346. }