2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_inode_item.h"
38 #include "xfs_alloc.h"
39 #include "xfs_btree.h"
40 #include "xfs_btree_trace.h"
41 #include "xfs_ialloc.h"
42 #include "xfs_itable.h"
44 #include "xfs_error.h"
45 #include "xfs_quota.h"
48 * Prototypes for internal btree functions.
52 STATIC void xfs_bmbt_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
53 STATIC void xfs_bmbt_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
57 #define ENTRY XBT_ENTRY
58 #define ERROR XBT_ERROR
62 * Keep the XFS_BMBT_TRACE_ names around for now until all code using them
63 * is converted to be generic and thus switches to the XFS_BTREE_TRACE_ names.
65 #define XFS_BMBT_TRACE_ARGBI(c,b,i) \
66 XFS_BTREE_TRACE_ARGBI(c,b,i)
67 #define XFS_BMBT_TRACE_ARGBII(c,b,i,j) \
68 XFS_BTREE_TRACE_ARGBII(c,b,i,j)
69 #define XFS_BMBT_TRACE_ARGFFFI(c,o,b,i,j) \
70 XFS_BTREE_TRACE_ARGFFFI(c,o,b,i,j)
71 #define XFS_BMBT_TRACE_ARGI(c,i) \
72 XFS_BTREE_TRACE_ARGI(c,i)
73 #define XFS_BMBT_TRACE_ARGIFK(c,i,f,s) \
74 XFS_BTREE_TRACE_ARGIPK(c,i,(union xfs_btree_ptr)f,s)
75 #define XFS_BMBT_TRACE_ARGIFR(c,i,f,r) \
76 XFS_BTREE_TRACE_ARGIPR(c,i, \
77 (union xfs_btree_ptr)f, (union xfs_btree_rec *)r)
78 #define XFS_BMBT_TRACE_ARGIK(c,i,k) \
79 XFS_BTREE_TRACE_ARGIK(c,i,(union xfs_btree_key *)k)
80 #define XFS_BMBT_TRACE_CURSOR(c,s) \
81 XFS_BTREE_TRACE_CURSOR(c,s)
89 * Delete record pointed to by cur/level.
91 STATIC int /* error */
95 int *stat) /* success/failure */
97 xfs_bmbt_block_t *block; /* bmap btree block */
98 xfs_fsblock_t bno; /* fs-relative block number */
99 xfs_buf_t *bp; /* buffer for block */
100 int error; /* error return value */
101 int i; /* loop counter */
102 int j; /* temp state */
103 xfs_bmbt_key_t key; /* bmap btree key */
104 xfs_bmbt_key_t *kp=NULL; /* pointer to bmap btree key */
105 xfs_fsblock_t lbno; /* left sibling block number */
106 xfs_buf_t *lbp; /* left buffer pointer */
107 xfs_bmbt_block_t *left; /* left btree block */
108 xfs_bmbt_key_t *lkp; /* left btree key */
109 xfs_bmbt_ptr_t *lpp; /* left address pointer */
110 int lrecs=0; /* left record count */
111 xfs_bmbt_rec_t *lrp; /* left record pointer */
112 xfs_mount_t *mp; /* file system mount point */
113 xfs_bmbt_ptr_t *pp; /* pointer to bmap block addr */
114 int ptr; /* key/record index */
115 xfs_fsblock_t rbno; /* right sibling block number */
116 xfs_buf_t *rbp; /* right buffer pointer */
117 xfs_bmbt_block_t *right; /* right btree block */
118 xfs_bmbt_key_t *rkp; /* right btree key */
119 xfs_bmbt_rec_t *rp; /* pointer to bmap btree rec */
120 xfs_bmbt_ptr_t *rpp; /* right address pointer */
121 xfs_bmbt_block_t *rrblock; /* right-right btree block */
122 xfs_buf_t *rrbp; /* right-right buffer pointer */
123 int rrecs=0; /* right record count */
124 xfs_bmbt_rec_t *rrp; /* right record pointer */
125 xfs_btree_cur_t *tcur; /* temporary btree cursor */
126 int numrecs; /* temporary numrec count */
127 int numlrecs, numrrecs;
129 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
130 XFS_BMBT_TRACE_ARGI(cur, level);
131 ptr = cur->bc_ptrs[level];
134 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
138 block = xfs_bmbt_get_block(cur, level, &bp);
139 numrecs = be16_to_cpu(block->bb_numrecs);
141 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
142 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
147 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
151 XFS_STATS_INC(xs_bmbt_delrec);
153 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
154 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
156 for (i = ptr; i < numrecs; i++) {
157 if ((error = xfs_btree_check_lptr_disk(cur, pp[i], level))) {
158 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
164 memmove(&kp[ptr - 1], &kp[ptr],
165 (numrecs - ptr) * sizeof(*kp));
166 memmove(&pp[ptr - 1], &pp[ptr],
167 (numrecs - ptr) * sizeof(*pp));
168 xfs_bmbt_log_ptrs(cur, bp, ptr, numrecs - 1);
169 xfs_bmbt_log_keys(cur, bp, ptr, numrecs - 1);
172 rp = XFS_BMAP_REC_IADDR(block, 1, cur);
174 memmove(&rp[ptr - 1], &rp[ptr],
175 (numrecs - ptr) * sizeof(*rp));
176 xfs_bmbt_log_recs(cur, bp, ptr, numrecs - 1);
180 cpu_to_be64(xfs_bmbt_disk_get_startoff(rp));
185 block->bb_numrecs = cpu_to_be16(numrecs);
186 xfs_bmbt_log_block(cur, bp, XFS_BB_NUMRECS);
188 * We're at the root level.
189 * First, shrink the root block in-memory.
190 * Try to get rid of the next level down.
191 * If we can't then there's nothing left to do.
193 if (level == cur->bc_nlevels - 1) {
194 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
195 cur->bc_private.b.whichfork);
196 if ((error = xfs_btree_kill_iroot(cur))) {
197 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
200 if (level > 0 && (error = xfs_btree_decrement(cur, level, &j))) {
201 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
204 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
208 if (ptr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)kp, level + 1))) {
209 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
212 if (numrecs >= XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
213 if (level > 0 && (error = xfs_btree_decrement(cur, level, &j))) {
214 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
217 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
221 rbno = be64_to_cpu(block->bb_rightsib);
222 lbno = be64_to_cpu(block->bb_leftsib);
224 * One child of root, need to get a chance to copy its contents
225 * into the root and delete it. Can't go up to next level,
226 * there's nothing to delete there.
228 if (lbno == NULLFSBLOCK && rbno == NULLFSBLOCK &&
229 level == cur->bc_nlevels - 2) {
230 if ((error = xfs_btree_kill_iroot(cur))) {
231 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
234 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i))) {
235 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
238 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
242 ASSERT(rbno != NULLFSBLOCK || lbno != NULLFSBLOCK);
243 if ((error = xfs_btree_dup_cursor(cur, &tcur))) {
244 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
248 if (rbno != NULLFSBLOCK) {
249 i = xfs_btree_lastrec(tcur, level);
250 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
251 if ((error = xfs_btree_increment(tcur, level, &i))) {
252 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
255 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
256 i = xfs_btree_lastrec(tcur, level);
257 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
258 rbp = tcur->bc_bufs[level];
259 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
261 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
262 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
266 bno = be64_to_cpu(right->bb_leftsib);
267 if (be16_to_cpu(right->bb_numrecs) - 1 >=
268 XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
269 if ((error = xfs_btree_lshift(tcur, level, &i))) {
270 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
274 ASSERT(be16_to_cpu(block->bb_numrecs) >=
275 XFS_BMAP_BLOCK_IMINRECS(level, tcur));
276 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
279 if ((error = xfs_btree_decrement(cur,
281 XFS_BMBT_TRACE_CURSOR(cur,
286 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
291 rrecs = be16_to_cpu(right->bb_numrecs);
292 if (lbno != NULLFSBLOCK) {
293 i = xfs_btree_firstrec(tcur, level);
294 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
295 if ((error = xfs_btree_decrement(tcur, level, &i))) {
296 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
299 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
302 if (lbno != NULLFSBLOCK) {
303 i = xfs_btree_firstrec(tcur, level);
304 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
306 * decrement to last in block
308 if ((error = xfs_btree_decrement(tcur, level, &i))) {
309 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
312 i = xfs_btree_firstrec(tcur, level);
313 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
314 lbp = tcur->bc_bufs[level];
315 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
317 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
318 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
322 bno = be64_to_cpu(left->bb_rightsib);
323 if (be16_to_cpu(left->bb_numrecs) - 1 >=
324 XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
325 if ((error = xfs_btree_rshift(tcur, level, &i))) {
326 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
330 ASSERT(be16_to_cpu(block->bb_numrecs) >=
331 XFS_BMAP_BLOCK_IMINRECS(level, tcur));
332 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
336 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
341 lrecs = be16_to_cpu(left->bb_numrecs);
343 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
346 ASSERT(bno != NULLFSBLOCK);
347 if (lbno != NULLFSBLOCK &&
348 lrecs + be16_to_cpu(block->bb_numrecs) <= XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
352 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, lbno, 0, &lbp,
353 XFS_BMAP_BTREE_REF))) {
354 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
357 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
358 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
359 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
362 } else if (rbno != NULLFSBLOCK &&
363 rrecs + be16_to_cpu(block->bb_numrecs) <=
364 XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
368 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, rbno, 0, &rbp,
369 XFS_BMAP_BTREE_REF))) {
370 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
373 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
374 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
375 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
378 lrecs = be16_to_cpu(left->bb_numrecs);
380 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i))) {
381 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
384 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
388 numlrecs = be16_to_cpu(left->bb_numrecs);
389 numrrecs = be16_to_cpu(right->bb_numrecs);
391 lkp = XFS_BMAP_KEY_IADDR(left, numlrecs + 1, cur);
392 lpp = XFS_BMAP_PTR_IADDR(left, numlrecs + 1, cur);
393 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
394 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
396 for (i = 0; i < numrrecs; i++) {
397 if ((error = xfs_btree_check_lptr_disk(cur, rpp[i], level))) {
398 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
403 memcpy(lkp, rkp, numrrecs * sizeof(*lkp));
404 memcpy(lpp, rpp, numrrecs * sizeof(*lpp));
405 xfs_bmbt_log_keys(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
406 xfs_bmbt_log_ptrs(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
408 lrp = XFS_BMAP_REC_IADDR(left, numlrecs + 1, cur);
409 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
410 memcpy(lrp, rrp, numrrecs * sizeof(*lrp));
411 xfs_bmbt_log_recs(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
413 be16_add_cpu(&left->bb_numrecs, numrrecs);
414 left->bb_rightsib = right->bb_rightsib;
415 xfs_bmbt_log_block(cur, lbp, XFS_BB_RIGHTSIB | XFS_BB_NUMRECS);
416 if (be64_to_cpu(left->bb_rightsib) != NULLDFSBNO) {
417 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp,
418 be64_to_cpu(left->bb_rightsib),
419 0, &rrbp, XFS_BMAP_BTREE_REF))) {
420 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
423 rrblock = XFS_BUF_TO_BMBT_BLOCK(rrbp);
424 if ((error = xfs_btree_check_lblock(cur, rrblock, level, rrbp))) {
425 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
428 rrblock->bb_leftsib = cpu_to_be64(lbno);
429 xfs_bmbt_log_block(cur, rrbp, XFS_BB_LEFTSIB);
431 xfs_bmap_add_free(XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(rbp)), 1,
432 cur->bc_private.b.flist, mp);
433 cur->bc_private.b.ip->i_d.di_nblocks--;
434 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
435 XFS_TRANS_MOD_DQUOT_BYINO(mp, cur->bc_tp, cur->bc_private.b.ip,
436 XFS_TRANS_DQ_BCOUNT, -1L);
437 xfs_trans_binval(cur->bc_tp, rbp);
439 cur->bc_bufs[level] = lbp;
440 cur->bc_ptrs[level] += lrecs;
441 cur->bc_ra[level] = 0;
442 } else if ((error = xfs_btree_increment(cur, level + 1, &i))) {
443 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
447 cur->bc_ptrs[level]--;
448 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
454 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
459 * Log key values from the btree block.
463 xfs_btree_cur_t *cur,
470 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
471 XFS_BMBT_TRACE_ARGBII(cur, bp, kfirst, klast);
474 xfs_bmbt_block_t *block;
479 block = XFS_BUF_TO_BMBT_BLOCK(bp);
480 kp = XFS_BMAP_KEY_DADDR(block, 1, cur);
481 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
482 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
483 xfs_trans_log_buf(tp, bp, first, last);
487 ip = cur->bc_private.b.ip;
488 xfs_trans_log_inode(tp, ip,
489 XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
491 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
495 * Log pointer values from the btree block.
499 xfs_btree_cur_t *cur,
506 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
507 XFS_BMBT_TRACE_ARGBII(cur, bp, pfirst, plast);
510 xfs_bmbt_block_t *block;
515 block = XFS_BUF_TO_BMBT_BLOCK(bp);
516 pp = XFS_BMAP_PTR_DADDR(block, 1, cur);
517 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
518 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
519 xfs_trans_log_buf(tp, bp, first, last);
523 ip = cur->bc_private.b.ip;
524 xfs_trans_log_inode(tp, ip,
525 XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
527 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
531 * Determine the extent state.
540 ASSERT(blks != 0); /* saved for DMIG */
541 return XFS_EXT_UNWRITTEN;
547 * Convert on-disk form of btree root to in-memory form.
551 xfs_bmdr_block_t *dblock,
553 xfs_bmbt_block_t *rblock,
562 rblock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
563 rblock->bb_level = dblock->bb_level;
564 ASSERT(be16_to_cpu(rblock->bb_level) > 0);
565 rblock->bb_numrecs = dblock->bb_numrecs;
566 rblock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
567 rblock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
568 dmxr = (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen, xfs_bmdr, 0);
569 fkp = XFS_BTREE_KEY_ADDR(xfs_bmdr, dblock, 1);
570 tkp = XFS_BMAP_BROOT_KEY_ADDR(rblock, 1, rblocklen);
571 fpp = XFS_BTREE_PTR_ADDR(xfs_bmdr, dblock, 1, dmxr);
572 tpp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, rblocklen);
573 dmxr = be16_to_cpu(dblock->bb_numrecs);
574 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
575 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
579 * Delete the record pointed to by cur.
583 xfs_btree_cur_t *cur,
584 int *stat) /* success/failure */
586 int error; /* error return value */
590 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
591 for (level = 0, i = 2; i == 2; level++) {
592 if ((error = xfs_bmbt_delrec(cur, level, &i))) {
593 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
598 for (level = 1; level < cur->bc_nlevels; level++) {
599 if (cur->bc_ptrs[level] == 0) {
600 if ((error = xfs_btree_decrement(cur, level,
602 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
609 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
615 * Convert a compressed bmap extent record to an uncompressed form.
616 * This code must be in sync with the routines xfs_bmbt_get_startoff,
617 * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
629 ext_flag = (int)(l0 >> (64 - BMBT_EXNTFLAG_BITLEN));
630 s->br_startoff = ((xfs_fileoff_t)l0 &
631 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
633 s->br_startblock = (((xfs_fsblock_t)l0 & XFS_MASK64LO(9)) << 43) |
634 (((xfs_fsblock_t)l1) >> 21);
640 b = (((xfs_dfsbno_t)l0 & XFS_MASK64LO(9)) << 43) |
641 (((xfs_dfsbno_t)l1) >> 21);
642 ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
643 s->br_startblock = (xfs_fsblock_t)b;
646 s->br_startblock = (xfs_fsblock_t)(((xfs_dfsbno_t)l1) >> 21);
648 #endif /* XFS_BIG_BLKNOS */
649 s->br_blockcount = (xfs_filblks_t)(l1 & XFS_MASK64LO(21));
650 /* This is xfs_extent_state() in-line */
652 ASSERT(s->br_blockcount != 0); /* saved for DMIG */
653 st = XFS_EXT_UNWRITTEN;
661 xfs_bmbt_rec_host_t *r,
664 __xfs_bmbt_get_all(r->l0, r->l1, s);
668 * Get the block pointer for the given level of the cursor.
669 * Fill in the buffer pointer, if applicable.
673 xfs_btree_cur_t *cur,
678 xfs_bmbt_block_t *rval;
680 if (level < cur->bc_nlevels - 1) {
681 *bpp = cur->bc_bufs[level];
682 rval = XFS_BUF_TO_BMBT_BLOCK(*bpp);
685 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
686 cur->bc_private.b.whichfork);
687 rval = ifp->if_broot;
693 * Extract the blockcount field from an in memory bmap extent record.
696 xfs_bmbt_get_blockcount(
697 xfs_bmbt_rec_host_t *r)
699 return (xfs_filblks_t)(r->l1 & XFS_MASK64LO(21));
703 * Extract the startblock field from an in memory bmap extent record.
706 xfs_bmbt_get_startblock(
707 xfs_bmbt_rec_host_t *r)
710 return (((xfs_fsblock_t)r->l0 & XFS_MASK64LO(9)) << 43) |
711 (((xfs_fsblock_t)r->l1) >> 21);
716 b = (((xfs_dfsbno_t)r->l0 & XFS_MASK64LO(9)) << 43) |
717 (((xfs_dfsbno_t)r->l1) >> 21);
718 ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
719 return (xfs_fsblock_t)b;
721 return (xfs_fsblock_t)(((xfs_dfsbno_t)r->l1) >> 21);
723 #endif /* XFS_BIG_BLKNOS */
727 * Extract the startoff field from an in memory bmap extent record.
730 xfs_bmbt_get_startoff(
731 xfs_bmbt_rec_host_t *r)
733 return ((xfs_fileoff_t)r->l0 &
734 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
739 xfs_bmbt_rec_host_t *r)
743 ext_flag = (int)((r->l0) >> (64 - BMBT_EXNTFLAG_BITLEN));
744 return xfs_extent_state(xfs_bmbt_get_blockcount(r),
748 /* Endian flipping versions of the bmbt extraction functions */
750 xfs_bmbt_disk_get_all(
754 __xfs_bmbt_get_all(be64_to_cpu(r->l0), be64_to_cpu(r->l1), s);
758 * Extract the blockcount field from an on disk bmap extent record.
761 xfs_bmbt_disk_get_blockcount(
764 return (xfs_filblks_t)(be64_to_cpu(r->l1) & XFS_MASK64LO(21));
768 * Extract the startoff field from a disk format bmap extent record.
771 xfs_bmbt_disk_get_startoff(
774 return ((xfs_fileoff_t)be64_to_cpu(r->l0) &
775 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
779 * Log fields from the btree block header.
783 xfs_btree_cur_t *cur,
790 static const short offsets[] = {
791 offsetof(xfs_bmbt_block_t, bb_magic),
792 offsetof(xfs_bmbt_block_t, bb_level),
793 offsetof(xfs_bmbt_block_t, bb_numrecs),
794 offsetof(xfs_bmbt_block_t, bb_leftsib),
795 offsetof(xfs_bmbt_block_t, bb_rightsib),
796 sizeof(xfs_bmbt_block_t)
799 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
800 XFS_BMBT_TRACE_ARGBI(cur, bp, fields);
803 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first,
805 xfs_trans_log_buf(tp, bp, first, last);
807 xfs_trans_log_inode(tp, cur->bc_private.b.ip,
808 XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
809 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
813 * Log record values from the btree block.
817 xfs_btree_cur_t *cur,
822 xfs_bmbt_block_t *block;
828 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
829 XFS_BMBT_TRACE_ARGBII(cur, bp, rfirst, rlast);
832 block = XFS_BUF_TO_BMBT_BLOCK(bp);
833 rp = XFS_BMAP_REC_DADDR(block, 1, cur);
834 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
835 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
836 xfs_trans_log_buf(tp, bp, first, last);
837 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
841 * Set all the fields in a bmap extent record from the arguments.
845 xfs_bmbt_rec_host_t *r,
846 xfs_fileoff_t startoff,
847 xfs_fsblock_t startblock,
848 xfs_filblks_t blockcount,
851 int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
853 ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN);
854 ASSERT((startoff & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
855 ASSERT((blockcount & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
858 ASSERT((startblock & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
860 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
861 ((xfs_bmbt_rec_base_t)startoff << 9) |
862 ((xfs_bmbt_rec_base_t)startblock >> 43);
863 r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) |
864 ((xfs_bmbt_rec_base_t)blockcount &
865 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
866 #else /* !XFS_BIG_BLKNOS */
867 if (ISNULLSTARTBLOCK(startblock)) {
868 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
869 ((xfs_bmbt_rec_base_t)startoff << 9) |
870 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
871 r->l1 = XFS_MASK64HI(11) |
872 ((xfs_bmbt_rec_base_t)startblock << 21) |
873 ((xfs_bmbt_rec_base_t)blockcount &
874 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
876 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
877 ((xfs_bmbt_rec_base_t)startoff << 9);
878 r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) |
879 ((xfs_bmbt_rec_base_t)blockcount &
880 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
882 #endif /* XFS_BIG_BLKNOS */
886 * Set all the fields in a bmap extent record from the uncompressed form.
890 xfs_bmbt_rec_host_t *r,
893 xfs_bmbt_set_allf(r, s->br_startoff, s->br_startblock,
894 s->br_blockcount, s->br_state);
899 * Set all the fields in a disk format bmap extent record from the arguments.
902 xfs_bmbt_disk_set_allf(
904 xfs_fileoff_t startoff,
905 xfs_fsblock_t startblock,
906 xfs_filblks_t blockcount,
909 int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
911 ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN);
912 ASSERT((startoff & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
913 ASSERT((blockcount & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
916 ASSERT((startblock & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
919 ((xfs_bmbt_rec_base_t)extent_flag << 63) |
920 ((xfs_bmbt_rec_base_t)startoff << 9) |
921 ((xfs_bmbt_rec_base_t)startblock >> 43));
923 ((xfs_bmbt_rec_base_t)startblock << 21) |
924 ((xfs_bmbt_rec_base_t)blockcount &
925 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
926 #else /* !XFS_BIG_BLKNOS */
927 if (ISNULLSTARTBLOCK(startblock)) {
929 ((xfs_bmbt_rec_base_t)extent_flag << 63) |
930 ((xfs_bmbt_rec_base_t)startoff << 9) |
931 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
932 r->l1 = cpu_to_be64(XFS_MASK64HI(11) |
933 ((xfs_bmbt_rec_base_t)startblock << 21) |
934 ((xfs_bmbt_rec_base_t)blockcount &
935 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
938 ((xfs_bmbt_rec_base_t)extent_flag << 63) |
939 ((xfs_bmbt_rec_base_t)startoff << 9));
941 ((xfs_bmbt_rec_base_t)startblock << 21) |
942 ((xfs_bmbt_rec_base_t)blockcount &
943 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
945 #endif /* XFS_BIG_BLKNOS */
949 * Set all the fields in a bmap extent record from the uncompressed form.
952 xfs_bmbt_disk_set_all(
956 xfs_bmbt_disk_set_allf(r, s->br_startoff, s->br_startblock,
957 s->br_blockcount, s->br_state);
961 * Set the blockcount field in a bmap extent record.
964 xfs_bmbt_set_blockcount(
965 xfs_bmbt_rec_host_t *r,
968 ASSERT((v & XFS_MASK64HI(43)) == 0);
969 r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(43)) |
970 (xfs_bmbt_rec_base_t)(v & XFS_MASK64LO(21));
974 * Set the startblock field in a bmap extent record.
977 xfs_bmbt_set_startblock(
978 xfs_bmbt_rec_host_t *r,
982 ASSERT((v & XFS_MASK64HI(12)) == 0);
983 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(55)) |
984 (xfs_bmbt_rec_base_t)(v >> 43);
985 r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)) |
986 (xfs_bmbt_rec_base_t)(v << 21);
987 #else /* !XFS_BIG_BLKNOS */
988 if (ISNULLSTARTBLOCK(v)) {
989 r->l0 |= (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
990 r->l1 = (xfs_bmbt_rec_base_t)XFS_MASK64HI(11) |
991 ((xfs_bmbt_rec_base_t)v << 21) |
992 (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
994 r->l0 &= ~(xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
995 r->l1 = ((xfs_bmbt_rec_base_t)v << 21) |
996 (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
998 #endif /* XFS_BIG_BLKNOS */
1002 * Set the startoff field in a bmap extent record.
1005 xfs_bmbt_set_startoff(
1006 xfs_bmbt_rec_host_t *r,
1009 ASSERT((v & XFS_MASK64HI(9)) == 0);
1010 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t) XFS_MASK64HI(1)) |
1011 ((xfs_bmbt_rec_base_t)v << 9) |
1012 (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
1016 * Set the extent state field in a bmap extent record.
1020 xfs_bmbt_rec_host_t *r,
1023 ASSERT(v == XFS_EXT_NORM || v == XFS_EXT_UNWRITTEN);
1024 if (v == XFS_EXT_NORM)
1025 r->l0 &= XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN);
1027 r->l0 |= XFS_MASK64HI(BMBT_EXNTFLAG_BITLEN);
1031 * Convert in-memory form of btree root to on-disk form.
1035 xfs_bmbt_block_t *rblock,
1037 xfs_bmdr_block_t *dblock,
1041 xfs_bmbt_key_t *fkp;
1043 xfs_bmbt_key_t *tkp;
1046 ASSERT(be32_to_cpu(rblock->bb_magic) == XFS_BMAP_MAGIC);
1047 ASSERT(be64_to_cpu(rblock->bb_leftsib) == NULLDFSBNO);
1048 ASSERT(be64_to_cpu(rblock->bb_rightsib) == NULLDFSBNO);
1049 ASSERT(be16_to_cpu(rblock->bb_level) > 0);
1050 dblock->bb_level = rblock->bb_level;
1051 dblock->bb_numrecs = rblock->bb_numrecs;
1052 dmxr = (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen, xfs_bmdr, 0);
1053 fkp = XFS_BMAP_BROOT_KEY_ADDR(rblock, 1, rblocklen);
1054 tkp = XFS_BTREE_KEY_ADDR(xfs_bmdr, dblock, 1);
1055 fpp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, rblocklen);
1056 tpp = XFS_BTREE_PTR_ADDR(xfs_bmdr, dblock, 1, dmxr);
1057 dmxr = be16_to_cpu(dblock->bb_numrecs);
1058 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
1059 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
1063 * Check extent records, which have just been read, for
1064 * any bit in the extent flag field. ASSERT on debug
1065 * kernels, as this condition should not occur.
1066 * Return an error condition (1) if any flags found,
1067 * otherwise return 0.
1071 xfs_check_nostate_extents(
1076 for (; num > 0; num--, idx++) {
1077 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1079 (64 - BMBT_EXNTFLAG_BITLEN)) != 0) {
1088 STATIC struct xfs_btree_cur *
1089 xfs_bmbt_dup_cursor(
1090 struct xfs_btree_cur *cur)
1092 struct xfs_btree_cur *new;
1094 new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp,
1095 cur->bc_private.b.ip, cur->bc_private.b.whichfork);
1098 * Copy the firstblock, flist, and flags values,
1099 * since init cursor doesn't get them.
1101 new->bc_private.b.firstblock = cur->bc_private.b.firstblock;
1102 new->bc_private.b.flist = cur->bc_private.b.flist;
1103 new->bc_private.b.flags = cur->bc_private.b.flags;
1109 xfs_bmbt_update_cursor(
1110 struct xfs_btree_cur *src,
1111 struct xfs_btree_cur *dst)
1113 ASSERT((dst->bc_private.b.firstblock != NULLFSBLOCK) ||
1114 (dst->bc_private.b.ip->i_d.di_flags & XFS_DIFLAG_REALTIME));
1115 ASSERT(dst->bc_private.b.flist == src->bc_private.b.flist);
1117 dst->bc_private.b.allocated += src->bc_private.b.allocated;
1118 dst->bc_private.b.firstblock = src->bc_private.b.firstblock;
1120 src->bc_private.b.allocated = 0;
1124 xfs_bmbt_alloc_block(
1125 struct xfs_btree_cur *cur,
1126 union xfs_btree_ptr *start,
1127 union xfs_btree_ptr *new,
1131 xfs_alloc_arg_t args; /* block allocation args */
1132 int error; /* error return value */
1134 memset(&args, 0, sizeof(args));
1135 args.tp = cur->bc_tp;
1136 args.mp = cur->bc_mp;
1137 args.fsbno = cur->bc_private.b.firstblock;
1138 args.firstblock = args.fsbno;
1140 if (args.fsbno == NULLFSBLOCK) {
1141 args.fsbno = be64_to_cpu(start->l);
1142 args.type = XFS_ALLOCTYPE_START_BNO;
1144 * Make sure there is sufficient room left in the AG to
1145 * complete a full tree split for an extent insert. If
1146 * we are converting the middle part of an extent then
1147 * we may need space for two tree splits.
1149 * We are relying on the caller to make the correct block
1150 * reservation for this operation to succeed. If the
1151 * reservation amount is insufficient then we may fail a
1152 * block allocation here and corrupt the filesystem.
1154 args.minleft = xfs_trans_get_block_res(args.tp);
1155 } else if (cur->bc_private.b.flist->xbf_low) {
1156 args.type = XFS_ALLOCTYPE_START_BNO;
1158 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1161 args.minlen = args.maxlen = args.prod = 1;
1162 args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
1163 if (!args.wasdel && xfs_trans_get_block_res(args.tp) == 0) {
1164 error = XFS_ERROR(ENOSPC);
1167 error = xfs_alloc_vextent(&args);
1171 if (args.fsbno == NULLFSBLOCK && args.minleft) {
1173 * Could not find an AG with enough free space to satisfy
1174 * a full btree split. Try again without minleft and if
1175 * successful activate the lowspace algorithm.
1178 args.type = XFS_ALLOCTYPE_FIRST_AG;
1180 error = xfs_alloc_vextent(&args);
1183 cur->bc_private.b.flist->xbf_low = 1;
1185 if (args.fsbno == NULLFSBLOCK) {
1186 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1190 ASSERT(args.len == 1);
1191 cur->bc_private.b.firstblock = args.fsbno;
1192 cur->bc_private.b.allocated++;
1193 cur->bc_private.b.ip->i_d.di_nblocks++;
1194 xfs_trans_log_inode(args.tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
1195 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, args.tp, cur->bc_private.b.ip,
1196 XFS_TRANS_DQ_BCOUNT, 1L);
1198 new->l = cpu_to_be64(args.fsbno);
1200 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1205 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1210 xfs_bmbt_free_block(
1211 struct xfs_btree_cur *cur,
1214 struct xfs_mount *mp = cur->bc_mp;
1215 struct xfs_inode *ip = cur->bc_private.b.ip;
1216 struct xfs_trans *tp = cur->bc_tp;
1217 xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp));
1219 xfs_bmap_add_free(fsbno, 1, cur->bc_private.b.flist, mp);
1220 ip->i_d.di_nblocks--;
1222 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1223 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
1224 xfs_trans_binval(tp, bp);
1229 xfs_bmbt_get_maxrecs(
1230 struct xfs_btree_cur *cur,
1233 return XFS_BMAP_BLOCK_IMAXRECS(level, cur);
1237 * Get the maximum records we could store in the on-disk format.
1239 * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
1240 * for the root node this checks the available space in the dinode fork
1241 * so that we can resize the in-memory buffer to match it. After a
1242 * resize to the maximum size this function returns the same value
1243 * as xfs_bmbt_get_maxrecs for the root node, too.
1246 xfs_bmbt_get_dmaxrecs(
1247 struct xfs_btree_cur *cur,
1250 return XFS_BMAP_BLOCK_DMAXRECS(level, cur);
1254 xfs_bmbt_init_key_from_rec(
1255 union xfs_btree_key *key,
1256 union xfs_btree_rec *rec)
1258 key->bmbt.br_startoff =
1259 cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt));
1263 xfs_bmbt_init_rec_from_key(
1264 union xfs_btree_key *key,
1265 union xfs_btree_rec *rec)
1267 ASSERT(key->bmbt.br_startoff != 0);
1269 xfs_bmbt_disk_set_allf(&rec->bmbt, be64_to_cpu(key->bmbt.br_startoff),
1270 0, 0, XFS_EXT_NORM);
1274 xfs_bmbt_init_rec_from_cur(
1275 struct xfs_btree_cur *cur,
1276 union xfs_btree_rec *rec)
1278 xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b);
1282 xfs_bmbt_init_ptr_from_cur(
1283 struct xfs_btree_cur *cur,
1284 union xfs_btree_ptr *ptr)
1291 struct xfs_btree_cur *cur,
1292 union xfs_btree_key *key)
1294 return (__int64_t)be64_to_cpu(key->bmbt.br_startoff) -
1295 cur->bc_rec.b.br_startoff;
1298 #ifdef XFS_BTREE_TRACE
1299 ktrace_t *xfs_bmbt_trace_buf;
1302 xfs_bmbt_trace_enter(
1303 struct xfs_btree_cur *cur,
1320 struct xfs_inode *ip = cur->bc_private.b.ip;
1321 int whichfork = cur->bc_private.b.whichfork;
1323 ktrace_enter(xfs_bmbt_trace_buf,
1324 (void *)((__psint_t)type | (whichfork << 8) | (line << 16)),
1325 (void *)func, (void *)s, (void *)ip, (void *)cur,
1326 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
1327 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
1328 (void *)a8, (void *)a9, (void *)a10);
1329 ktrace_enter(ip->i_btrace,
1330 (void *)((__psint_t)type | (whichfork << 8) | (line << 16)),
1331 (void *)func, (void *)s, (void *)ip, (void *)cur,
1332 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
1333 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
1334 (void *)a8, (void *)a9, (void *)a10);
1338 xfs_bmbt_trace_cursor(
1339 struct xfs_btree_cur *cur,
1344 struct xfs_bmbt_rec_host r;
1346 xfs_bmbt_set_all(&r, &cur->bc_rec.b);
1348 *s0 = (cur->bc_nlevels << 24) |
1349 (cur->bc_private.b.flags << 16) |
1350 cur->bc_private.b.allocated;
1357 struct xfs_btree_cur *cur,
1358 union xfs_btree_key *key,
1362 *l0 = be64_to_cpu(key->bmbt.br_startoff);
1367 xfs_bmbt_trace_record(
1368 struct xfs_btree_cur *cur,
1369 union xfs_btree_rec *rec,
1374 struct xfs_bmbt_irec irec;
1376 xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
1377 *l0 = irec.br_startoff;
1378 *l1 = irec.br_startblock;
1379 *l2 = irec.br_blockcount;
1381 #endif /* XFS_BTREE_TRACE */
1383 static const struct xfs_btree_ops xfs_bmbt_ops = {
1384 .rec_len = sizeof(xfs_bmbt_rec_t),
1385 .key_len = sizeof(xfs_bmbt_key_t),
1387 .dup_cursor = xfs_bmbt_dup_cursor,
1388 .update_cursor = xfs_bmbt_update_cursor,
1389 .alloc_block = xfs_bmbt_alloc_block,
1390 .free_block = xfs_bmbt_free_block,
1391 .get_maxrecs = xfs_bmbt_get_maxrecs,
1392 .get_dmaxrecs = xfs_bmbt_get_dmaxrecs,
1393 .init_key_from_rec = xfs_bmbt_init_key_from_rec,
1394 .init_rec_from_key = xfs_bmbt_init_rec_from_key,
1395 .init_rec_from_cur = xfs_bmbt_init_rec_from_cur,
1396 .init_ptr_from_cur = xfs_bmbt_init_ptr_from_cur,
1397 .key_diff = xfs_bmbt_key_diff,
1399 #ifdef XFS_BTREE_TRACE
1400 .trace_enter = xfs_bmbt_trace_enter,
1401 .trace_cursor = xfs_bmbt_trace_cursor,
1402 .trace_key = xfs_bmbt_trace_key,
1403 .trace_record = xfs_bmbt_trace_record,
1408 * Allocate a new bmap btree cursor.
1410 struct xfs_btree_cur * /* new bmap btree cursor */
1411 xfs_bmbt_init_cursor(
1412 struct xfs_mount *mp, /* file system mount point */
1413 struct xfs_trans *tp, /* transaction pointer */
1414 struct xfs_inode *ip, /* inode owning the btree */
1415 int whichfork) /* data or attr fork */
1417 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1418 struct xfs_btree_cur *cur;
1420 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
1424 cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
1425 cur->bc_btnum = XFS_BTNUM_BMAP;
1426 cur->bc_blocklog = mp->m_sb.sb_blocklog;
1428 cur->bc_ops = &xfs_bmbt_ops;
1429 cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE;
1431 cur->bc_private.b.forksize = XFS_IFORK_SIZE(ip, whichfork);
1432 cur->bc_private.b.ip = ip;
1433 cur->bc_private.b.firstblock = NULLFSBLOCK;
1434 cur->bc_private.b.flist = NULL;
1435 cur->bc_private.b.allocated = 0;
1436 cur->bc_private.b.flags = 0;
1437 cur->bc_private.b.whichfork = whichfork;