xfs_ag.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * Copyright (c) 2018 Red Hat, Inc.
  5. * All rights reserved.
  6. */
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_sb.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_btree.h"
  15. #include "xfs_alloc_btree.h"
  16. #include "xfs_rmap_btree.h"
  17. #include "xfs_alloc.h"
  18. #include "xfs_ialloc.h"
  19. #include "xfs_rmap.h"
  20. #include "xfs_ag.h"
  21. static struct xfs_buf *
  22. xfs_get_aghdr_buf(
  23. struct xfs_mount *mp,
  24. xfs_daddr_t blkno,
  25. size_t numblks,
  26. int flags,
  27. const struct xfs_buf_ops *ops)
  28. {
  29. struct xfs_buf *bp;
  30. bp = xfs_buf_get_uncached(mp->m_ddev_targp, numblks, flags);
  31. if (!bp)
  32. return NULL;
  33. xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
  34. bp->b_bn = blkno;
  35. bp->b_maps[0].bm_bn = blkno;
  36. bp->b_ops = ops;
  37. return bp;
  38. }
  39. /*
  40. * Generic btree root block init function
  41. */
  42. static void
  43. xfs_btroot_init(
  44. struct xfs_mount *mp,
  45. struct xfs_buf *bp,
  46. struct aghdr_init_data *id)
  47. {
  48. xfs_btree_init_block(mp, bp, id->type, 0, 0, id->agno, 0);
  49. }
  50. /*
  51. * Alloc btree root block init functions
  52. */
  53. static void
  54. xfs_bnoroot_init(
  55. struct xfs_mount *mp,
  56. struct xfs_buf *bp,
  57. struct aghdr_init_data *id)
  58. {
  59. struct xfs_alloc_rec *arec;
  60. xfs_btree_init_block(mp, bp, XFS_BTNUM_BNO, 0, 1, id->agno, 0);
  61. arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
  62. arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
  63. arec->ar_blockcount = cpu_to_be32(id->agsize -
  64. be32_to_cpu(arec->ar_startblock));
  65. }
  66. static void
  67. xfs_cntroot_init(
  68. struct xfs_mount *mp,
  69. struct xfs_buf *bp,
  70. struct aghdr_init_data *id)
  71. {
  72. struct xfs_alloc_rec *arec;
  73. xfs_btree_init_block(mp, bp, XFS_BTNUM_CNT, 0, 1, id->agno, 0);
  74. arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
  75. arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
  76. arec->ar_blockcount = cpu_to_be32(id->agsize -
  77. be32_to_cpu(arec->ar_startblock));
  78. }
  79. /*
  80. * Reverse map root block init
  81. */
  82. static void
  83. xfs_rmaproot_init(
  84. struct xfs_mount *mp,
  85. struct xfs_buf *bp,
  86. struct aghdr_init_data *id)
  87. {
  88. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  89. struct xfs_rmap_rec *rrec;
  90. xfs_btree_init_block(mp, bp, XFS_BTNUM_RMAP, 0, 4, id->agno, 0);
  91. /*
  92. * mark the AG header regions as static metadata The BNO
  93. * btree block is the first block after the headers, so
  94. * it's location defines the size of region the static
  95. * metadata consumes.
  96. *
  97. * Note: unlike mkfs, we never have to account for log
  98. * space when growing the data regions
  99. */
  100. rrec = XFS_RMAP_REC_ADDR(block, 1);
  101. rrec->rm_startblock = 0;
  102. rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp));
  103. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS);
  104. rrec->rm_offset = 0;
  105. /* account freespace btree root blocks */
  106. rrec = XFS_RMAP_REC_ADDR(block, 2);
  107. rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp));
  108. rrec->rm_blockcount = cpu_to_be32(2);
  109. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
  110. rrec->rm_offset = 0;
  111. /* account inode btree root blocks */
  112. rrec = XFS_RMAP_REC_ADDR(block, 3);
  113. rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp));
  114. rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) -
  115. XFS_IBT_BLOCK(mp));
  116. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT);
  117. rrec->rm_offset = 0;
  118. /* account for rmap btree root */
  119. rrec = XFS_RMAP_REC_ADDR(block, 4);
  120. rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp));
  121. rrec->rm_blockcount = cpu_to_be32(1);
  122. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
  123. rrec->rm_offset = 0;
  124. /* account for refc btree root */
  125. if (xfs_sb_version_hasreflink(&mp->m_sb)) {
  126. rrec = XFS_RMAP_REC_ADDR(block, 5);
  127. rrec->rm_startblock = cpu_to_be32(xfs_refc_block(mp));
  128. rrec->rm_blockcount = cpu_to_be32(1);
  129. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC);
  130. rrec->rm_offset = 0;
  131. be16_add_cpu(&block->bb_numrecs, 1);
  132. }
  133. }
  134. /*
  135. * Initialise new secondary superblocks with the pre-grow geometry, but mark
  136. * them as "in progress" so we know they haven't yet been activated. This will
  137. * get cleared when the update with the new geometry information is done after
  138. * changes to the primary are committed. This isn't strictly necessary, but we
  139. * get it for free with the delayed buffer write lists and it means we can tell
  140. * if a grow operation didn't complete properly after the fact.
  141. */
  142. static void
  143. xfs_sbblock_init(
  144. struct xfs_mount *mp,
  145. struct xfs_buf *bp,
  146. struct aghdr_init_data *id)
  147. {
  148. struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
  149. xfs_sb_to_disk(dsb, &mp->m_sb);
  150. dsb->sb_inprogress = 1;
  151. }
  152. static void
  153. xfs_agfblock_init(
  154. struct xfs_mount *mp,
  155. struct xfs_buf *bp,
  156. struct aghdr_init_data *id)
  157. {
  158. struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
  159. xfs_extlen_t tmpsize;
  160. agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
  161. agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
  162. agf->agf_seqno = cpu_to_be32(id->agno);
  163. agf->agf_length = cpu_to_be32(id->agsize);
  164. agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp));
  165. agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp));
  166. agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1);
  167. agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1);
  168. if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
  169. agf->agf_roots[XFS_BTNUM_RMAPi] =
  170. cpu_to_be32(XFS_RMAP_BLOCK(mp));
  171. agf->agf_levels[XFS_BTNUM_RMAPi] = cpu_to_be32(1);
  172. agf->agf_rmap_blocks = cpu_to_be32(1);
  173. }
  174. agf->agf_flfirst = cpu_to_be32(1);
  175. agf->agf_fllast = 0;
  176. agf->agf_flcount = 0;
  177. tmpsize = id->agsize - mp->m_ag_prealloc_blocks;
  178. agf->agf_freeblks = cpu_to_be32(tmpsize);
  179. agf->agf_longest = cpu_to_be32(tmpsize);
  180. if (xfs_sb_version_hascrc(&mp->m_sb))
  181. uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
  182. if (xfs_sb_version_hasreflink(&mp->m_sb)) {
  183. agf->agf_refcount_root = cpu_to_be32(
  184. xfs_refc_block(mp));
  185. agf->agf_refcount_level = cpu_to_be32(1);
  186. agf->agf_refcount_blocks = cpu_to_be32(1);
  187. }
  188. }
  189. static void
  190. xfs_agflblock_init(
  191. struct xfs_mount *mp,
  192. struct xfs_buf *bp,
  193. struct aghdr_init_data *id)
  194. {
  195. struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
  196. __be32 *agfl_bno;
  197. int bucket;
  198. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  199. agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
  200. agfl->agfl_seqno = cpu_to_be32(id->agno);
  201. uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
  202. }
  203. agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, bp);
  204. for (bucket = 0; bucket < xfs_agfl_size(mp); bucket++)
  205. agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK);
  206. }
  207. static void
  208. xfs_agiblock_init(
  209. struct xfs_mount *mp,
  210. struct xfs_buf *bp,
  211. struct aghdr_init_data *id)
  212. {
  213. struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
  214. int bucket;
  215. agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
  216. agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
  217. agi->agi_seqno = cpu_to_be32(id->agno);
  218. agi->agi_length = cpu_to_be32(id->agsize);
  219. agi->agi_count = 0;
  220. agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
  221. agi->agi_level = cpu_to_be32(1);
  222. agi->agi_freecount = 0;
  223. agi->agi_newino = cpu_to_be32(NULLAGINO);
  224. agi->agi_dirino = cpu_to_be32(NULLAGINO);
  225. if (xfs_sb_version_hascrc(&mp->m_sb))
  226. uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
  227. if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
  228. agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp));
  229. agi->agi_free_level = cpu_to_be32(1);
  230. }
  231. for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
  232. agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
  233. }
  234. typedef void (*aghdr_init_work_f)(struct xfs_mount *mp, struct xfs_buf *bp,
  235. struct aghdr_init_data *id);
  236. static int
  237. xfs_ag_init_hdr(
  238. struct xfs_mount *mp,
  239. struct aghdr_init_data *id,
  240. aghdr_init_work_f work,
  241. const struct xfs_buf_ops *ops)
  242. {
  243. struct xfs_buf *bp;
  244. bp = xfs_get_aghdr_buf(mp, id->daddr, id->numblks, 0, ops);
  245. if (!bp)
  246. return -ENOMEM;
  247. (*work)(mp, bp, id);
  248. xfs_buf_delwri_queue(bp, &id->buffer_list);
  249. xfs_buf_relse(bp);
  250. return 0;
  251. }
  252. struct xfs_aghdr_grow_data {
  253. xfs_daddr_t daddr;
  254. size_t numblks;
  255. const struct xfs_buf_ops *ops;
  256. aghdr_init_work_f work;
  257. xfs_btnum_t type;
  258. bool need_init;
  259. };
  260. /*
  261. * Prepare new AG headers to be written to disk. We use uncached buffers here,
  262. * as it is assumed these new AG headers are currently beyond the currently
  263. * valid filesystem address space. Using cached buffers would trip over EOFS
  264. * corruption detection alogrithms in the buffer cache lookup routines.
  265. *
  266. * This is a non-transactional function, but the prepared buffers are added to a
  267. * delayed write buffer list supplied by the caller so they can submit them to
  268. * disk and wait on them as required.
  269. */
  270. int
  271. xfs_ag_init_headers(
  272. struct xfs_mount *mp,
  273. struct aghdr_init_data *id)
  274. {
  275. struct xfs_aghdr_grow_data aghdr_data[] = {
  276. { /* SB */
  277. .daddr = XFS_AG_DADDR(mp, id->agno, XFS_SB_DADDR),
  278. .numblks = XFS_FSS_TO_BB(mp, 1),
  279. .ops = &xfs_sb_buf_ops,
  280. .work = &xfs_sbblock_init,
  281. .need_init = true
  282. },
  283. { /* AGF */
  284. .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGF_DADDR(mp)),
  285. .numblks = XFS_FSS_TO_BB(mp, 1),
  286. .ops = &xfs_agf_buf_ops,
  287. .work = &xfs_agfblock_init,
  288. .need_init = true
  289. },
  290. { /* AGFL */
  291. .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGFL_DADDR(mp)),
  292. .numblks = XFS_FSS_TO_BB(mp, 1),
  293. .ops = &xfs_agfl_buf_ops,
  294. .work = &xfs_agflblock_init,
  295. .need_init = true
  296. },
  297. { /* AGI */
  298. .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGI_DADDR(mp)),
  299. .numblks = XFS_FSS_TO_BB(mp, 1),
  300. .ops = &xfs_agi_buf_ops,
  301. .work = &xfs_agiblock_init,
  302. .need_init = true
  303. },
  304. { /* BNO root block */
  305. .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_BNO_BLOCK(mp)),
  306. .numblks = BTOBB(mp->m_sb.sb_blocksize),
  307. .ops = &xfs_allocbt_buf_ops,
  308. .work = &xfs_bnoroot_init,
  309. .need_init = true
  310. },
  311. { /* CNT root block */
  312. .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_CNT_BLOCK(mp)),
  313. .numblks = BTOBB(mp->m_sb.sb_blocksize),
  314. .ops = &xfs_allocbt_buf_ops,
  315. .work = &xfs_cntroot_init,
  316. .need_init = true
  317. },
  318. { /* INO root block */
  319. .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_IBT_BLOCK(mp)),
  320. .numblks = BTOBB(mp->m_sb.sb_blocksize),
  321. .ops = &xfs_inobt_buf_ops,
  322. .work = &xfs_btroot_init,
  323. .type = XFS_BTNUM_INO,
  324. .need_init = true
  325. },
  326. { /* FINO root block */
  327. .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_FIBT_BLOCK(mp)),
  328. .numblks = BTOBB(mp->m_sb.sb_blocksize),
  329. .ops = &xfs_inobt_buf_ops,
  330. .work = &xfs_btroot_init,
  331. .type = XFS_BTNUM_FINO,
  332. .need_init = xfs_sb_version_hasfinobt(&mp->m_sb)
  333. },
  334. { /* RMAP root block */
  335. .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_RMAP_BLOCK(mp)),
  336. .numblks = BTOBB(mp->m_sb.sb_blocksize),
  337. .ops = &xfs_rmapbt_buf_ops,
  338. .work = &xfs_rmaproot_init,
  339. .need_init = xfs_sb_version_hasrmapbt(&mp->m_sb)
  340. },
  341. { /* REFC root block */
  342. .daddr = XFS_AGB_TO_DADDR(mp, id->agno, xfs_refc_block(mp)),
  343. .numblks = BTOBB(mp->m_sb.sb_blocksize),
  344. .ops = &xfs_refcountbt_buf_ops,
  345. .work = &xfs_btroot_init,
  346. .type = XFS_BTNUM_REFC,
  347. .need_init = xfs_sb_version_hasreflink(&mp->m_sb)
  348. },
  349. { /* NULL terminating block */
  350. .daddr = XFS_BUF_DADDR_NULL,
  351. }
  352. };
  353. struct xfs_aghdr_grow_data *dp;
  354. int error = 0;
  355. /* Account for AG free space in new AG */
  356. id->nfree += id->agsize - mp->m_ag_prealloc_blocks;
  357. for (dp = &aghdr_data[0]; dp->daddr != XFS_BUF_DADDR_NULL; dp++) {
  358. if (!dp->need_init)
  359. continue;
  360. id->daddr = dp->daddr;
  361. id->numblks = dp->numblks;
  362. id->type = dp->type;
  363. error = xfs_ag_init_hdr(mp, id, dp->work, dp->ops);
  364. if (error)
  365. break;
  366. }
  367. return error;
  368. }
  369. /*
  370. * Extent the AG indicated by the @id by the length passed in
  371. */
  372. int
  373. xfs_ag_extend_space(
  374. struct xfs_mount *mp,
  375. struct xfs_trans *tp,
  376. struct aghdr_init_data *id,
  377. xfs_extlen_t len)
  378. {
  379. struct xfs_owner_info oinfo;
  380. struct xfs_buf *bp;
  381. struct xfs_agi *agi;
  382. struct xfs_agf *agf;
  383. int error;
  384. /*
  385. * Change the agi length.
  386. */
  387. error = xfs_ialloc_read_agi(mp, tp, id->agno, &bp);
  388. if (error)
  389. return error;
  390. agi = XFS_BUF_TO_AGI(bp);
  391. be32_add_cpu(&agi->agi_length, len);
  392. ASSERT(id->agno == mp->m_sb.sb_agcount - 1 ||
  393. be32_to_cpu(agi->agi_length) == mp->m_sb.sb_agblocks);
  394. xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
  395. /*
  396. * Change agf length.
  397. */
  398. error = xfs_alloc_read_agf(mp, tp, id->agno, 0, &bp);
  399. if (error)
  400. return error;
  401. agf = XFS_BUF_TO_AGF(bp);
  402. be32_add_cpu(&agf->agf_length, len);
  403. ASSERT(agf->agf_length == agi->agi_length);
  404. xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH);
  405. /*
  406. * Free the new space.
  407. *
  408. * XFS_RMAP_OWN_NULL is used here to tell the rmap btree that
  409. * this doesn't actually exist in the rmap btree.
  410. */
  411. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_NULL);
  412. error = xfs_rmap_free(tp, bp, id->agno,
  413. be32_to_cpu(agf->agf_length) - len,
  414. len, &oinfo);
  415. if (error)
  416. return error;
  417. return xfs_free_extent(tp, XFS_AGB_TO_FSB(mp, id->agno,
  418. be32_to_cpu(agf->agf_length) - len),
  419. len, &oinfo, XFS_AG_RESV_NONE);
  420. }