Line data Source code
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"
13 : #include "xfs_bit.h"
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"
20 : #include "xfs_ialloc.h"
21 : #include "xfs_rmap.h"
22 : #include "xfs_ag.h"
23 : #include "xfs_ag_resv.h"
24 : #include "xfs_health.h"
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"
30 : #include "xfs_trace.h"
31 : #include "xfs_inode.h"
32 : #include "xfs_icache.h"
33 :
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 : */
41 : struct xfs_perag *
42 >18794*10^7 : xfs_perag_get(
43 : struct xfs_mount *mp,
44 : xfs_agnumber_t agno)
45 : {
46 >18794*10^7 : struct xfs_perag *pag;
47 :
48 >18794*10^7 : rcu_read_lock();
49 >18735*10^7 : pag = radix_tree_lookup(&mp->m_perag_tree, agno);
50 >18737*10^7 : if (pag) {
51 >18737*10^7 : trace_xfs_perag_get(pag, _RET_IP_);
52 >18721*10^7 : ASSERT(atomic_read(&pag->pag_ref) >= 0);
53 >18721*10^7 : atomic_inc(&pag->pag_ref);
54 : }
55 >18909*10^7 : rcu_read_unlock();
56 >18955*10^7 : return pag;
57 : }
58 :
59 : /*
60 : * search from @first to find the next perag with the given tag set.
61 : */
62 : struct xfs_perag *
63 807449 : xfs_perag_get_tag(
64 : struct xfs_mount *mp,
65 : xfs_agnumber_t first,
66 : unsigned int tag)
67 : {
68 807449 : struct xfs_perag *pag;
69 807449 : int found;
70 :
71 807449 : rcu_read_lock();
72 807449 : found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
73 : (void **)&pag, first, 1, tag);
74 807449 : if (found <= 0) {
75 386465 : rcu_read_unlock();
76 386465 : return NULL;
77 : }
78 420984 : trace_xfs_perag_get_tag(pag, _RET_IP_);
79 420984 : atomic_inc(&pag->pag_ref);
80 420984 : rcu_read_unlock();
81 420984 : return pag;
82 : }
83 :
84 : /* Get a passive reference to the given perag. */
85 : struct xfs_perag *
86 9566348539 : xfs_perag_hold(
87 : struct xfs_perag *pag)
88 : {
89 9566348539 : ASSERT(atomic_read(&pag->pag_ref) > 0 ||
90 : atomic_read(&pag->pag_active_ref) > 0);
91 :
92 9566348539 : trace_xfs_perag_hold(pag, _RET_IP_);
93 9560529137 : atomic_inc(&pag->pag_ref);
94 9572557770 : return pag;
95 : }
96 :
97 : void
98 >19750*10^7 : xfs_perag_put(
99 : struct xfs_perag *pag)
100 : {
101 >19750*10^7 : trace_xfs_perag_put(pag, _RET_IP_);
102 >19754*10^7 : ASSERT(atomic_read(&pag->pag_ref) > 0);
103 >19754*10^7 : atomic_dec(&pag->pag_ref);
104 >19933*10^7 : }
105 :
106 : /*
107 : * Active references for perag structures. This is for short term access to the
108 : * per ag structures for walking trees or accessing state. If an AG is being
109 : * shrunk or is offline, then this will fail to find that AG and return NULL
110 : * instead.
111 : */
112 : struct xfs_perag *
113 1204898698 : xfs_perag_grab(
114 : struct xfs_mount *mp,
115 : xfs_agnumber_t agno)
116 : {
117 1204898698 : struct xfs_perag *pag;
118 :
119 1204898698 : rcu_read_lock();
120 1204757624 : pag = radix_tree_lookup(&mp->m_perag_tree, agno);
121 1205002328 : if (pag) {
122 1204974214 : trace_xfs_perag_grab(pag, _RET_IP_);
123 2409457306 : if (!atomic_inc_not_zero(&pag->pag_active_ref))
124 0 : pag = NULL;
125 : }
126 1205551269 : rcu_read_unlock();
127 1205823015 : return pag;
128 : }
129 :
130 : /*
131 : * search from @first to find the next perag with the given tag set.
132 : */
133 : struct xfs_perag *
134 37937024 : xfs_perag_grab_tag(
135 : struct xfs_mount *mp,
136 : xfs_agnumber_t first,
137 : int tag)
138 : {
139 37937024 : struct xfs_perag *pag;
140 37937024 : int found;
141 :
142 37937024 : rcu_read_lock();
143 37932928 : found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
144 : (void **)&pag, first, 1, tag);
145 37934824 : if (found <= 0) {
146 27573315 : rcu_read_unlock();
147 27573315 : return NULL;
148 : }
149 10361509 : trace_xfs_perag_grab_tag(pag, _RET_IP_);
150 20725570 : if (!atomic_inc_not_zero(&pag->pag_active_ref))
151 0 : pag = NULL;
152 10365976 : rcu_read_unlock();
153 10369420 : return pag;
154 : }
155 :
156 : void
157 1215105911 : xfs_perag_rele(
158 : struct xfs_perag *pag)
159 : {
160 1215105911 : trace_xfs_perag_rele(pag, _RET_IP_);
161 1214749774 : if (atomic_dec_and_test(&pag->pag_active_ref))
162 528705 : wake_up(&pag->pag_active_wq);
163 1216986802 : }
164 :
165 : /*
166 : * xfs_initialize_perag_data
167 : *
168 : * Read in each per-ag structure so we can count up the number of
169 : * allocated inodes, free inodes and used filesystem blocks as this
170 : * information is no longer persistent in the superblock. Once we have
171 : * this information, write it into the in-core superblock structure.
172 : */
173 : int
174 13668 : xfs_initialize_perag_data(
175 : struct xfs_mount *mp,
176 : xfs_agnumber_t agcount)
177 : {
178 13668 : xfs_agnumber_t index;
179 13668 : struct xfs_perag *pag;
180 13668 : struct xfs_sb *sbp = &mp->m_sb;
181 13668 : uint64_t ifree = 0;
182 13668 : uint64_t ialloc = 0;
183 13668 : uint64_t bfree = 0;
184 13668 : uint64_t bfreelst = 0;
185 13668 : uint64_t btree = 0;
186 13668 : uint64_t fdblocks;
187 13668 : int error = 0;
188 :
189 72138 : for (index = 0; index < agcount; index++) {
190 : /*
191 : * Read the AGF and AGI buffers to populate the per-ag
192 : * structures for us.
193 : */
194 58470 : pag = xfs_perag_get(mp, index);
195 58470 : error = xfs_alloc_read_agf(pag, NULL, 0, NULL);
196 58470 : if (!error)
197 58470 : error = xfs_ialloc_read_agi(pag, NULL, NULL);
198 58470 : if (error) {
199 0 : xfs_perag_put(pag);
200 0 : return error;
201 : }
202 :
203 58470 : ifree += pag->pagi_freecount;
204 58470 : ialloc += pag->pagi_count;
205 58470 : bfree += pag->pagf_freeblks;
206 58470 : bfreelst += pag->pagf_flcount;
207 58470 : btree += pag->pagf_btreeblks;
208 58470 : xfs_perag_put(pag);
209 : }
210 13668 : fdblocks = bfree + bfreelst + btree;
211 :
212 : /*
213 : * If the new summary counts are obviously incorrect, fail the
214 : * mount operation because that implies the AGFs are also corrupt.
215 : * Clear FS_COUNTERS so that we don't unmount with a dirty log, which
216 : * will prevent xfs_repair from fixing anything.
217 : */
218 13668 : if (fdblocks > sbp->sb_dblocks || ifree > ialloc) {
219 11 : xfs_alert(mp, "AGF corruption. Please run xfs_repair.");
220 11 : xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
221 11 : error = -EFSCORRUPTED;
222 11 : goto out;
223 : }
224 :
225 : /* Overwrite incore superblock counters with just-read data */
226 13657 : spin_lock(&mp->m_sb_lock);
227 13657 : sbp->sb_ifree = ifree;
228 13657 : sbp->sb_icount = ialloc;
229 13657 : sbp->sb_fdblocks = fdblocks;
230 13657 : spin_unlock(&mp->m_sb_lock);
231 :
232 13657 : xfs_reinit_percpu_counters(mp);
233 13668 : out:
234 13668 : xfs_fs_mark_healthy(mp, XFS_SICK_FS_COUNTERS);
235 13668 : return error;
236 : }
237 :
238 : STATIC void
239 528705 : __xfs_free_perag(
240 : struct rcu_head *head)
241 : {
242 528705 : struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
243 :
244 528705 : ASSERT(!delayed_work_pending(&pag->pag_blockgc_work));
245 528705 : kmem_free(pag);
246 528705 : }
247 :
248 : /*
249 : * Free up the per-ag resources associated with the mount structure.
250 : */
251 : void
252 66867 : xfs_free_perag(
253 : struct xfs_mount *mp)
254 : {
255 66867 : struct xfs_perag *pag;
256 66867 : xfs_agnumber_t agno;
257 :
258 595572 : for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
259 528705 : spin_lock(&mp->m_perag_lock);
260 528705 : pag = radix_tree_delete(&mp->m_perag_tree, agno);
261 528705 : spin_unlock(&mp->m_perag_lock);
262 528705 : ASSERT(pag);
263 528705 : XFS_IS_CORRUPT(pag->pag_mount, atomic_read(&pag->pag_ref) != 0);
264 528705 : xfs_defer_drain_free(&pag->pag_intents_drain);
265 :
266 528705 : cancel_delayed_work_sync(&pag->pag_blockgc_work);
267 528705 : xfs_buf_cache_destroy(&pag->pag_bcache);
268 :
269 : /* drop the mount's active reference */
270 528705 : xfs_perag_rele(pag);
271 528705 : XFS_IS_CORRUPT(pag->pag_mount,
272 : atomic_read(&pag->pag_active_ref) != 0);
273 528705 : call_rcu(&pag->rcu_head, __xfs_free_perag);
274 : }
275 66867 : }
276 :
277 : /* Find the size of the AG, in blocks. */
278 : static xfs_agblock_t
279 >79348*10^7 : __xfs_ag_block_count(
280 : struct xfs_mount *mp,
281 : xfs_agnumber_t agno,
282 : xfs_agnumber_t agcount,
283 : xfs_rfsblock_t dblocks)
284 : {
285 >79348*10^7 : ASSERT(agno < agcount);
286 :
287 >79348*10^7 : if (agno < agcount - 1)
288 >73737*10^7 : return mp->m_sb.sb_agblocks;
289 56113837895 : return dblocks - (agno * mp->m_sb.sb_agblocks);
290 : }
291 :
292 : xfs_agblock_t
293 >48450*10^7 : xfs_ag_block_count(
294 : struct xfs_mount *mp,
295 : xfs_agnumber_t agno)
296 : {
297 >48450*10^7 : return __xfs_ag_block_count(mp, agno, mp->m_sb.sb_agcount,
298 : mp->m_sb.sb_dblocks);
299 : }
300 :
301 : /* Calculate the first and last possible inode number in an AG. */
302 : static void
303 >30714*10^7 : __xfs_agino_range(
304 : struct xfs_mount *mp,
305 : xfs_agblock_t eoag,
306 : xfs_agino_t *first,
307 : xfs_agino_t *last)
308 : {
309 >30714*10^7 : xfs_agblock_t bno;
310 :
311 : /*
312 : * Calculate the first inode, which will be in the first
313 : * cluster-aligned block after the AGFL.
314 : */
315 >30714*10^7 : bno = round_up(XFS_AGFL_BLOCK(mp) + 1, M_IGEO(mp)->cluster_align);
316 >30714*10^7 : *first = XFS_AGB_TO_AGINO(mp, bno);
317 :
318 : /*
319 : * Calculate the last inode, which will be at the end of the
320 : * last (aligned) cluster that can be allocated in the AG.
321 : */
322 >30714*10^7 : bno = round_down(eoag, M_IGEO(mp)->cluster_align);
323 >30714*10^7 : *last = XFS_AGB_TO_AGINO(mp, bno) - 1;
324 >30714*10^7 : }
325 :
326 : void
327 >30879*10^7 : xfs_agino_range(
328 : struct xfs_mount *mp,
329 : xfs_agnumber_t agno,
330 : xfs_agino_t *first,
331 : xfs_agino_t *last)
332 : {
333 >30879*10^7 : return __xfs_agino_range(mp, xfs_ag_block_count(mp, agno), first, last);
334 : }
335 :
336 : int
337 80854 : xfs_initialize_perag(
338 : struct xfs_mount *mp,
339 : xfs_agnumber_t agcount,
340 : xfs_rfsblock_t dblocks,
341 : xfs_agnumber_t *maxagi)
342 : {
343 80854 : struct xfs_perag *pag;
344 80854 : xfs_agnumber_t index;
345 80854 : xfs_agnumber_t first_initialised = NULLAGNUMBER;
346 80854 : int error;
347 :
348 : /*
349 : * Walk the current per-ag tree so we don't try to initialise AGs
350 : * that already exist (growfs case). Allocate and insert all the
351 : * AGs we don't find ready for initialisation.
352 : */
353 670224 : for (index = 0; index < agcount; index++) {
354 589370 : pag = xfs_perag_get(mp, index);
355 589370 : if (pag) {
356 60709 : xfs_perag_put(pag);
357 60709 : continue;
358 : }
359 :
360 528661 : pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
361 528661 : if (!pag) {
362 0 : error = -ENOMEM;
363 0 : goto out_unwind_new_pags;
364 : }
365 528661 : pag->pag_agno = index;
366 528661 : pag->pag_mount = mp;
367 :
368 528661 : error = radix_tree_preload(GFP_NOFS);
369 528661 : if (error)
370 0 : goto out_free_pag;
371 :
372 528661 : spin_lock(&mp->m_perag_lock);
373 528661 : if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
374 0 : WARN_ON_ONCE(1);
375 0 : spin_unlock(&mp->m_perag_lock);
376 0 : radix_tree_preload_end();
377 0 : error = -EEXIST;
378 0 : goto out_free_pag;
379 : }
380 528661 : spin_unlock(&mp->m_perag_lock);
381 528661 : radix_tree_preload_end();
382 :
383 : #ifdef __KERNEL__
384 : /* Place kernel structure only init below this point. */
385 528661 : spin_lock_init(&pag->pag_ici_lock);
386 528661 : spin_lock_init(&pag->pagb_lock);
387 528661 : spin_lock_init(&pag->pag_state_lock);
388 528661 : INIT_DELAYED_WORK(&pag->pag_blockgc_work, xfs_blockgc_worker);
389 528661 : INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
390 528661 : xfs_defer_drain_init(&pag->pag_intents_drain);
391 528661 : init_waitqueue_head(&pag->pagb_wait);
392 528661 : init_waitqueue_head(&pag->pag_active_wq);
393 528661 : pag->pagb_count = 0;
394 528661 : pag->pagb_tree = RB_ROOT;
395 528661 : xfs_hooks_init(&pag->pag_rmap_update_hooks);
396 : #endif /* __KERNEL__ */
397 :
398 528661 : error = xfs_buf_cache_init(&pag->pag_bcache);
399 528661 : if (error)
400 0 : goto out_remove_pag;
401 :
402 : /* Active ref owned by mount indicates AG is online. */
403 528661 : atomic_set(&pag->pag_active_ref, 1);
404 :
405 : /* first new pag is fully initialized */
406 528661 : if (first_initialised == NULLAGNUMBER)
407 67273 : first_initialised = index;
408 :
409 : /*
410 : * Pre-calculated geometry
411 : */
412 528661 : pag->block_count = __xfs_ag_block_count(mp, index, agcount,
413 : dblocks);
414 528661 : pag->min_block = XFS_AGFL_BLOCK(mp);
415 528661 : __xfs_agino_range(mp, pag->block_count, &pag->agino_min,
416 : &pag->agino_max);
417 : }
418 :
419 80854 : index = xfs_set_inode_alloc(mp, agcount);
420 :
421 80854 : if (maxagi)
422 80854 : *maxagi = index;
423 :
424 80854 : mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
425 80854 : return 0;
426 :
427 : out_remove_pag:
428 0 : xfs_defer_drain_free(&pag->pag_intents_drain);
429 0 : radix_tree_delete(&mp->m_perag_tree, index);
430 0 : out_free_pag:
431 0 : kmem_free(pag);
432 0 : out_unwind_new_pags:
433 : /* unwind any prior newly initialized pags */
434 0 : for (index = first_initialised; index < agcount; index++) {
435 0 : pag = radix_tree_delete(&mp->m_perag_tree, index);
436 0 : if (!pag)
437 : break;
438 0 : xfs_buf_cache_destroy(&pag->pag_bcache);
439 0 : xfs_defer_drain_free(&pag->pag_intents_drain);
440 0 : kmem_free(pag);
441 : }
442 : return error;
443 : }
444 :
445 : static int
446 370345 : xfs_get_aghdr_buf(
447 : struct xfs_mount *mp,
448 : xfs_daddr_t blkno,
449 : size_t numblks,
450 : struct xfs_buf **bpp,
451 : const struct xfs_buf_ops *ops)
452 : {
453 370345 : struct xfs_buf *bp;
454 370345 : int error;
455 :
456 370345 : error = xfs_buf_get_uncached(mp->m_ddev_targp, numblks, 0, &bp);
457 370345 : if (error)
458 : return error;
459 :
460 370345 : bp->b_maps[0].bm_bn = blkno;
461 370345 : bp->b_ops = ops;
462 :
463 370345 : *bpp = bp;
464 370345 : return 0;
465 : }
466 :
467 : /*
468 : * Generic btree root block init function
469 : */
470 : static void
471 96132 : xfs_btroot_init(
472 : struct xfs_mount *mp,
473 : struct xfs_buf *bp,
474 : struct aghdr_init_data *id)
475 : {
476 96132 : xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 0, id->agno);
477 96132 : }
478 :
479 : /* Finish initializing a free space btree. */
480 : static void
481 82242 : xfs_freesp_init_recs(
482 : struct xfs_mount *mp,
483 : struct xfs_buf *bp,
484 : struct aghdr_init_data *id)
485 : {
486 82242 : struct xfs_alloc_rec *arec;
487 82242 : struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
488 :
489 82242 : arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
490 82242 : arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
491 :
492 82242 : if (xfs_ag_contains_log(mp, id->agno)) {
493 0 : struct xfs_alloc_rec *nrec;
494 0 : xfs_agblock_t start = XFS_FSB_TO_AGBNO(mp,
495 : mp->m_sb.sb_logstart);
496 :
497 0 : ASSERT(start >= mp->m_ag_prealloc_blocks);
498 0 : if (start != mp->m_ag_prealloc_blocks) {
499 : /*
500 : * Modify first record to pad stripe align of log and
501 : * bump the record count.
502 : */
503 0 : arec->ar_blockcount = cpu_to_be32(start -
504 : mp->m_ag_prealloc_blocks);
505 0 : be16_add_cpu(&block->bb_numrecs, 1);
506 0 : nrec = arec + 1;
507 :
508 : /*
509 : * Insert second record at start of internal log
510 : * which then gets trimmed.
511 : */
512 0 : nrec->ar_startblock = cpu_to_be32(
513 : be32_to_cpu(arec->ar_startblock) +
514 : be32_to_cpu(arec->ar_blockcount));
515 0 : arec = nrec;
516 : }
517 : /*
518 : * Change record start to after the internal log
519 : */
520 0 : be32_add_cpu(&arec->ar_startblock, mp->m_sb.sb_logblocks);
521 : }
522 :
523 : /*
524 : * Calculate the block count of this record; if it is nonzero,
525 : * increment the record count.
526 : */
527 82242 : arec->ar_blockcount = cpu_to_be32(id->agsize -
528 : be32_to_cpu(arec->ar_startblock));
529 82242 : if (arec->ar_blockcount)
530 82242 : be16_add_cpu(&block->bb_numrecs, 1);
531 82242 : }
532 :
533 : /*
534 : * bnobt/cntbt btree root block init functions
535 : */
536 : static void
537 82242 : xfs_bnoroot_init(
538 : struct xfs_mount *mp,
539 : struct xfs_buf *bp,
540 : struct aghdr_init_data *id)
541 : {
542 82242 : xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 0, id->agno);
543 82242 : xfs_freesp_init_recs(mp, bp, id);
544 82242 : }
545 :
546 : /*
547 : * Reverse map root block init
548 : */
549 : static void
550 27487 : xfs_rmaproot_init(
551 : struct xfs_mount *mp,
552 : struct xfs_buf *bp,
553 : struct aghdr_init_data *id)
554 : {
555 27487 : struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
556 27487 : struct xfs_rmap_rec *rrec;
557 :
558 27487 : xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 4, id->agno);
559 :
560 : /*
561 : * mark the AG header regions as static metadata The BNO
562 : * btree block is the first block after the headers, so
563 : * it's location defines the size of region the static
564 : * metadata consumes.
565 : *
566 : * Note: unlike mkfs, we never have to account for log
567 : * space when growing the data regions
568 : */
569 27487 : rrec = XFS_RMAP_REC_ADDR(block, 1);
570 27487 : rrec->rm_startblock = 0;
571 27487 : rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp));
572 27487 : rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS);
573 27487 : rrec->rm_offset = 0;
574 :
575 : /* account freespace btree root blocks */
576 27487 : rrec = XFS_RMAP_REC_ADDR(block, 2);
577 27487 : rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp));
578 27487 : rrec->rm_blockcount = cpu_to_be32(2);
579 27487 : rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
580 27487 : rrec->rm_offset = 0;
581 :
582 : /* account inode btree root blocks */
583 27487 : rrec = XFS_RMAP_REC_ADDR(block, 3);
584 27487 : rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp));
585 27487 : rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) -
586 : XFS_IBT_BLOCK(mp));
587 27487 : rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT);
588 27487 : rrec->rm_offset = 0;
589 :
590 : /* account for rmap btree root */
591 27487 : rrec = XFS_RMAP_REC_ADDR(block, 4);
592 27487 : rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp));
593 27487 : rrec->rm_blockcount = cpu_to_be32(1);
594 27487 : rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
595 27487 : rrec->rm_offset = 0;
596 :
597 : /* account for refc btree root */
598 27487 : if (xfs_has_reflink(mp)) {
599 27487 : rrec = XFS_RMAP_REC_ADDR(block, 5);
600 27487 : rrec->rm_startblock = cpu_to_be32(xfs_refc_block(mp));
601 27487 : rrec->rm_blockcount = cpu_to_be32(1);
602 27487 : rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC);
603 27487 : rrec->rm_offset = 0;
604 27487 : be16_add_cpu(&block->bb_numrecs, 1);
605 : }
606 :
607 : /* account for the log space */
608 27487 : if (xfs_ag_contains_log(mp, id->agno)) {
609 0 : rrec = XFS_RMAP_REC_ADDR(block,
610 : be16_to_cpu(block->bb_numrecs) + 1);
611 0 : rrec->rm_startblock = cpu_to_be32(
612 : XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart));
613 0 : rrec->rm_blockcount = cpu_to_be32(mp->m_sb.sb_logblocks);
614 0 : rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_LOG);
615 0 : rrec->rm_offset = 0;
616 0 : be16_add_cpu(&block->bb_numrecs, 1);
617 : }
618 27487 : }
619 :
620 : /*
621 : * Initialise new secondary superblocks with the pre-grow geometry, but mark
622 : * them as "in progress" so we know they haven't yet been activated. This will
623 : * get cleared when the update with the new geometry information is done after
624 : * changes to the primary are committed. This isn't strictly necessary, but we
625 : * get it for free with the delayed buffer write lists and it means we can tell
626 : * if a grow operation didn't complete properly after the fact.
627 : */
628 : static void
629 41121 : xfs_sbblock_init(
630 : struct xfs_mount *mp,
631 : struct xfs_buf *bp,
632 : struct aghdr_init_data *id)
633 : {
634 41121 : struct xfs_dsb *dsb = bp->b_addr;
635 :
636 41121 : xfs_sb_to_disk(dsb, &mp->m_sb);
637 41121 : dsb->sb_inprogress = 1;
638 41121 : }
639 :
640 : static void
641 41121 : xfs_agfblock_init(
642 : struct xfs_mount *mp,
643 : struct xfs_buf *bp,
644 : struct aghdr_init_data *id)
645 : {
646 41121 : struct xfs_agf *agf = bp->b_addr;
647 41121 : xfs_extlen_t tmpsize;
648 :
649 41121 : agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
650 41121 : agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
651 41121 : agf->agf_seqno = cpu_to_be32(id->agno);
652 41121 : agf->agf_length = cpu_to_be32(id->agsize);
653 41121 : agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp));
654 41121 : agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp));
655 41121 : agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1);
656 41121 : agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1);
657 41121 : if (xfs_has_rmapbt(mp)) {
658 27487 : agf->agf_roots[XFS_BTNUM_RMAPi] =
659 27487 : cpu_to_be32(XFS_RMAP_BLOCK(mp));
660 27487 : agf->agf_levels[XFS_BTNUM_RMAPi] = cpu_to_be32(1);
661 27487 : agf->agf_rmap_blocks = cpu_to_be32(1);
662 : }
663 :
664 41121 : agf->agf_flfirst = cpu_to_be32(1);
665 41121 : agf->agf_fllast = 0;
666 41121 : agf->agf_flcount = 0;
667 41121 : tmpsize = id->agsize - mp->m_ag_prealloc_blocks;
668 41121 : agf->agf_freeblks = cpu_to_be32(tmpsize);
669 41121 : agf->agf_longest = cpu_to_be32(tmpsize);
670 41121 : if (xfs_has_crc(mp))
671 27524 : uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
672 41121 : if (xfs_has_reflink(mp)) {
673 27487 : agf->agf_refcount_root = cpu_to_be32(
674 : xfs_refc_block(mp));
675 27487 : agf->agf_refcount_level = cpu_to_be32(1);
676 27487 : agf->agf_refcount_blocks = cpu_to_be32(1);
677 : }
678 :
679 41121 : if (xfs_ag_contains_log(mp, id->agno)) {
680 0 : int64_t logblocks = mp->m_sb.sb_logblocks;
681 :
682 0 : be32_add_cpu(&agf->agf_freeblks, -logblocks);
683 0 : agf->agf_longest = cpu_to_be32(id->agsize -
684 : XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart) - logblocks);
685 : }
686 41121 : }
687 :
688 : static void
689 41121 : xfs_agflblock_init(
690 : struct xfs_mount *mp,
691 : struct xfs_buf *bp,
692 : struct aghdr_init_data *id)
693 : {
694 41121 : struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
695 41121 : __be32 *agfl_bno;
696 41121 : int bucket;
697 :
698 41121 : if (xfs_has_crc(mp)) {
699 27524 : agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
700 27524 : agfl->agfl_seqno = cpu_to_be32(id->agno);
701 27524 : uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
702 : }
703 :
704 41121 : agfl_bno = xfs_buf_to_agfl_bno(bp);
705 38363005 : for (bucket = 0; bucket < xfs_agfl_size(mp); bucket++)
706 38321884 : agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK);
707 41121 : }
708 :
709 : static void
710 41121 : xfs_agiblock_init(
711 : struct xfs_mount *mp,
712 : struct xfs_buf *bp,
713 : struct aghdr_init_data *id)
714 : {
715 41121 : struct xfs_agi *agi = bp->b_addr;
716 41121 : int bucket;
717 :
718 41121 : agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
719 41121 : agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
720 41121 : agi->agi_seqno = cpu_to_be32(id->agno);
721 41121 : agi->agi_length = cpu_to_be32(id->agsize);
722 41121 : agi->agi_count = 0;
723 41121 : agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
724 41121 : agi->agi_level = cpu_to_be32(1);
725 41121 : agi->agi_freecount = 0;
726 41121 : agi->agi_newino = cpu_to_be32(NULLAGINO);
727 41121 : agi->agi_dirino = cpu_to_be32(NULLAGINO);
728 41121 : if (xfs_has_crc(mp))
729 27524 : uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
730 41121 : if (xfs_has_finobt(mp)) {
731 27524 : agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp));
732 27524 : agi->agi_free_level = cpu_to_be32(1);
733 : }
734 2672865 : for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
735 2631744 : agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
736 41121 : if (xfs_has_inobtcounts(mp)) {
737 27524 : agi->agi_iblocks = cpu_to_be32(1);
738 27524 : if (xfs_has_finobt(mp))
739 27524 : agi->agi_fblocks = cpu_to_be32(1);
740 : }
741 41121 : }
742 :
743 : typedef void (*aghdr_init_work_f)(struct xfs_mount *mp, struct xfs_buf *bp,
744 : struct aghdr_init_data *id);
745 : static int
746 370345 : xfs_ag_init_hdr(
747 : struct xfs_mount *mp,
748 : struct aghdr_init_data *id,
749 : aghdr_init_work_f work,
750 : const struct xfs_buf_ops *ops)
751 : {
752 370345 : struct xfs_buf *bp;
753 370345 : int error;
754 :
755 370345 : error = xfs_get_aghdr_buf(mp, id->daddr, id->numblks, &bp, ops);
756 370345 : if (error)
757 : return error;
758 :
759 370345 : (*work)(mp, bp, id);
760 :
761 370345 : xfs_buf_delwri_queue(bp, &id->buffer_list);
762 370345 : xfs_buf_relse(bp);
763 370345 : return 0;
764 : }
765 :
766 : struct xfs_aghdr_grow_data {
767 : xfs_daddr_t daddr;
768 : size_t numblks;
769 : const struct xfs_buf_ops *ops;
770 : aghdr_init_work_f work;
771 : const struct xfs_btree_ops *bc_ops;
772 : bool need_init;
773 : };
774 :
775 : /*
776 : * Prepare new AG headers to be written to disk. We use uncached buffers here,
777 : * as it is assumed these new AG headers are currently beyond the currently
778 : * valid filesystem address space. Using cached buffers would trip over EOFS
779 : * corruption detection alogrithms in the buffer cache lookup routines.
780 : *
781 : * This is a non-transactional function, but the prepared buffers are added to a
782 : * delayed write buffer list supplied by the caller so they can submit them to
783 : * disk and wait on them as required.
784 : */
785 : int
786 41121 : xfs_ag_init_headers(
787 : struct xfs_mount *mp,
788 : struct aghdr_init_data *id)
789 :
790 : {
791 986904 : struct xfs_aghdr_grow_data aghdr_data[] = {
792 : { /* SB */
793 41121 : .daddr = XFS_AG_DADDR(mp, id->agno, XFS_SB_DADDR),
794 41121 : .numblks = XFS_FSS_TO_BB(mp, 1),
795 : .ops = &xfs_sb_buf_ops,
796 : .work = &xfs_sbblock_init,
797 : .need_init = true
798 : },
799 : { /* AGF */
800 41121 : .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGF_DADDR(mp)),
801 41121 : .numblks = XFS_FSS_TO_BB(mp, 1),
802 : .ops = &xfs_agf_buf_ops,
803 : .work = &xfs_agfblock_init,
804 : .need_init = true
805 : },
806 : { /* AGFL */
807 41121 : .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGFL_DADDR(mp)),
808 41121 : .numblks = XFS_FSS_TO_BB(mp, 1),
809 : .ops = &xfs_agfl_buf_ops,
810 : .work = &xfs_agflblock_init,
811 : .need_init = true
812 : },
813 : { /* AGI */
814 41121 : .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGI_DADDR(mp)),
815 41121 : .numblks = XFS_FSS_TO_BB(mp, 1),
816 : .ops = &xfs_agi_buf_ops,
817 : .work = &xfs_agiblock_init,
818 : .need_init = true
819 : },
820 : { /* BNO root block */
821 41121 : .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_BNO_BLOCK(mp)),
822 41121 : .numblks = BTOBB(mp->m_sb.sb_blocksize),
823 : .ops = &xfs_bnobt_buf_ops,
824 : .work = &xfs_bnoroot_init,
825 : .bc_ops = &xfs_bnobt_ops,
826 : .need_init = true
827 : },
828 : { /* CNT root block */
829 41121 : .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_CNT_BLOCK(mp)),
830 41121 : .numblks = BTOBB(mp->m_sb.sb_blocksize),
831 : .ops = &xfs_cntbt_buf_ops,
832 : .work = &xfs_bnoroot_init,
833 : .bc_ops = &xfs_cntbt_ops,
834 : .need_init = true
835 : },
836 : { /* INO root block */
837 41121 : .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_IBT_BLOCK(mp)),
838 41121 : .numblks = BTOBB(mp->m_sb.sb_blocksize),
839 : .ops = &xfs_inobt_buf_ops,
840 : .work = &xfs_btroot_init,
841 : .bc_ops = &xfs_inobt_ops,
842 : .need_init = true
843 : },
844 : { /* FINO root block */
845 41121 : .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_FIBT_BLOCK(mp)),
846 41121 : .numblks = BTOBB(mp->m_sb.sb_blocksize),
847 : .ops = &xfs_finobt_buf_ops,
848 : .work = &xfs_btroot_init,
849 : .bc_ops = &xfs_finobt_ops,
850 : .need_init = xfs_has_finobt(mp)
851 : },
852 : { /* RMAP root block */
853 41121 : .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_RMAP_BLOCK(mp)),
854 41121 : .numblks = BTOBB(mp->m_sb.sb_blocksize),
855 : .ops = &xfs_rmapbt_buf_ops,
856 : .work = &xfs_rmaproot_init,
857 : .bc_ops = &xfs_rmapbt_ops,
858 : .need_init = xfs_has_rmapbt(mp)
859 : },
860 : { /* REFC root block */
861 41121 : .daddr = XFS_AGB_TO_DADDR(mp, id->agno, xfs_refc_block(mp)),
862 41121 : .numblks = BTOBB(mp->m_sb.sb_blocksize),
863 : .ops = &xfs_refcountbt_buf_ops,
864 : .work = &xfs_btroot_init,
865 : .bc_ops = &xfs_refcountbt_ops,
866 : .need_init = xfs_has_reflink(mp)
867 : },
868 : { /* NULL terminating block */
869 : .daddr = XFS_BUF_DADDR_NULL,
870 : }
871 : };
872 41121 : struct xfs_aghdr_grow_data *dp;
873 41121 : int error = 0;
874 :
875 : /* Account for AG free space in new AG */
876 41121 : id->nfree += id->agsize - mp->m_ag_prealloc_blocks;
877 452331 : for (dp = &aghdr_data[0]; dp->daddr != XFS_BUF_DADDR_NULL; dp++) {
878 411210 : if (!dp->need_init)
879 40865 : continue;
880 :
881 370345 : id->daddr = dp->daddr;
882 370345 : id->numblks = dp->numblks;
883 370345 : id->bc_ops = dp->bc_ops;
884 370345 : error = xfs_ag_init_hdr(mp, id, dp->work, dp->ops);
885 370345 : if (error)
886 : break;
887 : }
888 41121 : return error;
889 : }
890 :
891 : int
892 641 : xfs_ag_shrink_space(
893 : struct xfs_perag *pag,
894 : struct xfs_trans **tpp,
895 : xfs_extlen_t delta)
896 : {
897 641 : struct xfs_mount *mp = pag->pag_mount;
898 641 : struct xfs_alloc_arg args = {
899 641 : .tp = *tpp,
900 : .mp = mp,
901 : .pag = pag,
902 : .minlen = delta,
903 : .maxlen = delta,
904 : .oinfo = XFS_RMAP_OINFO_SKIP_UPDATE,
905 : .resv = XFS_AG_RESV_NONE,
906 : .prod = 1
907 : };
908 641 : struct xfs_buf *agibp, *agfbp;
909 641 : struct xfs_agi *agi;
910 641 : struct xfs_agf *agf;
911 641 : xfs_agblock_t aglen;
912 641 : int error, err2;
913 :
914 641 : ASSERT(pag->pag_agno == mp->m_sb.sb_agcount - 1);
915 641 : error = xfs_ialloc_read_agi(pag, *tpp, &agibp);
916 641 : if (error)
917 : return error;
918 :
919 641 : agi = agibp->b_addr;
920 :
921 641 : error = xfs_alloc_read_agf(pag, *tpp, 0, &agfbp);
922 641 : if (error)
923 : return error;
924 :
925 641 : agf = agfbp->b_addr;
926 641 : aglen = be32_to_cpu(agi->agi_length);
927 : /* some extra paranoid checks before we shrink the ag */
928 641 : if (XFS_IS_CORRUPT(mp, agf->agf_length != agi->agi_length)) {
929 0 : xfs_ag_mark_sick(pag, XFS_SICK_AG_AGF);
930 0 : return -EFSCORRUPTED;
931 : }
932 641 : if (delta >= aglen)
933 : return -EINVAL;
934 :
935 : /*
936 : * Make sure that the last inode cluster cannot overlap with the new
937 : * end of the AG, even if it's sparse.
938 : */
939 641 : error = xfs_ialloc_check_shrink(pag, *tpp, agibp, aglen - delta);
940 641 : if (error)
941 : return error;
942 :
943 : /*
944 : * Disable perag reservations so it doesn't cause the allocation request
945 : * to fail. We'll reestablish reservation before we return.
946 : */
947 450 : xfs_ag_resv_free(pag);
948 :
949 : /* internal log shouldn't also show up in the free space btrees */
950 900 : error = xfs_alloc_vextent_exact_bno(&args,
951 450 : XFS_AGB_TO_FSB(mp, pag->pag_agno, aglen - delta));
952 450 : if (!error && args.agbno == NULLAGBLOCK)
953 : error = -ENOSPC;
954 :
955 227 : if (error) {
956 : /*
957 : * if extent allocation fails, need to roll the transaction to
958 : * ensure that the AGFL fixup has been committed anyway.
959 : */
960 223 : xfs_trans_bhold(*tpp, agfbp);
961 223 : err2 = xfs_trans_roll(tpp);
962 223 : if (err2)
963 : return err2;
964 223 : xfs_trans_bjoin(*tpp, agfbp);
965 223 : goto resv_init_out;
966 : }
967 :
968 : /*
969 : * if successfully deleted from freespace btrees, need to confirm
970 : * per-AG reservation works as expected.
971 : */
972 227 : be32_add_cpu(&agi->agi_length, -delta);
973 227 : be32_add_cpu(&agf->agf_length, -delta);
974 :
975 227 : err2 = xfs_ag_resv_init(pag, *tpp);
976 227 : if (err2) {
977 0 : be32_add_cpu(&agi->agi_length, delta);
978 0 : be32_add_cpu(&agf->agf_length, delta);
979 0 : if (err2 != -ENOSPC)
980 0 : goto resv_err;
981 :
982 0 : err2 = xfs_free_extent_later(*tpp, args.fsbno, delta, NULL,
983 : XFS_AG_RESV_NONE, XFS_FREE_EXTENT_SKIP_DISCARD);
984 0 : if (err2)
985 0 : goto resv_err;
986 :
987 : /*
988 : * Roll the transaction before trying to re-init the per-ag
989 : * reservation. The new transaction is clean so it will cancel
990 : * without any side effects.
991 : */
992 0 : error = xfs_defer_finish(tpp);
993 0 : if (error)
994 : return error;
995 :
996 0 : error = -ENOSPC;
997 0 : goto resv_init_out;
998 : }
999 227 : xfs_ialloc_log_agi(*tpp, agibp, XFS_AGI_LENGTH);
1000 227 : xfs_alloc_log_agf(*tpp, agfbp, XFS_AGF_LENGTH);
1001 227 : return 0;
1002 :
1003 223 : resv_init_out:
1004 223 : err2 = xfs_ag_resv_init(pag, *tpp);
1005 223 : if (!err2)
1006 : return error;
1007 0 : resv_err:
1008 0 : xfs_warn(mp, "Error %d reserving per-AG metadata reserve pool.", err2);
1009 0 : xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1010 0 : return err2;
1011 : }
1012 :
1013 : /*
1014 : * Extent the AG indicated by the @id by the length passed in
1015 : */
1016 : int
1017 104 : xfs_ag_extend_space(
1018 : struct xfs_perag *pag,
1019 : struct xfs_trans *tp,
1020 : xfs_extlen_t len)
1021 : {
1022 104 : struct xfs_buf *bp;
1023 104 : struct xfs_agi *agi;
1024 104 : struct xfs_agf *agf;
1025 104 : int error;
1026 :
1027 104 : ASSERT(pag->pag_agno == pag->pag_mount->m_sb.sb_agcount - 1);
1028 :
1029 104 : error = xfs_ialloc_read_agi(pag, tp, &bp);
1030 104 : if (error)
1031 : return error;
1032 :
1033 104 : agi = bp->b_addr;
1034 104 : be32_add_cpu(&agi->agi_length, len);
1035 104 : xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
1036 :
1037 : /*
1038 : * Change agf length.
1039 : */
1040 104 : error = xfs_alloc_read_agf(pag, tp, 0, &bp);
1041 104 : if (error)
1042 : return error;
1043 :
1044 104 : agf = bp->b_addr;
1045 104 : be32_add_cpu(&agf->agf_length, len);
1046 104 : ASSERT(agf->agf_length == agi->agi_length);
1047 104 : xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH);
1048 :
1049 : /*
1050 : * Free the new space.
1051 : *
1052 : * XFS_RMAP_OINFO_SKIP_UPDATE is used here to tell the rmap btree that
1053 : * this doesn't actually exist in the rmap btree.
1054 : */
1055 104 : error = xfs_rmap_free(tp, bp, pag, be32_to_cpu(agf->agf_length) - len,
1056 : len, &XFS_RMAP_OINFO_SKIP_UPDATE);
1057 104 : if (error)
1058 : return error;
1059 :
1060 104 : error = xfs_free_extent(tp, pag, be32_to_cpu(agf->agf_length) - len,
1061 : len, &XFS_RMAP_OINFO_SKIP_UPDATE, XFS_AG_RESV_NONE);
1062 104 : if (error)
1063 : return error;
1064 :
1065 : /* Update perag geometry */
1066 104 : pag->block_count = be32_to_cpu(agf->agf_length);
1067 104 : __xfs_agino_range(pag->pag_mount, pag->block_count, &pag->agino_min,
1068 : &pag->agino_max);
1069 104 : return 0;
1070 : }
1071 :
1072 : /* Compute the AG geometry flags. */
1073 : static inline uint32_t
1074 89454 : xfs_ag_calc_geoflags(
1075 : struct xfs_perag *pag)
1076 : {
1077 89454 : uint32_t ret = 0;
1078 :
1079 178908 : if (xfs_perag_prohibits_alloc(pag))
1080 0 : ret |= XFS_AG_FLAG_NOALLOC;
1081 :
1082 89454 : return ret;
1083 : }
1084 :
1085 : /*
1086 : * Compare the current AG geometry flags against the flags in the AG geometry
1087 : * structure and update the AG state to reflect any changes, then update the
1088 : * struct to reflect the current status.
1089 : */
1090 : static inline int
1091 89454 : xfs_ag_update_geoflags(
1092 : struct xfs_perag *pag,
1093 : struct xfs_ag_geometry *ageo,
1094 : uint32_t new_flags)
1095 : {
1096 89454 : uint32_t old_flags = xfs_ag_calc_geoflags(pag);
1097 89454 : int error;
1098 :
1099 89454 : if (!(new_flags & XFS_AG_FLAG_UPDATE)) {
1100 89454 : ageo->ag_flags = old_flags;
1101 89454 : return 0;
1102 : }
1103 :
1104 0 : if ((old_flags & XFS_AG_FLAG_NOALLOC) &&
1105 0 : !(new_flags & XFS_AG_FLAG_NOALLOC)) {
1106 0 : xfs_ag_clear_noalloc(pag);
1107 : }
1108 :
1109 0 : if (!(old_flags & XFS_AG_FLAG_NOALLOC) &&
1110 0 : (new_flags & XFS_AG_FLAG_NOALLOC)) {
1111 0 : error = xfs_ag_set_noalloc(pag);
1112 0 : if (error)
1113 : return error;
1114 : }
1115 :
1116 0 : ageo->ag_flags = xfs_ag_calc_geoflags(pag);
1117 0 : return 0;
1118 : }
1119 :
1120 : /* Retrieve AG geometry. */
1121 : int
1122 89454 : xfs_ag_get_geometry(
1123 : struct xfs_perag *pag,
1124 : struct xfs_ag_geometry *ageo)
1125 : {
1126 89454 : struct xfs_buf *agi_bp;
1127 89454 : struct xfs_buf *agf_bp;
1128 89454 : struct xfs_agi *agi;
1129 89454 : struct xfs_agf *agf;
1130 89454 : unsigned int freeblks;
1131 89454 : uint32_t inflags = ageo->ag_flags;
1132 89454 : int error;
1133 :
1134 : /* Lock the AG headers. */
1135 89454 : error = xfs_ialloc_read_agi(pag, NULL, &agi_bp);
1136 89454 : if (error)
1137 : return error;
1138 89454 : error = xfs_alloc_read_agf(pag, NULL, 0, &agf_bp);
1139 89454 : if (error)
1140 0 : goto out_agi;
1141 :
1142 89454 : error = xfs_ag_update_geoflags(pag, ageo, inflags);
1143 89454 : if (error)
1144 0 : goto out;
1145 :
1146 : /* Fill out form. */
1147 89454 : memset(ageo, 0, sizeof(*ageo));
1148 89454 : ageo->ag_number = pag->pag_agno;
1149 :
1150 89454 : agi = agi_bp->b_addr;
1151 89454 : ageo->ag_icount = be32_to_cpu(agi->agi_count);
1152 89454 : ageo->ag_ifree = be32_to_cpu(agi->agi_freecount);
1153 :
1154 89454 : agf = agf_bp->b_addr;
1155 89454 : ageo->ag_length = be32_to_cpu(agf->agf_length);
1156 178908 : freeblks = pag->pagf_freeblks +
1157 89454 : pag->pagf_flcount +
1158 89454 : pag->pagf_btreeblks -
1159 89454 : xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE);
1160 89454 : ageo->ag_freeblks = freeblks;
1161 89454 : xfs_ag_geom_health(pag, ageo);
1162 :
1163 89454 : out:
1164 : /* Release resources. */
1165 89454 : xfs_buf_relse(agf_bp);
1166 89454 : out_agi:
1167 89454 : xfs_buf_relse(agi_bp);
1168 89454 : return error;
1169 : }
1170 :
1171 : /* How many blocks does this AG contribute to fdblocks? */
1172 : xfs_extlen_t
1173 0 : xfs_ag_fdblocks(
1174 : struct xfs_perag *pag)
1175 : {
1176 0 : xfs_extlen_t ret;
1177 :
1178 0 : ASSERT(xfs_perag_initialised_agf(pag));
1179 :
1180 0 : ret = pag->pagf_freeblks + pag->pagf_flcount + pag->pagf_btreeblks;
1181 0 : ret -= pag->pag_meta_resv.ar_reserved;
1182 0 : ret -= pag->pag_rmapbt_resv.ar_orig_reserved;
1183 0 : return ret;
1184 : }
1185 :
1186 : /*
1187 : * Hide all the free space in this AG. Caller must hold both the AGI and the
1188 : * AGF buffers or have otherwise prevented concurrent access.
1189 : */
1190 : int
1191 0 : xfs_ag_set_noalloc(
1192 : struct xfs_perag *pag)
1193 : {
1194 0 : struct xfs_mount *mp = pag->pag_mount;
1195 0 : int error;
1196 :
1197 0 : ASSERT(xfs_perag_initialised_agf(pag));
1198 0 : ASSERT(xfs_perag_initialised_agi(pag));
1199 :
1200 0 : if (xfs_perag_prohibits_alloc(pag))
1201 : return 0;
1202 :
1203 0 : error = xfs_mod_fdblocks(mp, -(int64_t)xfs_ag_fdblocks(pag), false);
1204 0 : if (error)
1205 : return error;
1206 :
1207 0 : trace_xfs_ag_set_noalloc(pag);
1208 0 : set_bit(XFS_AGSTATE_NOALLOC, &pag->pag_opstate);
1209 0 : return 0;
1210 : }
1211 :
1212 : /*
1213 : * Unhide all the free space in this AG. Caller must hold both the AGI and
1214 : * the AGF buffers or have otherwise prevented concurrent access.
1215 : */
1216 : void
1217 1017001 : xfs_ag_clear_noalloc(
1218 : struct xfs_perag *pag)
1219 : {
1220 1017001 : struct xfs_mount *mp = pag->pag_mount;
1221 :
1222 2034002 : if (!xfs_perag_prohibits_alloc(pag))
1223 : return;
1224 :
1225 0 : xfs_mod_fdblocks(mp, xfs_ag_fdblocks(pag), false);
1226 :
1227 0 : trace_xfs_ag_clear_noalloc(pag);
1228 0 : clear_bit(XFS_AGSTATE_NOALLOC, &pag->pag_opstate);
1229 : }
|