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 >10471*10^7 : xfs_perag_get(
43 : struct xfs_mount *mp,
44 : xfs_agnumber_t agno)
45 : {
46 >10471*10^7 : struct xfs_perag *pag;
47 :
48 >10471*10^7 : rcu_read_lock();
49 >10516*10^7 : pag = radix_tree_lookup(&mp->m_perag_tree, agno);
50 >10491*10^7 : if (pag) {
51 >10491*10^7 : trace_xfs_perag_get(pag, _RET_IP_);
52 >10522*10^7 : ASSERT(atomic_read(&pag->pag_ref) >= 0);
53 >10522*10^7 : atomic_inc(&pag->pag_ref);
54 : }
55 >10523*10^7 : rcu_read_unlock();
56 >10524*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 3474045 : xfs_perag_get_tag(
64 : struct xfs_mount *mp,
65 : xfs_agnumber_t first,
66 : unsigned int tag)
67 : {
68 3474045 : struct xfs_perag *pag;
69 3474045 : int found;
70 :
71 3474045 : rcu_read_lock();
72 3474045 : found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
73 : (void **)&pag, first, 1, tag);
74 3474044 : if (found <= 0) {
75 1035542 : rcu_read_unlock();
76 1035542 : return NULL;
77 : }
78 2438502 : trace_xfs_perag_get_tag(pag, _RET_IP_);
79 2438505 : atomic_inc(&pag->pag_ref);
80 2438505 : rcu_read_unlock();
81 2438505 : return pag;
82 : }
83 :
84 : /* Get a passive reference to the given perag. */
85 : struct xfs_perag *
86 6616420200 : xfs_perag_hold(
87 : struct xfs_perag *pag)
88 : {
89 6616420200 : ASSERT(atomic_read(&pag->pag_ref) > 0 ||
90 : atomic_read(&pag->pag_active_ref) > 0);
91 :
92 6616420200 : trace_xfs_perag_hold(pag, _RET_IP_);
93 6616667801 : atomic_inc(&pag->pag_ref);
94 6616682188 : return pag;
95 : }
96 :
97 : void
98 >11093*10^7 : xfs_perag_put(
99 : struct xfs_perag *pag)
100 : {
101 >11093*10^7 : trace_xfs_perag_put(pag, _RET_IP_);
102 >11165*10^7 : ASSERT(atomic_read(&pag->pag_ref) > 0);
103 >11165*10^7 : atomic_dec(&pag->pag_ref);
104 >11168*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 693479827 : xfs_perag_grab(
114 : struct xfs_mount *mp,
115 : xfs_agnumber_t agno)
116 : {
117 693479827 : struct xfs_perag *pag;
118 :
119 693479827 : rcu_read_lock();
120 693503151 : pag = radix_tree_lookup(&mp->m_perag_tree, agno);
121 693491382 : if (pag) {
122 693488849 : trace_xfs_perag_grab(pag, _RET_IP_);
123 1387006572 : if (!atomic_inc_not_zero(&pag->pag_active_ref))
124 0 : pag = NULL;
125 : }
126 693515349 : rcu_read_unlock();
127 693521018 : 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 12943780 : xfs_perag_grab_tag(
135 : struct xfs_mount *mp,
136 : xfs_agnumber_t first,
137 : int tag)
138 : {
139 12943780 : struct xfs_perag *pag;
140 12943780 : int found;
141 :
142 12943780 : rcu_read_lock();
143 12944061 : found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
144 : (void **)&pag, first, 1, tag);
145 12944003 : if (found <= 0) {
146 9712181 : rcu_read_unlock();
147 9712181 : return NULL;
148 : }
149 3231822 : trace_xfs_perag_grab_tag(pag, _RET_IP_);
150 6463475 : if (!atomic_inc_not_zero(&pag->pag_active_ref))
151 0 : pag = NULL;
152 3231696 : rcu_read_unlock();
153 3231900 : return pag;
154 : }
155 :
156 : void
157 696857528 : xfs_perag_rele(
158 : struct xfs_perag *pag)
159 : {
160 696857528 : trace_xfs_perag_rele(pag, _RET_IP_);
161 1393796961 : if (atomic_dec_and_test(&pag->pag_active_ref))
162 134282 : wake_up(&pag->pag_active_wq);
163 696910838 : }
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 11385 : xfs_initialize_perag_data(
175 : struct xfs_mount *mp,
176 : xfs_agnumber_t agcount)
177 : {
178 11385 : xfs_agnumber_t index;
179 11385 : struct xfs_perag *pag;
180 11385 : struct xfs_sb *sbp = &mp->m_sb;
181 11385 : uint64_t ifree = 0;
182 11385 : uint64_t ialloc = 0;
183 11385 : uint64_t bfree = 0;
184 11385 : uint64_t bfreelst = 0;
185 11385 : uint64_t btree = 0;
186 11385 : uint64_t fdblocks;
187 11385 : int error = 0;
188 :
189 56907 : 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 45522 : pag = xfs_perag_get(mp, index);
195 45522 : error = xfs_alloc_read_agf(pag, NULL, 0, NULL);
196 45522 : if (!error)
197 45522 : error = xfs_ialloc_read_agi(pag, NULL, NULL);
198 45522 : if (error) {
199 0 : xfs_perag_put(pag);
200 0 : return error;
201 : }
202 :
203 45522 : ifree += pag->pagi_freecount;
204 45522 : ialloc += pag->pagi_count;
205 45522 : bfree += pag->pagf_freeblks;
206 45522 : bfreelst += pag->pagf_flcount;
207 45522 : btree += pag->pagf_btreeblks;
208 45522 : xfs_perag_put(pag);
209 : }
210 11385 : 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 11385 : if (fdblocks > sbp->sb_dblocks || ifree > ialloc) {
219 2 : xfs_alert(mp, "AGF corruption. Please run xfs_repair.");
220 2 : xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
221 2 : error = -EFSCORRUPTED;
222 2 : goto out;
223 : }
224 :
225 : /* Overwrite incore superblock counters with just-read data */
226 11383 : spin_lock(&mp->m_sb_lock);
227 11383 : sbp->sb_ifree = ifree;
228 11383 : sbp->sb_icount = ialloc;
229 11383 : sbp->sb_fdblocks = fdblocks;
230 11383 : spin_unlock(&mp->m_sb_lock);
231 :
232 11383 : xfs_reinit_percpu_counters(mp);
233 11385 : out:
234 11385 : xfs_fs_mark_healthy(mp, XFS_SICK_FS_COUNTERS);
235 11385 : return error;
236 : }
237 :
238 : STATIC void
239 134282 : __xfs_free_perag(
240 : struct rcu_head *head)
241 : {
242 134282 : struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
243 :
244 134282 : ASSERT(!delayed_work_pending(&pag->pag_blockgc_work));
245 134282 : kmem_free(pag);
246 134282 : }
247 :
248 : /*
249 : * Free up the per-ag resources associated with the mount structure.
250 : */
251 : void
252 24339 : xfs_free_perag(
253 : struct xfs_mount *mp)
254 : {
255 24339 : struct xfs_perag *pag;
256 24339 : xfs_agnumber_t agno;
257 :
258 158621 : for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
259 134282 : spin_lock(&mp->m_perag_lock);
260 134282 : pag = radix_tree_delete(&mp->m_perag_tree, agno);
261 134282 : spin_unlock(&mp->m_perag_lock);
262 134282 : ASSERT(pag);
263 134282 : XFS_IS_CORRUPT(pag->pag_mount, atomic_read(&pag->pag_ref) != 0);
264 134282 : xfs_defer_drain_free(&pag->pag_intents_drain);
265 :
266 134282 : cancel_delayed_work_sync(&pag->pag_blockgc_work);
267 134282 : xfs_buf_cache_destroy(&pag->pag_bcache);
268 :
269 : /* drop the mount's active reference */
270 134282 : xfs_perag_rele(pag);
271 134282 : XFS_IS_CORRUPT(pag->pag_mount,
272 : atomic_read(&pag->pag_active_ref) != 0);
273 134282 : call_rcu(&pag->rcu_head, __xfs_free_perag);
274 : }
275 24339 : }
276 :
277 : /* Find the size of the AG, in blocks. */
278 : static xfs_agblock_t
279 >33679*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 >33679*10^7 : ASSERT(agno < agcount);
286 :
287 >33679*10^7 : if (agno < agcount - 1)
288 >29869*10^7 : return mp->m_sb.sb_agblocks;
289 38100886915 : return dblocks - (agno * mp->m_sb.sb_agblocks);
290 : }
291 :
292 : xfs_agblock_t
293 >17277*10^7 : xfs_ag_block_count(
294 : struct xfs_mount *mp,
295 : xfs_agnumber_t agno)
296 : {
297 >17277*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 >16612*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 >16612*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 >16612*10^7 : bno = round_up(XFS_AGFL_BLOCK(mp) + 1, M_IGEO(mp)->cluster_align);
316 >16612*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 >16612*10^7 : bno = round_down(eoag, M_IGEO(mp)->cluster_align);
323 >16612*10^7 : *last = XFS_AGB_TO_AGINO(mp, bno) - 1;
324 >16612*10^7 : }
325 :
326 : void
327 >16478*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 >16478*10^7 : return __xfs_agino_range(mp, xfs_ag_block_count(mp, agno), first, last);
334 : }
335 :
336 : int
337 35788 : 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 35788 : struct xfs_perag *pag;
344 35788 : xfs_agnumber_t index;
345 35788 : xfs_agnumber_t first_initialised = NULLAGNUMBER;
346 35788 : 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 216098 : for (index = 0; index < agcount; index++) {
354 180310 : pag = xfs_perag_get(mp, index);
355 180310 : if (pag) {
356 46052 : xfs_perag_put(pag);
357 46052 : continue;
358 : }
359 :
360 134258 : pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
361 134258 : if (!pag) {
362 0 : error = -ENOMEM;
363 0 : goto out_unwind_new_pags;
364 : }
365 134258 : pag->pag_agno = index;
366 134258 : pag->pag_mount = mp;
367 :
368 134258 : error = radix_tree_preload(GFP_NOFS);
369 134258 : if (error)
370 0 : goto out_free_pag;
371 :
372 134258 : spin_lock(&mp->m_perag_lock);
373 134258 : 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 134258 : spin_unlock(&mp->m_perag_lock);
381 134258 : radix_tree_preload_end();
382 :
383 : #ifdef __KERNEL__
384 : /* Place kernel structure only init below this point. */
385 134258 : spin_lock_init(&pag->pag_ici_lock);
386 134258 : spin_lock_init(&pag->pagb_lock);
387 134258 : spin_lock_init(&pag->pag_state_lock);
388 134258 : INIT_DELAYED_WORK(&pag->pag_blockgc_work, xfs_blockgc_worker);
389 134258 : INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
390 134258 : xfs_defer_drain_init(&pag->pag_intents_drain);
391 134258 : init_waitqueue_head(&pag->pagb_wait);
392 134258 : init_waitqueue_head(&pag->pag_active_wq);
393 134258 : pag->pagb_count = 0;
394 134258 : pag->pagb_tree = RB_ROOT;
395 134258 : xfs_hooks_init(&pag->pag_rmap_update_hooks);
396 : #endif /* __KERNEL__ */
397 :
398 134258 : error = xfs_buf_cache_init(&pag->pag_bcache);
399 134258 : if (error)
400 0 : goto out_remove_pag;
401 :
402 : /* Active ref owned by mount indicates AG is online. */
403 134258 : atomic_set(&pag->pag_active_ref, 1);
404 :
405 : /* first new pag is fully initialized */
406 134258 : if (first_initialised == NULLAGNUMBER)
407 24419 : first_initialised = index;
408 :
409 : /*
410 : * Pre-calculated geometry
411 : */
412 134258 : pag->block_count = __xfs_ag_block_count(mp, index, agcount,
413 : dblocks);
414 134258 : pag->min_block = XFS_AGFL_BLOCK(mp);
415 134258 : __xfs_agino_range(mp, pag->block_count, &pag->agino_min,
416 : &pag->agino_max);
417 : }
418 :
419 35788 : index = xfs_set_inode_alloc(mp, agcount);
420 :
421 35788 : if (maxagi)
422 35788 : *maxagi = index;
423 :
424 35788 : mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
425 35788 : 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 70054 : 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 70054 : struct xfs_buf *bp;
454 70054 : int error;
455 :
456 70054 : error = xfs_buf_get_uncached(mp->m_ddev_targp, numblks, 0, &bp);
457 70054 : if (error)
458 : return error;
459 :
460 70054 : bp->b_maps[0].bm_bn = blkno;
461 70054 : bp->b_ops = ops;
462 :
463 70054 : *bpp = bp;
464 70054 : return 0;
465 : }
466 :
467 : /*
468 : * Generic btree root block init function
469 : */
470 : static void
471 18506 : xfs_btroot_init(
472 : struct xfs_mount *mp,
473 : struct xfs_buf *bp,
474 : struct aghdr_init_data *id)
475 : {
476 18506 : xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 0, id->agno);
477 18506 : }
478 :
479 : /* Finish initializing a free space btree. */
480 : static void
481 15380 : xfs_freesp_init_recs(
482 : struct xfs_mount *mp,
483 : struct xfs_buf *bp,
484 : struct aghdr_init_data *id)
485 : {
486 15380 : struct xfs_alloc_rec *arec;
487 15380 : struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
488 :
489 15380 : arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
490 15380 : arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
491 :
492 30760 : 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 30760 : arec->ar_blockcount = cpu_to_be32(id->agsize -
528 : be32_to_cpu(arec->ar_startblock));
529 15380 : if (arec->ar_blockcount)
530 15380 : be16_add_cpu(&block->bb_numrecs, 1);
531 15380 : }
532 :
533 : /*
534 : * bnobt/cntbt btree root block init functions
535 : */
536 : static void
537 15380 : xfs_bnoroot_init(
538 : struct xfs_mount *mp,
539 : struct xfs_buf *bp,
540 : struct aghdr_init_data *id)
541 : {
542 15380 : xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 0, id->agno);
543 15380 : xfs_freesp_init_recs(mp, bp, id);
544 15380 : }
545 :
546 : /*
547 : * Reverse map root block init
548 : */
549 : static void
550 5408 : xfs_rmaproot_init(
551 : struct xfs_mount *mp,
552 : struct xfs_buf *bp,
553 : struct aghdr_init_data *id)
554 : {
555 5408 : struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
556 5408 : struct xfs_rmap_rec *rrec;
557 :
558 5408 : 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 5408 : rrec = XFS_RMAP_REC_ADDR(block, 1);
570 5408 : rrec->rm_startblock = 0;
571 5408 : rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp));
572 5408 : rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS);
573 5408 : rrec->rm_offset = 0;
574 :
575 : /* account freespace btree root blocks */
576 5408 : rrec = XFS_RMAP_REC_ADDR(block, 2);
577 5408 : rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp));
578 5408 : rrec->rm_blockcount = cpu_to_be32(2);
579 5408 : rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
580 5408 : rrec->rm_offset = 0;
581 :
582 : /* account inode btree root blocks */
583 5408 : rrec = XFS_RMAP_REC_ADDR(block, 3);
584 5408 : rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp));
585 5408 : rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) -
586 : XFS_IBT_BLOCK(mp));
587 5408 : rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT);
588 5408 : rrec->rm_offset = 0;
589 :
590 : /* account for rmap btree root */
591 5408 : rrec = XFS_RMAP_REC_ADDR(block, 4);
592 5408 : rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp));
593 5408 : rrec->rm_blockcount = cpu_to_be32(1);
594 5408 : rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
595 5408 : rrec->rm_offset = 0;
596 :
597 : /* account for refc btree root */
598 5408 : if (xfs_has_reflink(mp)) {
599 5408 : rrec = XFS_RMAP_REC_ADDR(block, 5);
600 5408 : rrec->rm_startblock = cpu_to_be32(xfs_refc_block(mp));
601 5408 : rrec->rm_blockcount = cpu_to_be32(1);
602 5408 : rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC);
603 5408 : rrec->rm_offset = 0;
604 5408 : be16_add_cpu(&block->bb_numrecs, 1);
605 : }
606 :
607 : /* account for the log space */
608 10816 : 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 5408 : }
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 7690 : xfs_sbblock_init(
630 : struct xfs_mount *mp,
631 : struct xfs_buf *bp,
632 : struct aghdr_init_data *id)
633 : {
634 7690 : struct xfs_dsb *dsb = bp->b_addr;
635 :
636 7690 : xfs_sb_to_disk(dsb, &mp->m_sb);
637 7690 : dsb->sb_inprogress = 1;
638 7690 : }
639 :
640 : static void
641 7690 : xfs_agfblock_init(
642 : struct xfs_mount *mp,
643 : struct xfs_buf *bp,
644 : struct aghdr_init_data *id)
645 : {
646 7690 : struct xfs_agf *agf = bp->b_addr;
647 7690 : xfs_extlen_t tmpsize;
648 :
649 7690 : agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
650 7690 : agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
651 7690 : agf->agf_seqno = cpu_to_be32(id->agno);
652 7690 : agf->agf_length = cpu_to_be32(id->agsize);
653 7690 : agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp));
654 7690 : agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp));
655 7690 : agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1);
656 7690 : agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1);
657 7690 : if (xfs_has_rmapbt(mp)) {
658 5408 : agf->agf_roots[XFS_BTNUM_RMAPi] =
659 5408 : cpu_to_be32(XFS_RMAP_BLOCK(mp));
660 5408 : agf->agf_levels[XFS_BTNUM_RMAPi] = cpu_to_be32(1);
661 5408 : agf->agf_rmap_blocks = cpu_to_be32(1);
662 : }
663 :
664 7690 : agf->agf_flfirst = cpu_to_be32(1);
665 7690 : agf->agf_fllast = 0;
666 7690 : agf->agf_flcount = 0;
667 7690 : tmpsize = id->agsize - mp->m_ag_prealloc_blocks;
668 7690 : agf->agf_freeblks = cpu_to_be32(tmpsize);
669 7690 : agf->agf_longest = cpu_to_be32(tmpsize);
670 7690 : if (xfs_has_crc(mp))
671 5408 : uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
672 7690 : if (xfs_has_reflink(mp)) {
673 5408 : agf->agf_refcount_root = cpu_to_be32(
674 : xfs_refc_block(mp));
675 5408 : agf->agf_refcount_level = cpu_to_be32(1);
676 5408 : agf->agf_refcount_blocks = cpu_to_be32(1);
677 : }
678 :
679 15380 : 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 7690 : }
687 :
688 : static void
689 7690 : xfs_agflblock_init(
690 : struct xfs_mount *mp,
691 : struct xfs_buf *bp,
692 : struct aghdr_init_data *id)
693 : {
694 7690 : struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
695 7690 : __be32 *agfl_bno;
696 7690 : int bucket;
697 :
698 7690 : if (xfs_has_crc(mp)) {
699 5408 : agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
700 5408 : agfl->agfl_seqno = cpu_to_be32(id->agno);
701 5408 : uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
702 : }
703 :
704 7690 : agfl_bno = xfs_buf_to_agfl_bno(bp);
705 7190250 : for (bucket = 0; bucket < xfs_agfl_size(mp); bucket++)
706 7182560 : agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK);
707 7690 : }
708 :
709 : static void
710 7690 : xfs_agiblock_init(
711 : struct xfs_mount *mp,
712 : struct xfs_buf *bp,
713 : struct aghdr_init_data *id)
714 : {
715 7690 : struct xfs_agi *agi = bp->b_addr;
716 7690 : int bucket;
717 :
718 7690 : agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
719 7690 : agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
720 7690 : agi->agi_seqno = cpu_to_be32(id->agno);
721 7690 : agi->agi_length = cpu_to_be32(id->agsize);
722 7690 : agi->agi_count = 0;
723 7690 : agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
724 7690 : agi->agi_level = cpu_to_be32(1);
725 7690 : agi->agi_freecount = 0;
726 7690 : agi->agi_newino = cpu_to_be32(NULLAGINO);
727 7690 : agi->agi_dirino = cpu_to_be32(NULLAGINO);
728 7690 : if (xfs_has_crc(mp))
729 5408 : uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
730 7690 : if (xfs_has_finobt(mp)) {
731 5408 : agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp));
732 5408 : agi->agi_free_level = cpu_to_be32(1);
733 : }
734 499850 : for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
735 492160 : agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
736 7690 : if (xfs_has_inobtcounts(mp)) {
737 5408 : agi->agi_iblocks = cpu_to_be32(1);
738 5408 : if (xfs_has_finobt(mp))
739 5408 : agi->agi_fblocks = cpu_to_be32(1);
740 : }
741 7690 : }
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 70054 : 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 70054 : struct xfs_buf *bp;
753 70054 : int error;
754 :
755 70054 : error = xfs_get_aghdr_buf(mp, id->daddr, id->numblks, &bp, ops);
756 70054 : if (error)
757 : return error;
758 :
759 70054 : (*work)(mp, bp, id);
760 :
761 70054 : xfs_buf_delwri_queue(bp, &id->buffer_list);
762 70054 : xfs_buf_relse(bp);
763 70054 : 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 7690 : xfs_ag_init_headers(
787 : struct xfs_mount *mp,
788 : struct aghdr_init_data *id)
789 :
790 : {
791 23070 : struct xfs_aghdr_grow_data aghdr_data[] = {
792 : { /* SB */
793 7690 : .daddr = XFS_AG_DADDR(mp, id->agno, XFS_SB_DADDR),
794 7690 : .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 7690 : .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGF_DADDR(mp)),
801 : .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 7690 : .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGFL_DADDR(mp)),
808 : .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 7690 : .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGI_DADDR(mp)),
815 : .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 7690 : .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_BNO_BLOCK(mp)),
822 7690 : .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 7690 : .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_CNT_BLOCK(mp)),
830 : .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 7690 : .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_IBT_BLOCK(mp)),
838 : .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 7690 : .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_FIBT_BLOCK(mp)),
846 : .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 7690 : .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_RMAP_BLOCK(mp)),
854 : .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 7690 : .daddr = XFS_AGB_TO_DADDR(mp, id->agno, xfs_refc_block(mp)),
862 7690 : .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 7690 : struct xfs_aghdr_grow_data *dp;
873 7690 : int error = 0;
874 :
875 : /* Account for AG free space in new AG */
876 7690 : id->nfree += id->agsize - mp->m_ag_prealloc_blocks;
877 84590 : for (dp = &aghdr_data[0]; dp->daddr != XFS_BUF_DADDR_NULL; dp++) {
878 76900 : if (!dp->need_init)
879 6846 : continue;
880 :
881 70054 : id->daddr = dp->daddr;
882 70054 : id->numblks = dp->numblks;
883 70054 : id->bc_ops = dp->bc_ops;
884 70054 : error = xfs_ag_init_hdr(mp, id, dp->work, dp->ops);
885 70054 : if (error)
886 : break;
887 : }
888 7690 : return error;
889 : }
890 :
891 : int
892 191 : xfs_ag_shrink_space(
893 : struct xfs_perag *pag,
894 : struct xfs_trans **tpp,
895 : xfs_extlen_t delta)
896 : {
897 191 : struct xfs_mount *mp = pag->pag_mount;
898 191 : struct xfs_alloc_arg args = {
899 191 : .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 191 : struct xfs_buf *agibp, *agfbp;
909 191 : struct xfs_agi *agi;
910 191 : struct xfs_agf *agf;
911 191 : xfs_agblock_t aglen;
912 191 : int error, err2;
913 :
914 191 : ASSERT(pag->pag_agno == mp->m_sb.sb_agcount - 1);
915 191 : error = xfs_ialloc_read_agi(pag, *tpp, &agibp);
916 191 : if (error)
917 : return error;
918 :
919 191 : agi = agibp->b_addr;
920 :
921 191 : error = xfs_alloc_read_agf(pag, *tpp, 0, &agfbp);
922 191 : if (error)
923 : return error;
924 :
925 191 : agf = agfbp->b_addr;
926 191 : aglen = be32_to_cpu(agi->agi_length);
927 : /* some extra paranoid checks before we shrink the ag */
928 191 : 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 191 : 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 191 : error = xfs_ialloc_check_shrink(pag, *tpp, agibp, aglen - delta);
940 191 : 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 96 : xfs_ag_resv_free(pag);
948 :
949 : /* internal log shouldn't also show up in the free space btrees */
950 96 : error = xfs_alloc_vextent_exact_bno(&args,
951 96 : XFS_AGB_TO_FSB(mp, pag->pag_agno, aglen - delta));
952 96 : if (!error && args.agbno == NULLAGBLOCK)
953 : error = -ENOSPC;
954 :
955 37 : 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 59 : xfs_trans_bhold(*tpp, agfbp);
961 59 : err2 = xfs_trans_roll(tpp);
962 59 : if (err2)
963 : return err2;
964 59 : xfs_trans_bjoin(*tpp, agfbp);
965 59 : 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 37 : be32_add_cpu(&agi->agi_length, -delta);
973 37 : be32_add_cpu(&agf->agf_length, -delta);
974 :
975 37 : err2 = xfs_ag_resv_init(pag, *tpp);
976 37 : 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 37 : xfs_ialloc_log_agi(*tpp, agibp, XFS_AGI_LENGTH);
1000 37 : xfs_alloc_log_agf(*tpp, agfbp, XFS_AGF_LENGTH);
1001 37 : return 0;
1002 :
1003 59 : resv_init_out:
1004 59 : err2 = xfs_ag_resv_init(pag, *tpp);
1005 59 : 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 28 : xfs_ag_extend_space(
1018 : struct xfs_perag *pag,
1019 : struct xfs_trans *tp,
1020 : xfs_extlen_t len)
1021 : {
1022 28 : struct xfs_buf *bp;
1023 28 : struct xfs_agi *agi;
1024 28 : struct xfs_agf *agf;
1025 28 : int error;
1026 :
1027 28 : ASSERT(pag->pag_agno == pag->pag_mount->m_sb.sb_agcount - 1);
1028 :
1029 28 : error = xfs_ialloc_read_agi(pag, tp, &bp);
1030 28 : if (error)
1031 : return error;
1032 :
1033 28 : agi = bp->b_addr;
1034 28 : be32_add_cpu(&agi->agi_length, len);
1035 28 : xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
1036 :
1037 : /*
1038 : * Change agf length.
1039 : */
1040 28 : error = xfs_alloc_read_agf(pag, tp, 0, &bp);
1041 28 : if (error)
1042 : return error;
1043 :
1044 28 : agf = bp->b_addr;
1045 28 : be32_add_cpu(&agf->agf_length, len);
1046 28 : ASSERT(agf->agf_length == agi->agi_length);
1047 28 : 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 56 : error = xfs_rmap_free(tp, bp, pag, be32_to_cpu(agf->agf_length) - len,
1056 : len, &XFS_RMAP_OINFO_SKIP_UPDATE);
1057 28 : if (error)
1058 : return error;
1059 :
1060 56 : 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 28 : if (error)
1063 : return error;
1064 :
1065 : /* Update perag geometry */
1066 28 : pag->block_count = be32_to_cpu(agf->agf_length);
1067 28 : __xfs_agino_range(pag->pag_mount, pag->block_count, &pag->agino_min,
1068 : &pag->agino_max);
1069 28 : return 0;
1070 : }
1071 :
1072 : /* Compute the AG geometry flags. */
1073 : static inline uint32_t
1074 15595 : xfs_ag_calc_geoflags(
1075 : struct xfs_perag *pag)
1076 : {
1077 15595 : uint32_t ret = 0;
1078 :
1079 31190 : if (xfs_perag_prohibits_alloc(pag))
1080 0 : ret |= XFS_AG_FLAG_NOALLOC;
1081 :
1082 15595 : 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 15595 : xfs_ag_update_geoflags(
1092 : struct xfs_perag *pag,
1093 : struct xfs_ag_geometry *ageo,
1094 : uint32_t new_flags)
1095 : {
1096 15595 : uint32_t old_flags = xfs_ag_calc_geoflags(pag);
1097 15595 : int error;
1098 :
1099 15595 : if (!(new_flags & XFS_AG_FLAG_UPDATE)) {
1100 15595 : ageo->ag_flags = old_flags;
1101 15595 : 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 15595 : xfs_ag_get_geometry(
1123 : struct xfs_perag *pag,
1124 : struct xfs_ag_geometry *ageo)
1125 : {
1126 15595 : struct xfs_buf *agi_bp;
1127 15595 : struct xfs_buf *agf_bp;
1128 15595 : struct xfs_agi *agi;
1129 15595 : struct xfs_agf *agf;
1130 15595 : unsigned int freeblks;
1131 15595 : uint32_t inflags = ageo->ag_flags;
1132 15595 : int error;
1133 :
1134 : /* Lock the AG headers. */
1135 15595 : error = xfs_ialloc_read_agi(pag, NULL, &agi_bp);
1136 15595 : if (error)
1137 : return error;
1138 15595 : error = xfs_alloc_read_agf(pag, NULL, 0, &agf_bp);
1139 15595 : if (error)
1140 0 : goto out_agi;
1141 :
1142 15595 : error = xfs_ag_update_geoflags(pag, ageo, inflags);
1143 15595 : if (error)
1144 0 : goto out;
1145 :
1146 : /* Fill out form. */
1147 15595 : memset(ageo, 0, sizeof(*ageo));
1148 15595 : ageo->ag_number = pag->pag_agno;
1149 :
1150 15595 : agi = agi_bp->b_addr;
1151 15595 : ageo->ag_icount = be32_to_cpu(agi->agi_count);
1152 15595 : ageo->ag_ifree = be32_to_cpu(agi->agi_freecount);
1153 :
1154 15595 : agf = agf_bp->b_addr;
1155 15595 : ageo->ag_length = be32_to_cpu(agf->agf_length);
1156 31190 : freeblks = pag->pagf_freeblks +
1157 15595 : pag->pagf_flcount +
1158 15595 : pag->pagf_btreeblks -
1159 15595 : xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE);
1160 15595 : ageo->ag_freeblks = freeblks;
1161 15595 : xfs_ag_geom_health(pag, ageo);
1162 :
1163 15595 : out:
1164 : /* Release resources. */
1165 15595 : xfs_buf_relse(agf_bp);
1166 15595 : out_agi:
1167 15595 : xfs_buf_relse(agi_bp);
1168 15595 : 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 : 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 261658 : xfs_ag_clear_noalloc(
1218 : struct xfs_perag *pag)
1219 : {
1220 261658 : struct xfs_mount *mp = pag->pag_mount;
1221 :
1222 523316 : 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 : }
|