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