[XFS] move xfs_bmbt_killroot to common code
[safe/jmp/linux-2.6] / fs / xfs / xfs_bmap_btree.c
1 /*
2  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.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"
43 #include "xfs_bmap.h"
44 #include "xfs_error.h"
45 #include "xfs_quota.h"
46
47 /*
48  * Prototypes for internal btree functions.
49  */
50
51
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);
54
55 #undef EXIT
56
57 #define ENTRY   XBT_ENTRY
58 #define ERROR   XBT_ERROR
59 #define EXIT    XBT_EXIT
60
61 /*
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.
64  */
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)
82
83
84 /*
85  * Internal functions.
86  */
87
88 /*
89  * Delete record pointed to by cur/level.
90  */
91 STATIC int                                      /* error */
92 xfs_bmbt_delrec(
93         xfs_btree_cur_t         *cur,
94         int                     level,
95         int                     *stat)          /* success/failure */
96 {
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;
128
129         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
130         XFS_BMBT_TRACE_ARGI(cur, level);
131         ptr = cur->bc_ptrs[level];
132         tcur = NULL;
133         if (ptr == 0) {
134                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
135                 *stat = 0;
136                 return 0;
137         }
138         block = xfs_bmbt_get_block(cur, level, &bp);
139         numrecs = be16_to_cpu(block->bb_numrecs);
140 #ifdef DEBUG
141         if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
142                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
143                 goto error0;
144         }
145 #endif
146         if (ptr > numrecs) {
147                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
148                 *stat = 0;
149                 return 0;
150         }
151         XFS_STATS_INC(xs_bmbt_delrec);
152         if (level > 0) {
153                 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
154                 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
155 #ifdef DEBUG
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);
159                                 goto error0;
160                         }
161                 }
162 #endif
163                 if (ptr < numrecs) {
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);
170                 }
171         } else {
172                 rp = XFS_BMAP_REC_IADDR(block, 1, cur);
173                 if (ptr < numrecs) {
174                         memmove(&rp[ptr - 1], &rp[ptr],
175                                 (numrecs - ptr) * sizeof(*rp));
176                         xfs_bmbt_log_recs(cur, bp, ptr, numrecs - 1);
177                 }
178                 if (ptr == 1) {
179                         key.br_startoff =
180                                 cpu_to_be64(xfs_bmbt_disk_get_startoff(rp));
181                         kp = &key;
182                 }
183         }
184         numrecs--;
185         block->bb_numrecs = cpu_to_be16(numrecs);
186         xfs_bmbt_log_block(cur, bp, XFS_BB_NUMRECS);
187         /*
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.
192          */
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);
198                         goto error0;
199                 }
200                 if (level > 0 && (error = xfs_btree_decrement(cur, level, &j))) {
201                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
202                         goto error0;
203                 }
204                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
205                 *stat = 1;
206                 return 0;
207         }
208         if (ptr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)kp, level + 1))) {
209                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
210                 goto error0;
211         }
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);
215                         goto error0;
216                 }
217                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
218                 *stat = 1;
219                 return 0;
220         }
221         rbno = be64_to_cpu(block->bb_rightsib);
222         lbno = be64_to_cpu(block->bb_leftsib);
223         /*
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.
227          */
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);
232                         goto error0;
233                 }
234                 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i))) {
235                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
236                         goto error0;
237                 }
238                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
239                 *stat = 1;
240                 return 0;
241         }
242         ASSERT(rbno != NULLFSBLOCK || lbno != NULLFSBLOCK);
243         if ((error = xfs_btree_dup_cursor(cur, &tcur))) {
244                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
245                 goto error0;
246         }
247         bno = NULLFSBLOCK;
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);
253                         goto error0;
254                 }
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);
260 #ifdef DEBUG
261                 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
262                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
263                         goto error0;
264                 }
265 #endif
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);
271                                 goto error0;
272                         }
273                         if (i) {
274                                 ASSERT(be16_to_cpu(block->bb_numrecs) >=
275                                        XFS_BMAP_BLOCK_IMINRECS(level, tcur));
276                                 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
277                                 tcur = NULL;
278                                 if (level > 0) {
279                                         if ((error = xfs_btree_decrement(cur,
280                                                         level, &i))) {
281                                                 XFS_BMBT_TRACE_CURSOR(cur,
282                                                         ERROR);
283                                                 goto error0;
284                                         }
285                                 }
286                                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
287                                 *stat = 1;
288                                 return 0;
289                         }
290                 }
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);
297                                 goto error0;
298                         }
299                         XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
300                 }
301         }
302         if (lbno != NULLFSBLOCK) {
303                 i = xfs_btree_firstrec(tcur, level);
304                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
305                 /*
306                  * decrement to last in block
307                  */
308                 if ((error = xfs_btree_decrement(tcur, level, &i))) {
309                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
310                         goto error0;
311                 }
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);
316 #ifdef DEBUG
317                 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
318                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
319                         goto error0;
320                 }
321 #endif
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);
327                                 goto error0;
328                         }
329                         if (i) {
330                                 ASSERT(be16_to_cpu(block->bb_numrecs) >=
331                                        XFS_BMAP_BLOCK_IMINRECS(level, tcur));
332                                 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
333                                 tcur = NULL;
334                                 if (level == 0)
335                                         cur->bc_ptrs[0]++;
336                                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
337                                 *stat = 1;
338                                 return 0;
339                         }
340                 }
341                 lrecs = be16_to_cpu(left->bb_numrecs);
342         }
343         xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
344         tcur = NULL;
345         mp = cur->bc_mp;
346         ASSERT(bno != NULLFSBLOCK);
347         if (lbno != NULLFSBLOCK &&
348             lrecs + be16_to_cpu(block->bb_numrecs) <= XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
349                 rbno = bno;
350                 right = block;
351                 rbp = bp;
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);
355                         goto error0;
356                 }
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);
360                         goto error0;
361                 }
362         } else if (rbno != NULLFSBLOCK &&
363                    rrecs + be16_to_cpu(block->bb_numrecs) <=
364                    XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
365                 lbno = bno;
366                 left = block;
367                 lbp = bp;
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);
371                         goto error0;
372                 }
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);
376                         goto error0;
377                 }
378                 lrecs = be16_to_cpu(left->bb_numrecs);
379         } else {
380                 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i))) {
381                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
382                         goto error0;
383                 }
384                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
385                 *stat = 1;
386                 return 0;
387         }
388         numlrecs = be16_to_cpu(left->bb_numrecs);
389         numrrecs = be16_to_cpu(right->bb_numrecs);
390         if (level > 0) {
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);
395 #ifdef DEBUG
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);
399                                 goto error0;
400                         }
401                 }
402 #endif
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);
407         } else {
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);
412         }
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);
421                         goto error0;
422                 }
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);
426                         goto error0;
427                 }
428                 rrblock->bb_leftsib = cpu_to_be64(lbno);
429                 xfs_bmbt_log_block(cur, rrbp, XFS_BB_LEFTSIB);
430         }
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);
438         if (bp != lbp) {
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);
444                 goto error0;
445         }
446         if (level > 0)
447                 cur->bc_ptrs[level]--;
448         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
449         *stat = 2;
450         return 0;
451
452 error0:
453         if (tcur)
454                 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
455         return error;
456 }
457
458 /*
459  * Log key values from the btree block.
460  */
461 STATIC void
462 xfs_bmbt_log_keys(
463         xfs_btree_cur_t *cur,
464         xfs_buf_t       *bp,
465         int             kfirst,
466         int             klast)
467 {
468         xfs_trans_t     *tp;
469
470         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
471         XFS_BMBT_TRACE_ARGBII(cur, bp, kfirst, klast);
472         tp = cur->bc_tp;
473         if (bp) {
474                 xfs_bmbt_block_t        *block;
475                 int                     first;
476                 xfs_bmbt_key_t          *kp;
477                 int                     last;
478
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);
484         } else {
485                 xfs_inode_t              *ip;
486
487                 ip = cur->bc_private.b.ip;
488                 xfs_trans_log_inode(tp, ip,
489                         XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
490         }
491         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
492 }
493
494 /*
495  * Log pointer values from the btree block.
496  */
497 STATIC void
498 xfs_bmbt_log_ptrs(
499         xfs_btree_cur_t *cur,
500         xfs_buf_t       *bp,
501         int             pfirst,
502         int             plast)
503 {
504         xfs_trans_t     *tp;
505
506         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
507         XFS_BMBT_TRACE_ARGBII(cur, bp, pfirst, plast);
508         tp = cur->bc_tp;
509         if (bp) {
510                 xfs_bmbt_block_t        *block;
511                 int                     first;
512                 int                     last;
513                 xfs_bmbt_ptr_t          *pp;
514
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);
520         } else {
521                 xfs_inode_t             *ip;
522
523                 ip = cur->bc_private.b.ip;
524                 xfs_trans_log_inode(tp, ip,
525                         XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
526         }
527         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
528 }
529
530 /*
531  * Determine the extent state.
532  */
533 /* ARGSUSED */
534 STATIC xfs_exntst_t
535 xfs_extent_state(
536         xfs_filblks_t           blks,
537         int                     extent_flag)
538 {
539         if (extent_flag) {
540                 ASSERT(blks != 0);      /* saved for DMIG */
541                 return XFS_EXT_UNWRITTEN;
542         }
543         return XFS_EXT_NORM;
544 }
545
546 /*
547  * Convert on-disk form of btree root to in-memory form.
548  */
549 void
550 xfs_bmdr_to_bmbt(
551         xfs_bmdr_block_t        *dblock,
552         int                     dblocklen,
553         xfs_bmbt_block_t        *rblock,
554         int                     rblocklen)
555 {
556         int                     dmxr;
557         xfs_bmbt_key_t          *fkp;
558         __be64                  *fpp;
559         xfs_bmbt_key_t          *tkp;
560         __be64                  *tpp;
561
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);
576 }
577
578 /*
579  * Delete the record pointed to by cur.
580  */
581 int                                     /* error */
582 xfs_bmbt_delete(
583         xfs_btree_cur_t *cur,
584         int             *stat)          /* success/failure */
585 {
586         int             error;          /* error return value */
587         int             i;
588         int             level;
589
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);
594                         return error;
595                 }
596         }
597         if (i == 0) {
598                 for (level = 1; level < cur->bc_nlevels; level++) {
599                         if (cur->bc_ptrs[level] == 0) {
600                                 if ((error = xfs_btree_decrement(cur, level,
601                                                 &i))) {
602                                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
603                                         return error;
604                                 }
605                                 break;
606                         }
607                 }
608         }
609         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
610         *stat = i;
611         return 0;
612 }
613
614 /*
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.
618  */
619
620 STATIC_INLINE void
621 __xfs_bmbt_get_all(
622                 __uint64_t l0,
623                 __uint64_t l1,
624                 xfs_bmbt_irec_t *s)
625 {
626         int     ext_flag;
627         xfs_exntst_t st;
628
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;
632 #if XFS_BIG_BLKNOS
633         s->br_startblock = (((xfs_fsblock_t)l0 & XFS_MASK64LO(9)) << 43) |
634                            (((xfs_fsblock_t)l1) >> 21);
635 #else
636 #ifdef DEBUG
637         {
638                 xfs_dfsbno_t    b;
639
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;
644         }
645 #else   /* !DEBUG */
646         s->br_startblock = (xfs_fsblock_t)(((xfs_dfsbno_t)l1) >> 21);
647 #endif  /* DEBUG */
648 #endif  /* XFS_BIG_BLKNOS */
649         s->br_blockcount = (xfs_filblks_t)(l1 & XFS_MASK64LO(21));
650         /* This is xfs_extent_state() in-line */
651         if (ext_flag) {
652                 ASSERT(s->br_blockcount != 0);  /* saved for DMIG */
653                 st = XFS_EXT_UNWRITTEN;
654         } else
655                 st = XFS_EXT_NORM;
656         s->br_state = st;
657 }
658
659 void
660 xfs_bmbt_get_all(
661         xfs_bmbt_rec_host_t *r,
662         xfs_bmbt_irec_t *s)
663 {
664         __xfs_bmbt_get_all(r->l0, r->l1, s);
665 }
666
667 /*
668  * Get the block pointer for the given level of the cursor.
669  * Fill in the buffer pointer, if applicable.
670  */
671 xfs_bmbt_block_t *
672 xfs_bmbt_get_block(
673         xfs_btree_cur_t         *cur,
674         int                     level,
675         xfs_buf_t               **bpp)
676 {
677         xfs_ifork_t             *ifp;
678         xfs_bmbt_block_t        *rval;
679
680         if (level < cur->bc_nlevels - 1) {
681                 *bpp = cur->bc_bufs[level];
682                 rval = XFS_BUF_TO_BMBT_BLOCK(*bpp);
683         } else {
684                 *bpp = NULL;
685                 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
686                         cur->bc_private.b.whichfork);
687                 rval = ifp->if_broot;
688         }
689         return rval;
690 }
691
692 /*
693  * Extract the blockcount field from an in memory bmap extent record.
694  */
695 xfs_filblks_t
696 xfs_bmbt_get_blockcount(
697         xfs_bmbt_rec_host_t     *r)
698 {
699         return (xfs_filblks_t)(r->l1 & XFS_MASK64LO(21));
700 }
701
702 /*
703  * Extract the startblock field from an in memory bmap extent record.
704  */
705 xfs_fsblock_t
706 xfs_bmbt_get_startblock(
707         xfs_bmbt_rec_host_t     *r)
708 {
709 #if XFS_BIG_BLKNOS
710         return (((xfs_fsblock_t)r->l0 & XFS_MASK64LO(9)) << 43) |
711                (((xfs_fsblock_t)r->l1) >> 21);
712 #else
713 #ifdef DEBUG
714         xfs_dfsbno_t    b;
715
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;
720 #else   /* !DEBUG */
721         return (xfs_fsblock_t)(((xfs_dfsbno_t)r->l1) >> 21);
722 #endif  /* DEBUG */
723 #endif  /* XFS_BIG_BLKNOS */
724 }
725
726 /*
727  * Extract the startoff field from an in memory bmap extent record.
728  */
729 xfs_fileoff_t
730 xfs_bmbt_get_startoff(
731         xfs_bmbt_rec_host_t     *r)
732 {
733         return ((xfs_fileoff_t)r->l0 &
734                  XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
735 }
736
737 xfs_exntst_t
738 xfs_bmbt_get_state(
739         xfs_bmbt_rec_host_t     *r)
740 {
741         int     ext_flag;
742
743         ext_flag = (int)((r->l0) >> (64 - BMBT_EXNTFLAG_BITLEN));
744         return xfs_extent_state(xfs_bmbt_get_blockcount(r),
745                                 ext_flag);
746 }
747
748 /* Endian flipping versions of the bmbt extraction functions */
749 void
750 xfs_bmbt_disk_get_all(
751         xfs_bmbt_rec_t  *r,
752         xfs_bmbt_irec_t *s)
753 {
754         __xfs_bmbt_get_all(be64_to_cpu(r->l0), be64_to_cpu(r->l1), s);
755 }
756
757 /*
758  * Extract the blockcount field from an on disk bmap extent record.
759  */
760 xfs_filblks_t
761 xfs_bmbt_disk_get_blockcount(
762         xfs_bmbt_rec_t  *r)
763 {
764         return (xfs_filblks_t)(be64_to_cpu(r->l1) & XFS_MASK64LO(21));
765 }
766
767 /*
768  * Extract the startoff field from a disk format bmap extent record.
769  */
770 xfs_fileoff_t
771 xfs_bmbt_disk_get_startoff(
772         xfs_bmbt_rec_t  *r)
773 {
774         return ((xfs_fileoff_t)be64_to_cpu(r->l0) &
775                  XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
776 }
777
778 /*
779  * Log fields from the btree block header.
780  */
781 void
782 xfs_bmbt_log_block(
783         xfs_btree_cur_t         *cur,
784         xfs_buf_t               *bp,
785         int                     fields)
786 {
787         int                     first;
788         int                     last;
789         xfs_trans_t             *tp;
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)
797         };
798
799         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
800         XFS_BMBT_TRACE_ARGBI(cur, bp, fields);
801         tp = cur->bc_tp;
802         if (bp) {
803                 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first,
804                                   &last);
805                 xfs_trans_log_buf(tp, bp, first, last);
806         } else
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);
810 }
811
812 /*
813  * Log record values from the btree block.
814  */
815 void
816 xfs_bmbt_log_recs(
817         xfs_btree_cur_t         *cur,
818         xfs_buf_t               *bp,
819         int                     rfirst,
820         int                     rlast)
821 {
822         xfs_bmbt_block_t        *block;
823         int                     first;
824         int                     last;
825         xfs_bmbt_rec_t          *rp;
826         xfs_trans_t             *tp;
827
828         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
829         XFS_BMBT_TRACE_ARGBII(cur, bp, rfirst, rlast);
830         ASSERT(bp);
831         tp = cur->bc_tp;
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);
838 }
839
840 /*
841  * Set all the fields in a bmap extent record from the arguments.
842  */
843 void
844 xfs_bmbt_set_allf(
845         xfs_bmbt_rec_host_t     *r,
846         xfs_fileoff_t           startoff,
847         xfs_fsblock_t           startblock,
848         xfs_filblks_t           blockcount,
849         xfs_exntst_t            state)
850 {
851         int             extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
852
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);
856
857 #if XFS_BIG_BLKNOS
858         ASSERT((startblock & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
859
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));
875         } else {
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));
881         }
882 #endif  /* XFS_BIG_BLKNOS */
883 }
884
885 /*
886  * Set all the fields in a bmap extent record from the uncompressed form.
887  */
888 void
889 xfs_bmbt_set_all(
890         xfs_bmbt_rec_host_t *r,
891         xfs_bmbt_irec_t *s)
892 {
893         xfs_bmbt_set_allf(r, s->br_startoff, s->br_startblock,
894                              s->br_blockcount, s->br_state);
895 }
896
897
898 /*
899  * Set all the fields in a disk format bmap extent record from the arguments.
900  */
901 void
902 xfs_bmbt_disk_set_allf(
903         xfs_bmbt_rec_t          *r,
904         xfs_fileoff_t           startoff,
905         xfs_fsblock_t           startblock,
906         xfs_filblks_t           blockcount,
907         xfs_exntst_t            state)
908 {
909         int                     extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
910
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);
914
915 #if XFS_BIG_BLKNOS
916         ASSERT((startblock & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
917
918         r->l0 = cpu_to_be64(
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));
922         r->l1 = cpu_to_be64(
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)) {
928                 r->l0 = cpu_to_be64(
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)));
936         } else {
937                 r->l0 = cpu_to_be64(
938                         ((xfs_bmbt_rec_base_t)extent_flag << 63) |
939                          ((xfs_bmbt_rec_base_t)startoff << 9));
940                 r->l1 = cpu_to_be64(
941                         ((xfs_bmbt_rec_base_t)startblock << 21) |
942                          ((xfs_bmbt_rec_base_t)blockcount &
943                           (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
944         }
945 #endif  /* XFS_BIG_BLKNOS */
946 }
947
948 /*
949  * Set all the fields in a bmap extent record from the uncompressed form.
950  */
951 void
952 xfs_bmbt_disk_set_all(
953         xfs_bmbt_rec_t  *r,
954         xfs_bmbt_irec_t *s)
955 {
956         xfs_bmbt_disk_set_allf(r, s->br_startoff, s->br_startblock,
957                                   s->br_blockcount, s->br_state);
958 }
959
960 /*
961  * Set the blockcount field in a bmap extent record.
962  */
963 void
964 xfs_bmbt_set_blockcount(
965         xfs_bmbt_rec_host_t *r,
966         xfs_filblks_t   v)
967 {
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));
971 }
972
973 /*
974  * Set the startblock field in a bmap extent record.
975  */
976 void
977 xfs_bmbt_set_startblock(
978         xfs_bmbt_rec_host_t *r,
979         xfs_fsblock_t   v)
980 {
981 #if XFS_BIG_BLKNOS
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));
993         } else {
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));
997         }
998 #endif  /* XFS_BIG_BLKNOS */
999 }
1000
1001 /*
1002  * Set the startoff field in a bmap extent record.
1003  */
1004 void
1005 xfs_bmbt_set_startoff(
1006         xfs_bmbt_rec_host_t *r,
1007         xfs_fileoff_t   v)
1008 {
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));
1013 }
1014
1015 /*
1016  * Set the extent state field in a bmap extent record.
1017  */
1018 void
1019 xfs_bmbt_set_state(
1020         xfs_bmbt_rec_host_t *r,
1021         xfs_exntst_t    v)
1022 {
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);
1026         else
1027                 r->l0 |= XFS_MASK64HI(BMBT_EXNTFLAG_BITLEN);
1028 }
1029
1030 /*
1031  * Convert in-memory form of btree root to on-disk form.
1032  */
1033 void
1034 xfs_bmbt_to_bmdr(
1035         xfs_bmbt_block_t        *rblock,
1036         int                     rblocklen,
1037         xfs_bmdr_block_t        *dblock,
1038         int                     dblocklen)
1039 {
1040         int                     dmxr;
1041         xfs_bmbt_key_t          *fkp;
1042         __be64                  *fpp;
1043         xfs_bmbt_key_t          *tkp;
1044         __be64                  *tpp;
1045
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);
1060 }
1061
1062 /*
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.
1068  */
1069
1070 int
1071 xfs_check_nostate_extents(
1072         xfs_ifork_t             *ifp,
1073         xfs_extnum_t            idx,
1074         xfs_extnum_t            num)
1075 {
1076         for (; num > 0; num--, idx++) {
1077                 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1078                 if ((ep->l0 >>
1079                      (64 - BMBT_EXNTFLAG_BITLEN)) != 0) {
1080                         ASSERT(0);
1081                         return 1;
1082                 }
1083         }
1084         return 0;
1085 }
1086
1087
1088 STATIC struct xfs_btree_cur *
1089 xfs_bmbt_dup_cursor(
1090         struct xfs_btree_cur    *cur)
1091 {
1092         struct xfs_btree_cur    *new;
1093
1094         new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp,
1095                         cur->bc_private.b.ip, cur->bc_private.b.whichfork);
1096
1097         /*
1098          * Copy the firstblock, flist, and flags values,
1099          * since init cursor doesn't get them.
1100          */
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;
1104
1105         return new;
1106 }
1107
1108 STATIC void
1109 xfs_bmbt_update_cursor(
1110         struct xfs_btree_cur    *src,
1111         struct xfs_btree_cur    *dst)
1112 {
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);
1116
1117         dst->bc_private.b.allocated += src->bc_private.b.allocated;
1118         dst->bc_private.b.firstblock = src->bc_private.b.firstblock;
1119
1120         src->bc_private.b.allocated = 0;
1121 }
1122
1123 STATIC int
1124 xfs_bmbt_alloc_block(
1125         struct xfs_btree_cur    *cur,
1126         union xfs_btree_ptr     *start,
1127         union xfs_btree_ptr     *new,
1128         int                     length,
1129         int                     *stat)
1130 {
1131         xfs_alloc_arg_t         args;           /* block allocation args */
1132         int                     error;          /* error return value */
1133
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;
1139
1140         if (args.fsbno == NULLFSBLOCK) {
1141                 args.fsbno = be64_to_cpu(start->l);
1142                 args.type = XFS_ALLOCTYPE_START_BNO;
1143                 /*
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.
1148                  *
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.
1153                  */
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;
1157         } else {
1158                 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1159         }
1160
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);
1165                 goto error0;
1166         }
1167         error = xfs_alloc_vextent(&args);
1168         if (error)
1169                 goto error0;
1170
1171         if (args.fsbno == NULLFSBLOCK && args.minleft) {
1172                 /*
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.
1176                  */
1177                 args.fsbno = 0;
1178                 args.type = XFS_ALLOCTYPE_FIRST_AG;
1179                 args.minleft = 0;
1180                 error = xfs_alloc_vextent(&args);
1181                 if (error)
1182                         goto error0;
1183                 cur->bc_private.b.flist->xbf_low = 1;
1184         }
1185         if (args.fsbno == NULLFSBLOCK) {
1186                 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1187                 *stat = 0;
1188                 return 0;
1189         }
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);
1197
1198         new->l = cpu_to_be64(args.fsbno);
1199
1200         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1201         *stat = 1;
1202         return 0;
1203
1204  error0:
1205         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1206         return error;
1207 }
1208
1209 STATIC int
1210 xfs_bmbt_free_block(
1211         struct xfs_btree_cur    *cur,
1212         struct xfs_buf          *bp)
1213 {
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));
1218
1219         xfs_bmap_add_free(fsbno, 1, cur->bc_private.b.flist, mp);
1220         ip->i_d.di_nblocks--;
1221
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);
1225         return 0;
1226 }
1227
1228 STATIC int
1229 xfs_bmbt_get_maxrecs(
1230         struct xfs_btree_cur    *cur,
1231         int                     level)
1232 {
1233         return XFS_BMAP_BLOCK_IMAXRECS(level, cur);
1234 }
1235
1236 /*
1237  * Get the maximum records we could store in the on-disk format.
1238  *
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.
1244  */
1245 STATIC int
1246 xfs_bmbt_get_dmaxrecs(
1247         struct xfs_btree_cur    *cur,
1248         int                     level)
1249 {
1250         return XFS_BMAP_BLOCK_DMAXRECS(level, cur);
1251 }
1252
1253 STATIC void
1254 xfs_bmbt_init_key_from_rec(
1255         union xfs_btree_key     *key,
1256         union xfs_btree_rec     *rec)
1257 {
1258         key->bmbt.br_startoff =
1259                 cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt));
1260 }
1261
1262 STATIC void
1263 xfs_bmbt_init_rec_from_key(
1264         union xfs_btree_key     *key,
1265         union xfs_btree_rec     *rec)
1266 {
1267         ASSERT(key->bmbt.br_startoff != 0);
1268
1269         xfs_bmbt_disk_set_allf(&rec->bmbt, be64_to_cpu(key->bmbt.br_startoff),
1270                                0, 0, XFS_EXT_NORM);
1271 }
1272
1273 STATIC void
1274 xfs_bmbt_init_rec_from_cur(
1275         struct xfs_btree_cur    *cur,
1276         union xfs_btree_rec     *rec)
1277 {
1278         xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b);
1279 }
1280
1281 STATIC void
1282 xfs_bmbt_init_ptr_from_cur(
1283         struct xfs_btree_cur    *cur,
1284         union xfs_btree_ptr     *ptr)
1285 {
1286         ptr->l = 0;
1287 }
1288
1289 STATIC __int64_t
1290 xfs_bmbt_key_diff(
1291         struct xfs_btree_cur    *cur,
1292         union xfs_btree_key     *key)
1293 {
1294         return (__int64_t)be64_to_cpu(key->bmbt.br_startoff) -
1295                                       cur->bc_rec.b.br_startoff;
1296 }
1297
1298 #ifdef XFS_BTREE_TRACE
1299 ktrace_t        *xfs_bmbt_trace_buf;
1300
1301 STATIC void
1302 xfs_bmbt_trace_enter(
1303         struct xfs_btree_cur    *cur,
1304         const char              *func,
1305         char                    *s,
1306         int                     type,
1307         int                     line,
1308         __psunsigned_t          a0,
1309         __psunsigned_t          a1,
1310         __psunsigned_t          a2,
1311         __psunsigned_t          a3,
1312         __psunsigned_t          a4,
1313         __psunsigned_t          a5,
1314         __psunsigned_t          a6,
1315         __psunsigned_t          a7,
1316         __psunsigned_t          a8,
1317         __psunsigned_t          a9,
1318         __psunsigned_t          a10)
1319 {
1320         struct xfs_inode        *ip = cur->bc_private.b.ip;
1321         int                     whichfork = cur->bc_private.b.whichfork;
1322
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);
1335 }
1336
1337 STATIC void
1338 xfs_bmbt_trace_cursor(
1339         struct xfs_btree_cur    *cur,
1340         __uint32_t              *s0,
1341         __uint64_t              *l0,
1342         __uint64_t              *l1)
1343 {
1344         struct xfs_bmbt_rec_host r;
1345
1346         xfs_bmbt_set_all(&r, &cur->bc_rec.b);
1347
1348         *s0 = (cur->bc_nlevels << 24) |
1349               (cur->bc_private.b.flags << 16) |
1350                cur->bc_private.b.allocated;
1351         *l0 = r.l0;
1352         *l1 = r.l1;
1353 }
1354
1355 STATIC void
1356 xfs_bmbt_trace_key(
1357         struct xfs_btree_cur    *cur,
1358         union xfs_btree_key     *key,
1359         __uint64_t              *l0,
1360         __uint64_t              *l1)
1361 {
1362         *l0 = be64_to_cpu(key->bmbt.br_startoff);
1363         *l1 = 0;
1364 }
1365
1366 STATIC void
1367 xfs_bmbt_trace_record(
1368         struct xfs_btree_cur    *cur,
1369         union xfs_btree_rec     *rec,
1370         __uint64_t              *l0,
1371         __uint64_t              *l1,
1372         __uint64_t              *l2)
1373 {
1374         struct xfs_bmbt_irec    irec;
1375
1376         xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
1377         *l0 = irec.br_startoff;
1378         *l1 = irec.br_startblock;
1379         *l2 = irec.br_blockcount;
1380 }
1381 #endif /* XFS_BTREE_TRACE */
1382
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),
1386
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,
1398
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,
1404 #endif
1405 };
1406
1407 /*
1408  * Allocate a new bmap btree cursor.
1409  */
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 */
1416 {
1417         struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
1418         struct xfs_btree_cur    *cur;
1419
1420         cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
1421
1422         cur->bc_tp = tp;
1423         cur->bc_mp = mp;
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;
1427
1428         cur->bc_ops = &xfs_bmbt_ops;
1429         cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE;
1430
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;
1438
1439         return cur;
1440 }