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