Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
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 | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
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. | |
1da177e4 | 13 | * |
7b718769 NS |
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 | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_fs.h" |
1da177e4 | 20 | #include "xfs_types.h" |
a844f451 | 21 | #include "xfs_bit.h" |
1da177e4 | 22 | #include "xfs_log.h" |
a844f451 | 23 | #include "xfs_inum.h" |
1da177e4 LT |
24 | #include "xfs_trans.h" |
25 | #include "xfs_sb.h" | |
26 | #include "xfs_ag.h" | |
1da177e4 LT |
27 | #include "xfs_dir2.h" |
28 | #include "xfs_dmapi.h" | |
29 | #include "xfs_mount.h" | |
1da177e4 | 30 | #include "xfs_bmap_btree.h" |
a844f451 | 31 | #include "xfs_alloc_btree.h" |
1da177e4 | 32 | #include "xfs_ialloc_btree.h" |
1da177e4 | 33 | #include "xfs_dir2_sf.h" |
a844f451 | 34 | #include "xfs_attr_sf.h" |
1da177e4 LT |
35 | #include "xfs_dinode.h" |
36 | #include "xfs_inode.h" | |
a844f451 NS |
37 | #include "xfs_btree.h" |
38 | #include "xfs_ialloc.h" | |
1da177e4 LT |
39 | #include "xfs_error.h" |
40 | ||
41 | /* | |
42 | * Cursor allocation zone. | |
43 | */ | |
44 | kmem_zone_t *xfs_btree_cur_zone; | |
45 | ||
46 | /* | |
47 | * Btree magic numbers. | |
48 | */ | |
cdcf4333 | 49 | const __uint32_t xfs_magics[XFS_BTNUM_MAX] = { |
1da177e4 LT |
50 | XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC |
51 | }; | |
52 | ||
1da177e4 LT |
53 | /* |
54 | * Checking routine: return maxrecs for the block. | |
55 | */ | |
56 | STATIC int /* number of records fitting in block */ | |
57 | xfs_btree_maxrecs( | |
58 | xfs_btree_cur_t *cur, /* btree cursor */ | |
59 | xfs_btree_block_t *block) /* generic btree block pointer */ | |
60 | { | |
61 | switch (cur->bc_btnum) { | |
62 | case XFS_BTNUM_BNO: | |
63 | case XFS_BTNUM_CNT: | |
16259e7d | 64 | return (int)XFS_ALLOC_BLOCK_MAXRECS( |
f2277f06 | 65 | be16_to_cpu(block->bb_level), cur); |
1da177e4 | 66 | case XFS_BTNUM_BMAP: |
16259e7d | 67 | return (int)XFS_BMAP_BLOCK_IMAXRECS( |
f2277f06 | 68 | be16_to_cpu(block->bb_level), cur); |
1da177e4 | 69 | case XFS_BTNUM_INO: |
16259e7d | 70 | return (int)XFS_INOBT_BLOCK_MAXRECS( |
f2277f06 | 71 | be16_to_cpu(block->bb_level), cur); |
1da177e4 LT |
72 | default: |
73 | ASSERT(0); | |
74 | return 0; | |
75 | } | |
76 | } | |
77 | ||
78 | /* | |
79 | * External routines. | |
80 | */ | |
81 | ||
82 | #ifdef DEBUG | |
83 | /* | |
84 | * Debug routine: check that block header is ok. | |
85 | */ | |
86 | void | |
87 | xfs_btree_check_block( | |
88 | xfs_btree_cur_t *cur, /* btree cursor */ | |
89 | xfs_btree_block_t *block, /* generic btree block pointer */ | |
90 | int level, /* level of the btree block */ | |
91 | xfs_buf_t *bp) /* buffer containing block, if any */ | |
92 | { | |
e99ab90d | 93 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
1da177e4 LT |
94 | xfs_btree_check_lblock(cur, (xfs_btree_lblock_t *)block, level, |
95 | bp); | |
96 | else | |
97 | xfs_btree_check_sblock(cur, (xfs_btree_sblock_t *)block, level, | |
98 | bp); | |
99 | } | |
100 | ||
101 | /* | |
102 | * Debug routine: check that keys are in the right order. | |
103 | */ | |
104 | void | |
105 | xfs_btree_check_key( | |
106 | xfs_btnum_t btnum, /* btree identifier */ | |
107 | void *ak1, /* pointer to left (lower) key */ | |
108 | void *ak2) /* pointer to right (higher) key */ | |
109 | { | |
110 | switch (btnum) { | |
111 | case XFS_BTNUM_BNO: { | |
112 | xfs_alloc_key_t *k1; | |
113 | xfs_alloc_key_t *k2; | |
114 | ||
115 | k1 = ak1; | |
116 | k2 = ak2; | |
16259e7d | 117 | ASSERT(be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock)); |
1da177e4 LT |
118 | break; |
119 | } | |
120 | case XFS_BTNUM_CNT: { | |
121 | xfs_alloc_key_t *k1; | |
122 | xfs_alloc_key_t *k2; | |
123 | ||
124 | k1 = ak1; | |
125 | k2 = ak2; | |
16259e7d CH |
126 | ASSERT(be32_to_cpu(k1->ar_blockcount) < be32_to_cpu(k2->ar_blockcount) || |
127 | (k1->ar_blockcount == k2->ar_blockcount && | |
128 | be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock))); | |
1da177e4 LT |
129 | break; |
130 | } | |
131 | case XFS_BTNUM_BMAP: { | |
132 | xfs_bmbt_key_t *k1; | |
133 | xfs_bmbt_key_t *k2; | |
134 | ||
135 | k1 = ak1; | |
136 | k2 = ak2; | |
8801bb99 | 137 | ASSERT(be64_to_cpu(k1->br_startoff) < be64_to_cpu(k2->br_startoff)); |
1da177e4 LT |
138 | break; |
139 | } | |
140 | case XFS_BTNUM_INO: { | |
141 | xfs_inobt_key_t *k1; | |
142 | xfs_inobt_key_t *k2; | |
143 | ||
144 | k1 = ak1; | |
145 | k2 = ak2; | |
61a25848 | 146 | ASSERT(be32_to_cpu(k1->ir_startino) < be32_to_cpu(k2->ir_startino)); |
1da177e4 LT |
147 | break; |
148 | } | |
149 | default: | |
150 | ASSERT(0); | |
151 | } | |
152 | } | |
153 | #endif /* DEBUG */ | |
154 | ||
155 | /* | |
156 | * Checking routine: check that long form block header is ok. | |
157 | */ | |
158 | /* ARGSUSED */ | |
159 | int /* error (0 or EFSCORRUPTED) */ | |
160 | xfs_btree_check_lblock( | |
161 | xfs_btree_cur_t *cur, /* btree cursor */ | |
162 | xfs_btree_lblock_t *block, /* btree long form block pointer */ | |
163 | int level, /* level of the btree block */ | |
164 | xfs_buf_t *bp) /* buffer for block, if any */ | |
165 | { | |
166 | int lblock_ok; /* block passes checks */ | |
167 | xfs_mount_t *mp; /* file system mount point */ | |
168 | ||
169 | mp = cur->bc_mp; | |
170 | lblock_ok = | |
16259e7d CH |
171 | be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] && |
172 | be16_to_cpu(block->bb_level) == level && | |
173 | be16_to_cpu(block->bb_numrecs) <= | |
1da177e4 LT |
174 | xfs_btree_maxrecs(cur, (xfs_btree_block_t *)block) && |
175 | block->bb_leftsib && | |
16259e7d CH |
176 | (be64_to_cpu(block->bb_leftsib) == NULLDFSBNO || |
177 | XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_leftsib))) && | |
1da177e4 | 178 | block->bb_rightsib && |
16259e7d CH |
179 | (be64_to_cpu(block->bb_rightsib) == NULLDFSBNO || |
180 | XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_rightsib))); | |
1da177e4 LT |
181 | if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp, XFS_ERRTAG_BTREE_CHECK_LBLOCK, |
182 | XFS_RANDOM_BTREE_CHECK_LBLOCK))) { | |
183 | if (bp) | |
184 | xfs_buftrace("LBTREE ERROR", bp); | |
185 | XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW, | |
186 | mp); | |
187 | return XFS_ERROR(EFSCORRUPTED); | |
188 | } | |
189 | return 0; | |
190 | } | |
191 | ||
192 | /* | |
193 | * Checking routine: check that (long) pointer is ok. | |
194 | */ | |
195 | int /* error (0 or EFSCORRUPTED) */ | |
196 | xfs_btree_check_lptr( | |
197 | xfs_btree_cur_t *cur, /* btree cursor */ | |
198 | xfs_dfsbno_t ptr, /* btree block disk address */ | |
199 | int level) /* btree block level */ | |
200 | { | |
201 | xfs_mount_t *mp; /* file system mount point */ | |
202 | ||
203 | mp = cur->bc_mp; | |
204 | XFS_WANT_CORRUPTED_RETURN( | |
205 | level > 0 && | |
206 | ptr != NULLDFSBNO && | |
207 | XFS_FSB_SANITY_CHECK(mp, ptr)); | |
208 | return 0; | |
209 | } | |
210 | ||
211 | #ifdef DEBUG | |
212 | /* | |
213 | * Debug routine: check that records are in the right order. | |
214 | */ | |
215 | void | |
216 | xfs_btree_check_rec( | |
217 | xfs_btnum_t btnum, /* btree identifier */ | |
218 | void *ar1, /* pointer to left (lower) record */ | |
219 | void *ar2) /* pointer to right (higher) record */ | |
220 | { | |
221 | switch (btnum) { | |
222 | case XFS_BTNUM_BNO: { | |
223 | xfs_alloc_rec_t *r1; | |
224 | xfs_alloc_rec_t *r2; | |
225 | ||
226 | r1 = ar1; | |
227 | r2 = ar2; | |
16259e7d CH |
228 | ASSERT(be32_to_cpu(r1->ar_startblock) + |
229 | be32_to_cpu(r1->ar_blockcount) <= | |
230 | be32_to_cpu(r2->ar_startblock)); | |
1da177e4 LT |
231 | break; |
232 | } | |
233 | case XFS_BTNUM_CNT: { | |
234 | xfs_alloc_rec_t *r1; | |
235 | xfs_alloc_rec_t *r2; | |
236 | ||
237 | r1 = ar1; | |
238 | r2 = ar2; | |
16259e7d CH |
239 | ASSERT(be32_to_cpu(r1->ar_blockcount) < be32_to_cpu(r2->ar_blockcount) || |
240 | (r1->ar_blockcount == r2->ar_blockcount && | |
241 | be32_to_cpu(r1->ar_startblock) < be32_to_cpu(r2->ar_startblock))); | |
1da177e4 LT |
242 | break; |
243 | } | |
244 | case XFS_BTNUM_BMAP: { | |
245 | xfs_bmbt_rec_t *r1; | |
246 | xfs_bmbt_rec_t *r2; | |
247 | ||
248 | r1 = ar1; | |
249 | r2 = ar2; | |
250 | ASSERT(xfs_bmbt_disk_get_startoff(r1) + | |
251 | xfs_bmbt_disk_get_blockcount(r1) <= | |
252 | xfs_bmbt_disk_get_startoff(r2)); | |
253 | break; | |
254 | } | |
255 | case XFS_BTNUM_INO: { | |
256 | xfs_inobt_rec_t *r1; | |
257 | xfs_inobt_rec_t *r2; | |
258 | ||
259 | r1 = ar1; | |
260 | r2 = ar2; | |
61a25848 CH |
261 | ASSERT(be32_to_cpu(r1->ir_startino) + XFS_INODES_PER_CHUNK <= |
262 | be32_to_cpu(r2->ir_startino)); | |
1da177e4 LT |
263 | break; |
264 | } | |
265 | default: | |
266 | ASSERT(0); | |
267 | } | |
268 | } | |
269 | #endif /* DEBUG */ | |
270 | ||
271 | /* | |
272 | * Checking routine: check that block header is ok. | |
273 | */ | |
274 | /* ARGSUSED */ | |
275 | int /* error (0 or EFSCORRUPTED) */ | |
276 | xfs_btree_check_sblock( | |
277 | xfs_btree_cur_t *cur, /* btree cursor */ | |
278 | xfs_btree_sblock_t *block, /* btree short form block pointer */ | |
279 | int level, /* level of the btree block */ | |
280 | xfs_buf_t *bp) /* buffer containing block */ | |
281 | { | |
282 | xfs_buf_t *agbp; /* buffer for ag. freespace struct */ | |
283 | xfs_agf_t *agf; /* ag. freespace structure */ | |
284 | xfs_agblock_t agflen; /* native ag. freespace length */ | |
285 | int sblock_ok; /* block passes checks */ | |
286 | ||
287 | agbp = cur->bc_private.a.agbp; | |
288 | agf = XFS_BUF_TO_AGF(agbp); | |
16259e7d | 289 | agflen = be32_to_cpu(agf->agf_length); |
1da177e4 | 290 | sblock_ok = |
16259e7d CH |
291 | be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] && |
292 | be16_to_cpu(block->bb_level) == level && | |
293 | be16_to_cpu(block->bb_numrecs) <= | |
1da177e4 | 294 | xfs_btree_maxrecs(cur, (xfs_btree_block_t *)block) && |
16259e7d CH |
295 | (be32_to_cpu(block->bb_leftsib) == NULLAGBLOCK || |
296 | be32_to_cpu(block->bb_leftsib) < agflen) && | |
1da177e4 | 297 | block->bb_leftsib && |
16259e7d CH |
298 | (be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK || |
299 | be32_to_cpu(block->bb_rightsib) < agflen) && | |
1da177e4 LT |
300 | block->bb_rightsib; |
301 | if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp, | |
302 | XFS_ERRTAG_BTREE_CHECK_SBLOCK, | |
303 | XFS_RANDOM_BTREE_CHECK_SBLOCK))) { | |
304 | if (bp) | |
305 | xfs_buftrace("SBTREE ERROR", bp); | |
306 | XFS_ERROR_REPORT("xfs_btree_check_sblock", XFS_ERRLEVEL_LOW, | |
307 | cur->bc_mp); | |
308 | return XFS_ERROR(EFSCORRUPTED); | |
309 | } | |
310 | return 0; | |
311 | } | |
312 | ||
313 | /* | |
314 | * Checking routine: check that (short) pointer is ok. | |
315 | */ | |
316 | int /* error (0 or EFSCORRUPTED) */ | |
317 | xfs_btree_check_sptr( | |
318 | xfs_btree_cur_t *cur, /* btree cursor */ | |
319 | xfs_agblock_t ptr, /* btree block disk address */ | |
320 | int level) /* btree block level */ | |
321 | { | |
322 | xfs_buf_t *agbp; /* buffer for ag. freespace struct */ | |
323 | xfs_agf_t *agf; /* ag. freespace structure */ | |
324 | ||
325 | agbp = cur->bc_private.a.agbp; | |
326 | agf = XFS_BUF_TO_AGF(agbp); | |
327 | XFS_WANT_CORRUPTED_RETURN( | |
328 | level > 0 && | |
329 | ptr != NULLAGBLOCK && ptr != 0 && | |
16259e7d | 330 | ptr < be32_to_cpu(agf->agf_length)); |
1da177e4 LT |
331 | return 0; |
332 | } | |
333 | ||
334 | /* | |
335 | * Delete the btree cursor. | |
336 | */ | |
337 | void | |
338 | xfs_btree_del_cursor( | |
339 | xfs_btree_cur_t *cur, /* btree cursor */ | |
340 | int error) /* del because of error */ | |
341 | { | |
342 | int i; /* btree level */ | |
343 | ||
344 | /* | |
345 | * Clear the buffer pointers, and release the buffers. | |
346 | * If we're doing this in the face of an error, we | |
347 | * need to make sure to inspect all of the entries | |
348 | * in the bc_bufs array for buffers to be unlocked. | |
349 | * This is because some of the btree code works from | |
350 | * level n down to 0, and if we get an error along | |
351 | * the way we won't have initialized all the entries | |
352 | * down to 0. | |
353 | */ | |
354 | for (i = 0; i < cur->bc_nlevels; i++) { | |
355 | if (cur->bc_bufs[i]) | |
356 | xfs_btree_setbuf(cur, i, NULL); | |
357 | else if (!error) | |
358 | break; | |
359 | } | |
360 | /* | |
361 | * Can't free a bmap cursor without having dealt with the | |
362 | * allocated indirect blocks' accounting. | |
363 | */ | |
364 | ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP || | |
365 | cur->bc_private.b.allocated == 0); | |
366 | /* | |
367 | * Free the cursor. | |
368 | */ | |
369 | kmem_zone_free(xfs_btree_cur_zone, cur); | |
370 | } | |
371 | ||
372 | /* | |
373 | * Duplicate the btree cursor. | |
374 | * Allocate a new one, copy the record, re-get the buffers. | |
375 | */ | |
376 | int /* error */ | |
377 | xfs_btree_dup_cursor( | |
378 | xfs_btree_cur_t *cur, /* input cursor */ | |
379 | xfs_btree_cur_t **ncur) /* output cursor */ | |
380 | { | |
381 | xfs_buf_t *bp; /* btree block's buffer pointer */ | |
382 | int error; /* error return value */ | |
383 | int i; /* level number of btree block */ | |
384 | xfs_mount_t *mp; /* mount structure for filesystem */ | |
385 | xfs_btree_cur_t *new; /* new cursor value */ | |
386 | xfs_trans_t *tp; /* transaction pointer, can be NULL */ | |
387 | ||
388 | tp = cur->bc_tp; | |
389 | mp = cur->bc_mp; | |
561f7d17 | 390 | |
1da177e4 LT |
391 | /* |
392 | * Allocate a new cursor like the old one. | |
393 | */ | |
561f7d17 CH |
394 | new = cur->bc_ops->dup_cursor(cur); |
395 | ||
1da177e4 LT |
396 | /* |
397 | * Copy the record currently in the cursor. | |
398 | */ | |
399 | new->bc_rec = cur->bc_rec; | |
561f7d17 | 400 | |
1da177e4 LT |
401 | /* |
402 | * For each level current, re-get the buffer and copy the ptr value. | |
403 | */ | |
404 | for (i = 0; i < new->bc_nlevels; i++) { | |
405 | new->bc_ptrs[i] = cur->bc_ptrs[i]; | |
406 | new->bc_ra[i] = cur->bc_ra[i]; | |
407 | if ((bp = cur->bc_bufs[i])) { | |
408 | if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, | |
409 | XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) { | |
410 | xfs_btree_del_cursor(new, error); | |
411 | *ncur = NULL; | |
412 | return error; | |
413 | } | |
414 | new->bc_bufs[i] = bp; | |
415 | ASSERT(bp); | |
416 | ASSERT(!XFS_BUF_GETERROR(bp)); | |
417 | } else | |
418 | new->bc_bufs[i] = NULL; | |
419 | } | |
1da177e4 LT |
420 | *ncur = new; |
421 | return 0; | |
422 | } | |
423 | ||
8186e517 CH |
424 | /* |
425 | * Get a the root block which is stored in the inode. | |
426 | * | |
427 | * For now this btree implementation assumes the btree root is always | |
428 | * stored in the if_broot field of an inode fork. | |
429 | */ | |
430 | STATIC struct xfs_btree_block * | |
431 | xfs_btree_get_iroot( | |
432 | struct xfs_btree_cur *cur) | |
433 | { | |
434 | struct xfs_ifork *ifp; | |
435 | ||
436 | ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork); | |
437 | return (struct xfs_btree_block *)ifp->if_broot; | |
438 | } | |
439 | ||
1da177e4 LT |
440 | /* |
441 | * Retrieve the block pointer from the cursor at the given level. | |
8186e517 | 442 | * This may be an inode btree root or from a buffer. |
1da177e4 | 443 | */ |
8186e517 | 444 | STATIC struct xfs_btree_block * /* generic btree block pointer */ |
1da177e4 | 445 | xfs_btree_get_block( |
8186e517 | 446 | struct xfs_btree_cur *cur, /* btree cursor */ |
1da177e4 | 447 | int level, /* level in btree */ |
8186e517 | 448 | struct xfs_buf **bpp) /* buffer containing the block */ |
1da177e4 | 449 | { |
8186e517 CH |
450 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
451 | (level == cur->bc_nlevels - 1)) { | |
452 | *bpp = NULL; | |
453 | return xfs_btree_get_iroot(cur); | |
1da177e4 | 454 | } |
8186e517 CH |
455 | |
456 | *bpp = cur->bc_bufs[level]; | |
457 | return XFS_BUF_TO_BLOCK(*bpp); | |
1da177e4 LT |
458 | } |
459 | ||
460 | /* | |
461 | * Get a buffer for the block, return it with no data read. | |
462 | * Long-form addressing. | |
463 | */ | |
464 | xfs_buf_t * /* buffer for fsbno */ | |
465 | xfs_btree_get_bufl( | |
466 | xfs_mount_t *mp, /* file system mount point */ | |
467 | xfs_trans_t *tp, /* transaction pointer */ | |
468 | xfs_fsblock_t fsbno, /* file system block number */ | |
469 | uint lock) /* lock flags for get_buf */ | |
470 | { | |
471 | xfs_buf_t *bp; /* buffer pointer (return value) */ | |
472 | xfs_daddr_t d; /* real disk block address */ | |
473 | ||
474 | ASSERT(fsbno != NULLFSBLOCK); | |
475 | d = XFS_FSB_TO_DADDR(mp, fsbno); | |
476 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock); | |
477 | ASSERT(bp); | |
478 | ASSERT(!XFS_BUF_GETERROR(bp)); | |
479 | return bp; | |
480 | } | |
481 | ||
482 | /* | |
483 | * Get a buffer for the block, return it with no data read. | |
484 | * Short-form addressing. | |
485 | */ | |
486 | xfs_buf_t * /* buffer for agno/agbno */ | |
487 | xfs_btree_get_bufs( | |
488 | xfs_mount_t *mp, /* file system mount point */ | |
489 | xfs_trans_t *tp, /* transaction pointer */ | |
490 | xfs_agnumber_t agno, /* allocation group number */ | |
491 | xfs_agblock_t agbno, /* allocation group block number */ | |
492 | uint lock) /* lock flags for get_buf */ | |
493 | { | |
494 | xfs_buf_t *bp; /* buffer pointer (return value) */ | |
495 | xfs_daddr_t d; /* real disk block address */ | |
496 | ||
497 | ASSERT(agno != NULLAGNUMBER); | |
498 | ASSERT(agbno != NULLAGBLOCK); | |
499 | d = XFS_AGB_TO_DADDR(mp, agno, agbno); | |
500 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock); | |
501 | ASSERT(bp); | |
502 | ASSERT(!XFS_BUF_GETERROR(bp)); | |
503 | return bp; | |
504 | } | |
505 | ||
1da177e4 LT |
506 | /* |
507 | * Check for the cursor referring to the last block at the given level. | |
508 | */ | |
509 | int /* 1=is last block, 0=not last block */ | |
510 | xfs_btree_islastblock( | |
511 | xfs_btree_cur_t *cur, /* btree cursor */ | |
512 | int level) /* level to check */ | |
513 | { | |
514 | xfs_btree_block_t *block; /* generic btree block pointer */ | |
515 | xfs_buf_t *bp; /* buffer containing block */ | |
516 | ||
517 | block = xfs_btree_get_block(cur, level, &bp); | |
518 | xfs_btree_check_block(cur, block, level, bp); | |
e99ab90d | 519 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
16259e7d | 520 | return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO; |
1da177e4 | 521 | else |
16259e7d | 522 | return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK; |
1da177e4 LT |
523 | } |
524 | ||
cdcf4333 CH |
525 | /* |
526 | * Change the cursor to point to the first record at the given level. | |
527 | * Other levels are unaffected. | |
528 | */ | |
529 | int /* success=1, failure=0 */ | |
530 | xfs_btree_firstrec( | |
531 | xfs_btree_cur_t *cur, /* btree cursor */ | |
532 | int level) /* level to change */ | |
533 | { | |
534 | xfs_btree_block_t *block; /* generic btree block pointer */ | |
535 | xfs_buf_t *bp; /* buffer containing block */ | |
536 | ||
537 | /* | |
538 | * Get the block pointer for this level. | |
539 | */ | |
540 | block = xfs_btree_get_block(cur, level, &bp); | |
541 | xfs_btree_check_block(cur, block, level, bp); | |
542 | /* | |
543 | * It's empty, there is no such record. | |
544 | */ | |
f2277f06 | 545 | if (!block->bb_numrecs) |
cdcf4333 CH |
546 | return 0; |
547 | /* | |
548 | * Set the ptr value to 1, that's the first record/key. | |
549 | */ | |
550 | cur->bc_ptrs[level] = 1; | |
551 | return 1; | |
552 | } | |
553 | ||
1da177e4 LT |
554 | /* |
555 | * Change the cursor to point to the last record in the current block | |
556 | * at the given level. Other levels are unaffected. | |
557 | */ | |
558 | int /* success=1, failure=0 */ | |
559 | xfs_btree_lastrec( | |
560 | xfs_btree_cur_t *cur, /* btree cursor */ | |
561 | int level) /* level to change */ | |
562 | { | |
563 | xfs_btree_block_t *block; /* generic btree block pointer */ | |
564 | xfs_buf_t *bp; /* buffer containing block */ | |
565 | ||
566 | /* | |
567 | * Get the block pointer for this level. | |
568 | */ | |
569 | block = xfs_btree_get_block(cur, level, &bp); | |
570 | xfs_btree_check_block(cur, block, level, bp); | |
571 | /* | |
572 | * It's empty, there is no such record. | |
573 | */ | |
f2277f06 | 574 | if (!block->bb_numrecs) |
1da177e4 LT |
575 | return 0; |
576 | /* | |
577 | * Set the ptr value to numrecs, that's the last record/key. | |
578 | */ | |
f2277f06 | 579 | cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs); |
1da177e4 LT |
580 | return 1; |
581 | } | |
582 | ||
583 | /* | |
584 | * Compute first and last byte offsets for the fields given. | |
585 | * Interprets the offsets table, which contains struct field offsets. | |
586 | */ | |
587 | void | |
588 | xfs_btree_offsets( | |
589 | __int64_t fields, /* bitmask of fields */ | |
590 | const short *offsets, /* table of field offsets */ | |
591 | int nbits, /* number of bits to inspect */ | |
592 | int *first, /* output: first byte offset */ | |
593 | int *last) /* output: last byte offset */ | |
594 | { | |
595 | int i; /* current bit number */ | |
596 | __int64_t imask; /* mask for current bit number */ | |
597 | ||
598 | ASSERT(fields != 0); | |
599 | /* | |
600 | * Find the lowest bit, so the first byte offset. | |
601 | */ | |
602 | for (i = 0, imask = 1LL; ; i++, imask <<= 1) { | |
603 | if (imask & fields) { | |
604 | *first = offsets[i]; | |
605 | break; | |
606 | } | |
607 | } | |
608 | /* | |
609 | * Find the highest bit, so the last byte offset. | |
610 | */ | |
611 | for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) { | |
612 | if (imask & fields) { | |
613 | *last = offsets[i + 1] - 1; | |
614 | break; | |
615 | } | |
616 | } | |
617 | } | |
618 | ||
619 | /* | |
620 | * Get a buffer for the block, return it read in. | |
621 | * Long-form addressing. | |
622 | */ | |
623 | int /* error */ | |
624 | xfs_btree_read_bufl( | |
625 | xfs_mount_t *mp, /* file system mount point */ | |
626 | xfs_trans_t *tp, /* transaction pointer */ | |
627 | xfs_fsblock_t fsbno, /* file system block number */ | |
628 | uint lock, /* lock flags for read_buf */ | |
629 | xfs_buf_t **bpp, /* buffer for fsbno */ | |
630 | int refval) /* ref count value for buffer */ | |
631 | { | |
632 | xfs_buf_t *bp; /* return value */ | |
633 | xfs_daddr_t d; /* real disk block address */ | |
634 | int error; | |
635 | ||
636 | ASSERT(fsbno != NULLFSBLOCK); | |
637 | d = XFS_FSB_TO_DADDR(mp, fsbno); | |
638 | if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d, | |
639 | mp->m_bsize, lock, &bp))) { | |
640 | return error; | |
641 | } | |
642 | ASSERT(!bp || !XFS_BUF_GETERROR(bp)); | |
643 | if (bp != NULL) { | |
644 | XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval); | |
645 | } | |
646 | *bpp = bp; | |
647 | return 0; | |
648 | } | |
649 | ||
650 | /* | |
651 | * Get a buffer for the block, return it read in. | |
652 | * Short-form addressing. | |
653 | */ | |
654 | int /* error */ | |
655 | xfs_btree_read_bufs( | |
656 | xfs_mount_t *mp, /* file system mount point */ | |
657 | xfs_trans_t *tp, /* transaction pointer */ | |
658 | xfs_agnumber_t agno, /* allocation group number */ | |
659 | xfs_agblock_t agbno, /* allocation group block number */ | |
660 | uint lock, /* lock flags for read_buf */ | |
661 | xfs_buf_t **bpp, /* buffer for agno/agbno */ | |
662 | int refval) /* ref count value for buffer */ | |
663 | { | |
664 | xfs_buf_t *bp; /* return value */ | |
665 | xfs_daddr_t d; /* real disk block address */ | |
666 | int error; | |
667 | ||
668 | ASSERT(agno != NULLAGNUMBER); | |
669 | ASSERT(agbno != NULLAGBLOCK); | |
670 | d = XFS_AGB_TO_DADDR(mp, agno, agbno); | |
671 | if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d, | |
672 | mp->m_bsize, lock, &bp))) { | |
673 | return error; | |
674 | } | |
675 | ASSERT(!bp || !XFS_BUF_GETERROR(bp)); | |
676 | if (bp != NULL) { | |
677 | switch (refval) { | |
678 | case XFS_ALLOC_BTREE_REF: | |
679 | XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval); | |
680 | break; | |
681 | case XFS_INO_BTREE_REF: | |
682 | XFS_BUF_SET_VTYPE_REF(bp, B_FS_INOMAP, refval); | |
683 | break; | |
684 | } | |
685 | } | |
686 | *bpp = bp; | |
687 | return 0; | |
688 | } | |
689 | ||
690 | /* | |
691 | * Read-ahead the block, don't wait for it, don't return a buffer. | |
692 | * Long-form addressing. | |
693 | */ | |
694 | /* ARGSUSED */ | |
695 | void | |
696 | xfs_btree_reada_bufl( | |
697 | xfs_mount_t *mp, /* file system mount point */ | |
698 | xfs_fsblock_t fsbno, /* file system block number */ | |
699 | xfs_extlen_t count) /* count of filesystem blocks */ | |
700 | { | |
701 | xfs_daddr_t d; | |
702 | ||
703 | ASSERT(fsbno != NULLFSBLOCK); | |
704 | d = XFS_FSB_TO_DADDR(mp, fsbno); | |
705 | xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count); | |
706 | } | |
707 | ||
708 | /* | |
709 | * Read-ahead the block, don't wait for it, don't return a buffer. | |
710 | * Short-form addressing. | |
711 | */ | |
712 | /* ARGSUSED */ | |
713 | void | |
714 | xfs_btree_reada_bufs( | |
715 | xfs_mount_t *mp, /* file system mount point */ | |
716 | xfs_agnumber_t agno, /* allocation group number */ | |
717 | xfs_agblock_t agbno, /* allocation group block number */ | |
718 | xfs_extlen_t count) /* count of filesystem blocks */ | |
719 | { | |
720 | xfs_daddr_t d; | |
721 | ||
722 | ASSERT(agno != NULLAGNUMBER); | |
723 | ASSERT(agbno != NULLAGBLOCK); | |
724 | d = XFS_AGB_TO_DADDR(mp, agno, agbno); | |
725 | xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count); | |
726 | } | |
727 | ||
728 | /* | |
729 | * Read-ahead btree blocks, at the given level. | |
730 | * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA. | |
731 | */ | |
732 | int | |
733 | xfs_btree_readahead_core( | |
734 | xfs_btree_cur_t *cur, /* btree cursor */ | |
735 | int lev, /* level in btree */ | |
736 | int lr) /* left/right bits */ | |
737 | { | |
738 | xfs_alloc_block_t *a; | |
739 | xfs_bmbt_block_t *b; | |
740 | xfs_inobt_block_t *i; | |
741 | int rval = 0; | |
742 | ||
743 | ASSERT(cur->bc_bufs[lev] != NULL); | |
744 | cur->bc_ra[lev] |= lr; | |
745 | switch (cur->bc_btnum) { | |
746 | case XFS_BTNUM_BNO: | |
747 | case XFS_BTNUM_CNT: | |
748 | a = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[lev]); | |
16259e7d | 749 | if ((lr & XFS_BTCUR_LEFTRA) && be32_to_cpu(a->bb_leftsib) != NULLAGBLOCK) { |
1da177e4 | 750 | xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno, |
16259e7d | 751 | be32_to_cpu(a->bb_leftsib), 1); |
1da177e4 LT |
752 | rval++; |
753 | } | |
16259e7d | 754 | if ((lr & XFS_BTCUR_RIGHTRA) && be32_to_cpu(a->bb_rightsib) != NULLAGBLOCK) { |
1da177e4 | 755 | xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno, |
16259e7d | 756 | be32_to_cpu(a->bb_rightsib), 1); |
1da177e4 LT |
757 | rval++; |
758 | } | |
759 | break; | |
760 | case XFS_BTNUM_BMAP: | |
761 | b = XFS_BUF_TO_BMBT_BLOCK(cur->bc_bufs[lev]); | |
16259e7d CH |
762 | if ((lr & XFS_BTCUR_LEFTRA) && be64_to_cpu(b->bb_leftsib) != NULLDFSBNO) { |
763 | xfs_btree_reada_bufl(cur->bc_mp, be64_to_cpu(b->bb_leftsib), 1); | |
1da177e4 LT |
764 | rval++; |
765 | } | |
16259e7d CH |
766 | if ((lr & XFS_BTCUR_RIGHTRA) && be64_to_cpu(b->bb_rightsib) != NULLDFSBNO) { |
767 | xfs_btree_reada_bufl(cur->bc_mp, be64_to_cpu(b->bb_rightsib), 1); | |
1da177e4 LT |
768 | rval++; |
769 | } | |
770 | break; | |
771 | case XFS_BTNUM_INO: | |
772 | i = XFS_BUF_TO_INOBT_BLOCK(cur->bc_bufs[lev]); | |
16259e7d | 773 | if ((lr & XFS_BTCUR_LEFTRA) && be32_to_cpu(i->bb_leftsib) != NULLAGBLOCK) { |
169d6227 | 774 | xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno, |
16259e7d | 775 | be32_to_cpu(i->bb_leftsib), 1); |
1da177e4 LT |
776 | rval++; |
777 | } | |
16259e7d | 778 | if ((lr & XFS_BTCUR_RIGHTRA) && be32_to_cpu(i->bb_rightsib) != NULLAGBLOCK) { |
169d6227 | 779 | xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno, |
16259e7d | 780 | be32_to_cpu(i->bb_rightsib), 1); |
1da177e4 LT |
781 | rval++; |
782 | } | |
783 | break; | |
784 | default: | |
785 | ASSERT(0); | |
786 | } | |
787 | return rval; | |
788 | } | |
789 | ||
790 | /* | |
791 | * Set the buffer for level "lev" in the cursor to bp, releasing | |
792 | * any previous buffer. | |
793 | */ | |
794 | void | |
795 | xfs_btree_setbuf( | |
796 | xfs_btree_cur_t *cur, /* btree cursor */ | |
797 | int lev, /* level in btree */ | |
798 | xfs_buf_t *bp) /* new buffer to set */ | |
799 | { | |
800 | xfs_btree_block_t *b; /* btree block */ | |
801 | xfs_buf_t *obp; /* old buffer pointer */ | |
802 | ||
803 | obp = cur->bc_bufs[lev]; | |
804 | if (obp) | |
805 | xfs_trans_brelse(cur->bc_tp, obp); | |
806 | cur->bc_bufs[lev] = bp; | |
807 | cur->bc_ra[lev] = 0; | |
808 | if (!bp) | |
809 | return; | |
810 | b = XFS_BUF_TO_BLOCK(bp); | |
e99ab90d | 811 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { |
16259e7d | 812 | if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO) |
1da177e4 | 813 | cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA; |
16259e7d | 814 | if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO) |
1da177e4 LT |
815 | cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA; |
816 | } else { | |
16259e7d | 817 | if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK) |
1da177e4 | 818 | cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA; |
16259e7d | 819 | if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK) |
1da177e4 LT |
820 | cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA; |
821 | } | |
822 | } |