]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_alloc_btree.c
libxfs: update to 3.16 kernel code
[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 *stat)
57 {
58 int error;
59 xfs_agblock_t bno;
60
61 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
62
63 /* Allocate the new block from the freelist. If we can't, give up. */
64 error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
65 &bno, 1);
66 if (error) {
67 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
68 return error;
69 }
70
71 if (bno == NULLAGBLOCK) {
72 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
73 *stat = 0;
74 return 0;
75 }
76
77 xfs_extent_busy_reuse(cur->bc_mp, cur->bc_private.a.agno, bno, 1, false);
78
79 xfs_trans_agbtree_delta(cur->bc_tp, 1);
80 new->s = cpu_to_be32(bno);
81
82 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
83 *stat = 1;
84 return 0;
85 }
86
87 STATIC int
88 xfs_allocbt_free_block(
89 struct xfs_btree_cur *cur,
90 struct xfs_buf *bp)
91 {
92 struct xfs_buf *agbp = cur->bc_private.a.agbp;
93 struct xfs_agf *agf = XFS_BUF_TO_AGF(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 * Similarly, during log recovery we will have a perag structure
273 * attached, but the agf information will not yet have been initialised
274 * from the on disk AGF. Again, we can only check against maximum limits
275 * in this case.
276 */
277 level = be16_to_cpu(block->bb_level);
278 switch (block->bb_magic) {
279 case cpu_to_be32(XFS_ABTB_CRC_MAGIC):
280 if (!xfs_sb_version_hascrc(&mp->m_sb))
281 return false;
282 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid))
283 return false;
284 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
285 return false;
286 if (pag &&
287 be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
288 return false;
289 /* fall through */
290 case cpu_to_be32(XFS_ABTB_MAGIC):
291 if (pag && pag->pagf_init) {
292 if (level >= pag->pagf_levels[XFS_BTNUM_BNOi])
293 return false;
294 } else if (level >= mp->m_ag_maxlevels)
295 return false;
296 break;
297 case cpu_to_be32(XFS_ABTC_CRC_MAGIC):
298 if (!xfs_sb_version_hascrc(&mp->m_sb))
299 return false;
300 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid))
301 return false;
302 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
303 return false;
304 if (pag &&
305 be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
306 return false;
307 /* fall through */
308 case cpu_to_be32(XFS_ABTC_MAGIC):
309 if (pag && pag->pagf_init) {
310 if (level >= pag->pagf_levels[XFS_BTNUM_CNTi])
311 return false;
312 } else if (level >= mp->m_ag_maxlevels)
313 return false;
314 break;
315 default:
316 return false;
317 }
318
319 /* numrecs verification */
320 if (be16_to_cpu(block->bb_numrecs) > mp->m_alloc_mxr[level != 0])
321 return false;
322
323 /* sibling pointer verification */
324 if (!block->bb_u.s.bb_leftsib ||
325 (be32_to_cpu(block->bb_u.s.bb_leftsib) >= mp->m_sb.sb_agblocks &&
326 block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK)))
327 return false;
328 if (!block->bb_u.s.bb_rightsib ||
329 (be32_to_cpu(block->bb_u.s.bb_rightsib) >= mp->m_sb.sb_agblocks &&
330 block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK)))
331 return false;
332
333 return true;
334 }
335
336 static void
337 xfs_allocbt_read_verify(
338 struct xfs_buf *bp)
339 {
340 if (!xfs_btree_sblock_verify_crc(bp))
341 xfs_buf_ioerror(bp, EFSBADCRC);
342 else if (!xfs_allocbt_verify(bp))
343 xfs_buf_ioerror(bp, EFSCORRUPTED);
344
345 if (bp->b_error) {
346 trace_xfs_btree_corrupt(bp, _RET_IP_);
347 xfs_verifier_error(bp);
348 }
349 }
350
351 static void
352 xfs_allocbt_write_verify(
353 struct xfs_buf *bp)
354 {
355 if (!xfs_allocbt_verify(bp)) {
356 trace_xfs_btree_corrupt(bp, _RET_IP_);
357 xfs_buf_ioerror(bp, EFSCORRUPTED);
358 xfs_verifier_error(bp);
359 return;
360 }
361 xfs_btree_sblock_calc_crc(bp);
362
363 }
364
365 const struct xfs_buf_ops xfs_allocbt_buf_ops = {
366 .verify_read = xfs_allocbt_read_verify,
367 .verify_write = xfs_allocbt_write_verify,
368 };
369
370
371 #if defined(DEBUG) || defined(XFS_WARN)
372 STATIC int
373 xfs_allocbt_keys_inorder(
374 struct xfs_btree_cur *cur,
375 union xfs_btree_key *k1,
376 union xfs_btree_key *k2)
377 {
378 if (cur->bc_btnum == XFS_BTNUM_BNO) {
379 return be32_to_cpu(k1->alloc.ar_startblock) <
380 be32_to_cpu(k2->alloc.ar_startblock);
381 } else {
382 return be32_to_cpu(k1->alloc.ar_blockcount) <
383 be32_to_cpu(k2->alloc.ar_blockcount) ||
384 (k1->alloc.ar_blockcount == k2->alloc.ar_blockcount &&
385 be32_to_cpu(k1->alloc.ar_startblock) <
386 be32_to_cpu(k2->alloc.ar_startblock));
387 }
388 }
389
390 STATIC int
391 xfs_allocbt_recs_inorder(
392 struct xfs_btree_cur *cur,
393 union xfs_btree_rec *r1,
394 union xfs_btree_rec *r2)
395 {
396 if (cur->bc_btnum == XFS_BTNUM_BNO) {
397 return be32_to_cpu(r1->alloc.ar_startblock) +
398 be32_to_cpu(r1->alloc.ar_blockcount) <=
399 be32_to_cpu(r2->alloc.ar_startblock);
400 } else {
401 return be32_to_cpu(r1->alloc.ar_blockcount) <
402 be32_to_cpu(r2->alloc.ar_blockcount) ||
403 (r1->alloc.ar_blockcount == r2->alloc.ar_blockcount &&
404 be32_to_cpu(r1->alloc.ar_startblock) <
405 be32_to_cpu(r2->alloc.ar_startblock));
406 }
407 }
408 #endif /* DEBUG */
409
410 static const struct xfs_btree_ops xfs_allocbt_ops = {
411 .rec_len = sizeof(xfs_alloc_rec_t),
412 .key_len = sizeof(xfs_alloc_key_t),
413
414 .dup_cursor = xfs_allocbt_dup_cursor,
415 .set_root = xfs_allocbt_set_root,
416 .alloc_block = xfs_allocbt_alloc_block,
417 .free_block = xfs_allocbt_free_block,
418 .update_lastrec = xfs_allocbt_update_lastrec,
419 .get_minrecs = xfs_allocbt_get_minrecs,
420 .get_maxrecs = xfs_allocbt_get_maxrecs,
421 .init_key_from_rec = xfs_allocbt_init_key_from_rec,
422 .init_rec_from_key = xfs_allocbt_init_rec_from_key,
423 .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
424 .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
425 .key_diff = xfs_allocbt_key_diff,
426 .buf_ops = &xfs_allocbt_buf_ops,
427 #if defined(DEBUG) || defined(XFS_WARN)
428 .keys_inorder = xfs_allocbt_keys_inorder,
429 .recs_inorder = xfs_allocbt_recs_inorder,
430 #endif
431 };
432
433 /*
434 * Allocate a new allocation btree cursor.
435 */
436 struct xfs_btree_cur * /* new alloc btree cursor */
437 xfs_allocbt_init_cursor(
438 struct xfs_mount *mp, /* file system mount point */
439 struct xfs_trans *tp, /* transaction pointer */
440 struct xfs_buf *agbp, /* buffer for agf structure */
441 xfs_agnumber_t agno, /* allocation group number */
442 xfs_btnum_t btnum) /* btree identifier */
443 {
444 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
445 struct xfs_btree_cur *cur;
446
447 ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
448
449 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
450
451 cur->bc_tp = tp;
452 cur->bc_mp = mp;
453 cur->bc_btnum = btnum;
454 cur->bc_blocklog = mp->m_sb.sb_blocklog;
455 cur->bc_ops = &xfs_allocbt_ops;
456
457 if (btnum == XFS_BTNUM_CNT) {
458 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
459 cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
460 } else {
461 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
462 }
463
464 cur->bc_private.a.agbp = agbp;
465 cur->bc_private.a.agno = agno;
466
467 if (xfs_sb_version_hascrc(&mp->m_sb))
468 cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
469
470 return cur;
471 }
472
473 /*
474 * Calculate number of records in an alloc btree block.
475 */
476 int
477 xfs_allocbt_maxrecs(
478 struct xfs_mount *mp,
479 int blocklen,
480 int leaf)
481 {
482 blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
483
484 if (leaf)
485 return blocklen / sizeof(xfs_alloc_rec_t);
486 return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t));
487 }