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