]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_alloc_btree.c
libxfs: add crc format changes to generic btrees
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_alloc_btree.c
1 /*
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 #include <xfs.h>
19
20 STATIC struct xfs_btree_cur *
21 xfs_allocbt_dup_cursor(
22 struct xfs_btree_cur *cur)
23 {
24 return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
25 cur->bc_private.a.agbp, cur->bc_private.a.agno,
26 cur->bc_btnum);
27 }
28
29 STATIC void
30 xfs_allocbt_set_root(
31 struct xfs_btree_cur *cur,
32 union xfs_btree_ptr *ptr,
33 int inc)
34 {
35 struct xfs_buf *agbp = cur->bc_private.a.agbp;
36 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
37 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
38 int btnum = cur->bc_btnum;
39 struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
40
41 ASSERT(ptr->s != 0);
42
43 agf->agf_roots[btnum] = ptr->s;
44 be32_add_cpu(&agf->agf_levels[btnum], inc);
45 pag->pagf_levels[btnum] += inc;
46 xfs_perag_put(pag);
47
48 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
49 }
50
51 STATIC int
52 xfs_allocbt_alloc_block(
53 struct xfs_btree_cur *cur,
54 union xfs_btree_ptr *start,
55 union xfs_btree_ptr *new,
56 int length,
57 int *stat)
58 {
59 int error;
60 xfs_agblock_t bno;
61
62 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
63
64 /* Allocate the new block from the freelist. If we can't, give up. */
65 error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
66 &bno, 1);
67 if (error) {
68 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
69 return error;
70 }
71
72 if (bno == NULLAGBLOCK) {
73 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
74 *stat = 0;
75 return 0;
76 }
77
78 xfs_extent_busy_reuse(cur->bc_mp, cur->bc_private.a.agno, bno, 1, false);
79
80 xfs_trans_agbtree_delta(cur->bc_tp, 1);
81 new->s = cpu_to_be32(bno);
82
83 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
84 *stat = 1;
85 return 0;
86 }
87
88 STATIC int
89 xfs_allocbt_free_block(
90 struct xfs_btree_cur *cur,
91 struct xfs_buf *bp)
92 {
93 struct xfs_buf *agbp = cur->bc_private.a.agbp;
94 xfs_agblock_t bno;
95 int error;
96
97 bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
98 error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
99 if (error)
100 return error;
101
102 xfs_extent_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1,
103 XFS_EXTENT_BUSY_SKIP_DISCARD);
104 xfs_trans_agbtree_delta(cur->bc_tp, -1);
105
106 xfs_trans_binval(cur->bc_tp, bp);
107 return 0;
108 }
109
110 /*
111 * Update the longest extent in the AGF
112 */
113 STATIC void
114 xfs_allocbt_update_lastrec(
115 struct xfs_btree_cur *cur,
116 struct xfs_btree_block *block,
117 union xfs_btree_rec *rec,
118 int ptr,
119 int reason)
120 {
121 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
122 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
123 struct xfs_perag *pag;
124 __be32 len;
125 int numrecs;
126
127 ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
128
129 switch (reason) {
130 case LASTREC_UPDATE:
131 /*
132 * If this is the last leaf block and it's the last record,
133 * then update the size of the longest extent in the AG.
134 */
135 if (ptr != xfs_btree_get_numrecs(block))
136 return;
137 len = rec->alloc.ar_blockcount;
138 break;
139 case LASTREC_INSREC:
140 if (be32_to_cpu(rec->alloc.ar_blockcount) <=
141 be32_to_cpu(agf->agf_longest))
142 return;
143 len = rec->alloc.ar_blockcount;
144 break;
145 case LASTREC_DELREC:
146 numrecs = xfs_btree_get_numrecs(block);
147 if (ptr <= numrecs)
148 return;
149 ASSERT(ptr == numrecs + 1);
150
151 if (numrecs) {
152 xfs_alloc_rec_t *rrp;
153
154 rrp = XFS_ALLOC_REC_ADDR(cur->bc_mp, block, numrecs);
155 len = rrp->ar_blockcount;
156 } else {
157 len = 0;
158 }
159
160 break;
161 default:
162 ASSERT(0);
163 return;
164 }
165
166 agf->agf_longest = len;
167 pag = xfs_perag_get(cur->bc_mp, seqno);
168 pag->pagf_longest = be32_to_cpu(len);
169 xfs_perag_put(pag);
170 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
171 }
172
173 STATIC int
174 xfs_allocbt_get_minrecs(
175 struct xfs_btree_cur *cur,
176 int level)
177 {
178 return cur->bc_mp->m_alloc_mnr[level != 0];
179 }
180
181 STATIC int
182 xfs_allocbt_get_maxrecs(
183 struct xfs_btree_cur *cur,
184 int level)
185 {
186 return cur->bc_mp->m_alloc_mxr[level != 0];
187 }
188
189 STATIC void
190 xfs_allocbt_init_key_from_rec(
191 union xfs_btree_key *key,
192 union xfs_btree_rec *rec)
193 {
194 ASSERT(rec->alloc.ar_startblock != 0);
195
196 key->alloc.ar_startblock = rec->alloc.ar_startblock;
197 key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
198 }
199
200 STATIC void
201 xfs_allocbt_init_rec_from_key(
202 union xfs_btree_key *key,
203 union xfs_btree_rec *rec)
204 {
205 ASSERT(key->alloc.ar_startblock != 0);
206
207 rec->alloc.ar_startblock = key->alloc.ar_startblock;
208 rec->alloc.ar_blockcount = key->alloc.ar_blockcount;
209 }
210
211 STATIC void
212 xfs_allocbt_init_rec_from_cur(
213 struct xfs_btree_cur *cur,
214 union xfs_btree_rec *rec)
215 {
216 ASSERT(cur->bc_rec.a.ar_startblock != 0);
217
218 rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
219 rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
220 }
221
222 STATIC void
223 xfs_allocbt_init_ptr_from_cur(
224 struct xfs_btree_cur *cur,
225 union xfs_btree_ptr *ptr)
226 {
227 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
228
229 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
230 ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
231
232 ptr->s = agf->agf_roots[cur->bc_btnum];
233 }
234
235 STATIC __int64_t
236 xfs_allocbt_key_diff(
237 struct xfs_btree_cur *cur,
238 union xfs_btree_key *key)
239 {
240 xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
241 xfs_alloc_key_t *kp = &key->alloc;
242 __int64_t diff;
243
244 if (cur->bc_btnum == XFS_BTNUM_BNO) {
245 return (__int64_t)be32_to_cpu(kp->ar_startblock) -
246 rec->ar_startblock;
247 }
248
249 diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
250 if (diff)
251 return diff;
252
253 return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
254 }
255
256 static bool
257 xfs_allocbt_verify(
258 struct xfs_buf *bp)
259 {
260 struct xfs_mount *mp = bp->b_target->bt_mount;
261 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
262 struct xfs_perag *pag = bp->b_pag;
263 unsigned int level;
264
265 /*
266 * magic number and level verification
267 *
268 * During growfs operations, we can't verify the exact level or owner as
269 * the perag is not fully initialised and hence not attached to the
270 * buffer. In this case, check against the maximum tree depth.
271 */
272 level = be16_to_cpu(block->bb_level);
273 switch (cpu_to_be32(block->bb_magic)) {
274 case XFS_ABTB_CRC_MAGIC:
275 if (!xfs_sb_version_hascrc(&mp->m_sb))
276 return false;
277 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid))
278 return false;
279 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
280 return false;
281 if (pag &&
282 be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
283 return false;
284 /* fall through */
285 case XFS_ABTB_MAGIC:
286 if (pag) {
287 if (level >= pag->pagf_levels[XFS_BTNUM_BNOi])
288 return false;
289 } else if (level >= mp->m_ag_maxlevels)
290 return false;
291 break;
292 case XFS_ABTC_CRC_MAGIC:
293 if (!xfs_sb_version_hascrc(&mp->m_sb))
294 return false;
295 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid))
296 return false;
297 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
298 return false;
299 if (pag &&
300 be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
301 return false;
302 /* fall through */
303 case XFS_ABTC_MAGIC:
304 if (pag) {
305 if (level >= pag->pagf_levels[XFS_BTNUM_CNTi])
306 return false;
307 } else if (level >= mp->m_ag_maxlevels)
308 return false;
309 break;
310 default:
311 return false;
312 }
313
314 /* numrecs verification */
315 if (be16_to_cpu(block->bb_numrecs) > mp->m_alloc_mxr[level != 0])
316 return false;
317
318 /* sibling pointer verification */
319 if (!block->bb_u.s.bb_leftsib ||
320 (be32_to_cpu(block->bb_u.s.bb_leftsib) >= mp->m_sb.sb_agblocks &&
321 block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK)))
322 return false;
323 if (!block->bb_u.s.bb_rightsib ||
324 (be32_to_cpu(block->bb_u.s.bb_rightsib) >= mp->m_sb.sb_agblocks &&
325 block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK)))
326 return false;
327
328 return true;
329 }
330
331 static void
332 xfs_allocbt_read_verify(
333 struct xfs_buf *bp)
334 {
335 if (!(xfs_btree_sblock_verify_crc(bp) &&
336 xfs_allocbt_verify(bp))) {
337 trace_xfs_btree_corrupt(bp, _RET_IP_);
338 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
339 bp->b_target->bt_mount, bp->b_addr);
340 xfs_buf_ioerror(bp, EFSCORRUPTED);
341 }
342 }
343
344 static void
345 xfs_allocbt_write_verify(
346 struct xfs_buf *bp)
347 {
348 if (!xfs_allocbt_verify(bp)) {
349 trace_xfs_btree_corrupt(bp, _RET_IP_);
350 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
351 bp->b_target->bt_mount, bp->b_addr);
352 xfs_buf_ioerror(bp, EFSCORRUPTED);
353 }
354 xfs_btree_sblock_calc_crc(bp);
355
356 }
357
358 const struct xfs_buf_ops xfs_allocbt_buf_ops = {
359 .verify_read = xfs_allocbt_read_verify,
360 .verify_write = xfs_allocbt_write_verify,
361 };
362
363
364 #ifdef DEBUG
365 STATIC int
366 xfs_allocbt_keys_inorder(
367 struct xfs_btree_cur *cur,
368 union xfs_btree_key *k1,
369 union xfs_btree_key *k2)
370 {
371 if (cur->bc_btnum == XFS_BTNUM_BNO) {
372 return be32_to_cpu(k1->alloc.ar_startblock) <
373 be32_to_cpu(k2->alloc.ar_startblock);
374 } else {
375 return be32_to_cpu(k1->alloc.ar_blockcount) <
376 be32_to_cpu(k2->alloc.ar_blockcount) ||
377 (k1->alloc.ar_blockcount == k2->alloc.ar_blockcount &&
378 be32_to_cpu(k1->alloc.ar_startblock) <
379 be32_to_cpu(k2->alloc.ar_startblock));
380 }
381 }
382
383 STATIC int
384 xfs_allocbt_recs_inorder(
385 struct xfs_btree_cur *cur,
386 union xfs_btree_rec *r1,
387 union xfs_btree_rec *r2)
388 {
389 if (cur->bc_btnum == XFS_BTNUM_BNO) {
390 return be32_to_cpu(r1->alloc.ar_startblock) +
391 be32_to_cpu(r1->alloc.ar_blockcount) <=
392 be32_to_cpu(r2->alloc.ar_startblock);
393 } else {
394 return be32_to_cpu(r1->alloc.ar_blockcount) <
395 be32_to_cpu(r2->alloc.ar_blockcount) ||
396 (r1->alloc.ar_blockcount == r2->alloc.ar_blockcount &&
397 be32_to_cpu(r1->alloc.ar_startblock) <
398 be32_to_cpu(r2->alloc.ar_startblock));
399 }
400 }
401 #endif /* DEBUG */
402
403 #ifdef XFS_BTREE_TRACE
404 ktrace_t *xfs_allocbt_trace_buf;
405
406 STATIC void
407 xfs_allocbt_trace_enter(
408 struct xfs_btree_cur *cur,
409 const char *func,
410 char *s,
411 int type,
412 int line,
413 __psunsigned_t a0,
414 __psunsigned_t a1,
415 __psunsigned_t a2,
416 __psunsigned_t a3,
417 __psunsigned_t a4,
418 __psunsigned_t a5,
419 __psunsigned_t a6,
420 __psunsigned_t a7,
421 __psunsigned_t a8,
422 __psunsigned_t a9,
423 __psunsigned_t a10)
424 {
425 ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
426 (void *)func, (void *)s, NULL, (void *)cur,
427 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
428 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
429 (void *)a8, (void *)a9, (void *)a10);
430 }
431
432 STATIC void
433 xfs_allocbt_trace_cursor(
434 struct xfs_btree_cur *cur,
435 __uint32_t *s0,
436 __uint64_t *l0,
437 __uint64_t *l1)
438 {
439 *s0 = cur->bc_private.a.agno;
440 *l0 = cur->bc_rec.a.ar_startblock;
441 *l1 = cur->bc_rec.a.ar_blockcount;
442 }
443
444 STATIC void
445 xfs_allocbt_trace_key(
446 struct xfs_btree_cur *cur,
447 union xfs_btree_key *key,
448 __uint64_t *l0,
449 __uint64_t *l1)
450 {
451 *l0 = be32_to_cpu(key->alloc.ar_startblock);
452 *l1 = be32_to_cpu(key->alloc.ar_blockcount);
453 }
454
455 STATIC void
456 xfs_allocbt_trace_record(
457 struct xfs_btree_cur *cur,
458 union xfs_btree_rec *rec,
459 __uint64_t *l0,
460 __uint64_t *l1,
461 __uint64_t *l2)
462 {
463 *l0 = be32_to_cpu(rec->alloc.ar_startblock);
464 *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
465 *l2 = 0;
466 }
467 #endif /* XFS_BTREE_TRACE */
468
469 static const struct xfs_btree_ops xfs_allocbt_ops = {
470 .rec_len = sizeof(xfs_alloc_rec_t),
471 .key_len = sizeof(xfs_alloc_key_t),
472
473 .dup_cursor = xfs_allocbt_dup_cursor,
474 .set_root = xfs_allocbt_set_root,
475 .alloc_block = xfs_allocbt_alloc_block,
476 .free_block = xfs_allocbt_free_block,
477 .update_lastrec = xfs_allocbt_update_lastrec,
478 .get_minrecs = xfs_allocbt_get_minrecs,
479 .get_maxrecs = xfs_allocbt_get_maxrecs,
480 .init_key_from_rec = xfs_allocbt_init_key_from_rec,
481 .init_rec_from_key = xfs_allocbt_init_rec_from_key,
482 .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
483 .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
484 .key_diff = xfs_allocbt_key_diff,
485 .buf_ops = &xfs_allocbt_buf_ops,
486 #ifdef DEBUG
487 .keys_inorder = xfs_allocbt_keys_inorder,
488 .recs_inorder = xfs_allocbt_recs_inorder,
489 #endif
490
491 #ifdef XFS_BTREE_TRACE
492 .trace_enter = xfs_allocbt_trace_enter,
493 .trace_cursor = xfs_allocbt_trace_cursor,
494 .trace_key = xfs_allocbt_trace_key,
495 .trace_record = xfs_allocbt_trace_record,
496 #endif
497 };
498
499 /*
500 * Allocate a new allocation btree cursor.
501 */
502 struct xfs_btree_cur * /* new alloc btree cursor */
503 xfs_allocbt_init_cursor(
504 struct xfs_mount *mp, /* file system mount point */
505 struct xfs_trans *tp, /* transaction pointer */
506 struct xfs_buf *agbp, /* buffer for agf structure */
507 xfs_agnumber_t agno, /* allocation group number */
508 xfs_btnum_t btnum) /* btree identifier */
509 {
510 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
511 struct xfs_btree_cur *cur;
512
513 ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
514
515 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
516
517 cur->bc_tp = tp;
518 cur->bc_mp = mp;
519 cur->bc_btnum = btnum;
520 cur->bc_blocklog = mp->m_sb.sb_blocklog;
521 cur->bc_ops = &xfs_allocbt_ops;
522
523 if (btnum == XFS_BTNUM_CNT) {
524 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
525 cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
526 } else {
527 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
528 }
529
530 cur->bc_private.a.agbp = agbp;
531 cur->bc_private.a.agno = agno;
532
533 if (xfs_sb_version_hascrc(&mp->m_sb))
534 cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
535
536 return cur;
537 }
538
539 /*
540 * Calculate number of records in an alloc btree block.
541 */
542 int
543 xfs_allocbt_maxrecs(
544 struct xfs_mount *mp,
545 int blocklen,
546 int leaf)
547 {
548 blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
549
550 if (leaf)
551 return blocklen / sizeof(xfs_alloc_rec_t);
552 return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t));
553 }