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