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