blob: 6b7774ebc26aab622dbc2557838408cedc90cdee [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_btree.h"
Christoph Hellwig8c4ed632008-10-30 16:55:13 +110040#include "xfs_btree_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_ialloc.h"
42#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_bmap.h"
44#include "xfs_error.h"
45#include "xfs_quota.h"
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * Prototypes for internal btree functions.
49 */
50
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052STATIC void xfs_bmbt_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
53STATIC void xfs_bmbt_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#undef EXIT
Christoph Hellwig8c4ed632008-10-30 16:55:13 +110056
57#define ENTRY XBT_ENTRY
58#define ERROR XBT_ERROR
59#define EXIT XBT_EXIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/*
Christoph Hellwig8c4ed632008-10-30 16:55:13 +110062 * Keep the XFS_BMBT_TRACE_ names around for now until all code using them
63 * is converted to be generic and thus switches to the XFS_BTREE_TRACE_ names.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
Christoph Hellwig8c4ed632008-10-30 16:55:13 +110065#define XFS_BMBT_TRACE_ARGBI(c,b,i) \
66 XFS_BTREE_TRACE_ARGBI(c,b,i)
67#define XFS_BMBT_TRACE_ARGBII(c,b,i,j) \
68 XFS_BTREE_TRACE_ARGBII(c,b,i,j)
69#define XFS_BMBT_TRACE_ARGFFFI(c,o,b,i,j) \
70 XFS_BTREE_TRACE_ARGFFFI(c,o,b,i,j)
71#define XFS_BMBT_TRACE_ARGI(c,i) \
72 XFS_BTREE_TRACE_ARGI(c,i)
73#define XFS_BMBT_TRACE_ARGIFK(c,i,f,s) \
74 XFS_BTREE_TRACE_ARGIPK(c,i,(union xfs_btree_ptr)f,s)
75#define XFS_BMBT_TRACE_ARGIFR(c,i,f,r) \
76 XFS_BTREE_TRACE_ARGIPR(c,i, \
77 (union xfs_btree_ptr)f, (union xfs_btree_rec *)r)
78#define XFS_BMBT_TRACE_ARGIK(c,i,k) \
79 XFS_BTREE_TRACE_ARGIK(c,i,(union xfs_btree_key *)k)
80#define XFS_BMBT_TRACE_CURSOR(c,s) \
81 XFS_BTREE_TRACE_CURSOR(c,s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83
84/*
85 * Internal functions.
86 */
87
88/*
89 * Delete record pointed to by cur/level.
90 */
91STATIC int /* error */
92xfs_bmbt_delrec(
93 xfs_btree_cur_t *cur,
94 int level,
95 int *stat) /* success/failure */
96{
97 xfs_bmbt_block_t *block; /* bmap btree block */
98 xfs_fsblock_t bno; /* fs-relative block number */
99 xfs_buf_t *bp; /* buffer for block */
100 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 int i; /* loop counter */
102 int j; /* temp state */
103 xfs_bmbt_key_t key; /* bmap btree key */
104 xfs_bmbt_key_t *kp=NULL; /* pointer to bmap btree key */
105 xfs_fsblock_t lbno; /* left sibling block number */
106 xfs_buf_t *lbp; /* left buffer pointer */
107 xfs_bmbt_block_t *left; /* left btree block */
108 xfs_bmbt_key_t *lkp; /* left btree key */
109 xfs_bmbt_ptr_t *lpp; /* left address pointer */
110 int lrecs=0; /* left record count */
111 xfs_bmbt_rec_t *lrp; /* left record pointer */
112 xfs_mount_t *mp; /* file system mount point */
113 xfs_bmbt_ptr_t *pp; /* pointer to bmap block addr */
114 int ptr; /* key/record index */
115 xfs_fsblock_t rbno; /* right sibling block number */
116 xfs_buf_t *rbp; /* right buffer pointer */
117 xfs_bmbt_block_t *right; /* right btree block */
118 xfs_bmbt_key_t *rkp; /* right btree key */
119 xfs_bmbt_rec_t *rp; /* pointer to bmap btree rec */
120 xfs_bmbt_ptr_t *rpp; /* right address pointer */
121 xfs_bmbt_block_t *rrblock; /* right-right btree block */
122 xfs_buf_t *rrbp; /* right-right buffer pointer */
123 int rrecs=0; /* right record count */
124 xfs_bmbt_rec_t *rrp; /* right record pointer */
125 xfs_btree_cur_t *tcur; /* temporary btree cursor */
126 int numrecs; /* temporary numrec count */
127 int numlrecs, numrrecs;
128
129 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
130 XFS_BMBT_TRACE_ARGI(cur, level);
131 ptr = cur->bc_ptrs[level];
Nathan Scott1121b212006-09-28 10:58:40 +1000132 tcur = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (ptr == 0) {
134 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
135 *stat = 0;
136 return 0;
137 }
138 block = xfs_bmbt_get_block(cur, level, &bp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100139 numrecs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#ifdef DEBUG
141 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
142 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
143 goto error0;
144 }
145#endif
146 if (ptr > numrecs) {
147 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
148 *stat = 0;
149 return 0;
150 }
151 XFS_STATS_INC(xs_bmbt_delrec);
152 if (level > 0) {
153 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
154 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
155#ifdef DEBUG
156 for (i = ptr; i < numrecs; i++) {
Christoph Hellwigb113bcb2006-09-28 10:57:42 +1000157 if ((error = xfs_btree_check_lptr_disk(cur, pp[i], level))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
159 goto error0;
160 }
161 }
162#endif
163 if (ptr < numrecs) {
164 memmove(&kp[ptr - 1], &kp[ptr],
165 (numrecs - ptr) * sizeof(*kp));
Christoph Hellwigd580ef62007-08-16 16:23:11 +1000166 memmove(&pp[ptr - 1], &pp[ptr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 (numrecs - ptr) * sizeof(*pp));
168 xfs_bmbt_log_ptrs(cur, bp, ptr, numrecs - 1);
169 xfs_bmbt_log_keys(cur, bp, ptr, numrecs - 1);
170 }
171 } else {
172 rp = XFS_BMAP_REC_IADDR(block, 1, cur);
173 if (ptr < numrecs) {
174 memmove(&rp[ptr - 1], &rp[ptr],
175 (numrecs - ptr) * sizeof(*rp));
176 xfs_bmbt_log_recs(cur, bp, ptr, numrecs - 1);
177 }
178 if (ptr == 1) {
Christoph Hellwig8801bb92006-09-28 10:58:17 +1000179 key.br_startoff =
180 cpu_to_be64(xfs_bmbt_disk_get_startoff(rp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 kp = &key;
182 }
183 }
184 numrecs--;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100185 block->bb_numrecs = cpu_to_be16(numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 xfs_bmbt_log_block(cur, bp, XFS_BB_NUMRECS);
187 /*
188 * We're at the root level.
189 * First, shrink the root block in-memory.
190 * Try to get rid of the next level down.
191 * If we can't then there's nothing left to do.
192 */
193 if (level == cur->bc_nlevels - 1) {
194 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
195 cur->bc_private.b.whichfork);
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +1100196 if ((error = xfs_btree_kill_iroot(cur))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
198 goto error0;
199 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100200 if (level > 0 && (error = xfs_btree_decrement(cur, level, &j))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
202 goto error0;
203 }
204 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
205 *stat = 1;
206 return 0;
207 }
Christoph Hellwig38bb7422008-10-30 16:56:22 +1100208 if (ptr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)kp, level + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
210 goto error0;
211 }
212 if (numrecs >= XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100213 if (level > 0 && (error = xfs_btree_decrement(cur, level, &j))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
215 goto error0;
216 }
217 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
218 *stat = 1;
219 return 0;
220 }
Christoph Hellwig16259e72005-11-02 15:11:25 +1100221 rbno = be64_to_cpu(block->bb_rightsib);
222 lbno = be64_to_cpu(block->bb_leftsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /*
224 * One child of root, need to get a chance to copy its contents
225 * into the root and delete it. Can't go up to next level,
226 * there's nothing to delete there.
227 */
228 if (lbno == NULLFSBLOCK && rbno == NULLFSBLOCK &&
229 level == cur->bc_nlevels - 2) {
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +1100230 if ((error = xfs_btree_kill_iroot(cur))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
232 goto error0;
233 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100234 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
236 goto error0;
237 }
238 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
239 *stat = 1;
240 return 0;
241 }
242 ASSERT(rbno != NULLFSBLOCK || lbno != NULLFSBLOCK);
243 if ((error = xfs_btree_dup_cursor(cur, &tcur))) {
244 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
245 goto error0;
246 }
247 bno = NULLFSBLOCK;
248 if (rbno != NULLFSBLOCK) {
249 i = xfs_btree_lastrec(tcur, level);
250 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig637aa502008-10-30 16:55:45 +1100251 if ((error = xfs_btree_increment(tcur, level, &i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
253 goto error0;
254 }
255 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
256 i = xfs_btree_lastrec(tcur, level);
257 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
258 rbp = tcur->bc_bufs[level];
259 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
260#ifdef DEBUG
261 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
262 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
263 goto error0;
264 }
265#endif
Christoph Hellwig16259e72005-11-02 15:11:25 +1100266 bno = be64_to_cpu(right->bb_leftsib);
267 if (be16_to_cpu(right->bb_numrecs) - 1 >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
Christoph Hellwig687b8902008-10-30 16:56:53 +1100269 if ((error = xfs_btree_lshift(tcur, level, &i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
271 goto error0;
272 }
273 if (i) {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100274 ASSERT(be16_to_cpu(block->bb_numrecs) >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 XFS_BMAP_BLOCK_IMINRECS(level, tcur));
276 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
277 tcur = NULL;
278 if (level > 0) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100279 if ((error = xfs_btree_decrement(cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 level, &i))) {
281 XFS_BMBT_TRACE_CURSOR(cur,
282 ERROR);
283 goto error0;
284 }
285 }
286 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
287 *stat = 1;
288 return 0;
289 }
290 }
Christoph Hellwig16259e72005-11-02 15:11:25 +1100291 rrecs = be16_to_cpu(right->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (lbno != NULLFSBLOCK) {
293 i = xfs_btree_firstrec(tcur, level);
294 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100295 if ((error = xfs_btree_decrement(tcur, level, &i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
297 goto error0;
298 }
299 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
300 }
301 }
302 if (lbno != NULLFSBLOCK) {
303 i = xfs_btree_firstrec(tcur, level);
304 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
305 /*
306 * decrement to last in block
307 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100308 if ((error = xfs_btree_decrement(tcur, level, &i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
310 goto error0;
311 }
312 i = xfs_btree_firstrec(tcur, level);
313 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
314 lbp = tcur->bc_bufs[level];
315 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
316#ifdef DEBUG
317 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
318 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
319 goto error0;
320 }
321#endif
Christoph Hellwig16259e72005-11-02 15:11:25 +1100322 bno = be64_to_cpu(left->bb_rightsib);
323 if (be16_to_cpu(left->bb_numrecs) - 1 >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
Christoph Hellwig9eaead52008-10-30 16:56:43 +1100325 if ((error = xfs_btree_rshift(tcur, level, &i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
327 goto error0;
328 }
329 if (i) {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100330 ASSERT(be16_to_cpu(block->bb_numrecs) >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 XFS_BMAP_BLOCK_IMINRECS(level, tcur));
332 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
333 tcur = NULL;
334 if (level == 0)
335 cur->bc_ptrs[0]++;
336 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
337 *stat = 1;
338 return 0;
339 }
340 }
Christoph Hellwig16259e72005-11-02 15:11:25 +1100341 lrecs = be16_to_cpu(left->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
344 tcur = NULL;
345 mp = cur->bc_mp;
346 ASSERT(bno != NULLFSBLOCK);
347 if (lbno != NULLFSBLOCK &&
Christoph Hellwig16259e72005-11-02 15:11:25 +1100348 lrecs + be16_to_cpu(block->bb_numrecs) <= XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 rbno = bno;
350 right = block;
351 rbp = bp;
352 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, lbno, 0, &lbp,
353 XFS_BMAP_BTREE_REF))) {
354 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
355 goto error0;
356 }
357 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
358 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
359 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
360 goto error0;
361 }
362 } else if (rbno != NULLFSBLOCK &&
Christoph Hellwig16259e72005-11-02 15:11:25 +1100363 rrecs + be16_to_cpu(block->bb_numrecs) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
365 lbno = bno;
366 left = block;
367 lbp = bp;
368 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, rbno, 0, &rbp,
369 XFS_BMAP_BTREE_REF))) {
370 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
371 goto error0;
372 }
373 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
374 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
375 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
376 goto error0;
377 }
Christoph Hellwig16259e72005-11-02 15:11:25 +1100378 lrecs = be16_to_cpu(left->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 } else {
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100380 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
382 goto error0;
383 }
384 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
385 *stat = 1;
386 return 0;
387 }
Christoph Hellwig16259e72005-11-02 15:11:25 +1100388 numlrecs = be16_to_cpu(left->bb_numrecs);
389 numrrecs = be16_to_cpu(right->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (level > 0) {
391 lkp = XFS_BMAP_KEY_IADDR(left, numlrecs + 1, cur);
392 lpp = XFS_BMAP_PTR_IADDR(left, numlrecs + 1, cur);
393 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
394 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
395#ifdef DEBUG
396 for (i = 0; i < numrrecs; i++) {
Christoph Hellwigb113bcb2006-09-28 10:57:42 +1000397 if ((error = xfs_btree_check_lptr_disk(cur, rpp[i], level))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
399 goto error0;
400 }
401 }
402#endif
403 memcpy(lkp, rkp, numrrecs * sizeof(*lkp));
404 memcpy(lpp, rpp, numrrecs * sizeof(*lpp));
405 xfs_bmbt_log_keys(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
406 xfs_bmbt_log_ptrs(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
407 } else {
408 lrp = XFS_BMAP_REC_IADDR(left, numlrecs + 1, cur);
409 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
410 memcpy(lrp, rrp, numrrecs * sizeof(*lrp));
411 xfs_bmbt_log_recs(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
412 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800413 be16_add_cpu(&left->bb_numrecs, numrrecs);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100414 left->bb_rightsib = right->bb_rightsib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 xfs_bmbt_log_block(cur, lbp, XFS_BB_RIGHTSIB | XFS_BB_NUMRECS);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100416 if (be64_to_cpu(left->bb_rightsib) != NULLDFSBNO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +1100418 be64_to_cpu(left->bb_rightsib),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 0, &rrbp, XFS_BMAP_BTREE_REF))) {
420 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
421 goto error0;
422 }
423 rrblock = XFS_BUF_TO_BMBT_BLOCK(rrbp);
424 if ((error = xfs_btree_check_lblock(cur, rrblock, level, rrbp))) {
425 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
426 goto error0;
427 }
Christoph Hellwig16259e72005-11-02 15:11:25 +1100428 rrblock->bb_leftsib = cpu_to_be64(lbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 xfs_bmbt_log_block(cur, rrbp, XFS_BB_LEFTSIB);
430 }
431 xfs_bmap_add_free(XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(rbp)), 1,
432 cur->bc_private.b.flist, mp);
433 cur->bc_private.b.ip->i_d.di_nblocks--;
434 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
435 XFS_TRANS_MOD_DQUOT_BYINO(mp, cur->bc_tp, cur->bc_private.b.ip,
436 XFS_TRANS_DQ_BCOUNT, -1L);
437 xfs_trans_binval(cur->bc_tp, rbp);
438 if (bp != lbp) {
439 cur->bc_bufs[level] = lbp;
440 cur->bc_ptrs[level] += lrecs;
441 cur->bc_ra[level] = 0;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100442 } else if ((error = xfs_btree_increment(cur, level + 1, &i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
444 goto error0;
445 }
446 if (level > 0)
447 cur->bc_ptrs[level]--;
448 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
449 *stat = 2;
450 return 0;
451
452error0:
453 if (tcur)
454 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
455 return error;
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*
459 * Log key values from the btree block.
460 */
461STATIC void
462xfs_bmbt_log_keys(
463 xfs_btree_cur_t *cur,
464 xfs_buf_t *bp,
465 int kfirst,
466 int klast)
467{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 xfs_trans_t *tp;
469
470 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
471 XFS_BMBT_TRACE_ARGBII(cur, bp, kfirst, klast);
472 tp = cur->bc_tp;
473 if (bp) {
474 xfs_bmbt_block_t *block;
475 int first;
476 xfs_bmbt_key_t *kp;
477 int last;
478
479 block = XFS_BUF_TO_BMBT_BLOCK(bp);
480 kp = XFS_BMAP_KEY_DADDR(block, 1, cur);
481 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
482 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
483 xfs_trans_log_buf(tp, bp, first, last);
484 } else {
485 xfs_inode_t *ip;
486
487 ip = cur->bc_private.b.ip;
488 xfs_trans_log_inode(tp, ip,
489 XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
490 }
491 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
492}
493
494/*
495 * Log pointer values from the btree block.
496 */
497STATIC void
498xfs_bmbt_log_ptrs(
499 xfs_btree_cur_t *cur,
500 xfs_buf_t *bp,
501 int pfirst,
502 int plast)
503{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 xfs_trans_t *tp;
505
506 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
507 XFS_BMBT_TRACE_ARGBII(cur, bp, pfirst, plast);
508 tp = cur->bc_tp;
509 if (bp) {
510 xfs_bmbt_block_t *block;
511 int first;
512 int last;
513 xfs_bmbt_ptr_t *pp;
514
515 block = XFS_BUF_TO_BMBT_BLOCK(bp);
516 pp = XFS_BMAP_PTR_DADDR(block, 1, cur);
517 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
518 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
519 xfs_trans_log_buf(tp, bp, first, last);
520 } else {
521 xfs_inode_t *ip;
522
523 ip = cur->bc_private.b.ip;
524 xfs_trans_log_inode(tp, ip,
525 XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
526 }
527 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
528}
529
530/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * Determine the extent state.
532 */
533/* ARGSUSED */
534STATIC xfs_exntst_t
535xfs_extent_state(
536 xfs_filblks_t blks,
537 int extent_flag)
538{
539 if (extent_flag) {
540 ASSERT(blks != 0); /* saved for DMIG */
541 return XFS_EXT_UNWRITTEN;
542 }
543 return XFS_EXT_NORM;
544}
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546/*
547 * Convert on-disk form of btree root to in-memory form.
548 */
549void
550xfs_bmdr_to_bmbt(
551 xfs_bmdr_block_t *dblock,
552 int dblocklen,
553 xfs_bmbt_block_t *rblock,
554 int rblocklen)
555{
556 int dmxr;
557 xfs_bmbt_key_t *fkp;
Christoph Hellwig576039c2006-09-28 10:58:06 +1000558 __be64 *fpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 xfs_bmbt_key_t *tkp;
Christoph Hellwig576039c2006-09-28 10:58:06 +1000560 __be64 *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Christoph Hellwig16259e72005-11-02 15:11:25 +1100562 rblock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
563 rblock->bb_level = dblock->bb_level;
564 ASSERT(be16_to_cpu(rblock->bb_level) > 0);
565 rblock->bb_numrecs = dblock->bb_numrecs;
566 rblock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
567 rblock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 dmxr = (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen, xfs_bmdr, 0);
Eric Sandeen2c36dde2007-02-10 18:37:33 +1100569 fkp = XFS_BTREE_KEY_ADDR(xfs_bmdr, dblock, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 tkp = XFS_BMAP_BROOT_KEY_ADDR(rblock, 1, rblocklen);
Eric Sandeen2c36dde2007-02-10 18:37:33 +1100571 fpp = XFS_BTREE_PTR_ADDR(xfs_bmdr, dblock, 1, dmxr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 tpp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, rblocklen);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100573 dmxr = be16_to_cpu(dblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
Christoph Hellwig576039c2006-09-28 10:58:06 +1000575 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * Delete the record pointed to by cur.
580 */
581int /* error */
582xfs_bmbt_delete(
583 xfs_btree_cur_t *cur,
584 int *stat) /* success/failure */
585{
586 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int i;
588 int level;
589
590 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
591 for (level = 0, i = 2; i == 2; level++) {
592 if ((error = xfs_bmbt_delrec(cur, level, &i))) {
593 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
594 return error;
595 }
596 }
597 if (i == 0) {
598 for (level = 1; level < cur->bc_nlevels; level++) {
599 if (cur->bc_ptrs[level] == 0) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100600 if ((error = xfs_btree_decrement(cur, level,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 &i))) {
602 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
603 return error;
604 }
605 break;
606 }
607 }
608 }
609 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
610 *stat = i;
611 return 0;
612}
613
614/*
615 * Convert a compressed bmap extent record to an uncompressed form.
616 * This code must be in sync with the routines xfs_bmbt_get_startoff,
617 * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
618 */
619
David Chinner7989cb82007-02-10 18:34:56 +1100620STATIC_INLINE void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621__xfs_bmbt_get_all(
622 __uint64_t l0,
623 __uint64_t l1,
624 xfs_bmbt_irec_t *s)
625{
626 int ext_flag;
627 xfs_exntst_t st;
628
629 ext_flag = (int)(l0 >> (64 - BMBT_EXNTFLAG_BITLEN));
630 s->br_startoff = ((xfs_fileoff_t)l0 &
631 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
632#if XFS_BIG_BLKNOS
633 s->br_startblock = (((xfs_fsblock_t)l0 & XFS_MASK64LO(9)) << 43) |
634 (((xfs_fsblock_t)l1) >> 21);
635#else
636#ifdef DEBUG
637 {
638 xfs_dfsbno_t b;
639
640 b = (((xfs_dfsbno_t)l0 & XFS_MASK64LO(9)) << 43) |
641 (((xfs_dfsbno_t)l1) >> 21);
642 ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
643 s->br_startblock = (xfs_fsblock_t)b;
644 }
645#else /* !DEBUG */
646 s->br_startblock = (xfs_fsblock_t)(((xfs_dfsbno_t)l1) >> 21);
647#endif /* DEBUG */
648#endif /* XFS_BIG_BLKNOS */
649 s->br_blockcount = (xfs_filblks_t)(l1 & XFS_MASK64LO(21));
650 /* This is xfs_extent_state() in-line */
651 if (ext_flag) {
652 ASSERT(s->br_blockcount != 0); /* saved for DMIG */
653 st = XFS_EXT_UNWRITTEN;
654 } else
655 st = XFS_EXT_NORM;
656 s->br_state = st;
657}
658
659void
660xfs_bmbt_get_all(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000661 xfs_bmbt_rec_host_t *r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 xfs_bmbt_irec_t *s)
663{
664 __xfs_bmbt_get_all(r->l0, r->l1, s);
665}
666
667/*
668 * Get the block pointer for the given level of the cursor.
669 * Fill in the buffer pointer, if applicable.
670 */
671xfs_bmbt_block_t *
672xfs_bmbt_get_block(
673 xfs_btree_cur_t *cur,
674 int level,
675 xfs_buf_t **bpp)
676{
677 xfs_ifork_t *ifp;
678 xfs_bmbt_block_t *rval;
679
680 if (level < cur->bc_nlevels - 1) {
681 *bpp = cur->bc_bufs[level];
682 rval = XFS_BUF_TO_BMBT_BLOCK(*bpp);
683 } else {
684 *bpp = NULL;
685 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
686 cur->bc_private.b.whichfork);
687 rval = ifp->if_broot;
688 }
689 return rval;
690}
691
692/*
693 * Extract the blockcount field from an in memory bmap extent record.
694 */
695xfs_filblks_t
696xfs_bmbt_get_blockcount(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000697 xfs_bmbt_rec_host_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 return (xfs_filblks_t)(r->l1 & XFS_MASK64LO(21));
700}
701
702/*
703 * Extract the startblock field from an in memory bmap extent record.
704 */
705xfs_fsblock_t
706xfs_bmbt_get_startblock(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000707 xfs_bmbt_rec_host_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709#if XFS_BIG_BLKNOS
710 return (((xfs_fsblock_t)r->l0 & XFS_MASK64LO(9)) << 43) |
711 (((xfs_fsblock_t)r->l1) >> 21);
712#else
713#ifdef DEBUG
714 xfs_dfsbno_t b;
715
716 b = (((xfs_dfsbno_t)r->l0 & XFS_MASK64LO(9)) << 43) |
717 (((xfs_dfsbno_t)r->l1) >> 21);
718 ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
719 return (xfs_fsblock_t)b;
720#else /* !DEBUG */
721 return (xfs_fsblock_t)(((xfs_dfsbno_t)r->l1) >> 21);
722#endif /* DEBUG */
723#endif /* XFS_BIG_BLKNOS */
724}
725
726/*
727 * Extract the startoff field from an in memory bmap extent record.
728 */
729xfs_fileoff_t
730xfs_bmbt_get_startoff(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000731 xfs_bmbt_rec_host_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 return ((xfs_fileoff_t)r->l0 &
734 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
735}
736
737xfs_exntst_t
738xfs_bmbt_get_state(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000739 xfs_bmbt_rec_host_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 int ext_flag;
742
743 ext_flag = (int)((r->l0) >> (64 - BMBT_EXNTFLAG_BITLEN));
744 return xfs_extent_state(xfs_bmbt_get_blockcount(r),
745 ext_flag);
746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748/* Endian flipping versions of the bmbt extraction functions */
749void
750xfs_bmbt_disk_get_all(
751 xfs_bmbt_rec_t *r,
752 xfs_bmbt_irec_t *s)
753{
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000754 __xfs_bmbt_get_all(be64_to_cpu(r->l0), be64_to_cpu(r->l1), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757/*
758 * Extract the blockcount field from an on disk bmap extent record.
759 */
760xfs_filblks_t
761xfs_bmbt_disk_get_blockcount(
762 xfs_bmbt_rec_t *r)
763{
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000764 return (xfs_filblks_t)(be64_to_cpu(r->l1) & XFS_MASK64LO(21));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
767/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 * Extract the startoff field from a disk format bmap extent record.
769 */
770xfs_fileoff_t
771xfs_bmbt_disk_get_startoff(
772 xfs_bmbt_rec_t *r)
773{
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000774 return ((xfs_fileoff_t)be64_to_cpu(r->l0) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
776}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * Log fields from the btree block header.
780 */
781void
782xfs_bmbt_log_block(
783 xfs_btree_cur_t *cur,
784 xfs_buf_t *bp,
785 int fields)
786{
787 int first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int last;
789 xfs_trans_t *tp;
790 static const short offsets[] = {
791 offsetof(xfs_bmbt_block_t, bb_magic),
792 offsetof(xfs_bmbt_block_t, bb_level),
793 offsetof(xfs_bmbt_block_t, bb_numrecs),
794 offsetof(xfs_bmbt_block_t, bb_leftsib),
795 offsetof(xfs_bmbt_block_t, bb_rightsib),
796 sizeof(xfs_bmbt_block_t)
797 };
798
799 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
800 XFS_BMBT_TRACE_ARGBI(cur, bp, fields);
801 tp = cur->bc_tp;
802 if (bp) {
803 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first,
804 &last);
805 xfs_trans_log_buf(tp, bp, first, last);
806 } else
807 xfs_trans_log_inode(tp, cur->bc_private.b.ip,
808 XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
809 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
810}
811
812/*
813 * Log record values from the btree block.
814 */
815void
816xfs_bmbt_log_recs(
817 xfs_btree_cur_t *cur,
818 xfs_buf_t *bp,
819 int rfirst,
820 int rlast)
821{
822 xfs_bmbt_block_t *block;
823 int first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 int last;
825 xfs_bmbt_rec_t *rp;
826 xfs_trans_t *tp;
827
828 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
829 XFS_BMBT_TRACE_ARGBII(cur, bp, rfirst, rlast);
830 ASSERT(bp);
831 tp = cur->bc_tp;
832 block = XFS_BUF_TO_BMBT_BLOCK(bp);
833 rp = XFS_BMAP_REC_DADDR(block, 1, cur);
834 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
835 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
836 xfs_trans_log_buf(tp, bp, first, last);
837 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840/*
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000841 * Set all the fields in a bmap extent record from the arguments.
842 */
843void
844xfs_bmbt_set_allf(
845 xfs_bmbt_rec_host_t *r,
846 xfs_fileoff_t startoff,
847 xfs_fsblock_t startblock,
848 xfs_filblks_t blockcount,
849 xfs_exntst_t state)
850{
851 int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
852
853 ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN);
854 ASSERT((startoff & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
855 ASSERT((blockcount & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
856
857#if XFS_BIG_BLKNOS
858 ASSERT((startblock & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
859
860 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
861 ((xfs_bmbt_rec_base_t)startoff << 9) |
862 ((xfs_bmbt_rec_base_t)startblock >> 43);
863 r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) |
864 ((xfs_bmbt_rec_base_t)blockcount &
865 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
866#else /* !XFS_BIG_BLKNOS */
Michal Piotrowskiddc6d3b2007-08-23 16:20:10 +1000867 if (ISNULLSTARTBLOCK(startblock)) {
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000868 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
869 ((xfs_bmbt_rec_base_t)startoff << 9) |
870 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
871 r->l1 = XFS_MASK64HI(11) |
872 ((xfs_bmbt_rec_base_t)startblock << 21) |
873 ((xfs_bmbt_rec_base_t)blockcount &
874 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
875 } else {
876 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
877 ((xfs_bmbt_rec_base_t)startoff << 9);
878 r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) |
879 ((xfs_bmbt_rec_base_t)blockcount &
880 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
881 }
882#endif /* XFS_BIG_BLKNOS */
883}
884
885/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * Set all the fields in a bmap extent record from the uncompressed form.
887 */
888void
889xfs_bmbt_set_all(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000890 xfs_bmbt_rec_host_t *r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 xfs_bmbt_irec_t *s)
892{
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000893 xfs_bmbt_set_allf(r, s->br_startoff, s->br_startblock,
894 s->br_blockcount, s->br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898/*
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000899 * Set all the fields in a disk format bmap extent record from the arguments.
900 */
901void
902xfs_bmbt_disk_set_allf(
903 xfs_bmbt_rec_t *r,
904 xfs_fileoff_t startoff,
905 xfs_fsblock_t startblock,
906 xfs_filblks_t blockcount,
907 xfs_exntst_t state)
908{
909 int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
910
911 ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN);
912 ASSERT((startoff & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
913 ASSERT((blockcount & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
914
915#if XFS_BIG_BLKNOS
916 ASSERT((startblock & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
917
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000918 r->l0 = cpu_to_be64(
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000919 ((xfs_bmbt_rec_base_t)extent_flag << 63) |
920 ((xfs_bmbt_rec_base_t)startoff << 9) |
921 ((xfs_bmbt_rec_base_t)startblock >> 43));
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000922 r->l1 = cpu_to_be64(
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000923 ((xfs_bmbt_rec_base_t)startblock << 21) |
924 ((xfs_bmbt_rec_base_t)blockcount &
925 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
926#else /* !XFS_BIG_BLKNOS */
Michal Piotrowskiddc6d3b2007-08-23 16:20:10 +1000927 if (ISNULLSTARTBLOCK(startblock)) {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000928 r->l0 = cpu_to_be64(
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000929 ((xfs_bmbt_rec_base_t)extent_flag << 63) |
930 ((xfs_bmbt_rec_base_t)startoff << 9) |
931 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000932 r->l1 = cpu_to_be64(XFS_MASK64HI(11) |
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000933 ((xfs_bmbt_rec_base_t)startblock << 21) |
934 ((xfs_bmbt_rec_base_t)blockcount &
935 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
936 } else {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000937 r->l0 = cpu_to_be64(
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000938 ((xfs_bmbt_rec_base_t)extent_flag << 63) |
939 ((xfs_bmbt_rec_base_t)startoff << 9));
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000940 r->l1 = cpu_to_be64(
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000941 ((xfs_bmbt_rec_base_t)startblock << 21) |
942 ((xfs_bmbt_rec_base_t)blockcount &
943 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
944 }
945#endif /* XFS_BIG_BLKNOS */
946}
947
948/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * Set all the fields in a bmap extent record from the uncompressed form.
950 */
951void
952xfs_bmbt_disk_set_all(
953 xfs_bmbt_rec_t *r,
954 xfs_bmbt_irec_t *s)
955{
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000956 xfs_bmbt_disk_set_allf(r, s->br_startoff, s->br_startblock,
957 s->br_blockcount, s->br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960/*
961 * Set the blockcount field in a bmap extent record.
962 */
963void
964xfs_bmbt_set_blockcount(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000965 xfs_bmbt_rec_host_t *r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 xfs_filblks_t v)
967{
968 ASSERT((v & XFS_MASK64HI(43)) == 0);
969 r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(43)) |
970 (xfs_bmbt_rec_base_t)(v & XFS_MASK64LO(21));
971}
972
973/*
974 * Set the startblock field in a bmap extent record.
975 */
976void
977xfs_bmbt_set_startblock(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000978 xfs_bmbt_rec_host_t *r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 xfs_fsblock_t v)
980{
981#if XFS_BIG_BLKNOS
982 ASSERT((v & XFS_MASK64HI(12)) == 0);
983 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(55)) |
984 (xfs_bmbt_rec_base_t)(v >> 43);
985 r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)) |
986 (xfs_bmbt_rec_base_t)(v << 21);
987#else /* !XFS_BIG_BLKNOS */
988 if (ISNULLSTARTBLOCK(v)) {
989 r->l0 |= (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
990 r->l1 = (xfs_bmbt_rec_base_t)XFS_MASK64HI(11) |
991 ((xfs_bmbt_rec_base_t)v << 21) |
992 (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
993 } else {
994 r->l0 &= ~(xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
995 r->l1 = ((xfs_bmbt_rec_base_t)v << 21) |
996 (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
997 }
998#endif /* XFS_BIG_BLKNOS */
999}
1000
1001/*
1002 * Set the startoff field in a bmap extent record.
1003 */
1004void
1005xfs_bmbt_set_startoff(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001006 xfs_bmbt_rec_host_t *r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 xfs_fileoff_t v)
1008{
1009 ASSERT((v & XFS_MASK64HI(9)) == 0);
1010 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t) XFS_MASK64HI(1)) |
1011 ((xfs_bmbt_rec_base_t)v << 9) |
1012 (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
1013}
1014
1015/*
1016 * Set the extent state field in a bmap extent record.
1017 */
1018void
1019xfs_bmbt_set_state(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001020 xfs_bmbt_rec_host_t *r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 xfs_exntst_t v)
1022{
1023 ASSERT(v == XFS_EXT_NORM || v == XFS_EXT_UNWRITTEN);
1024 if (v == XFS_EXT_NORM)
1025 r->l0 &= XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN);
1026 else
1027 r->l0 |= XFS_MASK64HI(BMBT_EXNTFLAG_BITLEN);
1028}
1029
1030/*
1031 * Convert in-memory form of btree root to on-disk form.
1032 */
1033void
1034xfs_bmbt_to_bmdr(
1035 xfs_bmbt_block_t *rblock,
1036 int rblocklen,
1037 xfs_bmdr_block_t *dblock,
1038 int dblocklen)
1039{
1040 int dmxr;
1041 xfs_bmbt_key_t *fkp;
Christoph Hellwig576039c2006-09-28 10:58:06 +10001042 __be64 *fpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 xfs_bmbt_key_t *tkp;
Christoph Hellwig576039c2006-09-28 10:58:06 +10001044 __be64 *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Christoph Hellwig16259e72005-11-02 15:11:25 +11001046 ASSERT(be32_to_cpu(rblock->bb_magic) == XFS_BMAP_MAGIC);
1047 ASSERT(be64_to_cpu(rblock->bb_leftsib) == NULLDFSBNO);
1048 ASSERT(be64_to_cpu(rblock->bb_rightsib) == NULLDFSBNO);
1049 ASSERT(be16_to_cpu(rblock->bb_level) > 0);
1050 dblock->bb_level = rblock->bb_level;
1051 dblock->bb_numrecs = rblock->bb_numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 dmxr = (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen, xfs_bmdr, 0);
1053 fkp = XFS_BMAP_BROOT_KEY_ADDR(rblock, 1, rblocklen);
Eric Sandeen2c36dde2007-02-10 18:37:33 +11001054 tkp = XFS_BTREE_KEY_ADDR(xfs_bmdr, dblock, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 fpp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, rblocklen);
Eric Sandeen2c36dde2007-02-10 18:37:33 +11001056 tpp = XFS_BTREE_PTR_ADDR(xfs_bmdr, dblock, 1, dmxr);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001057 dmxr = be16_to_cpu(dblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
Christoph Hellwig576039c2006-09-28 10:58:06 +10001059 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001063 * Check extent records, which have just been read, for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 * any bit in the extent flag field. ASSERT on debug
1065 * kernels, as this condition should not occur.
1066 * Return an error condition (1) if any flags found,
1067 * otherwise return 0.
1068 */
1069
1070int
1071xfs_check_nostate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001072 xfs_ifork_t *ifp,
1073 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 xfs_extnum_t num)
1075{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001076 for (; num > 0; num--, idx++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001077 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if ((ep->l0 >>
1079 (64 - BMBT_EXNTFLAG_BITLEN)) != 0) {
1080 ASSERT(0);
1081 return 1;
1082 }
1083 }
1084 return 0;
1085}
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001086
1087
1088STATIC struct xfs_btree_cur *
1089xfs_bmbt_dup_cursor(
1090 struct xfs_btree_cur *cur)
1091{
1092 struct xfs_btree_cur *new;
1093
1094 new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp,
1095 cur->bc_private.b.ip, cur->bc_private.b.whichfork);
1096
1097 /*
1098 * Copy the firstblock, flist, and flags values,
1099 * since init cursor doesn't get them.
1100 */
1101 new->bc_private.b.firstblock = cur->bc_private.b.firstblock;
1102 new->bc_private.b.flist = cur->bc_private.b.flist;
1103 new->bc_private.b.flags = cur->bc_private.b.flags;
1104
1105 return new;
1106}
1107
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001108STATIC void
1109xfs_bmbt_update_cursor(
1110 struct xfs_btree_cur *src,
1111 struct xfs_btree_cur *dst)
1112{
1113 ASSERT((dst->bc_private.b.firstblock != NULLFSBLOCK) ||
1114 (dst->bc_private.b.ip->i_d.di_flags & XFS_DIFLAG_REALTIME));
1115 ASSERT(dst->bc_private.b.flist == src->bc_private.b.flist);
1116
1117 dst->bc_private.b.allocated += src->bc_private.b.allocated;
1118 dst->bc_private.b.firstblock = src->bc_private.b.firstblock;
1119
1120 src->bc_private.b.allocated = 0;
1121}
1122
Christoph Hellwigce5e42d2008-10-30 16:55:23 +11001123STATIC int
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001124xfs_bmbt_alloc_block(
1125 struct xfs_btree_cur *cur,
1126 union xfs_btree_ptr *start,
1127 union xfs_btree_ptr *new,
1128 int length,
1129 int *stat)
1130{
1131 xfs_alloc_arg_t args; /* block allocation args */
1132 int error; /* error return value */
1133
1134 memset(&args, 0, sizeof(args));
1135 args.tp = cur->bc_tp;
1136 args.mp = cur->bc_mp;
1137 args.fsbno = cur->bc_private.b.firstblock;
1138 args.firstblock = args.fsbno;
1139
1140 if (args.fsbno == NULLFSBLOCK) {
1141 args.fsbno = be64_to_cpu(start->l);
1142 args.type = XFS_ALLOCTYPE_START_BNO;
1143 /*
1144 * Make sure there is sufficient room left in the AG to
1145 * complete a full tree split for an extent insert. If
1146 * we are converting the middle part of an extent then
1147 * we may need space for two tree splits.
1148 *
1149 * We are relying on the caller to make the correct block
1150 * reservation for this operation to succeed. If the
1151 * reservation amount is insufficient then we may fail a
1152 * block allocation here and corrupt the filesystem.
1153 */
1154 args.minleft = xfs_trans_get_block_res(args.tp);
1155 } else if (cur->bc_private.b.flist->xbf_low) {
1156 args.type = XFS_ALLOCTYPE_START_BNO;
1157 } else {
1158 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1159 }
1160
1161 args.minlen = args.maxlen = args.prod = 1;
1162 args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
1163 if (!args.wasdel && xfs_trans_get_block_res(args.tp) == 0) {
1164 error = XFS_ERROR(ENOSPC);
1165 goto error0;
1166 }
1167 error = xfs_alloc_vextent(&args);
1168 if (error)
1169 goto error0;
1170
1171 if (args.fsbno == NULLFSBLOCK && args.minleft) {
1172 /*
1173 * Could not find an AG with enough free space to satisfy
1174 * a full btree split. Try again without minleft and if
1175 * successful activate the lowspace algorithm.
1176 */
1177 args.fsbno = 0;
1178 args.type = XFS_ALLOCTYPE_FIRST_AG;
1179 args.minleft = 0;
1180 error = xfs_alloc_vextent(&args);
1181 if (error)
1182 goto error0;
1183 cur->bc_private.b.flist->xbf_low = 1;
1184 }
1185 if (args.fsbno == NULLFSBLOCK) {
1186 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1187 *stat = 0;
1188 return 0;
1189 }
1190 ASSERT(args.len == 1);
1191 cur->bc_private.b.firstblock = args.fsbno;
1192 cur->bc_private.b.allocated++;
1193 cur->bc_private.b.ip->i_d.di_nblocks++;
1194 xfs_trans_log_inode(args.tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
1195 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, args.tp, cur->bc_private.b.ip,
1196 XFS_TRANS_DQ_BCOUNT, 1L);
1197
1198 new->l = cpu_to_be64(args.fsbno);
1199
1200 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1201 *stat = 1;
1202 return 0;
1203
1204 error0:
1205 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1206 return error;
1207}
1208
1209STATIC int
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11001210xfs_bmbt_free_block(
1211 struct xfs_btree_cur *cur,
1212 struct xfs_buf *bp)
1213{
1214 struct xfs_mount *mp = cur->bc_mp;
1215 struct xfs_inode *ip = cur->bc_private.b.ip;
1216 struct xfs_trans *tp = cur->bc_tp;
1217 xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp));
1218
1219 xfs_bmap_add_free(fsbno, 1, cur->bc_private.b.flist, mp);
1220 ip->i_d.di_nblocks--;
1221
1222 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1223 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
1224 xfs_trans_binval(tp, bp);
1225 return 0;
1226}
1227
1228STATIC int
Christoph Hellwigce5e42d2008-10-30 16:55:23 +11001229xfs_bmbt_get_maxrecs(
1230 struct xfs_btree_cur *cur,
1231 int level)
1232{
1233 return XFS_BMAP_BLOCK_IMAXRECS(level, cur);
1234}
1235
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001236/*
1237 * Get the maximum records we could store in the on-disk format.
1238 *
1239 * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
1240 * for the root node this checks the available space in the dinode fork
1241 * so that we can resize the in-memory buffer to match it. After a
1242 * resize to the maximum size this function returns the same value
1243 * as xfs_bmbt_get_maxrecs for the root node, too.
1244 */
1245STATIC int
1246xfs_bmbt_get_dmaxrecs(
1247 struct xfs_btree_cur *cur,
1248 int level)
1249{
1250 return XFS_BMAP_BLOCK_DMAXRECS(level, cur);
1251}
1252
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001253STATIC void
1254xfs_bmbt_init_key_from_rec(
1255 union xfs_btree_key *key,
1256 union xfs_btree_rec *rec)
1257{
1258 key->bmbt.br_startoff =
1259 cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt));
1260}
1261
1262STATIC void
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001263xfs_bmbt_init_rec_from_key(
1264 union xfs_btree_key *key,
1265 union xfs_btree_rec *rec)
1266{
1267 ASSERT(key->bmbt.br_startoff != 0);
1268
1269 xfs_bmbt_disk_set_allf(&rec->bmbt, be64_to_cpu(key->bmbt.br_startoff),
1270 0, 0, XFS_EXT_NORM);
1271}
1272
1273STATIC void
1274xfs_bmbt_init_rec_from_cur(
1275 struct xfs_btree_cur *cur,
1276 union xfs_btree_rec *rec)
1277{
1278 xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b);
1279}
1280
1281STATIC void
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001282xfs_bmbt_init_ptr_from_cur(
1283 struct xfs_btree_cur *cur,
1284 union xfs_btree_ptr *ptr)
1285{
1286 ptr->l = 0;
1287}
1288
1289STATIC __int64_t
1290xfs_bmbt_key_diff(
1291 struct xfs_btree_cur *cur,
1292 union xfs_btree_key *key)
1293{
1294 return (__int64_t)be64_to_cpu(key->bmbt.br_startoff) -
1295 cur->bc_rec.b.br_startoff;
1296}
1297
Christoph Hellwig8c4ed632008-10-30 16:55:13 +11001298#ifdef XFS_BTREE_TRACE
1299ktrace_t *xfs_bmbt_trace_buf;
1300
1301STATIC void
1302xfs_bmbt_trace_enter(
1303 struct xfs_btree_cur *cur,
1304 const char *func,
1305 char *s,
1306 int type,
1307 int line,
1308 __psunsigned_t a0,
1309 __psunsigned_t a1,
1310 __psunsigned_t a2,
1311 __psunsigned_t a3,
1312 __psunsigned_t a4,
1313 __psunsigned_t a5,
1314 __psunsigned_t a6,
1315 __psunsigned_t a7,
1316 __psunsigned_t a8,
1317 __psunsigned_t a9,
1318 __psunsigned_t a10)
1319{
1320 struct xfs_inode *ip = cur->bc_private.b.ip;
1321 int whichfork = cur->bc_private.b.whichfork;
1322
1323 ktrace_enter(xfs_bmbt_trace_buf,
1324 (void *)((__psint_t)type | (whichfork << 8) | (line << 16)),
1325 (void *)func, (void *)s, (void *)ip, (void *)cur,
1326 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
1327 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
1328 (void *)a8, (void *)a9, (void *)a10);
1329 ktrace_enter(ip->i_btrace,
1330 (void *)((__psint_t)type | (whichfork << 8) | (line << 16)),
1331 (void *)func, (void *)s, (void *)ip, (void *)cur,
1332 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
1333 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
1334 (void *)a8, (void *)a9, (void *)a10);
1335}
1336
1337STATIC void
1338xfs_bmbt_trace_cursor(
1339 struct xfs_btree_cur *cur,
1340 __uint32_t *s0,
1341 __uint64_t *l0,
1342 __uint64_t *l1)
1343{
1344 struct xfs_bmbt_rec_host r;
1345
1346 xfs_bmbt_set_all(&r, &cur->bc_rec.b);
1347
1348 *s0 = (cur->bc_nlevels << 24) |
1349 (cur->bc_private.b.flags << 16) |
1350 cur->bc_private.b.allocated;
1351 *l0 = r.l0;
1352 *l1 = r.l1;
1353}
1354
1355STATIC void
1356xfs_bmbt_trace_key(
1357 struct xfs_btree_cur *cur,
1358 union xfs_btree_key *key,
1359 __uint64_t *l0,
1360 __uint64_t *l1)
1361{
1362 *l0 = be64_to_cpu(key->bmbt.br_startoff);
1363 *l1 = 0;
1364}
1365
1366STATIC void
1367xfs_bmbt_trace_record(
1368 struct xfs_btree_cur *cur,
1369 union xfs_btree_rec *rec,
1370 __uint64_t *l0,
1371 __uint64_t *l1,
1372 __uint64_t *l2)
1373{
1374 struct xfs_bmbt_irec irec;
1375
1376 xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
1377 *l0 = irec.br_startoff;
1378 *l1 = irec.br_startblock;
1379 *l2 = irec.br_blockcount;
1380}
1381#endif /* XFS_BTREE_TRACE */
1382
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001383static const struct xfs_btree_ops xfs_bmbt_ops = {
Christoph Hellwig65f1eae2008-10-30 16:55:34 +11001384 .rec_len = sizeof(xfs_bmbt_rec_t),
1385 .key_len = sizeof(xfs_bmbt_key_t),
1386
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001387 .dup_cursor = xfs_bmbt_dup_cursor,
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001388 .update_cursor = xfs_bmbt_update_cursor,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001389 .alloc_block = xfs_bmbt_alloc_block,
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11001390 .free_block = xfs_bmbt_free_block,
Christoph Hellwigce5e42d2008-10-30 16:55:23 +11001391 .get_maxrecs = xfs_bmbt_get_maxrecs,
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001392 .get_dmaxrecs = xfs_bmbt_get_dmaxrecs,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001393 .init_key_from_rec = xfs_bmbt_init_key_from_rec,
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001394 .init_rec_from_key = xfs_bmbt_init_rec_from_key,
1395 .init_rec_from_cur = xfs_bmbt_init_rec_from_cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001396 .init_ptr_from_cur = xfs_bmbt_init_ptr_from_cur,
1397 .key_diff = xfs_bmbt_key_diff,
Christoph Hellwig8c4ed632008-10-30 16:55:13 +11001398
1399#ifdef XFS_BTREE_TRACE
1400 .trace_enter = xfs_bmbt_trace_enter,
1401 .trace_cursor = xfs_bmbt_trace_cursor,
1402 .trace_key = xfs_bmbt_trace_key,
1403 .trace_record = xfs_bmbt_trace_record,
1404#endif
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001405};
1406
1407/*
1408 * Allocate a new bmap btree cursor.
1409 */
1410struct xfs_btree_cur * /* new bmap btree cursor */
1411xfs_bmbt_init_cursor(
1412 struct xfs_mount *mp, /* file system mount point */
1413 struct xfs_trans *tp, /* transaction pointer */
1414 struct xfs_inode *ip, /* inode owning the btree */
1415 int whichfork) /* data or attr fork */
1416{
1417 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1418 struct xfs_btree_cur *cur;
1419
1420 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
1421
1422 cur->bc_tp = tp;
1423 cur->bc_mp = mp;
1424 cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
1425 cur->bc_btnum = XFS_BTNUM_BMAP;
1426 cur->bc_blocklog = mp->m_sb.sb_blocklog;
1427
1428 cur->bc_ops = &xfs_bmbt_ops;
Christoph Hellwige99ab902008-10-30 16:54:33 +11001429 cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE;
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001430
1431 cur->bc_private.b.forksize = XFS_IFORK_SIZE(ip, whichfork);
1432 cur->bc_private.b.ip = ip;
1433 cur->bc_private.b.firstblock = NULLFSBLOCK;
1434 cur->bc_private.b.flist = NULL;
1435 cur->bc_private.b.allocated = 0;
1436 cur->bc_private.b.flags = 0;
1437 cur->bc_private.b.whichfork = whichfork;
1438
1439 return cur;
1440}