Pull thinkpad into release branch
[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_ialloc.h"
41 #include "xfs_itable.h"
42 #include "xfs_bmap.h"
43 #include "xfs_error.h"
44 #include "xfs_quota.h"
45
46 #if defined(XFS_BMBT_TRACE)
47 ktrace_t        *xfs_bmbt_trace_buf;
48 #endif
49
50 /*
51  * Prototypes for internal btree functions.
52  */
53
54
55 STATIC int xfs_bmbt_killroot(xfs_btree_cur_t *);
56 STATIC void xfs_bmbt_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
57 STATIC void xfs_bmbt_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
58 STATIC int xfs_bmbt_lshift(xfs_btree_cur_t *, int, int *);
59 STATIC int xfs_bmbt_rshift(xfs_btree_cur_t *, int, int *);
60 STATIC int xfs_bmbt_split(xfs_btree_cur_t *, int, xfs_fsblock_t *,
61                 __uint64_t *, xfs_btree_cur_t **, int *);
62 STATIC int xfs_bmbt_updkey(xfs_btree_cur_t *, xfs_bmbt_key_t *, int);
63
64
65 #if defined(XFS_BMBT_TRACE)
66
67 static char     ARGS[] = "args";
68 static char     ENTRY[] = "entry";
69 static char     ERROR[] = "error";
70 #undef EXIT
71 static char     EXIT[] = "exit";
72
73 /*
74  * Add a trace buffer entry for the arguments given to the routine,
75  * generic form.
76  */
77 STATIC void
78 xfs_bmbt_trace_enter(
79         const char      *func,
80         xfs_btree_cur_t *cur,
81         char            *s,
82         int             type,
83         int             line,
84         __psunsigned_t  a0,
85         __psunsigned_t  a1,
86         __psunsigned_t  a2,
87         __psunsigned_t  a3,
88         __psunsigned_t  a4,
89         __psunsigned_t  a5,
90         __psunsigned_t  a6,
91         __psunsigned_t  a7,
92         __psunsigned_t  a8,
93         __psunsigned_t  a9,
94         __psunsigned_t  a10)
95 {
96         xfs_inode_t     *ip;
97         int             whichfork;
98
99         ip = cur->bc_private.b.ip;
100         whichfork = cur->bc_private.b.whichfork;
101         ktrace_enter(xfs_bmbt_trace_buf,
102                 (void *)((__psint_t)type | (whichfork << 8) | (line << 16)),
103                 (void *)func, (void *)s, (void *)ip, (void *)cur,
104                 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
105                 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
106                 (void *)a8, (void *)a9, (void *)a10);
107         ASSERT(ip->i_btrace);
108         ktrace_enter(ip->i_btrace,
109                 (void *)((__psint_t)type | (whichfork << 8) | (line << 16)),
110                 (void *)func, (void *)s, (void *)ip, (void *)cur,
111                 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
112                 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
113                 (void *)a8, (void *)a9, (void *)a10);
114 }
115 /*
116  * Add a trace buffer entry for arguments, for a buffer & 1 integer arg.
117  */
118 STATIC void
119 xfs_bmbt_trace_argbi(
120         const char      *func,
121         xfs_btree_cur_t *cur,
122         xfs_buf_t       *b,
123         int             i,
124         int             line)
125 {
126         xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGBI, line,
127                 (__psunsigned_t)b, i, 0, 0,
128                 0, 0, 0, 0,
129                 0, 0, 0);
130 }
131
132 /*
133  * Add a trace buffer entry for arguments, for a buffer & 2 integer args.
134  */
135 STATIC void
136 xfs_bmbt_trace_argbii(
137         const char      *func,
138         xfs_btree_cur_t *cur,
139         xfs_buf_t       *b,
140         int             i0,
141         int             i1,
142         int             line)
143 {
144         xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGBII, line,
145                 (__psunsigned_t)b, i0, i1, 0,
146                 0, 0, 0, 0,
147                 0, 0, 0);
148 }
149
150 /*
151  * Add a trace buffer entry for arguments, for 3 block-length args
152  * and an integer arg.
153  */
154 STATIC void
155 xfs_bmbt_trace_argfffi(
156         const char              *func,
157         xfs_btree_cur_t         *cur,
158         xfs_dfiloff_t           o,
159         xfs_dfsbno_t            b,
160         xfs_dfilblks_t          i,
161         int                     j,
162         int                     line)
163 {
164         xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGFFFI, line,
165                 o >> 32, (int)o, b >> 32, (int)b,
166                 i >> 32, (int)i, (int)j, 0,
167                 0, 0, 0);
168 }
169
170 /*
171  * Add a trace buffer entry for arguments, for one integer arg.
172  */
173 STATIC void
174 xfs_bmbt_trace_argi(
175         const char      *func,
176         xfs_btree_cur_t *cur,
177         int             i,
178         int             line)
179 {
180         xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGI, line,
181                 i, 0, 0, 0,
182                 0, 0, 0, 0,
183                 0, 0, 0);
184 }
185
186 /*
187  * Add a trace buffer entry for arguments, for int, fsblock, key.
188  */
189 STATIC void
190 xfs_bmbt_trace_argifk(
191         const char              *func,
192         xfs_btree_cur_t         *cur,
193         int                     i,
194         xfs_fsblock_t           f,
195         xfs_dfiloff_t           o,
196         int                     line)
197 {
198         xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGIFK, line,
199                 i, (xfs_dfsbno_t)f >> 32, (int)f, o >> 32,
200                 (int)o, 0, 0, 0,
201                 0, 0, 0);
202 }
203
204 /*
205  * Add a trace buffer entry for arguments, for int, fsblock, rec.
206  */
207 STATIC void
208 xfs_bmbt_trace_argifr(
209         const char              *func,
210         xfs_btree_cur_t         *cur,
211         int                     i,
212         xfs_fsblock_t           f,
213         xfs_bmbt_rec_t          *r,
214         int                     line)
215 {
216         xfs_dfsbno_t            b;
217         xfs_dfilblks_t          c;
218         xfs_dfsbno_t            d;
219         xfs_dfiloff_t           o;
220         xfs_bmbt_irec_t         s;
221
222         d = (xfs_dfsbno_t)f;
223         xfs_bmbt_disk_get_all(r, &s);
224         o = (xfs_dfiloff_t)s.br_startoff;
225         b = (xfs_dfsbno_t)s.br_startblock;
226         c = s.br_blockcount;
227         xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGIFR, line,
228                 i, d >> 32, (int)d, o >> 32,
229                 (int)o, b >> 32, (int)b, c >> 32,
230                 (int)c, 0, 0);
231 }
232
233 /*
234  * Add a trace buffer entry for arguments, for int, key.
235  */
236 STATIC void
237 xfs_bmbt_trace_argik(
238         const char              *func,
239         xfs_btree_cur_t         *cur,
240         int                     i,
241         xfs_bmbt_key_t          *k,
242         int                     line)
243 {
244         xfs_dfiloff_t           o;
245
246         o = be64_to_cpu(k->br_startoff);
247         xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGIFK, line,
248                 i, o >> 32, (int)o, 0,
249                 0, 0, 0, 0,
250                 0, 0, 0);
251 }
252
253 /*
254  * Add a trace buffer entry for the cursor/operation.
255  */
256 STATIC void
257 xfs_bmbt_trace_cursor(
258         const char      *func,
259         xfs_btree_cur_t *cur,
260         char            *s,
261         int             line)
262 {
263         xfs_bmbt_rec_t  r;
264
265         xfs_bmbt_set_all(&r, &cur->bc_rec.b);
266         xfs_bmbt_trace_enter(func, cur, s, XFS_BMBT_KTRACE_CUR, line,
267                 (cur->bc_nlevels << 24) | (cur->bc_private.b.flags << 16) |
268                 cur->bc_private.b.allocated,
269                 INT_GET(r.l0, ARCH_CONVERT) >> 32, (int)INT_GET(r.l0, ARCH_CONVERT), INT_GET(r.l1, ARCH_CONVERT) >> 32, (int)INT_GET(r.l1, ARCH_CONVERT),
270                 (unsigned long)cur->bc_bufs[0], (unsigned long)cur->bc_bufs[1],
271                 (unsigned long)cur->bc_bufs[2], (unsigned long)cur->bc_bufs[3],
272                 (cur->bc_ptrs[0] << 16) | cur->bc_ptrs[1],
273                 (cur->bc_ptrs[2] << 16) | cur->bc_ptrs[3]);
274 }
275
276 #define XFS_BMBT_TRACE_ARGBI(c,b,i)     \
277         xfs_bmbt_trace_argbi(__FUNCTION__, c, b, i, __LINE__)
278 #define XFS_BMBT_TRACE_ARGBII(c,b,i,j)  \
279         xfs_bmbt_trace_argbii(__FUNCTION__, c, b, i, j, __LINE__)
280 #define XFS_BMBT_TRACE_ARGFFFI(c,o,b,i,j)       \
281         xfs_bmbt_trace_argfffi(__FUNCTION__, c, o, b, i, j, __LINE__)
282 #define XFS_BMBT_TRACE_ARGI(c,i)        \
283         xfs_bmbt_trace_argi(__FUNCTION__, c, i, __LINE__)
284 #define XFS_BMBT_TRACE_ARGIFK(c,i,f,s)  \
285         xfs_bmbt_trace_argifk(__FUNCTION__, c, i, f, s, __LINE__)
286 #define XFS_BMBT_TRACE_ARGIFR(c,i,f,r)  \
287         xfs_bmbt_trace_argifr(__FUNCTION__, c, i, f, r, __LINE__)
288 #define XFS_BMBT_TRACE_ARGIK(c,i,k)     \
289         xfs_bmbt_trace_argik(__FUNCTION__, c, i, k, __LINE__)
290 #define XFS_BMBT_TRACE_CURSOR(c,s)      \
291         xfs_bmbt_trace_cursor(__FUNCTION__, c, s, __LINE__)
292 #else
293 #define XFS_BMBT_TRACE_ARGBI(c,b,i)
294 #define XFS_BMBT_TRACE_ARGBII(c,b,i,j)
295 #define XFS_BMBT_TRACE_ARGFFFI(c,o,b,i,j)
296 #define XFS_BMBT_TRACE_ARGI(c,i)
297 #define XFS_BMBT_TRACE_ARGIFK(c,i,f,s)
298 #define XFS_BMBT_TRACE_ARGIFR(c,i,f,r)
299 #define XFS_BMBT_TRACE_ARGIK(c,i,k)
300 #define XFS_BMBT_TRACE_CURSOR(c,s)
301 #endif  /* XFS_BMBT_TRACE */
302
303
304 /*
305  * Internal functions.
306  */
307
308 /*
309  * Delete record pointed to by cur/level.
310  */
311 STATIC int                                      /* error */
312 xfs_bmbt_delrec(
313         xfs_btree_cur_t         *cur,
314         int                     level,
315         int                     *stat)          /* success/failure */
316 {
317         xfs_bmbt_block_t        *block;         /* bmap btree block */
318         xfs_fsblock_t           bno;            /* fs-relative block number */
319         xfs_buf_t               *bp;            /* buffer for block */
320         int                     error;          /* error return value */
321         int                     i;              /* loop counter */
322         int                     j;              /* temp state */
323         xfs_bmbt_key_t          key;            /* bmap btree key */
324         xfs_bmbt_key_t          *kp=NULL;       /* pointer to bmap btree key */
325         xfs_fsblock_t           lbno;           /* left sibling block number */
326         xfs_buf_t               *lbp;           /* left buffer pointer */
327         xfs_bmbt_block_t        *left;          /* left btree block */
328         xfs_bmbt_key_t          *lkp;           /* left btree key */
329         xfs_bmbt_ptr_t          *lpp;           /* left address pointer */
330         int                     lrecs=0;        /* left record count */
331         xfs_bmbt_rec_t          *lrp;           /* left record pointer */
332         xfs_mount_t             *mp;            /* file system mount point */
333         xfs_bmbt_ptr_t          *pp;            /* pointer to bmap block addr */
334         int                     ptr;            /* key/record index */
335         xfs_fsblock_t           rbno;           /* right sibling block number */
336         xfs_buf_t               *rbp;           /* right buffer pointer */
337         xfs_bmbt_block_t        *right;         /* right btree block */
338         xfs_bmbt_key_t          *rkp;           /* right btree key */
339         xfs_bmbt_rec_t          *rp;            /* pointer to bmap btree rec */
340         xfs_bmbt_ptr_t          *rpp;           /* right address pointer */
341         xfs_bmbt_block_t        *rrblock;       /* right-right btree block */
342         xfs_buf_t               *rrbp;          /* right-right buffer pointer */
343         int                     rrecs=0;        /* right record count */
344         xfs_bmbt_rec_t          *rrp;           /* right record pointer */
345         xfs_btree_cur_t         *tcur;          /* temporary btree cursor */
346         int                     numrecs;        /* temporary numrec count */
347         int                     numlrecs, numrrecs;
348
349         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
350         XFS_BMBT_TRACE_ARGI(cur, level);
351         ptr = cur->bc_ptrs[level];
352         tcur = NULL;
353         if (ptr == 0) {
354                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
355                 *stat = 0;
356                 return 0;
357         }
358         block = xfs_bmbt_get_block(cur, level, &bp);
359         numrecs = be16_to_cpu(block->bb_numrecs);
360 #ifdef DEBUG
361         if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
362                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
363                 goto error0;
364         }
365 #endif
366         if (ptr > numrecs) {
367                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
368                 *stat = 0;
369                 return 0;
370         }
371         XFS_STATS_INC(xs_bmbt_delrec);
372         if (level > 0) {
373                 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
374                 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
375 #ifdef DEBUG
376                 for (i = ptr; i < numrecs; i++) {
377                         if ((error = xfs_btree_check_lptr_disk(cur, pp[i], level))) {
378                                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
379                                 goto error0;
380                         }
381                 }
382 #endif
383                 if (ptr < numrecs) {
384                         memmove(&kp[ptr - 1], &kp[ptr],
385                                 (numrecs - ptr) * sizeof(*kp));
386                         memmove(&pp[ptr - 1], &pp[ptr], /* INT_: direct copy */
387                                 (numrecs - ptr) * sizeof(*pp));
388                         xfs_bmbt_log_ptrs(cur, bp, ptr, numrecs - 1);
389                         xfs_bmbt_log_keys(cur, bp, ptr, numrecs - 1);
390                 }
391         } else {
392                 rp = XFS_BMAP_REC_IADDR(block, 1, cur);
393                 if (ptr < numrecs) {
394                         memmove(&rp[ptr - 1], &rp[ptr],
395                                 (numrecs - ptr) * sizeof(*rp));
396                         xfs_bmbt_log_recs(cur, bp, ptr, numrecs - 1);
397                 }
398                 if (ptr == 1) {
399                         key.br_startoff =
400                                 cpu_to_be64(xfs_bmbt_disk_get_startoff(rp));
401                         kp = &key;
402                 }
403         }
404         numrecs--;
405         block->bb_numrecs = cpu_to_be16(numrecs);
406         xfs_bmbt_log_block(cur, bp, XFS_BB_NUMRECS);
407         /*
408          * We're at the root level.
409          * First, shrink the root block in-memory.
410          * Try to get rid of the next level down.
411          * If we can't then there's nothing left to do.
412          */
413         if (level == cur->bc_nlevels - 1) {
414                 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
415                         cur->bc_private.b.whichfork);
416                 if ((error = xfs_bmbt_killroot(cur))) {
417                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
418                         goto error0;
419                 }
420                 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &j))) {
421                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
422                         goto error0;
423                 }
424                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
425                 *stat = 1;
426                 return 0;
427         }
428         if (ptr == 1 && (error = xfs_bmbt_updkey(cur, kp, level + 1))) {
429                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
430                 goto error0;
431         }
432         if (numrecs >= XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
433                 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &j))) {
434                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
435                         goto error0;
436                 }
437                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
438                 *stat = 1;
439                 return 0;
440         }
441         rbno = be64_to_cpu(block->bb_rightsib);
442         lbno = be64_to_cpu(block->bb_leftsib);
443         /*
444          * One child of root, need to get a chance to copy its contents
445          * into the root and delete it. Can't go up to next level,
446          * there's nothing to delete there.
447          */
448         if (lbno == NULLFSBLOCK && rbno == NULLFSBLOCK &&
449             level == cur->bc_nlevels - 2) {
450                 if ((error = xfs_bmbt_killroot(cur))) {
451                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
452                         goto error0;
453                 }
454                 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &i))) {
455                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
456                         goto error0;
457                 }
458                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
459                 *stat = 1;
460                 return 0;
461         }
462         ASSERT(rbno != NULLFSBLOCK || lbno != NULLFSBLOCK);
463         if ((error = xfs_btree_dup_cursor(cur, &tcur))) {
464                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
465                 goto error0;
466         }
467         bno = NULLFSBLOCK;
468         if (rbno != NULLFSBLOCK) {
469                 i = xfs_btree_lastrec(tcur, level);
470                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
471                 if ((error = xfs_bmbt_increment(tcur, level, &i))) {
472                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
473                         goto error0;
474                 }
475                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
476                 i = xfs_btree_lastrec(tcur, level);
477                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
478                 rbp = tcur->bc_bufs[level];
479                 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
480 #ifdef DEBUG
481                 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
482                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
483                         goto error0;
484                 }
485 #endif
486                 bno = be64_to_cpu(right->bb_leftsib);
487                 if (be16_to_cpu(right->bb_numrecs) - 1 >=
488                     XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
489                         if ((error = xfs_bmbt_lshift(tcur, level, &i))) {
490                                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
491                                 goto error0;
492                         }
493                         if (i) {
494                                 ASSERT(be16_to_cpu(block->bb_numrecs) >=
495                                        XFS_BMAP_BLOCK_IMINRECS(level, tcur));
496                                 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
497                                 tcur = NULL;
498                                 if (level > 0) {
499                                         if ((error = xfs_bmbt_decrement(cur,
500                                                         level, &i))) {
501                                                 XFS_BMBT_TRACE_CURSOR(cur,
502                                                         ERROR);
503                                                 goto error0;
504                                         }
505                                 }
506                                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
507                                 *stat = 1;
508                                 return 0;
509                         }
510                 }
511                 rrecs = be16_to_cpu(right->bb_numrecs);
512                 if (lbno != NULLFSBLOCK) {
513                         i = xfs_btree_firstrec(tcur, level);
514                         XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
515                         if ((error = xfs_bmbt_decrement(tcur, level, &i))) {
516                                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
517                                 goto error0;
518                         }
519                         XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
520                 }
521         }
522         if (lbno != NULLFSBLOCK) {
523                 i = xfs_btree_firstrec(tcur, level);
524                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
525                 /*
526                  * decrement to last in block
527                  */
528                 if ((error = xfs_bmbt_decrement(tcur, level, &i))) {
529                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
530                         goto error0;
531                 }
532                 i = xfs_btree_firstrec(tcur, level);
533                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
534                 lbp = tcur->bc_bufs[level];
535                 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
536 #ifdef DEBUG
537                 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
538                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
539                         goto error0;
540                 }
541 #endif
542                 bno = be64_to_cpu(left->bb_rightsib);
543                 if (be16_to_cpu(left->bb_numrecs) - 1 >=
544                     XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
545                         if ((error = xfs_bmbt_rshift(tcur, level, &i))) {
546                                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
547                                 goto error0;
548                         }
549                         if (i) {
550                                 ASSERT(be16_to_cpu(block->bb_numrecs) >=
551                                        XFS_BMAP_BLOCK_IMINRECS(level, tcur));
552                                 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
553                                 tcur = NULL;
554                                 if (level == 0)
555                                         cur->bc_ptrs[0]++;
556                                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
557                                 *stat = 1;
558                                 return 0;
559                         }
560                 }
561                 lrecs = be16_to_cpu(left->bb_numrecs);
562         }
563         xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
564         tcur = NULL;
565         mp = cur->bc_mp;
566         ASSERT(bno != NULLFSBLOCK);
567         if (lbno != NULLFSBLOCK &&
568             lrecs + be16_to_cpu(block->bb_numrecs) <= XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
569                 rbno = bno;
570                 right = block;
571                 rbp = bp;
572                 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, lbno, 0, &lbp,
573                                 XFS_BMAP_BTREE_REF))) {
574                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
575                         goto error0;
576                 }
577                 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
578                 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
579                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
580                         goto error0;
581                 }
582         } else if (rbno != NULLFSBLOCK &&
583                    rrecs + be16_to_cpu(block->bb_numrecs) <=
584                    XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
585                 lbno = bno;
586                 left = block;
587                 lbp = bp;
588                 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, rbno, 0, &rbp,
589                                 XFS_BMAP_BTREE_REF))) {
590                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
591                         goto error0;
592                 }
593                 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
594                 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
595                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
596                         goto error0;
597                 }
598                 lrecs = be16_to_cpu(left->bb_numrecs);
599         } else {
600                 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &i))) {
601                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
602                         goto error0;
603                 }
604                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
605                 *stat = 1;
606                 return 0;
607         }
608         numlrecs = be16_to_cpu(left->bb_numrecs);
609         numrrecs = be16_to_cpu(right->bb_numrecs);
610         if (level > 0) {
611                 lkp = XFS_BMAP_KEY_IADDR(left, numlrecs + 1, cur);
612                 lpp = XFS_BMAP_PTR_IADDR(left, numlrecs + 1, cur);
613                 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
614                 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
615 #ifdef DEBUG
616                 for (i = 0; i < numrrecs; i++) {
617                         if ((error = xfs_btree_check_lptr_disk(cur, rpp[i], level))) {
618                                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
619                                 goto error0;
620                         }
621                 }
622 #endif
623                 memcpy(lkp, rkp, numrrecs * sizeof(*lkp));
624                 memcpy(lpp, rpp, numrrecs * sizeof(*lpp));
625                 xfs_bmbt_log_keys(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
626                 xfs_bmbt_log_ptrs(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
627         } else {
628                 lrp = XFS_BMAP_REC_IADDR(left, numlrecs + 1, cur);
629                 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
630                 memcpy(lrp, rrp, numrrecs * sizeof(*lrp));
631                 xfs_bmbt_log_recs(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
632         }
633         be16_add(&left->bb_numrecs, numrrecs);
634         left->bb_rightsib = right->bb_rightsib;
635         xfs_bmbt_log_block(cur, lbp, XFS_BB_RIGHTSIB | XFS_BB_NUMRECS);
636         if (be64_to_cpu(left->bb_rightsib) != NULLDFSBNO) {
637                 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp,
638                                 be64_to_cpu(left->bb_rightsib),
639                                 0, &rrbp, XFS_BMAP_BTREE_REF))) {
640                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
641                         goto error0;
642                 }
643                 rrblock = XFS_BUF_TO_BMBT_BLOCK(rrbp);
644                 if ((error = xfs_btree_check_lblock(cur, rrblock, level, rrbp))) {
645                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
646                         goto error0;
647                 }
648                 rrblock->bb_leftsib = cpu_to_be64(lbno);
649                 xfs_bmbt_log_block(cur, rrbp, XFS_BB_LEFTSIB);
650         }
651         xfs_bmap_add_free(XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(rbp)), 1,
652                 cur->bc_private.b.flist, mp);
653         cur->bc_private.b.ip->i_d.di_nblocks--;
654         xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
655         XFS_TRANS_MOD_DQUOT_BYINO(mp, cur->bc_tp, cur->bc_private.b.ip,
656                         XFS_TRANS_DQ_BCOUNT, -1L);
657         xfs_trans_binval(cur->bc_tp, rbp);
658         if (bp != lbp) {
659                 cur->bc_bufs[level] = lbp;
660                 cur->bc_ptrs[level] += lrecs;
661                 cur->bc_ra[level] = 0;
662         } else if ((error = xfs_bmbt_increment(cur, level + 1, &i))) {
663                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
664                 goto error0;
665         }
666         if (level > 0)
667                 cur->bc_ptrs[level]--;
668         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
669         *stat = 2;
670         return 0;
671
672 error0:
673         if (tcur)
674                 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
675         return error;
676 }
677
678 /*
679  * Insert one record/level.  Return information to the caller
680  * allowing the next level up to proceed if necessary.
681  */
682 STATIC int                                      /* error */
683 xfs_bmbt_insrec(
684         xfs_btree_cur_t         *cur,
685         int                     level,
686         xfs_fsblock_t           *bnop,
687         xfs_bmbt_rec_t          *recp,
688         xfs_btree_cur_t         **curp,
689         int                     *stat)          /* no-go/done/continue */
690 {
691         xfs_bmbt_block_t        *block;         /* bmap btree block */
692         xfs_buf_t               *bp;            /* buffer for block */
693         int                     error;          /* error return value */
694         int                     i;              /* loop index */
695         xfs_bmbt_key_t          key;            /* bmap btree key */
696         xfs_bmbt_key_t          *kp=NULL;       /* pointer to bmap btree key */
697         int                     logflags;       /* inode logging flags */
698         xfs_fsblock_t           nbno;           /* new block number */
699         struct xfs_btree_cur    *ncur;          /* new btree cursor */
700         __uint64_t              startoff;       /* new btree key value */
701         xfs_bmbt_rec_t          nrec;           /* new record count */
702         int                     optr;           /* old key/record index */
703         xfs_bmbt_ptr_t          *pp;            /* pointer to bmap block addr */
704         int                     ptr;            /* key/record index */
705         xfs_bmbt_rec_t          *rp=NULL;       /* pointer to bmap btree rec */
706         int                     numrecs;
707
708         ASSERT(level < cur->bc_nlevels);
709         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
710         XFS_BMBT_TRACE_ARGIFR(cur, level, *bnop, recp);
711         ncur = NULL;
712         key.br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(recp));
713         optr = ptr = cur->bc_ptrs[level];
714         if (ptr == 0) {
715                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
716                 *stat = 0;
717                 return 0;
718         }
719         XFS_STATS_INC(xs_bmbt_insrec);
720         block = xfs_bmbt_get_block(cur, level, &bp);
721         numrecs = be16_to_cpu(block->bb_numrecs);
722 #ifdef DEBUG
723         if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
724                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
725                 return error;
726         }
727         if (ptr <= numrecs) {
728                 if (level == 0) {
729                         rp = XFS_BMAP_REC_IADDR(block, ptr, cur);
730                         xfs_btree_check_rec(XFS_BTNUM_BMAP, recp, rp);
731                 } else {
732                         kp = XFS_BMAP_KEY_IADDR(block, ptr, cur);
733                         xfs_btree_check_key(XFS_BTNUM_BMAP, &key, kp);
734                 }
735         }
736 #endif
737         nbno = NULLFSBLOCK;
738         if (numrecs == XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
739                 if (numrecs < XFS_BMAP_BLOCK_DMAXRECS(level, cur)) {
740                         /*
741                          * A root block, that can be made bigger.
742                          */
743                         xfs_iroot_realloc(cur->bc_private.b.ip, 1,
744                                 cur->bc_private.b.whichfork);
745                         block = xfs_bmbt_get_block(cur, level, &bp);
746                 } else if (level == cur->bc_nlevels - 1) {
747                         if ((error = xfs_bmbt_newroot(cur, &logflags, stat)) ||
748                             *stat == 0) {
749                                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
750                                 return error;
751                         }
752                         xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
753                                 logflags);
754                         block = xfs_bmbt_get_block(cur, level, &bp);
755                 } else {
756                         if ((error = xfs_bmbt_rshift(cur, level, &i))) {
757                                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
758                                 return error;
759                         }
760                         if (i) {
761                                 /* nothing */
762                         } else {
763                                 if ((error = xfs_bmbt_lshift(cur, level, &i))) {
764                                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
765                                         return error;
766                                 }
767                                 if (i) {
768                                         optr = ptr = cur->bc_ptrs[level];
769                                 } else {
770                                         if ((error = xfs_bmbt_split(cur, level,
771                                                         &nbno, &startoff, &ncur,
772                                                         &i))) {
773                                                 XFS_BMBT_TRACE_CURSOR(cur,
774                                                         ERROR);
775                                                 return error;
776                                         }
777                                         if (i) {
778                                                 block = xfs_bmbt_get_block(
779                                                             cur, level, &bp);
780 #ifdef DEBUG
781                                                 if ((error =
782                                                     xfs_btree_check_lblock(cur,
783                                                             block, level, bp))) {
784                                                         XFS_BMBT_TRACE_CURSOR(
785                                                                 cur, ERROR);
786                                                         return error;
787                                                 }
788 #endif
789                                                 ptr = cur->bc_ptrs[level];
790                                                 xfs_bmbt_disk_set_allf(&nrec,
791                                                         startoff, 0, 0,
792                                                         XFS_EXT_NORM);
793                                         } else {
794                                                 XFS_BMBT_TRACE_CURSOR(cur,
795                                                         EXIT);
796                                                 *stat = 0;
797                                                 return 0;
798                                         }
799                                 }
800                         }
801                 }
802         }
803         numrecs = be16_to_cpu(block->bb_numrecs);
804         if (level > 0) {
805                 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
806                 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
807 #ifdef DEBUG
808                 for (i = numrecs; i >= ptr; i--) {
809                         if ((error = xfs_btree_check_lptr_disk(cur, pp[i - 1],
810                                         level))) {
811                                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
812                                 return error;
813                         }
814                 }
815 #endif
816                 memmove(&kp[ptr], &kp[ptr - 1],
817                         (numrecs - ptr + 1) * sizeof(*kp));
818                 memmove(&pp[ptr], &pp[ptr - 1], /* INT_: direct copy */
819                         (numrecs - ptr + 1) * sizeof(*pp));
820 #ifdef DEBUG
821                 if ((error = xfs_btree_check_lptr(cur, *bnop, level))) {
822                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
823                         return error;
824                 }
825 #endif
826                 kp[ptr - 1] = key;
827                 pp[ptr - 1] = cpu_to_be64(*bnop);
828                 numrecs++;
829                 block->bb_numrecs = cpu_to_be16(numrecs);
830                 xfs_bmbt_log_keys(cur, bp, ptr, numrecs);
831                 xfs_bmbt_log_ptrs(cur, bp, ptr, numrecs);
832         } else {
833                 rp = XFS_BMAP_REC_IADDR(block, 1, cur);
834                 memmove(&rp[ptr], &rp[ptr - 1],
835                         (numrecs - ptr + 1) * sizeof(*rp));
836                 rp[ptr - 1] = *recp;
837                 numrecs++;
838                 block->bb_numrecs = cpu_to_be16(numrecs);
839                 xfs_bmbt_log_recs(cur, bp, ptr, numrecs);
840         }
841         xfs_bmbt_log_block(cur, bp, XFS_BB_NUMRECS);
842 #ifdef DEBUG
843         if (ptr < numrecs) {
844                 if (level == 0)
845                         xfs_btree_check_rec(XFS_BTNUM_BMAP, rp + ptr - 1,
846                                 rp + ptr);
847                 else
848                         xfs_btree_check_key(XFS_BTNUM_BMAP, kp + ptr - 1,
849                                 kp + ptr);
850         }
851 #endif
852         if (optr == 1 && (error = xfs_bmbt_updkey(cur, &key, level + 1))) {
853                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
854                 return error;
855         }
856         *bnop = nbno;
857         if (nbno != NULLFSBLOCK) {
858                 *recp = nrec;
859                 *curp = ncur;
860         }
861         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
862         *stat = 1;
863         return 0;
864 }
865
866 STATIC int
867 xfs_bmbt_killroot(
868         xfs_btree_cur_t         *cur)
869 {
870         xfs_bmbt_block_t        *block;
871         xfs_bmbt_block_t        *cblock;
872         xfs_buf_t               *cbp;
873         xfs_bmbt_key_t          *ckp;
874         xfs_bmbt_ptr_t          *cpp;
875 #ifdef DEBUG
876         int                     error;
877 #endif
878         int                     i;
879         xfs_bmbt_key_t          *kp;
880         xfs_inode_t             *ip;
881         xfs_ifork_t             *ifp;
882         int                     level;
883         xfs_bmbt_ptr_t          *pp;
884
885         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
886         level = cur->bc_nlevels - 1;
887         ASSERT(level >= 1);
888         /*
889          * Don't deal with the root block needs to be a leaf case.
890          * We're just going to turn the thing back into extents anyway.
891          */
892         if (level == 1) {
893                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
894                 return 0;
895         }
896         block = xfs_bmbt_get_block(cur, level, &cbp);
897         /*
898          * Give up if the root has multiple children.
899          */
900         if (be16_to_cpu(block->bb_numrecs) != 1) {
901                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
902                 return 0;
903         }
904         /*
905          * Only do this if the next level will fit.
906          * Then the data must be copied up to the inode,
907          * instead of freeing the root you free the next level.
908          */
909         cbp = cur->bc_bufs[level - 1];
910         cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
911         if (be16_to_cpu(cblock->bb_numrecs) > XFS_BMAP_BLOCK_DMAXRECS(level, cur)) {
912                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
913                 return 0;
914         }
915         ASSERT(be64_to_cpu(cblock->bb_leftsib) == NULLDFSBNO);
916         ASSERT(be64_to_cpu(cblock->bb_rightsib) == NULLDFSBNO);
917         ip = cur->bc_private.b.ip;
918         ifp = XFS_IFORK_PTR(ip, cur->bc_private.b.whichfork);
919         ASSERT(XFS_BMAP_BLOCK_IMAXRECS(level, cur) ==
920                XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes));
921         i = (int)(be16_to_cpu(cblock->bb_numrecs) - XFS_BMAP_BLOCK_IMAXRECS(level, cur));
922         if (i) {
923                 xfs_iroot_realloc(ip, i, cur->bc_private.b.whichfork);
924                 block = ifp->if_broot;
925         }
926         be16_add(&block->bb_numrecs, i);
927         ASSERT(block->bb_numrecs == cblock->bb_numrecs);
928         kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
929         ckp = XFS_BMAP_KEY_IADDR(cblock, 1, cur);
930         memcpy(kp, ckp, be16_to_cpu(block->bb_numrecs) * sizeof(*kp));
931         pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
932         cpp = XFS_BMAP_PTR_IADDR(cblock, 1, cur);
933 #ifdef DEBUG
934         for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
935                 if ((error = xfs_btree_check_lptr_disk(cur, cpp[i], level - 1))) {
936                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
937                         return error;
938                 }
939         }
940 #endif
941         memcpy(pp, cpp, be16_to_cpu(block->bb_numrecs) * sizeof(*pp));
942         xfs_bmap_add_free(XFS_DADDR_TO_FSB(cur->bc_mp, XFS_BUF_ADDR(cbp)), 1,
943                         cur->bc_private.b.flist, cur->bc_mp);
944         ip->i_d.di_nblocks--;
945         XFS_TRANS_MOD_DQUOT_BYINO(cur->bc_mp, cur->bc_tp, ip,
946                         XFS_TRANS_DQ_BCOUNT, -1L);
947         xfs_trans_binval(cur->bc_tp, cbp);
948         cur->bc_bufs[level - 1] = NULL;
949         be16_add(&block->bb_level, -1);
950         xfs_trans_log_inode(cur->bc_tp, ip,
951                 XFS_ILOG_CORE | XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
952         cur->bc_nlevels--;
953         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
954         return 0;
955 }
956
957 /*
958  * Log key values from the btree block.
959  */
960 STATIC void
961 xfs_bmbt_log_keys(
962         xfs_btree_cur_t *cur,
963         xfs_buf_t       *bp,
964         int             kfirst,
965         int             klast)
966 {
967         xfs_trans_t     *tp;
968
969         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
970         XFS_BMBT_TRACE_ARGBII(cur, bp, kfirst, klast);
971         tp = cur->bc_tp;
972         if (bp) {
973                 xfs_bmbt_block_t        *block;
974                 int                     first;
975                 xfs_bmbt_key_t          *kp;
976                 int                     last;
977
978                 block = XFS_BUF_TO_BMBT_BLOCK(bp);
979                 kp = XFS_BMAP_KEY_DADDR(block, 1, cur);
980                 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
981                 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
982                 xfs_trans_log_buf(tp, bp, first, last);
983         } else {
984                 xfs_inode_t              *ip;
985
986                 ip = cur->bc_private.b.ip;
987                 xfs_trans_log_inode(tp, ip,
988                         XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
989         }
990         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
991 }
992
993 /*
994  * Log pointer values from the btree block.
995  */
996 STATIC void
997 xfs_bmbt_log_ptrs(
998         xfs_btree_cur_t *cur,
999         xfs_buf_t       *bp,
1000         int             pfirst,
1001         int             plast)
1002 {
1003         xfs_trans_t     *tp;
1004
1005         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1006         XFS_BMBT_TRACE_ARGBII(cur, bp, pfirst, plast);
1007         tp = cur->bc_tp;
1008         if (bp) {
1009                 xfs_bmbt_block_t        *block;
1010                 int                     first;
1011                 int                     last;
1012                 xfs_bmbt_ptr_t          *pp;
1013
1014                 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1015                 pp = XFS_BMAP_PTR_DADDR(block, 1, cur);
1016                 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
1017                 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
1018                 xfs_trans_log_buf(tp, bp, first, last);
1019         } else {
1020                 xfs_inode_t             *ip;
1021
1022                 ip = cur->bc_private.b.ip;
1023                 xfs_trans_log_inode(tp, ip,
1024                         XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
1025         }
1026         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1027 }
1028
1029 /*
1030  * Lookup the record.  The cursor is made to point to it, based on dir.
1031  */
1032 STATIC int                              /* error */
1033 xfs_bmbt_lookup(
1034         xfs_btree_cur_t         *cur,
1035         xfs_lookup_t            dir,
1036         int                     *stat)          /* success/failure */
1037 {
1038         xfs_bmbt_block_t        *block=NULL;
1039         xfs_buf_t               *bp;
1040         xfs_daddr_t             d;
1041         xfs_sfiloff_t           diff;
1042         int                     error;          /* error return value */
1043         xfs_fsblock_t           fsbno=0;
1044         int                     high;
1045         int                     i;
1046         int                     keyno=0;
1047         xfs_bmbt_key_t          *kkbase=NULL;
1048         xfs_bmbt_key_t          *kkp;
1049         xfs_bmbt_rec_t          *krbase=NULL;
1050         xfs_bmbt_rec_t          *krp;
1051         int                     level;
1052         int                     low;
1053         xfs_mount_t             *mp;
1054         xfs_bmbt_ptr_t          *pp;
1055         xfs_bmbt_irec_t         *rp;
1056         xfs_fileoff_t           startoff;
1057         xfs_trans_t             *tp;
1058
1059         XFS_STATS_INC(xs_bmbt_lookup);
1060         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1061         XFS_BMBT_TRACE_ARGI(cur, (int)dir);
1062         tp = cur->bc_tp;
1063         mp = cur->bc_mp;
1064         rp = &cur->bc_rec.b;
1065         for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1066                 if (level < cur->bc_nlevels - 1) {
1067                         d = XFS_FSB_TO_DADDR(mp, fsbno);
1068                         bp = cur->bc_bufs[level];
1069                         if (bp && XFS_BUF_ADDR(bp) != d)
1070                                 bp = NULL;
1071                         if (!bp) {
1072                                 if ((error = xfs_btree_read_bufl(mp, tp, fsbno,
1073                                                 0, &bp, XFS_BMAP_BTREE_REF))) {
1074                                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1075                                         return error;
1076                                 }
1077                                 xfs_btree_setbuf(cur, level, bp);
1078                                 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1079                                 if ((error = xfs_btree_check_lblock(cur, block,
1080                                                 level, bp))) {
1081                                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1082                                         return error;
1083                                 }
1084                         } else
1085                                 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1086                 } else
1087                         block = xfs_bmbt_get_block(cur, level, &bp);
1088                 if (diff == 0)
1089                         keyno = 1;
1090                 else {
1091                         if (level > 0)
1092                                 kkbase = XFS_BMAP_KEY_IADDR(block, 1, cur);
1093                         else
1094                                 krbase = XFS_BMAP_REC_IADDR(block, 1, cur);
1095                         low = 1;
1096                         if (!(high = be16_to_cpu(block->bb_numrecs))) {
1097                                 ASSERT(level == 0);
1098                                 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1099                                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1100                                 *stat = 0;
1101                                 return 0;
1102                         }
1103                         while (low <= high) {
1104                                 XFS_STATS_INC(xs_bmbt_compare);
1105                                 keyno = (low + high) >> 1;
1106                                 if (level > 0) {
1107                                         kkp = kkbase + keyno - 1;
1108                                         startoff = be64_to_cpu(kkp->br_startoff);
1109                                 } else {
1110                                         krp = krbase + keyno - 1;
1111                                         startoff = xfs_bmbt_disk_get_startoff(krp);
1112                                 }
1113                                 diff = (xfs_sfiloff_t)
1114                                                 (startoff - rp->br_startoff);
1115                                 if (diff < 0)
1116                                         low = keyno + 1;
1117                                 else if (diff > 0)
1118                                         high = keyno - 1;
1119                                 else
1120                                         break;
1121                         }
1122                 }
1123                 if (level > 0) {
1124                         if (diff > 0 && --keyno < 1)
1125                                 keyno = 1;
1126                         pp = XFS_BMAP_PTR_IADDR(block, keyno, cur);
1127                         fsbno = be64_to_cpu(*pp);
1128 #ifdef DEBUG
1129                         if ((error = xfs_btree_check_lptr(cur, fsbno, level))) {
1130                                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1131                                 return error;
1132                         }
1133 #endif
1134                         cur->bc_ptrs[level] = keyno;
1135                 }
1136         }
1137         if (dir != XFS_LOOKUP_LE && diff < 0) {
1138                 keyno++;
1139                 /*
1140                  * If ge search and we went off the end of the block, but it's
1141                  * not the last block, we're in the wrong block.
1142                  */
1143                 if (dir == XFS_LOOKUP_GE && keyno > be16_to_cpu(block->bb_numrecs) &&
1144                     be64_to_cpu(block->bb_rightsib) != NULLDFSBNO) {
1145                         cur->bc_ptrs[0] = keyno;
1146                         if ((error = xfs_bmbt_increment(cur, 0, &i))) {
1147                                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1148                                 return error;
1149                         }
1150                         XFS_WANT_CORRUPTED_RETURN(i == 1);
1151                         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1152                         *stat = 1;
1153                         return 0;
1154                 }
1155         }
1156         else if (dir == XFS_LOOKUP_LE && diff > 0)
1157                 keyno--;
1158         cur->bc_ptrs[0] = keyno;
1159         if (keyno == 0 || keyno > be16_to_cpu(block->bb_numrecs)) {
1160                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1161                 *stat = 0;
1162         } else {
1163                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1164                 *stat = ((dir != XFS_LOOKUP_EQ) || (diff == 0));
1165         }
1166         return 0;
1167 }
1168
1169 /*
1170  * Move 1 record left from cur/level if possible.
1171  * Update cur to reflect the new path.
1172  */
1173 STATIC int                                      /* error */
1174 xfs_bmbt_lshift(
1175         xfs_btree_cur_t         *cur,
1176         int                     level,
1177         int                     *stat)          /* success/failure */
1178 {
1179         int                     error;          /* error return value */
1180 #ifdef DEBUG
1181         int                     i;              /* loop counter */
1182 #endif
1183         xfs_bmbt_key_t          key;            /* bmap btree key */
1184         xfs_buf_t               *lbp;           /* left buffer pointer */
1185         xfs_bmbt_block_t        *left;          /* left btree block */
1186         xfs_bmbt_key_t          *lkp=NULL;      /* left btree key */
1187         xfs_bmbt_ptr_t          *lpp;           /* left address pointer */
1188         int                     lrecs;          /* left record count */
1189         xfs_bmbt_rec_t          *lrp=NULL;      /* left record pointer */
1190         xfs_mount_t             *mp;            /* file system mount point */
1191         xfs_buf_t               *rbp;           /* right buffer pointer */
1192         xfs_bmbt_block_t        *right;         /* right btree block */
1193         xfs_bmbt_key_t          *rkp=NULL;      /* right btree key */
1194         xfs_bmbt_ptr_t          *rpp=NULL;      /* right address pointer */
1195         xfs_bmbt_rec_t          *rrp=NULL;      /* right record pointer */
1196         int                     rrecs;          /* right record count */
1197
1198         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1199         XFS_BMBT_TRACE_ARGI(cur, level);
1200         if (level == cur->bc_nlevels - 1) {
1201                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1202                 *stat = 0;
1203                 return 0;
1204         }
1205         rbp = cur->bc_bufs[level];
1206         right = XFS_BUF_TO_BMBT_BLOCK(rbp);
1207 #ifdef DEBUG
1208         if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
1209                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1210                 return error;
1211         }
1212 #endif
1213         if (be64_to_cpu(right->bb_leftsib) == NULLDFSBNO) {
1214                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1215                 *stat = 0;
1216                 return 0;
1217         }
1218         if (cur->bc_ptrs[level] <= 1) {
1219                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1220                 *stat = 0;
1221                 return 0;
1222         }
1223         mp = cur->bc_mp;
1224         if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, be64_to_cpu(right->bb_leftsib), 0,
1225                         &lbp, XFS_BMAP_BTREE_REF))) {
1226                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1227                 return error;
1228         }
1229         left = XFS_BUF_TO_BMBT_BLOCK(lbp);
1230         if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
1231                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1232                 return error;
1233         }
1234         if (be16_to_cpu(left->bb_numrecs) == XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
1235                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1236                 *stat = 0;
1237                 return 0;
1238         }
1239         lrecs = be16_to_cpu(left->bb_numrecs) + 1;
1240         if (level > 0) {
1241                 lkp = XFS_BMAP_KEY_IADDR(left, lrecs, cur);
1242                 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
1243                 *lkp = *rkp;
1244                 xfs_bmbt_log_keys(cur, lbp, lrecs, lrecs);
1245                 lpp = XFS_BMAP_PTR_IADDR(left, lrecs, cur);
1246                 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
1247 #ifdef DEBUG
1248                 if ((error = xfs_btree_check_lptr_disk(cur, *rpp, level))) {
1249                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1250                         return error;
1251                 }
1252 #endif
1253                 *lpp = *rpp; /* INT_: direct copy */
1254                 xfs_bmbt_log_ptrs(cur, lbp, lrecs, lrecs);
1255         } else {
1256                 lrp = XFS_BMAP_REC_IADDR(left, lrecs, cur);
1257                 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
1258                 *lrp = *rrp;
1259                 xfs_bmbt_log_recs(cur, lbp, lrecs, lrecs);
1260         }
1261         left->bb_numrecs = cpu_to_be16(lrecs);
1262         xfs_bmbt_log_block(cur, lbp, XFS_BB_NUMRECS);
1263 #ifdef DEBUG
1264         if (level > 0)
1265                 xfs_btree_check_key(XFS_BTNUM_BMAP, lkp - 1, lkp);
1266         else
1267                 xfs_btree_check_rec(XFS_BTNUM_BMAP, lrp - 1, lrp);
1268 #endif
1269         rrecs = be16_to_cpu(right->bb_numrecs) - 1;
1270         right->bb_numrecs = cpu_to_be16(rrecs);
1271         xfs_bmbt_log_block(cur, rbp, XFS_BB_NUMRECS);
1272         if (level > 0) {
1273 #ifdef DEBUG
1274                 for (i = 0; i < rrecs; i++) {
1275                         if ((error = xfs_btree_check_lptr_disk(cur, rpp[i + 1],
1276                                         level))) {
1277                                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1278                                 return error;
1279                         }
1280                 }
1281 #endif
1282                 memmove(rkp, rkp + 1, rrecs * sizeof(*rkp));
1283                 memmove(rpp, rpp + 1, rrecs * sizeof(*rpp));
1284                 xfs_bmbt_log_keys(cur, rbp, 1, rrecs);
1285                 xfs_bmbt_log_ptrs(cur, rbp, 1, rrecs);
1286         } else {
1287                 memmove(rrp, rrp + 1, rrecs * sizeof(*rrp));
1288                 xfs_bmbt_log_recs(cur, rbp, 1, rrecs);
1289                 key.br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(rrp));
1290                 rkp = &key;
1291         }
1292         if ((error = xfs_bmbt_updkey(cur, rkp, level + 1))) {
1293                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1294                 return error;
1295         }
1296         cur->bc_ptrs[level]--;
1297         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1298         *stat = 1;
1299         return 0;
1300 }
1301
1302 /*
1303  * Move 1 record right from cur/level if possible.
1304  * Update cur to reflect the new path.
1305  */
1306 STATIC int                                      /* error */
1307 xfs_bmbt_rshift(
1308         xfs_btree_cur_t         *cur,
1309         int                     level,
1310         int                     *stat)          /* success/failure */
1311 {
1312         int                     error;          /* error return value */
1313         int                     i;              /* loop counter */
1314         xfs_bmbt_key_t          key;            /* bmap btree key */
1315         xfs_buf_t               *lbp;           /* left buffer pointer */
1316         xfs_bmbt_block_t        *left;          /* left btree block */
1317         xfs_bmbt_key_t          *lkp;           /* left btree key */
1318         xfs_bmbt_ptr_t          *lpp;           /* left address pointer */
1319         xfs_bmbt_rec_t          *lrp;           /* left record pointer */
1320         xfs_mount_t             *mp;            /* file system mount point */
1321         xfs_buf_t               *rbp;           /* right buffer pointer */
1322         xfs_bmbt_block_t        *right;         /* right btree block */
1323         xfs_bmbt_key_t          *rkp;           /* right btree key */
1324         xfs_bmbt_ptr_t          *rpp;           /* right address pointer */
1325         xfs_bmbt_rec_t          *rrp=NULL;      /* right record pointer */
1326         struct xfs_btree_cur    *tcur;          /* temporary btree cursor */
1327
1328         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1329         XFS_BMBT_TRACE_ARGI(cur, level);
1330         if (level == cur->bc_nlevels - 1) {
1331                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1332                 *stat = 0;
1333                 return 0;
1334         }
1335         lbp = cur->bc_bufs[level];
1336         left = XFS_BUF_TO_BMBT_BLOCK(lbp);
1337 #ifdef DEBUG
1338         if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
1339                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1340                 return error;
1341         }
1342 #endif
1343         if (be64_to_cpu(left->bb_rightsib) == NULLDFSBNO) {
1344                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1345                 *stat = 0;
1346                 return 0;
1347         }
1348         if (cur->bc_ptrs[level] >= be16_to_cpu(left->bb_numrecs)) {
1349                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1350                 *stat = 0;
1351                 return 0;
1352         }
1353         mp = cur->bc_mp;
1354         if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, be64_to_cpu(left->bb_rightsib), 0,
1355                         &rbp, XFS_BMAP_BTREE_REF))) {
1356                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1357                 return error;
1358         }
1359         right = XFS_BUF_TO_BMBT_BLOCK(rbp);
1360         if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
1361                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1362                 return error;
1363         }
1364         if (be16_to_cpu(right->bb_numrecs) == XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
1365                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1366                 *stat = 0;
1367                 return 0;
1368         }
1369         if (level > 0) {
1370                 lkp = XFS_BMAP_KEY_IADDR(left, be16_to_cpu(left->bb_numrecs), cur);
1371                 lpp = XFS_BMAP_PTR_IADDR(left, be16_to_cpu(left->bb_numrecs), cur);
1372                 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
1373                 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
1374 #ifdef DEBUG
1375                 for (i = be16_to_cpu(right->bb_numrecs) - 1; i >= 0; i--) {
1376                         if ((error = xfs_btree_check_lptr_disk(cur, rpp[i], level))) {
1377                                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1378                                 return error;
1379                         }
1380                 }
1381 #endif
1382                 memmove(rkp + 1, rkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1383                 memmove(rpp + 1, rpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1384 #ifdef DEBUG
1385                 if ((error = xfs_btree_check_lptr_disk(cur, *lpp, level))) {
1386                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1387                         return error;
1388                 }
1389 #endif
1390                 *rkp = *lkp;
1391                 *rpp = *lpp; /* INT_: direct copy */
1392                 xfs_bmbt_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
1393                 xfs_bmbt_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
1394         } else {
1395                 lrp = XFS_BMAP_REC_IADDR(left, be16_to_cpu(left->bb_numrecs), cur);
1396                 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
1397                 memmove(rrp + 1, rrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1398                 *rrp = *lrp;
1399                 xfs_bmbt_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
1400                 key.br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(rrp));
1401                 rkp = &key;
1402         }
1403         be16_add(&left->bb_numrecs, -1);
1404         xfs_bmbt_log_block(cur, lbp, XFS_BB_NUMRECS);
1405         be16_add(&right->bb_numrecs, 1);
1406 #ifdef DEBUG
1407         if (level > 0)
1408                 xfs_btree_check_key(XFS_BTNUM_BMAP, rkp, rkp + 1);
1409         else
1410                 xfs_btree_check_rec(XFS_BTNUM_BMAP, rrp, rrp + 1);
1411 #endif
1412         xfs_bmbt_log_block(cur, rbp, XFS_BB_NUMRECS);
1413         if ((error = xfs_btree_dup_cursor(cur, &tcur))) {
1414                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1415                 return error;
1416         }
1417         i = xfs_btree_lastrec(tcur, level);
1418         XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1419         if ((error = xfs_bmbt_increment(tcur, level, &i))) {
1420                 XFS_BMBT_TRACE_CURSOR(tcur, ERROR);
1421                 goto error1;
1422         }
1423         XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1424         if ((error = xfs_bmbt_updkey(tcur, rkp, level + 1))) {
1425                 XFS_BMBT_TRACE_CURSOR(tcur, ERROR);
1426                 goto error1;
1427         }
1428         xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1429         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1430         *stat = 1;
1431         return 0;
1432 error0:
1433         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1434 error1:
1435         xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1436         return error;
1437 }
1438
1439 /*
1440  * Determine the extent state.
1441  */
1442 /* ARGSUSED */
1443 STATIC xfs_exntst_t
1444 xfs_extent_state(
1445         xfs_filblks_t           blks,
1446         int                     extent_flag)
1447 {
1448         if (extent_flag) {
1449                 ASSERT(blks != 0);      /* saved for DMIG */
1450                 return XFS_EXT_UNWRITTEN;
1451         }
1452         return XFS_EXT_NORM;
1453 }
1454
1455
1456 /*
1457  * Split cur/level block in half.
1458  * Return new block number and its first record (to be inserted into parent).
1459  */
1460 STATIC int                                      /* error */
1461 xfs_bmbt_split(
1462         xfs_btree_cur_t         *cur,
1463         int                     level,
1464         xfs_fsblock_t           *bnop,
1465         __uint64_t              *startoff,
1466         xfs_btree_cur_t         **curp,
1467         int                     *stat)          /* success/failure */
1468 {
1469         xfs_alloc_arg_t         args;           /* block allocation args */
1470         int                     error;          /* error return value */
1471         int                     i;              /* loop counter */
1472         xfs_fsblock_t           lbno;           /* left sibling block number */
1473         xfs_buf_t               *lbp;           /* left buffer pointer */
1474         xfs_bmbt_block_t        *left;          /* left btree block */
1475         xfs_bmbt_key_t          *lkp;           /* left btree key */
1476         xfs_bmbt_ptr_t          *lpp;           /* left address pointer */
1477         xfs_bmbt_rec_t          *lrp;           /* left record pointer */
1478         xfs_buf_t               *rbp;           /* right buffer pointer */
1479         xfs_bmbt_block_t        *right;         /* right btree block */
1480         xfs_bmbt_key_t          *rkp;           /* right btree key */
1481         xfs_bmbt_ptr_t          *rpp;           /* right address pointer */
1482         xfs_bmbt_block_t        *rrblock;       /* right-right btree block */
1483         xfs_buf_t               *rrbp;          /* right-right buffer pointer */
1484         xfs_bmbt_rec_t          *rrp;           /* right record pointer */
1485
1486         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1487         XFS_BMBT_TRACE_ARGIFK(cur, level, *bnop, *startoff);
1488         args.tp = cur->bc_tp;
1489         args.mp = cur->bc_mp;
1490         lbp = cur->bc_bufs[level];
1491         lbno = XFS_DADDR_TO_FSB(args.mp, XFS_BUF_ADDR(lbp));
1492         left = XFS_BUF_TO_BMBT_BLOCK(lbp);
1493         args.fsbno = cur->bc_private.b.firstblock;
1494         args.firstblock = args.fsbno;
1495         if (args.fsbno == NULLFSBLOCK) {
1496                 args.fsbno = lbno;
1497                 args.type = XFS_ALLOCTYPE_START_BNO;
1498         } else
1499                 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1500         args.mod = args.minleft = args.alignment = args.total = args.isfl =
1501                 args.userdata = args.minalignslop = 0;
1502         args.minlen = args.maxlen = args.prod = 1;
1503         args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
1504         if (!args.wasdel && xfs_trans_get_block_res(args.tp) == 0) {
1505                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1506                 return XFS_ERROR(ENOSPC);
1507         }
1508         if ((error = xfs_alloc_vextent(&args))) {
1509                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1510                 return error;
1511         }
1512         if (args.fsbno == NULLFSBLOCK) {
1513                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1514                 *stat = 0;
1515                 return 0;
1516         }
1517         ASSERT(args.len == 1);
1518         cur->bc_private.b.firstblock = args.fsbno;
1519         cur->bc_private.b.allocated++;
1520         cur->bc_private.b.ip->i_d.di_nblocks++;
1521         xfs_trans_log_inode(args.tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
1522         XFS_TRANS_MOD_DQUOT_BYINO(args.mp, args.tp, cur->bc_private.b.ip,
1523                         XFS_TRANS_DQ_BCOUNT, 1L);
1524         rbp = xfs_btree_get_bufl(args.mp, args.tp, args.fsbno, 0);
1525         right = XFS_BUF_TO_BMBT_BLOCK(rbp);
1526 #ifdef DEBUG
1527         if ((error = xfs_btree_check_lblock(cur, left, level, rbp))) {
1528                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1529                 return error;
1530         }
1531 #endif
1532         right->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
1533         right->bb_level = left->bb_level;
1534         right->bb_numrecs = cpu_to_be16(be16_to_cpu(left->bb_numrecs) / 2);
1535         if ((be16_to_cpu(left->bb_numrecs) & 1) &&
1536             cur->bc_ptrs[level] <= be16_to_cpu(right->bb_numrecs) + 1)
1537                 be16_add(&right->bb_numrecs, 1);
1538         i = be16_to_cpu(left->bb_numrecs) - be16_to_cpu(right->bb_numrecs) + 1;
1539         if (level > 0) {
1540                 lkp = XFS_BMAP_KEY_IADDR(left, i, cur);
1541                 lpp = XFS_BMAP_PTR_IADDR(left, i, cur);
1542                 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
1543                 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
1544 #ifdef DEBUG
1545                 for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
1546                         if ((error = xfs_btree_check_lptr_disk(cur, lpp[i], level))) {
1547                                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1548                                 return error;
1549                         }
1550                 }
1551 #endif
1552                 memcpy(rkp, lkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1553                 memcpy(rpp, lpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1554                 xfs_bmbt_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1555                 xfs_bmbt_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1556                 *startoff = be64_to_cpu(rkp->br_startoff);
1557         } else {
1558                 lrp = XFS_BMAP_REC_IADDR(left, i, cur);
1559                 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
1560                 memcpy(rrp, lrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1561                 xfs_bmbt_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1562                 *startoff = xfs_bmbt_disk_get_startoff(rrp);
1563         }
1564         be16_add(&left->bb_numrecs, -(be16_to_cpu(right->bb_numrecs)));
1565         right->bb_rightsib = left->bb_rightsib;
1566         left->bb_rightsib = cpu_to_be64(args.fsbno);
1567         right->bb_leftsib = cpu_to_be64(lbno);
1568         xfs_bmbt_log_block(cur, rbp, XFS_BB_ALL_BITS);
1569         xfs_bmbt_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
1570         if (be64_to_cpu(right->bb_rightsib) != NULLDFSBNO) {
1571                 if ((error = xfs_btree_read_bufl(args.mp, args.tp,
1572                                 be64_to_cpu(right->bb_rightsib), 0, &rrbp,
1573                                 XFS_BMAP_BTREE_REF))) {
1574                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1575                         return error;
1576                 }
1577                 rrblock = XFS_BUF_TO_BMBT_BLOCK(rrbp);
1578                 if ((error = xfs_btree_check_lblock(cur, rrblock, level, rrbp))) {
1579                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1580                         return error;
1581                 }
1582                 rrblock->bb_leftsib = cpu_to_be64(args.fsbno);
1583                 xfs_bmbt_log_block(cur, rrbp, XFS_BB_LEFTSIB);
1584         }
1585         if (cur->bc_ptrs[level] > be16_to_cpu(left->bb_numrecs) + 1) {
1586                 xfs_btree_setbuf(cur, level, rbp);
1587                 cur->bc_ptrs[level] -= be16_to_cpu(left->bb_numrecs);
1588         }
1589         if (level + 1 < cur->bc_nlevels) {
1590                 if ((error = xfs_btree_dup_cursor(cur, curp))) {
1591                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1592                         return error;
1593                 }
1594                 (*curp)->bc_ptrs[level + 1]++;
1595         }
1596         *bnop = args.fsbno;
1597         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1598         *stat = 1;
1599         return 0;
1600 }
1601
1602
1603 /*
1604  * Update keys for the record.
1605  */
1606 STATIC int
1607 xfs_bmbt_updkey(
1608         xfs_btree_cur_t         *cur,
1609         xfs_bmbt_key_t          *keyp,  /* on-disk format */
1610         int                     level)
1611 {
1612         xfs_bmbt_block_t        *block;
1613         xfs_buf_t               *bp;
1614 #ifdef DEBUG
1615         int                     error;
1616 #endif
1617         xfs_bmbt_key_t          *kp;
1618         int                     ptr;
1619
1620         ASSERT(level >= 1);
1621         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1622         XFS_BMBT_TRACE_ARGIK(cur, level, keyp);
1623         for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1624                 block = xfs_bmbt_get_block(cur, level, &bp);
1625 #ifdef DEBUG
1626                 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
1627                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1628                         return error;
1629                 }
1630 #endif
1631                 ptr = cur->bc_ptrs[level];
1632                 kp = XFS_BMAP_KEY_IADDR(block, ptr, cur);
1633                 *kp = *keyp;
1634                 xfs_bmbt_log_keys(cur, bp, ptr, ptr);
1635         }
1636         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1637         return 0;
1638 }
1639
1640 /*
1641  * Convert on-disk form of btree root to in-memory form.
1642  */
1643 void
1644 xfs_bmdr_to_bmbt(
1645         xfs_bmdr_block_t        *dblock,
1646         int                     dblocklen,
1647         xfs_bmbt_block_t        *rblock,
1648         int                     rblocklen)
1649 {
1650         int                     dmxr;
1651         xfs_bmbt_key_t          *fkp;
1652         __be64                  *fpp;
1653         xfs_bmbt_key_t          *tkp;
1654         __be64                  *tpp;
1655
1656         rblock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
1657         rblock->bb_level = dblock->bb_level;
1658         ASSERT(be16_to_cpu(rblock->bb_level) > 0);
1659         rblock->bb_numrecs = dblock->bb_numrecs;
1660         rblock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
1661         rblock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
1662         dmxr = (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen, xfs_bmdr, 0);
1663         fkp = XFS_BTREE_KEY_ADDR(xfs_bmdr, dblock, 1);
1664         tkp = XFS_BMAP_BROOT_KEY_ADDR(rblock, 1, rblocklen);
1665         fpp = XFS_BTREE_PTR_ADDR(xfs_bmdr, dblock, 1, dmxr);
1666         tpp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, rblocklen);
1667         dmxr = be16_to_cpu(dblock->bb_numrecs);
1668         memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
1669         memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
1670 }
1671
1672 /*
1673  * Decrement cursor by one record at the level.
1674  * For nonzero levels the leaf-ward information is untouched.
1675  */
1676 int                                             /* error */
1677 xfs_bmbt_decrement(
1678         xfs_btree_cur_t         *cur,
1679         int                     level,
1680         int                     *stat)          /* success/failure */
1681 {
1682         xfs_bmbt_block_t        *block;
1683         xfs_buf_t               *bp;
1684         int                     error;          /* error return value */
1685         xfs_fsblock_t           fsbno;
1686         int                     lev;
1687         xfs_mount_t             *mp;
1688         xfs_trans_t             *tp;
1689
1690         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1691         XFS_BMBT_TRACE_ARGI(cur, level);
1692         ASSERT(level < cur->bc_nlevels);
1693         if (level < cur->bc_nlevels - 1)
1694                 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1695         if (--cur->bc_ptrs[level] > 0) {
1696                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1697                 *stat = 1;
1698                 return 0;
1699         }
1700         block = xfs_bmbt_get_block(cur, level, &bp);
1701 #ifdef DEBUG
1702         if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
1703                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1704                 return error;
1705         }
1706 #endif
1707         if (be64_to_cpu(block->bb_leftsib) == NULLDFSBNO) {
1708                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1709                 *stat = 0;
1710                 return 0;
1711         }
1712         for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1713                 if (--cur->bc_ptrs[lev] > 0)
1714                         break;
1715                 if (lev < cur->bc_nlevels - 1)
1716                         xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1717         }
1718         if (lev == cur->bc_nlevels) {
1719                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1720                 *stat = 0;
1721                 return 0;
1722         }
1723         tp = cur->bc_tp;
1724         mp = cur->bc_mp;
1725         for (block = xfs_bmbt_get_block(cur, lev, &bp); lev > level; ) {
1726                 fsbno = be64_to_cpu(*XFS_BMAP_PTR_IADDR(block, cur->bc_ptrs[lev], cur));
1727                 if ((error = xfs_btree_read_bufl(mp, tp, fsbno, 0, &bp,
1728                                 XFS_BMAP_BTREE_REF))) {
1729                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1730                         return error;
1731                 }
1732                 lev--;
1733                 xfs_btree_setbuf(cur, lev, bp);
1734                 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1735                 if ((error = xfs_btree_check_lblock(cur, block, lev, bp))) {
1736                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1737                         return error;
1738                 }
1739                 cur->bc_ptrs[lev] = be16_to_cpu(block->bb_numrecs);
1740         }
1741         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1742         *stat = 1;
1743         return 0;
1744 }
1745
1746 /*
1747  * Delete the record pointed to by cur.
1748  */
1749 int                                     /* error */
1750 xfs_bmbt_delete(
1751         xfs_btree_cur_t *cur,
1752         int             *stat)          /* success/failure */
1753 {
1754         int             error;          /* error return value */
1755         int             i;
1756         int             level;
1757
1758         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1759         for (level = 0, i = 2; i == 2; level++) {
1760                 if ((error = xfs_bmbt_delrec(cur, level, &i))) {
1761                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1762                         return error;
1763                 }
1764         }
1765         if (i == 0) {
1766                 for (level = 1; level < cur->bc_nlevels; level++) {
1767                         if (cur->bc_ptrs[level] == 0) {
1768                                 if ((error = xfs_bmbt_decrement(cur, level,
1769                                                 &i))) {
1770                                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1771                                         return error;
1772                                 }
1773                                 break;
1774                         }
1775                 }
1776         }
1777         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1778         *stat = i;
1779         return 0;
1780 }
1781
1782 /*
1783  * Convert a compressed bmap extent record to an uncompressed form.
1784  * This code must be in sync with the routines xfs_bmbt_get_startoff,
1785  * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
1786  */
1787
1788 STATIC_INLINE void
1789 __xfs_bmbt_get_all(
1790                 __uint64_t l0,
1791                 __uint64_t l1,
1792                 xfs_bmbt_irec_t *s)
1793 {
1794         int     ext_flag;
1795         xfs_exntst_t st;
1796
1797         ext_flag = (int)(l0 >> (64 - BMBT_EXNTFLAG_BITLEN));
1798         s->br_startoff = ((xfs_fileoff_t)l0 &
1799                            XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
1800 #if XFS_BIG_BLKNOS
1801         s->br_startblock = (((xfs_fsblock_t)l0 & XFS_MASK64LO(9)) << 43) |
1802                            (((xfs_fsblock_t)l1) >> 21);
1803 #else
1804 #ifdef DEBUG
1805         {
1806                 xfs_dfsbno_t    b;
1807
1808                 b = (((xfs_dfsbno_t)l0 & XFS_MASK64LO(9)) << 43) |
1809                     (((xfs_dfsbno_t)l1) >> 21);
1810                 ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
1811                 s->br_startblock = (xfs_fsblock_t)b;
1812         }
1813 #else   /* !DEBUG */
1814         s->br_startblock = (xfs_fsblock_t)(((xfs_dfsbno_t)l1) >> 21);
1815 #endif  /* DEBUG */
1816 #endif  /* XFS_BIG_BLKNOS */
1817         s->br_blockcount = (xfs_filblks_t)(l1 & XFS_MASK64LO(21));
1818         /* This is xfs_extent_state() in-line */
1819         if (ext_flag) {
1820                 ASSERT(s->br_blockcount != 0);  /* saved for DMIG */
1821                 st = XFS_EXT_UNWRITTEN;
1822         } else
1823                 st = XFS_EXT_NORM;
1824         s->br_state = st;
1825 }
1826
1827 void
1828 xfs_bmbt_get_all(
1829         xfs_bmbt_rec_t  *r,
1830         xfs_bmbt_irec_t *s)
1831 {
1832         __xfs_bmbt_get_all(r->l0, r->l1, s);
1833 }
1834
1835 /*
1836  * Get the block pointer for the given level of the cursor.
1837  * Fill in the buffer pointer, if applicable.
1838  */
1839 xfs_bmbt_block_t *
1840 xfs_bmbt_get_block(
1841         xfs_btree_cur_t         *cur,
1842         int                     level,
1843         xfs_buf_t               **bpp)
1844 {
1845         xfs_ifork_t             *ifp;
1846         xfs_bmbt_block_t        *rval;
1847
1848         if (level < cur->bc_nlevels - 1) {
1849                 *bpp = cur->bc_bufs[level];
1850                 rval = XFS_BUF_TO_BMBT_BLOCK(*bpp);
1851         } else {
1852                 *bpp = NULL;
1853                 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
1854                         cur->bc_private.b.whichfork);
1855                 rval = ifp->if_broot;
1856         }
1857         return rval;
1858 }
1859
1860 /*
1861  * Extract the blockcount field from an in memory bmap extent record.
1862  */
1863 xfs_filblks_t
1864 xfs_bmbt_get_blockcount(
1865         xfs_bmbt_rec_t  *r)
1866 {
1867         return (xfs_filblks_t)(r->l1 & XFS_MASK64LO(21));
1868 }
1869
1870 /*
1871  * Extract the startblock field from an in memory bmap extent record.
1872  */
1873 xfs_fsblock_t
1874 xfs_bmbt_get_startblock(
1875         xfs_bmbt_rec_t  *r)
1876 {
1877 #if XFS_BIG_BLKNOS
1878         return (((xfs_fsblock_t)r->l0 & XFS_MASK64LO(9)) << 43) |
1879                (((xfs_fsblock_t)r->l1) >> 21);
1880 #else
1881 #ifdef DEBUG
1882         xfs_dfsbno_t    b;
1883
1884         b = (((xfs_dfsbno_t)r->l0 & XFS_MASK64LO(9)) << 43) |
1885             (((xfs_dfsbno_t)r->l1) >> 21);
1886         ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
1887         return (xfs_fsblock_t)b;
1888 #else   /* !DEBUG */
1889         return (xfs_fsblock_t)(((xfs_dfsbno_t)r->l1) >> 21);
1890 #endif  /* DEBUG */
1891 #endif  /* XFS_BIG_BLKNOS */
1892 }
1893
1894 /*
1895  * Extract the startoff field from an in memory bmap extent record.
1896  */
1897 xfs_fileoff_t
1898 xfs_bmbt_get_startoff(
1899         xfs_bmbt_rec_t  *r)
1900 {
1901         return ((xfs_fileoff_t)r->l0 &
1902                  XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
1903 }
1904
1905 xfs_exntst_t
1906 xfs_bmbt_get_state(
1907         xfs_bmbt_rec_t  *r)
1908 {
1909         int     ext_flag;
1910
1911         ext_flag = (int)((r->l0) >> (64 - BMBT_EXNTFLAG_BITLEN));
1912         return xfs_extent_state(xfs_bmbt_get_blockcount(r),
1913                                 ext_flag);
1914 }
1915
1916 #ifndef XFS_NATIVE_HOST
1917 /* Endian flipping versions of the bmbt extraction functions */
1918 void
1919 xfs_bmbt_disk_get_all(
1920         xfs_bmbt_rec_t  *r,
1921         xfs_bmbt_irec_t *s)
1922 {
1923         __uint64_t      l0, l1;
1924
1925         l0 = INT_GET(r->l0, ARCH_CONVERT);
1926         l1 = INT_GET(r->l1, ARCH_CONVERT);
1927
1928         __xfs_bmbt_get_all(l0, l1, s);
1929 }
1930
1931 /*
1932  * Extract the blockcount field from an on disk bmap extent record.
1933  */
1934 xfs_filblks_t
1935 xfs_bmbt_disk_get_blockcount(
1936         xfs_bmbt_rec_t  *r)
1937 {
1938         return (xfs_filblks_t)(INT_GET(r->l1, ARCH_CONVERT) & XFS_MASK64LO(21));
1939 }
1940
1941 /*
1942  * Extract the startoff field from a disk format bmap extent record.
1943  */
1944 xfs_fileoff_t
1945 xfs_bmbt_disk_get_startoff(
1946         xfs_bmbt_rec_t  *r)
1947 {
1948         return ((xfs_fileoff_t)INT_GET(r->l0, ARCH_CONVERT) &
1949                  XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
1950 }
1951 #endif /* XFS_NATIVE_HOST */
1952
1953
1954 /*
1955  * Increment cursor by one record at the level.
1956  * For nonzero levels the leaf-ward information is untouched.
1957  */
1958 int                                             /* error */
1959 xfs_bmbt_increment(
1960         xfs_btree_cur_t         *cur,
1961         int                     level,
1962         int                     *stat)          /* success/failure */
1963 {
1964         xfs_bmbt_block_t        *block;
1965         xfs_buf_t               *bp;
1966         int                     error;          /* error return value */
1967         xfs_fsblock_t           fsbno;
1968         int                     lev;
1969         xfs_mount_t             *mp;
1970         xfs_trans_t             *tp;
1971
1972         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1973         XFS_BMBT_TRACE_ARGI(cur, level);
1974         ASSERT(level < cur->bc_nlevels);
1975         if (level < cur->bc_nlevels - 1)
1976                 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1977         block = xfs_bmbt_get_block(cur, level, &bp);
1978 #ifdef DEBUG
1979         if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
1980                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1981                 return error;
1982         }
1983 #endif
1984         if (++cur->bc_ptrs[level] <= be16_to_cpu(block->bb_numrecs)) {
1985                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1986                 *stat = 1;
1987                 return 0;
1988         }
1989         if (be64_to_cpu(block->bb_rightsib) == NULLDFSBNO) {
1990                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1991                 *stat = 0;
1992                 return 0;
1993         }
1994         for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1995                 block = xfs_bmbt_get_block(cur, lev, &bp);
1996 #ifdef DEBUG
1997                 if ((error = xfs_btree_check_lblock(cur, block, lev, bp))) {
1998                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1999                         return error;
2000                 }
2001 #endif
2002                 if (++cur->bc_ptrs[lev] <= be16_to_cpu(block->bb_numrecs))
2003                         break;
2004                 if (lev < cur->bc_nlevels - 1)
2005                         xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
2006         }
2007         if (lev == cur->bc_nlevels) {
2008                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2009                 *stat = 0;
2010                 return 0;
2011         }
2012         tp = cur->bc_tp;
2013         mp = cur->bc_mp;
2014         for (block = xfs_bmbt_get_block(cur, lev, &bp); lev > level; ) {
2015                 fsbno = be64_to_cpu(*XFS_BMAP_PTR_IADDR(block, cur->bc_ptrs[lev], cur));
2016                 if ((error = xfs_btree_read_bufl(mp, tp, fsbno, 0, &bp,
2017                                 XFS_BMAP_BTREE_REF))) {
2018                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2019                         return error;
2020                 }
2021                 lev--;
2022                 xfs_btree_setbuf(cur, lev, bp);
2023                 block = XFS_BUF_TO_BMBT_BLOCK(bp);
2024                 if ((error = xfs_btree_check_lblock(cur, block, lev, bp))) {
2025                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2026                         return error;
2027                 }
2028                 cur->bc_ptrs[lev] = 1;
2029         }
2030         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2031         *stat = 1;
2032         return 0;
2033 }
2034
2035 /*
2036  * Insert the current record at the point referenced by cur.
2037  */
2038 int                                     /* error */
2039 xfs_bmbt_insert(
2040         xfs_btree_cur_t *cur,
2041         int             *stat)          /* success/failure */
2042 {
2043         int             error;          /* error return value */
2044         int             i;
2045         int             level;
2046         xfs_fsblock_t   nbno;
2047         xfs_btree_cur_t *ncur;
2048         xfs_bmbt_rec_t  nrec;
2049         xfs_btree_cur_t *pcur;
2050
2051         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2052         level = 0;
2053         nbno = NULLFSBLOCK;
2054         xfs_bmbt_disk_set_all(&nrec, &cur->bc_rec.b);
2055         ncur = NULL;
2056         pcur = cur;
2057         do {
2058                 if ((error = xfs_bmbt_insrec(pcur, level++, &nbno, &nrec, &ncur,
2059                                 &i))) {
2060                         if (pcur != cur)
2061                                 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
2062                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2063                         return error;
2064                 }
2065                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2066                 if (pcur != cur && (ncur || nbno == NULLFSBLOCK)) {
2067                         cur->bc_nlevels = pcur->bc_nlevels;
2068                         cur->bc_private.b.allocated +=
2069                                 pcur->bc_private.b.allocated;
2070                         pcur->bc_private.b.allocated = 0;
2071                         ASSERT((cur->bc_private.b.firstblock != NULLFSBLOCK) ||
2072                                (cur->bc_private.b.ip->i_d.di_flags &
2073                                 XFS_DIFLAG_REALTIME));
2074                         cur->bc_private.b.firstblock =
2075                                 pcur->bc_private.b.firstblock;
2076                         ASSERT(cur->bc_private.b.flist ==
2077                                pcur->bc_private.b.flist);
2078                         xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
2079                 }
2080                 if (ncur) {
2081                         pcur = ncur;
2082                         ncur = NULL;
2083                 }
2084         } while (nbno != NULLFSBLOCK);
2085         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2086         *stat = i;
2087         return 0;
2088 error0:
2089         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2090         return error;
2091 }
2092
2093 /*
2094  * Log fields from the btree block header.
2095  */
2096 void
2097 xfs_bmbt_log_block(
2098         xfs_btree_cur_t         *cur,
2099         xfs_buf_t               *bp,
2100         int                     fields)
2101 {
2102         int                     first;
2103         int                     last;
2104         xfs_trans_t             *tp;
2105         static const short      offsets[] = {
2106                 offsetof(xfs_bmbt_block_t, bb_magic),
2107                 offsetof(xfs_bmbt_block_t, bb_level),
2108                 offsetof(xfs_bmbt_block_t, bb_numrecs),
2109                 offsetof(xfs_bmbt_block_t, bb_leftsib),
2110                 offsetof(xfs_bmbt_block_t, bb_rightsib),
2111                 sizeof(xfs_bmbt_block_t)
2112         };
2113
2114         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2115         XFS_BMBT_TRACE_ARGBI(cur, bp, fields);
2116         tp = cur->bc_tp;
2117         if (bp) {
2118                 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first,
2119                                   &last);
2120                 xfs_trans_log_buf(tp, bp, first, last);
2121         } else
2122                 xfs_trans_log_inode(tp, cur->bc_private.b.ip,
2123                         XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
2124         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2125 }
2126
2127 /*
2128  * Log record values from the btree block.
2129  */
2130 void
2131 xfs_bmbt_log_recs(
2132         xfs_btree_cur_t         *cur,
2133         xfs_buf_t               *bp,
2134         int                     rfirst,
2135         int                     rlast)
2136 {
2137         xfs_bmbt_block_t        *block;
2138         int                     first;
2139         int                     last;
2140         xfs_bmbt_rec_t          *rp;
2141         xfs_trans_t             *tp;
2142
2143         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2144         XFS_BMBT_TRACE_ARGBII(cur, bp, rfirst, rlast);
2145         ASSERT(bp);
2146         tp = cur->bc_tp;
2147         block = XFS_BUF_TO_BMBT_BLOCK(bp);
2148         rp = XFS_BMAP_REC_DADDR(block, 1, cur);
2149         first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
2150         last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
2151         xfs_trans_log_buf(tp, bp, first, last);
2152         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2153 }
2154
2155 int                                     /* error */
2156 xfs_bmbt_lookup_eq(
2157         xfs_btree_cur_t *cur,
2158         xfs_fileoff_t   off,
2159         xfs_fsblock_t   bno,
2160         xfs_filblks_t   len,
2161         int             *stat)          /* success/failure */
2162 {
2163         cur->bc_rec.b.br_startoff = off;
2164         cur->bc_rec.b.br_startblock = bno;
2165         cur->bc_rec.b.br_blockcount = len;
2166         return xfs_bmbt_lookup(cur, XFS_LOOKUP_EQ, stat);
2167 }
2168
2169 int                                     /* error */
2170 xfs_bmbt_lookup_ge(
2171         xfs_btree_cur_t *cur,
2172         xfs_fileoff_t   off,
2173         xfs_fsblock_t   bno,
2174         xfs_filblks_t   len,
2175         int             *stat)          /* success/failure */
2176 {
2177         cur->bc_rec.b.br_startoff = off;
2178         cur->bc_rec.b.br_startblock = bno;
2179         cur->bc_rec.b.br_blockcount = len;
2180         return xfs_bmbt_lookup(cur, XFS_LOOKUP_GE, stat);
2181 }
2182
2183 /*
2184  * Give the bmap btree a new root block.  Copy the old broot contents
2185  * down into a real block and make the broot point to it.
2186  */
2187 int                                             /* error */
2188 xfs_bmbt_newroot(
2189         xfs_btree_cur_t         *cur,           /* btree cursor */
2190         int                     *logflags,      /* logging flags for inode */
2191         int                     *stat)          /* return status - 0 fail */
2192 {
2193         xfs_alloc_arg_t         args;           /* allocation arguments */
2194         xfs_bmbt_block_t        *block;         /* bmap btree block */
2195         xfs_buf_t               *bp;            /* buffer for block */
2196         xfs_bmbt_block_t        *cblock;        /* child btree block */
2197         xfs_bmbt_key_t          *ckp;           /* child key pointer */
2198         xfs_bmbt_ptr_t          *cpp;           /* child ptr pointer */
2199         int                     error;          /* error return code */
2200 #ifdef DEBUG
2201         int                     i;              /* loop counter */
2202 #endif
2203         xfs_bmbt_key_t          *kp;            /* pointer to bmap btree key */
2204         int                     level;          /* btree level */
2205         xfs_bmbt_ptr_t          *pp;            /* pointer to bmap block addr */
2206
2207         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2208         level = cur->bc_nlevels - 1;
2209         block = xfs_bmbt_get_block(cur, level, &bp);
2210         /*
2211          * Copy the root into a real block.
2212          */
2213         args.mp = cur->bc_mp;
2214         pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
2215         args.tp = cur->bc_tp;
2216         args.fsbno = cur->bc_private.b.firstblock;
2217         args.mod = args.minleft = args.alignment = args.total = args.isfl =
2218                 args.userdata = args.minalignslop = 0;
2219         args.minlen = args.maxlen = args.prod = 1;
2220         args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
2221         args.firstblock = args.fsbno;
2222         if (args.fsbno == NULLFSBLOCK) {
2223 #ifdef DEBUG
2224                 if ((error = xfs_btree_check_lptr_disk(cur, *pp, level))) {
2225                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2226                         return error;
2227                 }
2228 #endif
2229                 args.fsbno = be64_to_cpu(*pp);
2230                 args.type = XFS_ALLOCTYPE_START_BNO;
2231         } else
2232                 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2233         if ((error = xfs_alloc_vextent(&args))) {
2234                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2235                 return error;
2236         }
2237         if (args.fsbno == NULLFSBLOCK) {
2238                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2239                 *stat = 0;
2240                 return 0;
2241         }
2242         ASSERT(args.len == 1);
2243         cur->bc_private.b.firstblock = args.fsbno;
2244         cur->bc_private.b.allocated++;
2245         cur->bc_private.b.ip->i_d.di_nblocks++;
2246         XFS_TRANS_MOD_DQUOT_BYINO(args.mp, args.tp, cur->bc_private.b.ip,
2247                           XFS_TRANS_DQ_BCOUNT, 1L);
2248         bp = xfs_btree_get_bufl(args.mp, cur->bc_tp, args.fsbno, 0);
2249         cblock = XFS_BUF_TO_BMBT_BLOCK(bp);
2250         *cblock = *block;
2251         be16_add(&block->bb_level, 1);
2252         block->bb_numrecs = cpu_to_be16(1);
2253         cur->bc_nlevels++;
2254         cur->bc_ptrs[level + 1] = 1;
2255         kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
2256         ckp = XFS_BMAP_KEY_IADDR(cblock, 1, cur);
2257         memcpy(ckp, kp, be16_to_cpu(cblock->bb_numrecs) * sizeof(*kp));
2258         cpp = XFS_BMAP_PTR_IADDR(cblock, 1, cur);
2259 #ifdef DEBUG
2260         for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2261                 if ((error = xfs_btree_check_lptr_disk(cur, pp[i], level))) {
2262                         XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2263                         return error;
2264                 }
2265         }
2266 #endif
2267         memcpy(cpp, pp, be16_to_cpu(cblock->bb_numrecs) * sizeof(*pp));
2268 #ifdef DEBUG
2269         if ((error = xfs_btree_check_lptr(cur, args.fsbno, level))) {
2270                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2271                 return error;
2272         }
2273 #endif
2274         *pp = cpu_to_be64(args.fsbno);
2275         xfs_iroot_realloc(cur->bc_private.b.ip, 1 - be16_to_cpu(cblock->bb_numrecs),
2276                 cur->bc_private.b.whichfork);
2277         xfs_btree_setbuf(cur, level, bp);
2278         /*
2279          * Do all this logging at the end so that
2280          * the root is at the right level.
2281          */
2282         xfs_bmbt_log_block(cur, bp, XFS_BB_ALL_BITS);
2283         xfs_bmbt_log_keys(cur, bp, 1, be16_to_cpu(cblock->bb_numrecs));
2284         xfs_bmbt_log_ptrs(cur, bp, 1, be16_to_cpu(cblock->bb_numrecs));
2285         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2286         *logflags |=
2287                 XFS_ILOG_CORE | XFS_ILOG_FBROOT(cur->bc_private.b.whichfork);
2288         *stat = 1;
2289         return 0;
2290 }
2291
2292 /*
2293  * Set all the fields in a bmap extent record from the uncompressed form.
2294  */
2295 void
2296 xfs_bmbt_set_all(
2297         xfs_bmbt_rec_t  *r,
2298         xfs_bmbt_irec_t *s)
2299 {
2300         int     extent_flag;
2301
2302         ASSERT((s->br_state == XFS_EXT_NORM) ||
2303                 (s->br_state == XFS_EXT_UNWRITTEN));
2304         extent_flag = (s->br_state == XFS_EXT_NORM) ? 0 : 1;
2305         ASSERT((s->br_startoff & XFS_MASK64HI(9)) == 0);
2306         ASSERT((s->br_blockcount & XFS_MASK64HI(43)) == 0);
2307 #if XFS_BIG_BLKNOS
2308         ASSERT((s->br_startblock & XFS_MASK64HI(12)) == 0);
2309         r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2310                  ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2311                  ((xfs_bmbt_rec_base_t)s->br_startblock >> 43);
2312         r->l1 = ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2313                  ((xfs_bmbt_rec_base_t)s->br_blockcount &
2314                  (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2315 #else   /* !XFS_BIG_BLKNOS */
2316         if (ISNULLSTARTBLOCK(s->br_startblock)) {
2317                 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2318                         ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2319                           (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2320                 r->l1 = XFS_MASK64HI(11) |
2321                           ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2322                           ((xfs_bmbt_rec_base_t)s->br_blockcount &
2323                            (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2324         } else {
2325                 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2326                         ((xfs_bmbt_rec_base_t)s->br_startoff << 9);
2327                 r->l1 = ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2328                           ((xfs_bmbt_rec_base_t)s->br_blockcount &
2329                            (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2330         }
2331 #endif  /* XFS_BIG_BLKNOS */
2332 }
2333
2334 /*
2335  * Set all the fields in a bmap extent record from the arguments.
2336  */
2337 void
2338 xfs_bmbt_set_allf(
2339         xfs_bmbt_rec_t  *r,
2340         xfs_fileoff_t   o,
2341         xfs_fsblock_t   b,
2342         xfs_filblks_t   c,
2343         xfs_exntst_t    v)
2344 {
2345         int     extent_flag;
2346
2347         ASSERT((v == XFS_EXT_NORM) || (v == XFS_EXT_UNWRITTEN));
2348         extent_flag = (v == XFS_EXT_NORM) ? 0 : 1;
2349         ASSERT((o & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
2350         ASSERT((c & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
2351 #if XFS_BIG_BLKNOS
2352         ASSERT((b & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
2353         r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2354                 ((xfs_bmbt_rec_base_t)o << 9) |
2355                 ((xfs_bmbt_rec_base_t)b >> 43);
2356         r->l1 = ((xfs_bmbt_rec_base_t)b << 21) |
2357                 ((xfs_bmbt_rec_base_t)c &
2358                 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2359 #else   /* !XFS_BIG_BLKNOS */
2360         if (ISNULLSTARTBLOCK(b)) {
2361                 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2362                         ((xfs_bmbt_rec_base_t)o << 9) |
2363                          (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2364                 r->l1 = XFS_MASK64HI(11) |
2365                           ((xfs_bmbt_rec_base_t)b << 21) |
2366                           ((xfs_bmbt_rec_base_t)c &
2367                            (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2368         } else {
2369                 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2370                         ((xfs_bmbt_rec_base_t)o << 9);
2371                 r->l1 = ((xfs_bmbt_rec_base_t)b << 21) |
2372                          ((xfs_bmbt_rec_base_t)c &
2373                          (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2374         }
2375 #endif  /* XFS_BIG_BLKNOS */
2376 }
2377
2378 #ifndef XFS_NATIVE_HOST
2379 /*
2380  * Set all the fields in a bmap extent record from the uncompressed form.
2381  */
2382 void
2383 xfs_bmbt_disk_set_all(
2384         xfs_bmbt_rec_t  *r,
2385         xfs_bmbt_irec_t *s)
2386 {
2387         int     extent_flag;
2388
2389         ASSERT((s->br_state == XFS_EXT_NORM) ||
2390                 (s->br_state == XFS_EXT_UNWRITTEN));
2391         extent_flag = (s->br_state == XFS_EXT_NORM) ? 0 : 1;
2392         ASSERT((s->br_startoff & XFS_MASK64HI(9)) == 0);
2393         ASSERT((s->br_blockcount & XFS_MASK64HI(43)) == 0);
2394 #if XFS_BIG_BLKNOS
2395         ASSERT((s->br_startblock & XFS_MASK64HI(12)) == 0);
2396         INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2397                   ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2398                   ((xfs_bmbt_rec_base_t)s->br_startblock >> 43));
2399         INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2400                   ((xfs_bmbt_rec_base_t)s->br_blockcount &
2401                    (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2402 #else   /* !XFS_BIG_BLKNOS */
2403         if (ISNULLSTARTBLOCK(s->br_startblock)) {
2404                 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2405                         ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2406                           (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
2407                 INT_SET(r->l1, ARCH_CONVERT, XFS_MASK64HI(11) |
2408                           ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2409                           ((xfs_bmbt_rec_base_t)s->br_blockcount &
2410                            (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2411         } else {
2412                 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2413                         ((xfs_bmbt_rec_base_t)s->br_startoff << 9));
2414                 INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2415                           ((xfs_bmbt_rec_base_t)s->br_blockcount &
2416                            (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2417         }
2418 #endif  /* XFS_BIG_BLKNOS */
2419 }
2420
2421 /*
2422  * Set all the fields in a disk format bmap extent record from the arguments.
2423  */
2424 void
2425 xfs_bmbt_disk_set_allf(
2426         xfs_bmbt_rec_t  *r,
2427         xfs_fileoff_t   o,
2428         xfs_fsblock_t   b,
2429         xfs_filblks_t   c,
2430         xfs_exntst_t    v)
2431 {
2432         int     extent_flag;
2433
2434         ASSERT((v == XFS_EXT_NORM) || (v == XFS_EXT_UNWRITTEN));
2435         extent_flag = (v == XFS_EXT_NORM) ? 0 : 1;
2436         ASSERT((o & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
2437         ASSERT((c & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
2438 #if XFS_BIG_BLKNOS
2439         ASSERT((b & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
2440         INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2441                 ((xfs_bmbt_rec_base_t)o << 9) |
2442                 ((xfs_bmbt_rec_base_t)b >> 43));
2443         INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)b << 21) |
2444                   ((xfs_bmbt_rec_base_t)c &
2445                    (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2446 #else   /* !XFS_BIG_BLKNOS */
2447         if (ISNULLSTARTBLOCK(b)) {
2448                 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2449                         ((xfs_bmbt_rec_base_t)o << 9) |
2450                          (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
2451                 INT_SET(r->l1, ARCH_CONVERT, XFS_MASK64HI(11) |
2452                           ((xfs_bmbt_rec_base_t)b << 21) |
2453                           ((xfs_bmbt_rec_base_t)c &
2454                            (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2455         } else {
2456                 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2457                         ((xfs_bmbt_rec_base_t)o << 9));
2458                 INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)b << 21) |
2459                           ((xfs_bmbt_rec_base_t)c &
2460                            (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2461         }
2462 #endif  /* XFS_BIG_BLKNOS */
2463 }
2464 #endif /* XFS_NATIVE_HOST */
2465
2466 /*
2467  * Set the blockcount field in a bmap extent record.
2468  */
2469 void
2470 xfs_bmbt_set_blockcount(
2471         xfs_bmbt_rec_t  *r,
2472         xfs_filblks_t   v)
2473 {
2474         ASSERT((v & XFS_MASK64HI(43)) == 0);
2475         r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(43)) |
2476                   (xfs_bmbt_rec_base_t)(v & XFS_MASK64LO(21));
2477 }
2478
2479 /*
2480  * Set the startblock field in a bmap extent record.
2481  */
2482 void
2483 xfs_bmbt_set_startblock(
2484         xfs_bmbt_rec_t  *r,
2485         xfs_fsblock_t   v)
2486 {
2487 #if XFS_BIG_BLKNOS
2488         ASSERT((v & XFS_MASK64HI(12)) == 0);
2489         r->l0 = (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(55)) |
2490                   (xfs_bmbt_rec_base_t)(v >> 43);
2491         r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)) |
2492                   (xfs_bmbt_rec_base_t)(v << 21);
2493 #else   /* !XFS_BIG_BLKNOS */
2494         if (ISNULLSTARTBLOCK(v)) {
2495                 r->l0 |= (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2496                 r->l1 = (xfs_bmbt_rec_base_t)XFS_MASK64HI(11) |
2497                           ((xfs_bmbt_rec_base_t)v << 21) |
2498                           (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2499         } else {
2500                 r->l0 &= ~(xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2501                 r->l1 = ((xfs_bmbt_rec_base_t)v << 21) |
2502                           (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2503         }
2504 #endif  /* XFS_BIG_BLKNOS */
2505 }
2506
2507 /*
2508  * Set the startoff field in a bmap extent record.
2509  */
2510 void
2511 xfs_bmbt_set_startoff(
2512         xfs_bmbt_rec_t  *r,
2513         xfs_fileoff_t   v)
2514 {
2515         ASSERT((v & XFS_MASK64HI(9)) == 0);
2516         r->l0 = (r->l0 & (xfs_bmbt_rec_base_t) XFS_MASK64HI(1)) |
2517                 ((xfs_bmbt_rec_base_t)v << 9) |
2518                   (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
2519 }
2520
2521 /*
2522  * Set the extent state field in a bmap extent record.
2523  */
2524 void
2525 xfs_bmbt_set_state(
2526         xfs_bmbt_rec_t  *r,
2527         xfs_exntst_t    v)
2528 {
2529         ASSERT(v == XFS_EXT_NORM || v == XFS_EXT_UNWRITTEN);
2530         if (v == XFS_EXT_NORM)
2531                 r->l0 &= XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN);
2532         else
2533                 r->l0 |= XFS_MASK64HI(BMBT_EXNTFLAG_BITLEN);
2534 }
2535
2536 /*
2537  * Convert in-memory form of btree root to on-disk form.
2538  */
2539 void
2540 xfs_bmbt_to_bmdr(
2541         xfs_bmbt_block_t        *rblock,
2542         int                     rblocklen,
2543         xfs_bmdr_block_t        *dblock,
2544         int                     dblocklen)
2545 {
2546         int                     dmxr;
2547         xfs_bmbt_key_t          *fkp;
2548         __be64                  *fpp;
2549         xfs_bmbt_key_t          *tkp;
2550         __be64                  *tpp;
2551
2552         ASSERT(be32_to_cpu(rblock->bb_magic) == XFS_BMAP_MAGIC);
2553         ASSERT(be64_to_cpu(rblock->bb_leftsib) == NULLDFSBNO);
2554         ASSERT(be64_to_cpu(rblock->bb_rightsib) == NULLDFSBNO);
2555         ASSERT(be16_to_cpu(rblock->bb_level) > 0);
2556         dblock->bb_level = rblock->bb_level;
2557         dblock->bb_numrecs = rblock->bb_numrecs;
2558         dmxr = (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen, xfs_bmdr, 0);
2559         fkp = XFS_BMAP_BROOT_KEY_ADDR(rblock, 1, rblocklen);
2560         tkp = XFS_BTREE_KEY_ADDR(xfs_bmdr, dblock, 1);
2561         fpp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, rblocklen);
2562         tpp = XFS_BTREE_PTR_ADDR(xfs_bmdr, dblock, 1, dmxr);
2563         dmxr = be16_to_cpu(dblock->bb_numrecs);
2564         memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
2565         memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
2566 }
2567
2568 /*
2569  * Update the record to the passed values.
2570  */
2571 int
2572 xfs_bmbt_update(
2573         xfs_btree_cur_t         *cur,
2574         xfs_fileoff_t           off,
2575         xfs_fsblock_t           bno,
2576         xfs_filblks_t           len,
2577         xfs_exntst_t            state)
2578 {
2579         xfs_bmbt_block_t        *block;
2580         xfs_buf_t               *bp;
2581         int                     error;
2582         xfs_bmbt_key_t          key;
2583         int                     ptr;
2584         xfs_bmbt_rec_t          *rp;
2585
2586         XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2587         XFS_BMBT_TRACE_ARGFFFI(cur, (xfs_dfiloff_t)off, (xfs_dfsbno_t)bno,
2588                 (xfs_dfilblks_t)len, (int)state);
2589         block = xfs_bmbt_get_block(cur, 0, &bp);
2590 #ifdef DEBUG
2591         if ((error = xfs_btree_check_lblock(cur, block, 0, bp))) {
2592                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2593                 return error;
2594         }
2595 #endif
2596         ptr = cur->bc_ptrs[0];
2597         rp = XFS_BMAP_REC_IADDR(block, ptr, cur);
2598         xfs_bmbt_disk_set_allf(rp, off, bno, len, state);
2599         xfs_bmbt_log_recs(cur, bp, ptr, ptr);
2600         if (ptr > 1) {
2601                 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2602                 return 0;
2603         }
2604         key.br_startoff = cpu_to_be64(off);
2605         if ((error = xfs_bmbt_updkey(cur, &key, 1))) {
2606                 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2607                 return error;
2608         }
2609         XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2610         return 0;
2611 }
2612
2613 /*
2614  * Check extent records, which have just been read, for
2615  * any bit in the extent flag field. ASSERT on debug
2616  * kernels, as this condition should not occur.
2617  * Return an error condition (1) if any flags found,
2618  * otherwise return 0.
2619  */
2620
2621 int
2622 xfs_check_nostate_extents(
2623         xfs_ifork_t             *ifp,
2624         xfs_extnum_t            idx,
2625         xfs_extnum_t            num)
2626 {
2627         xfs_bmbt_rec_t          *ep;
2628
2629         for (; num > 0; num--, idx++) {
2630                 ep = xfs_iext_get_ext(ifp, idx);
2631                 if ((ep->l0 >>
2632                      (64 - BMBT_EXNTFLAG_BITLEN)) != 0) {
2633                         ASSERT(0);
2634                         return 1;
2635                 }
2636         }
2637         return 0;
2638 }