]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 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 | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 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. | |
1da177e4 | 13 | * |
7b718769 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 | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_fs.h" |
1da177e4 | 20 | #include "xfs_types.h" |
a844f451 | 21 | #include "xfs_bit.h" |
1da177e4 | 22 | #include "xfs_log.h" |
a844f451 | 23 | #include "xfs_inum.h" |
1da177e4 LT |
24 | #include "xfs_trans.h" |
25 | #include "xfs_sb.h" | |
26 | #include "xfs_ag.h" | |
1da177e4 | 27 | #include "xfs_mount.h" |
1da177e4 | 28 | #include "xfs_bmap_btree.h" |
a844f451 | 29 | #include "xfs_alloc_btree.h" |
1da177e4 | 30 | #include "xfs_ialloc_btree.h" |
1da177e4 LT |
31 | #include "xfs_dinode.h" |
32 | #include "xfs_inode.h" | |
38bb7423 | 33 | #include "xfs_inode_item.h" |
a844f451 | 34 | #include "xfs_btree.h" |
637aa50f | 35 | #include "xfs_btree_trace.h" |
1da177e4 | 36 | #include "xfs_error.h" |
0b1b213f | 37 | #include "xfs_trace.h" |
1da177e4 LT |
38 | |
39 | /* | |
40 | * Cursor allocation zone. | |
41 | */ | |
42 | kmem_zone_t *xfs_btree_cur_zone; | |
43 | ||
44 | /* | |
45 | * Btree magic numbers. | |
46 | */ | |
cdcf4333 | 47 | const __uint32_t xfs_magics[XFS_BTNUM_MAX] = { |
1da177e4 LT |
48 | XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC |
49 | }; | |
50 | ||
1da177e4 | 51 | |
7cc95a82 | 52 | STATIC int /* error (0 or EFSCORRUPTED) */ |
a23f6ef8 CH |
53 | xfs_btree_check_lblock( |
54 | struct xfs_btree_cur *cur, /* btree cursor */ | |
7cc95a82 | 55 | struct xfs_btree_block *block, /* btree long form block pointer */ |
a23f6ef8 CH |
56 | int level, /* level of the btree block */ |
57 | struct xfs_buf *bp) /* buffer for block, if any */ | |
58 | { | |
59 | int lblock_ok; /* block passes checks */ | |
60 | struct xfs_mount *mp; /* file system mount point */ | |
61 | ||
62 | mp = cur->bc_mp; | |
63 | lblock_ok = | |
64 | be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] && | |
65 | be16_to_cpu(block->bb_level) == level && | |
66 | be16_to_cpu(block->bb_numrecs) <= | |
ce5e42db | 67 | cur->bc_ops->get_maxrecs(cur, level) && |
7cc95a82 CH |
68 | block->bb_u.l.bb_leftsib && |
69 | (be64_to_cpu(block->bb_u.l.bb_leftsib) == NULLDFSBNO || | |
70 | XFS_FSB_SANITY_CHECK(mp, | |
71 | be64_to_cpu(block->bb_u.l.bb_leftsib))) && | |
72 | block->bb_u.l.bb_rightsib && | |
73 | (be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO || | |
74 | XFS_FSB_SANITY_CHECK(mp, | |
75 | be64_to_cpu(block->bb_u.l.bb_rightsib))); | |
a23f6ef8 CH |
76 | if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp, |
77 | XFS_ERRTAG_BTREE_CHECK_LBLOCK, | |
78 | XFS_RANDOM_BTREE_CHECK_LBLOCK))) { | |
79 | if (bp) | |
0b1b213f | 80 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
a23f6ef8 CH |
81 | XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW, |
82 | mp); | |
83 | return XFS_ERROR(EFSCORRUPTED); | |
84 | } | |
85 | return 0; | |
86 | } | |
87 | ||
3cc7524c | 88 | STATIC int /* error (0 or EFSCORRUPTED) */ |
1da177e4 | 89 | xfs_btree_check_sblock( |
a23f6ef8 | 90 | struct xfs_btree_cur *cur, /* btree cursor */ |
7cc95a82 | 91 | struct xfs_btree_block *block, /* btree short form block pointer */ |
1da177e4 | 92 | int level, /* level of the btree block */ |
a23f6ef8 | 93 | struct xfs_buf *bp) /* buffer containing block */ |
1da177e4 | 94 | { |
a23f6ef8 CH |
95 | struct xfs_buf *agbp; /* buffer for ag. freespace struct */ |
96 | struct xfs_agf *agf; /* ag. freespace structure */ | |
1da177e4 LT |
97 | xfs_agblock_t agflen; /* native ag. freespace length */ |
98 | int sblock_ok; /* block passes checks */ | |
99 | ||
100 | agbp = cur->bc_private.a.agbp; | |
101 | agf = XFS_BUF_TO_AGF(agbp); | |
16259e7d | 102 | agflen = be32_to_cpu(agf->agf_length); |
1da177e4 | 103 | sblock_ok = |
16259e7d CH |
104 | be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] && |
105 | be16_to_cpu(block->bb_level) == level && | |
106 | be16_to_cpu(block->bb_numrecs) <= | |
ce5e42db | 107 | cur->bc_ops->get_maxrecs(cur, level) && |
7cc95a82 CH |
108 | (be32_to_cpu(block->bb_u.s.bb_leftsib) == NULLAGBLOCK || |
109 | be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) && | |
110 | block->bb_u.s.bb_leftsib && | |
111 | (be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK || | |
112 | be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) && | |
113 | block->bb_u.s.bb_rightsib; | |
1da177e4 LT |
114 | if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp, |
115 | XFS_ERRTAG_BTREE_CHECK_SBLOCK, | |
116 | XFS_RANDOM_BTREE_CHECK_SBLOCK))) { | |
117 | if (bp) | |
0b1b213f | 118 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
e0c222c4 ES |
119 | XFS_CORRUPTION_ERROR("xfs_btree_check_sblock", |
120 | XFS_ERRLEVEL_LOW, cur->bc_mp, block); | |
1da177e4 LT |
121 | return XFS_ERROR(EFSCORRUPTED); |
122 | } | |
123 | return 0; | |
124 | } | |
125 | ||
126 | /* | |
a23f6ef8 CH |
127 | * Debug routine: check that block header is ok. |
128 | */ | |
129 | int | |
130 | xfs_btree_check_block( | |
131 | struct xfs_btree_cur *cur, /* btree cursor */ | |
132 | struct xfs_btree_block *block, /* generic btree block pointer */ | |
133 | int level, /* level of the btree block */ | |
134 | struct xfs_buf *bp) /* buffer containing block, if any */ | |
135 | { | |
7cc95a82 CH |
136 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
137 | return xfs_btree_check_lblock(cur, block, level, bp); | |
138 | else | |
139 | return xfs_btree_check_sblock(cur, block, level, bp); | |
a23f6ef8 CH |
140 | } |
141 | ||
142 | /* | |
143 | * Check that (long) pointer is ok. | |
144 | */ | |
145 | int /* error (0 or EFSCORRUPTED) */ | |
146 | xfs_btree_check_lptr( | |
147 | struct xfs_btree_cur *cur, /* btree cursor */ | |
148 | xfs_dfsbno_t bno, /* btree block disk address */ | |
149 | int level) /* btree block level */ | |
150 | { | |
151 | XFS_WANT_CORRUPTED_RETURN( | |
152 | level > 0 && | |
153 | bno != NULLDFSBNO && | |
154 | XFS_FSB_SANITY_CHECK(cur->bc_mp, bno)); | |
155 | return 0; | |
156 | } | |
157 | ||
24ee0e49 | 158 | #ifdef DEBUG |
a23f6ef8 CH |
159 | /* |
160 | * Check that (short) pointer is ok. | |
1da177e4 | 161 | */ |
3cc7524c | 162 | STATIC int /* error (0 or EFSCORRUPTED) */ |
1da177e4 | 163 | xfs_btree_check_sptr( |
a23f6ef8 CH |
164 | struct xfs_btree_cur *cur, /* btree cursor */ |
165 | xfs_agblock_t bno, /* btree block disk address */ | |
166 | int level) /* btree block level */ | |
1da177e4 | 167 | { |
a23f6ef8 | 168 | xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks; |
1da177e4 | 169 | |
1da177e4 LT |
170 | XFS_WANT_CORRUPTED_RETURN( |
171 | level > 0 && | |
a23f6ef8 CH |
172 | bno != NULLAGBLOCK && |
173 | bno != 0 && | |
174 | bno < agblocks); | |
1da177e4 LT |
175 | return 0; |
176 | } | |
177 | ||
a23f6ef8 CH |
178 | /* |
179 | * Check that block ptr is ok. | |
180 | */ | |
3cc7524c | 181 | STATIC int /* error (0 or EFSCORRUPTED) */ |
a23f6ef8 CH |
182 | xfs_btree_check_ptr( |
183 | struct xfs_btree_cur *cur, /* btree cursor */ | |
184 | union xfs_btree_ptr *ptr, /* btree block disk address */ | |
185 | int index, /* offset from ptr to check */ | |
186 | int level) /* btree block level */ | |
187 | { | |
188 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { | |
189 | return xfs_btree_check_lptr(cur, | |
190 | be64_to_cpu((&ptr->l)[index]), level); | |
191 | } else { | |
192 | return xfs_btree_check_sptr(cur, | |
193 | be32_to_cpu((&ptr->s)[index]), level); | |
194 | } | |
195 | } | |
24ee0e49 | 196 | #endif |
a23f6ef8 | 197 | |
1da177e4 LT |
198 | /* |
199 | * Delete the btree cursor. | |
200 | */ | |
201 | void | |
202 | xfs_btree_del_cursor( | |
203 | xfs_btree_cur_t *cur, /* btree cursor */ | |
204 | int error) /* del because of error */ | |
205 | { | |
206 | int i; /* btree level */ | |
207 | ||
208 | /* | |
209 | * Clear the buffer pointers, and release the buffers. | |
210 | * If we're doing this in the face of an error, we | |
211 | * need to make sure to inspect all of the entries | |
212 | * in the bc_bufs array for buffers to be unlocked. | |
213 | * This is because some of the btree code works from | |
214 | * level n down to 0, and if we get an error along | |
215 | * the way we won't have initialized all the entries | |
216 | * down to 0. | |
217 | */ | |
218 | for (i = 0; i < cur->bc_nlevels; i++) { | |
219 | if (cur->bc_bufs[i]) | |
c0e59e1a | 220 | xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]); |
1da177e4 LT |
221 | else if (!error) |
222 | break; | |
223 | } | |
224 | /* | |
225 | * Can't free a bmap cursor without having dealt with the | |
226 | * allocated indirect blocks' accounting. | |
227 | */ | |
228 | ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP || | |
229 | cur->bc_private.b.allocated == 0); | |
230 | /* | |
231 | * Free the cursor. | |
232 | */ | |
233 | kmem_zone_free(xfs_btree_cur_zone, cur); | |
234 | } | |
235 | ||
236 | /* | |
237 | * Duplicate the btree cursor. | |
238 | * Allocate a new one, copy the record, re-get the buffers. | |
239 | */ | |
240 | int /* error */ | |
241 | xfs_btree_dup_cursor( | |
242 | xfs_btree_cur_t *cur, /* input cursor */ | |
243 | xfs_btree_cur_t **ncur) /* output cursor */ | |
244 | { | |
245 | xfs_buf_t *bp; /* btree block's buffer pointer */ | |
246 | int error; /* error return value */ | |
247 | int i; /* level number of btree block */ | |
248 | xfs_mount_t *mp; /* mount structure for filesystem */ | |
249 | xfs_btree_cur_t *new; /* new cursor value */ | |
250 | xfs_trans_t *tp; /* transaction pointer, can be NULL */ | |
251 | ||
252 | tp = cur->bc_tp; | |
253 | mp = cur->bc_mp; | |
561f7d17 | 254 | |
1da177e4 LT |
255 | /* |
256 | * Allocate a new cursor like the old one. | |
257 | */ | |
561f7d17 CH |
258 | new = cur->bc_ops->dup_cursor(cur); |
259 | ||
1da177e4 LT |
260 | /* |
261 | * Copy the record currently in the cursor. | |
262 | */ | |
263 | new->bc_rec = cur->bc_rec; | |
561f7d17 | 264 | |
1da177e4 LT |
265 | /* |
266 | * For each level current, re-get the buffer and copy the ptr value. | |
267 | */ | |
268 | for (i = 0; i < new->bc_nlevels; i++) { | |
269 | new->bc_ptrs[i] = cur->bc_ptrs[i]; | |
270 | new->bc_ra[i] = cur->bc_ra[i]; | |
271 | if ((bp = cur->bc_bufs[i])) { | |
272 | if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, | |
273 | XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) { | |
274 | xfs_btree_del_cursor(new, error); | |
275 | *ncur = NULL; | |
276 | return error; | |
277 | } | |
278 | new->bc_bufs[i] = bp; | |
279 | ASSERT(bp); | |
280 | ASSERT(!XFS_BUF_GETERROR(bp)); | |
281 | } else | |
282 | new->bc_bufs[i] = NULL; | |
283 | } | |
1da177e4 LT |
284 | *ncur = new; |
285 | return 0; | |
286 | } | |
287 | ||
65f1eaea CH |
288 | /* |
289 | * XFS btree block layout and addressing: | |
290 | * | |
291 | * There are two types of blocks in the btree: leaf and non-leaf blocks. | |
292 | * | |
293 | * The leaf record start with a header then followed by records containing | |
294 | * the values. A non-leaf block also starts with the same header, and | |
295 | * then first contains lookup keys followed by an equal number of pointers | |
296 | * to the btree blocks at the previous level. | |
297 | * | |
298 | * +--------+-------+-------+-------+-------+-------+-------+ | |
299 | * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N | | |
300 | * +--------+-------+-------+-------+-------+-------+-------+ | |
301 | * | |
302 | * +--------+-------+-------+-------+-------+-------+-------+ | |
303 | * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N | | |
304 | * +--------+-------+-------+-------+-------+-------+-------+ | |
305 | * | |
306 | * The header is called struct xfs_btree_block for reasons better left unknown | |
307 | * and comes in different versions for short (32bit) and long (64bit) block | |
308 | * pointers. The record and key structures are defined by the btree instances | |
309 | * and opaque to the btree core. The block pointers are simple disk endian | |
310 | * integers, available in a short (32bit) and long (64bit) variant. | |
311 | * | |
312 | * The helpers below calculate the offset of a given record, key or pointer | |
313 | * into a btree block (xfs_btree_*_offset) or return a pointer to the given | |
314 | * record, key or pointer (xfs_btree_*_addr). Note that all addressing | |
315 | * inside the btree block is done using indices starting at one, not zero! | |
316 | */ | |
317 | ||
318 | /* | |
319 | * Return size of the btree block header for this btree instance. | |
320 | */ | |
321 | static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur) | |
322 | { | |
323 | return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ? | |
7cc95a82 CH |
324 | XFS_BTREE_LBLOCK_LEN : |
325 | XFS_BTREE_SBLOCK_LEN; | |
65f1eaea CH |
326 | } |
327 | ||
328 | /* | |
329 | * Return size of btree block pointers for this btree instance. | |
330 | */ | |
331 | static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur) | |
332 | { | |
333 | return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ? | |
334 | sizeof(__be64) : sizeof(__be32); | |
335 | } | |
336 | ||
337 | /* | |
338 | * Calculate offset of the n-th record in a btree block. | |
339 | */ | |
340 | STATIC size_t | |
341 | xfs_btree_rec_offset( | |
342 | struct xfs_btree_cur *cur, | |
343 | int n) | |
344 | { | |
345 | return xfs_btree_block_len(cur) + | |
346 | (n - 1) * cur->bc_ops->rec_len; | |
347 | } | |
348 | ||
349 | /* | |
350 | * Calculate offset of the n-th key in a btree block. | |
351 | */ | |
352 | STATIC size_t | |
353 | xfs_btree_key_offset( | |
354 | struct xfs_btree_cur *cur, | |
355 | int n) | |
356 | { | |
357 | return xfs_btree_block_len(cur) + | |
358 | (n - 1) * cur->bc_ops->key_len; | |
359 | } | |
360 | ||
361 | /* | |
362 | * Calculate offset of the n-th block pointer in a btree block. | |
363 | */ | |
364 | STATIC size_t | |
365 | xfs_btree_ptr_offset( | |
366 | struct xfs_btree_cur *cur, | |
367 | int n, | |
368 | int level) | |
369 | { | |
370 | return xfs_btree_block_len(cur) + | |
371 | cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len + | |
372 | (n - 1) * xfs_btree_ptr_len(cur); | |
373 | } | |
374 | ||
375 | /* | |
376 | * Return a pointer to the n-th record in the btree block. | |
377 | */ | |
378 | STATIC union xfs_btree_rec * | |
379 | xfs_btree_rec_addr( | |
380 | struct xfs_btree_cur *cur, | |
381 | int n, | |
382 | struct xfs_btree_block *block) | |
383 | { | |
384 | return (union xfs_btree_rec *) | |
385 | ((char *)block + xfs_btree_rec_offset(cur, n)); | |
386 | } | |
387 | ||
388 | /* | |
389 | * Return a pointer to the n-th key in the btree block. | |
390 | */ | |
391 | STATIC union xfs_btree_key * | |
392 | xfs_btree_key_addr( | |
393 | struct xfs_btree_cur *cur, | |
394 | int n, | |
395 | struct xfs_btree_block *block) | |
396 | { | |
397 | return (union xfs_btree_key *) | |
398 | ((char *)block + xfs_btree_key_offset(cur, n)); | |
399 | } | |
400 | ||
401 | /* | |
402 | * Return a pointer to the n-th block pointer in the btree block. | |
403 | */ | |
404 | STATIC union xfs_btree_ptr * | |
405 | xfs_btree_ptr_addr( | |
406 | struct xfs_btree_cur *cur, | |
407 | int n, | |
408 | struct xfs_btree_block *block) | |
409 | { | |
410 | int level = xfs_btree_get_level(block); | |
411 | ||
412 | ASSERT(block->bb_level != 0); | |
413 | ||
414 | return (union xfs_btree_ptr *) | |
415 | ((char *)block + xfs_btree_ptr_offset(cur, n, level)); | |
416 | } | |
417 | ||
8186e517 CH |
418 | /* |
419 | * Get a the root block which is stored in the inode. | |
420 | * | |
421 | * For now this btree implementation assumes the btree root is always | |
422 | * stored in the if_broot field of an inode fork. | |
423 | */ | |
424 | STATIC struct xfs_btree_block * | |
425 | xfs_btree_get_iroot( | |
426 | struct xfs_btree_cur *cur) | |
427 | { | |
428 | struct xfs_ifork *ifp; | |
429 | ||
430 | ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork); | |
431 | return (struct xfs_btree_block *)ifp->if_broot; | |
432 | } | |
433 | ||
1da177e4 LT |
434 | /* |
435 | * Retrieve the block pointer from the cursor at the given level. | |
8186e517 | 436 | * This may be an inode btree root or from a buffer. |
1da177e4 | 437 | */ |
8186e517 | 438 | STATIC struct xfs_btree_block * /* generic btree block pointer */ |
1da177e4 | 439 | xfs_btree_get_block( |
8186e517 | 440 | struct xfs_btree_cur *cur, /* btree cursor */ |
1da177e4 | 441 | int level, /* level in btree */ |
8186e517 | 442 | struct xfs_buf **bpp) /* buffer containing the block */ |
1da177e4 | 443 | { |
8186e517 CH |
444 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
445 | (level == cur->bc_nlevels - 1)) { | |
446 | *bpp = NULL; | |
447 | return xfs_btree_get_iroot(cur); | |
1da177e4 | 448 | } |
8186e517 CH |
449 | |
450 | *bpp = cur->bc_bufs[level]; | |
451 | return XFS_BUF_TO_BLOCK(*bpp); | |
1da177e4 LT |
452 | } |
453 | ||
454 | /* | |
455 | * Get a buffer for the block, return it with no data read. | |
456 | * Long-form addressing. | |
457 | */ | |
458 | xfs_buf_t * /* buffer for fsbno */ | |
459 | xfs_btree_get_bufl( | |
460 | xfs_mount_t *mp, /* file system mount point */ | |
461 | xfs_trans_t *tp, /* transaction pointer */ | |
462 | xfs_fsblock_t fsbno, /* file system block number */ | |
463 | uint lock) /* lock flags for get_buf */ | |
464 | { | |
465 | xfs_buf_t *bp; /* buffer pointer (return value) */ | |
466 | xfs_daddr_t d; /* real disk block address */ | |
467 | ||
468 | ASSERT(fsbno != NULLFSBLOCK); | |
469 | d = XFS_FSB_TO_DADDR(mp, fsbno); | |
470 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock); | |
471 | ASSERT(bp); | |
472 | ASSERT(!XFS_BUF_GETERROR(bp)); | |
473 | return bp; | |
474 | } | |
475 | ||
476 | /* | |
477 | * Get a buffer for the block, return it with no data read. | |
478 | * Short-form addressing. | |
479 | */ | |
480 | xfs_buf_t * /* buffer for agno/agbno */ | |
481 | xfs_btree_get_bufs( | |
482 | xfs_mount_t *mp, /* file system mount point */ | |
483 | xfs_trans_t *tp, /* transaction pointer */ | |
484 | xfs_agnumber_t agno, /* allocation group number */ | |
485 | xfs_agblock_t agbno, /* allocation group block number */ | |
486 | uint lock) /* lock flags for get_buf */ | |
487 | { | |
488 | xfs_buf_t *bp; /* buffer pointer (return value) */ | |
489 | xfs_daddr_t d; /* real disk block address */ | |
490 | ||
491 | ASSERT(agno != NULLAGNUMBER); | |
492 | ASSERT(agbno != NULLAGBLOCK); | |
493 | d = XFS_AGB_TO_DADDR(mp, agno, agbno); | |
494 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock); | |
495 | ASSERT(bp); | |
496 | ASSERT(!XFS_BUF_GETERROR(bp)); | |
497 | return bp; | |
498 | } | |
499 | ||
1da177e4 LT |
500 | /* |
501 | * Check for the cursor referring to the last block at the given level. | |
502 | */ | |
503 | int /* 1=is last block, 0=not last block */ | |
504 | xfs_btree_islastblock( | |
505 | xfs_btree_cur_t *cur, /* btree cursor */ | |
506 | int level) /* level to check */ | |
507 | { | |
7cc95a82 | 508 | struct xfs_btree_block *block; /* generic btree block pointer */ |
1da177e4 LT |
509 | xfs_buf_t *bp; /* buffer containing block */ |
510 | ||
511 | block = xfs_btree_get_block(cur, level, &bp); | |
512 | xfs_btree_check_block(cur, block, level, bp); | |
e99ab90d | 513 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
16259e7d | 514 | return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO; |
1da177e4 | 515 | else |
16259e7d | 516 | return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK; |
1da177e4 LT |
517 | } |
518 | ||
cdcf4333 CH |
519 | /* |
520 | * Change the cursor to point to the first record at the given level. | |
521 | * Other levels are unaffected. | |
522 | */ | |
3cc7524c | 523 | STATIC int /* success=1, failure=0 */ |
cdcf4333 CH |
524 | xfs_btree_firstrec( |
525 | xfs_btree_cur_t *cur, /* btree cursor */ | |
526 | int level) /* level to change */ | |
527 | { | |
7cc95a82 | 528 | struct xfs_btree_block *block; /* generic btree block pointer */ |
cdcf4333 CH |
529 | xfs_buf_t *bp; /* buffer containing block */ |
530 | ||
531 | /* | |
532 | * Get the block pointer for this level. | |
533 | */ | |
534 | block = xfs_btree_get_block(cur, level, &bp); | |
535 | xfs_btree_check_block(cur, block, level, bp); | |
536 | /* | |
537 | * It's empty, there is no such record. | |
538 | */ | |
f2277f06 | 539 | if (!block->bb_numrecs) |
cdcf4333 CH |
540 | return 0; |
541 | /* | |
542 | * Set the ptr value to 1, that's the first record/key. | |
543 | */ | |
544 | cur->bc_ptrs[level] = 1; | |
545 | return 1; | |
546 | } | |
547 | ||
1da177e4 LT |
548 | /* |
549 | * Change the cursor to point to the last record in the current block | |
550 | * at the given level. Other levels are unaffected. | |
551 | */ | |
3cc7524c | 552 | STATIC int /* success=1, failure=0 */ |
1da177e4 LT |
553 | xfs_btree_lastrec( |
554 | xfs_btree_cur_t *cur, /* btree cursor */ | |
555 | int level) /* level to change */ | |
556 | { | |
7cc95a82 | 557 | struct xfs_btree_block *block; /* generic btree block pointer */ |
1da177e4 LT |
558 | xfs_buf_t *bp; /* buffer containing block */ |
559 | ||
560 | /* | |
561 | * Get the block pointer for this level. | |
562 | */ | |
563 | block = xfs_btree_get_block(cur, level, &bp); | |
564 | xfs_btree_check_block(cur, block, level, bp); | |
565 | /* | |
566 | * It's empty, there is no such record. | |
567 | */ | |
f2277f06 | 568 | if (!block->bb_numrecs) |
1da177e4 LT |
569 | return 0; |
570 | /* | |
571 | * Set the ptr value to numrecs, that's the last record/key. | |
572 | */ | |
f2277f06 | 573 | cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs); |
1da177e4 LT |
574 | return 1; |
575 | } | |
576 | ||
577 | /* | |
578 | * Compute first and last byte offsets for the fields given. | |
579 | * Interprets the offsets table, which contains struct field offsets. | |
580 | */ | |
581 | void | |
582 | xfs_btree_offsets( | |
583 | __int64_t fields, /* bitmask of fields */ | |
584 | const short *offsets, /* table of field offsets */ | |
585 | int nbits, /* number of bits to inspect */ | |
586 | int *first, /* output: first byte offset */ | |
587 | int *last) /* output: last byte offset */ | |
588 | { | |
589 | int i; /* current bit number */ | |
590 | __int64_t imask; /* mask for current bit number */ | |
591 | ||
592 | ASSERT(fields != 0); | |
593 | /* | |
594 | * Find the lowest bit, so the first byte offset. | |
595 | */ | |
596 | for (i = 0, imask = 1LL; ; i++, imask <<= 1) { | |
597 | if (imask & fields) { | |
598 | *first = offsets[i]; | |
599 | break; | |
600 | } | |
601 | } | |
602 | /* | |
603 | * Find the highest bit, so the last byte offset. | |
604 | */ | |
605 | for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) { | |
606 | if (imask & fields) { | |
607 | *last = offsets[i + 1] - 1; | |
608 | break; | |
609 | } | |
610 | } | |
611 | } | |
612 | ||
613 | /* | |
614 | * Get a buffer for the block, return it read in. | |
615 | * Long-form addressing. | |
616 | */ | |
617 | int /* error */ | |
618 | xfs_btree_read_bufl( | |
619 | xfs_mount_t *mp, /* file system mount point */ | |
620 | xfs_trans_t *tp, /* transaction pointer */ | |
621 | xfs_fsblock_t fsbno, /* file system block number */ | |
622 | uint lock, /* lock flags for read_buf */ | |
623 | xfs_buf_t **bpp, /* buffer for fsbno */ | |
624 | int refval) /* ref count value for buffer */ | |
625 | { | |
626 | xfs_buf_t *bp; /* return value */ | |
627 | xfs_daddr_t d; /* real disk block address */ | |
628 | int error; | |
629 | ||
630 | ASSERT(fsbno != NULLFSBLOCK); | |
631 | d = XFS_FSB_TO_DADDR(mp, fsbno); | |
632 | if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d, | |
633 | mp->m_bsize, lock, &bp))) { | |
634 | return error; | |
635 | } | |
636 | ASSERT(!bp || !XFS_BUF_GETERROR(bp)); | |
637 | if (bp != NULL) { | |
638 | XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval); | |
639 | } | |
640 | *bpp = bp; | |
641 | return 0; | |
642 | } | |
643 | ||
1da177e4 LT |
644 | /* |
645 | * Read-ahead the block, don't wait for it, don't return a buffer. | |
646 | * Long-form addressing. | |
647 | */ | |
648 | /* ARGSUSED */ | |
649 | void | |
650 | xfs_btree_reada_bufl( | |
651 | xfs_mount_t *mp, /* file system mount point */ | |
652 | xfs_fsblock_t fsbno, /* file system block number */ | |
653 | xfs_extlen_t count) /* count of filesystem blocks */ | |
654 | { | |
655 | xfs_daddr_t d; | |
656 | ||
657 | ASSERT(fsbno != NULLFSBLOCK); | |
658 | d = XFS_FSB_TO_DADDR(mp, fsbno); | |
1a1a3e97 | 659 | xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count); |
1da177e4 LT |
660 | } |
661 | ||
662 | /* | |
663 | * Read-ahead the block, don't wait for it, don't return a buffer. | |
664 | * Short-form addressing. | |
665 | */ | |
666 | /* ARGSUSED */ | |
667 | void | |
668 | xfs_btree_reada_bufs( | |
669 | xfs_mount_t *mp, /* file system mount point */ | |
670 | xfs_agnumber_t agno, /* allocation group number */ | |
671 | xfs_agblock_t agbno, /* allocation group block number */ | |
672 | xfs_extlen_t count) /* count of filesystem blocks */ | |
673 | { | |
674 | xfs_daddr_t d; | |
675 | ||
676 | ASSERT(agno != NULLAGNUMBER); | |
677 | ASSERT(agbno != NULLAGBLOCK); | |
678 | d = XFS_AGB_TO_DADDR(mp, agno, agbno); | |
1a1a3e97 | 679 | xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count); |
1da177e4 LT |
680 | } |
681 | ||
b524bfee CH |
682 | STATIC int |
683 | xfs_btree_readahead_lblock( | |
684 | struct xfs_btree_cur *cur, | |
685 | int lr, | |
686 | struct xfs_btree_block *block) | |
687 | { | |
688 | int rval = 0; | |
e6edbd1c CH |
689 | xfs_dfsbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib); |
690 | xfs_dfsbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib); | |
b524bfee CH |
691 | |
692 | if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) { | |
693 | xfs_btree_reada_bufl(cur->bc_mp, left, 1); | |
694 | rval++; | |
695 | } | |
696 | ||
697 | if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) { | |
698 | xfs_btree_reada_bufl(cur->bc_mp, right, 1); | |
699 | rval++; | |
700 | } | |
701 | ||
702 | return rval; | |
703 | } | |
704 | ||
705 | STATIC int | |
706 | xfs_btree_readahead_sblock( | |
707 | struct xfs_btree_cur *cur, | |
708 | int lr, | |
709 | struct xfs_btree_block *block) | |
710 | { | |
711 | int rval = 0; | |
712 | xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib); | |
713 | xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib); | |
714 | ||
715 | ||
716 | if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) { | |
717 | xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno, | |
718 | left, 1); | |
719 | rval++; | |
720 | } | |
721 | ||
722 | if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) { | |
723 | xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno, | |
724 | right, 1); | |
725 | rval++; | |
726 | } | |
727 | ||
728 | return rval; | |
729 | } | |
730 | ||
1da177e4 LT |
731 | /* |
732 | * Read-ahead btree blocks, at the given level. | |
733 | * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA. | |
734 | */ | |
3cc7524c | 735 | STATIC int |
b524bfee CH |
736 | xfs_btree_readahead( |
737 | struct xfs_btree_cur *cur, /* btree cursor */ | |
1da177e4 LT |
738 | int lev, /* level in btree */ |
739 | int lr) /* left/right bits */ | |
740 | { | |
b524bfee CH |
741 | struct xfs_btree_block *block; |
742 | ||
743 | /* | |
744 | * No readahead needed if we are at the root level and the | |
745 | * btree root is stored in the inode. | |
746 | */ | |
747 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && | |
748 | (lev == cur->bc_nlevels - 1)) | |
749 | return 0; | |
750 | ||
751 | if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev]) | |
752 | return 0; | |
1da177e4 | 753 | |
1da177e4 | 754 | cur->bc_ra[lev] |= lr; |
b524bfee CH |
755 | block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]); |
756 | ||
757 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) | |
758 | return xfs_btree_readahead_lblock(cur, lr, block); | |
759 | return xfs_btree_readahead_sblock(cur, lr, block); | |
1da177e4 LT |
760 | } |
761 | ||
762 | /* | |
763 | * Set the buffer for level "lev" in the cursor to bp, releasing | |
764 | * any previous buffer. | |
765 | */ | |
c0e59e1a | 766 | STATIC void |
1da177e4 LT |
767 | xfs_btree_setbuf( |
768 | xfs_btree_cur_t *cur, /* btree cursor */ | |
769 | int lev, /* level in btree */ | |
770 | xfs_buf_t *bp) /* new buffer to set */ | |
771 | { | |
7cc95a82 | 772 | struct xfs_btree_block *b; /* btree block */ |
1da177e4 | 773 | |
c0e59e1a CH |
774 | if (cur->bc_bufs[lev]) |
775 | xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]); | |
1da177e4 LT |
776 | cur->bc_bufs[lev] = bp; |
777 | cur->bc_ra[lev] = 0; | |
c0e59e1a | 778 | |
1da177e4 | 779 | b = XFS_BUF_TO_BLOCK(bp); |
e99ab90d | 780 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { |
16259e7d | 781 | if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO) |
1da177e4 | 782 | cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA; |
16259e7d | 783 | if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO) |
1da177e4 LT |
784 | cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA; |
785 | } else { | |
16259e7d | 786 | if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK) |
1da177e4 | 787 | cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA; |
16259e7d | 788 | if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK) |
1da177e4 LT |
789 | cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA; |
790 | } | |
791 | } | |
637aa50f CH |
792 | |
793 | STATIC int | |
794 | xfs_btree_ptr_is_null( | |
795 | struct xfs_btree_cur *cur, | |
796 | union xfs_btree_ptr *ptr) | |
797 | { | |
798 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) | |
33ad965d | 799 | return be64_to_cpu(ptr->l) == NULLDFSBNO; |
637aa50f CH |
800 | else |
801 | return be32_to_cpu(ptr->s) == NULLAGBLOCK; | |
802 | } | |
803 | ||
4b22a571 CH |
804 | STATIC void |
805 | xfs_btree_set_ptr_null( | |
806 | struct xfs_btree_cur *cur, | |
807 | union xfs_btree_ptr *ptr) | |
808 | { | |
809 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) | |
33ad965d | 810 | ptr->l = cpu_to_be64(NULLDFSBNO); |
4b22a571 CH |
811 | else |
812 | ptr->s = cpu_to_be32(NULLAGBLOCK); | |
813 | } | |
814 | ||
637aa50f CH |
815 | /* |
816 | * Get/set/init sibling pointers | |
817 | */ | |
818 | STATIC void | |
819 | xfs_btree_get_sibling( | |
820 | struct xfs_btree_cur *cur, | |
821 | struct xfs_btree_block *block, | |
822 | union xfs_btree_ptr *ptr, | |
823 | int lr) | |
824 | { | |
825 | ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB); | |
826 | ||
827 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { | |
828 | if (lr == XFS_BB_RIGHTSIB) | |
829 | ptr->l = block->bb_u.l.bb_rightsib; | |
830 | else | |
831 | ptr->l = block->bb_u.l.bb_leftsib; | |
832 | } else { | |
833 | if (lr == XFS_BB_RIGHTSIB) | |
834 | ptr->s = block->bb_u.s.bb_rightsib; | |
835 | else | |
836 | ptr->s = block->bb_u.s.bb_leftsib; | |
837 | } | |
838 | } | |
839 | ||
f5eb8e7c CH |
840 | STATIC void |
841 | xfs_btree_set_sibling( | |
842 | struct xfs_btree_cur *cur, | |
843 | struct xfs_btree_block *block, | |
844 | union xfs_btree_ptr *ptr, | |
845 | int lr) | |
846 | { | |
847 | ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB); | |
848 | ||
849 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { | |
850 | if (lr == XFS_BB_RIGHTSIB) | |
851 | block->bb_u.l.bb_rightsib = ptr->l; | |
852 | else | |
853 | block->bb_u.l.bb_leftsib = ptr->l; | |
854 | } else { | |
855 | if (lr == XFS_BB_RIGHTSIB) | |
856 | block->bb_u.s.bb_rightsib = ptr->s; | |
857 | else | |
858 | block->bb_u.s.bb_leftsib = ptr->s; | |
859 | } | |
860 | } | |
861 | ||
862 | STATIC void | |
863 | xfs_btree_init_block( | |
864 | struct xfs_btree_cur *cur, | |
865 | int level, | |
866 | int numrecs, | |
867 | struct xfs_btree_block *new) /* new block */ | |
868 | { | |
869 | new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]); | |
870 | new->bb_level = cpu_to_be16(level); | |
871 | new->bb_numrecs = cpu_to_be16(numrecs); | |
872 | ||
873 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { | |
33ad965d DC |
874 | new->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
875 | new->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); | |
f5eb8e7c CH |
876 | } else { |
877 | new->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK); | |
878 | new->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK); | |
879 | } | |
880 | } | |
881 | ||
278d0ca1 CH |
882 | /* |
883 | * Return true if ptr is the last record in the btree and | |
884 | * we need to track updateѕ to this record. The decision | |
885 | * will be further refined in the update_lastrec method. | |
886 | */ | |
887 | STATIC int | |
888 | xfs_btree_is_lastrec( | |
889 | struct xfs_btree_cur *cur, | |
890 | struct xfs_btree_block *block, | |
891 | int level) | |
892 | { | |
893 | union xfs_btree_ptr ptr; | |
894 | ||
895 | if (level > 0) | |
896 | return 0; | |
897 | if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE)) | |
898 | return 0; | |
899 | ||
900 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB); | |
901 | if (!xfs_btree_ptr_is_null(cur, &ptr)) | |
902 | return 0; | |
903 | return 1; | |
904 | } | |
905 | ||
f5eb8e7c CH |
906 | STATIC void |
907 | xfs_btree_buf_to_ptr( | |
908 | struct xfs_btree_cur *cur, | |
909 | struct xfs_buf *bp, | |
910 | union xfs_btree_ptr *ptr) | |
911 | { | |
912 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) | |
913 | ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp, | |
914 | XFS_BUF_ADDR(bp))); | |
915 | else { | |
9d87c319 | 916 | ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp, |
f5eb8e7c CH |
917 | XFS_BUF_ADDR(bp))); |
918 | } | |
919 | } | |
920 | ||
637aa50f CH |
921 | STATIC xfs_daddr_t |
922 | xfs_btree_ptr_to_daddr( | |
923 | struct xfs_btree_cur *cur, | |
924 | union xfs_btree_ptr *ptr) | |
925 | { | |
926 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { | |
33ad965d | 927 | ASSERT(be64_to_cpu(ptr->l) != NULLDFSBNO); |
637aa50f CH |
928 | |
929 | return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l)); | |
930 | } else { | |
931 | ASSERT(cur->bc_private.a.agno != NULLAGNUMBER); | |
932 | ASSERT(be32_to_cpu(ptr->s) != NULLAGBLOCK); | |
933 | ||
934 | return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno, | |
935 | be32_to_cpu(ptr->s)); | |
936 | } | |
937 | } | |
938 | ||
939 | STATIC void | |
940 | xfs_btree_set_refs( | |
941 | struct xfs_btree_cur *cur, | |
942 | struct xfs_buf *bp) | |
943 | { | |
944 | switch (cur->bc_btnum) { | |
945 | case XFS_BTNUM_BNO: | |
946 | case XFS_BTNUM_CNT: | |
947 | XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_ALLOC_BTREE_REF); | |
948 | break; | |
949 | case XFS_BTNUM_INO: | |
950 | XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_INOMAP, XFS_INO_BTREE_REF); | |
951 | break; | |
952 | case XFS_BTNUM_BMAP: | |
953 | XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_BMAP_BTREE_REF); | |
954 | break; | |
955 | default: | |
956 | ASSERT(0); | |
957 | } | |
958 | } | |
959 | ||
f5eb8e7c CH |
960 | STATIC int |
961 | xfs_btree_get_buf_block( | |
962 | struct xfs_btree_cur *cur, | |
963 | union xfs_btree_ptr *ptr, | |
964 | int flags, | |
965 | struct xfs_btree_block **block, | |
966 | struct xfs_buf **bpp) | |
967 | { | |
968 | struct xfs_mount *mp = cur->bc_mp; | |
969 | xfs_daddr_t d; | |
970 | ||
971 | /* need to sort out how callers deal with failures first */ | |
0cadda1c | 972 | ASSERT(!(flags & XBF_TRYLOCK)); |
f5eb8e7c CH |
973 | |
974 | d = xfs_btree_ptr_to_daddr(cur, ptr); | |
975 | *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d, | |
976 | mp->m_bsize, flags); | |
977 | ||
978 | ASSERT(*bpp); | |
979 | ASSERT(!XFS_BUF_GETERROR(*bpp)); | |
980 | ||
981 | *block = XFS_BUF_TO_BLOCK(*bpp); | |
982 | return 0; | |
983 | } | |
984 | ||
637aa50f CH |
985 | /* |
986 | * Read in the buffer at the given ptr and return the buffer and | |
987 | * the block pointer within the buffer. | |
988 | */ | |
989 | STATIC int | |
990 | xfs_btree_read_buf_block( | |
991 | struct xfs_btree_cur *cur, | |
992 | union xfs_btree_ptr *ptr, | |
993 | int level, | |
994 | int flags, | |
995 | struct xfs_btree_block **block, | |
996 | struct xfs_buf **bpp) | |
997 | { | |
998 | struct xfs_mount *mp = cur->bc_mp; | |
999 | xfs_daddr_t d; | |
1000 | int error; | |
1001 | ||
1002 | /* need to sort out how callers deal with failures first */ | |
0cadda1c | 1003 | ASSERT(!(flags & XBF_TRYLOCK)); |
637aa50f CH |
1004 | |
1005 | d = xfs_btree_ptr_to_daddr(cur, ptr); | |
1006 | error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d, | |
1007 | mp->m_bsize, flags, bpp); | |
1008 | if (error) | |
1009 | return error; | |
1010 | ||
1011 | ASSERT(*bpp != NULL); | |
1012 | ASSERT(!XFS_BUF_GETERROR(*bpp)); | |
1013 | ||
1014 | xfs_btree_set_refs(cur, *bpp); | |
1015 | *block = XFS_BUF_TO_BLOCK(*bpp); | |
1016 | ||
1017 | error = xfs_btree_check_block(cur, *block, level, *bpp); | |
1018 | if (error) | |
1019 | xfs_trans_brelse(cur->bc_tp, *bpp); | |
1020 | return error; | |
1021 | } | |
1022 | ||
38bb7423 CH |
1023 | /* |
1024 | * Copy keys from one btree block to another. | |
1025 | */ | |
1026 | STATIC void | |
1027 | xfs_btree_copy_keys( | |
1028 | struct xfs_btree_cur *cur, | |
1029 | union xfs_btree_key *dst_key, | |
1030 | union xfs_btree_key *src_key, | |
1031 | int numkeys) | |
1032 | { | |
1033 | ASSERT(numkeys >= 0); | |
1034 | memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len); | |
1035 | } | |
1036 | ||
278d0ca1 CH |
1037 | /* |
1038 | * Copy records from one btree block to another. | |
1039 | */ | |
1040 | STATIC void | |
1041 | xfs_btree_copy_recs( | |
1042 | struct xfs_btree_cur *cur, | |
1043 | union xfs_btree_rec *dst_rec, | |
1044 | union xfs_btree_rec *src_rec, | |
1045 | int numrecs) | |
1046 | { | |
1047 | ASSERT(numrecs >= 0); | |
1048 | memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len); | |
1049 | } | |
1050 | ||
9eaead51 CH |
1051 | /* |
1052 | * Copy block pointers from one btree block to another. | |
1053 | */ | |
1054 | STATIC void | |
1055 | xfs_btree_copy_ptrs( | |
1056 | struct xfs_btree_cur *cur, | |
1057 | union xfs_btree_ptr *dst_ptr, | |
1058 | union xfs_btree_ptr *src_ptr, | |
1059 | int numptrs) | |
1060 | { | |
1061 | ASSERT(numptrs >= 0); | |
1062 | memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur)); | |
1063 | } | |
1064 | ||
1065 | /* | |
1066 | * Shift keys one index left/right inside a single btree block. | |
1067 | */ | |
1068 | STATIC void | |
1069 | xfs_btree_shift_keys( | |
1070 | struct xfs_btree_cur *cur, | |
1071 | union xfs_btree_key *key, | |
1072 | int dir, | |
1073 | int numkeys) | |
1074 | { | |
1075 | char *dst_key; | |
1076 | ||
1077 | ASSERT(numkeys >= 0); | |
1078 | ASSERT(dir == 1 || dir == -1); | |
1079 | ||
1080 | dst_key = (char *)key + (dir * cur->bc_ops->key_len); | |
1081 | memmove(dst_key, key, numkeys * cur->bc_ops->key_len); | |
1082 | } | |
1083 | ||
1084 | /* | |
1085 | * Shift records one index left/right inside a single btree block. | |
1086 | */ | |
1087 | STATIC void | |
1088 | xfs_btree_shift_recs( | |
1089 | struct xfs_btree_cur *cur, | |
1090 | union xfs_btree_rec *rec, | |
1091 | int dir, | |
1092 | int numrecs) | |
1093 | { | |
1094 | char *dst_rec; | |
1095 | ||
1096 | ASSERT(numrecs >= 0); | |
1097 | ASSERT(dir == 1 || dir == -1); | |
1098 | ||
1099 | dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len); | |
1100 | memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len); | |
1101 | } | |
1102 | ||
1103 | /* | |
1104 | * Shift block pointers one index left/right inside a single btree block. | |
1105 | */ | |
1106 | STATIC void | |
1107 | xfs_btree_shift_ptrs( | |
1108 | struct xfs_btree_cur *cur, | |
1109 | union xfs_btree_ptr *ptr, | |
1110 | int dir, | |
1111 | int numptrs) | |
1112 | { | |
1113 | char *dst_ptr; | |
1114 | ||
1115 | ASSERT(numptrs >= 0); | |
1116 | ASSERT(dir == 1 || dir == -1); | |
1117 | ||
1118 | dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur)); | |
1119 | memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur)); | |
1120 | } | |
1121 | ||
38bb7423 CH |
1122 | /* |
1123 | * Log key values from the btree block. | |
1124 | */ | |
1125 | STATIC void | |
1126 | xfs_btree_log_keys( | |
1127 | struct xfs_btree_cur *cur, | |
1128 | struct xfs_buf *bp, | |
1129 | int first, | |
1130 | int last) | |
1131 | { | |
1132 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
1133 | XFS_BTREE_TRACE_ARGBII(cur, bp, first, last); | |
1134 | ||
1135 | if (bp) { | |
1136 | xfs_trans_log_buf(cur->bc_tp, bp, | |
1137 | xfs_btree_key_offset(cur, first), | |
1138 | xfs_btree_key_offset(cur, last + 1) - 1); | |
1139 | } else { | |
1140 | xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip, | |
1141 | xfs_ilog_fbroot(cur->bc_private.b.whichfork)); | |
1142 | } | |
1143 | ||
1144 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
1145 | } | |
1146 | ||
278d0ca1 CH |
1147 | /* |
1148 | * Log record values from the btree block. | |
1149 | */ | |
fd6bcc5b | 1150 | void |
278d0ca1 CH |
1151 | xfs_btree_log_recs( |
1152 | struct xfs_btree_cur *cur, | |
1153 | struct xfs_buf *bp, | |
1154 | int first, | |
1155 | int last) | |
1156 | { | |
1157 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
1158 | XFS_BTREE_TRACE_ARGBII(cur, bp, first, last); | |
1159 | ||
1160 | xfs_trans_log_buf(cur->bc_tp, bp, | |
1161 | xfs_btree_rec_offset(cur, first), | |
1162 | xfs_btree_rec_offset(cur, last + 1) - 1); | |
1163 | ||
1164 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
1165 | } | |
1166 | ||
9eaead51 CH |
1167 | /* |
1168 | * Log block pointer fields from a btree block (nonleaf). | |
1169 | */ | |
1170 | STATIC void | |
1171 | xfs_btree_log_ptrs( | |
1172 | struct xfs_btree_cur *cur, /* btree cursor */ | |
1173 | struct xfs_buf *bp, /* buffer containing btree block */ | |
1174 | int first, /* index of first pointer to log */ | |
1175 | int last) /* index of last pointer to log */ | |
1176 | { | |
1177 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
1178 | XFS_BTREE_TRACE_ARGBII(cur, bp, first, last); | |
1179 | ||
1180 | if (bp) { | |
1181 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); | |
1182 | int level = xfs_btree_get_level(block); | |
1183 | ||
1184 | xfs_trans_log_buf(cur->bc_tp, bp, | |
1185 | xfs_btree_ptr_offset(cur, first, level), | |
1186 | xfs_btree_ptr_offset(cur, last + 1, level) - 1); | |
1187 | } else { | |
1188 | xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip, | |
1189 | xfs_ilog_fbroot(cur->bc_private.b.whichfork)); | |
1190 | } | |
1191 | ||
1192 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
1193 | } | |
1194 | ||
1195 | /* | |
1196 | * Log fields from a btree block header. | |
1197 | */ | |
fd6bcc5b | 1198 | void |
9eaead51 CH |
1199 | xfs_btree_log_block( |
1200 | struct xfs_btree_cur *cur, /* btree cursor */ | |
1201 | struct xfs_buf *bp, /* buffer containing btree block */ | |
1202 | int fields) /* mask of fields: XFS_BB_... */ | |
1203 | { | |
1204 | int first; /* first byte offset logged */ | |
1205 | int last; /* last byte offset logged */ | |
1206 | static const short soffsets[] = { /* table of offsets (short) */ | |
7cc95a82 CH |
1207 | offsetof(struct xfs_btree_block, bb_magic), |
1208 | offsetof(struct xfs_btree_block, bb_level), | |
1209 | offsetof(struct xfs_btree_block, bb_numrecs), | |
1210 | offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib), | |
1211 | offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib), | |
1212 | XFS_BTREE_SBLOCK_LEN | |
9eaead51 CH |
1213 | }; |
1214 | static const short loffsets[] = { /* table of offsets (long) */ | |
7cc95a82 CH |
1215 | offsetof(struct xfs_btree_block, bb_magic), |
1216 | offsetof(struct xfs_btree_block, bb_level), | |
1217 | offsetof(struct xfs_btree_block, bb_numrecs), | |
1218 | offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib), | |
1219 | offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib), | |
1220 | XFS_BTREE_LBLOCK_LEN | |
9eaead51 CH |
1221 | }; |
1222 | ||
1223 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
1224 | XFS_BTREE_TRACE_ARGBI(cur, bp, fields); | |
1225 | ||
1226 | if (bp) { | |
1227 | xfs_btree_offsets(fields, | |
1228 | (cur->bc_flags & XFS_BTREE_LONG_PTRS) ? | |
1229 | loffsets : soffsets, | |
1230 | XFS_BB_NUM_BITS, &first, &last); | |
1231 | xfs_trans_log_buf(cur->bc_tp, bp, first, last); | |
1232 | } else { | |
1233 | xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip, | |
1234 | xfs_ilog_fbroot(cur->bc_private.b.whichfork)); | |
1235 | } | |
1236 | ||
1237 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
1238 | } | |
1239 | ||
637aa50f CH |
1240 | /* |
1241 | * Increment cursor by one record at the level. | |
1242 | * For nonzero levels the leaf-ward information is untouched. | |
1243 | */ | |
1244 | int /* error */ | |
1245 | xfs_btree_increment( | |
1246 | struct xfs_btree_cur *cur, | |
1247 | int level, | |
1248 | int *stat) /* success/failure */ | |
1249 | { | |
1250 | struct xfs_btree_block *block; | |
1251 | union xfs_btree_ptr ptr; | |
1252 | struct xfs_buf *bp; | |
1253 | int error; /* error return value */ | |
1254 | int lev; | |
1255 | ||
1256 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
1257 | XFS_BTREE_TRACE_ARGI(cur, level); | |
1258 | ||
1259 | ASSERT(level < cur->bc_nlevels); | |
1260 | ||
1261 | /* Read-ahead to the right at this level. */ | |
1262 | xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA); | |
1263 | ||
1264 | /* Get a pointer to the btree block. */ | |
1265 | block = xfs_btree_get_block(cur, level, &bp); | |
1266 | ||
1267 | #ifdef DEBUG | |
1268 | error = xfs_btree_check_block(cur, block, level, bp); | |
1269 | if (error) | |
1270 | goto error0; | |
1271 | #endif | |
1272 | ||
1273 | /* We're done if we remain in the block after the increment. */ | |
1274 | if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block)) | |
1275 | goto out1; | |
1276 | ||
1277 | /* Fail if we just went off the right edge of the tree. */ | |
1278 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB); | |
1279 | if (xfs_btree_ptr_is_null(cur, &ptr)) | |
1280 | goto out0; | |
1281 | ||
1282 | XFS_BTREE_STATS_INC(cur, increment); | |
1283 | ||
1284 | /* | |
1285 | * March up the tree incrementing pointers. | |
1286 | * Stop when we don't go off the right edge of a block. | |
1287 | */ | |
1288 | for (lev = level + 1; lev < cur->bc_nlevels; lev++) { | |
1289 | block = xfs_btree_get_block(cur, lev, &bp); | |
1290 | ||
1291 | #ifdef DEBUG | |
1292 | error = xfs_btree_check_block(cur, block, lev, bp); | |
1293 | if (error) | |
1294 | goto error0; | |
1295 | #endif | |
1296 | ||
1297 | if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block)) | |
1298 | break; | |
1299 | ||
1300 | /* Read-ahead the right block for the next loop. */ | |
1301 | xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA); | |
1302 | } | |
1303 | ||
1304 | /* | |
1305 | * If we went off the root then we are either seriously | |
1306 | * confused or have the tree root in an inode. | |
1307 | */ | |
1308 | if (lev == cur->bc_nlevels) { | |
1309 | if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) | |
1310 | goto out0; | |
1311 | ASSERT(0); | |
1312 | error = EFSCORRUPTED; | |
1313 | goto error0; | |
1314 | } | |
1315 | ASSERT(lev < cur->bc_nlevels); | |
1316 | ||
1317 | /* | |
1318 | * Now walk back down the tree, fixing up the cursor's buffer | |
1319 | * pointers and key numbers. | |
1320 | */ | |
1321 | for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) { | |
1322 | union xfs_btree_ptr *ptrp; | |
1323 | ||
1324 | ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block); | |
1325 | error = xfs_btree_read_buf_block(cur, ptrp, --lev, | |
1326 | 0, &block, &bp); | |
1327 | if (error) | |
1328 | goto error0; | |
1329 | ||
1330 | xfs_btree_setbuf(cur, lev, bp); | |
1331 | cur->bc_ptrs[lev] = 1; | |
1332 | } | |
1333 | out1: | |
1334 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
1335 | *stat = 1; | |
1336 | return 0; | |
1337 | ||
1338 | out0: | |
1339 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
1340 | *stat = 0; | |
1341 | return 0; | |
1342 | ||
1343 | error0: | |
1344 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
1345 | return error; | |
1346 | } | |
8df4da4a CH |
1347 | |
1348 | /* | |
1349 | * Decrement cursor by one record at the level. | |
1350 | * For nonzero levels the leaf-ward information is untouched. | |
1351 | */ | |
1352 | int /* error */ | |
1353 | xfs_btree_decrement( | |
1354 | struct xfs_btree_cur *cur, | |
1355 | int level, | |
1356 | int *stat) /* success/failure */ | |
1357 | { | |
1358 | struct xfs_btree_block *block; | |
1359 | xfs_buf_t *bp; | |
1360 | int error; /* error return value */ | |
1361 | int lev; | |
1362 | union xfs_btree_ptr ptr; | |
1363 | ||
1364 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
1365 | XFS_BTREE_TRACE_ARGI(cur, level); | |
1366 | ||
1367 | ASSERT(level < cur->bc_nlevels); | |
1368 | ||
1369 | /* Read-ahead to the left at this level. */ | |
1370 | xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA); | |
1371 | ||
1372 | /* We're done if we remain in the block after the decrement. */ | |
1373 | if (--cur->bc_ptrs[level] > 0) | |
1374 | goto out1; | |
1375 | ||
1376 | /* Get a pointer to the btree block. */ | |
1377 | block = xfs_btree_get_block(cur, level, &bp); | |
1378 | ||
1379 | #ifdef DEBUG | |
1380 | error = xfs_btree_check_block(cur, block, level, bp); | |
1381 | if (error) | |
1382 | goto error0; | |
1383 | #endif | |
1384 | ||
1385 | /* Fail if we just went off the left edge of the tree. */ | |
1386 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB); | |
1387 | if (xfs_btree_ptr_is_null(cur, &ptr)) | |
1388 | goto out0; | |
1389 | ||
1390 | XFS_BTREE_STATS_INC(cur, decrement); | |
1391 | ||
1392 | /* | |
1393 | * March up the tree decrementing pointers. | |
1394 | * Stop when we don't go off the left edge of a block. | |
1395 | */ | |
1396 | for (lev = level + 1; lev < cur->bc_nlevels; lev++) { | |
1397 | if (--cur->bc_ptrs[lev] > 0) | |
1398 | break; | |
1399 | /* Read-ahead the left block for the next loop. */ | |
1400 | xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA); | |
1401 | } | |
1402 | ||
1403 | /* | |
1404 | * If we went off the root then we are seriously confused. | |
1405 | * or the root of the tree is in an inode. | |
1406 | */ | |
1407 | if (lev == cur->bc_nlevels) { | |
1408 | if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) | |
1409 | goto out0; | |
1410 | ASSERT(0); | |
1411 | error = EFSCORRUPTED; | |
1412 | goto error0; | |
1413 | } | |
1414 | ASSERT(lev < cur->bc_nlevels); | |
1415 | ||
1416 | /* | |
1417 | * Now walk back down the tree, fixing up the cursor's buffer | |
1418 | * pointers and key numbers. | |
1419 | */ | |
1420 | for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) { | |
1421 | union xfs_btree_ptr *ptrp; | |
1422 | ||
1423 | ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block); | |
1424 | error = xfs_btree_read_buf_block(cur, ptrp, --lev, | |
1425 | 0, &block, &bp); | |
1426 | if (error) | |
1427 | goto error0; | |
1428 | xfs_btree_setbuf(cur, lev, bp); | |
1429 | cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block); | |
1430 | } | |
1431 | out1: | |
1432 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
1433 | *stat = 1; | |
1434 | return 0; | |
1435 | ||
1436 | out0: | |
1437 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
1438 | *stat = 0; | |
1439 | return 0; | |
1440 | ||
1441 | error0: | |
1442 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
1443 | return error; | |
1444 | } | |
1445 | ||
fe033cc8 CH |
1446 | STATIC int |
1447 | xfs_btree_lookup_get_block( | |
1448 | struct xfs_btree_cur *cur, /* btree cursor */ | |
1449 | int level, /* level in the btree */ | |
1450 | union xfs_btree_ptr *pp, /* ptr to btree block */ | |
1451 | struct xfs_btree_block **blkp) /* return btree block */ | |
1452 | { | |
1453 | struct xfs_buf *bp; /* buffer pointer for btree block */ | |
1454 | int error = 0; | |
1455 | ||
1456 | /* special case the root block if in an inode */ | |
1457 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && | |
1458 | (level == cur->bc_nlevels - 1)) { | |
1459 | *blkp = xfs_btree_get_iroot(cur); | |
1460 | return 0; | |
1461 | } | |
1462 | ||
1463 | /* | |
1464 | * If the old buffer at this level for the disk address we are | |
1465 | * looking for re-use it. | |
1466 | * | |
1467 | * Otherwise throw it away and get a new one. | |
1468 | */ | |
1469 | bp = cur->bc_bufs[level]; | |
1470 | if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) { | |
1471 | *blkp = XFS_BUF_TO_BLOCK(bp); | |
1472 | return 0; | |
1473 | } | |
1474 | ||
1475 | error = xfs_btree_read_buf_block(cur, pp, level, 0, blkp, &bp); | |
1476 | if (error) | |
1477 | return error; | |
1478 | ||
1479 | xfs_btree_setbuf(cur, level, bp); | |
1480 | return 0; | |
1481 | } | |
1482 | ||
1483 | /* | |
1484 | * Get current search key. For level 0 we don't actually have a key | |
1485 | * structure so we make one up from the record. For all other levels | |
1486 | * we just return the right key. | |
1487 | */ | |
1488 | STATIC union xfs_btree_key * | |
1489 | xfs_lookup_get_search_key( | |
1490 | struct xfs_btree_cur *cur, | |
1491 | int level, | |
1492 | int keyno, | |
1493 | struct xfs_btree_block *block, | |
1494 | union xfs_btree_key *kp) | |
1495 | { | |
1496 | if (level == 0) { | |
1497 | cur->bc_ops->init_key_from_rec(kp, | |
1498 | xfs_btree_rec_addr(cur, keyno, block)); | |
1499 | return kp; | |
1500 | } | |
1501 | ||
1502 | return xfs_btree_key_addr(cur, keyno, block); | |
1503 | } | |
1504 | ||
1505 | /* | |
1506 | * Lookup the record. The cursor is made to point to it, based on dir. | |
1507 | * Return 0 if can't find any such record, 1 for success. | |
1508 | */ | |
1509 | int /* error */ | |
1510 | xfs_btree_lookup( | |
1511 | struct xfs_btree_cur *cur, /* btree cursor */ | |
1512 | xfs_lookup_t dir, /* <=, ==, or >= */ | |
1513 | int *stat) /* success/failure */ | |
1514 | { | |
1515 | struct xfs_btree_block *block; /* current btree block */ | |
1516 | __int64_t diff; /* difference for the current key */ | |
1517 | int error; /* error return value */ | |
1518 | int keyno; /* current key number */ | |
1519 | int level; /* level in the btree */ | |
1520 | union xfs_btree_ptr *pp; /* ptr to btree block */ | |
1521 | union xfs_btree_ptr ptr; /* ptr to btree block */ | |
1522 | ||
1523 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
1524 | XFS_BTREE_TRACE_ARGI(cur, dir); | |
1525 | ||
1526 | XFS_BTREE_STATS_INC(cur, lookup); | |
1527 | ||
1528 | block = NULL; | |
1529 | keyno = 0; | |
1530 | ||
1531 | /* initialise start pointer from cursor */ | |
1532 | cur->bc_ops->init_ptr_from_cur(cur, &ptr); | |
1533 | pp = &ptr; | |
1534 | ||
1535 | /* | |
1536 | * Iterate over each level in the btree, starting at the root. | |
1537 | * For each level above the leaves, find the key we need, based | |
1538 | * on the lookup record, then follow the corresponding block | |
1539 | * pointer down to the next level. | |
1540 | */ | |
1541 | for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) { | |
1542 | /* Get the block we need to do the lookup on. */ | |
1543 | error = xfs_btree_lookup_get_block(cur, level, pp, &block); | |
1544 | if (error) | |
1545 | goto error0; | |
1546 | ||
1547 | if (diff == 0) { | |
1548 | /* | |
1549 | * If we already had a key match at a higher level, we | |
1550 | * know we need to use the first entry in this block. | |
1551 | */ | |
1552 | keyno = 1; | |
1553 | } else { | |
1554 | /* Otherwise search this block. Do a binary search. */ | |
1555 | ||
1556 | int high; /* high entry number */ | |
1557 | int low; /* low entry number */ | |
1558 | ||
1559 | /* Set low and high entry numbers, 1-based. */ | |
1560 | low = 1; | |
1561 | high = xfs_btree_get_numrecs(block); | |
1562 | if (!high) { | |
1563 | /* Block is empty, must be an empty leaf. */ | |
1564 | ASSERT(level == 0 && cur->bc_nlevels == 1); | |
1565 | ||
1566 | cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE; | |
1567 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
1568 | *stat = 0; | |
1569 | return 0; | |
1570 | } | |
1571 | ||
1572 | /* Binary search the block. */ | |
1573 | while (low <= high) { | |
1574 | union xfs_btree_key key; | |
1575 | union xfs_btree_key *kp; | |
1576 | ||
1577 | XFS_BTREE_STATS_INC(cur, compare); | |
1578 | ||
1579 | /* keyno is average of low and high. */ | |
1580 | keyno = (low + high) >> 1; | |
1581 | ||
1582 | /* Get current search key */ | |
1583 | kp = xfs_lookup_get_search_key(cur, level, | |
1584 | keyno, block, &key); | |
1585 | ||
1586 | /* | |
1587 | * Compute difference to get next direction: | |
1588 | * - less than, move right | |
1589 | * - greater than, move left | |
1590 | * - equal, we're done | |
1591 | */ | |
1592 | diff = cur->bc_ops->key_diff(cur, kp); | |
1593 | if (diff < 0) | |
1594 | low = keyno + 1; | |
1595 | else if (diff > 0) | |
1596 | high = keyno - 1; | |
1597 | else | |
1598 | break; | |
1599 | } | |
1600 | } | |
1601 | ||
1602 | /* | |
1603 | * If there are more levels, set up for the next level | |
1604 | * by getting the block number and filling in the cursor. | |
1605 | */ | |
1606 | if (level > 0) { | |
1607 | /* | |
1608 | * If we moved left, need the previous key number, | |
1609 | * unless there isn't one. | |
1610 | */ | |
1611 | if (diff > 0 && --keyno < 1) | |
1612 | keyno = 1; | |
1613 | pp = xfs_btree_ptr_addr(cur, keyno, block); | |
1614 | ||
1615 | #ifdef DEBUG | |
1616 | error = xfs_btree_check_ptr(cur, pp, 0, level); | |
1617 | if (error) | |
1618 | goto error0; | |
1619 | #endif | |
1620 | cur->bc_ptrs[level] = keyno; | |
1621 | } | |
1622 | } | |
1623 | ||
1624 | /* Done with the search. See if we need to adjust the results. */ | |
1625 | if (dir != XFS_LOOKUP_LE && diff < 0) { | |
1626 | keyno++; | |
1627 | /* | |
1628 | * If ge search and we went off the end of the block, but it's | |
1629 | * not the last block, we're in the wrong block. | |
1630 | */ | |
1631 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB); | |
1632 | if (dir == XFS_LOOKUP_GE && | |
1633 | keyno > xfs_btree_get_numrecs(block) && | |
1634 | !xfs_btree_ptr_is_null(cur, &ptr)) { | |
1635 | int i; | |
1636 | ||
1637 | cur->bc_ptrs[0] = keyno; | |
1638 | error = xfs_btree_increment(cur, 0, &i); | |
1639 | if (error) | |
1640 | goto error0; | |
1641 | XFS_WANT_CORRUPTED_RETURN(i == 1); | |
1642 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
1643 | *stat = 1; | |
1644 | return 0; | |
1645 | } | |
1646 | } else if (dir == XFS_LOOKUP_LE && diff > 0) | |
1647 | keyno--; | |
1648 | cur->bc_ptrs[0] = keyno; | |
1649 | ||
1650 | /* Return if we succeeded or not. */ | |
1651 | if (keyno == 0 || keyno > xfs_btree_get_numrecs(block)) | |
1652 | *stat = 0; | |
1653 | else if (dir != XFS_LOOKUP_EQ || diff == 0) | |
1654 | *stat = 1; | |
1655 | else | |
1656 | *stat = 0; | |
1657 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
1658 | return 0; | |
1659 | ||
1660 | error0: | |
1661 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
1662 | return error; | |
1663 | } | |
38bb7423 CH |
1664 | |
1665 | /* | |
1666 | * Update keys at all levels from here to the root along the cursor's path. | |
1667 | */ | |
3cc7524c | 1668 | STATIC int |
38bb7423 CH |
1669 | xfs_btree_updkey( |
1670 | struct xfs_btree_cur *cur, | |
1671 | union xfs_btree_key *keyp, | |
1672 | int level) | |
1673 | { | |
1674 | struct xfs_btree_block *block; | |
1675 | struct xfs_buf *bp; | |
1676 | union xfs_btree_key *kp; | |
1677 | int ptr; | |
1678 | ||
1679 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
1680 | XFS_BTREE_TRACE_ARGIK(cur, level, keyp); | |
1681 | ||
1682 | ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1); | |
1683 | ||
1684 | /* | |
1685 | * Go up the tree from this level toward the root. | |
1686 | * At each level, update the key value to the value input. | |
1687 | * Stop when we reach a level where the cursor isn't pointing | |
1688 | * at the first entry in the block. | |
1689 | */ | |
1690 | for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) { | |
1691 | #ifdef DEBUG | |
1692 | int error; | |
1693 | #endif | |
1694 | block = xfs_btree_get_block(cur, level, &bp); | |
1695 | #ifdef DEBUG | |
1696 | error = xfs_btree_check_block(cur, block, level, bp); | |
1697 | if (error) { | |
1698 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
1699 | return error; | |
1700 | } | |
1701 | #endif | |
1702 | ptr = cur->bc_ptrs[level]; | |
1703 | kp = xfs_btree_key_addr(cur, ptr, block); | |
1704 | xfs_btree_copy_keys(cur, kp, keyp, 1); | |
1705 | xfs_btree_log_keys(cur, bp, ptr, ptr); | |
1706 | } | |
1707 | ||
1708 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
1709 | return 0; | |
1710 | } | |
278d0ca1 CH |
1711 | |
1712 | /* | |
1713 | * Update the record referred to by cur to the value in the | |
1714 | * given record. This either works (return 0) or gets an | |
1715 | * EFSCORRUPTED error. | |
1716 | */ | |
1717 | int | |
1718 | xfs_btree_update( | |
1719 | struct xfs_btree_cur *cur, | |
1720 | union xfs_btree_rec *rec) | |
1721 | { | |
1722 | struct xfs_btree_block *block; | |
1723 | struct xfs_buf *bp; | |
1724 | int error; | |
1725 | int ptr; | |
1726 | union xfs_btree_rec *rp; | |
1727 | ||
1728 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
1729 | XFS_BTREE_TRACE_ARGR(cur, rec); | |
1730 | ||
1731 | /* Pick up the current block. */ | |
1732 | block = xfs_btree_get_block(cur, 0, &bp); | |
1733 | ||
1734 | #ifdef DEBUG | |
1735 | error = xfs_btree_check_block(cur, block, 0, bp); | |
1736 | if (error) | |
1737 | goto error0; | |
1738 | #endif | |
1739 | /* Get the address of the rec to be updated. */ | |
1740 | ptr = cur->bc_ptrs[0]; | |
1741 | rp = xfs_btree_rec_addr(cur, ptr, block); | |
1742 | ||
1743 | /* Fill in the new contents and log them. */ | |
1744 | xfs_btree_copy_recs(cur, rp, rec, 1); | |
1745 | xfs_btree_log_recs(cur, bp, ptr, ptr); | |
1746 | ||
1747 | /* | |
1748 | * If we are tracking the last record in the tree and | |
1749 | * we are at the far right edge of the tree, update it. | |
1750 | */ | |
1751 | if (xfs_btree_is_lastrec(cur, block, 0)) { | |
1752 | cur->bc_ops->update_lastrec(cur, block, rec, | |
1753 | ptr, LASTREC_UPDATE); | |
1754 | } | |
1755 | ||
1756 | /* Updating first rec in leaf. Pass new key value up to our parent. */ | |
1757 | if (ptr == 1) { | |
1758 | union xfs_btree_key key; | |
1759 | ||
1760 | cur->bc_ops->init_key_from_rec(&key, rec); | |
1761 | error = xfs_btree_updkey(cur, &key, 1); | |
1762 | if (error) | |
1763 | goto error0; | |
1764 | } | |
1765 | ||
1766 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
1767 | return 0; | |
1768 | ||
1769 | error0: | |
1770 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
1771 | return error; | |
1772 | } | |
1773 | ||
687b890a CH |
1774 | /* |
1775 | * Move 1 record left from cur/level if possible. | |
1776 | * Update cur to reflect the new path. | |
1777 | */ | |
3cc7524c | 1778 | STATIC int /* error */ |
687b890a CH |
1779 | xfs_btree_lshift( |
1780 | struct xfs_btree_cur *cur, | |
1781 | int level, | |
1782 | int *stat) /* success/failure */ | |
1783 | { | |
1784 | union xfs_btree_key key; /* btree key */ | |
1785 | struct xfs_buf *lbp; /* left buffer pointer */ | |
1786 | struct xfs_btree_block *left; /* left btree block */ | |
1787 | int lrecs; /* left record count */ | |
1788 | struct xfs_buf *rbp; /* right buffer pointer */ | |
1789 | struct xfs_btree_block *right; /* right btree block */ | |
1790 | int rrecs; /* right record count */ | |
1791 | union xfs_btree_ptr lptr; /* left btree pointer */ | |
1792 | union xfs_btree_key *rkp = NULL; /* right btree key */ | |
1793 | union xfs_btree_ptr *rpp = NULL; /* right address pointer */ | |
1794 | union xfs_btree_rec *rrp = NULL; /* right record pointer */ | |
1795 | int error; /* error return value */ | |
1796 | ||
1797 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
1798 | XFS_BTREE_TRACE_ARGI(cur, level); | |
1799 | ||
1800 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && | |
1801 | level == cur->bc_nlevels - 1) | |
1802 | goto out0; | |
1803 | ||
1804 | /* Set up variables for this block as "right". */ | |
1805 | right = xfs_btree_get_block(cur, level, &rbp); | |
1806 | ||
1807 | #ifdef DEBUG | |
1808 | error = xfs_btree_check_block(cur, right, level, rbp); | |
1809 | if (error) | |
1810 | goto error0; | |
1811 | #endif | |
1812 | ||
1813 | /* If we've got no left sibling then we can't shift an entry left. */ | |
1814 | xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB); | |
1815 | if (xfs_btree_ptr_is_null(cur, &lptr)) | |
1816 | goto out0; | |
1817 | ||
1818 | /* | |
1819 | * If the cursor entry is the one that would be moved, don't | |
1820 | * do it... it's too complicated. | |
1821 | */ | |
1822 | if (cur->bc_ptrs[level] <= 1) | |
1823 | goto out0; | |
1824 | ||
1825 | /* Set up the left neighbor as "left". */ | |
1826 | error = xfs_btree_read_buf_block(cur, &lptr, level, 0, &left, &lbp); | |
1827 | if (error) | |
1828 | goto error0; | |
1829 | ||
1830 | /* If it's full, it can't take another entry. */ | |
1831 | lrecs = xfs_btree_get_numrecs(left); | |
1832 | if (lrecs == cur->bc_ops->get_maxrecs(cur, level)) | |
1833 | goto out0; | |
1834 | ||
1835 | rrecs = xfs_btree_get_numrecs(right); | |
1836 | ||
1837 | /* | |
1838 | * We add one entry to the left side and remove one for the right side. | |
9da096fd | 1839 | * Account for it here, the changes will be updated on disk and logged |
687b890a CH |
1840 | * later. |
1841 | */ | |
1842 | lrecs++; | |
1843 | rrecs--; | |
1844 | ||
1845 | XFS_BTREE_STATS_INC(cur, lshift); | |
1846 | XFS_BTREE_STATS_ADD(cur, moves, 1); | |
1847 | ||
1848 | /* | |
1849 | * If non-leaf, copy a key and a ptr to the left block. | |
1850 | * Log the changes to the left block. | |
1851 | */ | |
1852 | if (level > 0) { | |
1853 | /* It's a non-leaf. Move keys and pointers. */ | |
1854 | union xfs_btree_key *lkp; /* left btree key */ | |
1855 | union xfs_btree_ptr *lpp; /* left address pointer */ | |
1856 | ||
1857 | lkp = xfs_btree_key_addr(cur, lrecs, left); | |
1858 | rkp = xfs_btree_key_addr(cur, 1, right); | |
1859 | ||
1860 | lpp = xfs_btree_ptr_addr(cur, lrecs, left); | |
1861 | rpp = xfs_btree_ptr_addr(cur, 1, right); | |
1862 | #ifdef DEBUG | |
1863 | error = xfs_btree_check_ptr(cur, rpp, 0, level); | |
1864 | if (error) | |
1865 | goto error0; | |
1866 | #endif | |
1867 | xfs_btree_copy_keys(cur, lkp, rkp, 1); | |
1868 | xfs_btree_copy_ptrs(cur, lpp, rpp, 1); | |
1869 | ||
1870 | xfs_btree_log_keys(cur, lbp, lrecs, lrecs); | |
1871 | xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs); | |
1872 | ||
4a26e66e CH |
1873 | ASSERT(cur->bc_ops->keys_inorder(cur, |
1874 | xfs_btree_key_addr(cur, lrecs - 1, left), lkp)); | |
687b890a CH |
1875 | } else { |
1876 | /* It's a leaf. Move records. */ | |
1877 | union xfs_btree_rec *lrp; /* left record pointer */ | |
1878 | ||
1879 | lrp = xfs_btree_rec_addr(cur, lrecs, left); | |
1880 | rrp = xfs_btree_rec_addr(cur, 1, right); | |
1881 | ||
1882 | xfs_btree_copy_recs(cur, lrp, rrp, 1); | |
1883 | xfs_btree_log_recs(cur, lbp, lrecs, lrecs); | |
1884 | ||
4a26e66e CH |
1885 | ASSERT(cur->bc_ops->recs_inorder(cur, |
1886 | xfs_btree_rec_addr(cur, lrecs - 1, left), lrp)); | |
687b890a CH |
1887 | } |
1888 | ||
1889 | xfs_btree_set_numrecs(left, lrecs); | |
1890 | xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS); | |
1891 | ||
1892 | xfs_btree_set_numrecs(right, rrecs); | |
1893 | xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS); | |
1894 | ||
1895 | /* | |
1896 | * Slide the contents of right down one entry. | |
1897 | */ | |
1898 | XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1); | |
1899 | if (level > 0) { | |
1900 | /* It's a nonleaf. operate on keys and ptrs */ | |
1901 | #ifdef DEBUG | |
1902 | int i; /* loop index */ | |
1903 | ||
1904 | for (i = 0; i < rrecs; i++) { | |
1905 | error = xfs_btree_check_ptr(cur, rpp, i + 1, level); | |
1906 | if (error) | |
1907 | goto error0; | |
1908 | } | |
1909 | #endif | |
1910 | xfs_btree_shift_keys(cur, | |
1911 | xfs_btree_key_addr(cur, 2, right), | |
1912 | -1, rrecs); | |
1913 | xfs_btree_shift_ptrs(cur, | |
1914 | xfs_btree_ptr_addr(cur, 2, right), | |
1915 | -1, rrecs); | |
1916 | ||
1917 | xfs_btree_log_keys(cur, rbp, 1, rrecs); | |
1918 | xfs_btree_log_ptrs(cur, rbp, 1, rrecs); | |
1919 | } else { | |
1920 | /* It's a leaf. operate on records */ | |
1921 | xfs_btree_shift_recs(cur, | |
1922 | xfs_btree_rec_addr(cur, 2, right), | |
1923 | -1, rrecs); | |
1924 | xfs_btree_log_recs(cur, rbp, 1, rrecs); | |
1925 | ||
1926 | /* | |
1927 | * If it's the first record in the block, we'll need a key | |
1928 | * structure to pass up to the next level (updkey). | |
1929 | */ | |
1930 | cur->bc_ops->init_key_from_rec(&key, | |
1931 | xfs_btree_rec_addr(cur, 1, right)); | |
1932 | rkp = &key; | |
1933 | } | |
1934 | ||
1935 | /* Update the parent key values of right. */ | |
1936 | error = xfs_btree_updkey(cur, rkp, level + 1); | |
1937 | if (error) | |
1938 | goto error0; | |
1939 | ||
1940 | /* Slide the cursor value left one. */ | |
1941 | cur->bc_ptrs[level]--; | |
1942 | ||
1943 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
1944 | *stat = 1; | |
1945 | return 0; | |
1946 | ||
1947 | out0: | |
1948 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
1949 | *stat = 0; | |
1950 | return 0; | |
1951 | ||
1952 | error0: | |
1953 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
1954 | return error; | |
1955 | } | |
1956 | ||
9eaead51 CH |
1957 | /* |
1958 | * Move 1 record right from cur/level if possible. | |
1959 | * Update cur to reflect the new path. | |
1960 | */ | |
3cc7524c | 1961 | STATIC int /* error */ |
9eaead51 CH |
1962 | xfs_btree_rshift( |
1963 | struct xfs_btree_cur *cur, | |
1964 | int level, | |
1965 | int *stat) /* success/failure */ | |
1966 | { | |
1967 | union xfs_btree_key key; /* btree key */ | |
1968 | struct xfs_buf *lbp; /* left buffer pointer */ | |
1969 | struct xfs_btree_block *left; /* left btree block */ | |
1970 | struct xfs_buf *rbp; /* right buffer pointer */ | |
1971 | struct xfs_btree_block *right; /* right btree block */ | |
1972 | struct xfs_btree_cur *tcur; /* temporary btree cursor */ | |
1973 | union xfs_btree_ptr rptr; /* right block pointer */ | |
1974 | union xfs_btree_key *rkp; /* right btree key */ | |
1975 | int rrecs; /* right record count */ | |
1976 | int lrecs; /* left record count */ | |
1977 | int error; /* error return value */ | |
1978 | int i; /* loop counter */ | |
1979 | ||
1980 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
1981 | XFS_BTREE_TRACE_ARGI(cur, level); | |
1982 | ||
1983 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && | |
1984 | (level == cur->bc_nlevels - 1)) | |
1985 | goto out0; | |
1986 | ||
1987 | /* Set up variables for this block as "left". */ | |
1988 | left = xfs_btree_get_block(cur, level, &lbp); | |
1989 | ||
1990 | #ifdef DEBUG | |
1991 | error = xfs_btree_check_block(cur, left, level, lbp); | |
1992 | if (error) | |
1993 | goto error0; | |
1994 | #endif | |
1995 | ||
1996 | /* If we've got no right sibling then we can't shift an entry right. */ | |
1997 | xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB); | |
1998 | if (xfs_btree_ptr_is_null(cur, &rptr)) | |
1999 | goto out0; | |
2000 | ||
2001 | /* | |
2002 | * If the cursor entry is the one that would be moved, don't | |
2003 | * do it... it's too complicated. | |
2004 | */ | |
2005 | lrecs = xfs_btree_get_numrecs(left); | |
2006 | if (cur->bc_ptrs[level] >= lrecs) | |
2007 | goto out0; | |
2008 | ||
2009 | /* Set up the right neighbor as "right". */ | |
2010 | error = xfs_btree_read_buf_block(cur, &rptr, level, 0, &right, &rbp); | |
2011 | if (error) | |
2012 | goto error0; | |
2013 | ||
2014 | /* If it's full, it can't take another entry. */ | |
2015 | rrecs = xfs_btree_get_numrecs(right); | |
2016 | if (rrecs == cur->bc_ops->get_maxrecs(cur, level)) | |
2017 | goto out0; | |
2018 | ||
2019 | XFS_BTREE_STATS_INC(cur, rshift); | |
2020 | XFS_BTREE_STATS_ADD(cur, moves, rrecs); | |
2021 | ||
2022 | /* | |
2023 | * Make a hole at the start of the right neighbor block, then | |
2024 | * copy the last left block entry to the hole. | |
2025 | */ | |
2026 | if (level > 0) { | |
2027 | /* It's a nonleaf. make a hole in the keys and ptrs */ | |
2028 | union xfs_btree_key *lkp; | |
2029 | union xfs_btree_ptr *lpp; | |
2030 | union xfs_btree_ptr *rpp; | |
2031 | ||
2032 | lkp = xfs_btree_key_addr(cur, lrecs, left); | |
2033 | lpp = xfs_btree_ptr_addr(cur, lrecs, left); | |
2034 | rkp = xfs_btree_key_addr(cur, 1, right); | |
2035 | rpp = xfs_btree_ptr_addr(cur, 1, right); | |
2036 | ||
2037 | #ifdef DEBUG | |
2038 | for (i = rrecs - 1; i >= 0; i--) { | |
2039 | error = xfs_btree_check_ptr(cur, rpp, i, level); | |
2040 | if (error) | |
2041 | goto error0; | |
2042 | } | |
2043 | #endif | |
2044 | ||
2045 | xfs_btree_shift_keys(cur, rkp, 1, rrecs); | |
2046 | xfs_btree_shift_ptrs(cur, rpp, 1, rrecs); | |
2047 | ||
2048 | #ifdef DEBUG | |
2049 | error = xfs_btree_check_ptr(cur, lpp, 0, level); | |
2050 | if (error) | |
2051 | goto error0; | |
2052 | #endif | |
2053 | ||
2054 | /* Now put the new data in, and log it. */ | |
2055 | xfs_btree_copy_keys(cur, rkp, lkp, 1); | |
2056 | xfs_btree_copy_ptrs(cur, rpp, lpp, 1); | |
2057 | ||
2058 | xfs_btree_log_keys(cur, rbp, 1, rrecs + 1); | |
2059 | xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1); | |
2060 | ||
4a26e66e CH |
2061 | ASSERT(cur->bc_ops->keys_inorder(cur, rkp, |
2062 | xfs_btree_key_addr(cur, 2, right))); | |
9eaead51 CH |
2063 | } else { |
2064 | /* It's a leaf. make a hole in the records */ | |
2065 | union xfs_btree_rec *lrp; | |
2066 | union xfs_btree_rec *rrp; | |
2067 | ||
2068 | lrp = xfs_btree_rec_addr(cur, lrecs, left); | |
2069 | rrp = xfs_btree_rec_addr(cur, 1, right); | |
2070 | ||
2071 | xfs_btree_shift_recs(cur, rrp, 1, rrecs); | |
2072 | ||
2073 | /* Now put the new data in, and log it. */ | |
2074 | xfs_btree_copy_recs(cur, rrp, lrp, 1); | |
2075 | xfs_btree_log_recs(cur, rbp, 1, rrecs + 1); | |
2076 | ||
2077 | cur->bc_ops->init_key_from_rec(&key, rrp); | |
2078 | rkp = &key; | |
2079 | ||
4a26e66e CH |
2080 | ASSERT(cur->bc_ops->recs_inorder(cur, rrp, |
2081 | xfs_btree_rec_addr(cur, 2, right))); | |
9eaead51 CH |
2082 | } |
2083 | ||
2084 | /* | |
2085 | * Decrement and log left's numrecs, bump and log right's numrecs. | |
2086 | */ | |
2087 | xfs_btree_set_numrecs(left, --lrecs); | |
2088 | xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS); | |
2089 | ||
2090 | xfs_btree_set_numrecs(right, ++rrecs); | |
2091 | xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS); | |
2092 | ||
2093 | /* | |
2094 | * Using a temporary cursor, update the parent key values of the | |
2095 | * block on the right. | |
2096 | */ | |
2097 | error = xfs_btree_dup_cursor(cur, &tcur); | |
2098 | if (error) | |
2099 | goto error0; | |
2100 | i = xfs_btree_lastrec(tcur, level); | |
2101 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
2102 | ||
2103 | error = xfs_btree_increment(tcur, level, &i); | |
2104 | if (error) | |
2105 | goto error1; | |
2106 | ||
2107 | error = xfs_btree_updkey(tcur, rkp, level + 1); | |
2108 | if (error) | |
2109 | goto error1; | |
2110 | ||
2111 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); | |
2112 | ||
2113 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
2114 | *stat = 1; | |
2115 | return 0; | |
2116 | ||
2117 | out0: | |
2118 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
2119 | *stat = 0; | |
2120 | return 0; | |
2121 | ||
2122 | error0: | |
2123 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
2124 | return error; | |
2125 | ||
2126 | error1: | |
2127 | XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR); | |
2128 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); | |
2129 | return error; | |
2130 | } | |
f5eb8e7c CH |
2131 | |
2132 | /* | |
2133 | * Split cur/level block in half. | |
2134 | * Return new block number and the key to its first | |
2135 | * record (to be inserted into parent). | |
2136 | */ | |
3cc7524c | 2137 | STATIC int /* error */ |
f5eb8e7c CH |
2138 | xfs_btree_split( |
2139 | struct xfs_btree_cur *cur, | |
2140 | int level, | |
2141 | union xfs_btree_ptr *ptrp, | |
2142 | union xfs_btree_key *key, | |
2143 | struct xfs_btree_cur **curp, | |
2144 | int *stat) /* success/failure */ | |
2145 | { | |
2146 | union xfs_btree_ptr lptr; /* left sibling block ptr */ | |
2147 | struct xfs_buf *lbp; /* left buffer pointer */ | |
2148 | struct xfs_btree_block *left; /* left btree block */ | |
2149 | union xfs_btree_ptr rptr; /* right sibling block ptr */ | |
2150 | struct xfs_buf *rbp; /* right buffer pointer */ | |
2151 | struct xfs_btree_block *right; /* right btree block */ | |
2152 | union xfs_btree_ptr rrptr; /* right-right sibling ptr */ | |
2153 | struct xfs_buf *rrbp; /* right-right buffer pointer */ | |
2154 | struct xfs_btree_block *rrblock; /* right-right btree block */ | |
2155 | int lrecs; | |
2156 | int rrecs; | |
2157 | int src_index; | |
2158 | int error; /* error return value */ | |
2159 | #ifdef DEBUG | |
2160 | int i; | |
2161 | #endif | |
2162 | ||
2163 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
2164 | XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key); | |
2165 | ||
2166 | XFS_BTREE_STATS_INC(cur, split); | |
2167 | ||
2168 | /* Set up left block (current one). */ | |
2169 | left = xfs_btree_get_block(cur, level, &lbp); | |
2170 | ||
2171 | #ifdef DEBUG | |
2172 | error = xfs_btree_check_block(cur, left, level, lbp); | |
2173 | if (error) | |
2174 | goto error0; | |
2175 | #endif | |
2176 | ||
2177 | xfs_btree_buf_to_ptr(cur, lbp, &lptr); | |
2178 | ||
2179 | /* Allocate the new block. If we can't do it, we're toast. Give up. */ | |
2180 | error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, 1, stat); | |
2181 | if (error) | |
2182 | goto error0; | |
2183 | if (*stat == 0) | |
2184 | goto out0; | |
2185 | XFS_BTREE_STATS_INC(cur, alloc); | |
2186 | ||
2187 | /* Set up the new block as "right". */ | |
2188 | error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp); | |
2189 | if (error) | |
2190 | goto error0; | |
2191 | ||
2192 | /* Fill in the btree header for the new right block. */ | |
2193 | xfs_btree_init_block(cur, xfs_btree_get_level(left), 0, right); | |
2194 | ||
2195 | /* | |
2196 | * Split the entries between the old and the new block evenly. | |
2197 | * Make sure that if there's an odd number of entries now, that | |
2198 | * each new block will have the same number of entries. | |
2199 | */ | |
2200 | lrecs = xfs_btree_get_numrecs(left); | |
2201 | rrecs = lrecs / 2; | |
2202 | if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1) | |
2203 | rrecs++; | |
2204 | src_index = (lrecs - rrecs + 1); | |
2205 | ||
2206 | XFS_BTREE_STATS_ADD(cur, moves, rrecs); | |
2207 | ||
2208 | /* | |
2209 | * Copy btree block entries from the left block over to the | |
2210 | * new block, the right. Update the right block and log the | |
2211 | * changes. | |
2212 | */ | |
2213 | if (level > 0) { | |
2214 | /* It's a non-leaf. Move keys and pointers. */ | |
2215 | union xfs_btree_key *lkp; /* left btree key */ | |
2216 | union xfs_btree_ptr *lpp; /* left address pointer */ | |
2217 | union xfs_btree_key *rkp; /* right btree key */ | |
2218 | union xfs_btree_ptr *rpp; /* right address pointer */ | |
2219 | ||
2220 | lkp = xfs_btree_key_addr(cur, src_index, left); | |
2221 | lpp = xfs_btree_ptr_addr(cur, src_index, left); | |
2222 | rkp = xfs_btree_key_addr(cur, 1, right); | |
2223 | rpp = xfs_btree_ptr_addr(cur, 1, right); | |
2224 | ||
2225 | #ifdef DEBUG | |
2226 | for (i = src_index; i < rrecs; i++) { | |
2227 | error = xfs_btree_check_ptr(cur, lpp, i, level); | |
2228 | if (error) | |
2229 | goto error0; | |
2230 | } | |
2231 | #endif | |
2232 | ||
2233 | xfs_btree_copy_keys(cur, rkp, lkp, rrecs); | |
2234 | xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs); | |
2235 | ||
2236 | xfs_btree_log_keys(cur, rbp, 1, rrecs); | |
2237 | xfs_btree_log_ptrs(cur, rbp, 1, rrecs); | |
2238 | ||
2239 | /* Grab the keys to the entries moved to the right block */ | |
2240 | xfs_btree_copy_keys(cur, key, rkp, 1); | |
2241 | } else { | |
2242 | /* It's a leaf. Move records. */ | |
2243 | union xfs_btree_rec *lrp; /* left record pointer */ | |
2244 | union xfs_btree_rec *rrp; /* right record pointer */ | |
2245 | ||
2246 | lrp = xfs_btree_rec_addr(cur, src_index, left); | |
2247 | rrp = xfs_btree_rec_addr(cur, 1, right); | |
2248 | ||
2249 | xfs_btree_copy_recs(cur, rrp, lrp, rrecs); | |
2250 | xfs_btree_log_recs(cur, rbp, 1, rrecs); | |
2251 | ||
2252 | cur->bc_ops->init_key_from_rec(key, | |
2253 | xfs_btree_rec_addr(cur, 1, right)); | |
2254 | } | |
2255 | ||
2256 | ||
2257 | /* | |
2258 | * Find the left block number by looking in the buffer. | |
2259 | * Adjust numrecs, sibling pointers. | |
2260 | */ | |
2261 | xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB); | |
2262 | xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB); | |
2263 | xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB); | |
2264 | xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB); | |
2265 | ||
2266 | lrecs -= rrecs; | |
2267 | xfs_btree_set_numrecs(left, lrecs); | |
2268 | xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs); | |
2269 | ||
2270 | xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS); | |
2271 | xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB); | |
2272 | ||
2273 | /* | |
2274 | * If there's a block to the new block's right, make that block | |
2275 | * point back to right instead of to left. | |
2276 | */ | |
2277 | if (!xfs_btree_ptr_is_null(cur, &rrptr)) { | |
2278 | error = xfs_btree_read_buf_block(cur, &rrptr, level, | |
2279 | 0, &rrblock, &rrbp); | |
2280 | if (error) | |
2281 | goto error0; | |
2282 | xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB); | |
2283 | xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB); | |
2284 | } | |
2285 | /* | |
2286 | * If the cursor is really in the right block, move it there. | |
2287 | * If it's just pointing past the last entry in left, then we'll | |
2288 | * insert there, so don't change anything in that case. | |
2289 | */ | |
2290 | if (cur->bc_ptrs[level] > lrecs + 1) { | |
2291 | xfs_btree_setbuf(cur, level, rbp); | |
2292 | cur->bc_ptrs[level] -= lrecs; | |
2293 | } | |
2294 | /* | |
2295 | * If there are more levels, we'll need another cursor which refers | |
2296 | * the right block, no matter where this cursor was. | |
2297 | */ | |
2298 | if (level + 1 < cur->bc_nlevels) { | |
2299 | error = xfs_btree_dup_cursor(cur, curp); | |
2300 | if (error) | |
2301 | goto error0; | |
2302 | (*curp)->bc_ptrs[level + 1]++; | |
2303 | } | |
2304 | *ptrp = rptr; | |
2305 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
2306 | *stat = 1; | |
2307 | return 0; | |
2308 | out0: | |
2309 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
2310 | *stat = 0; | |
2311 | return 0; | |
2312 | ||
2313 | error0: | |
2314 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
2315 | return error; | |
2316 | } | |
344207ce | 2317 | |
ea77b0a6 CH |
2318 | /* |
2319 | * Copy the old inode root contents into a real block and make the | |
2320 | * broot point to it. | |
2321 | */ | |
2322 | int /* error */ | |
2323 | xfs_btree_new_iroot( | |
2324 | struct xfs_btree_cur *cur, /* btree cursor */ | |
2325 | int *logflags, /* logging flags for inode */ | |
2326 | int *stat) /* return status - 0 fail */ | |
2327 | { | |
2328 | struct xfs_buf *cbp; /* buffer for cblock */ | |
2329 | struct xfs_btree_block *block; /* btree block */ | |
2330 | struct xfs_btree_block *cblock; /* child btree block */ | |
2331 | union xfs_btree_key *ckp; /* child key pointer */ | |
2332 | union xfs_btree_ptr *cpp; /* child ptr pointer */ | |
2333 | union xfs_btree_key *kp; /* pointer to btree key */ | |
2334 | union xfs_btree_ptr *pp; /* pointer to block addr */ | |
2335 | union xfs_btree_ptr nptr; /* new block addr */ | |
2336 | int level; /* btree level */ | |
2337 | int error; /* error return code */ | |
2338 | #ifdef DEBUG | |
2339 | int i; /* loop counter */ | |
2340 | #endif | |
2341 | ||
2342 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
2343 | XFS_BTREE_STATS_INC(cur, newroot); | |
2344 | ||
2345 | ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE); | |
2346 | ||
2347 | level = cur->bc_nlevels - 1; | |
2348 | ||
2349 | block = xfs_btree_get_iroot(cur); | |
2350 | pp = xfs_btree_ptr_addr(cur, 1, block); | |
2351 | ||
2352 | /* Allocate the new block. If we can't do it, we're toast. Give up. */ | |
2353 | error = cur->bc_ops->alloc_block(cur, pp, &nptr, 1, stat); | |
2354 | if (error) | |
2355 | goto error0; | |
2356 | if (*stat == 0) { | |
2357 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
2358 | return 0; | |
2359 | } | |
2360 | XFS_BTREE_STATS_INC(cur, alloc); | |
2361 | ||
2362 | /* Copy the root into a real block. */ | |
2363 | error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp); | |
2364 | if (error) | |
2365 | goto error0; | |
2366 | ||
2367 | memcpy(cblock, block, xfs_btree_block_len(cur)); | |
2368 | ||
2369 | be16_add_cpu(&block->bb_level, 1); | |
2370 | xfs_btree_set_numrecs(block, 1); | |
2371 | cur->bc_nlevels++; | |
2372 | cur->bc_ptrs[level + 1] = 1; | |
2373 | ||
2374 | kp = xfs_btree_key_addr(cur, 1, block); | |
2375 | ckp = xfs_btree_key_addr(cur, 1, cblock); | |
2376 | xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock)); | |
2377 | ||
2378 | cpp = xfs_btree_ptr_addr(cur, 1, cblock); | |
2379 | #ifdef DEBUG | |
2380 | for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) { | |
2381 | error = xfs_btree_check_ptr(cur, pp, i, level); | |
2382 | if (error) | |
2383 | goto error0; | |
2384 | } | |
2385 | #endif | |
2386 | xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock)); | |
2387 | ||
2388 | #ifdef DEBUG | |
2389 | error = xfs_btree_check_ptr(cur, &nptr, 0, level); | |
2390 | if (error) | |
2391 | goto error0; | |
2392 | #endif | |
2393 | xfs_btree_copy_ptrs(cur, pp, &nptr, 1); | |
2394 | ||
2395 | xfs_iroot_realloc(cur->bc_private.b.ip, | |
2396 | 1 - xfs_btree_get_numrecs(cblock), | |
2397 | cur->bc_private.b.whichfork); | |
2398 | ||
2399 | xfs_btree_setbuf(cur, level, cbp); | |
2400 | ||
2401 | /* | |
2402 | * Do all this logging at the end so that | |
2403 | * the root is at the right level. | |
2404 | */ | |
2405 | xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS); | |
2406 | xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs)); | |
2407 | xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs)); | |
2408 | ||
2409 | *logflags |= | |
9d87c319 | 2410 | XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork); |
ea77b0a6 CH |
2411 | *stat = 1; |
2412 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
2413 | return 0; | |
2414 | error0: | |
2415 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
2416 | return error; | |
2417 | } | |
2418 | ||
344207ce CH |
2419 | /* |
2420 | * Allocate a new root block, fill it in. | |
2421 | */ | |
3cc7524c | 2422 | STATIC int /* error */ |
344207ce CH |
2423 | xfs_btree_new_root( |
2424 | struct xfs_btree_cur *cur, /* btree cursor */ | |
2425 | int *stat) /* success/failure */ | |
2426 | { | |
2427 | struct xfs_btree_block *block; /* one half of the old root block */ | |
2428 | struct xfs_buf *bp; /* buffer containing block */ | |
2429 | int error; /* error return value */ | |
2430 | struct xfs_buf *lbp; /* left buffer pointer */ | |
2431 | struct xfs_btree_block *left; /* left btree block */ | |
2432 | struct xfs_buf *nbp; /* new (root) buffer */ | |
2433 | struct xfs_btree_block *new; /* new (root) btree block */ | |
2434 | int nptr; /* new value for key index, 1 or 2 */ | |
2435 | struct xfs_buf *rbp; /* right buffer pointer */ | |
2436 | struct xfs_btree_block *right; /* right btree block */ | |
2437 | union xfs_btree_ptr rptr; | |
2438 | union xfs_btree_ptr lptr; | |
2439 | ||
2440 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
2441 | XFS_BTREE_STATS_INC(cur, newroot); | |
2442 | ||
2443 | /* initialise our start point from the cursor */ | |
2444 | cur->bc_ops->init_ptr_from_cur(cur, &rptr); | |
2445 | ||
2446 | /* Allocate the new block. If we can't do it, we're toast. Give up. */ | |
2447 | error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, 1, stat); | |
2448 | if (error) | |
2449 | goto error0; | |
2450 | if (*stat == 0) | |
2451 | goto out0; | |
2452 | XFS_BTREE_STATS_INC(cur, alloc); | |
2453 | ||
2454 | /* Set up the new block. */ | |
2455 | error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp); | |
2456 | if (error) | |
2457 | goto error0; | |
2458 | ||
2459 | /* Set the root in the holding structure increasing the level by 1. */ | |
2460 | cur->bc_ops->set_root(cur, &lptr, 1); | |
2461 | ||
2462 | /* | |
2463 | * At the previous root level there are now two blocks: the old root, | |
2464 | * and the new block generated when it was split. We don't know which | |
2465 | * one the cursor is pointing at, so we set up variables "left" and | |
2466 | * "right" for each case. | |
2467 | */ | |
2468 | block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp); | |
2469 | ||
2470 | #ifdef DEBUG | |
2471 | error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp); | |
2472 | if (error) | |
2473 | goto error0; | |
2474 | #endif | |
2475 | ||
2476 | xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB); | |
2477 | if (!xfs_btree_ptr_is_null(cur, &rptr)) { | |
2478 | /* Our block is left, pick up the right block. */ | |
2479 | lbp = bp; | |
2480 | xfs_btree_buf_to_ptr(cur, lbp, &lptr); | |
2481 | left = block; | |
2482 | error = xfs_btree_read_buf_block(cur, &rptr, | |
2483 | cur->bc_nlevels - 1, 0, &right, &rbp); | |
2484 | if (error) | |
2485 | goto error0; | |
2486 | bp = rbp; | |
2487 | nptr = 1; | |
2488 | } else { | |
2489 | /* Our block is right, pick up the left block. */ | |
2490 | rbp = bp; | |
2491 | xfs_btree_buf_to_ptr(cur, rbp, &rptr); | |
2492 | right = block; | |
2493 | xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB); | |
2494 | error = xfs_btree_read_buf_block(cur, &lptr, | |
2495 | cur->bc_nlevels - 1, 0, &left, &lbp); | |
2496 | if (error) | |
2497 | goto error0; | |
2498 | bp = lbp; | |
2499 | nptr = 2; | |
2500 | } | |
2501 | /* Fill in the new block's btree header and log it. */ | |
2502 | xfs_btree_init_block(cur, cur->bc_nlevels, 2, new); | |
2503 | xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS); | |
2504 | ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) && | |
2505 | !xfs_btree_ptr_is_null(cur, &rptr)); | |
2506 | ||
2507 | /* Fill in the key data in the new root. */ | |
2508 | if (xfs_btree_get_level(left) > 0) { | |
2509 | xfs_btree_copy_keys(cur, | |
2510 | xfs_btree_key_addr(cur, 1, new), | |
2511 | xfs_btree_key_addr(cur, 1, left), 1); | |
2512 | xfs_btree_copy_keys(cur, | |
2513 | xfs_btree_key_addr(cur, 2, new), | |
2514 | xfs_btree_key_addr(cur, 1, right), 1); | |
2515 | } else { | |
2516 | cur->bc_ops->init_key_from_rec( | |
2517 | xfs_btree_key_addr(cur, 1, new), | |
2518 | xfs_btree_rec_addr(cur, 1, left)); | |
2519 | cur->bc_ops->init_key_from_rec( | |
2520 | xfs_btree_key_addr(cur, 2, new), | |
2521 | xfs_btree_rec_addr(cur, 1, right)); | |
2522 | } | |
2523 | xfs_btree_log_keys(cur, nbp, 1, 2); | |
2524 | ||
2525 | /* Fill in the pointer data in the new root. */ | |
2526 | xfs_btree_copy_ptrs(cur, | |
2527 | xfs_btree_ptr_addr(cur, 1, new), &lptr, 1); | |
2528 | xfs_btree_copy_ptrs(cur, | |
2529 | xfs_btree_ptr_addr(cur, 2, new), &rptr, 1); | |
2530 | xfs_btree_log_ptrs(cur, nbp, 1, 2); | |
2531 | ||
2532 | /* Fix up the cursor. */ | |
2533 | xfs_btree_setbuf(cur, cur->bc_nlevels, nbp); | |
2534 | cur->bc_ptrs[cur->bc_nlevels] = nptr; | |
2535 | cur->bc_nlevels++; | |
2536 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
2537 | *stat = 1; | |
2538 | return 0; | |
2539 | error0: | |
2540 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
2541 | return error; | |
2542 | out0: | |
2543 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
2544 | *stat = 0; | |
2545 | return 0; | |
2546 | } | |
4b22a571 CH |
2547 | |
2548 | STATIC int | |
2549 | xfs_btree_make_block_unfull( | |
2550 | struct xfs_btree_cur *cur, /* btree cursor */ | |
2551 | int level, /* btree level */ | |
2552 | int numrecs,/* # of recs in block */ | |
2553 | int *oindex,/* old tree index */ | |
2554 | int *index, /* new tree index */ | |
2555 | union xfs_btree_ptr *nptr, /* new btree ptr */ | |
2556 | struct xfs_btree_cur **ncur, /* new btree cursor */ | |
2557 | union xfs_btree_rec *nrec, /* new record */ | |
2558 | int *stat) | |
2559 | { | |
2560 | union xfs_btree_key key; /* new btree key value */ | |
2561 | int error = 0; | |
2562 | ||
2563 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && | |
2564 | level == cur->bc_nlevels - 1) { | |
2565 | struct xfs_inode *ip = cur->bc_private.b.ip; | |
2566 | ||
2567 | if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) { | |
2568 | /* A root block that can be made bigger. */ | |
2569 | ||
2570 | xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork); | |
2571 | } else { | |
2572 | /* A root block that needs replacing */ | |
2573 | int logflags = 0; | |
2574 | ||
2575 | error = xfs_btree_new_iroot(cur, &logflags, stat); | |
2576 | if (error || *stat == 0) | |
2577 | return error; | |
2578 | ||
2579 | xfs_trans_log_inode(cur->bc_tp, ip, logflags); | |
2580 | } | |
2581 | ||
2582 | return 0; | |
2583 | } | |
2584 | ||
2585 | /* First, try shifting an entry to the right neighbor. */ | |
2586 | error = xfs_btree_rshift(cur, level, stat); | |
2587 | if (error || *stat) | |
2588 | return error; | |
2589 | ||
2590 | /* Next, try shifting an entry to the left neighbor. */ | |
2591 | error = xfs_btree_lshift(cur, level, stat); | |
2592 | if (error) | |
2593 | return error; | |
2594 | ||
2595 | if (*stat) { | |
2596 | *oindex = *index = cur->bc_ptrs[level]; | |
2597 | return 0; | |
2598 | } | |
2599 | ||
2600 | /* | |
2601 | * Next, try splitting the current block in half. | |
2602 | * | |
2603 | * If this works we have to re-set our variables because we | |
2604 | * could be in a different block now. | |
2605 | */ | |
2606 | error = xfs_btree_split(cur, level, nptr, &key, ncur, stat); | |
2607 | if (error || *stat == 0) | |
2608 | return error; | |
2609 | ||
2610 | ||
2611 | *index = cur->bc_ptrs[level]; | |
2612 | cur->bc_ops->init_rec_from_key(&key, nrec); | |
2613 | return 0; | |
2614 | } | |
2615 | ||
2616 | /* | |
2617 | * Insert one record/level. Return information to the caller | |
2618 | * allowing the next level up to proceed if necessary. | |
2619 | */ | |
2620 | STATIC int | |
2621 | xfs_btree_insrec( | |
2622 | struct xfs_btree_cur *cur, /* btree cursor */ | |
2623 | int level, /* level to insert record at */ | |
2624 | union xfs_btree_ptr *ptrp, /* i/o: block number inserted */ | |
2625 | union xfs_btree_rec *recp, /* i/o: record data inserted */ | |
2626 | struct xfs_btree_cur **curp, /* output: new cursor replacing cur */ | |
2627 | int *stat) /* success/failure */ | |
2628 | { | |
2629 | struct xfs_btree_block *block; /* btree block */ | |
2630 | struct xfs_buf *bp; /* buffer for block */ | |
2631 | union xfs_btree_key key; /* btree key */ | |
2632 | union xfs_btree_ptr nptr; /* new block ptr */ | |
2633 | struct xfs_btree_cur *ncur; /* new btree cursor */ | |
2634 | union xfs_btree_rec nrec; /* new record count */ | |
2635 | int optr; /* old key/record index */ | |
2636 | int ptr; /* key/record index */ | |
2637 | int numrecs;/* number of records */ | |
2638 | int error; /* error return value */ | |
2639 | #ifdef DEBUG | |
2640 | int i; | |
2641 | #endif | |
2642 | ||
2643 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
2644 | XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp); | |
2645 | ||
2646 | ncur = NULL; | |
2647 | ||
2648 | /* | |
2649 | * If we have an external root pointer, and we've made it to the | |
2650 | * root level, allocate a new root block and we're done. | |
2651 | */ | |
2652 | if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && | |
2653 | (level >= cur->bc_nlevels)) { | |
2654 | error = xfs_btree_new_root(cur, stat); | |
2655 | xfs_btree_set_ptr_null(cur, ptrp); | |
2656 | ||
2657 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
2658 | return error; | |
2659 | } | |
2660 | ||
2661 | /* If we're off the left edge, return failure. */ | |
2662 | ptr = cur->bc_ptrs[level]; | |
2663 | if (ptr == 0) { | |
2664 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
2665 | *stat = 0; | |
2666 | return 0; | |
2667 | } | |
2668 | ||
2669 | /* Make a key out of the record data to be inserted, and save it. */ | |
2670 | cur->bc_ops->init_key_from_rec(&key, recp); | |
2671 | ||
2672 | optr = ptr; | |
2673 | ||
2674 | XFS_BTREE_STATS_INC(cur, insrec); | |
2675 | ||
2676 | /* Get pointers to the btree buffer and block. */ | |
2677 | block = xfs_btree_get_block(cur, level, &bp); | |
2678 | numrecs = xfs_btree_get_numrecs(block); | |
2679 | ||
2680 | #ifdef DEBUG | |
2681 | error = xfs_btree_check_block(cur, block, level, bp); | |
2682 | if (error) | |
2683 | goto error0; | |
2684 | ||
2685 | /* Check that the new entry is being inserted in the right place. */ | |
2686 | if (ptr <= numrecs) { | |
2687 | if (level == 0) { | |
4a26e66e CH |
2688 | ASSERT(cur->bc_ops->recs_inorder(cur, recp, |
2689 | xfs_btree_rec_addr(cur, ptr, block))); | |
4b22a571 | 2690 | } else { |
4a26e66e CH |
2691 | ASSERT(cur->bc_ops->keys_inorder(cur, &key, |
2692 | xfs_btree_key_addr(cur, ptr, block))); | |
4b22a571 CH |
2693 | } |
2694 | } | |
2695 | #endif | |
2696 | ||
2697 | /* | |
2698 | * If the block is full, we can't insert the new entry until we | |
2699 | * make the block un-full. | |
2700 | */ | |
2701 | xfs_btree_set_ptr_null(cur, &nptr); | |
2702 | if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) { | |
2703 | error = xfs_btree_make_block_unfull(cur, level, numrecs, | |
2704 | &optr, &ptr, &nptr, &ncur, &nrec, stat); | |
2705 | if (error || *stat == 0) | |
2706 | goto error0; | |
2707 | } | |
2708 | ||
2709 | /* | |
2710 | * The current block may have changed if the block was | |
2711 | * previously full and we have just made space in it. | |
2712 | */ | |
2713 | block = xfs_btree_get_block(cur, level, &bp); | |
2714 | numrecs = xfs_btree_get_numrecs(block); | |
2715 | ||
2716 | #ifdef DEBUG | |
2717 | error = xfs_btree_check_block(cur, block, level, bp); | |
2718 | if (error) | |
2719 | return error; | |
2720 | #endif | |
2721 | ||
2722 | /* | |
2723 | * At this point we know there's room for our new entry in the block | |
2724 | * we're pointing at. | |
2725 | */ | |
2726 | XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1); | |
2727 | ||
2728 | if (level > 0) { | |
2729 | /* It's a nonleaf. make a hole in the keys and ptrs */ | |
2730 | union xfs_btree_key *kp; | |
2731 | union xfs_btree_ptr *pp; | |
2732 | ||
2733 | kp = xfs_btree_key_addr(cur, ptr, block); | |
2734 | pp = xfs_btree_ptr_addr(cur, ptr, block); | |
2735 | ||
2736 | #ifdef DEBUG | |
2737 | for (i = numrecs - ptr; i >= 0; i--) { | |
2738 | error = xfs_btree_check_ptr(cur, pp, i, level); | |
2739 | if (error) | |
2740 | return error; | |
2741 | } | |
2742 | #endif | |
2743 | ||
2744 | xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1); | |
2745 | xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1); | |
2746 | ||
2747 | #ifdef DEBUG | |
2748 | error = xfs_btree_check_ptr(cur, ptrp, 0, level); | |
2749 | if (error) | |
2750 | goto error0; | |
2751 | #endif | |
2752 | ||
2753 | /* Now put the new data in, bump numrecs and log it. */ | |
2754 | xfs_btree_copy_keys(cur, kp, &key, 1); | |
2755 | xfs_btree_copy_ptrs(cur, pp, ptrp, 1); | |
2756 | numrecs++; | |
2757 | xfs_btree_set_numrecs(block, numrecs); | |
2758 | xfs_btree_log_ptrs(cur, bp, ptr, numrecs); | |
2759 | xfs_btree_log_keys(cur, bp, ptr, numrecs); | |
2760 | #ifdef DEBUG | |
2761 | if (ptr < numrecs) { | |
4a26e66e CH |
2762 | ASSERT(cur->bc_ops->keys_inorder(cur, kp, |
2763 | xfs_btree_key_addr(cur, ptr + 1, block))); | |
4b22a571 CH |
2764 | } |
2765 | #endif | |
2766 | } else { | |
2767 | /* It's a leaf. make a hole in the records */ | |
2768 | union xfs_btree_rec *rp; | |
2769 | ||
2770 | rp = xfs_btree_rec_addr(cur, ptr, block); | |
2771 | ||
2772 | xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1); | |
2773 | ||
2774 | /* Now put the new data in, bump numrecs and log it. */ | |
2775 | xfs_btree_copy_recs(cur, rp, recp, 1); | |
2776 | xfs_btree_set_numrecs(block, ++numrecs); | |
2777 | xfs_btree_log_recs(cur, bp, ptr, numrecs); | |
2778 | #ifdef DEBUG | |
2779 | if (ptr < numrecs) { | |
4a26e66e CH |
2780 | ASSERT(cur->bc_ops->recs_inorder(cur, rp, |
2781 | xfs_btree_rec_addr(cur, ptr + 1, block))); | |
4b22a571 CH |
2782 | } |
2783 | #endif | |
2784 | } | |
2785 | ||
2786 | /* Log the new number of records in the btree header. */ | |
2787 | xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS); | |
2788 | ||
2789 | /* If we inserted at the start of a block, update the parents' keys. */ | |
2790 | if (optr == 1) { | |
2791 | error = xfs_btree_updkey(cur, &key, level + 1); | |
2792 | if (error) | |
2793 | goto error0; | |
2794 | } | |
2795 | ||
2796 | /* | |
2797 | * If we are tracking the last record in the tree and | |
2798 | * we are at the far right edge of the tree, update it. | |
2799 | */ | |
2800 | if (xfs_btree_is_lastrec(cur, block, level)) { | |
2801 | cur->bc_ops->update_lastrec(cur, block, recp, | |
2802 | ptr, LASTREC_INSREC); | |
2803 | } | |
2804 | ||
2805 | /* | |
2806 | * Return the new block number, if any. | |
2807 | * If there is one, give back a record value and a cursor too. | |
2808 | */ | |
2809 | *ptrp = nptr; | |
2810 | if (!xfs_btree_ptr_is_null(cur, &nptr)) { | |
2811 | *recp = nrec; | |
2812 | *curp = ncur; | |
2813 | } | |
2814 | ||
2815 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
2816 | *stat = 1; | |
2817 | return 0; | |
2818 | ||
2819 | error0: | |
2820 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
2821 | return error; | |
2822 | } | |
2823 | ||
2824 | /* | |
2825 | * Insert the record at the point referenced by cur. | |
2826 | * | |
2827 | * A multi-level split of the tree on insert will invalidate the original | |
2828 | * cursor. All callers of this function should assume that the cursor is | |
2829 | * no longer valid and revalidate it. | |
2830 | */ | |
2831 | int | |
2832 | xfs_btree_insert( | |
2833 | struct xfs_btree_cur *cur, | |
2834 | int *stat) | |
2835 | { | |
2836 | int error; /* error return value */ | |
2837 | int i; /* result value, 0 for failure */ | |
2838 | int level; /* current level number in btree */ | |
2839 | union xfs_btree_ptr nptr; /* new block number (split result) */ | |
2840 | struct xfs_btree_cur *ncur; /* new cursor (split result) */ | |
2841 | struct xfs_btree_cur *pcur; /* previous level's cursor */ | |
2842 | union xfs_btree_rec rec; /* record to insert */ | |
2843 | ||
2844 | level = 0; | |
2845 | ncur = NULL; | |
2846 | pcur = cur; | |
2847 | ||
2848 | xfs_btree_set_ptr_null(cur, &nptr); | |
2849 | cur->bc_ops->init_rec_from_cur(cur, &rec); | |
2850 | ||
2851 | /* | |
2852 | * Loop going up the tree, starting at the leaf level. | |
2853 | * Stop when we don't get a split block, that must mean that | |
2854 | * the insert is finished with this level. | |
2855 | */ | |
2856 | do { | |
2857 | /* | |
2858 | * Insert nrec/nptr into this level of the tree. | |
2859 | * Note if we fail, nptr will be null. | |
2860 | */ | |
2861 | error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i); | |
2862 | if (error) { | |
2863 | if (pcur != cur) | |
2864 | xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR); | |
2865 | goto error0; | |
2866 | } | |
2867 | ||
2868 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
2869 | level++; | |
2870 | ||
2871 | /* | |
2872 | * See if the cursor we just used is trash. | |
2873 | * Can't trash the caller's cursor, but otherwise we should | |
2874 | * if ncur is a new cursor or we're about to be done. | |
2875 | */ | |
2876 | if (pcur != cur && | |
2877 | (ncur || xfs_btree_ptr_is_null(cur, &nptr))) { | |
2878 | /* Save the state from the cursor before we trash it */ | |
2879 | if (cur->bc_ops->update_cursor) | |
2880 | cur->bc_ops->update_cursor(pcur, cur); | |
2881 | cur->bc_nlevels = pcur->bc_nlevels; | |
2882 | xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR); | |
2883 | } | |
2884 | /* If we got a new cursor, switch to it. */ | |
2885 | if (ncur) { | |
2886 | pcur = ncur; | |
2887 | ncur = NULL; | |
2888 | } | |
2889 | } while (!xfs_btree_ptr_is_null(cur, &nptr)); | |
2890 | ||
2891 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
2892 | *stat = i; | |
2893 | return 0; | |
2894 | error0: | |
2895 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
2896 | return error; | |
2897 | } | |
d4b3a4b7 CH |
2898 | |
2899 | /* | |
2900 | * Try to merge a non-leaf block back into the inode root. | |
2901 | * | |
2902 | * Note: the killroot names comes from the fact that we're effectively | |
2903 | * killing the old root block. But because we can't just delete the | |
2904 | * inode we have to copy the single block it was pointing to into the | |
2905 | * inode. | |
2906 | */ | |
d96f8f89 | 2907 | STATIC int |
d4b3a4b7 CH |
2908 | xfs_btree_kill_iroot( |
2909 | struct xfs_btree_cur *cur) | |
2910 | { | |
2911 | int whichfork = cur->bc_private.b.whichfork; | |
2912 | struct xfs_inode *ip = cur->bc_private.b.ip; | |
2913 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); | |
2914 | struct xfs_btree_block *block; | |
2915 | struct xfs_btree_block *cblock; | |
2916 | union xfs_btree_key *kp; | |
2917 | union xfs_btree_key *ckp; | |
2918 | union xfs_btree_ptr *pp; | |
2919 | union xfs_btree_ptr *cpp; | |
2920 | struct xfs_buf *cbp; | |
2921 | int level; | |
2922 | int index; | |
2923 | int numrecs; | |
2924 | #ifdef DEBUG | |
2925 | union xfs_btree_ptr ptr; | |
2926 | int i; | |
2927 | #endif | |
2928 | ||
2929 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
2930 | ||
2931 | ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE); | |
2932 | ASSERT(cur->bc_nlevels > 1); | |
2933 | ||
2934 | /* | |
2935 | * Don't deal with the root block needs to be a leaf case. | |
2936 | * We're just going to turn the thing back into extents anyway. | |
2937 | */ | |
2938 | level = cur->bc_nlevels - 1; | |
2939 | if (level == 1) | |
2940 | goto out0; | |
2941 | ||
2942 | /* | |
2943 | * Give up if the root has multiple children. | |
2944 | */ | |
2945 | block = xfs_btree_get_iroot(cur); | |
2946 | if (xfs_btree_get_numrecs(block) != 1) | |
2947 | goto out0; | |
2948 | ||
2949 | cblock = xfs_btree_get_block(cur, level - 1, &cbp); | |
2950 | numrecs = xfs_btree_get_numrecs(cblock); | |
2951 | ||
2952 | /* | |
2953 | * Only do this if the next level will fit. | |
2954 | * Then the data must be copied up to the inode, | |
2955 | * instead of freeing the root you free the next level. | |
2956 | */ | |
2957 | if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level)) | |
2958 | goto out0; | |
2959 | ||
2960 | XFS_BTREE_STATS_INC(cur, killroot); | |
2961 | ||
2962 | #ifdef DEBUG | |
2963 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB); | |
2964 | ASSERT(xfs_btree_ptr_is_null(cur, &ptr)); | |
2965 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB); | |
2966 | ASSERT(xfs_btree_ptr_is_null(cur, &ptr)); | |
2967 | #endif | |
2968 | ||
2969 | index = numrecs - cur->bc_ops->get_maxrecs(cur, level); | |
2970 | if (index) { | |
2971 | xfs_iroot_realloc(cur->bc_private.b.ip, index, | |
2972 | cur->bc_private.b.whichfork); | |
7cc95a82 | 2973 | block = ifp->if_broot; |
d4b3a4b7 CH |
2974 | } |
2975 | ||
2976 | be16_add_cpu(&block->bb_numrecs, index); | |
2977 | ASSERT(block->bb_numrecs == cblock->bb_numrecs); | |
2978 | ||
2979 | kp = xfs_btree_key_addr(cur, 1, block); | |
2980 | ckp = xfs_btree_key_addr(cur, 1, cblock); | |
2981 | xfs_btree_copy_keys(cur, kp, ckp, numrecs); | |
2982 | ||
2983 | pp = xfs_btree_ptr_addr(cur, 1, block); | |
2984 | cpp = xfs_btree_ptr_addr(cur, 1, cblock); | |
2985 | #ifdef DEBUG | |
2986 | for (i = 0; i < numrecs; i++) { | |
2987 | int error; | |
2988 | ||
2989 | error = xfs_btree_check_ptr(cur, cpp, i, level - 1); | |
2990 | if (error) { | |
2991 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
2992 | return error; | |
2993 | } | |
2994 | } | |
2995 | #endif | |
2996 | xfs_btree_copy_ptrs(cur, pp, cpp, numrecs); | |
2997 | ||
2998 | cur->bc_ops->free_block(cur, cbp); | |
2999 | XFS_BTREE_STATS_INC(cur, free); | |
3000 | ||
3001 | cur->bc_bufs[level - 1] = NULL; | |
3002 | be16_add_cpu(&block->bb_level, -1); | |
3003 | xfs_trans_log_inode(cur->bc_tp, ip, | |
9d87c319 | 3004 | XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork)); |
d4b3a4b7 CH |
3005 | cur->bc_nlevels--; |
3006 | out0: | |
3007 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
3008 | return 0; | |
3009 | } | |
91cca5df | 3010 | |
c0e59e1a CH |
3011 | /* |
3012 | * Kill the current root node, and replace it with it's only child node. | |
3013 | */ | |
3014 | STATIC int | |
3015 | xfs_btree_kill_root( | |
3016 | struct xfs_btree_cur *cur, | |
3017 | struct xfs_buf *bp, | |
3018 | int level, | |
3019 | union xfs_btree_ptr *newroot) | |
3020 | { | |
3021 | int error; | |
3022 | ||
3023 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
3024 | XFS_BTREE_STATS_INC(cur, killroot); | |
3025 | ||
3026 | /* | |
3027 | * Update the root pointer, decreasing the level by 1 and then | |
3028 | * free the old root. | |
3029 | */ | |
3030 | cur->bc_ops->set_root(cur, newroot, -1); | |
3031 | ||
3032 | error = cur->bc_ops->free_block(cur, bp); | |
3033 | if (error) { | |
3034 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
3035 | return error; | |
3036 | } | |
3037 | ||
3038 | XFS_BTREE_STATS_INC(cur, free); | |
3039 | ||
3040 | cur->bc_bufs[level] = NULL; | |
3041 | cur->bc_ra[level] = 0; | |
3042 | cur->bc_nlevels--; | |
3043 | ||
3044 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
3045 | return 0; | |
3046 | } | |
3047 | ||
91cca5df CH |
3048 | STATIC int |
3049 | xfs_btree_dec_cursor( | |
3050 | struct xfs_btree_cur *cur, | |
3051 | int level, | |
3052 | int *stat) | |
3053 | { | |
3054 | int error; | |
3055 | int i; | |
3056 | ||
3057 | if (level > 0) { | |
3058 | error = xfs_btree_decrement(cur, level, &i); | |
3059 | if (error) | |
3060 | return error; | |
3061 | } | |
3062 | ||
3063 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
3064 | *stat = 1; | |
3065 | return 0; | |
3066 | } | |
3067 | ||
3068 | /* | |
3069 | * Single level of the btree record deletion routine. | |
3070 | * Delete record pointed to by cur/level. | |
3071 | * Remove the record from its block then rebalance the tree. | |
3072 | * Return 0 for error, 1 for done, 2 to go on to the next level. | |
3073 | */ | |
3074 | STATIC int /* error */ | |
3075 | xfs_btree_delrec( | |
3076 | struct xfs_btree_cur *cur, /* btree cursor */ | |
3077 | int level, /* level removing record from */ | |
3078 | int *stat) /* fail/done/go-on */ | |
3079 | { | |
3080 | struct xfs_btree_block *block; /* btree block */ | |
3081 | union xfs_btree_ptr cptr; /* current block ptr */ | |
3082 | struct xfs_buf *bp; /* buffer for block */ | |
3083 | int error; /* error return value */ | |
3084 | int i; /* loop counter */ | |
3085 | union xfs_btree_key key; /* storage for keyp */ | |
3086 | union xfs_btree_key *keyp = &key; /* passed to the next level */ | |
3087 | union xfs_btree_ptr lptr; /* left sibling block ptr */ | |
3088 | struct xfs_buf *lbp; /* left buffer pointer */ | |
3089 | struct xfs_btree_block *left; /* left btree block */ | |
3090 | int lrecs = 0; /* left record count */ | |
3091 | int ptr; /* key/record index */ | |
3092 | union xfs_btree_ptr rptr; /* right sibling block ptr */ | |
3093 | struct xfs_buf *rbp; /* right buffer pointer */ | |
3094 | struct xfs_btree_block *right; /* right btree block */ | |
3095 | struct xfs_btree_block *rrblock; /* right-right btree block */ | |
3096 | struct xfs_buf *rrbp; /* right-right buffer pointer */ | |
3097 | int rrecs = 0; /* right record count */ | |
3098 | struct xfs_btree_cur *tcur; /* temporary btree cursor */ | |
3099 | int numrecs; /* temporary numrec count */ | |
3100 | ||
3101 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
3102 | XFS_BTREE_TRACE_ARGI(cur, level); | |
3103 | ||
3104 | tcur = NULL; | |
3105 | ||
3106 | /* Get the index of the entry being deleted, check for nothing there. */ | |
3107 | ptr = cur->bc_ptrs[level]; | |
3108 | if (ptr == 0) { | |
3109 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
3110 | *stat = 0; | |
3111 | return 0; | |
3112 | } | |
3113 | ||
3114 | /* Get the buffer & block containing the record or key/ptr. */ | |
3115 | block = xfs_btree_get_block(cur, level, &bp); | |
3116 | numrecs = xfs_btree_get_numrecs(block); | |
3117 | ||
3118 | #ifdef DEBUG | |
3119 | error = xfs_btree_check_block(cur, block, level, bp); | |
3120 | if (error) | |
3121 | goto error0; | |
3122 | #endif | |
3123 | ||
3124 | /* Fail if we're off the end of the block. */ | |
3125 | if (ptr > numrecs) { | |
3126 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
3127 | *stat = 0; | |
3128 | return 0; | |
3129 | } | |
3130 | ||
3131 | XFS_BTREE_STATS_INC(cur, delrec); | |
3132 | XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr); | |
3133 | ||
3134 | /* Excise the entries being deleted. */ | |
3135 | if (level > 0) { | |
3136 | /* It's a nonleaf. operate on keys and ptrs */ | |
3137 | union xfs_btree_key *lkp; | |
3138 | union xfs_btree_ptr *lpp; | |
3139 | ||
3140 | lkp = xfs_btree_key_addr(cur, ptr + 1, block); | |
3141 | lpp = xfs_btree_ptr_addr(cur, ptr + 1, block); | |
3142 | ||
3143 | #ifdef DEBUG | |
3144 | for (i = 0; i < numrecs - ptr; i++) { | |
3145 | error = xfs_btree_check_ptr(cur, lpp, i, level); | |
3146 | if (error) | |
3147 | goto error0; | |
3148 | } | |
3149 | #endif | |
3150 | ||
3151 | if (ptr < numrecs) { | |
3152 | xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr); | |
3153 | xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr); | |
3154 | xfs_btree_log_keys(cur, bp, ptr, numrecs - 1); | |
3155 | xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1); | |
3156 | } | |
3157 | ||
3158 | /* | |
3159 | * If it's the first record in the block, we'll need to pass a | |
3160 | * key up to the next level (updkey). | |
3161 | */ | |
3162 | if (ptr == 1) | |
3163 | keyp = xfs_btree_key_addr(cur, 1, block); | |
3164 | } else { | |
3165 | /* It's a leaf. operate on records */ | |
3166 | if (ptr < numrecs) { | |
3167 | xfs_btree_shift_recs(cur, | |
3168 | xfs_btree_rec_addr(cur, ptr + 1, block), | |
3169 | -1, numrecs - ptr); | |
3170 | xfs_btree_log_recs(cur, bp, ptr, numrecs - 1); | |
3171 | } | |
3172 | ||
3173 | /* | |
3174 | * If it's the first record in the block, we'll need a key | |
3175 | * structure to pass up to the next level (updkey). | |
3176 | */ | |
3177 | if (ptr == 1) { | |
3178 | cur->bc_ops->init_key_from_rec(&key, | |
3179 | xfs_btree_rec_addr(cur, 1, block)); | |
3180 | keyp = &key; | |
3181 | } | |
3182 | } | |
3183 | ||
3184 | /* | |
3185 | * Decrement and log the number of entries in the block. | |
3186 | */ | |
3187 | xfs_btree_set_numrecs(block, --numrecs); | |
3188 | xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS); | |
3189 | ||
3190 | /* | |
3191 | * If we are tracking the last record in the tree and | |
3192 | * we are at the far right edge of the tree, update it. | |
3193 | */ | |
3194 | if (xfs_btree_is_lastrec(cur, block, level)) { | |
3195 | cur->bc_ops->update_lastrec(cur, block, NULL, | |
3196 | ptr, LASTREC_DELREC); | |
3197 | } | |
3198 | ||
3199 | /* | |
3200 | * We're at the root level. First, shrink the root block in-memory. | |
3201 | * Try to get rid of the next level down. If we can't then there's | |
3202 | * nothing left to do. | |
3203 | */ | |
3204 | if (level == cur->bc_nlevels - 1) { | |
3205 | if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) { | |
3206 | xfs_iroot_realloc(cur->bc_private.b.ip, -1, | |
3207 | cur->bc_private.b.whichfork); | |
3208 | ||
3209 | error = xfs_btree_kill_iroot(cur); | |
3210 | if (error) | |
3211 | goto error0; | |
3212 | ||
3213 | error = xfs_btree_dec_cursor(cur, level, stat); | |
3214 | if (error) | |
3215 | goto error0; | |
3216 | *stat = 1; | |
3217 | return 0; | |
3218 | } | |
3219 | ||
3220 | /* | |
3221 | * If this is the root level, and there's only one entry left, | |
3222 | * and it's NOT the leaf level, then we can get rid of this | |
3223 | * level. | |
3224 | */ | |
3225 | if (numrecs == 1 && level > 0) { | |
3226 | union xfs_btree_ptr *pp; | |
3227 | /* | |
3228 | * pp is still set to the first pointer in the block. | |
3229 | * Make it the new root of the btree. | |
3230 | */ | |
3231 | pp = xfs_btree_ptr_addr(cur, 1, block); | |
c0e59e1a | 3232 | error = xfs_btree_kill_root(cur, bp, level, pp); |
91cca5df CH |
3233 | if (error) |
3234 | goto error0; | |
3235 | } else if (level > 0) { | |
3236 | error = xfs_btree_dec_cursor(cur, level, stat); | |
3237 | if (error) | |
3238 | goto error0; | |
3239 | } | |
3240 | *stat = 1; | |
3241 | return 0; | |
3242 | } | |
3243 | ||
3244 | /* | |
3245 | * If we deleted the leftmost entry in the block, update the | |
3246 | * key values above us in the tree. | |
3247 | */ | |
3248 | if (ptr == 1) { | |
3249 | error = xfs_btree_updkey(cur, keyp, level + 1); | |
3250 | if (error) | |
3251 | goto error0; | |
3252 | } | |
3253 | ||
3254 | /* | |
3255 | * If the number of records remaining in the block is at least | |
3256 | * the minimum, we're done. | |
3257 | */ | |
3258 | if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) { | |
3259 | error = xfs_btree_dec_cursor(cur, level, stat); | |
3260 | if (error) | |
3261 | goto error0; | |
3262 | return 0; | |
3263 | } | |
3264 | ||
3265 | /* | |
3266 | * Otherwise, we have to move some records around to keep the | |
3267 | * tree balanced. Look at the left and right sibling blocks to | |
3268 | * see if we can re-balance by moving only one record. | |
3269 | */ | |
3270 | xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB); | |
3271 | xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB); | |
3272 | ||
3273 | if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) { | |
3274 | /* | |
3275 | * One child of root, need to get a chance to copy its contents | |
3276 | * into the root and delete it. Can't go up to next level, | |
3277 | * there's nothing to delete there. | |
3278 | */ | |
3279 | if (xfs_btree_ptr_is_null(cur, &rptr) && | |
3280 | xfs_btree_ptr_is_null(cur, &lptr) && | |
3281 | level == cur->bc_nlevels - 2) { | |
3282 | error = xfs_btree_kill_iroot(cur); | |
3283 | if (!error) | |
3284 | error = xfs_btree_dec_cursor(cur, level, stat); | |
3285 | if (error) | |
3286 | goto error0; | |
3287 | return 0; | |
3288 | } | |
3289 | } | |
3290 | ||
3291 | ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) || | |
3292 | !xfs_btree_ptr_is_null(cur, &lptr)); | |
3293 | ||
3294 | /* | |
3295 | * Duplicate the cursor so our btree manipulations here won't | |
3296 | * disrupt the next level up. | |
3297 | */ | |
3298 | error = xfs_btree_dup_cursor(cur, &tcur); | |
3299 | if (error) | |
3300 | goto error0; | |
3301 | ||
3302 | /* | |
3303 | * If there's a right sibling, see if it's ok to shift an entry | |
3304 | * out of it. | |
3305 | */ | |
3306 | if (!xfs_btree_ptr_is_null(cur, &rptr)) { | |
3307 | /* | |
3308 | * Move the temp cursor to the last entry in the next block. | |
3309 | * Actually any entry but the first would suffice. | |
3310 | */ | |
3311 | i = xfs_btree_lastrec(tcur, level); | |
3312 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
3313 | ||
3314 | error = xfs_btree_increment(tcur, level, &i); | |
3315 | if (error) | |
3316 | goto error0; | |
3317 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
3318 | ||
3319 | i = xfs_btree_lastrec(tcur, level); | |
3320 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
3321 | ||
3322 | /* Grab a pointer to the block. */ | |
3323 | right = xfs_btree_get_block(tcur, level, &rbp); | |
3324 | #ifdef DEBUG | |
3325 | error = xfs_btree_check_block(tcur, right, level, rbp); | |
3326 | if (error) | |
3327 | goto error0; | |
3328 | #endif | |
3329 | /* Grab the current block number, for future use. */ | |
3330 | xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB); | |
3331 | ||
3332 | /* | |
3333 | * If right block is full enough so that removing one entry | |
3334 | * won't make it too empty, and left-shifting an entry out | |
3335 | * of right to us works, we're done. | |
3336 | */ | |
3337 | if (xfs_btree_get_numrecs(right) - 1 >= | |
3338 | cur->bc_ops->get_minrecs(tcur, level)) { | |
3339 | error = xfs_btree_lshift(tcur, level, &i); | |
3340 | if (error) | |
3341 | goto error0; | |
3342 | if (i) { | |
3343 | ASSERT(xfs_btree_get_numrecs(block) >= | |
3344 | cur->bc_ops->get_minrecs(tcur, level)); | |
3345 | ||
3346 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); | |
3347 | tcur = NULL; | |
3348 | ||
3349 | error = xfs_btree_dec_cursor(cur, level, stat); | |
3350 | if (error) | |
3351 | goto error0; | |
3352 | return 0; | |
3353 | } | |
3354 | } | |
3355 | ||
3356 | /* | |
3357 | * Otherwise, grab the number of records in right for | |
3358 | * future reference, and fix up the temp cursor to point | |
3359 | * to our block again (last record). | |
3360 | */ | |
3361 | rrecs = xfs_btree_get_numrecs(right); | |
3362 | if (!xfs_btree_ptr_is_null(cur, &lptr)) { | |
3363 | i = xfs_btree_firstrec(tcur, level); | |
3364 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
3365 | ||
3366 | error = xfs_btree_decrement(tcur, level, &i); | |
3367 | if (error) | |
3368 | goto error0; | |
3369 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
3370 | } | |
3371 | } | |
3372 | ||
3373 | /* | |
3374 | * If there's a left sibling, see if it's ok to shift an entry | |
3375 | * out of it. | |
3376 | */ | |
3377 | if (!xfs_btree_ptr_is_null(cur, &lptr)) { | |
3378 | /* | |
3379 | * Move the temp cursor to the first entry in the | |
3380 | * previous block. | |
3381 | */ | |
3382 | i = xfs_btree_firstrec(tcur, level); | |
3383 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
3384 | ||
3385 | error = xfs_btree_decrement(tcur, level, &i); | |
3386 | if (error) | |
3387 | goto error0; | |
3388 | i = xfs_btree_firstrec(tcur, level); | |
3389 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | |
3390 | ||
3391 | /* Grab a pointer to the block. */ | |
3392 | left = xfs_btree_get_block(tcur, level, &lbp); | |
3393 | #ifdef DEBUG | |
3394 | error = xfs_btree_check_block(cur, left, level, lbp); | |
3395 | if (error) | |
3396 | goto error0; | |
3397 | #endif | |
3398 | /* Grab the current block number, for future use. */ | |
3399 | xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB); | |
3400 | ||
3401 | /* | |
3402 | * If left block is full enough so that removing one entry | |
3403 | * won't make it too empty, and right-shifting an entry out | |
3404 | * of left to us works, we're done. | |
3405 | */ | |
3406 | if (xfs_btree_get_numrecs(left) - 1 >= | |
3407 | cur->bc_ops->get_minrecs(tcur, level)) { | |
3408 | error = xfs_btree_rshift(tcur, level, &i); | |
3409 | if (error) | |
3410 | goto error0; | |
3411 | if (i) { | |
3412 | ASSERT(xfs_btree_get_numrecs(block) >= | |
3413 | cur->bc_ops->get_minrecs(tcur, level)); | |
3414 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); | |
3415 | tcur = NULL; | |
3416 | if (level == 0) | |
3417 | cur->bc_ptrs[0]++; | |
3418 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
3419 | *stat = 1; | |
3420 | return 0; | |
3421 | } | |
3422 | } | |
3423 | ||
3424 | /* | |
3425 | * Otherwise, grab the number of records in right for | |
3426 | * future reference. | |
3427 | */ | |
3428 | lrecs = xfs_btree_get_numrecs(left); | |
3429 | } | |
3430 | ||
3431 | /* Delete the temp cursor, we're done with it. */ | |
3432 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); | |
3433 | tcur = NULL; | |
3434 | ||
3435 | /* If here, we need to do a join to keep the tree balanced. */ | |
3436 | ASSERT(!xfs_btree_ptr_is_null(cur, &cptr)); | |
3437 | ||
3438 | if (!xfs_btree_ptr_is_null(cur, &lptr) && | |
3439 | lrecs + xfs_btree_get_numrecs(block) <= | |
3440 | cur->bc_ops->get_maxrecs(cur, level)) { | |
3441 | /* | |
3442 | * Set "right" to be the starting block, | |
3443 | * "left" to be the left neighbor. | |
3444 | */ | |
3445 | rptr = cptr; | |
3446 | right = block; | |
3447 | rbp = bp; | |
3448 | error = xfs_btree_read_buf_block(cur, &lptr, level, | |
3449 | 0, &left, &lbp); | |
3450 | if (error) | |
3451 | goto error0; | |
3452 | ||
3453 | /* | |
3454 | * If that won't work, see if we can join with the right neighbor block. | |
3455 | */ | |
3456 | } else if (!xfs_btree_ptr_is_null(cur, &rptr) && | |
3457 | rrecs + xfs_btree_get_numrecs(block) <= | |
3458 | cur->bc_ops->get_maxrecs(cur, level)) { | |
3459 | /* | |
3460 | * Set "left" to be the starting block, | |
3461 | * "right" to be the right neighbor. | |
3462 | */ | |
3463 | lptr = cptr; | |
3464 | left = block; | |
3465 | lbp = bp; | |
3466 | error = xfs_btree_read_buf_block(cur, &rptr, level, | |
3467 | 0, &right, &rbp); | |
3468 | if (error) | |
3469 | goto error0; | |
3470 | ||
3471 | /* | |
3472 | * Otherwise, we can't fix the imbalance. | |
3473 | * Just return. This is probably a logic error, but it's not fatal. | |
3474 | */ | |
3475 | } else { | |
3476 | error = xfs_btree_dec_cursor(cur, level, stat); | |
3477 | if (error) | |
3478 | goto error0; | |
3479 | return 0; | |
3480 | } | |
3481 | ||
3482 | rrecs = xfs_btree_get_numrecs(right); | |
3483 | lrecs = xfs_btree_get_numrecs(left); | |
3484 | ||
3485 | /* | |
3486 | * We're now going to join "left" and "right" by moving all the stuff | |
3487 | * in "right" to "left" and deleting "right". | |
3488 | */ | |
3489 | XFS_BTREE_STATS_ADD(cur, moves, rrecs); | |
3490 | if (level > 0) { | |
3491 | /* It's a non-leaf. Move keys and pointers. */ | |
3492 | union xfs_btree_key *lkp; /* left btree key */ | |
3493 | union xfs_btree_ptr *lpp; /* left address pointer */ | |
3494 | union xfs_btree_key *rkp; /* right btree key */ | |
3495 | union xfs_btree_ptr *rpp; /* right address pointer */ | |
3496 | ||
3497 | lkp = xfs_btree_key_addr(cur, lrecs + 1, left); | |
3498 | lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left); | |
3499 | rkp = xfs_btree_key_addr(cur, 1, right); | |
3500 | rpp = xfs_btree_ptr_addr(cur, 1, right); | |
3501 | #ifdef DEBUG | |
3502 | for (i = 1; i < rrecs; i++) { | |
3503 | error = xfs_btree_check_ptr(cur, rpp, i, level); | |
3504 | if (error) | |
3505 | goto error0; | |
3506 | } | |
3507 | #endif | |
3508 | xfs_btree_copy_keys(cur, lkp, rkp, rrecs); | |
3509 | xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs); | |
3510 | ||
3511 | xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs); | |
3512 | xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs); | |
3513 | } else { | |
3514 | /* It's a leaf. Move records. */ | |
3515 | union xfs_btree_rec *lrp; /* left record pointer */ | |
3516 | union xfs_btree_rec *rrp; /* right record pointer */ | |
3517 | ||
3518 | lrp = xfs_btree_rec_addr(cur, lrecs + 1, left); | |
3519 | rrp = xfs_btree_rec_addr(cur, 1, right); | |
3520 | ||
3521 | xfs_btree_copy_recs(cur, lrp, rrp, rrecs); | |
3522 | xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs); | |
3523 | } | |
3524 | ||
3525 | XFS_BTREE_STATS_INC(cur, join); | |
3526 | ||
3527 | /* | |
9da096fd | 3528 | * Fix up the number of records and right block pointer in the |
91cca5df CH |
3529 | * surviving block, and log it. |
3530 | */ | |
3531 | xfs_btree_set_numrecs(left, lrecs + rrecs); | |
3532 | xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB), | |
3533 | xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB); | |
3534 | xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB); | |
3535 | ||
3536 | /* If there is a right sibling, point it to the remaining block. */ | |
3537 | xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB); | |
3538 | if (!xfs_btree_ptr_is_null(cur, &cptr)) { | |
3539 | error = xfs_btree_read_buf_block(cur, &cptr, level, | |
3540 | 0, &rrblock, &rrbp); | |
3541 | if (error) | |
3542 | goto error0; | |
3543 | xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB); | |
3544 | xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB); | |
3545 | } | |
3546 | ||
3547 | /* Free the deleted block. */ | |
3548 | error = cur->bc_ops->free_block(cur, rbp); | |
3549 | if (error) | |
3550 | goto error0; | |
3551 | XFS_BTREE_STATS_INC(cur, free); | |
3552 | ||
3553 | /* | |
3554 | * If we joined with the left neighbor, set the buffer in the | |
3555 | * cursor to the left block, and fix up the index. | |
3556 | */ | |
3557 | if (bp != lbp) { | |
3558 | cur->bc_bufs[level] = lbp; | |
3559 | cur->bc_ptrs[level] += lrecs; | |
3560 | cur->bc_ra[level] = 0; | |
3561 | } | |
3562 | /* | |
3563 | * If we joined with the right neighbor and there's a level above | |
3564 | * us, increment the cursor at that level. | |
3565 | */ | |
3566 | else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || | |
3567 | (level + 1 < cur->bc_nlevels)) { | |
3568 | error = xfs_btree_increment(cur, level + 1, &i); | |
3569 | if (error) | |
3570 | goto error0; | |
3571 | } | |
3572 | ||
3573 | /* | |
3574 | * Readjust the ptr at this level if it's not a leaf, since it's | |
3575 | * still pointing at the deletion point, which makes the cursor | |
3576 | * inconsistent. If this makes the ptr 0, the caller fixes it up. | |
3577 | * We can't use decrement because it would change the next level up. | |
3578 | */ | |
3579 | if (level > 0) | |
3580 | cur->bc_ptrs[level]--; | |
3581 | ||
3582 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
3583 | /* Return value means the next level up has something to do. */ | |
3584 | *stat = 2; | |
3585 | return 0; | |
3586 | ||
3587 | error0: | |
3588 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
3589 | if (tcur) | |
3590 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); | |
3591 | return error; | |
3592 | } | |
3593 | ||
3594 | /* | |
3595 | * Delete the record pointed to by cur. | |
3596 | * The cursor refers to the place where the record was (could be inserted) | |
3597 | * when the operation returns. | |
3598 | */ | |
3599 | int /* error */ | |
3600 | xfs_btree_delete( | |
3601 | struct xfs_btree_cur *cur, | |
3602 | int *stat) /* success/failure */ | |
3603 | { | |
3604 | int error; /* error return value */ | |
3605 | int level; | |
3606 | int i; | |
3607 | ||
3608 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | |
3609 | ||
3610 | /* | |
3611 | * Go up the tree, starting at leaf level. | |
3612 | * | |
3613 | * If 2 is returned then a join was done; go to the next level. | |
3614 | * Otherwise we are done. | |
3615 | */ | |
3616 | for (level = 0, i = 2; i == 2; level++) { | |
3617 | error = xfs_btree_delrec(cur, level, &i); | |
3618 | if (error) | |
3619 | goto error0; | |
3620 | } | |
3621 | ||
3622 | if (i == 0) { | |
3623 | for (level = 1; level < cur->bc_nlevels; level++) { | |
3624 | if (cur->bc_ptrs[level] == 0) { | |
3625 | error = xfs_btree_decrement(cur, level, &i); | |
3626 | if (error) | |
3627 | goto error0; | |
3628 | break; | |
3629 | } | |
3630 | } | |
3631 | } | |
3632 | ||
3633 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
3634 | *stat = i; | |
3635 | return 0; | |
3636 | error0: | |
3637 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
3638 | return error; | |
3639 | } | |
8cc938fe CH |
3640 | |
3641 | /* | |
3642 | * Get the data from the pointed-to record. | |
3643 | */ | |
3644 | int /* error */ | |
3645 | xfs_btree_get_rec( | |
3646 | struct xfs_btree_cur *cur, /* btree cursor */ | |
3647 | union xfs_btree_rec **recp, /* output: btree record */ | |
3648 | int *stat) /* output: success/failure */ | |
3649 | { | |
3650 | struct xfs_btree_block *block; /* btree block */ | |
3651 | struct xfs_buf *bp; /* buffer pointer */ | |
3652 | int ptr; /* record number */ | |
3653 | #ifdef DEBUG | |
3654 | int error; /* error return value */ | |
3655 | #endif | |
3656 | ||
3657 | ptr = cur->bc_ptrs[0]; | |
3658 | block = xfs_btree_get_block(cur, 0, &bp); | |
3659 | ||
3660 | #ifdef DEBUG | |
3661 | error = xfs_btree_check_block(cur, block, 0, bp); | |
3662 | if (error) | |
3663 | return error; | |
3664 | #endif | |
3665 | ||
3666 | /* | |
3667 | * Off the right end or left end, return failure. | |
3668 | */ | |
3669 | if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) { | |
3670 | *stat = 0; | |
3671 | return 0; | |
3672 | } | |
3673 | ||
3674 | /* | |
3675 | * Point to the record and extract its data. | |
3676 | */ | |
3677 | *recp = xfs_btree_rec_addr(cur, ptr, block); | |
3678 | *stat = 1; | |
3679 | return 0; | |
3680 | } |