]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_bmap_btree.c
libxlog: sync up with 2.6.38 kernel code
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_bmap_btree.c
CommitLineData
2bd0ea18 1/*
da23017d
NS
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5000d01d 4 *
da23017d
NS
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
2bd0ea18 7 * published by the Free Software Foundation.
5000d01d 8 *
da23017d
NS
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.
5000d01d 13 *
da23017d
NS
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
2bd0ea18 17 */
2bd0ea18
NS
18#include <xfs.h>
19
2bd0ea18
NS
20/*
21 * Determine the extent state.
22 */
23/* ARGSUSED */
24STATIC xfs_exntst_t
25xfs_extent_state(
26 xfs_filblks_t blks,
27 int extent_flag)
28{
29 if (extent_flag) {
30 ASSERT(blks != 0); /* saved for DMIG */
31 return XFS_EXT_UNWRITTEN;
32 }
33 return XFS_EXT_NORM;
34}
35
2bd0ea18
NS
36/*
37 * Convert on-disk form of btree root to in-memory form.
38 */
39void
40xfs_bmdr_to_bmbt(
b3563c19 41 struct xfs_mount *mp,
2bd0ea18
NS
42 xfs_bmdr_block_t *dblock,
43 int dblocklen,
b3563c19 44 struct xfs_btree_block *rblock,
2bd0ea18
NS
45 int rblocklen)
46{
47 int dmxr;
48 xfs_bmbt_key_t *fkp;
5e656dbb 49 __be64 *fpp;
2bd0ea18 50 xfs_bmbt_key_t *tkp;
5e656dbb 51 __be64 *tpp;
2bd0ea18 52
6e3140c7
NS
53 rblock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
54 rblock->bb_level = dblock->bb_level;
55 ASSERT(be16_to_cpu(rblock->bb_level) > 0);
56 rblock->bb_numrecs = dblock->bb_numrecs;
b3563c19
BN
57 rblock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
58 rblock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
59 dmxr = xfs_bmdr_maxrecs(mp, dblocklen, 0);
60 fkp = XFS_BMDR_KEY_ADDR(dblock, 1);
61 tkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
62 fpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
63 tpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
6e3140c7 64 dmxr = be16_to_cpu(dblock->bb_numrecs);
32181a02 65 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
5e656dbb 66 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
2bd0ea18
NS
67}
68
2bd0ea18
NS
69/*
70 * Convert a compressed bmap extent record to an uncompressed form.
71 * This code must be in sync with the routines xfs_bmbt_get_startoff,
72 * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
73 */
dfc130f3 74
5e656dbb 75STATIC_INLINE void
f9e56f43
NS
76__xfs_bmbt_get_all(
77 __uint64_t l0,
78 __uint64_t l1,
79 xfs_bmbt_irec_t *s)
2bd0ea18
NS
80{
81 int ext_flag;
82 xfs_exntst_t st;
83
4542f191
NS
84 ext_flag = (int)(l0 >> (64 - BMBT_EXNTFLAG_BITLEN));
85 s->br_startoff = ((xfs_fileoff_t)l0 &
2bd0ea18 86 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
2b288ccf 87#if XFS_BIG_BLKNOS
4542f191
NS
88 s->br_startblock = (((xfs_fsblock_t)l0 & XFS_MASK64LO(9)) << 43) |
89 (((xfs_fsblock_t)l1) >> 21);
2bd0ea18
NS
90#else
91#ifdef DEBUG
92 {
93 xfs_dfsbno_t b;
94
4542f191
NS
95 b = (((xfs_dfsbno_t)l0 & XFS_MASK64LO(9)) << 43) |
96 (((xfs_dfsbno_t)l1) >> 21);
2bd0ea18
NS
97 ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
98 s->br_startblock = (xfs_fsblock_t)b;
99 }
100#else /* !DEBUG */
4542f191 101 s->br_startblock = (xfs_fsblock_t)(((xfs_dfsbno_t)l1) >> 21);
2bd0ea18 102#endif /* DEBUG */
2b288ccf 103#endif /* XFS_BIG_BLKNOS */
4542f191 104 s->br_blockcount = (xfs_filblks_t)(l1 & XFS_MASK64LO(21));
2bd0ea18
NS
105 /* This is xfs_extent_state() in-line */
106 if (ext_flag) {
107 ASSERT(s->br_blockcount != 0); /* saved for DMIG */
108 st = XFS_EXT_UNWRITTEN;
109 } else
110 st = XFS_EXT_NORM;
111 s->br_state = st;
112}
113
f9e56f43
NS
114void
115xfs_bmbt_get_all(
5e656dbb 116 xfs_bmbt_rec_host_t *r,
f9e56f43
NS
117 xfs_bmbt_irec_t *s)
118{
119 __xfs_bmbt_get_all(r->l0, r->l1, s);
120}
121
2bd0ea18 122/*
b34acbba 123 * Extract the blockcount field from an in memory bmap extent record.
2bd0ea18
NS
124 */
125xfs_filblks_t
126xfs_bmbt_get_blockcount(
5e656dbb 127 xfs_bmbt_rec_host_t *r)
2bd0ea18 128{
f9e56f43 129 return (xfs_filblks_t)(r->l1 & XFS_MASK64LO(21));
2bd0ea18
NS
130}
131
132/*
b34acbba 133 * Extract the startblock field from an in memory bmap extent record.
2bd0ea18
NS
134 */
135xfs_fsblock_t
136xfs_bmbt_get_startblock(
5e656dbb 137 xfs_bmbt_rec_host_t *r)
2bd0ea18 138{
2b288ccf 139#if XFS_BIG_BLKNOS
f9e56f43
NS
140 return (((xfs_fsblock_t)r->l0 & XFS_MASK64LO(9)) << 43) |
141 (((xfs_fsblock_t)r->l1) >> 21);
142#else
143#ifdef DEBUG
144 xfs_dfsbno_t b;
145
146 b = (((xfs_dfsbno_t)r->l0 & XFS_MASK64LO(9)) << 43) |
147 (((xfs_dfsbno_t)r->l1) >> 21);
148 ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
149 return (xfs_fsblock_t)b;
150#else /* !DEBUG */
151 return (xfs_fsblock_t)(((xfs_dfsbno_t)r->l1) >> 21);
152#endif /* DEBUG */
2b288ccf 153#endif /* XFS_BIG_BLKNOS */
f9e56f43
NS
154}
155
156/*
b34acbba 157 * Extract the startoff field from an in memory bmap extent record.
f9e56f43
NS
158 */
159xfs_fileoff_t
160xfs_bmbt_get_startoff(
5e656dbb 161 xfs_bmbt_rec_host_t *r)
f9e56f43
NS
162{
163 return ((xfs_fileoff_t)r->l0 &
164 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
165}
166
167xfs_exntst_t
168xfs_bmbt_get_state(
5e656dbb 169 xfs_bmbt_rec_host_t *r)
f9e56f43
NS
170{
171 int ext_flag;
172
173 ext_flag = (int)((r->l0) >> (64 - BMBT_EXNTFLAG_BITLEN));
174 return xfs_extent_state(xfs_bmbt_get_blockcount(r),
b194c7d8 175 ext_flag);
2bd0ea18
NS
176}
177
b194c7d8 178/* Endian flipping versions of the bmbt extraction functions */
2bd0ea18 179void
b194c7d8
BN
180xfs_bmbt_disk_get_all(
181 xfs_bmbt_rec_t *r,
182 xfs_bmbt_irec_t *s)
2bd0ea18 183{
9c32ccfc
BN
184 __xfs_bmbt_get_all(get_unaligned_be64(&r->l0),
185 get_unaligned_be64(&r->l1), s);
2bd0ea18
NS
186}
187
188/*
b194c7d8 189 * Extract the blockcount field from an on disk bmap extent record.
2bd0ea18 190 */
b194c7d8
BN
191xfs_filblks_t
192xfs_bmbt_disk_get_blockcount(
193 xfs_bmbt_rec_t *r)
2bd0ea18 194{
b194c7d8 195 return (xfs_filblks_t)(be64_to_cpu(r->l1) & XFS_MASK64LO(21));
2bd0ea18
NS
196}
197
2bd0ea18 198/*
b194c7d8 199 * Extract the startoff field from a disk format bmap extent record.
2bd0ea18 200 */
b194c7d8
BN
201xfs_fileoff_t
202xfs_bmbt_disk_get_startoff(
203 xfs_bmbt_rec_t *r)
2bd0ea18 204{
b194c7d8
BN
205 return ((xfs_fileoff_t)be64_to_cpu(r->l0) &
206 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
2bd0ea18
NS
207}
208
b194c7d8 209
f9e56f43
NS
210/*
211 * Set all the fields in a bmap extent record from the arguments.
212 */
213void
214xfs_bmbt_set_allf(
5e656dbb
BN
215 xfs_bmbt_rec_host_t *r,
216 xfs_fileoff_t startoff,
217 xfs_fsblock_t startblock,
218 xfs_filblks_t blockcount,
219 xfs_exntst_t state)
f9e56f43 220{
5e656dbb
BN
221 int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
222
223 ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN);
224 ASSERT((startoff & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
225 ASSERT((blockcount & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
f9e56f43 226
2b288ccf 227#if XFS_BIG_BLKNOS
5e656dbb
BN
228 ASSERT((startblock & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
229
f9e56f43 230 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
5e656dbb
BN
231 ((xfs_bmbt_rec_base_t)startoff << 9) |
232 ((xfs_bmbt_rec_base_t)startblock >> 43);
233 r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) |
234 ((xfs_bmbt_rec_base_t)blockcount &
f9e56f43 235 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2b288ccf 236#else /* !XFS_BIG_BLKNOS */
5e656dbb 237 if (ISNULLSTARTBLOCK(startblock)) {
f9e56f43 238 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
5e656dbb 239 ((xfs_bmbt_rec_base_t)startoff << 9) |
f9e56f43
NS
240 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
241 r->l1 = XFS_MASK64HI(11) |
5e656dbb
BN
242 ((xfs_bmbt_rec_base_t)startblock << 21) |
243 ((xfs_bmbt_rec_base_t)blockcount &
f9e56f43
NS
244 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
245 } else {
246 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
5e656dbb
BN
247 ((xfs_bmbt_rec_base_t)startoff << 9);
248 r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) |
249 ((xfs_bmbt_rec_base_t)blockcount &
f9e56f43
NS
250 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
251 }
2b288ccf 252#endif /* XFS_BIG_BLKNOS */
f9e56f43
NS
253}
254
f9e56f43
NS
255/*
256 * Set all the fields in a bmap extent record from the uncompressed form.
257 */
258void
5e656dbb
BN
259xfs_bmbt_set_all(
260 xfs_bmbt_rec_host_t *r,
261 xfs_bmbt_irec_t *s)
f9e56f43 262{
5e656dbb
BN
263 xfs_bmbt_set_allf(r, s->br_startoff, s->br_startblock,
264 s->br_blockcount, s->br_state);
2bd0ea18
NS
265}
266
5e656dbb 267
2bd0ea18 268/*
f9e56f43 269 * Set all the fields in a disk format bmap extent record from the arguments.
2bd0ea18
NS
270 */
271void
f9e56f43 272xfs_bmbt_disk_set_allf(
5e656dbb
BN
273 xfs_bmbt_rec_t *r,
274 xfs_fileoff_t startoff,
275 xfs_fsblock_t startblock,
276 xfs_filblks_t blockcount,
277 xfs_exntst_t state)
2bd0ea18 278{
5e656dbb
BN
279 int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
280
281 ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN);
282 ASSERT((startoff & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
283 ASSERT((blockcount & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
2bd0ea18 284
2b288ccf 285#if XFS_BIG_BLKNOS
5e656dbb
BN
286 ASSERT((startblock & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
287
288 r->l0 = cpu_to_be64(
289 ((xfs_bmbt_rec_base_t)extent_flag << 63) |
290 ((xfs_bmbt_rec_base_t)startoff << 9) |
291 ((xfs_bmbt_rec_base_t)startblock >> 43));
292 r->l1 = cpu_to_be64(
293 ((xfs_bmbt_rec_base_t)startblock << 21) |
294 ((xfs_bmbt_rec_base_t)blockcount &
295 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2b288ccf 296#else /* !XFS_BIG_BLKNOS */
5e656dbb
BN
297 if (ISNULLSTARTBLOCK(startblock)) {
298 r->l0 = cpu_to_be64(
299 ((xfs_bmbt_rec_base_t)extent_flag << 63) |
300 ((xfs_bmbt_rec_base_t)startoff << 9) |
301 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
302 r->l1 = cpu_to_be64(XFS_MASK64HI(11) |
303 ((xfs_bmbt_rec_base_t)startblock << 21) |
304 ((xfs_bmbt_rec_base_t)blockcount &
2bd0ea18
NS
305 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
306 } else {
5e656dbb
BN
307 r->l0 = cpu_to_be64(
308 ((xfs_bmbt_rec_base_t)extent_flag << 63) |
309 ((xfs_bmbt_rec_base_t)startoff << 9));
310 r->l1 = cpu_to_be64(
311 ((xfs_bmbt_rec_base_t)startblock << 21) |
312 ((xfs_bmbt_rec_base_t)blockcount &
313 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2bd0ea18 314 }
2b288ccf 315#endif /* XFS_BIG_BLKNOS */
2bd0ea18 316}
5e656dbb
BN
317
318/*
319 * Set all the fields in a bmap extent record from the uncompressed form.
320 */
321void
322xfs_bmbt_disk_set_all(
323 xfs_bmbt_rec_t *r,
324 xfs_bmbt_irec_t *s)
325{
326 xfs_bmbt_disk_set_allf(r, s->br_startoff, s->br_startblock,
327 s->br_blockcount, s->br_state);
328}
2bd0ea18
NS
329
330/*
331 * Set the blockcount field in a bmap extent record.
332 */
333void
334xfs_bmbt_set_blockcount(
5e656dbb 335 xfs_bmbt_rec_host_t *r,
2bd0ea18
NS
336 xfs_filblks_t v)
337{
2bd0ea18 338 ASSERT((v & XFS_MASK64HI(43)) == 0);
f9e56f43
NS
339 r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(43)) |
340 (xfs_bmbt_rec_base_t)(v & XFS_MASK64LO(21));
2bd0ea18
NS
341}
342
343/*
344 * Set the startblock field in a bmap extent record.
345 */
346void
347xfs_bmbt_set_startblock(
5e656dbb 348 xfs_bmbt_rec_host_t *r,
2bd0ea18
NS
349 xfs_fsblock_t v)
350{
2b288ccf 351#if XFS_BIG_BLKNOS
2bd0ea18 352 ASSERT((v & XFS_MASK64HI(12)) == 0);
f9e56f43
NS
353 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(55)) |
354 (xfs_bmbt_rec_base_t)(v >> 43);
355 r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)) |
356 (xfs_bmbt_rec_base_t)(v << 21);
2b288ccf 357#else /* !XFS_BIG_BLKNOS */
2bd0ea18 358 if (ISNULLSTARTBLOCK(v)) {
f9e56f43
NS
359 r->l0 |= (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
360 r->l1 = (xfs_bmbt_rec_base_t)XFS_MASK64HI(11) |
2bd0ea18 361 ((xfs_bmbt_rec_base_t)v << 21) |
f9e56f43 362 (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2bd0ea18 363 } else {
f9e56f43
NS
364 r->l0 &= ~(xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
365 r->l1 = ((xfs_bmbt_rec_base_t)v << 21) |
366 (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2bd0ea18 367 }
2b288ccf 368#endif /* XFS_BIG_BLKNOS */
2bd0ea18
NS
369}
370
371/*
372 * Set the startoff field in a bmap extent record.
373 */
374void
375xfs_bmbt_set_startoff(
5e656dbb 376 xfs_bmbt_rec_host_t *r,
2bd0ea18
NS
377 xfs_fileoff_t v)
378{
2bd0ea18 379 ASSERT((v & XFS_MASK64HI(9)) == 0);
f9e56f43 380 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t) XFS_MASK64HI(1)) |
2bd0ea18 381 ((xfs_bmbt_rec_base_t)v << 9) |
f9e56f43 382 (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
2bd0ea18
NS
383}
384
385/*
386 * Set the extent state field in a bmap extent record.
387 */
388void
389xfs_bmbt_set_state(
5e656dbb 390 xfs_bmbt_rec_host_t *r,
2bd0ea18
NS
391 xfs_exntst_t v)
392{
393 ASSERT(v == XFS_EXT_NORM || v == XFS_EXT_UNWRITTEN);
394 if (v == XFS_EXT_NORM)
f9e56f43 395 r->l0 &= XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN);
2bd0ea18 396 else
f9e56f43 397 r->l0 |= XFS_MASK64HI(BMBT_EXNTFLAG_BITLEN);
2bd0ea18
NS
398}
399
400/*
401 * Convert in-memory form of btree root to on-disk form.
402 */
403void
404xfs_bmbt_to_bmdr(
b3563c19
BN
405 struct xfs_mount *mp,
406 struct xfs_btree_block *rblock,
2bd0ea18
NS
407 int rblocklen,
408 xfs_bmdr_block_t *dblock,
409 int dblocklen)
410{
411 int dmxr;
412 xfs_bmbt_key_t *fkp;
5e656dbb 413 __be64 *fpp;
2bd0ea18 414 xfs_bmbt_key_t *tkp;
5e656dbb 415 __be64 *tpp;
2bd0ea18 416
6e3140c7 417 ASSERT(be32_to_cpu(rblock->bb_magic) == XFS_BMAP_MAGIC);
b3563c19
BN
418 ASSERT(be64_to_cpu(rblock->bb_u.l.bb_leftsib) == NULLDFSBNO);
419 ASSERT(be64_to_cpu(rblock->bb_u.l.bb_rightsib) == NULLDFSBNO);
6e3140c7
NS
420 ASSERT(be16_to_cpu(rblock->bb_level) > 0);
421 dblock->bb_level = rblock->bb_level;
422 dblock->bb_numrecs = rblock->bb_numrecs;
b3563c19
BN
423 dmxr = xfs_bmdr_maxrecs(mp, dblocklen, 0);
424 fkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
425 tkp = XFS_BMDR_KEY_ADDR(dblock, 1);
426 fpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
427 tpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
6e3140c7 428 dmxr = be16_to_cpu(dblock->bb_numrecs);
32181a02 429 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
5e656dbb 430 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
2bd0ea18
NS
431}
432
2bd0ea18 433/*
5e656dbb 434 * Check extent records, which have just been read, for
2bd0ea18
NS
435 * any bit in the extent flag field. ASSERT on debug
436 * kernels, as this condition should not occur.
437 * Return an error condition (1) if any flags found,
438 * otherwise return 0.
439 */
dfc130f3 440
2bd0ea18
NS
441int
442xfs_check_nostate_extents(
5e656dbb
BN
443 xfs_ifork_t *ifp,
444 xfs_extnum_t idx,
2bd0ea18
NS
445 xfs_extnum_t num)
446{
5e656dbb
BN
447 for (; num > 0; num--, idx++) {
448 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
f9e56f43 449 if ((ep->l0 >>
4542f191 450 (64 - BMBT_EXNTFLAG_BITLEN)) != 0) {
2bd0ea18
NS
451 ASSERT(0);
452 return 1;
453 }
454 }
455 return 0;
456}
b194c7d8
BN
457
458
459STATIC struct xfs_btree_cur *
460xfs_bmbt_dup_cursor(
461 struct xfs_btree_cur *cur)
462{
463 struct xfs_btree_cur *new;
464
465 new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp,
466 cur->bc_private.b.ip, cur->bc_private.b.whichfork);
467
468 /*
469 * Copy the firstblock, flist, and flags values,
470 * since init cursor doesn't get them.
471 */
472 new->bc_private.b.firstblock = cur->bc_private.b.firstblock;
473 new->bc_private.b.flist = cur->bc_private.b.flist;
474 new->bc_private.b.flags = cur->bc_private.b.flags;
475
476 return new;
477}
478
479STATIC void
480xfs_bmbt_update_cursor(
481 struct xfs_btree_cur *src,
482 struct xfs_btree_cur *dst)
483{
484 ASSERT((dst->bc_private.b.firstblock != NULLFSBLOCK) ||
485 (dst->bc_private.b.ip->i_d.di_flags & XFS_DIFLAG_REALTIME));
486 ASSERT(dst->bc_private.b.flist == src->bc_private.b.flist);
487
488 dst->bc_private.b.allocated += src->bc_private.b.allocated;
489 dst->bc_private.b.firstblock = src->bc_private.b.firstblock;
490
491 src->bc_private.b.allocated = 0;
492}
493
494STATIC int
495xfs_bmbt_alloc_block(
496 struct xfs_btree_cur *cur,
497 union xfs_btree_ptr *start,
498 union xfs_btree_ptr *new,
499 int length,
500 int *stat)
501{
502 xfs_alloc_arg_t args; /* block allocation args */
503 int error; /* error return value */
504
505 memset(&args, 0, sizeof(args));
506 args.tp = cur->bc_tp;
507 args.mp = cur->bc_mp;
508 args.fsbno = cur->bc_private.b.firstblock;
509 args.firstblock = args.fsbno;
510
511 if (args.fsbno == NULLFSBLOCK) {
512 args.fsbno = be64_to_cpu(start->l);
513 args.type = XFS_ALLOCTYPE_START_BNO;
514 /*
515 * Make sure there is sufficient room left in the AG to
516 * complete a full tree split for an extent insert. If
517 * we are converting the middle part of an extent then
518 * we may need space for two tree splits.
519 *
520 * We are relying on the caller to make the correct block
521 * reservation for this operation to succeed. If the
522 * reservation amount is insufficient then we may fail a
523 * block allocation here and corrupt the filesystem.
524 */
525 args.minleft = xfs_trans_get_block_res(args.tp);
526 } else if (cur->bc_private.b.flist->xbf_low) {
527 args.type = XFS_ALLOCTYPE_START_BNO;
528 } else {
529 args.type = XFS_ALLOCTYPE_NEAR_BNO;
530 }
531
532 args.minlen = args.maxlen = args.prod = 1;
533 args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
534 if (!args.wasdel && xfs_trans_get_block_res(args.tp) == 0) {
535 error = XFS_ERROR(ENOSPC);
536 goto error0;
537 }
538 error = xfs_alloc_vextent(&args);
539 if (error)
540 goto error0;
541
542 if (args.fsbno == NULLFSBLOCK && args.minleft) {
543 /*
544 * Could not find an AG with enough free space to satisfy
545 * a full btree split. Try again without minleft and if
546 * successful activate the lowspace algorithm.
547 */
548 args.fsbno = 0;
549 args.type = XFS_ALLOCTYPE_FIRST_AG;
550 args.minleft = 0;
551 error = xfs_alloc_vextent(&args);
552 if (error)
553 goto error0;
554 cur->bc_private.b.flist->xbf_low = 1;
555 }
556 if (args.fsbno == NULLFSBLOCK) {
557 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
558 *stat = 0;
559 return 0;
560 }
561 ASSERT(args.len == 1);
562 cur->bc_private.b.firstblock = args.fsbno;
563 cur->bc_private.b.allocated++;
564 cur->bc_private.b.ip->i_d.di_nblocks++;
565 xfs_trans_log_inode(args.tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
566 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, args.tp, cur->bc_private.b.ip,
567 XFS_TRANS_DQ_BCOUNT, 1L);
568
569 new->l = cpu_to_be64(args.fsbno);
570
571 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
572 *stat = 1;
573 return 0;
574
575 error0:
576 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
577 return error;
578}
579
580STATIC int
581xfs_bmbt_free_block(
582 struct xfs_btree_cur *cur,
583 struct xfs_buf *bp)
584{
585 struct xfs_mount *mp = cur->bc_mp;
586 struct xfs_inode *ip = cur->bc_private.b.ip;
587 struct xfs_trans *tp = cur->bc_tp;
588 xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp));
589
590 xfs_bmap_add_free(fsbno, 1, cur->bc_private.b.flist, mp);
591 ip->i_d.di_nblocks--;
592
593 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
594 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
595 xfs_trans_binval(tp, bp);
596 return 0;
597}
598
599STATIC int
600xfs_bmbt_get_minrecs(
601 struct xfs_btree_cur *cur,
602 int level)
603{
b3563c19
BN
604 if (level == cur->bc_nlevels - 1) {
605 struct xfs_ifork *ifp;
606
607 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
608 cur->bc_private.b.whichfork);
609
610 return xfs_bmbt_maxrecs(cur->bc_mp,
611 ifp->if_broot_bytes, level == 0) / 2;
612 }
613
614 return cur->bc_mp->m_bmap_dmnr[level != 0];
b194c7d8
BN
615}
616
b3563c19 617int
b194c7d8
BN
618xfs_bmbt_get_maxrecs(
619 struct xfs_btree_cur *cur,
620 int level)
621{
b3563c19
BN
622 if (level == cur->bc_nlevels - 1) {
623 struct xfs_ifork *ifp;
624
625 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
626 cur->bc_private.b.whichfork);
627
628 return xfs_bmbt_maxrecs(cur->bc_mp,
629 ifp->if_broot_bytes, level == 0);
630 }
631
632 return cur->bc_mp->m_bmap_dmxr[level != 0];
633
b194c7d8
BN
634}
635
636/*
637 * Get the maximum records we could store in the on-disk format.
638 *
639 * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
640 * for the root node this checks the available space in the dinode fork
641 * so that we can resize the in-memory buffer to match it. After a
642 * resize to the maximum size this function returns the same value
643 * as xfs_bmbt_get_maxrecs for the root node, too.
644 */
645STATIC int
646xfs_bmbt_get_dmaxrecs(
647 struct xfs_btree_cur *cur,
648 int level)
649{
b3563c19
BN
650 if (level != cur->bc_nlevels - 1)
651 return cur->bc_mp->m_bmap_dmxr[level != 0];
652 return xfs_bmdr_maxrecs(cur->bc_mp, cur->bc_private.b.forksize,
653 level == 0);
b194c7d8
BN
654}
655
656STATIC void
657xfs_bmbt_init_key_from_rec(
658 union xfs_btree_key *key,
659 union xfs_btree_rec *rec)
660{
661 key->bmbt.br_startoff =
662 cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt));
663}
664
665STATIC void
666xfs_bmbt_init_rec_from_key(
667 union xfs_btree_key *key,
668 union xfs_btree_rec *rec)
669{
670 ASSERT(key->bmbt.br_startoff != 0);
671
672 xfs_bmbt_disk_set_allf(&rec->bmbt, be64_to_cpu(key->bmbt.br_startoff),
673 0, 0, XFS_EXT_NORM);
674}
675
676STATIC void
677xfs_bmbt_init_rec_from_cur(
678 struct xfs_btree_cur *cur,
679 union xfs_btree_rec *rec)
680{
681 xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b);
682}
683
684STATIC void
685xfs_bmbt_init_ptr_from_cur(
686 struct xfs_btree_cur *cur,
687 union xfs_btree_ptr *ptr)
688{
689 ptr->l = 0;
690}
691
692STATIC __int64_t
693xfs_bmbt_key_diff(
694 struct xfs_btree_cur *cur,
695 union xfs_btree_key *key)
696{
697 return (__int64_t)be64_to_cpu(key->bmbt.br_startoff) -
698 cur->bc_rec.b.br_startoff;
699}
700
701#ifdef DEBUG
702STATIC int
703xfs_bmbt_keys_inorder(
704 struct xfs_btree_cur *cur,
705 union xfs_btree_key *k1,
706 union xfs_btree_key *k2)
707{
708 return be64_to_cpu(k1->bmbt.br_startoff) <
709 be64_to_cpu(k2->bmbt.br_startoff);
710}
711
712STATIC int
713xfs_bmbt_recs_inorder(
714 struct xfs_btree_cur *cur,
715 union xfs_btree_rec *r1,
716 union xfs_btree_rec *r2)
717{
718 return xfs_bmbt_disk_get_startoff(&r1->bmbt) +
719 xfs_bmbt_disk_get_blockcount(&r1->bmbt) <=
720 xfs_bmbt_disk_get_startoff(&r2->bmbt);
721}
722#endif /* DEBUG */
723
724#ifdef XFS_BTREE_TRACE
725ktrace_t *xfs_bmbt_trace_buf;
726
727STATIC void
728xfs_bmbt_trace_enter(
729 struct xfs_btree_cur *cur,
730 const char *func,
731 char *s,
732 int type,
733 int line,
734 __psunsigned_t a0,
735 __psunsigned_t a1,
736 __psunsigned_t a2,
737 __psunsigned_t a3,
738 __psunsigned_t a4,
739 __psunsigned_t a5,
740 __psunsigned_t a6,
741 __psunsigned_t a7,
742 __psunsigned_t a8,
743 __psunsigned_t a9,
744 __psunsigned_t a10)
745{
746 struct xfs_inode *ip = cur->bc_private.b.ip;
747 int whichfork = cur->bc_private.b.whichfork;
748
749 ktrace_enter(xfs_bmbt_trace_buf,
750 (void *)((__psint_t)type | (whichfork << 8) | (line << 16)),
751 (void *)func, (void *)s, (void *)ip, (void *)cur,
752 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
753 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
754 (void *)a8, (void *)a9, (void *)a10);
755 ktrace_enter(ip->i_btrace,
756 (void *)((__psint_t)type | (whichfork << 8) | (line << 16)),
757 (void *)func, (void *)s, (void *)ip, (void *)cur,
758 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
759 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
760 (void *)a8, (void *)a9, (void *)a10);
761}
762
763STATIC void
764xfs_bmbt_trace_cursor(
765 struct xfs_btree_cur *cur,
766 __uint32_t *s0,
767 __uint64_t *l0,
768 __uint64_t *l1)
769{
770 struct xfs_bmbt_rec_host r;
771
772 xfs_bmbt_set_all(&r, &cur->bc_rec.b);
773
774 *s0 = (cur->bc_nlevels << 24) |
775 (cur->bc_private.b.flags << 16) |
776 cur->bc_private.b.allocated;
777 *l0 = r.l0;
778 *l1 = r.l1;
779}
780
781STATIC void
782xfs_bmbt_trace_key(
783 struct xfs_btree_cur *cur,
784 union xfs_btree_key *key,
785 __uint64_t *l0,
786 __uint64_t *l1)
787{
788 *l0 = be64_to_cpu(key->bmbt.br_startoff);
789 *l1 = 0;
790}
791
792STATIC void
793xfs_bmbt_trace_record(
794 struct xfs_btree_cur *cur,
795 union xfs_btree_rec *rec,
796 __uint64_t *l0,
797 __uint64_t *l1,
798 __uint64_t *l2)
799{
800 struct xfs_bmbt_irec irec;
801
802 xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
803 *l0 = irec.br_startoff;
804 *l1 = irec.br_startblock;
805 *l2 = irec.br_blockcount;
806}
807#endif /* XFS_BTREE_TRACE */
808
809static const struct xfs_btree_ops xfs_bmbt_ops = {
810 .rec_len = sizeof(xfs_bmbt_rec_t),
811 .key_len = sizeof(xfs_bmbt_key_t),
812
813 .dup_cursor = xfs_bmbt_dup_cursor,
814 .update_cursor = xfs_bmbt_update_cursor,
815 .alloc_block = xfs_bmbt_alloc_block,
816 .free_block = xfs_bmbt_free_block,
817 .get_maxrecs = xfs_bmbt_get_maxrecs,
818 .get_minrecs = xfs_bmbt_get_minrecs,
819 .get_dmaxrecs = xfs_bmbt_get_dmaxrecs,
820 .init_key_from_rec = xfs_bmbt_init_key_from_rec,
821 .init_rec_from_key = xfs_bmbt_init_rec_from_key,
822 .init_rec_from_cur = xfs_bmbt_init_rec_from_cur,
823 .init_ptr_from_cur = xfs_bmbt_init_ptr_from_cur,
824 .key_diff = xfs_bmbt_key_diff,
825
826#ifdef DEBUG
827 .keys_inorder = xfs_bmbt_keys_inorder,
828 .recs_inorder = xfs_bmbt_recs_inorder,
829#endif
830
831#ifdef XFS_BTREE_TRACE
832 .trace_enter = xfs_bmbt_trace_enter,
833 .trace_cursor = xfs_bmbt_trace_cursor,
834 .trace_key = xfs_bmbt_trace_key,
835 .trace_record = xfs_bmbt_trace_record,
836#endif
837};
838
839/*
840 * Allocate a new bmap btree cursor.
841 */
842struct xfs_btree_cur * /* new bmap btree cursor */
843xfs_bmbt_init_cursor(
844 struct xfs_mount *mp, /* file system mount point */
845 struct xfs_trans *tp, /* transaction pointer */
846 struct xfs_inode *ip, /* inode owning the btree */
847 int whichfork) /* data or attr fork */
848{
849 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
850 struct xfs_btree_cur *cur;
851
852 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
853
854 cur->bc_tp = tp;
855 cur->bc_mp = mp;
856 cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
857 cur->bc_btnum = XFS_BTNUM_BMAP;
858 cur->bc_blocklog = mp->m_sb.sb_blocklog;
859
860 cur->bc_ops = &xfs_bmbt_ops;
861 cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE;
862
863 cur->bc_private.b.forksize = XFS_IFORK_SIZE(ip, whichfork);
864 cur->bc_private.b.ip = ip;
865 cur->bc_private.b.firstblock = NULLFSBLOCK;
866 cur->bc_private.b.flist = NULL;
867 cur->bc_private.b.allocated = 0;
868 cur->bc_private.b.flags = 0;
869 cur->bc_private.b.whichfork = whichfork;
870
871 return cur;
872}
b3563c19
BN
873
874/*
875 * Calculate number of records in a bmap btree block.
876 */
877int
878xfs_bmbt_maxrecs(
879 struct xfs_mount *mp,
880 int blocklen,
881 int leaf)
882{
883 blocklen -= XFS_BMBT_BLOCK_LEN(mp);
884
885 if (leaf)
886 return blocklen / sizeof(xfs_bmbt_rec_t);
887 return blocklen / (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t));
888}
889
890/*
891 * Calculate number of records in a bmap btree inode root.
892 */
893int
894xfs_bmdr_maxrecs(
895 struct xfs_mount *mp,
896 int blocklen,
897 int leaf)
898{
899 blocklen -= sizeof(xfs_bmdr_block_t);
900
901 if (leaf)
902 return blocklen / sizeof(xfs_bmdr_rec_t);
903 return blocklen / (sizeof(xfs_bmdr_key_t) + sizeof(xfs_bmdr_ptr_t));
904}