]>
Commit | Line | Data |
---|---|---|
37b3b4d6 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2bd0ea18 | 2 | /* |
da23017d | 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
88b32f06 | 4 | * Copyright (c) 2013 Red Hat, Inc. |
da23017d | 5 | * All Rights Reserved. |
2bd0ea18 | 6 | */ |
9c799827 | 7 | #include "libxfs_priv.h" |
b626fb59 DC |
8 | #include "xfs_fs.h" |
9 | #include "xfs_shared.h" | |
10 | #include "xfs_format.h" | |
11 | #include "xfs_log_format.h" | |
12 | #include "xfs_trans_resv.h" | |
13 | #include "xfs_bit.h" | |
14 | #include "xfs_mount.h" | |
15 | #include "xfs_da_format.h" | |
16 | #include "xfs_da_btree.h" | |
17 | #include "xfs_dir2.h" | |
18 | #include "xfs_dir2_priv.h" | |
19 | #include "xfs_inode.h" | |
20 | #include "xfs_trans.h" | |
21 | #include "xfs_alloc.h" | |
22 | #include "xfs_bmap.h" | |
23 | #include "xfs_attr_leaf.h" | |
24 | #include "xfs_trace.h" | |
25 | #include "xfs_cksum.h" | |
2bd0ea18 NS |
26 | |
27 | /* | |
28 | * xfs_da_btree.c | |
29 | * | |
30 | * Routines to implement directories as Btrees of hashed names. | |
31 | */ | |
32 | ||
5e656dbb BN |
33 | /*======================================================================== |
34 | * Function prototypes for the kernel. | |
35 | *========================================================================*/ | |
36 | ||
37 | /* | |
38 | * Routines used for growing the Btree. | |
39 | */ | |
88b32f06 | 40 | STATIC int xfs_da3_root_split(xfs_da_state_t *state, |
5e656dbb BN |
41 | xfs_da_state_blk_t *existing_root, |
42 | xfs_da_state_blk_t *new_child); | |
88b32f06 | 43 | STATIC int xfs_da3_node_split(xfs_da_state_t *state, |
5e656dbb BN |
44 | xfs_da_state_blk_t *existing_blk, |
45 | xfs_da_state_blk_t *split_blk, | |
46 | xfs_da_state_blk_t *blk_to_add, | |
47 | int treelevel, | |
48 | int *result); | |
88b32f06 | 49 | STATIC void xfs_da3_node_rebalance(xfs_da_state_t *state, |
5e656dbb BN |
50 | xfs_da_state_blk_t *node_blk_1, |
51 | xfs_da_state_blk_t *node_blk_2); | |
88b32f06 | 52 | STATIC void xfs_da3_node_add(xfs_da_state_t *state, |
5e656dbb BN |
53 | xfs_da_state_blk_t *old_node_blk, |
54 | xfs_da_state_blk_t *new_node_blk); | |
55 | ||
56 | /* | |
57 | * Routines used for shrinking the Btree. | |
58 | */ | |
88b32f06 | 59 | STATIC int xfs_da3_root_join(xfs_da_state_t *state, |
5e656dbb | 60 | xfs_da_state_blk_t *root_blk); |
88b32f06 DC |
61 | STATIC int xfs_da3_node_toosmall(xfs_da_state_t *state, int *retval); |
62 | STATIC void xfs_da3_node_remove(xfs_da_state_t *state, | |
5e656dbb | 63 | xfs_da_state_blk_t *drop_blk); |
88b32f06 | 64 | STATIC void xfs_da3_node_unbalance(xfs_da_state_t *state, |
5e656dbb BN |
65 | xfs_da_state_blk_t *src_node_blk, |
66 | xfs_da_state_blk_t *dst_node_blk); | |
67 | ||
68 | /* | |
69 | * Utility routines. | |
70 | */ | |
88b32f06 | 71 | STATIC int xfs_da3_blk_unlink(xfs_da_state_t *state, |
5e656dbb BN |
72 | xfs_da_state_blk_t *drop_blk, |
73 | xfs_da_state_blk_t *save_blk); | |
5e656dbb | 74 | |
88b32f06 DC |
75 | |
76 | kmem_zone_t *xfs_da_state_zone; /* anchor for state struct zone */ | |
77 | ||
78 | /* | |
79 | * Allocate a dir-state structure. | |
80 | * We don't put them on the stack since they're large. | |
81 | */ | |
82 | xfs_da_state_t * | |
83 | xfs_da_state_alloc(void) | |
84 | { | |
85 | return kmem_zone_zalloc(xfs_da_state_zone, KM_NOFS); | |
86 | } | |
87 | ||
88 | /* | |
89 | * Kill the altpath contents of a da-state structure. | |
90 | */ | |
91 | STATIC void | |
92 | xfs_da_state_kill_altpath(xfs_da_state_t *state) | |
93 | { | |
94 | int i; | |
95 | ||
96 | for (i = 0; i < state->altpath.active; i++) | |
97 | state->altpath.blk[i].bp = NULL; | |
98 | state->altpath.active = 0; | |
99 | } | |
100 | ||
101 | /* | |
102 | * Free a da-state structure. | |
103 | */ | |
104 | void | |
105 | xfs_da_state_free(xfs_da_state_t *state) | |
106 | { | |
107 | xfs_da_state_kill_altpath(state); | |
108 | #ifdef DEBUG | |
109 | memset((char *)state, 0, sizeof(*state)); | |
110 | #endif /* DEBUG */ | |
111 | kmem_zone_free(xfs_da_state_zone, state); | |
112 | } | |
113 | ||
bc01119d | 114 | static xfs_failaddr_t |
88b32f06 | 115 | xfs_da3_node_verify( |
a2ceac1f DC |
116 | struct xfs_buf *bp) |
117 | { | |
118 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
88b32f06 DC |
119 | struct xfs_da_intnode *hdr = bp->b_addr; |
120 | struct xfs_da3_icnode_hdr ichdr; | |
ff105f75 DC |
121 | const struct xfs_dir_ops *ops; |
122 | ||
123 | ops = xfs_dir_get_ops(mp, NULL); | |
88b32f06 | 124 | |
ff105f75 | 125 | ops->node_hdr_from_disk(&ichdr, hdr); |
88b32f06 DC |
126 | |
127 | if (xfs_sb_version_hascrc(&mp->m_sb)) { | |
128 | struct xfs_da3_node_hdr *hdr3 = bp->b_addr; | |
129 | ||
130 | if (ichdr.magic != XFS_DA3_NODE_MAGIC) | |
bc01119d | 131 | return __this_address; |
88b32f06 | 132 | |
9c4e12fb | 133 | if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid)) |
bc01119d | 134 | return __this_address; |
88b32f06 | 135 | if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn) |
bc01119d | 136 | return __this_address; |
a65d8d29 | 137 | if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->info.lsn))) |
bc01119d | 138 | return __this_address; |
88b32f06 DC |
139 | } else { |
140 | if (ichdr.magic != XFS_DA_NODE_MAGIC) | |
bc01119d | 141 | return __this_address; |
a2ceac1f | 142 | } |
88b32f06 | 143 | if (ichdr.level == 0) |
bc01119d | 144 | return __this_address; |
88b32f06 | 145 | if (ichdr.level > XFS_DA_NODE_MAXDEPTH) |
bc01119d | 146 | return __this_address; |
88b32f06 | 147 | if (ichdr.count == 0) |
bc01119d | 148 | return __this_address; |
88b32f06 DC |
149 | |
150 | /* | |
151 | * we don't know if the node is for and attribute or directory tree, | |
152 | * so only fail if the count is outside both bounds | |
153 | */ | |
ff105f75 DC |
154 | if (ichdr.count > mp->m_dir_geo->node_ents && |
155 | ichdr.count > mp->m_attr_geo->node_ents) | |
bc01119d | 156 | return __this_address; |
88b32f06 DC |
157 | |
158 | /* XXX: hash order check? */ | |
a2ceac1f | 159 | |
bc01119d | 160 | return NULL; |
a2ceac1f DC |
161 | } |
162 | ||
163 | static void | |
88b32f06 | 164 | xfs_da3_node_write_verify( |
a2ceac1f DC |
165 | struct xfs_buf *bp) |
166 | { | |
88b32f06 | 167 | struct xfs_mount *mp = bp->b_target->bt_mount; |
37d086ca | 168 | struct xfs_buf_log_item *bip = bp->b_log_item; |
88b32f06 | 169 | struct xfs_da3_node_hdr *hdr3 = bp->b_addr; |
1e697959 | 170 | xfs_failaddr_t fa; |
88b32f06 | 171 | |
1e697959 DW |
172 | fa = xfs_da3_node_verify(bp); |
173 | if (fa) { | |
174 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
88b32f06 DC |
175 | return; |
176 | } | |
177 | ||
178 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
179 | return; | |
180 | ||
181 | if (bip) | |
182 | hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn); | |
183 | ||
43b5aeed | 184 | xfs_buf_update_cksum(bp, XFS_DA3_NODE_CRC_OFF); |
a2ceac1f DC |
185 | } |
186 | ||
187 | /* | |
188 | * leaf/node format detection on trees is sketchy, so a node read can be done on | |
189 | * leaf level blocks when detection identifies the tree as a node format tree | |
190 | * incorrectly. In this case, we need to swap the verifier to match the correct | |
191 | * format of the block being read. | |
192 | */ | |
193 | static void | |
88b32f06 | 194 | xfs_da3_node_read_verify( |
a2ceac1f DC |
195 | struct xfs_buf *bp) |
196 | { | |
a2ceac1f | 197 | struct xfs_da_blkinfo *info = bp->b_addr; |
1e697959 | 198 | xfs_failaddr_t fa; |
a2ceac1f DC |
199 | |
200 | switch (be16_to_cpu(info->magic)) { | |
88b32f06 | 201 | case XFS_DA3_NODE_MAGIC: |
45922933 | 202 | if (!xfs_buf_verify_cksum(bp, XFS_DA3_NODE_CRC_OFF)) { |
1e697959 DW |
203 | xfs_verifier_error(bp, -EFSBADCRC, |
204 | __this_address); | |
88b32f06 | 205 | break; |
45922933 | 206 | } |
88b32f06 | 207 | /* fall through */ |
a2ceac1f | 208 | case XFS_DA_NODE_MAGIC: |
1e697959 DW |
209 | fa = xfs_da3_node_verify(bp); |
210 | if (fa) | |
211 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
88b32f06 | 212 | return; |
a2ceac1f | 213 | case XFS_ATTR_LEAF_MAGIC: |
a24374f4 DC |
214 | case XFS_ATTR3_LEAF_MAGIC: |
215 | bp->b_ops = &xfs_attr3_leaf_buf_ops; | |
a2ceac1f DC |
216 | bp->b_ops->verify_read(bp); |
217 | return; | |
218 | case XFS_DIR2_LEAFN_MAGIC: | |
65b80c98 DC |
219 | case XFS_DIR3_LEAFN_MAGIC: |
220 | bp->b_ops = &xfs_dir3_leafn_buf_ops; | |
a2ceac1f DC |
221 | bp->b_ops->verify_read(bp); |
222 | return; | |
223 | default: | |
1e697959 | 224 | xfs_verifier_error(bp, -EFSCORRUPTED, __this_address); |
a2ceac1f DC |
225 | break; |
226 | } | |
227 | } | |
228 | ||
95d9582b DW |
229 | /* Verify the structure of a da3 block. */ |
230 | static xfs_failaddr_t | |
231 | xfs_da3_node_verify_struct( | |
232 | struct xfs_buf *bp) | |
233 | { | |
234 | struct xfs_da_blkinfo *info = bp->b_addr; | |
235 | ||
236 | switch (be16_to_cpu(info->magic)) { | |
237 | case XFS_DA3_NODE_MAGIC: | |
238 | case XFS_DA_NODE_MAGIC: | |
239 | return xfs_da3_node_verify(bp); | |
240 | case XFS_ATTR_LEAF_MAGIC: | |
241 | case XFS_ATTR3_LEAF_MAGIC: | |
242 | bp->b_ops = &xfs_attr3_leaf_buf_ops; | |
243 | return bp->b_ops->verify_struct(bp); | |
244 | case XFS_DIR2_LEAFN_MAGIC: | |
245 | case XFS_DIR3_LEAFN_MAGIC: | |
246 | bp->b_ops = &xfs_dir3_leafn_buf_ops; | |
247 | return bp->b_ops->verify_struct(bp); | |
248 | default: | |
249 | return __this_address; | |
250 | } | |
251 | } | |
252 | ||
88b32f06 | 253 | const struct xfs_buf_ops xfs_da3_node_buf_ops = { |
a3fac935 | 254 | .name = "xfs_da3_node", |
88b32f06 DC |
255 | .verify_read = xfs_da3_node_read_verify, |
256 | .verify_write = xfs_da3_node_write_verify, | |
95d9582b | 257 | .verify_struct = xfs_da3_node_verify_struct, |
a2ceac1f DC |
258 | }; |
259 | ||
a2ceac1f | 260 | int |
88b32f06 | 261 | xfs_da3_node_read( |
a2ceac1f DC |
262 | struct xfs_trans *tp, |
263 | struct xfs_inode *dp, | |
264 | xfs_dablk_t bno, | |
265 | xfs_daddr_t mappedbno, | |
266 | struct xfs_buf **bpp, | |
267 | int which_fork) | |
268 | { | |
8b4dc4a9 DC |
269 | int err; |
270 | ||
271 | err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp, | |
88b32f06 | 272 | which_fork, &xfs_da3_node_buf_ops); |
a6c6b1bd | 273 | if (!err && tp && *bpp) { |
8b4dc4a9 DC |
274 | struct xfs_da_blkinfo *info = (*bpp)->b_addr; |
275 | int type; | |
276 | ||
277 | switch (be16_to_cpu(info->magic)) { | |
8b4dc4a9 | 278 | case XFS_DA_NODE_MAGIC: |
f383ee8c | 279 | case XFS_DA3_NODE_MAGIC: |
bdc16ee5 | 280 | type = XFS_BLFT_DA_NODE_BUF; |
8b4dc4a9 DC |
281 | break; |
282 | case XFS_ATTR_LEAF_MAGIC: | |
283 | case XFS_ATTR3_LEAF_MAGIC: | |
bdc16ee5 | 284 | type = XFS_BLFT_ATTR_LEAF_BUF; |
8b4dc4a9 DC |
285 | break; |
286 | case XFS_DIR2_LEAFN_MAGIC: | |
287 | case XFS_DIR3_LEAFN_MAGIC: | |
bdc16ee5 | 288 | type = XFS_BLFT_DIR_LEAFN_BUF; |
8b4dc4a9 DC |
289 | break; |
290 | default: | |
52581d38 | 291 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, |
3177b915 | 292 | tp->t_mountp, info, sizeof(*info)); |
52581d38 DW |
293 | xfs_trans_brelse(tp, *bpp); |
294 | *bpp = NULL; | |
295 | return -EFSCORRUPTED; | |
8b4dc4a9 DC |
296 | } |
297 | xfs_trans_buf_set_type(tp, *bpp, type); | |
298 | } | |
299 | return err; | |
a2ceac1f DC |
300 | } |
301 | ||
2bd0ea18 NS |
302 | /*======================================================================== |
303 | * Routines used for growing the Btree. | |
304 | *========================================================================*/ | |
305 | ||
306 | /* | |
307 | * Create the initial contents of an intermediate node. | |
308 | */ | |
309 | int | |
88b32f06 DC |
310 | xfs_da3_node_create( |
311 | struct xfs_da_args *args, | |
312 | xfs_dablk_t blkno, | |
313 | int level, | |
314 | struct xfs_buf **bpp, | |
315 | int whichfork) | |
2bd0ea18 | 316 | { |
88b32f06 DC |
317 | struct xfs_da_intnode *node; |
318 | struct xfs_trans *tp = args->trans; | |
319 | struct xfs_mount *mp = tp->t_mountp; | |
320 | struct xfs_da3_icnode_hdr ichdr = {0}; | |
321 | struct xfs_buf *bp; | |
322 | int error; | |
ff105f75 | 323 | struct xfs_inode *dp = args->dp; |
2bd0ea18 | 324 | |
a2ceac1f | 325 | trace_xfs_da_node_create(args); |
88b32f06 | 326 | ASSERT(level <= XFS_DA_NODE_MAXDEPTH); |
a2ceac1f | 327 | |
ff105f75 | 328 | error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, whichfork); |
2bd0ea18 | 329 | if (error) |
af43ca9f | 330 | return error; |
8b4dc4a9 | 331 | bp->b_ops = &xfs_da3_node_buf_ops; |
bdc16ee5 | 332 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF); |
a2ceac1f | 333 | node = bp->b_addr; |
2bd0ea18 | 334 | |
88b32f06 DC |
335 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
336 | struct xfs_da3_node_hdr *hdr3 = bp->b_addr; | |
337 | ||
a65d8d29 | 338 | memset(hdr3, 0, sizeof(struct xfs_da3_node_hdr)); |
88b32f06 DC |
339 | ichdr.magic = XFS_DA3_NODE_MAGIC; |
340 | hdr3->info.blkno = cpu_to_be64(bp->b_bn); | |
341 | hdr3->info.owner = cpu_to_be64(args->dp->i_ino); | |
9c4e12fb | 342 | uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid); |
88b32f06 DC |
343 | } else { |
344 | ichdr.magic = XFS_DA_NODE_MAGIC; | |
345 | } | |
346 | ichdr.level = level; | |
347 | ||
ff105f75 | 348 | dp->d_ops->node_hdr_to_disk(node, &ichdr); |
a2ceac1f | 349 | xfs_trans_log_buf(tp, bp, |
ff105f75 | 350 | XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size)); |
2bd0ea18 NS |
351 | |
352 | *bpp = bp; | |
af43ca9f | 353 | return 0; |
2bd0ea18 NS |
354 | } |
355 | ||
356 | /* | |
357 | * Split a leaf node, rebalance, then possibly split | |
358 | * intermediate nodes, rebalance, etc. | |
359 | */ | |
360 | int /* error */ | |
88b32f06 DC |
361 | xfs_da3_split( |
362 | struct xfs_da_state *state) | |
2bd0ea18 | 363 | { |
88b32f06 DC |
364 | struct xfs_da_state_blk *oldblk; |
365 | struct xfs_da_state_blk *newblk; | |
366 | struct xfs_da_state_blk *addblk; | |
367 | struct xfs_da_intnode *node; | |
88b32f06 | 368 | int max; |
6bddecbc | 369 | int action = 0; |
88b32f06 DC |
370 | int error; |
371 | int i; | |
2bd0ea18 | 372 | |
a2ceac1f DC |
373 | trace_xfs_da_split(state->args); |
374 | ||
2bd0ea18 NS |
375 | /* |
376 | * Walk back up the tree splitting/inserting/adjusting as necessary. | |
377 | * If we need to insert and there isn't room, split the node, then | |
378 | * decide which fragment to insert the new block from below into. | |
379 | * Note that we may split the root this way, but we need more fixup. | |
380 | */ | |
381 | max = state->path.active - 1; | |
382 | ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH)); | |
383 | ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC || | |
5e656dbb | 384 | state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC); |
2bd0ea18 NS |
385 | |
386 | addblk = &state->path.blk[max]; /* initial dummy value */ | |
387 | for (i = max; (i >= 0) && addblk; state->path.active--, i--) { | |
388 | oldblk = &state->path.blk[i]; | |
389 | newblk = &state->altpath.blk[i]; | |
390 | ||
391 | /* | |
392 | * If a leaf node then | |
393 | * Allocate a new leaf node, then rebalance across them. | |
394 | * else if an intermediate node then | |
395 | * We split on the last layer, must we split the node? | |
396 | */ | |
397 | switch (oldblk->magic) { | |
398 | case XFS_ATTR_LEAF_MAGIC: | |
a24374f4 | 399 | error = xfs_attr3_leaf_split(state, oldblk, newblk); |
12b53197 | 400 | if ((error != 0) && (error != -ENOSPC)) { |
af43ca9f | 401 | return error; /* GROT: attr is inconsistent */ |
2bd0ea18 NS |
402 | } |
403 | if (!error) { | |
404 | addblk = newblk; | |
405 | break; | |
406 | } | |
407 | /* | |
4280e59d DC |
408 | * Entry wouldn't fit, split the leaf again. The new |
409 | * extrablk will be consumed by xfs_da3_node_split if | |
410 | * the node is split. | |
2bd0ea18 NS |
411 | */ |
412 | state->extravalid = 1; | |
413 | if (state->inleaf) { | |
414 | state->extraafter = 0; /* before newblk */ | |
a2ceac1f | 415 | trace_xfs_attr_leaf_split_before(state->args); |
a24374f4 | 416 | error = xfs_attr3_leaf_split(state, oldblk, |
2bd0ea18 NS |
417 | &state->extrablk); |
418 | } else { | |
419 | state->extraafter = 1; /* after newblk */ | |
a2ceac1f | 420 | trace_xfs_attr_leaf_split_after(state->args); |
a24374f4 | 421 | error = xfs_attr3_leaf_split(state, newblk, |
2bd0ea18 NS |
422 | &state->extrablk); |
423 | } | |
424 | if (error) | |
af43ca9f | 425 | return error; /* GROT: attr inconsistent */ |
2bd0ea18 NS |
426 | addblk = newblk; |
427 | break; | |
2bd0ea18 | 428 | case XFS_DIR2_LEAFN_MAGIC: |
2bd0ea18 NS |
429 | error = xfs_dir2_leafn_split(state, oldblk, newblk); |
430 | if (error) | |
431 | return error; | |
432 | addblk = newblk; | |
433 | break; | |
434 | case XFS_DA_NODE_MAGIC: | |
88b32f06 | 435 | error = xfs_da3_node_split(state, oldblk, newblk, addblk, |
2bd0ea18 | 436 | max - i, &action); |
2bd0ea18 NS |
437 | addblk->bp = NULL; |
438 | if (error) | |
af43ca9f | 439 | return error; /* GROT: dir is inconsistent */ |
2bd0ea18 NS |
440 | /* |
441 | * Record the newly split block for the next time thru? | |
442 | */ | |
443 | if (action) | |
444 | addblk = newblk; | |
445 | else | |
446 | addblk = NULL; | |
447 | break; | |
448 | } | |
449 | ||
450 | /* | |
451 | * Update the btree to show the new hashval for this child. | |
452 | */ | |
88b32f06 | 453 | xfs_da3_fixhashpath(state, &state->path); |
2bd0ea18 NS |
454 | } |
455 | if (!addblk) | |
af43ca9f | 456 | return 0; |
2bd0ea18 | 457 | |
4280e59d DC |
458 | /* |
459 | * xfs_da3_node_split() should have consumed any extra blocks we added | |
460 | * during a double leaf split in the attr fork. This is guaranteed as | |
461 | * we can't be here if the attr fork only has a single leaf block. | |
462 | */ | |
463 | ASSERT(state->extravalid == 0 || | |
464 | state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC); | |
465 | ||
2bd0ea18 NS |
466 | /* |
467 | * Split the root node. | |
468 | */ | |
469 | ASSERT(state->path.active == 0); | |
470 | oldblk = &state->path.blk[0]; | |
88b32f06 | 471 | error = xfs_da3_root_split(state, oldblk, addblk); |
2bd0ea18 | 472 | if (error) { |
2bd0ea18 | 473 | addblk->bp = NULL; |
af43ca9f | 474 | return error; /* GROT: dir is inconsistent */ |
2bd0ea18 NS |
475 | } |
476 | ||
477 | /* | |
4280e59d DC |
478 | * Update pointers to the node which used to be block 0 and just got |
479 | * bumped because of the addition of a new root node. Note that the | |
480 | * original block 0 could be at any position in the list of blocks in | |
481 | * the tree. | |
88b32f06 | 482 | * |
4280e59d DC |
483 | * Note: the magic numbers and sibling pointers are in the same physical |
484 | * place for both v2 and v3 headers (by design). Hence it doesn't matter | |
485 | * which version of the xfs_da_intnode structure we use here as the | |
486 | * result will be the same using either structure. | |
2bd0ea18 | 487 | */ |
a2ceac1f | 488 | node = oldblk->bp->b_addr; |
46eca962 | 489 | if (node->hdr.info.forw) { |
4280e59d DC |
490 | ASSERT(be32_to_cpu(node->hdr.info.forw) == addblk->blkno); |
491 | node = addblk->bp->b_addr; | |
5e656dbb | 492 | node->hdr.info.back = cpu_to_be32(oldblk->blkno); |
4280e59d DC |
493 | xfs_trans_log_buf(state->args->trans, addblk->bp, |
494 | XFS_DA_LOGRANGE(node, &node->hdr.info, | |
495 | sizeof(node->hdr.info))); | |
2bd0ea18 | 496 | } |
a2ceac1f | 497 | node = oldblk->bp->b_addr; |
5e656dbb | 498 | if (node->hdr.info.back) { |
4280e59d DC |
499 | ASSERT(be32_to_cpu(node->hdr.info.back) == addblk->blkno); |
500 | node = addblk->bp->b_addr; | |
5e656dbb | 501 | node->hdr.info.forw = cpu_to_be32(oldblk->blkno); |
4280e59d DC |
502 | xfs_trans_log_buf(state->args->trans, addblk->bp, |
503 | XFS_DA_LOGRANGE(node, &node->hdr.info, | |
504 | sizeof(node->hdr.info))); | |
2bd0ea18 | 505 | } |
2bd0ea18 | 506 | addblk->bp = NULL; |
af43ca9f | 507 | return 0; |
2bd0ea18 NS |
508 | } |
509 | ||
510 | /* | |
511 | * Split the root. We have to create a new root and point to the two | |
512 | * parts (the split old root) that we just created. Copy block zero to | |
513 | * the EOF, extending the inode in process. | |
514 | */ | |
515 | STATIC int /* error */ | |
88b32f06 DC |
516 | xfs_da3_root_split( |
517 | struct xfs_da_state *state, | |
518 | struct xfs_da_state_blk *blk1, | |
519 | struct xfs_da_state_blk *blk2) | |
2bd0ea18 | 520 | { |
88b32f06 DC |
521 | struct xfs_da_intnode *node; |
522 | struct xfs_da_intnode *oldroot; | |
523 | struct xfs_da_node_entry *btree; | |
524 | struct xfs_da3_icnode_hdr nodehdr; | |
525 | struct xfs_da_args *args; | |
526 | struct xfs_buf *bp; | |
527 | struct xfs_inode *dp; | |
528 | struct xfs_trans *tp; | |
88b32f06 DC |
529 | struct xfs_dir2_leaf *leaf; |
530 | xfs_dablk_t blkno; | |
531 | int level; | |
532 | int error; | |
533 | int size; | |
2bd0ea18 | 534 | |
a2ceac1f DC |
535 | trace_xfs_da_root_split(state->args); |
536 | ||
2bd0ea18 NS |
537 | /* |
538 | * Copy the existing (incorrect) block from the root node position | |
539 | * to a free space somewhere. | |
540 | */ | |
541 | args = state->args; | |
2bd0ea18 NS |
542 | error = xfs_da_grow_inode(args, &blkno); |
543 | if (error) | |
88b32f06 DC |
544 | return error; |
545 | ||
2bd0ea18 NS |
546 | dp = args->dp; |
547 | tp = args->trans; | |
2bd0ea18 NS |
548 | error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, args->whichfork); |
549 | if (error) | |
88b32f06 | 550 | return error; |
a2ceac1f DC |
551 | node = bp->b_addr; |
552 | oldroot = blk1->bp->b_addr; | |
88b32f06 DC |
553 | if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) || |
554 | oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) { | |
19ebedcf | 555 | struct xfs_da3_icnode_hdr icnodehdr; |
88b32f06 | 556 | |
19ebedcf | 557 | dp->d_ops->node_hdr_from_disk(&icnodehdr, oldroot); |
ff105f75 | 558 | btree = dp->d_ops->node_tree_p(oldroot); |
19ebedcf DC |
559 | size = (int)((char *)&btree[icnodehdr.count] - (char *)oldroot); |
560 | level = icnodehdr.level; | |
8b4dc4a9 DC |
561 | |
562 | /* | |
563 | * we are about to copy oldroot to bp, so set up the type | |
564 | * of bp while we know exactly what it will be. | |
565 | */ | |
bdc16ee5 | 566 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF); |
2bd0ea18 | 567 | } else { |
65b80c98 DC |
568 | struct xfs_dir3_icleaf_hdr leafhdr; |
569 | struct xfs_dir2_leaf_entry *ents; | |
570 | ||
2bd0ea18 | 571 | leaf = (xfs_dir2_leaf_t *)oldroot; |
ff105f75 DC |
572 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
573 | ents = dp->d_ops->leaf_ents_p(leaf); | |
65b80c98 DC |
574 | |
575 | ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC || | |
576 | leafhdr.magic == XFS_DIR3_LEAFN_MAGIC); | |
577 | size = (int)((char *)&ents[leafhdr.count] - (char *)leaf); | |
88b32f06 | 578 | level = 0; |
8b4dc4a9 DC |
579 | |
580 | /* | |
581 | * we are about to copy oldroot to bp, so set up the type | |
582 | * of bp while we know exactly what it will be. | |
583 | */ | |
bdc16ee5 | 584 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF); |
2bd0ea18 | 585 | } |
88b32f06 DC |
586 | |
587 | /* | |
588 | * we can copy most of the information in the node from one block to | |
589 | * another, but for CRC enabled headers we have to make sure that the | |
590 | * block specific identifiers are kept intact. We update the buffer | |
591 | * directly for this. | |
592 | */ | |
32181a02 | 593 | memcpy(node, oldroot, size); |
88b32f06 DC |
594 | if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) || |
595 | oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) { | |
596 | struct xfs_da3_intnode *node3 = (struct xfs_da3_intnode *)node; | |
597 | ||
598 | node3->hdr.info.blkno = cpu_to_be64(bp->b_bn); | |
599 | } | |
a2ceac1f DC |
600 | xfs_trans_log_buf(tp, bp, 0, size - 1); |
601 | ||
602 | bp->b_ops = blk1->bp->b_ops; | |
e26915ee | 603 | xfs_trans_buf_copy_type(bp, blk1->bp); |
2bd0ea18 NS |
604 | blk1->bp = bp; |
605 | blk1->blkno = blkno; | |
606 | ||
607 | /* | |
608 | * Set up the new root node. | |
609 | */ | |
88b32f06 | 610 | error = xfs_da3_node_create(args, |
ff105f75 | 611 | (args->whichfork == XFS_DATA_FORK) ? args->geo->leafblk : 0, |
88b32f06 | 612 | level + 1, &bp, args->whichfork); |
2bd0ea18 | 613 | if (error) |
88b32f06 DC |
614 | return error; |
615 | ||
a2ceac1f | 616 | node = bp->b_addr; |
ff105f75 DC |
617 | dp->d_ops->node_hdr_from_disk(&nodehdr, node); |
618 | btree = dp->d_ops->node_tree_p(node); | |
88b32f06 DC |
619 | btree[0].hashval = cpu_to_be32(blk1->hashval); |
620 | btree[0].before = cpu_to_be32(blk1->blkno); | |
621 | btree[1].hashval = cpu_to_be32(blk2->hashval); | |
622 | btree[1].before = cpu_to_be32(blk2->blkno); | |
623 | nodehdr.count = 2; | |
ff105f75 | 624 | dp->d_ops->node_hdr_to_disk(node, &nodehdr); |
6bef826c NS |
625 | |
626 | #ifdef DEBUG | |
65b80c98 DC |
627 | if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) || |
628 | oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) { | |
ff105f75 DC |
629 | ASSERT(blk1->blkno >= args->geo->leafblk && |
630 | blk1->blkno < args->geo->freeblk); | |
631 | ASSERT(blk2->blkno >= args->geo->leafblk && | |
632 | blk2->blkno < args->geo->freeblk); | |
2bd0ea18 | 633 | } |
6bef826c NS |
634 | #endif |
635 | ||
2bd0ea18 | 636 | /* Header is already logged by xfs_da_node_create */ |
a2ceac1f | 637 | xfs_trans_log_buf(tp, bp, |
88b32f06 | 638 | XFS_DA_LOGRANGE(node, btree, sizeof(xfs_da_node_entry_t) * 2)); |
2bd0ea18 | 639 | |
88b32f06 | 640 | return 0; |
2bd0ea18 NS |
641 | } |
642 | ||
643 | /* | |
644 | * Split the node, rebalance, then add the new entry. | |
645 | */ | |
646 | STATIC int /* error */ | |
88b32f06 DC |
647 | xfs_da3_node_split( |
648 | struct xfs_da_state *state, | |
649 | struct xfs_da_state_blk *oldblk, | |
650 | struct xfs_da_state_blk *newblk, | |
651 | struct xfs_da_state_blk *addblk, | |
652 | int treelevel, | |
653 | int *result) | |
2bd0ea18 | 654 | { |
88b32f06 DC |
655 | struct xfs_da_intnode *node; |
656 | struct xfs_da3_icnode_hdr nodehdr; | |
657 | xfs_dablk_t blkno; | |
658 | int newcount; | |
659 | int error; | |
660 | int useextra; | |
ff105f75 | 661 | struct xfs_inode *dp = state->args->dp; |
2bd0ea18 | 662 | |
a2ceac1f DC |
663 | trace_xfs_da_node_split(state->args); |
664 | ||
665 | node = oldblk->bp->b_addr; | |
ff105f75 | 666 | dp->d_ops->node_hdr_from_disk(&nodehdr, node); |
2bd0ea18 NS |
667 | |
668 | /* | |
5e656dbb | 669 | * With V2 dirs the extra block is data or freespace. |
2bd0ea18 | 670 | */ |
5e656dbb | 671 | useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK; |
2bd0ea18 NS |
672 | newcount = 1 + useextra; |
673 | /* | |
674 | * Do we have to split the node? | |
675 | */ | |
ff105f75 | 676 | if (nodehdr.count + newcount > state->args->geo->node_ents) { |
2bd0ea18 NS |
677 | /* |
678 | * Allocate a new node, add to the doubly linked chain of | |
679 | * nodes, then move some of our excess entries into it. | |
680 | */ | |
681 | error = xfs_da_grow_inode(state->args, &blkno); | |
682 | if (error) | |
af43ca9f | 683 | return error; /* GROT: dir is inconsistent */ |
5000d01d | 684 | |
88b32f06 | 685 | error = xfs_da3_node_create(state->args, blkno, treelevel, |
2bd0ea18 NS |
686 | &newblk->bp, state->args->whichfork); |
687 | if (error) | |
af43ca9f | 688 | return error; /* GROT: dir is inconsistent */ |
2bd0ea18 NS |
689 | newblk->blkno = blkno; |
690 | newblk->magic = XFS_DA_NODE_MAGIC; | |
88b32f06 DC |
691 | xfs_da3_node_rebalance(state, oldblk, newblk); |
692 | error = xfs_da3_blk_link(state, oldblk, newblk); | |
2bd0ea18 | 693 | if (error) |
af43ca9f | 694 | return error; |
2bd0ea18 NS |
695 | *result = 1; |
696 | } else { | |
697 | *result = 0; | |
698 | } | |
699 | ||
700 | /* | |
701 | * Insert the new entry(s) into the correct block | |
702 | * (updating last hashval in the process). | |
703 | * | |
88b32f06 | 704 | * xfs_da3_node_add() inserts BEFORE the given index, |
2bd0ea18 NS |
705 | * and as a result of using node_lookup_int() we always |
706 | * point to a valid entry (not after one), but a split | |
707 | * operation always results in a new block whose hashvals | |
708 | * FOLLOW the current block. | |
709 | * | |
710 | * If we had double-split op below us, then add the extra block too. | |
711 | */ | |
a2ceac1f | 712 | node = oldblk->bp->b_addr; |
ff105f75 | 713 | dp->d_ops->node_hdr_from_disk(&nodehdr, node); |
88b32f06 | 714 | if (oldblk->index <= nodehdr.count) { |
2bd0ea18 | 715 | oldblk->index++; |
88b32f06 | 716 | xfs_da3_node_add(state, oldblk, addblk); |
2bd0ea18 NS |
717 | if (useextra) { |
718 | if (state->extraafter) | |
719 | oldblk->index++; | |
88b32f06 | 720 | xfs_da3_node_add(state, oldblk, &state->extrablk); |
2bd0ea18 NS |
721 | state->extravalid = 0; |
722 | } | |
723 | } else { | |
724 | newblk->index++; | |
88b32f06 | 725 | xfs_da3_node_add(state, newblk, addblk); |
2bd0ea18 NS |
726 | if (useextra) { |
727 | if (state->extraafter) | |
728 | newblk->index++; | |
88b32f06 | 729 | xfs_da3_node_add(state, newblk, &state->extrablk); |
2bd0ea18 NS |
730 | state->extravalid = 0; |
731 | } | |
732 | } | |
733 | ||
af43ca9f | 734 | return 0; |
2bd0ea18 NS |
735 | } |
736 | ||
737 | /* | |
738 | * Balance the btree elements between two intermediate nodes, | |
739 | * usually one full and one empty. | |
740 | * | |
741 | * NOTE: if blk2 is empty, then it will get the upper half of blk1. | |
742 | */ | |
743 | STATIC void | |
88b32f06 DC |
744 | xfs_da3_node_rebalance( |
745 | struct xfs_da_state *state, | |
746 | struct xfs_da_state_blk *blk1, | |
747 | struct xfs_da_state_blk *blk2) | |
2bd0ea18 | 748 | { |
88b32f06 DC |
749 | struct xfs_da_intnode *node1; |
750 | struct xfs_da_intnode *node2; | |
751 | struct xfs_da_intnode *tmpnode; | |
752 | struct xfs_da_node_entry *btree1; | |
753 | struct xfs_da_node_entry *btree2; | |
754 | struct xfs_da_node_entry *btree_s; | |
755 | struct xfs_da_node_entry *btree_d; | |
756 | struct xfs_da3_icnode_hdr nodehdr1; | |
757 | struct xfs_da3_icnode_hdr nodehdr2; | |
758 | struct xfs_trans *tp; | |
759 | int count; | |
760 | int tmp; | |
761 | int swap = 0; | |
ff105f75 | 762 | struct xfs_inode *dp = state->args->dp; |
2bd0ea18 | 763 | |
a2ceac1f DC |
764 | trace_xfs_da_node_rebalance(state->args); |
765 | ||
766 | node1 = blk1->bp->b_addr; | |
767 | node2 = blk2->bp->b_addr; | |
ff105f75 DC |
768 | dp->d_ops->node_hdr_from_disk(&nodehdr1, node1); |
769 | dp->d_ops->node_hdr_from_disk(&nodehdr2, node2); | |
770 | btree1 = dp->d_ops->node_tree_p(node1); | |
771 | btree2 = dp->d_ops->node_tree_p(node2); | |
88b32f06 | 772 | |
2bd0ea18 NS |
773 | /* |
774 | * Figure out how many entries need to move, and in which direction. | |
775 | * Swap the nodes around if that makes it simpler. | |
776 | */ | |
88b32f06 DC |
777 | if (nodehdr1.count > 0 && nodehdr2.count > 0 && |
778 | ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) || | |
779 | (be32_to_cpu(btree2[nodehdr2.count - 1].hashval) < | |
780 | be32_to_cpu(btree1[nodehdr1.count - 1].hashval)))) { | |
2bd0ea18 NS |
781 | tmpnode = node1; |
782 | node1 = node2; | |
783 | node2 = tmpnode; | |
ff105f75 DC |
784 | dp->d_ops->node_hdr_from_disk(&nodehdr1, node1); |
785 | dp->d_ops->node_hdr_from_disk(&nodehdr2, node2); | |
786 | btree1 = dp->d_ops->node_tree_p(node1); | |
787 | btree2 = dp->d_ops->node_tree_p(node2); | |
88b32f06 | 788 | swap = 1; |
2bd0ea18 | 789 | } |
88b32f06 DC |
790 | |
791 | count = (nodehdr1.count - nodehdr2.count) / 2; | |
2bd0ea18 NS |
792 | if (count == 0) |
793 | return; | |
794 | tp = state->args->trans; | |
795 | /* | |
796 | * Two cases: high-to-low and low-to-high. | |
797 | */ | |
798 | if (count > 0) { | |
799 | /* | |
800 | * Move elements in node2 up to make a hole. | |
801 | */ | |
88b32f06 DC |
802 | tmp = nodehdr2.count; |
803 | if (tmp > 0) { | |
2bd0ea18 | 804 | tmp *= (uint)sizeof(xfs_da_node_entry_t); |
88b32f06 DC |
805 | btree_s = &btree2[0]; |
806 | btree_d = &btree2[count]; | |
32181a02 | 807 | memmove(btree_d, btree_s, tmp); |
2bd0ea18 NS |
808 | } |
809 | ||
810 | /* | |
811 | * Move the req'd B-tree elements from high in node1 to | |
812 | * low in node2. | |
813 | */ | |
88b32f06 | 814 | nodehdr2.count += count; |
2bd0ea18 | 815 | tmp = count * (uint)sizeof(xfs_da_node_entry_t); |
7b02556d | 816 | btree_s = &btree1[nodehdr1.count - count]; |
88b32f06 | 817 | btree_d = &btree2[0]; |
32181a02 | 818 | memcpy(btree_d, btree_s, tmp); |
88b32f06 | 819 | nodehdr1.count -= count; |
2bd0ea18 NS |
820 | } else { |
821 | /* | |
822 | * Move the req'd B-tree elements from low in node2 to | |
823 | * high in node1. | |
824 | */ | |
825 | count = -count; | |
826 | tmp = count * (uint)sizeof(xfs_da_node_entry_t); | |
88b32f06 DC |
827 | btree_s = &btree2[0]; |
828 | btree_d = &btree1[nodehdr1.count]; | |
32181a02 | 829 | memcpy(btree_d, btree_s, tmp); |
88b32f06 DC |
830 | nodehdr1.count += count; |
831 | ||
a2ceac1f | 832 | xfs_trans_log_buf(tp, blk1->bp, |
2bd0ea18 NS |
833 | XFS_DA_LOGRANGE(node1, btree_d, tmp)); |
834 | ||
835 | /* | |
836 | * Move elements in node2 down to fill the hole. | |
837 | */ | |
88b32f06 | 838 | tmp = nodehdr2.count - count; |
2bd0ea18 | 839 | tmp *= (uint)sizeof(xfs_da_node_entry_t); |
88b32f06 DC |
840 | btree_s = &btree2[count]; |
841 | btree_d = &btree2[0]; | |
32181a02 | 842 | memmove(btree_d, btree_s, tmp); |
88b32f06 | 843 | nodehdr2.count -= count; |
2bd0ea18 NS |
844 | } |
845 | ||
846 | /* | |
847 | * Log header of node 1 and all current bits of node 2. | |
848 | */ | |
ff105f75 | 849 | dp->d_ops->node_hdr_to_disk(node1, &nodehdr1); |
a2ceac1f | 850 | xfs_trans_log_buf(tp, blk1->bp, |
ff105f75 | 851 | XFS_DA_LOGRANGE(node1, &node1->hdr, dp->d_ops->node_hdr_size)); |
88b32f06 | 852 | |
ff105f75 | 853 | dp->d_ops->node_hdr_to_disk(node2, &nodehdr2); |
a2ceac1f | 854 | xfs_trans_log_buf(tp, blk2->bp, |
2bd0ea18 | 855 | XFS_DA_LOGRANGE(node2, &node2->hdr, |
ff105f75 | 856 | dp->d_ops->node_hdr_size + |
88b32f06 | 857 | (sizeof(btree2[0]) * nodehdr2.count))); |
2bd0ea18 NS |
858 | |
859 | /* | |
860 | * Record the last hashval from each block for upward propagation. | |
861 | * (note: don't use the swapped node pointers) | |
862 | */ | |
88b32f06 DC |
863 | if (swap) { |
864 | node1 = blk1->bp->b_addr; | |
865 | node2 = blk2->bp->b_addr; | |
ff105f75 DC |
866 | dp->d_ops->node_hdr_from_disk(&nodehdr1, node1); |
867 | dp->d_ops->node_hdr_from_disk(&nodehdr2, node2); | |
868 | btree1 = dp->d_ops->node_tree_p(node1); | |
869 | btree2 = dp->d_ops->node_tree_p(node2); | |
88b32f06 DC |
870 | } |
871 | blk1->hashval = be32_to_cpu(btree1[nodehdr1.count - 1].hashval); | |
872 | blk2->hashval = be32_to_cpu(btree2[nodehdr2.count - 1].hashval); | |
2bd0ea18 NS |
873 | |
874 | /* | |
875 | * Adjust the expected index for insertion. | |
876 | */ | |
88b32f06 DC |
877 | if (blk1->index >= nodehdr1.count) { |
878 | blk2->index = blk1->index - nodehdr1.count; | |
879 | blk1->index = nodehdr1.count + 1; /* make it invalid */ | |
2bd0ea18 NS |
880 | } |
881 | } | |
882 | ||
883 | /* | |
884 | * Add a new entry to an intermediate node. | |
885 | */ | |
886 | STATIC void | |
88b32f06 DC |
887 | xfs_da3_node_add( |
888 | struct xfs_da_state *state, | |
889 | struct xfs_da_state_blk *oldblk, | |
890 | struct xfs_da_state_blk *newblk) | |
2bd0ea18 | 891 | { |
88b32f06 DC |
892 | struct xfs_da_intnode *node; |
893 | struct xfs_da3_icnode_hdr nodehdr; | |
894 | struct xfs_da_node_entry *btree; | |
895 | int tmp; | |
ff105f75 | 896 | struct xfs_inode *dp = state->args->dp; |
2bd0ea18 | 897 | |
a2ceac1f DC |
898 | trace_xfs_da_node_add(state->args); |
899 | ||
900 | node = oldblk->bp->b_addr; | |
ff105f75 DC |
901 | dp->d_ops->node_hdr_from_disk(&nodehdr, node); |
902 | btree = dp->d_ops->node_tree_p(node); | |
88b32f06 DC |
903 | |
904 | ASSERT(oldblk->index >= 0 && oldblk->index <= nodehdr.count); | |
2bd0ea18 | 905 | ASSERT(newblk->blkno != 0); |
5e656dbb | 906 | if (state->args->whichfork == XFS_DATA_FORK) |
ff105f75 DC |
907 | ASSERT(newblk->blkno >= state->args->geo->leafblk && |
908 | newblk->blkno < state->args->geo->freeblk); | |
2bd0ea18 NS |
909 | |
910 | /* | |
911 | * We may need to make some room before we insert the new node. | |
912 | */ | |
913 | tmp = 0; | |
88b32f06 DC |
914 | if (oldblk->index < nodehdr.count) { |
915 | tmp = (nodehdr.count - oldblk->index) * (uint)sizeof(*btree); | |
916 | memmove(&btree[oldblk->index + 1], &btree[oldblk->index], tmp); | |
2bd0ea18 | 917 | } |
88b32f06 DC |
918 | btree[oldblk->index].hashval = cpu_to_be32(newblk->hashval); |
919 | btree[oldblk->index].before = cpu_to_be32(newblk->blkno); | |
a2ceac1f | 920 | xfs_trans_log_buf(state->args->trans, oldblk->bp, |
88b32f06 DC |
921 | XFS_DA_LOGRANGE(node, &btree[oldblk->index], |
922 | tmp + sizeof(*btree))); | |
923 | ||
924 | nodehdr.count += 1; | |
ff105f75 | 925 | dp->d_ops->node_hdr_to_disk(node, &nodehdr); |
a2ceac1f | 926 | xfs_trans_log_buf(state->args->trans, oldblk->bp, |
ff105f75 | 927 | XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size)); |
2bd0ea18 NS |
928 | |
929 | /* | |
930 | * Copy the last hash value from the oldblk to propagate upwards. | |
931 | */ | |
88b32f06 | 932 | oldblk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval); |
2bd0ea18 NS |
933 | } |
934 | ||
935 | /*======================================================================== | |
936 | * Routines used for shrinking the Btree. | |
937 | *========================================================================*/ | |
938 | ||
939 | /* | |
940 | * Deallocate an empty leaf node, remove it from its parent, | |
941 | * possibly deallocating that block, etc... | |
942 | */ | |
943 | int | |
88b32f06 DC |
944 | xfs_da3_join( |
945 | struct xfs_da_state *state) | |
2bd0ea18 | 946 | { |
88b32f06 DC |
947 | struct xfs_da_state_blk *drop_blk; |
948 | struct xfs_da_state_blk *save_blk; | |
949 | int action = 0; | |
950 | int error; | |
2bd0ea18 | 951 | |
a2ceac1f DC |
952 | trace_xfs_da_join(state->args); |
953 | ||
2bd0ea18 NS |
954 | drop_blk = &state->path.blk[ state->path.active-1 ]; |
955 | save_blk = &state->altpath.blk[ state->path.active-1 ]; | |
956 | ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC); | |
957 | ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC || | |
5e656dbb | 958 | drop_blk->magic == XFS_DIR2_LEAFN_MAGIC); |
2bd0ea18 NS |
959 | |
960 | /* | |
961 | * Walk back up the tree joining/deallocating as necessary. | |
962 | * When we stop dropping blocks, break out. | |
963 | */ | |
964 | for ( ; state->path.active >= 2; drop_blk--, save_blk--, | |
965 | state->path.active--) { | |
966 | /* | |
967 | * See if we can combine the block with a neighbor. | |
968 | * (action == 0) => no options, just leave | |
969 | * (action == 1) => coalesce, then unlink | |
970 | * (action == 2) => block empty, unlink it | |
971 | */ | |
972 | switch (drop_blk->magic) { | |
973 | case XFS_ATTR_LEAF_MAGIC: | |
a24374f4 | 974 | error = xfs_attr3_leaf_toosmall(state, &action); |
2bd0ea18 | 975 | if (error) |
af43ca9f | 976 | return error; |
2bd0ea18 | 977 | if (action == 0) |
af43ca9f | 978 | return 0; |
a24374f4 | 979 | xfs_attr3_leaf_unbalance(state, drop_blk, save_blk); |
2bd0ea18 | 980 | break; |
2bd0ea18 | 981 | case XFS_DIR2_LEAFN_MAGIC: |
2bd0ea18 NS |
982 | error = xfs_dir2_leafn_toosmall(state, &action); |
983 | if (error) | |
984 | return error; | |
985 | if (action == 0) | |
986 | return 0; | |
987 | xfs_dir2_leafn_unbalance(state, drop_blk, save_blk); | |
988 | break; | |
989 | case XFS_DA_NODE_MAGIC: | |
990 | /* | |
991 | * Remove the offending node, fixup hashvals, | |
992 | * check for a toosmall neighbor. | |
993 | */ | |
88b32f06 DC |
994 | xfs_da3_node_remove(state, drop_blk); |
995 | xfs_da3_fixhashpath(state, &state->path); | |
996 | error = xfs_da3_node_toosmall(state, &action); | |
2bd0ea18 | 997 | if (error) |
af43ca9f | 998 | return error; |
2bd0ea18 NS |
999 | if (action == 0) |
1000 | return 0; | |
88b32f06 | 1001 | xfs_da3_node_unbalance(state, drop_blk, save_blk); |
2bd0ea18 NS |
1002 | break; |
1003 | } | |
88b32f06 DC |
1004 | xfs_da3_fixhashpath(state, &state->altpath); |
1005 | error = xfs_da3_blk_unlink(state, drop_blk, save_blk); | |
2bd0ea18 NS |
1006 | xfs_da_state_kill_altpath(state); |
1007 | if (error) | |
af43ca9f | 1008 | return error; |
2bd0ea18 NS |
1009 | error = xfs_da_shrink_inode(state->args, drop_blk->blkno, |
1010 | drop_blk->bp); | |
1011 | drop_blk->bp = NULL; | |
1012 | if (error) | |
af43ca9f | 1013 | return error; |
2bd0ea18 NS |
1014 | } |
1015 | /* | |
1016 | * We joined all the way to the top. If it turns out that | |
1017 | * we only have one entry in the root, make the child block | |
1018 | * the new root. | |
1019 | */ | |
88b32f06 DC |
1020 | xfs_da3_node_remove(state, drop_blk); |
1021 | xfs_da3_fixhashpath(state, &state->path); | |
1022 | error = xfs_da3_root_join(state, &state->path.blk[0]); | |
af43ca9f | 1023 | return error; |
2bd0ea18 NS |
1024 | } |
1025 | ||
a2ceac1f DC |
1026 | #ifdef DEBUG |
1027 | static void | |
1028 | xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo *blkinfo, __u16 level) | |
1029 | { | |
1030 | __be16 magic = blkinfo->magic; | |
1031 | ||
1032 | if (level == 1) { | |
1033 | ASSERT(magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) || | |
65b80c98 | 1034 | magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) || |
a24374f4 DC |
1035 | magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) || |
1036 | magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)); | |
88b32f06 DC |
1037 | } else { |
1038 | ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC) || | |
1039 | magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)); | |
1040 | } | |
a2ceac1f DC |
1041 | ASSERT(!blkinfo->forw); |
1042 | ASSERT(!blkinfo->back); | |
1043 | } | |
1044 | #else /* !DEBUG */ | |
1045 | #define xfs_da_blkinfo_onlychild_validate(blkinfo, level) | |
1046 | #endif /* !DEBUG */ | |
1047 | ||
2bd0ea18 | 1048 | /* |
dfc130f3 | 1049 | * We have only one entry in the root. Copy the only remaining child of |
2bd0ea18 NS |
1050 | * the old root to block 0 as the new root node. |
1051 | */ | |
1052 | STATIC int | |
88b32f06 DC |
1053 | xfs_da3_root_join( |
1054 | struct xfs_da_state *state, | |
1055 | struct xfs_da_state_blk *root_blk) | |
2bd0ea18 | 1056 | { |
88b32f06 DC |
1057 | struct xfs_da_intnode *oldroot; |
1058 | struct xfs_da_args *args; | |
1059 | xfs_dablk_t child; | |
1060 | struct xfs_buf *bp; | |
1061 | struct xfs_da3_icnode_hdr oldroothdr; | |
1062 | struct xfs_da_node_entry *btree; | |
1063 | int error; | |
ff105f75 | 1064 | struct xfs_inode *dp = state->args->dp; |
2bd0ea18 | 1065 | |
a2ceac1f DC |
1066 | trace_xfs_da_root_join(state->args); |
1067 | ||
2bd0ea18 | 1068 | ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC); |
88b32f06 DC |
1069 | |
1070 | args = state->args; | |
a2ceac1f | 1071 | oldroot = root_blk->bp->b_addr; |
ff105f75 | 1072 | dp->d_ops->node_hdr_from_disk(&oldroothdr, oldroot); |
88b32f06 DC |
1073 | ASSERT(oldroothdr.forw == 0); |
1074 | ASSERT(oldroothdr.back == 0); | |
2bd0ea18 NS |
1075 | |
1076 | /* | |
1077 | * If the root has more than one child, then don't do anything. | |
1078 | */ | |
88b32f06 DC |
1079 | if (oldroothdr.count > 1) |
1080 | return 0; | |
2bd0ea18 NS |
1081 | |
1082 | /* | |
1083 | * Read in the (only) child block, then copy those bytes into | |
1084 | * the root block's buffer and free the original child block. | |
1085 | */ | |
ff105f75 | 1086 | btree = dp->d_ops->node_tree_p(oldroot); |
88b32f06 | 1087 | child = be32_to_cpu(btree[0].before); |
2bd0ea18 | 1088 | ASSERT(child != 0); |
ff105f75 | 1089 | error = xfs_da3_node_read(args->trans, dp, child, -1, &bp, |
2bd0ea18 NS |
1090 | args->whichfork); |
1091 | if (error) | |
88b32f06 DC |
1092 | return error; |
1093 | xfs_da_blkinfo_onlychild_validate(bp->b_addr, oldroothdr.level); | |
a2ceac1f DC |
1094 | |
1095 | /* | |
1096 | * This could be copying a leaf back into the root block in the case of | |
1097 | * there only being a single leaf block left in the tree. Hence we have | |
1098 | * to update the b_ops pointer as well to match the buffer type change | |
88b32f06 DC |
1099 | * that could occur. For dir3 blocks we also need to update the block |
1100 | * number in the buffer header. | |
a2ceac1f | 1101 | */ |
ff105f75 | 1102 | memcpy(root_blk->bp->b_addr, bp->b_addr, args->geo->blksize); |
a2ceac1f | 1103 | root_blk->bp->b_ops = bp->b_ops; |
8b4dc4a9 | 1104 | xfs_trans_buf_copy_type(root_blk->bp, bp); |
88b32f06 DC |
1105 | if (oldroothdr.magic == XFS_DA3_NODE_MAGIC) { |
1106 | struct xfs_da3_blkinfo *da3 = root_blk->bp->b_addr; | |
1107 | da3->blkno = cpu_to_be64(root_blk->bp->b_bn); | |
1108 | } | |
ff105f75 DC |
1109 | xfs_trans_log_buf(args->trans, root_blk->bp, 0, |
1110 | args->geo->blksize - 1); | |
2bd0ea18 | 1111 | error = xfs_da_shrink_inode(args, child, bp); |
af43ca9f | 1112 | return error; |
2bd0ea18 NS |
1113 | } |
1114 | ||
1115 | /* | |
1116 | * Check a node block and its neighbors to see if the block should be | |
1117 | * collapsed into one or the other neighbor. Always keep the block | |
1118 | * with the smaller block number. | |
1119 | * If the current block is over 50% full, don't try to join it, return 0. | |
1120 | * If the block is empty, fill in the state structure and return 2. | |
1121 | * If it can be collapsed, fill in the state structure and return 1. | |
1122 | * If nothing can be done, return 0. | |
1123 | */ | |
1124 | STATIC int | |
88b32f06 DC |
1125 | xfs_da3_node_toosmall( |
1126 | struct xfs_da_state *state, | |
1127 | int *action) | |
2bd0ea18 | 1128 | { |
88b32f06 DC |
1129 | struct xfs_da_intnode *node; |
1130 | struct xfs_da_state_blk *blk; | |
1131 | struct xfs_da_blkinfo *info; | |
1132 | xfs_dablk_t blkno; | |
1133 | struct xfs_buf *bp; | |
1134 | struct xfs_da3_icnode_hdr nodehdr; | |
1135 | int count; | |
1136 | int forward; | |
1137 | int error; | |
1138 | int retval; | |
1139 | int i; | |
ff105f75 | 1140 | struct xfs_inode *dp = state->args->dp; |
a2ceac1f DC |
1141 | |
1142 | trace_xfs_da_node_toosmall(state->args); | |
2bd0ea18 NS |
1143 | |
1144 | /* | |
1145 | * Check for the degenerate case of the block being over 50% full. | |
1146 | * If so, it's not worth even looking to see if we might be able | |
1147 | * to coalesce with a sibling. | |
1148 | */ | |
1149 | blk = &state->path.blk[ state->path.active-1 ]; | |
a2ceac1f | 1150 | info = blk->bp->b_addr; |
2bd0ea18 | 1151 | node = (xfs_da_intnode_t *)info; |
ff105f75 DC |
1152 | dp->d_ops->node_hdr_from_disk(&nodehdr, node); |
1153 | if (nodehdr.count > (state->args->geo->node_ents >> 1)) { | |
4ca431fc | 1154 | *action = 0; /* blk over 50%, don't try to join */ |
af43ca9f | 1155 | return 0; /* blk over 50%, don't try to join */ |
2bd0ea18 NS |
1156 | } |
1157 | ||
1158 | /* | |
1159 | * Check for the degenerate case of the block being empty. | |
1160 | * If the block is empty, we'll simply delete it, no need to | |
5e656dbb | 1161 | * coalesce it with a sibling block. We choose (arbitrarily) |
2bd0ea18 NS |
1162 | * to merge with the forward block unless it is NULL. |
1163 | */ | |
88b32f06 | 1164 | if (nodehdr.count == 0) { |
2bd0ea18 NS |
1165 | /* |
1166 | * Make altpath point to the block we want to keep and | |
1167 | * path point to the block we want to drop (this one). | |
1168 | */ | |
5e656dbb | 1169 | forward = (info->forw != 0); |
32181a02 | 1170 | memcpy(&state->altpath, &state->path, sizeof(state->path)); |
88b32f06 | 1171 | error = xfs_da3_path_shift(state, &state->altpath, forward, |
2bd0ea18 NS |
1172 | 0, &retval); |
1173 | if (error) | |
af43ca9f | 1174 | return error; |
2bd0ea18 NS |
1175 | if (retval) { |
1176 | *action = 0; | |
1177 | } else { | |
1178 | *action = 2; | |
1179 | } | |
af43ca9f | 1180 | return 0; |
2bd0ea18 NS |
1181 | } |
1182 | ||
1183 | /* | |
1184 | * Examine each sibling block to see if we can coalesce with | |
1185 | * at least 25% free space to spare. We need to figure out | |
1186 | * whether to merge with the forward or the backward block. | |
1187 | * We prefer coalescing with the lower numbered sibling so as | |
1188 | * to shrink a directory over time. | |
1189 | */ | |
ff105f75 DC |
1190 | count = state->args->geo->node_ents; |
1191 | count -= state->args->geo->node_ents >> 2; | |
88b32f06 DC |
1192 | count -= nodehdr.count; |
1193 | ||
2bd0ea18 | 1194 | /* start with smaller blk num */ |
88b32f06 | 1195 | forward = nodehdr.forw < nodehdr.back; |
2bd0ea18 | 1196 | for (i = 0; i < 2; forward = !forward, i++) { |
c9522f4d | 1197 | struct xfs_da3_icnode_hdr thdr; |
2bd0ea18 | 1198 | if (forward) |
88b32f06 | 1199 | blkno = nodehdr.forw; |
2bd0ea18 | 1200 | else |
88b32f06 | 1201 | blkno = nodehdr.back; |
2bd0ea18 NS |
1202 | if (blkno == 0) |
1203 | continue; | |
ff105f75 | 1204 | error = xfs_da3_node_read(state->args->trans, dp, |
2bd0ea18 NS |
1205 | blkno, -1, &bp, state->args->whichfork); |
1206 | if (error) | |
af43ca9f | 1207 | return error; |
2bd0ea18 | 1208 | |
a2ceac1f | 1209 | node = bp->b_addr; |
ff105f75 | 1210 | dp->d_ops->node_hdr_from_disk(&thdr, node); |
a2ceac1f | 1211 | xfs_trans_brelse(state->args->trans, bp); |
88b32f06 | 1212 | |
c9522f4d | 1213 | if (count - thdr.count >= 0) |
2bd0ea18 NS |
1214 | break; /* fits with at least 25% to spare */ |
1215 | } | |
1216 | if (i >= 2) { | |
1217 | *action = 0; | |
88b32f06 | 1218 | return 0; |
2bd0ea18 NS |
1219 | } |
1220 | ||
1221 | /* | |
1222 | * Make altpath point to the block we want to keep (the lower | |
1223 | * numbered block) and path point to the block we want to drop. | |
1224 | */ | |
32181a02 | 1225 | memcpy(&state->altpath, &state->path, sizeof(state->path)); |
2bd0ea18 | 1226 | if (blkno < blk->blkno) { |
88b32f06 | 1227 | error = xfs_da3_path_shift(state, &state->altpath, forward, |
2bd0ea18 | 1228 | 0, &retval); |
2bd0ea18 | 1229 | } else { |
88b32f06 | 1230 | error = xfs_da3_path_shift(state, &state->path, forward, |
2bd0ea18 | 1231 | 0, &retval); |
88b32f06 DC |
1232 | } |
1233 | if (error) | |
1234 | return error; | |
1235 | if (retval) { | |
1236 | *action = 0; | |
1237 | return 0; | |
2bd0ea18 NS |
1238 | } |
1239 | *action = 1; | |
88b32f06 DC |
1240 | return 0; |
1241 | } | |
1242 | ||
1243 | /* | |
1244 | * Pick up the last hashvalue from an intermediate node. | |
1245 | */ | |
1246 | STATIC uint | |
1247 | xfs_da3_node_lasthash( | |
ff105f75 | 1248 | struct xfs_inode *dp, |
88b32f06 DC |
1249 | struct xfs_buf *bp, |
1250 | int *count) | |
1251 | { | |
1252 | struct xfs_da_intnode *node; | |
1253 | struct xfs_da_node_entry *btree; | |
1254 | struct xfs_da3_icnode_hdr nodehdr; | |
1255 | ||
1256 | node = bp->b_addr; | |
ff105f75 | 1257 | dp->d_ops->node_hdr_from_disk(&nodehdr, node); |
88b32f06 DC |
1258 | if (count) |
1259 | *count = nodehdr.count; | |
1260 | if (!nodehdr.count) | |
1261 | return 0; | |
ff105f75 | 1262 | btree = dp->d_ops->node_tree_p(node); |
88b32f06 | 1263 | return be32_to_cpu(btree[nodehdr.count - 1].hashval); |
2bd0ea18 NS |
1264 | } |
1265 | ||
2bd0ea18 NS |
1266 | /* |
1267 | * Walk back up the tree adjusting hash values as necessary, | |
1268 | * when we stop making changes, return. | |
1269 | */ | |
1270 | void | |
88b32f06 DC |
1271 | xfs_da3_fixhashpath( |
1272 | struct xfs_da_state *state, | |
1273 | struct xfs_da_state_path *path) | |
2bd0ea18 | 1274 | { |
88b32f06 DC |
1275 | struct xfs_da_state_blk *blk; |
1276 | struct xfs_da_intnode *node; | |
1277 | struct xfs_da_node_entry *btree; | |
1278 | xfs_dahash_t lasthash=0; | |
1279 | int level; | |
1280 | int count; | |
ff105f75 | 1281 | struct xfs_inode *dp = state->args->dp; |
2bd0ea18 | 1282 | |
a2ceac1f DC |
1283 | trace_xfs_da_fixhashpath(state->args); |
1284 | ||
2bd0ea18 NS |
1285 | level = path->active-1; |
1286 | blk = &path->blk[ level ]; | |
1287 | switch (blk->magic) { | |
2bd0ea18 NS |
1288 | case XFS_ATTR_LEAF_MAGIC: |
1289 | lasthash = xfs_attr_leaf_lasthash(blk->bp, &count); | |
1290 | if (count == 0) | |
1291 | return; | |
1292 | break; | |
2bd0ea18 | 1293 | case XFS_DIR2_LEAFN_MAGIC: |
94ffb80a | 1294 | lasthash = xfs_dir2_leaf_lasthash(dp, blk->bp, &count); |
2bd0ea18 NS |
1295 | if (count == 0) |
1296 | return; | |
1297 | break; | |
1298 | case XFS_DA_NODE_MAGIC: | |
ff105f75 | 1299 | lasthash = xfs_da3_node_lasthash(dp, blk->bp, &count); |
2bd0ea18 NS |
1300 | if (count == 0) |
1301 | return; | |
1302 | break; | |
1303 | } | |
1304 | for (blk--, level--; level >= 0; blk--, level--) { | |
88b32f06 DC |
1305 | struct xfs_da3_icnode_hdr nodehdr; |
1306 | ||
a2ceac1f | 1307 | node = blk->bp->b_addr; |
ff105f75 DC |
1308 | dp->d_ops->node_hdr_from_disk(&nodehdr, node); |
1309 | btree = dp->d_ops->node_tree_p(node); | |
0a3717c9 | 1310 | if (be32_to_cpu(btree[blk->index].hashval) == lasthash) |
2bd0ea18 NS |
1311 | break; |
1312 | blk->hashval = lasthash; | |
88b32f06 | 1313 | btree[blk->index].hashval = cpu_to_be32(lasthash); |
a2ceac1f | 1314 | xfs_trans_log_buf(state->args->trans, blk->bp, |
88b32f06 DC |
1315 | XFS_DA_LOGRANGE(node, &btree[blk->index], |
1316 | sizeof(*btree))); | |
2bd0ea18 | 1317 | |
88b32f06 | 1318 | lasthash = be32_to_cpu(btree[nodehdr.count - 1].hashval); |
2bd0ea18 NS |
1319 | } |
1320 | } | |
1321 | ||
2bd0ea18 NS |
1322 | /* |
1323 | * Remove an entry from an intermediate node. | |
1324 | */ | |
1325 | STATIC void | |
88b32f06 DC |
1326 | xfs_da3_node_remove( |
1327 | struct xfs_da_state *state, | |
1328 | struct xfs_da_state_blk *drop_blk) | |
2bd0ea18 | 1329 | { |
88b32f06 DC |
1330 | struct xfs_da_intnode *node; |
1331 | struct xfs_da3_icnode_hdr nodehdr; | |
1332 | struct xfs_da_node_entry *btree; | |
1333 | int index; | |
1334 | int tmp; | |
ff105f75 | 1335 | struct xfs_inode *dp = state->args->dp; |
2bd0ea18 | 1336 | |
a2ceac1f DC |
1337 | trace_xfs_da_node_remove(state->args); |
1338 | ||
1339 | node = drop_blk->bp->b_addr; | |
ff105f75 | 1340 | dp->d_ops->node_hdr_from_disk(&nodehdr, node); |
88b32f06 | 1341 | ASSERT(drop_blk->index < nodehdr.count); |
2bd0ea18 NS |
1342 | ASSERT(drop_blk->index >= 0); |
1343 | ||
1344 | /* | |
1345 | * Copy over the offending entry, or just zero it out. | |
1346 | */ | |
88b32f06 | 1347 | index = drop_blk->index; |
ff105f75 | 1348 | btree = dp->d_ops->node_tree_p(node); |
88b32f06 DC |
1349 | if (index < nodehdr.count - 1) { |
1350 | tmp = nodehdr.count - index - 1; | |
2bd0ea18 | 1351 | tmp *= (uint)sizeof(xfs_da_node_entry_t); |
88b32f06 | 1352 | memmove(&btree[index], &btree[index + 1], tmp); |
a2ceac1f | 1353 | xfs_trans_log_buf(state->args->trans, drop_blk->bp, |
88b32f06 DC |
1354 | XFS_DA_LOGRANGE(node, &btree[index], tmp)); |
1355 | index = nodehdr.count - 1; | |
2bd0ea18 | 1356 | } |
88b32f06 | 1357 | memset(&btree[index], 0, sizeof(xfs_da_node_entry_t)); |
a2ceac1f | 1358 | xfs_trans_log_buf(state->args->trans, drop_blk->bp, |
88b32f06 DC |
1359 | XFS_DA_LOGRANGE(node, &btree[index], sizeof(btree[index]))); |
1360 | nodehdr.count -= 1; | |
ff105f75 | 1361 | dp->d_ops->node_hdr_to_disk(node, &nodehdr); |
a2ceac1f | 1362 | xfs_trans_log_buf(state->args->trans, drop_blk->bp, |
ff105f75 | 1363 | XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size)); |
2bd0ea18 NS |
1364 | |
1365 | /* | |
1366 | * Copy the last hash value from the block to propagate upwards. | |
1367 | */ | |
88b32f06 | 1368 | drop_blk->hashval = be32_to_cpu(btree[index - 1].hashval); |
2bd0ea18 NS |
1369 | } |
1370 | ||
1371 | /* | |
88b32f06 | 1372 | * Unbalance the elements between two intermediate nodes, |
2bd0ea18 NS |
1373 | * move all Btree elements from one node into another. |
1374 | */ | |
1375 | STATIC void | |
88b32f06 DC |
1376 | xfs_da3_node_unbalance( |
1377 | struct xfs_da_state *state, | |
1378 | struct xfs_da_state_blk *drop_blk, | |
1379 | struct xfs_da_state_blk *save_blk) | |
2bd0ea18 | 1380 | { |
88b32f06 DC |
1381 | struct xfs_da_intnode *drop_node; |
1382 | struct xfs_da_intnode *save_node; | |
7b02556d DC |
1383 | struct xfs_da_node_entry *drop_btree; |
1384 | struct xfs_da_node_entry *save_btree; | |
1385 | struct xfs_da3_icnode_hdr drop_hdr; | |
1386 | struct xfs_da3_icnode_hdr save_hdr; | |
88b32f06 DC |
1387 | struct xfs_trans *tp; |
1388 | int sindex; | |
1389 | int tmp; | |
ff105f75 | 1390 | struct xfs_inode *dp = state->args->dp; |
2bd0ea18 | 1391 | |
a2ceac1f DC |
1392 | trace_xfs_da_node_unbalance(state->args); |
1393 | ||
1394 | drop_node = drop_blk->bp->b_addr; | |
1395 | save_node = save_blk->bp->b_addr; | |
ff105f75 DC |
1396 | dp->d_ops->node_hdr_from_disk(&drop_hdr, drop_node); |
1397 | dp->d_ops->node_hdr_from_disk(&save_hdr, save_node); | |
1398 | drop_btree = dp->d_ops->node_tree_p(drop_node); | |
1399 | save_btree = dp->d_ops->node_tree_p(save_node); | |
2bd0ea18 NS |
1400 | tp = state->args->trans; |
1401 | ||
1402 | /* | |
1403 | * If the dying block has lower hashvals, then move all the | |
1404 | * elements in the remaining block up to make a hole. | |
1405 | */ | |
7b02556d DC |
1406 | if ((be32_to_cpu(drop_btree[0].hashval) < |
1407 | be32_to_cpu(save_btree[0].hashval)) || | |
1408 | (be32_to_cpu(drop_btree[drop_hdr.count - 1].hashval) < | |
1409 | be32_to_cpu(save_btree[save_hdr.count - 1].hashval))) { | |
88b32f06 | 1410 | /* XXX: check this - is memmove dst correct? */ |
7b02556d DC |
1411 | tmp = save_hdr.count * sizeof(xfs_da_node_entry_t); |
1412 | memmove(&save_btree[drop_hdr.count], &save_btree[0], tmp); | |
88b32f06 DC |
1413 | |
1414 | sindex = 0; | |
a2ceac1f | 1415 | xfs_trans_log_buf(tp, save_blk->bp, |
7b02556d DC |
1416 | XFS_DA_LOGRANGE(save_node, &save_btree[0], |
1417 | (save_hdr.count + drop_hdr.count) * | |
88b32f06 | 1418 | sizeof(xfs_da_node_entry_t))); |
2bd0ea18 | 1419 | } else { |
7b02556d | 1420 | sindex = save_hdr.count; |
a2ceac1f | 1421 | xfs_trans_log_buf(tp, save_blk->bp, |
7b02556d DC |
1422 | XFS_DA_LOGRANGE(save_node, &save_btree[sindex], |
1423 | drop_hdr.count * sizeof(xfs_da_node_entry_t))); | |
2bd0ea18 NS |
1424 | } |
1425 | ||
1426 | /* | |
1427 | * Move all the B-tree elements from drop_blk to save_blk. | |
1428 | */ | |
7b02556d DC |
1429 | tmp = drop_hdr.count * (uint)sizeof(xfs_da_node_entry_t); |
1430 | memcpy(&save_btree[sindex], &drop_btree[0], tmp); | |
1431 | save_hdr.count += drop_hdr.count; | |
2bd0ea18 | 1432 | |
ff105f75 | 1433 | dp->d_ops->node_hdr_to_disk(save_node, &save_hdr); |
a2ceac1f | 1434 | xfs_trans_log_buf(tp, save_blk->bp, |
2bd0ea18 | 1435 | XFS_DA_LOGRANGE(save_node, &save_node->hdr, |
ff105f75 | 1436 | dp->d_ops->node_hdr_size)); |
2bd0ea18 NS |
1437 | |
1438 | /* | |
1439 | * Save the last hashval in the remaining block for upward propagation. | |
1440 | */ | |
7b02556d | 1441 | save_blk->hashval = be32_to_cpu(save_btree[save_hdr.count - 1].hashval); |
2bd0ea18 NS |
1442 | } |
1443 | ||
2bd0ea18 NS |
1444 | /*======================================================================== |
1445 | * Routines used for finding things in the Btree. | |
1446 | *========================================================================*/ | |
1447 | ||
1448 | /* | |
1449 | * Walk down the Btree looking for a particular filename, filling | |
1450 | * in the state structure as we go. | |
1451 | * | |
1452 | * We will set the state structure to point to each of the elements | |
1453 | * in each of the nodes where either the hashval is or should be. | |
1454 | * | |
1455 | * We support duplicate hashval's so for each entry in the current | |
1456 | * node that could contain the desired hashval, descend. This is a | |
1457 | * pruned depth-first tree search. | |
1458 | */ | |
1459 | int /* error */ | |
88b32f06 DC |
1460 | xfs_da3_node_lookup_int( |
1461 | struct xfs_da_state *state, | |
1462 | int *result) | |
2bd0ea18 | 1463 | { |
88b32f06 DC |
1464 | struct xfs_da_state_blk *blk; |
1465 | struct xfs_da_blkinfo *curr; | |
1466 | struct xfs_da_intnode *node; | |
1467 | struct xfs_da_node_entry *btree; | |
1468 | struct xfs_da3_icnode_hdr nodehdr; | |
1469 | struct xfs_da_args *args; | |
1470 | xfs_dablk_t blkno; | |
1471 | xfs_dahash_t hashval; | |
1472 | xfs_dahash_t btreehashval; | |
1473 | int probe; | |
1474 | int span; | |
1475 | int max; | |
1476 | int error; | |
1477 | int retval; | |
9473ee32 | 1478 | unsigned int expected_level = 0; |
ea09a747 | 1479 | uint16_t magic; |
ff105f75 | 1480 | struct xfs_inode *dp = state->args->dp; |
2bd0ea18 NS |
1481 | |
1482 | args = state->args; | |
32a82561 | 1483 | |
2bd0ea18 NS |
1484 | /* |
1485 | * Descend thru the B-tree searching each level for the right | |
1486 | * node to use, until the right hashval is found. | |
1487 | */ | |
9473ee32 | 1488 | blkno = args->geo->leafblk; |
2bd0ea18 NS |
1489 | for (blk = &state->path.blk[0], state->path.active = 1; |
1490 | state->path.active <= XFS_DA_NODE_MAXDEPTH; | |
1491 | blk++, state->path.active++) { | |
1492 | /* | |
1493 | * Read the next node down in the tree. | |
1494 | */ | |
1495 | blk->blkno = blkno; | |
88b32f06 | 1496 | error = xfs_da3_node_read(args->trans, args->dp, blkno, |
32a82561 | 1497 | -1, &blk->bp, args->whichfork); |
2bd0ea18 NS |
1498 | if (error) { |
1499 | blk->blkno = 0; | |
1500 | state->path.active--; | |
af43ca9f | 1501 | return error; |
2bd0ea18 | 1502 | } |
a2ceac1f | 1503 | curr = blk->bp->b_addr; |
ea09a747 | 1504 | magic = be16_to_cpu(curr->magic); |
88b32f06 | 1505 | |
ea09a747 DW |
1506 | if (magic == XFS_ATTR_LEAF_MAGIC || |
1507 | magic == XFS_ATTR3_LEAF_MAGIC) { | |
a24374f4 | 1508 | blk->magic = XFS_ATTR_LEAF_MAGIC; |
88b32f06 DC |
1509 | blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL); |
1510 | break; | |
1511 | } | |
1512 | ||
ea09a747 DW |
1513 | if (magic == XFS_DIR2_LEAFN_MAGIC || |
1514 | magic == XFS_DIR3_LEAFN_MAGIC) { | |
88b32f06 | 1515 | blk->magic = XFS_DIR2_LEAFN_MAGIC; |
94ffb80a DW |
1516 | blk->hashval = xfs_dir2_leaf_lasthash(args->dp, |
1517 | blk->bp, NULL); | |
88b32f06 DC |
1518 | break; |
1519 | } | |
1520 | ||
b403e029 DW |
1521 | if (magic != XFS_DA_NODE_MAGIC && magic != XFS_DA3_NODE_MAGIC) |
1522 | return -EFSCORRUPTED; | |
88b32f06 | 1523 | |
b403e029 | 1524 | blk->magic = XFS_DA_NODE_MAGIC; |
2bd0ea18 NS |
1525 | |
1526 | /* | |
1527 | * Search an intermediate node for a match. | |
1528 | */ | |
88b32f06 | 1529 | node = blk->bp->b_addr; |
ff105f75 DC |
1530 | dp->d_ops->node_hdr_from_disk(&nodehdr, node); |
1531 | btree = dp->d_ops->node_tree_p(node); | |
2bd0ea18 | 1532 | |
9473ee32 DW |
1533 | /* Tree taller than we can handle; bail out! */ |
1534 | if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH) | |
1535 | return -EFSCORRUPTED; | |
1536 | ||
1537 | /* Check the level from the root. */ | |
1538 | if (blkno == args->geo->leafblk) | |
1539 | expected_level = nodehdr.level - 1; | |
1540 | else if (expected_level != nodehdr.level) | |
1541 | return -EFSCORRUPTED; | |
1542 | else | |
1543 | expected_level--; | |
1544 | ||
88b32f06 DC |
1545 | max = nodehdr.count; |
1546 | blk->hashval = be32_to_cpu(btree[max - 1].hashval); | |
2bd0ea18 | 1547 | |
88b32f06 DC |
1548 | /* |
1549 | * Binary search. (note: small blocks will skip loop) | |
1550 | */ | |
1551 | probe = span = max / 2; | |
1552 | hashval = args->hashval; | |
1553 | while (span > 4) { | |
1554 | span /= 2; | |
1555 | btreehashval = be32_to_cpu(btree[probe].hashval); | |
1556 | if (btreehashval < hashval) | |
1557 | probe += span; | |
1558 | else if (btreehashval > hashval) | |
1559 | probe -= span; | |
1560 | else | |
1561 | break; | |
1562 | } | |
1563 | ASSERT((probe >= 0) && (probe < max)); | |
1564 | ASSERT((span <= 4) || | |
1565 | (be32_to_cpu(btree[probe].hashval) == hashval)); | |
2bd0ea18 | 1566 | |
88b32f06 DC |
1567 | /* |
1568 | * Since we may have duplicate hashval's, find the first | |
1569 | * matching hashval in the node. | |
1570 | */ | |
1571 | while (probe > 0 && | |
1572 | be32_to_cpu(btree[probe].hashval) >= hashval) { | |
1573 | probe--; | |
1574 | } | |
1575 | while (probe < max && | |
1576 | be32_to_cpu(btree[probe].hashval) < hashval) { | |
1577 | probe++; | |
1578 | } | |
1579 | ||
1580 | /* | |
1581 | * Pick the right block to descend on. | |
1582 | */ | |
1583 | if (probe == max) { | |
1584 | blk->index = max - 1; | |
1585 | blkno = be32_to_cpu(btree[max - 1].before); | |
1586 | } else { | |
1587 | blk->index = probe; | |
1588 | blkno = be32_to_cpu(btree[probe].before); | |
2bd0ea18 | 1589 | } |
9473ee32 DW |
1590 | |
1591 | /* We can't point back to the root. */ | |
1592 | if (blkno == args->geo->leafblk) | |
1593 | return -EFSCORRUPTED; | |
2bd0ea18 NS |
1594 | } |
1595 | ||
9473ee32 DW |
1596 | if (expected_level != 0) |
1597 | return -EFSCORRUPTED; | |
1598 | ||
2bd0ea18 NS |
1599 | /* |
1600 | * A leaf block that ends in the hashval that we are interested in | |
1601 | * (final hashval == search hashval) means that the next block may | |
1602 | * contain more entries with the same hashval, shift upward to the | |
1603 | * next leaf and keep searching. | |
1604 | */ | |
1605 | for (;;) { | |
5e656dbb | 1606 | if (blk->magic == XFS_DIR2_LEAFN_MAGIC) { |
32a82561 | 1607 | retval = xfs_dir2_leafn_lookup_int(blk->bp, args, |
2bd0ea18 | 1608 | &blk->index, state); |
5e656dbb | 1609 | } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) { |
a24374f4 | 1610 | retval = xfs_attr3_leaf_lookup_int(blk->bp, args); |
32a82561 NS |
1611 | blk->index = args->index; |
1612 | args->blkno = blk->blkno; | |
5e656dbb BN |
1613 | } else { |
1614 | ASSERT(0); | |
12b53197 | 1615 | return -EFSCORRUPTED; |
2bd0ea18 | 1616 | } |
12b53197 | 1617 | if (((retval == -ENOENT) || (retval == -ENOATTR)) && |
32a82561 | 1618 | (blk->hashval == args->hashval)) { |
88b32f06 | 1619 | error = xfs_da3_path_shift(state, &state->path, 1, 1, |
2bd0ea18 NS |
1620 | &retval); |
1621 | if (error) | |
af43ca9f | 1622 | return error; |
2bd0ea18 NS |
1623 | if (retval == 0) { |
1624 | continue; | |
5e656dbb | 1625 | } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) { |
2bd0ea18 | 1626 | /* path_shift() gives ENOENT */ |
12b53197 | 1627 | retval = -ENOATTR; |
2bd0ea18 | 1628 | } |
2bd0ea18 NS |
1629 | } |
1630 | break; | |
1631 | } | |
1632 | *result = retval; | |
af43ca9f | 1633 | return 0; |
2bd0ea18 NS |
1634 | } |
1635 | ||
2bd0ea18 NS |
1636 | /*======================================================================== |
1637 | * Utility routines. | |
1638 | *========================================================================*/ | |
1639 | ||
88b32f06 DC |
1640 | /* |
1641 | * Compare two intermediate nodes for "order". | |
1642 | */ | |
1643 | STATIC int | |
1644 | xfs_da3_node_order( | |
ff105f75 | 1645 | struct xfs_inode *dp, |
88b32f06 DC |
1646 | struct xfs_buf *node1_bp, |
1647 | struct xfs_buf *node2_bp) | |
1648 | { | |
1649 | struct xfs_da_intnode *node1; | |
1650 | struct xfs_da_intnode *node2; | |
1651 | struct xfs_da_node_entry *btree1; | |
1652 | struct xfs_da_node_entry *btree2; | |
1653 | struct xfs_da3_icnode_hdr node1hdr; | |
1654 | struct xfs_da3_icnode_hdr node2hdr; | |
1655 | ||
1656 | node1 = node1_bp->b_addr; | |
1657 | node2 = node2_bp->b_addr; | |
ff105f75 DC |
1658 | dp->d_ops->node_hdr_from_disk(&node1hdr, node1); |
1659 | dp->d_ops->node_hdr_from_disk(&node2hdr, node2); | |
1660 | btree1 = dp->d_ops->node_tree_p(node1); | |
1661 | btree2 = dp->d_ops->node_tree_p(node2); | |
88b32f06 DC |
1662 | |
1663 | if (node1hdr.count > 0 && node2hdr.count > 0 && | |
1664 | ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) || | |
1665 | (be32_to_cpu(btree2[node2hdr.count - 1].hashval) < | |
1666 | be32_to_cpu(btree1[node1hdr.count - 1].hashval)))) { | |
1667 | return 1; | |
1668 | } | |
1669 | return 0; | |
1670 | } | |
1671 | ||
2bd0ea18 NS |
1672 | /* |
1673 | * Link a new block into a doubly linked list of blocks (of whatever type). | |
1674 | */ | |
1675 | int /* error */ | |
88b32f06 DC |
1676 | xfs_da3_blk_link( |
1677 | struct xfs_da_state *state, | |
1678 | struct xfs_da_state_blk *old_blk, | |
1679 | struct xfs_da_state_blk *new_blk) | |
2bd0ea18 | 1680 | { |
88b32f06 DC |
1681 | struct xfs_da_blkinfo *old_info; |
1682 | struct xfs_da_blkinfo *new_info; | |
1683 | struct xfs_da_blkinfo *tmp_info; | |
1684 | struct xfs_da_args *args; | |
1685 | struct xfs_buf *bp; | |
1686 | int before = 0; | |
1687 | int error; | |
ff105f75 | 1688 | struct xfs_inode *dp = state->args->dp; |
2bd0ea18 NS |
1689 | |
1690 | /* | |
1691 | * Set up environment. | |
1692 | */ | |
1693 | args = state->args; | |
1694 | ASSERT(args != NULL); | |
a2ceac1f DC |
1695 | old_info = old_blk->bp->b_addr; |
1696 | new_info = new_blk->bp->b_addr; | |
2bd0ea18 | 1697 | ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC || |
5e656dbb | 1698 | old_blk->magic == XFS_DIR2_LEAFN_MAGIC || |
2bd0ea18 | 1699 | old_blk->magic == XFS_ATTR_LEAF_MAGIC); |
2bd0ea18 NS |
1700 | |
1701 | switch (old_blk->magic) { | |
2bd0ea18 NS |
1702 | case XFS_ATTR_LEAF_MAGIC: |
1703 | before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp); | |
1704 | break; | |
2bd0ea18 | 1705 | case XFS_DIR2_LEAFN_MAGIC: |
ff105f75 | 1706 | before = xfs_dir2_leafn_order(dp, old_blk->bp, new_blk->bp); |
2bd0ea18 NS |
1707 | break; |
1708 | case XFS_DA_NODE_MAGIC: | |
ff105f75 | 1709 | before = xfs_da3_node_order(dp, old_blk->bp, new_blk->bp); |
2bd0ea18 NS |
1710 | break; |
1711 | } | |
1712 | ||
1713 | /* | |
1714 | * Link blocks in appropriate order. | |
1715 | */ | |
1716 | if (before) { | |
1717 | /* | |
1718 | * Link new block in before existing block. | |
1719 | */ | |
a2ceac1f | 1720 | trace_xfs_da_link_before(args); |
5e656dbb BN |
1721 | new_info->forw = cpu_to_be32(old_blk->blkno); |
1722 | new_info->back = old_info->back; | |
1723 | if (old_info->back) { | |
ff105f75 | 1724 | error = xfs_da3_node_read(args->trans, dp, |
5e656dbb BN |
1725 | be32_to_cpu(old_info->back), |
1726 | -1, &bp, args->whichfork); | |
2bd0ea18 | 1727 | if (error) |
af43ca9f | 1728 | return error; |
2bd0ea18 | 1729 | ASSERT(bp != NULL); |
a2ceac1f | 1730 | tmp_info = bp->b_addr; |
88b32f06 | 1731 | ASSERT(tmp_info->magic == old_info->magic); |
5e656dbb BN |
1732 | ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno); |
1733 | tmp_info->forw = cpu_to_be32(new_blk->blkno); | |
a2ceac1f | 1734 | xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1); |
2bd0ea18 | 1735 | } |
5e656dbb | 1736 | old_info->back = cpu_to_be32(new_blk->blkno); |
2bd0ea18 NS |
1737 | } else { |
1738 | /* | |
1739 | * Link new block in after existing block. | |
1740 | */ | |
a2ceac1f | 1741 | trace_xfs_da_link_after(args); |
5e656dbb BN |
1742 | new_info->forw = old_info->forw; |
1743 | new_info->back = cpu_to_be32(old_blk->blkno); | |
1744 | if (old_info->forw) { | |
ff105f75 | 1745 | error = xfs_da3_node_read(args->trans, dp, |
5e656dbb BN |
1746 | be32_to_cpu(old_info->forw), |
1747 | -1, &bp, args->whichfork); | |
2bd0ea18 | 1748 | if (error) |
af43ca9f | 1749 | return error; |
2bd0ea18 | 1750 | ASSERT(bp != NULL); |
a2ceac1f | 1751 | tmp_info = bp->b_addr; |
5e656dbb BN |
1752 | ASSERT(tmp_info->magic == old_info->magic); |
1753 | ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno); | |
1754 | tmp_info->back = cpu_to_be32(new_blk->blkno); | |
a2ceac1f | 1755 | xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1); |
2bd0ea18 | 1756 | } |
5e656dbb | 1757 | old_info->forw = cpu_to_be32(new_blk->blkno); |
2bd0ea18 NS |
1758 | } |
1759 | ||
a2ceac1f DC |
1760 | xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1); |
1761 | xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1); | |
af43ca9f | 1762 | return 0; |
2bd0ea18 NS |
1763 | } |
1764 | ||
2bd0ea18 NS |
1765 | /* |
1766 | * Unlink a block from a doubly linked list of blocks. | |
1767 | */ | |
5e656dbb | 1768 | STATIC int /* error */ |
88b32f06 DC |
1769 | xfs_da3_blk_unlink( |
1770 | struct xfs_da_state *state, | |
1771 | struct xfs_da_state_blk *drop_blk, | |
1772 | struct xfs_da_state_blk *save_blk) | |
2bd0ea18 | 1773 | { |
88b32f06 DC |
1774 | struct xfs_da_blkinfo *drop_info; |
1775 | struct xfs_da_blkinfo *save_info; | |
1776 | struct xfs_da_blkinfo *tmp_info; | |
1777 | struct xfs_da_args *args; | |
1778 | struct xfs_buf *bp; | |
1779 | int error; | |
2bd0ea18 NS |
1780 | |
1781 | /* | |
1782 | * Set up environment. | |
1783 | */ | |
1784 | args = state->args; | |
1785 | ASSERT(args != NULL); | |
a2ceac1f DC |
1786 | save_info = save_blk->bp->b_addr; |
1787 | drop_info = drop_blk->bp->b_addr; | |
2bd0ea18 | 1788 | ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC || |
5e656dbb | 1789 | save_blk->magic == XFS_DIR2_LEAFN_MAGIC || |
2bd0ea18 | 1790 | save_blk->magic == XFS_ATTR_LEAF_MAGIC); |
2bd0ea18 | 1791 | ASSERT(save_blk->magic == drop_blk->magic); |
5e656dbb BN |
1792 | ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) || |
1793 | (be32_to_cpu(save_info->back) == drop_blk->blkno)); | |
1794 | ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) || | |
1795 | (be32_to_cpu(drop_info->back) == save_blk->blkno)); | |
2bd0ea18 NS |
1796 | |
1797 | /* | |
1798 | * Unlink the leaf block from the doubly linked chain of leaves. | |
1799 | */ | |
5e656dbb | 1800 | if (be32_to_cpu(save_info->back) == drop_blk->blkno) { |
a2ceac1f | 1801 | trace_xfs_da_unlink_back(args); |
5e656dbb BN |
1802 | save_info->back = drop_info->back; |
1803 | if (drop_info->back) { | |
88b32f06 | 1804 | error = xfs_da3_node_read(args->trans, args->dp, |
5e656dbb BN |
1805 | be32_to_cpu(drop_info->back), |
1806 | -1, &bp, args->whichfork); | |
2bd0ea18 | 1807 | if (error) |
af43ca9f | 1808 | return error; |
2bd0ea18 | 1809 | ASSERT(bp != NULL); |
a2ceac1f | 1810 | tmp_info = bp->b_addr; |
5e656dbb BN |
1811 | ASSERT(tmp_info->magic == save_info->magic); |
1812 | ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno); | |
1813 | tmp_info->forw = cpu_to_be32(save_blk->blkno); | |
a2ceac1f | 1814 | xfs_trans_log_buf(args->trans, bp, 0, |
2bd0ea18 | 1815 | sizeof(*tmp_info) - 1); |
2bd0ea18 NS |
1816 | } |
1817 | } else { | |
a2ceac1f | 1818 | trace_xfs_da_unlink_forward(args); |
5e656dbb BN |
1819 | save_info->forw = drop_info->forw; |
1820 | if (drop_info->forw) { | |
88b32f06 | 1821 | error = xfs_da3_node_read(args->trans, args->dp, |
5e656dbb BN |
1822 | be32_to_cpu(drop_info->forw), |
1823 | -1, &bp, args->whichfork); | |
2bd0ea18 | 1824 | if (error) |
af43ca9f | 1825 | return error; |
2bd0ea18 | 1826 | ASSERT(bp != NULL); |
a2ceac1f | 1827 | tmp_info = bp->b_addr; |
5e656dbb BN |
1828 | ASSERT(tmp_info->magic == save_info->magic); |
1829 | ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno); | |
1830 | tmp_info->back = cpu_to_be32(save_blk->blkno); | |
a2ceac1f | 1831 | xfs_trans_log_buf(args->trans, bp, 0, |
2bd0ea18 | 1832 | sizeof(*tmp_info) - 1); |
2bd0ea18 NS |
1833 | } |
1834 | } | |
1835 | ||
a2ceac1f | 1836 | xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1); |
af43ca9f | 1837 | return 0; |
2bd0ea18 NS |
1838 | } |
1839 | ||
1840 | /* | |
1841 | * Move a path "forward" or "!forward" one block at the current level. | |
1842 | * | |
1843 | * This routine will adjust a "path" to point to the next block | |
1844 | * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the | |
1845 | * Btree, including updating pointers to the intermediate nodes between | |
1846 | * the new bottom and the root. | |
1847 | */ | |
1848 | int /* error */ | |
88b32f06 DC |
1849 | xfs_da3_path_shift( |
1850 | struct xfs_da_state *state, | |
1851 | struct xfs_da_state_path *path, | |
1852 | int forward, | |
1853 | int release, | |
1854 | int *result) | |
2bd0ea18 | 1855 | { |
88b32f06 DC |
1856 | struct xfs_da_state_blk *blk; |
1857 | struct xfs_da_blkinfo *info; | |
1858 | struct xfs_da_intnode *node; | |
1859 | struct xfs_da_args *args; | |
1860 | struct xfs_da_node_entry *btree; | |
1861 | struct xfs_da3_icnode_hdr nodehdr; | |
c063b960 | 1862 | struct xfs_buf *bp; |
88b32f06 DC |
1863 | xfs_dablk_t blkno = 0; |
1864 | int level; | |
1865 | int error; | |
ff105f75 | 1866 | struct xfs_inode *dp = state->args->dp; |
2bd0ea18 | 1867 | |
a2ceac1f DC |
1868 | trace_xfs_da_path_shift(state->args); |
1869 | ||
2bd0ea18 NS |
1870 | /* |
1871 | * Roll up the Btree looking for the first block where our | |
1872 | * current index is not at the edge of the block. Note that | |
1873 | * we skip the bottom layer because we want the sibling block. | |
1874 | */ | |
1875 | args = state->args; | |
1876 | ASSERT(args != NULL); | |
1877 | ASSERT(path != NULL); | |
1878 | ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); | |
1879 | level = (path->active-1) - 1; /* skip bottom layer in path */ | |
1880 | for (blk = &path->blk[level]; level >= 0; blk--, level--) { | |
a2ceac1f | 1881 | node = blk->bp->b_addr; |
ff105f75 DC |
1882 | dp->d_ops->node_hdr_from_disk(&nodehdr, node); |
1883 | btree = dp->d_ops->node_tree_p(node); | |
88b32f06 DC |
1884 | |
1885 | if (forward && (blk->index < nodehdr.count - 1)) { | |
2bd0ea18 | 1886 | blk->index++; |
88b32f06 | 1887 | blkno = be32_to_cpu(btree[blk->index].before); |
2bd0ea18 NS |
1888 | break; |
1889 | } else if (!forward && (blk->index > 0)) { | |
1890 | blk->index--; | |
88b32f06 | 1891 | blkno = be32_to_cpu(btree[blk->index].before); |
2bd0ea18 NS |
1892 | break; |
1893 | } | |
1894 | } | |
1895 | if (level < 0) { | |
12b53197 | 1896 | *result = -ENOENT; /* we're out of our tree */ |
5e656dbb | 1897 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
af43ca9f | 1898 | return 0; |
2bd0ea18 NS |
1899 | } |
1900 | ||
1901 | /* | |
1902 | * Roll down the edge of the subtree until we reach the | |
1903 | * same depth we were at originally. | |
1904 | */ | |
1905 | for (blk++, level++; level < path->active; blk++, level++) { | |
1906 | /* | |
c063b960 | 1907 | * Read the next child block into a local buffer. |
2bd0ea18 | 1908 | */ |
c063b960 BF |
1909 | error = xfs_da3_node_read(args->trans, dp, blkno, -1, &bp, |
1910 | args->whichfork); | |
1911 | if (error) | |
1912 | return error; | |
2bd0ea18 NS |
1913 | |
1914 | /* | |
c063b960 BF |
1915 | * Release the old block (if it's dirty, the trans doesn't |
1916 | * actually let go) and swap the local buffer into the path | |
1917 | * structure. This ensures failure of the above read doesn't set | |
1918 | * a NULL buffer in an active slot in the path. | |
2bd0ea18 | 1919 | */ |
c063b960 BF |
1920 | if (release) |
1921 | xfs_trans_brelse(args->trans, blk->bp); | |
2bd0ea18 | 1922 | blk->blkno = blkno; |
c063b960 BF |
1923 | blk->bp = bp; |
1924 | ||
a2ceac1f DC |
1925 | info = blk->bp->b_addr; |
1926 | ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) || | |
88b32f06 | 1927 | info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) || |
a2ceac1f | 1928 | info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) || |
65b80c98 | 1929 | info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) || |
a24374f4 DC |
1930 | info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) || |
1931 | info->magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)); | |
88b32f06 DC |
1932 | |
1933 | ||
1934 | /* | |
1935 | * Note: we flatten the magic number to a single type so we | |
1936 | * don't have to compare against crc/non-crc types elsewhere. | |
1937 | */ | |
1938 | switch (be16_to_cpu(info->magic)) { | |
1939 | case XFS_DA_NODE_MAGIC: | |
1940 | case XFS_DA3_NODE_MAGIC: | |
1941 | blk->magic = XFS_DA_NODE_MAGIC; | |
2bd0ea18 | 1942 | node = (xfs_da_intnode_t *)info; |
ff105f75 DC |
1943 | dp->d_ops->node_hdr_from_disk(&nodehdr, node); |
1944 | btree = dp->d_ops->node_tree_p(node); | |
88b32f06 | 1945 | blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval); |
2bd0ea18 NS |
1946 | if (forward) |
1947 | blk->index = 0; | |
1948 | else | |
88b32f06 DC |
1949 | blk->index = nodehdr.count - 1; |
1950 | blkno = be32_to_cpu(btree[blk->index].before); | |
1951 | break; | |
1952 | case XFS_ATTR_LEAF_MAGIC: | |
a24374f4 | 1953 | case XFS_ATTR3_LEAF_MAGIC: |
88b32f06 | 1954 | blk->magic = XFS_ATTR_LEAF_MAGIC; |
2bd0ea18 NS |
1955 | ASSERT(level == path->active-1); |
1956 | blk->index = 0; | |
ff105f75 | 1957 | blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL); |
88b32f06 DC |
1958 | break; |
1959 | case XFS_DIR2_LEAFN_MAGIC: | |
1960 | case XFS_DIR3_LEAFN_MAGIC: | |
1961 | blk->magic = XFS_DIR2_LEAFN_MAGIC; | |
1962 | ASSERT(level == path->active-1); | |
1963 | blk->index = 0; | |
94ffb80a DW |
1964 | blk->hashval = xfs_dir2_leaf_lasthash(args->dp, |
1965 | blk->bp, NULL); | |
88b32f06 DC |
1966 | break; |
1967 | default: | |
1968 | ASSERT(0); | |
1969 | break; | |
2bd0ea18 NS |
1970 | } |
1971 | } | |
1972 | *result = 0; | |
88b32f06 | 1973 | return 0; |
2bd0ea18 NS |
1974 | } |
1975 | ||
1976 | ||
1977 | /*======================================================================== | |
1978 | * Utility routines. | |
1979 | *========================================================================*/ | |
1980 | ||
1981 | /* | |
1982 | * Implement a simple hash on a character string. | |
1983 | * Rotate the hash value by 7 bits, then XOR each character in. | |
1984 | * This is implemented with some source-level loop unrolling. | |
1985 | */ | |
1986 | xfs_dahash_t | |
4a492e72 | 1987 | xfs_da_hashname(const uint8_t *name, int namelen) |
2bd0ea18 NS |
1988 | { |
1989 | xfs_dahash_t hash; | |
1990 | ||
2bd0ea18 NS |
1991 | /* |
1992 | * Do four characters at a time as long as we can. | |
1993 | */ | |
f9c48a87 | 1994 | for (hash = 0; namelen >= 4; namelen -= 4, name += 4) |
2bd0ea18 | 1995 | hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^ |
f9c48a87 NS |
1996 | (name[3] << 0) ^ rol32(hash, 7 * 4); |
1997 | ||
2bd0ea18 NS |
1998 | /* |
1999 | * Now do the rest of the characters. | |
2000 | */ | |
2001 | switch (namelen) { | |
2002 | case 3: | |
2003 | return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^ | |
f9c48a87 | 2004 | rol32(hash, 7 * 3); |
2bd0ea18 | 2005 | case 2: |
f9c48a87 | 2006 | return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2); |
2bd0ea18 | 2007 | case 1: |
f9c48a87 NS |
2008 | return (name[0] << 0) ^ rol32(hash, 7 * 1); |
2009 | default: /* case 0: */ | |
2bd0ea18 NS |
2010 | return hash; |
2011 | } | |
2bd0ea18 NS |
2012 | } |
2013 | ||
51ca7008 | 2014 | enum xfs_dacmp |
5e656dbb BN |
2015 | xfs_da_compname( |
2016 | struct xfs_da_args *args, | |
56b2de80 DC |
2017 | const unsigned char *name, |
2018 | int len) | |
51ca7008 | 2019 | { |
5e656dbb | 2020 | return (args->namelen == len && memcmp(args->name, name, len) == 0) ? |
51ca7008 BN |
2021 | XFS_CMP_EXACT : XFS_CMP_DIFFERENT; |
2022 | } | |
2023 | ||
5e656dbb BN |
2024 | static xfs_dahash_t |
2025 | xfs_default_hashname( | |
2026 | struct xfs_name *name) | |
2027 | { | |
2028 | return xfs_da_hashname(name->name, name->len); | |
2029 | } | |
2030 | ||
51ca7008 | 2031 | const struct xfs_nameops xfs_default_nameops = { |
5e656dbb | 2032 | .hashname = xfs_default_hashname, |
51ca7008 BN |
2033 | .compname = xfs_da_compname |
2034 | }; | |
2035 | ||
2bd0ea18 | 2036 | int |
a2ceac1f DC |
2037 | xfs_da_grow_inode_int( |
2038 | struct xfs_da_args *args, | |
2039 | xfs_fileoff_t *bno, | |
2040 | int count) | |
2bd0ea18 | 2041 | { |
a2ceac1f DC |
2042 | struct xfs_trans *tp = args->trans; |
2043 | struct xfs_inode *dp = args->dp; | |
2044 | int w = args->whichfork; | |
5a35bf2c | 2045 | xfs_rfsblock_t nblks = dp->i_d.di_nblocks; |
a2ceac1f DC |
2046 | struct xfs_bmbt_irec map, *mapp; |
2047 | int nmap, error, got, i, mapi; | |
56b2de80 | 2048 | |
2bd0ea18 NS |
2049 | /* |
2050 | * Find a spot in the file space to put the new block. | |
2051 | */ | |
a2ceac1f DC |
2052 | error = xfs_bmap_first_unused(tp, dp, count, bno, w); |
2053 | if (error) | |
2bd0ea18 | 2054 | return error; |
a2ceac1f | 2055 | |
2bd0ea18 NS |
2056 | /* |
2057 | * Try mapping it in one filesystem block. | |
2058 | */ | |
2059 | nmap = 1; | |
a2ceac1f DC |
2060 | error = xfs_bmapi_write(tp, dp, *bno, count, |
2061 | xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG, | |
b39f9fcb | 2062 | args->total, &map, &nmap); |
a2ceac1f | 2063 | if (error) |
2bd0ea18 | 2064 | return error; |
a2ceac1f | 2065 | |
2bd0ea18 NS |
2066 | ASSERT(nmap <= 1); |
2067 | if (nmap == 1) { | |
2068 | mapp = ↦ | |
2069 | mapi = 1; | |
a2ceac1f DC |
2070 | } else if (nmap == 0 && count > 1) { |
2071 | xfs_fileoff_t b; | |
2072 | int c; | |
2073 | ||
2074 | /* | |
2075 | * If we didn't get it and the block might work if fragmented, | |
2076 | * try without the CONTIG flag. Loop until we get it all. | |
2077 | */ | |
2bd0ea18 | 2078 | mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP); |
a2ceac1f | 2079 | for (b = *bno, mapi = 0; b < *bno + count; ) { |
d7ff12b8 | 2080 | nmap = min(XFS_BMAP_MAX_NMAP, count); |
a2ceac1f DC |
2081 | c = (int)(*bno + count - b); |
2082 | error = xfs_bmapi_write(tp, dp, b, c, | |
2083 | xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA, | |
b39f9fcb | 2084 | args->total, &mapp[mapi], &nmap); |
a2ceac1f DC |
2085 | if (error) |
2086 | goto out_free_map; | |
2bd0ea18 NS |
2087 | if (nmap < 1) |
2088 | break; | |
2089 | mapi += nmap; | |
2090 | b = mapp[mapi - 1].br_startoff + | |
2091 | mapp[mapi - 1].br_blockcount; | |
2092 | } | |
2093 | } else { | |
2bd0ea18 NS |
2094 | mapi = 0; |
2095 | mapp = NULL; | |
2096 | } | |
a2ceac1f | 2097 | |
2bd0ea18 NS |
2098 | /* |
2099 | * Count the blocks we got, make sure it matches the total. | |
2100 | */ | |
2101 | for (i = 0, got = 0; i < mapi; i++) | |
2102 | got += mapp[i].br_blockcount; | |
a2ceac1f | 2103 | if (got != count || mapp[0].br_startoff != *bno || |
2bd0ea18 | 2104 | mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount != |
a2ceac1f | 2105 | *bno + count) { |
12b53197 | 2106 | error = -ENOSPC; |
a2ceac1f | 2107 | goto out_free_map; |
2bd0ea18 | 2108 | } |
a2ceac1f | 2109 | |
56b2de80 DC |
2110 | /* account for newly allocated blocks in reserved blocks total */ |
2111 | args->total -= dp->i_d.di_nblocks - nblks; | |
a2ceac1f DC |
2112 | |
2113 | out_free_map: | |
2114 | if (mapp != &map) | |
2115 | kmem_free(mapp); | |
2116 | return error; | |
2117 | } | |
2118 | ||
2119 | /* | |
2120 | * Add a block to the btree ahead of the file. | |
2121 | * Return the new block number to the caller. | |
2122 | */ | |
2123 | int | |
2124 | xfs_da_grow_inode( | |
2125 | struct xfs_da_args *args, | |
2126 | xfs_dablk_t *new_blkno) | |
2127 | { | |
2128 | xfs_fileoff_t bno; | |
a2ceac1f DC |
2129 | int error; |
2130 | ||
2131 | trace_xfs_da_grow_inode(args); | |
2132 | ||
ff105f75 DC |
2133 | bno = args->geo->leafblk; |
2134 | error = xfs_da_grow_inode_int(args, &bno, args->geo->fsbcount); | |
a2ceac1f DC |
2135 | if (!error) |
2136 | *new_blkno = (xfs_dablk_t)bno; | |
2137 | return error; | |
2bd0ea18 NS |
2138 | } |
2139 | ||
2bd0ea18 | 2140 | /* |
dfc130f3 | 2141 | * Ick. We need to always be able to remove a btree block, even |
2bd0ea18 NS |
2142 | * if there's no space reservation because the filesystem is full. |
2143 | * This is called if xfs_bunmapi on a btree block fails due to ENOSPC. | |
2144 | * It swaps the target block with the last block in the file. The | |
2145 | * last block in the file can always be removed since it can't cause | |
2146 | * a bmap btree split to do that. | |
2147 | */ | |
2148 | STATIC int | |
88b32f06 DC |
2149 | xfs_da3_swap_lastblock( |
2150 | struct xfs_da_args *args, | |
2151 | xfs_dablk_t *dead_blknop, | |
2152 | struct xfs_buf **dead_bufp) | |
2bd0ea18 | 2153 | { |
88b32f06 DC |
2154 | struct xfs_da_blkinfo *dead_info; |
2155 | struct xfs_da_blkinfo *sib_info; | |
2156 | struct xfs_da_intnode *par_node; | |
2157 | struct xfs_da_intnode *dead_node; | |
2158 | struct xfs_dir2_leaf *dead_leaf2; | |
2159 | struct xfs_da_node_entry *btree; | |
2160 | struct xfs_da3_icnode_hdr par_hdr; | |
ff105f75 | 2161 | struct xfs_inode *dp; |
88b32f06 DC |
2162 | struct xfs_trans *tp; |
2163 | struct xfs_mount *mp; | |
2164 | struct xfs_buf *dead_buf; | |
2165 | struct xfs_buf *last_buf; | |
2166 | struct xfs_buf *sib_buf; | |
2167 | struct xfs_buf *par_buf; | |
2168 | xfs_dahash_t dead_hash; | |
2169 | xfs_fileoff_t lastoff; | |
2170 | xfs_dablk_t dead_blkno; | |
2171 | xfs_dablk_t last_blkno; | |
2172 | xfs_dablk_t sib_blkno; | |
2173 | xfs_dablk_t par_blkno; | |
2174 | int error; | |
2175 | int w; | |
2176 | int entno; | |
2177 | int level; | |
2178 | int dead_level; | |
2bd0ea18 | 2179 | |
a2ceac1f DC |
2180 | trace_xfs_da_swap_lastblock(args); |
2181 | ||
2bd0ea18 NS |
2182 | dead_buf = *dead_bufp; |
2183 | dead_blkno = *dead_blknop; | |
2184 | tp = args->trans; | |
ff105f75 | 2185 | dp = args->dp; |
2bd0ea18 NS |
2186 | w = args->whichfork; |
2187 | ASSERT(w == XFS_DATA_FORK); | |
ff105f75 DC |
2188 | mp = dp->i_mount; |
2189 | lastoff = args->geo->freeblk; | |
2190 | error = xfs_bmap_last_before(tp, dp, &lastoff, w); | |
2bd0ea18 NS |
2191 | if (error) |
2192 | return error; | |
4ca431fc NS |
2193 | if (unlikely(lastoff == 0)) { |
2194 | XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW, | |
2195 | mp); | |
12b53197 | 2196 | return -EFSCORRUPTED; |
4ca431fc | 2197 | } |
2bd0ea18 NS |
2198 | /* |
2199 | * Read the last block in the btree space. | |
2200 | */ | |
ff105f75 DC |
2201 | last_blkno = (xfs_dablk_t)lastoff - args->geo->fsbcount; |
2202 | error = xfs_da3_node_read(tp, dp, last_blkno, -1, &last_buf, w); | |
a2ceac1f | 2203 | if (error) |
2bd0ea18 NS |
2204 | return error; |
2205 | /* | |
2206 | * Copy the last block into the dead buffer and log it. | |
2207 | */ | |
ff105f75 DC |
2208 | memcpy(dead_buf->b_addr, last_buf->b_addr, args->geo->blksize); |
2209 | xfs_trans_log_buf(tp, dead_buf, 0, args->geo->blksize - 1); | |
a2ceac1f | 2210 | dead_info = dead_buf->b_addr; |
2bd0ea18 NS |
2211 | /* |
2212 | * Get values from the moved block. | |
2213 | */ | |
65b80c98 DC |
2214 | if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) || |
2215 | dead_info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) { | |
2216 | struct xfs_dir3_icleaf_hdr leafhdr; | |
2217 | struct xfs_dir2_leaf_entry *ents; | |
2218 | ||
2bd0ea18 | 2219 | dead_leaf2 = (xfs_dir2_leaf_t *)dead_info; |
ff105f75 DC |
2220 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, dead_leaf2); |
2221 | ents = dp->d_ops->leaf_ents_p(dead_leaf2); | |
2bd0ea18 | 2222 | dead_level = 0; |
65b80c98 | 2223 | dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval); |
2bd0ea18 | 2224 | } else { |
88b32f06 DC |
2225 | struct xfs_da3_icnode_hdr deadhdr; |
2226 | ||
2bd0ea18 | 2227 | dead_node = (xfs_da_intnode_t *)dead_info; |
ff105f75 DC |
2228 | dp->d_ops->node_hdr_from_disk(&deadhdr, dead_node); |
2229 | btree = dp->d_ops->node_tree_p(dead_node); | |
88b32f06 DC |
2230 | dead_level = deadhdr.level; |
2231 | dead_hash = be32_to_cpu(btree[deadhdr.count - 1].hashval); | |
2bd0ea18 NS |
2232 | } |
2233 | sib_buf = par_buf = NULL; | |
2234 | /* | |
2235 | * If the moved block has a left sibling, fix up the pointers. | |
2236 | */ | |
5e656dbb | 2237 | if ((sib_blkno = be32_to_cpu(dead_info->back))) { |
ff105f75 | 2238 | error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w); |
a2ceac1f | 2239 | if (error) |
2bd0ea18 | 2240 | goto done; |
a2ceac1f | 2241 | sib_info = sib_buf->b_addr; |
4ca431fc | 2242 | if (unlikely( |
5e656dbb BN |
2243 | be32_to_cpu(sib_info->forw) != last_blkno || |
2244 | sib_info->magic != dead_info->magic)) { | |
4ca431fc NS |
2245 | XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)", |
2246 | XFS_ERRLEVEL_LOW, mp); | |
12b53197 | 2247 | error = -EFSCORRUPTED; |
2bd0ea18 NS |
2248 | goto done; |
2249 | } | |
5e656dbb | 2250 | sib_info->forw = cpu_to_be32(dead_blkno); |
a2ceac1f | 2251 | xfs_trans_log_buf(tp, sib_buf, |
2bd0ea18 NS |
2252 | XFS_DA_LOGRANGE(sib_info, &sib_info->forw, |
2253 | sizeof(sib_info->forw))); | |
2bd0ea18 NS |
2254 | sib_buf = NULL; |
2255 | } | |
2256 | /* | |
2257 | * If the moved block has a right sibling, fix up the pointers. | |
2258 | */ | |
5e656dbb | 2259 | if ((sib_blkno = be32_to_cpu(dead_info->forw))) { |
ff105f75 | 2260 | error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w); |
a2ceac1f | 2261 | if (error) |
2bd0ea18 | 2262 | goto done; |
a2ceac1f | 2263 | sib_info = sib_buf->b_addr; |
4ca431fc | 2264 | if (unlikely( |
5e656dbb BN |
2265 | be32_to_cpu(sib_info->back) != last_blkno || |
2266 | sib_info->magic != dead_info->magic)) { | |
4ca431fc NS |
2267 | XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)", |
2268 | XFS_ERRLEVEL_LOW, mp); | |
12b53197 | 2269 | error = -EFSCORRUPTED; |
2bd0ea18 NS |
2270 | goto done; |
2271 | } | |
5e656dbb | 2272 | sib_info->back = cpu_to_be32(dead_blkno); |
a2ceac1f | 2273 | xfs_trans_log_buf(tp, sib_buf, |
2bd0ea18 NS |
2274 | XFS_DA_LOGRANGE(sib_info, &sib_info->back, |
2275 | sizeof(sib_info->back))); | |
2bd0ea18 NS |
2276 | sib_buf = NULL; |
2277 | } | |
ff105f75 | 2278 | par_blkno = args->geo->leafblk; |
2bd0ea18 NS |
2279 | level = -1; |
2280 | /* | |
2281 | * Walk down the tree looking for the parent of the moved block. | |
2282 | */ | |
2283 | for (;;) { | |
ff105f75 | 2284 | error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w); |
a2ceac1f | 2285 | if (error) |
2bd0ea18 | 2286 | goto done; |
a2ceac1f | 2287 | par_node = par_buf->b_addr; |
ff105f75 | 2288 | dp->d_ops->node_hdr_from_disk(&par_hdr, par_node); |
88b32f06 | 2289 | if (level >= 0 && level != par_hdr.level + 1) { |
4ca431fc NS |
2290 | XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)", |
2291 | XFS_ERRLEVEL_LOW, mp); | |
12b53197 | 2292 | error = -EFSCORRUPTED; |
2bd0ea18 NS |
2293 | goto done; |
2294 | } | |
88b32f06 | 2295 | level = par_hdr.level; |
ff105f75 | 2296 | btree = dp->d_ops->node_tree_p(par_node); |
2bd0ea18 | 2297 | for (entno = 0; |
88b32f06 DC |
2298 | entno < par_hdr.count && |
2299 | be32_to_cpu(btree[entno].hashval) < dead_hash; | |
2bd0ea18 NS |
2300 | entno++) |
2301 | continue; | |
88b32f06 | 2302 | if (entno == par_hdr.count) { |
4ca431fc NS |
2303 | XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)", |
2304 | XFS_ERRLEVEL_LOW, mp); | |
12b53197 | 2305 | error = -EFSCORRUPTED; |
2bd0ea18 NS |
2306 | goto done; |
2307 | } | |
88b32f06 | 2308 | par_blkno = be32_to_cpu(btree[entno].before); |
2bd0ea18 NS |
2309 | if (level == dead_level + 1) |
2310 | break; | |
a2ceac1f | 2311 | xfs_trans_brelse(tp, par_buf); |
2bd0ea18 NS |
2312 | par_buf = NULL; |
2313 | } | |
2314 | /* | |
2315 | * We're in the right parent block. | |
2316 | * Look for the right entry. | |
2317 | */ | |
2318 | for (;;) { | |
2319 | for (; | |
88b32f06 DC |
2320 | entno < par_hdr.count && |
2321 | be32_to_cpu(btree[entno].before) != last_blkno; | |
2bd0ea18 NS |
2322 | entno++) |
2323 | continue; | |
88b32f06 | 2324 | if (entno < par_hdr.count) |
2bd0ea18 | 2325 | break; |
88b32f06 | 2326 | par_blkno = par_hdr.forw; |
a2ceac1f | 2327 | xfs_trans_brelse(tp, par_buf); |
2bd0ea18 | 2328 | par_buf = NULL; |
4ca431fc NS |
2329 | if (unlikely(par_blkno == 0)) { |
2330 | XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)", | |
2331 | XFS_ERRLEVEL_LOW, mp); | |
12b53197 | 2332 | error = -EFSCORRUPTED; |
2bd0ea18 NS |
2333 | goto done; |
2334 | } | |
ff105f75 | 2335 | error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w); |
a2ceac1f | 2336 | if (error) |
2bd0ea18 | 2337 | goto done; |
a2ceac1f | 2338 | par_node = par_buf->b_addr; |
ff105f75 | 2339 | dp->d_ops->node_hdr_from_disk(&par_hdr, par_node); |
88b32f06 | 2340 | if (par_hdr.level != level) { |
4ca431fc NS |
2341 | XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)", |
2342 | XFS_ERRLEVEL_LOW, mp); | |
12b53197 | 2343 | error = -EFSCORRUPTED; |
2bd0ea18 NS |
2344 | goto done; |
2345 | } | |
ff105f75 | 2346 | btree = dp->d_ops->node_tree_p(par_node); |
2bd0ea18 NS |
2347 | entno = 0; |
2348 | } | |
2349 | /* | |
2350 | * Update the parent entry pointing to the moved block. | |
2351 | */ | |
88b32f06 | 2352 | btree[entno].before = cpu_to_be32(dead_blkno); |
a2ceac1f | 2353 | xfs_trans_log_buf(tp, par_buf, |
88b32f06 DC |
2354 | XFS_DA_LOGRANGE(par_node, &btree[entno].before, |
2355 | sizeof(btree[entno].before))); | |
2bd0ea18 NS |
2356 | *dead_blknop = last_blkno; |
2357 | *dead_bufp = last_buf; | |
2358 | return 0; | |
2359 | done: | |
2360 | if (par_buf) | |
a2ceac1f | 2361 | xfs_trans_brelse(tp, par_buf); |
2bd0ea18 | 2362 | if (sib_buf) |
a2ceac1f DC |
2363 | xfs_trans_brelse(tp, sib_buf); |
2364 | xfs_trans_brelse(tp, last_buf); | |
2bd0ea18 NS |
2365 | return error; |
2366 | } | |
2367 | ||
2368 | /* | |
2369 | * Remove a btree block from a directory or attribute. | |
2370 | */ | |
2371 | int | |
a2ceac1f | 2372 | xfs_da_shrink_inode( |
a50d2ab0 BF |
2373 | struct xfs_da_args *args, |
2374 | xfs_dablk_t dead_blkno, | |
2375 | struct xfs_buf *dead_buf) | |
2bd0ea18 | 2376 | { |
a50d2ab0 BF |
2377 | struct xfs_inode *dp; |
2378 | int done, error, w, count; | |
2379 | struct xfs_trans *tp; | |
2bd0ea18 | 2380 | |
a2ceac1f DC |
2381 | trace_xfs_da_shrink_inode(args); |
2382 | ||
2bd0ea18 NS |
2383 | dp = args->dp; |
2384 | w = args->whichfork; | |
2385 | tp = args->trans; | |
ff105f75 | 2386 | count = args->geo->fsbcount; |
2bd0ea18 NS |
2387 | for (;;) { |
2388 | /* | |
2389 | * Remove extents. If we get ENOSPC for a dir we have to move | |
2390 | * the last block to the place we want to kill. | |
2391 | */ | |
88b32f06 | 2392 | error = xfs_bunmapi(tp, dp, dead_blkno, count, |
d3c5f3dd | 2393 | xfs_bmapi_aflag(w), 0, &done); |
12b53197 | 2394 | if (error == -ENOSPC) { |
2bd0ea18 | 2395 | if (w != XFS_DATA_FORK) |
5e656dbb | 2396 | break; |
88b32f06 DC |
2397 | error = xfs_da3_swap_lastblock(args, &dead_blkno, |
2398 | &dead_buf); | |
2399 | if (error) | |
5e656dbb BN |
2400 | break; |
2401 | } else { | |
2bd0ea18 | 2402 | break; |
2bd0ea18 NS |
2403 | } |
2404 | } | |
a2ceac1f | 2405 | xfs_trans_binval(tp, dead_buf); |
2bd0ea18 NS |
2406 | return error; |
2407 | } | |
2408 | ||
2409 | /* | |
2410 | * See if the mapping(s) for this btree block are valid, i.e. | |
2411 | * don't contain holes, are logically contiguous, and cover the whole range. | |
2412 | */ | |
2413 | STATIC int | |
2414 | xfs_da_map_covers_blocks( | |
2415 | int nmap, | |
dfc130f3 | 2416 | xfs_bmbt_irec_t *mapp, |
2bd0ea18 NS |
2417 | xfs_dablk_t bno, |
2418 | int count) | |
2419 | { | |
2420 | int i; | |
2421 | xfs_fileoff_t off; | |
2422 | ||
2423 | for (i = 0, off = bno; i < nmap; i++) { | |
2424 | if (mapp[i].br_startblock == HOLESTARTBLOCK || | |
2425 | mapp[i].br_startblock == DELAYSTARTBLOCK) { | |
2bd0ea18 NS |
2426 | return 0; |
2427 | } | |
2428 | if (off != mapp[i].br_startoff) { | |
2bd0ea18 NS |
2429 | return 0; |
2430 | } | |
2431 | off += mapp[i].br_blockcount; | |
2432 | } | |
2433 | return off == bno + count; | |
2434 | } | |
2435 | ||
2436 | /* | |
a2ceac1f DC |
2437 | * Convert a struct xfs_bmbt_irec to a struct xfs_buf_map. |
2438 | * | |
2439 | * For the single map case, it is assumed that the caller has provided a pointer | |
2440 | * to a valid xfs_buf_map. For the multiple map case, this function will | |
2441 | * allocate the xfs_buf_map to hold all the maps and replace the caller's single | |
2442 | * map pointer with the allocated map. | |
2bd0ea18 | 2443 | */ |
a2ceac1f DC |
2444 | static int |
2445 | xfs_buf_map_from_irec( | |
2446 | struct xfs_mount *mp, | |
2447 | struct xfs_buf_map **mapp, | |
6bddecbc | 2448 | int *nmaps, |
a2ceac1f | 2449 | struct xfs_bmbt_irec *irecs, |
6bddecbc | 2450 | int nirecs) |
2bd0ea18 | 2451 | { |
a2ceac1f DC |
2452 | struct xfs_buf_map *map; |
2453 | int i; | |
2454 | ||
2455 | ASSERT(*nmaps == 1); | |
2456 | ASSERT(nirecs >= 1); | |
2457 | ||
2458 | if (nirecs > 1) { | |
7b02556d DC |
2459 | map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map), |
2460 | KM_SLEEP | KM_NOFS); | |
a2ceac1f | 2461 | if (!map) |
12b53197 | 2462 | return -ENOMEM; |
a2ceac1f DC |
2463 | *mapp = map; |
2464 | } | |
2465 | ||
2466 | *nmaps = nirecs; | |
2467 | map = *mapp; | |
2468 | for (i = 0; i < *nmaps; i++) { | |
2469 | ASSERT(irecs[i].br_startblock != DELAYSTARTBLOCK && | |
2470 | irecs[i].br_startblock != HOLESTARTBLOCK); | |
2471 | map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock); | |
2472 | map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount); | |
2473 | } | |
2474 | return 0; | |
2475 | } | |
2476 | ||
2477 | /* | |
2478 | * Map the block we are given ready for reading. There are three possible return | |
2479 | * values: | |
2480 | * -1 - will be returned if we land in a hole and mappedbno == -2 so the | |
2481 | * caller knows not to execute a subsequent read. | |
2482 | * 0 - if we mapped the block successfully | |
2483 | * >0 - positive error number if there was an error. | |
2484 | */ | |
2485 | static int | |
2486 | xfs_dabuf_map( | |
a2ceac1f DC |
2487 | struct xfs_inode *dp, |
2488 | xfs_dablk_t bno, | |
2489 | xfs_daddr_t mappedbno, | |
2490 | int whichfork, | |
2491 | struct xfs_buf_map **map, | |
2492 | int *nmaps) | |
2493 | { | |
2494 | struct xfs_mount *mp = dp->i_mount; | |
2495 | int nfsb; | |
2496 | int error = 0; | |
2497 | struct xfs_bmbt_irec irec; | |
2498 | struct xfs_bmbt_irec *irecs = &irec; | |
2499 | int nirecs; | |
2500 | ||
2501 | ASSERT(map && *map); | |
2502 | ASSERT(*nmaps == 1); | |
2bd0ea18 | 2503 | |
ff105f75 DC |
2504 | if (whichfork == XFS_DATA_FORK) |
2505 | nfsb = mp->m_dir_geo->fsbcount; | |
2506 | else | |
2507 | nfsb = mp->m_attr_geo->fsbcount; | |
a2ceac1f | 2508 | |
2bd0ea18 NS |
2509 | /* |
2510 | * Caller doesn't have a mapping. -2 means don't complain | |
2511 | * if we land in a hole. | |
2512 | */ | |
2513 | if (mappedbno == -1 || mappedbno == -2) { | |
2514 | /* | |
2515 | * Optimize the one-block case. | |
2516 | */ | |
a2ceac1f | 2517 | if (nfsb != 1) |
7b02556d DC |
2518 | irecs = kmem_zalloc(sizeof(irec) * nfsb, |
2519 | KM_SLEEP | KM_NOFS); | |
2bd0ea18 | 2520 | |
a2ceac1f DC |
2521 | nirecs = nfsb; |
2522 | error = xfs_bmapi_read(dp, (xfs_fileoff_t)bno, nfsb, irecs, | |
2523 | &nirecs, xfs_bmapi_aflag(whichfork)); | |
2524 | if (error) | |
2525 | goto out; | |
2bd0ea18 | 2526 | } else { |
a2ceac1f DC |
2527 | irecs->br_startblock = XFS_DADDR_TO_FSB(mp, mappedbno); |
2528 | irecs->br_startoff = (xfs_fileoff_t)bno; | |
2529 | irecs->br_blockcount = nfsb; | |
2530 | irecs->br_state = 0; | |
2531 | nirecs = 1; | |
2bd0ea18 | 2532 | } |
a2ceac1f DC |
2533 | |
2534 | if (!xfs_da_map_covers_blocks(nirecs, irecs, bno, nfsb)) { | |
12b53197 DC |
2535 | error = mappedbno == -2 ? -1 : -EFSCORRUPTED; |
2536 | if (unlikely(error == -EFSCORRUPTED)) { | |
4ca431fc | 2537 | if (xfs_error_level >= XFS_ERRLEVEL_LOW) { |
a2ceac1f DC |
2538 | int i; |
2539 | xfs_alert(mp, "%s: bno %lld dir: inode %lld", | |
2540 | __func__, (long long)bno, | |
4ca431fc | 2541 | (long long)dp->i_ino); |
a2ceac1f DC |
2542 | for (i = 0; i < *nmaps; i++) { |
2543 | xfs_alert(mp, | |
2544 | "[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d", | |
4ca431fc | 2545 | i, |
a2ceac1f DC |
2546 | (long long)irecs[i].br_startoff, |
2547 | (long long)irecs[i].br_startblock, | |
2548 | (long long)irecs[i].br_blockcount, | |
2549 | irecs[i].br_state); | |
4ca431fc NS |
2550 | } |
2551 | } | |
2552 | XFS_ERROR_REPORT("xfs_da_do_buf(1)", | |
2553 | XFS_ERRLEVEL_LOW, mp); | |
2554 | } | |
a2ceac1f | 2555 | goto out; |
2bd0ea18 | 2556 | } |
a2ceac1f DC |
2557 | error = xfs_buf_map_from_irec(mp, map, nmaps, irecs, nirecs); |
2558 | out: | |
2559 | if (irecs != &irec) | |
2560 | kmem_free(irecs); | |
2561 | return error; | |
2562 | } | |
2563 | ||
2564 | /* | |
2565 | * Get a buffer for the dir/attr block. | |
2566 | */ | |
2567 | int | |
2568 | xfs_da_get_buf( | |
2569 | struct xfs_trans *trans, | |
2570 | struct xfs_inode *dp, | |
2571 | xfs_dablk_t bno, | |
2572 | xfs_daddr_t mappedbno, | |
2573 | struct xfs_buf **bpp, | |
2574 | int whichfork) | |
2575 | { | |
2576 | struct xfs_buf *bp; | |
2577 | struct xfs_buf_map map; | |
2578 | struct xfs_buf_map *mapp; | |
2579 | int nmap; | |
2580 | int error; | |
2581 | ||
2582 | *bpp = NULL; | |
2583 | mapp = ↦ | |
2584 | nmap = 1; | |
ff105f75 | 2585 | error = xfs_dabuf_map(dp, bno, mappedbno, whichfork, |
a2ceac1f DC |
2586 | &mapp, &nmap); |
2587 | if (error) { | |
2588 | /* mapping a hole is not an error, but we don't continue */ | |
2589 | if (error == -1) | |
2bd0ea18 | 2590 | error = 0; |
a2ceac1f | 2591 | goto out_free; |
2bd0ea18 | 2592 | } |
a2ceac1f DC |
2593 | |
2594 | bp = xfs_trans_get_buf_map(trans, dp->i_mount->m_ddev_targp, | |
2595 | mapp, nmap, 0); | |
12b53197 | 2596 | error = bp ? bp->b_error : -EIO; |
a2ceac1f | 2597 | if (error) { |
5a35bf2c DC |
2598 | if (bp) |
2599 | xfs_trans_brelse(trans, bp); | |
a2ceac1f DC |
2600 | goto out_free; |
2601 | } | |
2602 | ||
2603 | *bpp = bp; | |
2604 | ||
2605 | out_free: | |
2606 | if (mapp != &map) | |
2607 | kmem_free(mapp); | |
2608 | ||
2609 | return error; | |
2610 | } | |
2611 | ||
2612 | /* | |
2613 | * Get a buffer for the dir/attr block, fill in the contents. | |
2614 | */ | |
2615 | int | |
2616 | xfs_da_read_buf( | |
2617 | struct xfs_trans *trans, | |
2618 | struct xfs_inode *dp, | |
2619 | xfs_dablk_t bno, | |
2620 | xfs_daddr_t mappedbno, | |
2621 | struct xfs_buf **bpp, | |
2622 | int whichfork, | |
2623 | const struct xfs_buf_ops *ops) | |
2624 | { | |
2625 | struct xfs_buf *bp; | |
2626 | struct xfs_buf_map map; | |
2627 | struct xfs_buf_map *mapp; | |
2628 | int nmap; | |
2629 | int error; | |
2630 | ||
2631 | *bpp = NULL; | |
2632 | mapp = ↦ | |
2633 | nmap = 1; | |
ff105f75 | 2634 | error = xfs_dabuf_map(dp, bno, mappedbno, whichfork, |
a2ceac1f DC |
2635 | &mapp, &nmap); |
2636 | if (error) { | |
2637 | /* mapping a hole is not an error, but we don't continue */ | |
2638 | if (error == -1) | |
2639 | error = 0; | |
2640 | goto out_free; | |
2641 | } | |
2642 | ||
2643 | error = xfs_trans_read_buf_map(dp->i_mount, trans, | |
2644 | dp->i_mount->m_ddev_targp, | |
2645 | mapp, nmap, 0, &bp, ops); | |
2646 | if (error) | |
2647 | goto out_free; | |
2648 | ||
2649 | if (whichfork == XFS_ATTR_FORK) | |
2650 | xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF); | |
2bd0ea18 | 2651 | else |
a2ceac1f | 2652 | xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF); |
a2ceac1f DC |
2653 | *bpp = bp; |
2654 | out_free: | |
2bd0ea18 | 2655 | if (mapp != &map) |
5e656dbb | 2656 | kmem_free(mapp); |
2bd0ea18 | 2657 | |
a2ceac1f | 2658 | return error; |
2bd0ea18 NS |
2659 | } |
2660 | ||
2661 | /* | |
5e656dbb | 2662 | * Readahead the dir/attr block. |
2bd0ea18 | 2663 | */ |
3a65062f | 2664 | int |
5e656dbb | 2665 | xfs_da_reada_buf( |
a2ceac1f DC |
2666 | struct xfs_inode *dp, |
2667 | xfs_dablk_t bno, | |
2668 | xfs_daddr_t mappedbno, | |
2669 | int whichfork, | |
2670 | const struct xfs_buf_ops *ops) | |
2bd0ea18 | 2671 | { |
a2ceac1f DC |
2672 | struct xfs_buf_map map; |
2673 | struct xfs_buf_map *mapp; | |
2674 | int nmap; | |
2675 | int error; | |
2bd0ea18 | 2676 | |
a2ceac1f DC |
2677 | mapp = ↦ |
2678 | nmap = 1; | |
ff105f75 | 2679 | error = xfs_dabuf_map(dp, bno, mappedbno, whichfork, |
a2ceac1f DC |
2680 | &mapp, &nmap); |
2681 | if (error) { | |
2682 | /* mapping a hole is not an error, but we don't continue */ | |
2683 | if (error == -1) | |
2684 | error = 0; | |
2685 | goto out_free; | |
2686 | } | |
2687 | ||
2688 | mappedbno = mapp[0].bm_bn; | |
2689 | xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, ops); | |
2690 | ||
2691 | out_free: | |
2692 | if (mapp != &map) | |
2693 | kmem_free(mapp); | |
2694 | ||
3a65062f | 2695 | return error; |
2bd0ea18 | 2696 | } |