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