xfs_ag.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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_bit.h"
  13. #include "xfs_sb.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_btree.h"
  16. #include "xfs_alloc_btree.h"
  17. #include "xfs_rmap_btree.h"
  18. #include "xfs_alloc.h"
  19. #include "xfs_ialloc.h"
  20. #include "xfs_rmap.h"
  21. #include "xfs_ag.h"
  22. #include "xfs_ag_resv.h"
  23. #include "xfs_health.h"
  24. #include "xfs_error.h"
  25. #include "xfs_bmap.h"
  26. #include "xfs_defer.h"
  27. #include "xfs_log_format.h"
  28. #include "xfs_trans.h"
  29. #include "xfs_trace.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_icache.h"
  32. /*
  33. * Passive reference counting access wrappers to the perag structures. If the
  34. * per-ag structure is to be freed, the freeing code is responsible for cleaning
  35. * up objects with passive references before freeing the structure. This is
  36. * things like cached buffers.
  37. */
  38. struct xfs_perag *
  39. xfs_perag_get(
  40. struct xfs_mount *mp,
  41. xfs_agnumber_t agno)
  42. {
  43. struct xfs_perag *pag;
  44. rcu_read_lock();
  45. pag = xa_load(&mp->m_perags, agno);
  46. if (pag) {
  47. trace_xfs_perag_get(pag, _RET_IP_);
  48. ASSERT(atomic_read(&pag->pag_ref) >= 0);
  49. atomic_inc(&pag->pag_ref);
  50. }
  51. rcu_read_unlock();
  52. return pag;
  53. }
  54. /* Get a passive reference to the given perag. */
  55. struct xfs_perag *
  56. xfs_perag_hold(
  57. struct xfs_perag *pag)
  58. {
  59. ASSERT(atomic_read(&pag->pag_ref) > 0 ||
  60. atomic_read(&pag->pag_active_ref) > 0);
  61. trace_xfs_perag_hold(pag, _RET_IP_);
  62. atomic_inc(&pag->pag_ref);
  63. return pag;
  64. }
  65. void
  66. xfs_perag_put(
  67. struct xfs_perag *pag)
  68. {
  69. trace_xfs_perag_put(pag, _RET_IP_);
  70. ASSERT(atomic_read(&pag->pag_ref) > 0);
  71. atomic_dec(&pag->pag_ref);
  72. }
  73. /*
  74. * Active references for perag structures. This is for short term access to the
  75. * per ag structures for walking trees or accessing state. If an AG is being
  76. * shrunk or is offline, then this will fail to find that AG and return NULL
  77. * instead.
  78. */
  79. struct xfs_perag *
  80. xfs_perag_grab(
  81. struct xfs_mount *mp,
  82. xfs_agnumber_t agno)
  83. {
  84. struct xfs_perag *pag;
  85. rcu_read_lock();
  86. pag = xa_load(&mp->m_perags, agno);
  87. if (pag) {
  88. trace_xfs_perag_grab(pag, _RET_IP_);
  89. if (!atomic_inc_not_zero(&pag->pag_active_ref))
  90. pag = NULL;
  91. }
  92. rcu_read_unlock();
  93. return pag;
  94. }
  95. void
  96. xfs_perag_rele(
  97. struct xfs_perag *pag)
  98. {
  99. trace_xfs_perag_rele(pag, _RET_IP_);
  100. if (atomic_dec_and_test(&pag->pag_active_ref))
  101. wake_up(&pag->pag_active_wq);
  102. }
  103. /*
  104. * xfs_initialize_perag_data
  105. *
  106. * Read in each per-ag structure so we can count up the number of
  107. * allocated inodes, free inodes and used filesystem blocks as this
  108. * information is no longer persistent in the superblock. Once we have
  109. * this information, write it into the in-core superblock structure.
  110. */
  111. int
  112. xfs_initialize_perag_data(
  113. struct xfs_mount *mp,
  114. xfs_agnumber_t agcount)
  115. {
  116. xfs_agnumber_t index;
  117. struct xfs_perag *pag;
  118. struct xfs_sb *sbp = &mp->m_sb;
  119. uint64_t ifree = 0;
  120. uint64_t ialloc = 0;
  121. uint64_t bfree = 0;
  122. uint64_t bfreelst = 0;
  123. uint64_t btree = 0;
  124. uint64_t fdblocks;
  125. int error = 0;
  126. for (index = 0; index < agcount; index++) {
  127. /*
  128. * Read the AGF and AGI buffers to populate the per-ag
  129. * structures for us.
  130. */
  131. pag = xfs_perag_get(mp, index);
  132. error = xfs_alloc_read_agf(pag, NULL, 0, NULL);
  133. if (!error)
  134. error = xfs_ialloc_read_agi(pag, NULL, 0, NULL);
  135. if (error) {
  136. xfs_perag_put(pag);
  137. return error;
  138. }
  139. ifree += pag->pagi_freecount;
  140. ialloc += pag->pagi_count;
  141. bfree += pag->pagf_freeblks;
  142. bfreelst += pag->pagf_flcount;
  143. btree += pag->pagf_btreeblks;
  144. xfs_perag_put(pag);
  145. }
  146. fdblocks = bfree + bfreelst + btree;
  147. /*
  148. * If the new summary counts are obviously incorrect, fail the
  149. * mount operation because that implies the AGFs are also corrupt.
  150. * Clear FS_COUNTERS so that we don't unmount with a dirty log, which
  151. * will prevent xfs_repair from fixing anything.
  152. */
  153. if (fdblocks > sbp->sb_dblocks || ifree > ialloc) {
  154. xfs_alert(mp, "AGF corruption. Please run xfs_repair.");
  155. xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
  156. error = -EFSCORRUPTED;
  157. goto out;
  158. }
  159. /* Overwrite incore superblock counters with just-read data */
  160. spin_lock(&mp->m_sb_lock);
  161. sbp->sb_ifree = ifree;
  162. sbp->sb_icount = ialloc;
  163. sbp->sb_fdblocks = fdblocks;
  164. spin_unlock(&mp->m_sb_lock);
  165. xfs_reinit_percpu_counters(mp);
  166. out:
  167. xfs_fs_mark_healthy(mp, XFS_SICK_FS_COUNTERS);
  168. return error;
  169. }
  170. /*
  171. * Free up the per-ag resources within the specified AG range.
  172. */
  173. void
  174. xfs_free_perag_range(
  175. struct xfs_mount *mp,
  176. xfs_agnumber_t first_agno,
  177. xfs_agnumber_t end_agno)
  178. {
  179. xfs_agnumber_t agno;
  180. for (agno = first_agno; agno < end_agno; agno++) {
  181. struct xfs_perag *pag = xa_erase(&mp->m_perags, agno);
  182. ASSERT(pag);
  183. XFS_IS_CORRUPT(pag->pag_mount, atomic_read(&pag->pag_ref) != 0);
  184. xfs_defer_drain_free(&pag->pag_intents_drain);
  185. cancel_delayed_work_sync(&pag->pag_blockgc_work);
  186. xfs_buf_cache_destroy(&pag->pag_bcache);
  187. /* drop the mount's active reference */
  188. xfs_perag_rele(pag);
  189. XFS_IS_CORRUPT(pag->pag_mount,
  190. atomic_read(&pag->pag_active_ref) != 0);
  191. kfree_rcu_mightsleep(pag);
  192. }
  193. }
  194. /* Find the size of the AG, in blocks. */
  195. static xfs_agblock_t
  196. __xfs_ag_block_count(
  197. struct xfs_mount *mp,
  198. xfs_agnumber_t agno,
  199. xfs_agnumber_t agcount,
  200. xfs_rfsblock_t dblocks)
  201. {
  202. ASSERT(agno < agcount);
  203. if (agno < agcount - 1)
  204. return mp->m_sb.sb_agblocks;
  205. return dblocks - (agno * mp->m_sb.sb_agblocks);
  206. }
  207. xfs_agblock_t
  208. xfs_ag_block_count(
  209. struct xfs_mount *mp,
  210. xfs_agnumber_t agno)
  211. {
  212. return __xfs_ag_block_count(mp, agno, mp->m_sb.sb_agcount,
  213. mp->m_sb.sb_dblocks);
  214. }
  215. /* Calculate the first and last possible inode number in an AG. */
  216. static void
  217. __xfs_agino_range(
  218. struct xfs_mount *mp,
  219. xfs_agblock_t eoag,
  220. xfs_agino_t *first,
  221. xfs_agino_t *last)
  222. {
  223. xfs_agblock_t bno;
  224. /*
  225. * Calculate the first inode, which will be in the first
  226. * cluster-aligned block after the AGFL.
  227. */
  228. bno = round_up(XFS_AGFL_BLOCK(mp) + 1, M_IGEO(mp)->cluster_align);
  229. *first = XFS_AGB_TO_AGINO(mp, bno);
  230. /*
  231. * Calculate the last inode, which will be at the end of the
  232. * last (aligned) cluster that can be allocated in the AG.
  233. */
  234. bno = round_down(eoag, M_IGEO(mp)->cluster_align);
  235. *last = XFS_AGB_TO_AGINO(mp, bno) - 1;
  236. }
  237. void
  238. xfs_agino_range(
  239. struct xfs_mount *mp,
  240. xfs_agnumber_t agno,
  241. xfs_agino_t *first,
  242. xfs_agino_t *last)
  243. {
  244. return __xfs_agino_range(mp, xfs_ag_block_count(mp, agno), first, last);
  245. }
  246. int
  247. xfs_update_last_ag_size(
  248. struct xfs_mount *mp,
  249. xfs_agnumber_t prev_agcount)
  250. {
  251. struct xfs_perag *pag = xfs_perag_grab(mp, prev_agcount - 1);
  252. if (!pag)
  253. return -EFSCORRUPTED;
  254. pag->block_count = __xfs_ag_block_count(mp, prev_agcount - 1,
  255. mp->m_sb.sb_agcount, mp->m_sb.sb_dblocks);
  256. __xfs_agino_range(mp, pag->block_count, &pag->agino_min,
  257. &pag->agino_max);
  258. xfs_perag_rele(pag);
  259. return 0;
  260. }
  261. int
  262. xfs_initialize_perag(
  263. struct xfs_mount *mp,
  264. xfs_agnumber_t old_agcount,
  265. xfs_agnumber_t new_agcount,
  266. xfs_rfsblock_t dblocks,
  267. xfs_agnumber_t *maxagi)
  268. {
  269. struct xfs_perag *pag;
  270. xfs_agnumber_t index;
  271. int error;
  272. for (index = old_agcount; index < new_agcount; index++) {
  273. pag = kzalloc(sizeof(*pag), GFP_KERNEL);
  274. if (!pag) {
  275. error = -ENOMEM;
  276. goto out_unwind_new_pags;
  277. }
  278. pag->pag_agno = index;
  279. pag->pag_mount = mp;
  280. error = xa_insert(&mp->m_perags, index, pag, GFP_KERNEL);
  281. if (error) {
  282. WARN_ON_ONCE(error == -EBUSY);
  283. goto out_free_pag;
  284. }
  285. #ifdef __KERNEL__
  286. /* Place kernel structure only init below this point. */
  287. spin_lock_init(&pag->pag_ici_lock);
  288. spin_lock_init(&pag->pagb_lock);
  289. spin_lock_init(&pag->pag_state_lock);
  290. INIT_DELAYED_WORK(&pag->pag_blockgc_work, xfs_blockgc_worker);
  291. INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
  292. xfs_defer_drain_init(&pag->pag_intents_drain);
  293. init_waitqueue_head(&pag->pagb_wait);
  294. init_waitqueue_head(&pag->pag_active_wq);
  295. pag->pagb_count = 0;
  296. pag->pagb_tree = RB_ROOT;
  297. xfs_hooks_init(&pag->pag_rmap_update_hooks);
  298. #endif /* __KERNEL__ */
  299. error = xfs_buf_cache_init(&pag->pag_bcache);
  300. if (error)
  301. goto out_remove_pag;
  302. /* Active ref owned by mount indicates AG is online. */
  303. atomic_set(&pag->pag_active_ref, 1);
  304. /*
  305. * Pre-calculated geometry
  306. */
  307. pag->block_count = __xfs_ag_block_count(mp, index, new_agcount,
  308. dblocks);
  309. pag->min_block = XFS_AGFL_BLOCK(mp);
  310. __xfs_agino_range(mp, pag->block_count, &pag->agino_min,
  311. &pag->agino_max);
  312. }
  313. index = xfs_set_inode_alloc(mp, new_agcount);
  314. if (maxagi)
  315. *maxagi = index;
  316. mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
  317. return 0;
  318. out_remove_pag:
  319. xfs_defer_drain_free(&pag->pag_intents_drain);
  320. pag = xa_erase(&mp->m_perags, index);
  321. out_free_pag:
  322. kfree(pag);
  323. out_unwind_new_pags:
  324. xfs_free_perag_range(mp, old_agcount, index);
  325. return error;
  326. }
  327. static int
  328. xfs_get_aghdr_buf(
  329. struct xfs_mount *mp,
  330. xfs_daddr_t blkno,
  331. size_t numblks,
  332. struct xfs_buf **bpp,
  333. const struct xfs_buf_ops *ops)
  334. {
  335. struct xfs_buf *bp;
  336. int error;
  337. error = xfs_buf_get_uncached(mp->m_ddev_targp, numblks, 0, &bp);
  338. if (error)
  339. return error;
  340. bp->b_maps[0].bm_bn = blkno;
  341. bp->b_ops = ops;
  342. *bpp = bp;
  343. return 0;
  344. }
  345. /*
  346. * Generic btree root block init function
  347. */
  348. static void
  349. xfs_btroot_init(
  350. struct xfs_mount *mp,
  351. struct xfs_buf *bp,
  352. struct aghdr_init_data *id)
  353. {
  354. xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 0, id->agno);
  355. }
  356. /* Finish initializing a free space btree. */
  357. static void
  358. xfs_freesp_init_recs(
  359. struct xfs_mount *mp,
  360. struct xfs_buf *bp,
  361. struct aghdr_init_data *id)
  362. {
  363. struct xfs_alloc_rec *arec;
  364. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  365. arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
  366. arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
  367. if (xfs_ag_contains_log(mp, id->agno)) {
  368. struct xfs_alloc_rec *nrec;
  369. xfs_agblock_t start = XFS_FSB_TO_AGBNO(mp,
  370. mp->m_sb.sb_logstart);
  371. ASSERT(start >= mp->m_ag_prealloc_blocks);
  372. if (start != mp->m_ag_prealloc_blocks) {
  373. /*
  374. * Modify first record to pad stripe align of log and
  375. * bump the record count.
  376. */
  377. arec->ar_blockcount = cpu_to_be32(start -
  378. mp->m_ag_prealloc_blocks);
  379. be16_add_cpu(&block->bb_numrecs, 1);
  380. nrec = arec + 1;
  381. /*
  382. * Insert second record at start of internal log
  383. * which then gets trimmed.
  384. */
  385. nrec->ar_startblock = cpu_to_be32(
  386. be32_to_cpu(arec->ar_startblock) +
  387. be32_to_cpu(arec->ar_blockcount));
  388. arec = nrec;
  389. }
  390. /*
  391. * Change record start to after the internal log
  392. */
  393. be32_add_cpu(&arec->ar_startblock, mp->m_sb.sb_logblocks);
  394. }
  395. /*
  396. * Calculate the block count of this record; if it is nonzero,
  397. * increment the record count.
  398. */
  399. arec->ar_blockcount = cpu_to_be32(id->agsize -
  400. be32_to_cpu(arec->ar_startblock));
  401. if (arec->ar_blockcount)
  402. be16_add_cpu(&block->bb_numrecs, 1);
  403. }
  404. /*
  405. * bnobt/cntbt btree root block init functions
  406. */
  407. static void
  408. xfs_bnoroot_init(
  409. struct xfs_mount *mp,
  410. struct xfs_buf *bp,
  411. struct aghdr_init_data *id)
  412. {
  413. xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 0, id->agno);
  414. xfs_freesp_init_recs(mp, bp, id);
  415. }
  416. /*
  417. * Reverse map root block init
  418. */
  419. static void
  420. xfs_rmaproot_init(
  421. struct xfs_mount *mp,
  422. struct xfs_buf *bp,
  423. struct aghdr_init_data *id)
  424. {
  425. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  426. struct xfs_rmap_rec *rrec;
  427. xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 4, id->agno);
  428. /*
  429. * mark the AG header regions as static metadata The BNO
  430. * btree block is the first block after the headers, so
  431. * it's location defines the size of region the static
  432. * metadata consumes.
  433. *
  434. * Note: unlike mkfs, we never have to account for log
  435. * space when growing the data regions
  436. */
  437. rrec = XFS_RMAP_REC_ADDR(block, 1);
  438. rrec->rm_startblock = 0;
  439. rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp));
  440. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS);
  441. rrec->rm_offset = 0;
  442. /* account freespace btree root blocks */
  443. rrec = XFS_RMAP_REC_ADDR(block, 2);
  444. rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp));
  445. rrec->rm_blockcount = cpu_to_be32(2);
  446. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
  447. rrec->rm_offset = 0;
  448. /* account inode btree root blocks */
  449. rrec = XFS_RMAP_REC_ADDR(block, 3);
  450. rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp));
  451. rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) -
  452. XFS_IBT_BLOCK(mp));
  453. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT);
  454. rrec->rm_offset = 0;
  455. /* account for rmap btree root */
  456. rrec = XFS_RMAP_REC_ADDR(block, 4);
  457. rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp));
  458. rrec->rm_blockcount = cpu_to_be32(1);
  459. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
  460. rrec->rm_offset = 0;
  461. /* account for refc btree root */
  462. if (xfs_has_reflink(mp)) {
  463. rrec = XFS_RMAP_REC_ADDR(block, 5);
  464. rrec->rm_startblock = cpu_to_be32(xfs_refc_block(mp));
  465. rrec->rm_blockcount = cpu_to_be32(1);
  466. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC);
  467. rrec->rm_offset = 0;
  468. be16_add_cpu(&block->bb_numrecs, 1);
  469. }
  470. /* account for the log space */
  471. if (xfs_ag_contains_log(mp, id->agno)) {
  472. rrec = XFS_RMAP_REC_ADDR(block,
  473. be16_to_cpu(block->bb_numrecs) + 1);
  474. rrec->rm_startblock = cpu_to_be32(
  475. XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart));
  476. rrec->rm_blockcount = cpu_to_be32(mp->m_sb.sb_logblocks);
  477. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_LOG);
  478. rrec->rm_offset = 0;
  479. be16_add_cpu(&block->bb_numrecs, 1);
  480. }
  481. }
  482. /*
  483. * Initialise new secondary superblocks with the pre-grow geometry, but mark
  484. * them as "in progress" so we know they haven't yet been activated. This will
  485. * get cleared when the update with the new geometry information is done after
  486. * changes to the primary are committed. This isn't strictly necessary, but we
  487. * get it for free with the delayed buffer write lists and it means we can tell
  488. * if a grow operation didn't complete properly after the fact.
  489. */
  490. static void
  491. xfs_sbblock_init(
  492. struct xfs_mount *mp,
  493. struct xfs_buf *bp,
  494. struct aghdr_init_data *id)
  495. {
  496. struct xfs_dsb *dsb = bp->b_addr;
  497. xfs_sb_to_disk(dsb, &mp->m_sb);
  498. dsb->sb_inprogress = 1;
  499. }
  500. static void
  501. xfs_agfblock_init(
  502. struct xfs_mount *mp,
  503. struct xfs_buf *bp,
  504. struct aghdr_init_data *id)
  505. {
  506. struct xfs_agf *agf = bp->b_addr;
  507. xfs_extlen_t tmpsize;
  508. agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
  509. agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
  510. agf->agf_seqno = cpu_to_be32(id->agno);
  511. agf->agf_length = cpu_to_be32(id->agsize);
  512. agf->agf_bno_root = cpu_to_be32(XFS_BNO_BLOCK(mp));
  513. agf->agf_cnt_root = cpu_to_be32(XFS_CNT_BLOCK(mp));
  514. agf->agf_bno_level = cpu_to_be32(1);
  515. agf->agf_cnt_level = cpu_to_be32(1);
  516. if (xfs_has_rmapbt(mp)) {
  517. agf->agf_rmap_root = cpu_to_be32(XFS_RMAP_BLOCK(mp));
  518. agf->agf_rmap_level = cpu_to_be32(1);
  519. agf->agf_rmap_blocks = cpu_to_be32(1);
  520. }
  521. agf->agf_flfirst = cpu_to_be32(1);
  522. agf->agf_fllast = 0;
  523. agf->agf_flcount = 0;
  524. tmpsize = id->agsize - mp->m_ag_prealloc_blocks;
  525. agf->agf_freeblks = cpu_to_be32(tmpsize);
  526. agf->agf_longest = cpu_to_be32(tmpsize);
  527. if (xfs_has_crc(mp))
  528. uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
  529. if (xfs_has_reflink(mp)) {
  530. agf->agf_refcount_root = cpu_to_be32(
  531. xfs_refc_block(mp));
  532. agf->agf_refcount_level = cpu_to_be32(1);
  533. agf->agf_refcount_blocks = cpu_to_be32(1);
  534. }
  535. if (xfs_ag_contains_log(mp, id->agno)) {
  536. int64_t logblocks = mp->m_sb.sb_logblocks;
  537. be32_add_cpu(&agf->agf_freeblks, -logblocks);
  538. agf->agf_longest = cpu_to_be32(id->agsize -
  539. XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart) - logblocks);
  540. }
  541. }
  542. static void
  543. xfs_agflblock_init(
  544. struct xfs_mount *mp,
  545. struct xfs_buf *bp,
  546. struct aghdr_init_data *id)
  547. {
  548. struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
  549. __be32 *agfl_bno;
  550. int bucket;
  551. if (xfs_has_crc(mp)) {
  552. agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
  553. agfl->agfl_seqno = cpu_to_be32(id->agno);
  554. uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
  555. }
  556. agfl_bno = xfs_buf_to_agfl_bno(bp);
  557. for (bucket = 0; bucket < xfs_agfl_size(mp); bucket++)
  558. agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK);
  559. }
  560. static void
  561. xfs_agiblock_init(
  562. struct xfs_mount *mp,
  563. struct xfs_buf *bp,
  564. struct aghdr_init_data *id)
  565. {
  566. struct xfs_agi *agi = bp->b_addr;
  567. int bucket;
  568. agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
  569. agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
  570. agi->agi_seqno = cpu_to_be32(id->agno);
  571. agi->agi_length = cpu_to_be32(id->agsize);
  572. agi->agi_count = 0;
  573. agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
  574. agi->agi_level = cpu_to_be32(1);
  575. agi->agi_freecount = 0;
  576. agi->agi_newino = cpu_to_be32(NULLAGINO);
  577. agi->agi_dirino = cpu_to_be32(NULLAGINO);
  578. if (xfs_has_crc(mp))
  579. uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
  580. if (xfs_has_finobt(mp)) {
  581. agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp));
  582. agi->agi_free_level = cpu_to_be32(1);
  583. }
  584. for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
  585. agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
  586. if (xfs_has_inobtcounts(mp)) {
  587. agi->agi_iblocks = cpu_to_be32(1);
  588. if (xfs_has_finobt(mp))
  589. agi->agi_fblocks = cpu_to_be32(1);
  590. }
  591. }
  592. typedef void (*aghdr_init_work_f)(struct xfs_mount *mp, struct xfs_buf *bp,
  593. struct aghdr_init_data *id);
  594. static int
  595. xfs_ag_init_hdr(
  596. struct xfs_mount *mp,
  597. struct aghdr_init_data *id,
  598. aghdr_init_work_f work,
  599. const struct xfs_buf_ops *ops)
  600. {
  601. struct xfs_buf *bp;
  602. int error;
  603. error = xfs_get_aghdr_buf(mp, id->daddr, id->numblks, &bp, ops);
  604. if (error)
  605. return error;
  606. (*work)(mp, bp, id);
  607. xfs_buf_delwri_queue(bp, &id->buffer_list);
  608. xfs_buf_relse(bp);
  609. return 0;
  610. }
  611. struct xfs_aghdr_grow_data {
  612. xfs_daddr_t daddr;
  613. size_t numblks;
  614. const struct xfs_buf_ops *ops;
  615. aghdr_init_work_f work;
  616. const struct xfs_btree_ops *bc_ops;
  617. bool need_init;
  618. };
  619. /*
  620. * Prepare new AG headers to be written to disk. We use uncached buffers here,
  621. * as it is assumed these new AG headers are currently beyond the currently
  622. * valid filesystem address space. Using cached buffers would trip over EOFS
  623. * corruption detection alogrithms in the buffer cache lookup routines.
  624. *
  625. * This is a non-transactional function, but the prepared buffers are added to a
  626. * delayed write buffer list supplied by the caller so they can submit them to
  627. * disk and wait on them as required.
  628. */
  629. int
  630. xfs_ag_init_headers(
  631. struct xfs_mount *mp,
  632. struct aghdr_init_data *id)
  633. {
  634. struct xfs_aghdr_grow_data aghdr_data[] = {
  635. { /* SB */
  636. .daddr = XFS_AG_DADDR(mp, id->agno, XFS_SB_DADDR),
  637. .numblks = XFS_FSS_TO_BB(mp, 1),
  638. .ops = &xfs_sb_buf_ops,
  639. .work = &xfs_sbblock_init,
  640. .need_init = true
  641. },
  642. { /* AGF */
  643. .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGF_DADDR(mp)),
  644. .numblks = XFS_FSS_TO_BB(mp, 1),
  645. .ops = &xfs_agf_buf_ops,
  646. .work = &xfs_agfblock_init,
  647. .need_init = true
  648. },
  649. { /* AGFL */
  650. .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGFL_DADDR(mp)),
  651. .numblks = XFS_FSS_TO_BB(mp, 1),
  652. .ops = &xfs_agfl_buf_ops,
  653. .work = &xfs_agflblock_init,
  654. .need_init = true
  655. },
  656. { /* AGI */
  657. .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGI_DADDR(mp)),
  658. .numblks = XFS_FSS_TO_BB(mp, 1),
  659. .ops = &xfs_agi_buf_ops,
  660. .work = &xfs_agiblock_init,
  661. .need_init = true
  662. },
  663. { /* BNO root block */
  664. .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_BNO_BLOCK(mp)),
  665. .numblks = BTOBB(mp->m_sb.sb_blocksize),
  666. .ops = &xfs_bnobt_buf_ops,
  667. .work = &xfs_bnoroot_init,
  668. .bc_ops = &xfs_bnobt_ops,
  669. .need_init = true
  670. },
  671. { /* CNT root block */
  672. .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_CNT_BLOCK(mp)),
  673. .numblks = BTOBB(mp->m_sb.sb_blocksize),
  674. .ops = &xfs_cntbt_buf_ops,
  675. .work = &xfs_bnoroot_init,
  676. .bc_ops = &xfs_cntbt_ops,
  677. .need_init = true
  678. },
  679. { /* INO root block */
  680. .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_IBT_BLOCK(mp)),
  681. .numblks = BTOBB(mp->m_sb.sb_blocksize),
  682. .ops = &xfs_inobt_buf_ops,
  683. .work = &xfs_btroot_init,
  684. .bc_ops = &xfs_inobt_ops,
  685. .need_init = true
  686. },
  687. { /* FINO root block */
  688. .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_FIBT_BLOCK(mp)),
  689. .numblks = BTOBB(mp->m_sb.sb_blocksize),
  690. .ops = &xfs_finobt_buf_ops,
  691. .work = &xfs_btroot_init,
  692. .bc_ops = &xfs_finobt_ops,
  693. .need_init = xfs_has_finobt(mp)
  694. },
  695. { /* RMAP root block */
  696. .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_RMAP_BLOCK(mp)),
  697. .numblks = BTOBB(mp->m_sb.sb_blocksize),
  698. .ops = &xfs_rmapbt_buf_ops,
  699. .work = &xfs_rmaproot_init,
  700. .bc_ops = &xfs_rmapbt_ops,
  701. .need_init = xfs_has_rmapbt(mp)
  702. },
  703. { /* REFC root block */
  704. .daddr = XFS_AGB_TO_DADDR(mp, id->agno, xfs_refc_block(mp)),
  705. .numblks = BTOBB(mp->m_sb.sb_blocksize),
  706. .ops = &xfs_refcountbt_buf_ops,
  707. .work = &xfs_btroot_init,
  708. .bc_ops = &xfs_refcountbt_ops,
  709. .need_init = xfs_has_reflink(mp)
  710. },
  711. { /* NULL terminating block */
  712. .daddr = XFS_BUF_DADDR_NULL,
  713. }
  714. };
  715. struct xfs_aghdr_grow_data *dp;
  716. int error = 0;
  717. /* Account for AG free space in new AG */
  718. id->nfree += id->agsize - mp->m_ag_prealloc_blocks;
  719. for (dp = &aghdr_data[0]; dp->daddr != XFS_BUF_DADDR_NULL; dp++) {
  720. if (!dp->need_init)
  721. continue;
  722. id->daddr = dp->daddr;
  723. id->numblks = dp->numblks;
  724. id->bc_ops = dp->bc_ops;
  725. error = xfs_ag_init_hdr(mp, id, dp->work, dp->ops);
  726. if (error)
  727. break;
  728. }
  729. return error;
  730. }
  731. int
  732. xfs_ag_shrink_space(
  733. struct xfs_perag *pag,
  734. struct xfs_trans **tpp,
  735. xfs_extlen_t delta)
  736. {
  737. struct xfs_mount *mp = pag->pag_mount;
  738. struct xfs_alloc_arg args = {
  739. .tp = *tpp,
  740. .mp = mp,
  741. .pag = pag,
  742. .minlen = delta,
  743. .maxlen = delta,
  744. .oinfo = XFS_RMAP_OINFO_SKIP_UPDATE,
  745. .resv = XFS_AG_RESV_NONE,
  746. .prod = 1
  747. };
  748. struct xfs_buf *agibp, *agfbp;
  749. struct xfs_agi *agi;
  750. struct xfs_agf *agf;
  751. xfs_agblock_t aglen;
  752. int error, err2;
  753. ASSERT(pag->pag_agno == mp->m_sb.sb_agcount - 1);
  754. error = xfs_ialloc_read_agi(pag, *tpp, 0, &agibp);
  755. if (error)
  756. return error;
  757. agi = agibp->b_addr;
  758. error = xfs_alloc_read_agf(pag, *tpp, 0, &agfbp);
  759. if (error)
  760. return error;
  761. agf = agfbp->b_addr;
  762. aglen = be32_to_cpu(agi->agi_length);
  763. /* some extra paranoid checks before we shrink the ag */
  764. if (XFS_IS_CORRUPT(mp, agf->agf_length != agi->agi_length)) {
  765. xfs_ag_mark_sick(pag, XFS_SICK_AG_AGF);
  766. return -EFSCORRUPTED;
  767. }
  768. if (delta >= aglen)
  769. return -EINVAL;
  770. /*
  771. * Make sure that the last inode cluster cannot overlap with the new
  772. * end of the AG, even if it's sparse.
  773. */
  774. error = xfs_ialloc_check_shrink(pag, *tpp, agibp, aglen - delta);
  775. if (error)
  776. return error;
  777. /*
  778. * Disable perag reservations so it doesn't cause the allocation request
  779. * to fail. We'll reestablish reservation before we return.
  780. */
  781. xfs_ag_resv_free(pag);
  782. /* internal log shouldn't also show up in the free space btrees */
  783. error = xfs_alloc_vextent_exact_bno(&args,
  784. XFS_AGB_TO_FSB(mp, pag->pag_agno, aglen - delta));
  785. if (!error && args.agbno == NULLAGBLOCK)
  786. error = -ENOSPC;
  787. if (error) {
  788. /*
  789. * If extent allocation fails, need to roll the transaction to
  790. * ensure that the AGFL fixup has been committed anyway.
  791. *
  792. * We need to hold the AGF across the roll to ensure nothing can
  793. * access the AG for allocation until the shrink is fully
  794. * cleaned up. And due to the resetting of the AG block
  795. * reservation space needing to lock the AGI, we also have to
  796. * hold that so we don't get AGI/AGF lock order inversions in
  797. * the error handling path.
  798. */
  799. xfs_trans_bhold(*tpp, agfbp);
  800. xfs_trans_bhold(*tpp, agibp);
  801. err2 = xfs_trans_roll(tpp);
  802. if (err2)
  803. return err2;
  804. xfs_trans_bjoin(*tpp, agfbp);
  805. xfs_trans_bjoin(*tpp, agibp);
  806. goto resv_init_out;
  807. }
  808. /*
  809. * if successfully deleted from freespace btrees, need to confirm
  810. * per-AG reservation works as expected.
  811. */
  812. be32_add_cpu(&agi->agi_length, -delta);
  813. be32_add_cpu(&agf->agf_length, -delta);
  814. err2 = xfs_ag_resv_init(pag, *tpp);
  815. if (err2) {
  816. be32_add_cpu(&agi->agi_length, delta);
  817. be32_add_cpu(&agf->agf_length, delta);
  818. if (err2 != -ENOSPC)
  819. goto resv_err;
  820. err2 = xfs_free_extent_later(*tpp, args.fsbno, delta, NULL,
  821. XFS_AG_RESV_NONE, XFS_FREE_EXTENT_SKIP_DISCARD);
  822. if (err2)
  823. goto resv_err;
  824. /*
  825. * Roll the transaction before trying to re-init the per-ag
  826. * reservation. The new transaction is clean so it will cancel
  827. * without any side effects.
  828. */
  829. error = xfs_defer_finish(tpp);
  830. if (error)
  831. return error;
  832. error = -ENOSPC;
  833. goto resv_init_out;
  834. }
  835. /* Update perag geometry */
  836. pag->block_count -= delta;
  837. __xfs_agino_range(pag->pag_mount, pag->block_count, &pag->agino_min,
  838. &pag->agino_max);
  839. xfs_ialloc_log_agi(*tpp, agibp, XFS_AGI_LENGTH);
  840. xfs_alloc_log_agf(*tpp, agfbp, XFS_AGF_LENGTH);
  841. return 0;
  842. resv_init_out:
  843. err2 = xfs_ag_resv_init(pag, *tpp);
  844. if (!err2)
  845. return error;
  846. resv_err:
  847. xfs_warn(mp, "Error %d reserving per-AG metadata reserve pool.", err2);
  848. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  849. return err2;
  850. }
  851. /*
  852. * Extent the AG indicated by the @id by the length passed in
  853. */
  854. int
  855. xfs_ag_extend_space(
  856. struct xfs_perag *pag,
  857. struct xfs_trans *tp,
  858. xfs_extlen_t len)
  859. {
  860. struct xfs_buf *bp;
  861. struct xfs_agi *agi;
  862. struct xfs_agf *agf;
  863. int error;
  864. ASSERT(pag->pag_agno == pag->pag_mount->m_sb.sb_agcount - 1);
  865. error = xfs_ialloc_read_agi(pag, tp, 0, &bp);
  866. if (error)
  867. return error;
  868. agi = bp->b_addr;
  869. be32_add_cpu(&agi->agi_length, len);
  870. xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
  871. /*
  872. * Change agf length.
  873. */
  874. error = xfs_alloc_read_agf(pag, tp, 0, &bp);
  875. if (error)
  876. return error;
  877. agf = bp->b_addr;
  878. be32_add_cpu(&agf->agf_length, len);
  879. ASSERT(agf->agf_length == agi->agi_length);
  880. xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH);
  881. /*
  882. * Free the new space.
  883. *
  884. * XFS_RMAP_OINFO_SKIP_UPDATE is used here to tell the rmap btree that
  885. * this doesn't actually exist in the rmap btree.
  886. */
  887. error = xfs_rmap_free(tp, bp, pag, be32_to_cpu(agf->agf_length) - len,
  888. len, &XFS_RMAP_OINFO_SKIP_UPDATE);
  889. if (error)
  890. return error;
  891. error = xfs_free_extent(tp, pag, be32_to_cpu(agf->agf_length) - len,
  892. len, &XFS_RMAP_OINFO_SKIP_UPDATE, XFS_AG_RESV_NONE);
  893. if (error)
  894. return error;
  895. /* Update perag geometry */
  896. pag->block_count = be32_to_cpu(agf->agf_length);
  897. __xfs_agino_range(pag->pag_mount, pag->block_count, &pag->agino_min,
  898. &pag->agino_max);
  899. return 0;
  900. }
  901. /* Retrieve AG geometry. */
  902. int
  903. xfs_ag_get_geometry(
  904. struct xfs_perag *pag,
  905. struct xfs_ag_geometry *ageo)
  906. {
  907. struct xfs_buf *agi_bp;
  908. struct xfs_buf *agf_bp;
  909. struct xfs_agi *agi;
  910. struct xfs_agf *agf;
  911. unsigned int freeblks;
  912. int error;
  913. /* Lock the AG headers. */
  914. error = xfs_ialloc_read_agi(pag, NULL, 0, &agi_bp);
  915. if (error)
  916. return error;
  917. error = xfs_alloc_read_agf(pag, NULL, 0, &agf_bp);
  918. if (error)
  919. goto out_agi;
  920. /* Fill out form. */
  921. memset(ageo, 0, sizeof(*ageo));
  922. ageo->ag_number = pag->pag_agno;
  923. agi = agi_bp->b_addr;
  924. ageo->ag_icount = be32_to_cpu(agi->agi_count);
  925. ageo->ag_ifree = be32_to_cpu(agi->agi_freecount);
  926. agf = agf_bp->b_addr;
  927. ageo->ag_length = be32_to_cpu(agf->agf_length);
  928. freeblks = pag->pagf_freeblks +
  929. pag->pagf_flcount +
  930. pag->pagf_btreeblks -
  931. xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE);
  932. ageo->ag_freeblks = freeblks;
  933. xfs_ag_geom_health(pag, ageo);
  934. /* Release resources. */
  935. xfs_buf_relse(agf_bp);
  936. out_agi:
  937. xfs_buf_relse(agi_bp);
  938. return error;
  939. }