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