blob: 893e86f2ad57cea72dd9d81c61b058259d210a01 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dinode.h"
36#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_btree.h"
38#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_error.h"
40
41/*
42 * Cursor allocation zone.
43 */
44kmem_zone_t *xfs_btree_cur_zone;
45
46/*
47 * Btree magic numbers.
48 */
Christoph Hellwigcdcf43332008-08-13 16:23:50 +100049const __uint32_t xfs_magics[XFS_BTNUM_MAX] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC
51};
52
53/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * External routines.
55 */
56
57#ifdef DEBUG
58/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * Debug routine: check that keys are in the right order.
60 */
61void
62xfs_btree_check_key(
63 xfs_btnum_t btnum, /* btree identifier */
64 void *ak1, /* pointer to left (lower) key */
65 void *ak2) /* pointer to right (higher) key */
66{
67 switch (btnum) {
68 case XFS_BTNUM_BNO: {
69 xfs_alloc_key_t *k1;
70 xfs_alloc_key_t *k2;
71
72 k1 = ak1;
73 k2 = ak2;
Christoph Hellwig16259e72005-11-02 15:11:25 +110074 ASSERT(be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 break;
76 }
77 case XFS_BTNUM_CNT: {
78 xfs_alloc_key_t *k1;
79 xfs_alloc_key_t *k2;
80
81 k1 = ak1;
82 k2 = ak2;
Christoph Hellwig16259e72005-11-02 15:11:25 +110083 ASSERT(be32_to_cpu(k1->ar_blockcount) < be32_to_cpu(k2->ar_blockcount) ||
84 (k1->ar_blockcount == k2->ar_blockcount &&
85 be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 break;
87 }
88 case XFS_BTNUM_BMAP: {
89 xfs_bmbt_key_t *k1;
90 xfs_bmbt_key_t *k2;
91
92 k1 = ak1;
93 k2 = ak2;
Christoph Hellwig8801bb92006-09-28 10:58:17 +100094 ASSERT(be64_to_cpu(k1->br_startoff) < be64_to_cpu(k2->br_startoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 break;
96 }
97 case XFS_BTNUM_INO: {
98 xfs_inobt_key_t *k1;
99 xfs_inobt_key_t *k2;
100
101 k1 = ak1;
102 k2 = ak2;
Christoph Hellwig61a25842006-09-28 10:57:04 +1000103 ASSERT(be32_to_cpu(k1->ir_startino) < be32_to_cpu(k2->ir_startino));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 break;
105 }
106 default:
107 ASSERT(0);
108 }
109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * Debug routine: check that records are in the right order.
113 */
114void
115xfs_btree_check_rec(
116 xfs_btnum_t btnum, /* btree identifier */
117 void *ar1, /* pointer to left (lower) record */
118 void *ar2) /* pointer to right (higher) record */
119{
120 switch (btnum) {
121 case XFS_BTNUM_BNO: {
122 xfs_alloc_rec_t *r1;
123 xfs_alloc_rec_t *r2;
124
125 r1 = ar1;
126 r2 = ar2;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100127 ASSERT(be32_to_cpu(r1->ar_startblock) +
128 be32_to_cpu(r1->ar_blockcount) <=
129 be32_to_cpu(r2->ar_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 break;
131 }
132 case XFS_BTNUM_CNT: {
133 xfs_alloc_rec_t *r1;
134 xfs_alloc_rec_t *r2;
135
136 r1 = ar1;
137 r2 = ar2;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100138 ASSERT(be32_to_cpu(r1->ar_blockcount) < be32_to_cpu(r2->ar_blockcount) ||
139 (r1->ar_blockcount == r2->ar_blockcount &&
140 be32_to_cpu(r1->ar_startblock) < be32_to_cpu(r2->ar_startblock)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 break;
142 }
143 case XFS_BTNUM_BMAP: {
144 xfs_bmbt_rec_t *r1;
145 xfs_bmbt_rec_t *r2;
146
147 r1 = ar1;
148 r2 = ar2;
149 ASSERT(xfs_bmbt_disk_get_startoff(r1) +
150 xfs_bmbt_disk_get_blockcount(r1) <=
151 xfs_bmbt_disk_get_startoff(r2));
152 break;
153 }
154 case XFS_BTNUM_INO: {
155 xfs_inobt_rec_t *r1;
156 xfs_inobt_rec_t *r2;
157
158 r1 = ar1;
159 r2 = ar2;
Christoph Hellwig61a25842006-09-28 10:57:04 +1000160 ASSERT(be32_to_cpu(r1->ir_startino) + XFS_INODES_PER_CHUNK <=
161 be32_to_cpu(r2->ir_startino));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 break;
163 }
164 default:
165 ASSERT(0);
166 }
167}
168#endif /* DEBUG */
169
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100170int /* error (0 or EFSCORRUPTED) */
171xfs_btree_check_lblock(
172 struct xfs_btree_cur *cur, /* btree cursor */
173 struct xfs_btree_lblock *block, /* btree long form block pointer */
174 int level, /* level of the btree block */
175 struct xfs_buf *bp) /* buffer for block, if any */
176{
177 int lblock_ok; /* block passes checks */
178 struct xfs_mount *mp; /* file system mount point */
179
180 mp = cur->bc_mp;
181 lblock_ok =
182 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
183 be16_to_cpu(block->bb_level) == level &&
184 be16_to_cpu(block->bb_numrecs) <=
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100185 cur->bc_ops->get_maxrecs(cur, level) &&
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100186 block->bb_leftsib &&
187 (be64_to_cpu(block->bb_leftsib) == NULLDFSBNO ||
188 XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_leftsib))) &&
189 block->bb_rightsib &&
190 (be64_to_cpu(block->bb_rightsib) == NULLDFSBNO ||
191 XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_rightsib)));
192 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
193 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
194 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
195 if (bp)
196 xfs_buftrace("LBTREE ERROR", bp);
197 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
198 mp);
199 return XFS_ERROR(EFSCORRUPTED);
200 }
201 return 0;
202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204int /* error (0 or EFSCORRUPTED) */
205xfs_btree_check_sblock(
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100206 struct xfs_btree_cur *cur, /* btree cursor */
207 struct xfs_btree_sblock *block, /* btree short form block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 int level, /* level of the btree block */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100209 struct xfs_buf *bp) /* buffer containing block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100211 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
212 struct xfs_agf *agf; /* ag. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 xfs_agblock_t agflen; /* native ag. freespace length */
214 int sblock_ok; /* block passes checks */
215
216 agbp = cur->bc_private.a.agbp;
217 agf = XFS_BUF_TO_AGF(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100218 agflen = be32_to_cpu(agf->agf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 sblock_ok =
Christoph Hellwig16259e72005-11-02 15:11:25 +1100220 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
221 be16_to_cpu(block->bb_level) == level &&
222 be16_to_cpu(block->bb_numrecs) <=
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100223 cur->bc_ops->get_maxrecs(cur, level) &&
Christoph Hellwig16259e72005-11-02 15:11:25 +1100224 (be32_to_cpu(block->bb_leftsib) == NULLAGBLOCK ||
225 be32_to_cpu(block->bb_leftsib) < agflen) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 block->bb_leftsib &&
Christoph Hellwig16259e72005-11-02 15:11:25 +1100227 (be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK ||
228 be32_to_cpu(block->bb_rightsib) < agflen) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 block->bb_rightsib;
230 if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
231 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
232 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
233 if (bp)
234 xfs_buftrace("SBTREE ERROR", bp);
235 XFS_ERROR_REPORT("xfs_btree_check_sblock", XFS_ERRLEVEL_LOW,
236 cur->bc_mp);
237 return XFS_ERROR(EFSCORRUPTED);
238 }
239 return 0;
240}
241
242/*
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100243 * Debug routine: check that block header is ok.
244 */
245int
246xfs_btree_check_block(
247 struct xfs_btree_cur *cur, /* btree cursor */
248 struct xfs_btree_block *block, /* generic btree block pointer */
249 int level, /* level of the btree block */
250 struct xfs_buf *bp) /* buffer containing block, if any */
251{
252 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
253 return xfs_btree_check_lblock(cur,
254 (struct xfs_btree_lblock *)block, level, bp);
255 } else {
256 return xfs_btree_check_sblock(cur,
257 (struct xfs_btree_sblock *)block, level, bp);
258 }
259}
260
261/*
262 * Check that (long) pointer is ok.
263 */
264int /* error (0 or EFSCORRUPTED) */
265xfs_btree_check_lptr(
266 struct xfs_btree_cur *cur, /* btree cursor */
267 xfs_dfsbno_t bno, /* btree block disk address */
268 int level) /* btree block level */
269{
270 XFS_WANT_CORRUPTED_RETURN(
271 level > 0 &&
272 bno != NULLDFSBNO &&
273 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
274 return 0;
275}
276
277/*
278 * Check that (short) pointer is ok.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
280int /* error (0 or EFSCORRUPTED) */
281xfs_btree_check_sptr(
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100282 struct xfs_btree_cur *cur, /* btree cursor */
283 xfs_agblock_t bno, /* btree block disk address */
284 int level) /* btree block level */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100286 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 XFS_WANT_CORRUPTED_RETURN(
289 level > 0 &&
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100290 bno != NULLAGBLOCK &&
291 bno != 0 &&
292 bno < agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return 0;
294}
295
296/*
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100297 * Check that block ptr is ok.
298 */
299int /* error (0 or EFSCORRUPTED) */
300xfs_btree_check_ptr(
301 struct xfs_btree_cur *cur, /* btree cursor */
302 union xfs_btree_ptr *ptr, /* btree block disk address */
303 int index, /* offset from ptr to check */
304 int level) /* btree block level */
305{
306 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
307 return xfs_btree_check_lptr(cur,
308 be64_to_cpu((&ptr->l)[index]), level);
309 } else {
310 return xfs_btree_check_sptr(cur,
311 be32_to_cpu((&ptr->s)[index]), level);
312 }
313}
314
315/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * Delete the btree cursor.
317 */
318void
319xfs_btree_del_cursor(
320 xfs_btree_cur_t *cur, /* btree cursor */
321 int error) /* del because of error */
322{
323 int i; /* btree level */
324
325 /*
326 * Clear the buffer pointers, and release the buffers.
327 * If we're doing this in the face of an error, we
328 * need to make sure to inspect all of the entries
329 * in the bc_bufs array for buffers to be unlocked.
330 * This is because some of the btree code works from
331 * level n down to 0, and if we get an error along
332 * the way we won't have initialized all the entries
333 * down to 0.
334 */
335 for (i = 0; i < cur->bc_nlevels; i++) {
336 if (cur->bc_bufs[i])
337 xfs_btree_setbuf(cur, i, NULL);
338 else if (!error)
339 break;
340 }
341 /*
342 * Can't free a bmap cursor without having dealt with the
343 * allocated indirect blocks' accounting.
344 */
345 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
346 cur->bc_private.b.allocated == 0);
347 /*
348 * Free the cursor.
349 */
350 kmem_zone_free(xfs_btree_cur_zone, cur);
351}
352
353/*
354 * Duplicate the btree cursor.
355 * Allocate a new one, copy the record, re-get the buffers.
356 */
357int /* error */
358xfs_btree_dup_cursor(
359 xfs_btree_cur_t *cur, /* input cursor */
360 xfs_btree_cur_t **ncur) /* output cursor */
361{
362 xfs_buf_t *bp; /* btree block's buffer pointer */
363 int error; /* error return value */
364 int i; /* level number of btree block */
365 xfs_mount_t *mp; /* mount structure for filesystem */
366 xfs_btree_cur_t *new; /* new cursor value */
367 xfs_trans_t *tp; /* transaction pointer, can be NULL */
368
369 tp = cur->bc_tp;
370 mp = cur->bc_mp;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /*
373 * Allocate a new cursor like the old one.
374 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100375 new = cur->bc_ops->dup_cursor(cur);
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /*
378 * Copy the record currently in the cursor.
379 */
380 new->bc_rec = cur->bc_rec;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /*
383 * For each level current, re-get the buffer and copy the ptr value.
384 */
385 for (i = 0; i < new->bc_nlevels; i++) {
386 new->bc_ptrs[i] = cur->bc_ptrs[i];
387 new->bc_ra[i] = cur->bc_ra[i];
388 if ((bp = cur->bc_bufs[i])) {
389 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
390 XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
391 xfs_btree_del_cursor(new, error);
392 *ncur = NULL;
393 return error;
394 }
395 new->bc_bufs[i] = bp;
396 ASSERT(bp);
397 ASSERT(!XFS_BUF_GETERROR(bp));
398 } else
399 new->bc_bufs[i] = NULL;
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 *ncur = new;
402 return 0;
403}
404
405/*
Christoph Hellwig8186e512008-10-30 16:54:22 +1100406 * Get a the root block which is stored in the inode.
407 *
408 * For now this btree implementation assumes the btree root is always
409 * stored in the if_broot field of an inode fork.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
Christoph Hellwig8186e512008-10-30 16:54:22 +1100411STATIC struct xfs_btree_block *
412xfs_btree_get_iroot(
413 struct xfs_btree_cur *cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Christoph Hellwig8186e512008-10-30 16:54:22 +1100415 struct xfs_ifork *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Christoph Hellwig8186e512008-10-30 16:54:22 +1100417 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
418 return (struct xfs_btree_block *)ifp->if_broot;
419}
420
421/*
422 * Retrieve the block pointer from the cursor at the given level.
423 * This may be an inode btree root or from a buffer.
424 */
425STATIC struct xfs_btree_block * /* generic btree block pointer */
426xfs_btree_get_block(
427 struct xfs_btree_cur *cur, /* btree cursor */
428 int level, /* level in btree */
429 struct xfs_buf **bpp) /* buffer containing the block */
430{
431 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
432 (level == cur->bc_nlevels - 1)) {
433 *bpp = NULL;
434 return xfs_btree_get_iroot(cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Christoph Hellwig8186e512008-10-30 16:54:22 +1100436
437 *bpp = cur->bc_bufs[level];
438 return XFS_BUF_TO_BLOCK(*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441/*
442 * Get a buffer for the block, return it with no data read.
443 * Long-form addressing.
444 */
445xfs_buf_t * /* buffer for fsbno */
446xfs_btree_get_bufl(
447 xfs_mount_t *mp, /* file system mount point */
448 xfs_trans_t *tp, /* transaction pointer */
449 xfs_fsblock_t fsbno, /* file system block number */
450 uint lock) /* lock flags for get_buf */
451{
452 xfs_buf_t *bp; /* buffer pointer (return value) */
453 xfs_daddr_t d; /* real disk block address */
454
455 ASSERT(fsbno != NULLFSBLOCK);
456 d = XFS_FSB_TO_DADDR(mp, fsbno);
457 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
458 ASSERT(bp);
459 ASSERT(!XFS_BUF_GETERROR(bp));
460 return bp;
461}
462
463/*
464 * Get a buffer for the block, return it with no data read.
465 * Short-form addressing.
466 */
467xfs_buf_t * /* buffer for agno/agbno */
468xfs_btree_get_bufs(
469 xfs_mount_t *mp, /* file system mount point */
470 xfs_trans_t *tp, /* transaction pointer */
471 xfs_agnumber_t agno, /* allocation group number */
472 xfs_agblock_t agbno, /* allocation group block number */
473 uint lock) /* lock flags for get_buf */
474{
475 xfs_buf_t *bp; /* buffer pointer (return value) */
476 xfs_daddr_t d; /* real disk block address */
477
478 ASSERT(agno != NULLAGNUMBER);
479 ASSERT(agbno != NULLAGBLOCK);
480 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
481 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
482 ASSERT(bp);
483 ASSERT(!XFS_BUF_GETERROR(bp));
484 return bp;
485}
486
487/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 * Check for the cursor referring to the last block at the given level.
489 */
490int /* 1=is last block, 0=not last block */
491xfs_btree_islastblock(
492 xfs_btree_cur_t *cur, /* btree cursor */
493 int level) /* level to check */
494{
495 xfs_btree_block_t *block; /* generic btree block pointer */
496 xfs_buf_t *bp; /* buffer containing block */
497
498 block = xfs_btree_get_block(cur, level, &bp);
499 xfs_btree_check_block(cur, block, level, bp);
Christoph Hellwige99ab902008-10-30 16:54:33 +1100500 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwig16259e72005-11-02 15:11:25 +1100501 return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 else
Christoph Hellwig16259e72005-11-02 15:11:25 +1100503 return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506/*
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000507 * Change the cursor to point to the first record at the given level.
508 * Other levels are unaffected.
509 */
510int /* success=1, failure=0 */
511xfs_btree_firstrec(
512 xfs_btree_cur_t *cur, /* btree cursor */
513 int level) /* level to change */
514{
515 xfs_btree_block_t *block; /* generic btree block pointer */
516 xfs_buf_t *bp; /* buffer containing block */
517
518 /*
519 * Get the block pointer for this level.
520 */
521 block = xfs_btree_get_block(cur, level, &bp);
522 xfs_btree_check_block(cur, block, level, bp);
523 /*
524 * It's empty, there is no such record.
525 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100526 if (!block->bb_numrecs)
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000527 return 0;
528 /*
529 * Set the ptr value to 1, that's the first record/key.
530 */
531 cur->bc_ptrs[level] = 1;
532 return 1;
533}
534
535/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * Change the cursor to point to the last record in the current block
537 * at the given level. Other levels are unaffected.
538 */
539int /* success=1, failure=0 */
540xfs_btree_lastrec(
541 xfs_btree_cur_t *cur, /* btree cursor */
542 int level) /* level to change */
543{
544 xfs_btree_block_t *block; /* generic btree block pointer */
545 xfs_buf_t *bp; /* buffer containing block */
546
547 /*
548 * Get the block pointer for this level.
549 */
550 block = xfs_btree_get_block(cur, level, &bp);
551 xfs_btree_check_block(cur, block, level, bp);
552 /*
553 * It's empty, there is no such record.
554 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100555 if (!block->bb_numrecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return 0;
557 /*
558 * Set the ptr value to numrecs, that's the last record/key.
559 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100560 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return 1;
562}
563
564/*
565 * Compute first and last byte offsets for the fields given.
566 * Interprets the offsets table, which contains struct field offsets.
567 */
568void
569xfs_btree_offsets(
570 __int64_t fields, /* bitmask of fields */
571 const short *offsets, /* table of field offsets */
572 int nbits, /* number of bits to inspect */
573 int *first, /* output: first byte offset */
574 int *last) /* output: last byte offset */
575{
576 int i; /* current bit number */
577 __int64_t imask; /* mask for current bit number */
578
579 ASSERT(fields != 0);
580 /*
581 * Find the lowest bit, so the first byte offset.
582 */
583 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
584 if (imask & fields) {
585 *first = offsets[i];
586 break;
587 }
588 }
589 /*
590 * Find the highest bit, so the last byte offset.
591 */
592 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
593 if (imask & fields) {
594 *last = offsets[i + 1] - 1;
595 break;
596 }
597 }
598}
599
600/*
601 * Get a buffer for the block, return it read in.
602 * Long-form addressing.
603 */
604int /* error */
605xfs_btree_read_bufl(
606 xfs_mount_t *mp, /* file system mount point */
607 xfs_trans_t *tp, /* transaction pointer */
608 xfs_fsblock_t fsbno, /* file system block number */
609 uint lock, /* lock flags for read_buf */
610 xfs_buf_t **bpp, /* buffer for fsbno */
611 int refval) /* ref count value for buffer */
612{
613 xfs_buf_t *bp; /* return value */
614 xfs_daddr_t d; /* real disk block address */
615 int error;
616
617 ASSERT(fsbno != NULLFSBLOCK);
618 d = XFS_FSB_TO_DADDR(mp, fsbno);
619 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
620 mp->m_bsize, lock, &bp))) {
621 return error;
622 }
623 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
624 if (bp != NULL) {
625 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
626 }
627 *bpp = bp;
628 return 0;
629}
630
631/*
632 * Get a buffer for the block, return it read in.
633 * Short-form addressing.
634 */
635int /* error */
636xfs_btree_read_bufs(
637 xfs_mount_t *mp, /* file system mount point */
638 xfs_trans_t *tp, /* transaction pointer */
639 xfs_agnumber_t agno, /* allocation group number */
640 xfs_agblock_t agbno, /* allocation group block number */
641 uint lock, /* lock flags for read_buf */
642 xfs_buf_t **bpp, /* buffer for agno/agbno */
643 int refval) /* ref count value for buffer */
644{
645 xfs_buf_t *bp; /* return value */
646 xfs_daddr_t d; /* real disk block address */
647 int error;
648
649 ASSERT(agno != NULLAGNUMBER);
650 ASSERT(agbno != NULLAGBLOCK);
651 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
652 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
653 mp->m_bsize, lock, &bp))) {
654 return error;
655 }
656 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
657 if (bp != NULL) {
658 switch (refval) {
659 case XFS_ALLOC_BTREE_REF:
660 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
661 break;
662 case XFS_INO_BTREE_REF:
663 XFS_BUF_SET_VTYPE_REF(bp, B_FS_INOMAP, refval);
664 break;
665 }
666 }
667 *bpp = bp;
668 return 0;
669}
670
671/*
672 * Read-ahead the block, don't wait for it, don't return a buffer.
673 * Long-form addressing.
674 */
675/* ARGSUSED */
676void
677xfs_btree_reada_bufl(
678 xfs_mount_t *mp, /* file system mount point */
679 xfs_fsblock_t fsbno, /* file system block number */
680 xfs_extlen_t count) /* count of filesystem blocks */
681{
682 xfs_daddr_t d;
683
684 ASSERT(fsbno != NULLFSBLOCK);
685 d = XFS_FSB_TO_DADDR(mp, fsbno);
686 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
687}
688
689/*
690 * Read-ahead the block, don't wait for it, don't return a buffer.
691 * Short-form addressing.
692 */
693/* ARGSUSED */
694void
695xfs_btree_reada_bufs(
696 xfs_mount_t *mp, /* file system mount point */
697 xfs_agnumber_t agno, /* allocation group number */
698 xfs_agblock_t agbno, /* allocation group block number */
699 xfs_extlen_t count) /* count of filesystem blocks */
700{
701 xfs_daddr_t d;
702
703 ASSERT(agno != NULLAGNUMBER);
704 ASSERT(agbno != NULLAGBLOCK);
705 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
706 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
707}
708
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100709STATIC int
710xfs_btree_readahead_lblock(
711 struct xfs_btree_cur *cur,
712 int lr,
713 struct xfs_btree_block *block)
714{
715 int rval = 0;
716 xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
717 xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
718
719 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
720 xfs_btree_reada_bufl(cur->bc_mp, left, 1);
721 rval++;
722 }
723
724 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
725 xfs_btree_reada_bufl(cur->bc_mp, right, 1);
726 rval++;
727 }
728
729 return rval;
730}
731
732STATIC int
733xfs_btree_readahead_sblock(
734 struct xfs_btree_cur *cur,
735 int lr,
736 struct xfs_btree_block *block)
737{
738 int rval = 0;
739 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
740 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
741
742
743 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
744 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
745 left, 1);
746 rval++;
747 }
748
749 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
750 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
751 right, 1);
752 rval++;
753 }
754
755 return rval;
756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*
759 * Read-ahead btree blocks, at the given level.
760 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
761 */
762int
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100763xfs_btree_readahead(
764 struct xfs_btree_cur *cur, /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 int lev, /* level in btree */
766 int lr) /* left/right bits */
767{
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100768 struct xfs_btree_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100770 /*
771 * No readahead needed if we are at the root level and the
772 * btree root is stored in the inode.
773 */
774 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
775 (lev == cur->bc_nlevels - 1))
776 return 0;
777
778 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
779 return 0;
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 cur->bc_ra[lev] |= lr;
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100782 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
783
784 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
785 return xfs_btree_readahead_lblock(cur, lr, block);
786 return xfs_btree_readahead_sblock(cur, lr, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
789/*
790 * Set the buffer for level "lev" in the cursor to bp, releasing
791 * any previous buffer.
792 */
793void
794xfs_btree_setbuf(
795 xfs_btree_cur_t *cur, /* btree cursor */
796 int lev, /* level in btree */
797 xfs_buf_t *bp) /* new buffer to set */
798{
799 xfs_btree_block_t *b; /* btree block */
800 xfs_buf_t *obp; /* old buffer pointer */
801
802 obp = cur->bc_bufs[lev];
803 if (obp)
804 xfs_trans_brelse(cur->bc_tp, obp);
805 cur->bc_bufs[lev] = bp;
806 cur->bc_ra[lev] = 0;
807 if (!bp)
808 return;
809 b = XFS_BUF_TO_BLOCK(bp);
Christoph Hellwige99ab902008-10-30 16:54:33 +1100810 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100811 if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100813 if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
815 } else {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100816 if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100818 if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
820 }
821}