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