0001
0002
0003
0004
0005
0006
0007 #include "xfs.h"
0008 #include "xfs_fs.h"
0009 #include "xfs_shared.h"
0010 #include "xfs_format.h"
0011 #include "xfs_log_format.h"
0012 #include "xfs_trans_resv.h"
0013 #include "xfs_bit.h"
0014 #include "xfs_mount.h"
0015 #include "xfs_inode.h"
0016 #include "xfs_dir2.h"
0017 #include "xfs_dir2_priv.h"
0018 #include "xfs_trans.h"
0019 #include "xfs_bmap.h"
0020 #include "xfs_attr_leaf.h"
0021 #include "xfs_error.h"
0022 #include "xfs_trace.h"
0023 #include "xfs_buf_item.h"
0024 #include "xfs_log.h"
0025 #include "xfs_errortag.h"
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 STATIC int xfs_da3_root_split(xfs_da_state_t *state,
0041 xfs_da_state_blk_t *existing_root,
0042 xfs_da_state_blk_t *new_child);
0043 STATIC int xfs_da3_node_split(xfs_da_state_t *state,
0044 xfs_da_state_blk_t *existing_blk,
0045 xfs_da_state_blk_t *split_blk,
0046 xfs_da_state_blk_t *blk_to_add,
0047 int treelevel,
0048 int *result);
0049 STATIC void xfs_da3_node_rebalance(xfs_da_state_t *state,
0050 xfs_da_state_blk_t *node_blk_1,
0051 xfs_da_state_blk_t *node_blk_2);
0052 STATIC void xfs_da3_node_add(xfs_da_state_t *state,
0053 xfs_da_state_blk_t *old_node_blk,
0054 xfs_da_state_blk_t *new_node_blk);
0055
0056
0057
0058
0059 STATIC int xfs_da3_root_join(xfs_da_state_t *state,
0060 xfs_da_state_blk_t *root_blk);
0061 STATIC int xfs_da3_node_toosmall(xfs_da_state_t *state, int *retval);
0062 STATIC void xfs_da3_node_remove(xfs_da_state_t *state,
0063 xfs_da_state_blk_t *drop_blk);
0064 STATIC void xfs_da3_node_unbalance(xfs_da_state_t *state,
0065 xfs_da_state_blk_t *src_node_blk,
0066 xfs_da_state_blk_t *dst_node_blk);
0067
0068
0069
0070
0071 STATIC int xfs_da3_blk_unlink(xfs_da_state_t *state,
0072 xfs_da_state_blk_t *drop_blk,
0073 xfs_da_state_blk_t *save_blk);
0074
0075
0076 struct kmem_cache *xfs_da_state_cache;
0077
0078
0079
0080
0081
0082 struct xfs_da_state *
0083 xfs_da_state_alloc(
0084 struct xfs_da_args *args)
0085 {
0086 struct xfs_da_state *state;
0087
0088 state = kmem_cache_zalloc(xfs_da_state_cache, GFP_NOFS | __GFP_NOFAIL);
0089 state->args = args;
0090 state->mp = args->dp->i_mount;
0091 return state;
0092 }
0093
0094
0095
0096
0097 STATIC void
0098 xfs_da_state_kill_altpath(xfs_da_state_t *state)
0099 {
0100 int i;
0101
0102 for (i = 0; i < state->altpath.active; i++)
0103 state->altpath.blk[i].bp = NULL;
0104 state->altpath.active = 0;
0105 }
0106
0107
0108
0109
0110 void
0111 xfs_da_state_free(xfs_da_state_t *state)
0112 {
0113 xfs_da_state_kill_altpath(state);
0114 #ifdef DEBUG
0115 memset((char *)state, 0, sizeof(*state));
0116 #endif
0117 kmem_cache_free(xfs_da_state_cache, state);
0118 }
0119
0120 void
0121 xfs_da_state_reset(
0122 struct xfs_da_state *state,
0123 struct xfs_da_args *args)
0124 {
0125 xfs_da_state_kill_altpath(state);
0126 memset(state, 0, sizeof(struct xfs_da_state));
0127 state->args = args;
0128 state->mp = state->args->dp->i_mount;
0129 }
0130
0131 static inline int xfs_dabuf_nfsb(struct xfs_mount *mp, int whichfork)
0132 {
0133 if (whichfork == XFS_DATA_FORK)
0134 return mp->m_dir_geo->fsbcount;
0135 return mp->m_attr_geo->fsbcount;
0136 }
0137
0138 void
0139 xfs_da3_node_hdr_from_disk(
0140 struct xfs_mount *mp,
0141 struct xfs_da3_icnode_hdr *to,
0142 struct xfs_da_intnode *from)
0143 {
0144 if (xfs_has_crc(mp)) {
0145 struct xfs_da3_intnode *from3 = (struct xfs_da3_intnode *)from;
0146
0147 to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
0148 to->back = be32_to_cpu(from3->hdr.info.hdr.back);
0149 to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
0150 to->count = be16_to_cpu(from3->hdr.__count);
0151 to->level = be16_to_cpu(from3->hdr.__level);
0152 to->btree = from3->__btree;
0153 ASSERT(to->magic == XFS_DA3_NODE_MAGIC);
0154 } else {
0155 to->forw = be32_to_cpu(from->hdr.info.forw);
0156 to->back = be32_to_cpu(from->hdr.info.back);
0157 to->magic = be16_to_cpu(from->hdr.info.magic);
0158 to->count = be16_to_cpu(from->hdr.__count);
0159 to->level = be16_to_cpu(from->hdr.__level);
0160 to->btree = from->__btree;
0161 ASSERT(to->magic == XFS_DA_NODE_MAGIC);
0162 }
0163 }
0164
0165 void
0166 xfs_da3_node_hdr_to_disk(
0167 struct xfs_mount *mp,
0168 struct xfs_da_intnode *to,
0169 struct xfs_da3_icnode_hdr *from)
0170 {
0171 if (xfs_has_crc(mp)) {
0172 struct xfs_da3_intnode *to3 = (struct xfs_da3_intnode *)to;
0173
0174 ASSERT(from->magic == XFS_DA3_NODE_MAGIC);
0175 to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
0176 to3->hdr.info.hdr.back = cpu_to_be32(from->back);
0177 to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
0178 to3->hdr.__count = cpu_to_be16(from->count);
0179 to3->hdr.__level = cpu_to_be16(from->level);
0180 } else {
0181 ASSERT(from->magic == XFS_DA_NODE_MAGIC);
0182 to->hdr.info.forw = cpu_to_be32(from->forw);
0183 to->hdr.info.back = cpu_to_be32(from->back);
0184 to->hdr.info.magic = cpu_to_be16(from->magic);
0185 to->hdr.__count = cpu_to_be16(from->count);
0186 to->hdr.__level = cpu_to_be16(from->level);
0187 }
0188 }
0189
0190
0191
0192
0193
0194
0195 xfs_failaddr_t
0196 xfs_da3_blkinfo_verify(
0197 struct xfs_buf *bp,
0198 struct xfs_da3_blkinfo *hdr3)
0199 {
0200 struct xfs_mount *mp = bp->b_mount;
0201 struct xfs_da_blkinfo *hdr = &hdr3->hdr;
0202
0203 if (!xfs_verify_magic16(bp, hdr->magic))
0204 return __this_address;
0205
0206 if (xfs_has_crc(mp)) {
0207 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
0208 return __this_address;
0209 if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
0210 return __this_address;
0211 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
0212 return __this_address;
0213 }
0214
0215 return NULL;
0216 }
0217
0218 static xfs_failaddr_t
0219 xfs_da3_node_verify(
0220 struct xfs_buf *bp)
0221 {
0222 struct xfs_mount *mp = bp->b_mount;
0223 struct xfs_da_intnode *hdr = bp->b_addr;
0224 struct xfs_da3_icnode_hdr ichdr;
0225 xfs_failaddr_t fa;
0226
0227 xfs_da3_node_hdr_from_disk(mp, &ichdr, hdr);
0228
0229 fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
0230 if (fa)
0231 return fa;
0232
0233 if (ichdr.level == 0)
0234 return __this_address;
0235 if (ichdr.level > XFS_DA_NODE_MAXDEPTH)
0236 return __this_address;
0237 if (ichdr.count == 0)
0238 return __this_address;
0239
0240
0241
0242
0243
0244 if (ichdr.count > mp->m_dir_geo->node_ents &&
0245 ichdr.count > mp->m_attr_geo->node_ents)
0246 return __this_address;
0247
0248
0249
0250 return NULL;
0251 }
0252
0253 static void
0254 xfs_da3_node_write_verify(
0255 struct xfs_buf *bp)
0256 {
0257 struct xfs_mount *mp = bp->b_mount;
0258 struct xfs_buf_log_item *bip = bp->b_log_item;
0259 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
0260 xfs_failaddr_t fa;
0261
0262 fa = xfs_da3_node_verify(bp);
0263 if (fa) {
0264 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
0265 return;
0266 }
0267
0268 if (!xfs_has_crc(mp))
0269 return;
0270
0271 if (bip)
0272 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
0273
0274 xfs_buf_update_cksum(bp, XFS_DA3_NODE_CRC_OFF);
0275 }
0276
0277
0278
0279
0280
0281
0282
0283 static void
0284 xfs_da3_node_read_verify(
0285 struct xfs_buf *bp)
0286 {
0287 struct xfs_da_blkinfo *info = bp->b_addr;
0288 xfs_failaddr_t fa;
0289
0290 switch (be16_to_cpu(info->magic)) {
0291 case XFS_DA3_NODE_MAGIC:
0292 if (!xfs_buf_verify_cksum(bp, XFS_DA3_NODE_CRC_OFF)) {
0293 xfs_verifier_error(bp, -EFSBADCRC,
0294 __this_address);
0295 break;
0296 }
0297 fallthrough;
0298 case XFS_DA_NODE_MAGIC:
0299 fa = xfs_da3_node_verify(bp);
0300 if (fa)
0301 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
0302 return;
0303 case XFS_ATTR_LEAF_MAGIC:
0304 case XFS_ATTR3_LEAF_MAGIC:
0305 bp->b_ops = &xfs_attr3_leaf_buf_ops;
0306 bp->b_ops->verify_read(bp);
0307 return;
0308 case XFS_DIR2_LEAFN_MAGIC:
0309 case XFS_DIR3_LEAFN_MAGIC:
0310 bp->b_ops = &xfs_dir3_leafn_buf_ops;
0311 bp->b_ops->verify_read(bp);
0312 return;
0313 default:
0314 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
0315 break;
0316 }
0317 }
0318
0319
0320 static xfs_failaddr_t
0321 xfs_da3_node_verify_struct(
0322 struct xfs_buf *bp)
0323 {
0324 struct xfs_da_blkinfo *info = bp->b_addr;
0325
0326 switch (be16_to_cpu(info->magic)) {
0327 case XFS_DA3_NODE_MAGIC:
0328 case XFS_DA_NODE_MAGIC:
0329 return xfs_da3_node_verify(bp);
0330 case XFS_ATTR_LEAF_MAGIC:
0331 case XFS_ATTR3_LEAF_MAGIC:
0332 bp->b_ops = &xfs_attr3_leaf_buf_ops;
0333 return bp->b_ops->verify_struct(bp);
0334 case XFS_DIR2_LEAFN_MAGIC:
0335 case XFS_DIR3_LEAFN_MAGIC:
0336 bp->b_ops = &xfs_dir3_leafn_buf_ops;
0337 return bp->b_ops->verify_struct(bp);
0338 default:
0339 return __this_address;
0340 }
0341 }
0342
0343 const struct xfs_buf_ops xfs_da3_node_buf_ops = {
0344 .name = "xfs_da3_node",
0345 .magic16 = { cpu_to_be16(XFS_DA_NODE_MAGIC),
0346 cpu_to_be16(XFS_DA3_NODE_MAGIC) },
0347 .verify_read = xfs_da3_node_read_verify,
0348 .verify_write = xfs_da3_node_write_verify,
0349 .verify_struct = xfs_da3_node_verify_struct,
0350 };
0351
0352 static int
0353 xfs_da3_node_set_type(
0354 struct xfs_trans *tp,
0355 struct xfs_buf *bp)
0356 {
0357 struct xfs_da_blkinfo *info = bp->b_addr;
0358
0359 switch (be16_to_cpu(info->magic)) {
0360 case XFS_DA_NODE_MAGIC:
0361 case XFS_DA3_NODE_MAGIC:
0362 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
0363 return 0;
0364 case XFS_ATTR_LEAF_MAGIC:
0365 case XFS_ATTR3_LEAF_MAGIC:
0366 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_ATTR_LEAF_BUF);
0367 return 0;
0368 case XFS_DIR2_LEAFN_MAGIC:
0369 case XFS_DIR3_LEAFN_MAGIC:
0370 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
0371 return 0;
0372 default:
0373 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, tp->t_mountp,
0374 info, sizeof(*info));
0375 xfs_trans_brelse(tp, bp);
0376 return -EFSCORRUPTED;
0377 }
0378 }
0379
0380 int
0381 xfs_da3_node_read(
0382 struct xfs_trans *tp,
0383 struct xfs_inode *dp,
0384 xfs_dablk_t bno,
0385 struct xfs_buf **bpp,
0386 int whichfork)
0387 {
0388 int error;
0389
0390 error = xfs_da_read_buf(tp, dp, bno, 0, bpp, whichfork,
0391 &xfs_da3_node_buf_ops);
0392 if (error || !*bpp || !tp)
0393 return error;
0394 return xfs_da3_node_set_type(tp, *bpp);
0395 }
0396
0397 int
0398 xfs_da3_node_read_mapped(
0399 struct xfs_trans *tp,
0400 struct xfs_inode *dp,
0401 xfs_daddr_t mappedbno,
0402 struct xfs_buf **bpp,
0403 int whichfork)
0404 {
0405 struct xfs_mount *mp = dp->i_mount;
0406 int error;
0407
0408 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, mappedbno,
0409 XFS_FSB_TO_BB(mp, xfs_dabuf_nfsb(mp, whichfork)), 0,
0410 bpp, &xfs_da3_node_buf_ops);
0411 if (error || !*bpp)
0412 return error;
0413
0414 if (whichfork == XFS_ATTR_FORK)
0415 xfs_buf_set_ref(*bpp, XFS_ATTR_BTREE_REF);
0416 else
0417 xfs_buf_set_ref(*bpp, XFS_DIR_BTREE_REF);
0418
0419 if (!tp)
0420 return 0;
0421 return xfs_da3_node_set_type(tp, *bpp);
0422 }
0423
0424
0425
0426
0427
0428
0429
0430
0431 int
0432 xfs_da3_node_create(
0433 struct xfs_da_args *args,
0434 xfs_dablk_t blkno,
0435 int level,
0436 struct xfs_buf **bpp,
0437 int whichfork)
0438 {
0439 struct xfs_da_intnode *node;
0440 struct xfs_trans *tp = args->trans;
0441 struct xfs_mount *mp = tp->t_mountp;
0442 struct xfs_da3_icnode_hdr ichdr = {0};
0443 struct xfs_buf *bp;
0444 int error;
0445 struct xfs_inode *dp = args->dp;
0446
0447 trace_xfs_da_node_create(args);
0448 ASSERT(level <= XFS_DA_NODE_MAXDEPTH);
0449
0450 error = xfs_da_get_buf(tp, dp, blkno, &bp, whichfork);
0451 if (error)
0452 return error;
0453 bp->b_ops = &xfs_da3_node_buf_ops;
0454 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
0455 node = bp->b_addr;
0456
0457 if (xfs_has_crc(mp)) {
0458 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
0459
0460 memset(hdr3, 0, sizeof(struct xfs_da3_node_hdr));
0461 ichdr.magic = XFS_DA3_NODE_MAGIC;
0462 hdr3->info.blkno = cpu_to_be64(xfs_buf_daddr(bp));
0463 hdr3->info.owner = cpu_to_be64(args->dp->i_ino);
0464 uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid);
0465 } else {
0466 ichdr.magic = XFS_DA_NODE_MAGIC;
0467 }
0468 ichdr.level = level;
0469
0470 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &ichdr);
0471 xfs_trans_log_buf(tp, bp,
0472 XFS_DA_LOGRANGE(node, &node->hdr, args->geo->node_hdr_size));
0473
0474 *bpp = bp;
0475 return 0;
0476 }
0477
0478
0479
0480
0481
0482 int
0483 xfs_da3_split(
0484 struct xfs_da_state *state)
0485 {
0486 struct xfs_da_state_blk *oldblk;
0487 struct xfs_da_state_blk *newblk;
0488 struct xfs_da_state_blk *addblk;
0489 struct xfs_da_intnode *node;
0490 int max;
0491 int action = 0;
0492 int error;
0493 int i;
0494
0495 trace_xfs_da_split(state->args);
0496
0497 if (XFS_TEST_ERROR(false, state->mp, XFS_ERRTAG_DA_LEAF_SPLIT))
0498 return -EIO;
0499
0500
0501
0502
0503
0504
0505
0506 max = state->path.active - 1;
0507 ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
0508 ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
0509 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
0510
0511 addblk = &state->path.blk[max];
0512 for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
0513 oldblk = &state->path.blk[i];
0514 newblk = &state->altpath.blk[i];
0515
0516
0517
0518
0519
0520
0521
0522 switch (oldblk->magic) {
0523 case XFS_ATTR_LEAF_MAGIC:
0524 error = xfs_attr3_leaf_split(state, oldblk, newblk);
0525 if ((error != 0) && (error != -ENOSPC)) {
0526 return error;
0527 }
0528 if (!error) {
0529 addblk = newblk;
0530 break;
0531 }
0532
0533
0534
0535
0536
0537 state->extravalid = 1;
0538 if (state->inleaf) {
0539 state->extraafter = 0;
0540 trace_xfs_attr_leaf_split_before(state->args);
0541 error = xfs_attr3_leaf_split(state, oldblk,
0542 &state->extrablk);
0543 } else {
0544 state->extraafter = 1;
0545 trace_xfs_attr_leaf_split_after(state->args);
0546 error = xfs_attr3_leaf_split(state, newblk,
0547 &state->extrablk);
0548 }
0549 if (error)
0550 return error;
0551 addblk = newblk;
0552 break;
0553 case XFS_DIR2_LEAFN_MAGIC:
0554 error = xfs_dir2_leafn_split(state, oldblk, newblk);
0555 if (error)
0556 return error;
0557 addblk = newblk;
0558 break;
0559 case XFS_DA_NODE_MAGIC:
0560 error = xfs_da3_node_split(state, oldblk, newblk, addblk,
0561 max - i, &action);
0562 addblk->bp = NULL;
0563 if (error)
0564 return error;
0565
0566
0567
0568 if (action)
0569 addblk = newblk;
0570 else
0571 addblk = NULL;
0572 break;
0573 }
0574
0575
0576
0577
0578 xfs_da3_fixhashpath(state, &state->path);
0579 }
0580 if (!addblk)
0581 return 0;
0582
0583
0584
0585
0586
0587
0588 ASSERT(state->extravalid == 0 ||
0589 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
0590
0591
0592
0593
0594 ASSERT(state->path.active == 0);
0595 oldblk = &state->path.blk[0];
0596 error = xfs_da3_root_split(state, oldblk, addblk);
0597 if (error)
0598 goto out;
0599
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611 node = oldblk->bp->b_addr;
0612 if (node->hdr.info.forw) {
0613 if (be32_to_cpu(node->hdr.info.forw) != addblk->blkno) {
0614 xfs_buf_mark_corrupt(oldblk->bp);
0615 error = -EFSCORRUPTED;
0616 goto out;
0617 }
0618 node = addblk->bp->b_addr;
0619 node->hdr.info.back = cpu_to_be32(oldblk->blkno);
0620 xfs_trans_log_buf(state->args->trans, addblk->bp,
0621 XFS_DA_LOGRANGE(node, &node->hdr.info,
0622 sizeof(node->hdr.info)));
0623 }
0624 node = oldblk->bp->b_addr;
0625 if (node->hdr.info.back) {
0626 if (be32_to_cpu(node->hdr.info.back) != addblk->blkno) {
0627 xfs_buf_mark_corrupt(oldblk->bp);
0628 error = -EFSCORRUPTED;
0629 goto out;
0630 }
0631 node = addblk->bp->b_addr;
0632 node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
0633 xfs_trans_log_buf(state->args->trans, addblk->bp,
0634 XFS_DA_LOGRANGE(node, &node->hdr.info,
0635 sizeof(node->hdr.info)));
0636 }
0637 out:
0638 addblk->bp = NULL;
0639 return error;
0640 }
0641
0642
0643
0644
0645
0646
0647 STATIC int
0648 xfs_da3_root_split(
0649 struct xfs_da_state *state,
0650 struct xfs_da_state_blk *blk1,
0651 struct xfs_da_state_blk *blk2)
0652 {
0653 struct xfs_da_intnode *node;
0654 struct xfs_da_intnode *oldroot;
0655 struct xfs_da_node_entry *btree;
0656 struct xfs_da3_icnode_hdr nodehdr;
0657 struct xfs_da_args *args;
0658 struct xfs_buf *bp;
0659 struct xfs_inode *dp;
0660 struct xfs_trans *tp;
0661 struct xfs_dir2_leaf *leaf;
0662 xfs_dablk_t blkno;
0663 int level;
0664 int error;
0665 int size;
0666
0667 trace_xfs_da_root_split(state->args);
0668
0669
0670
0671
0672
0673 args = state->args;
0674 error = xfs_da_grow_inode(args, &blkno);
0675 if (error)
0676 return error;
0677
0678 dp = args->dp;
0679 tp = args->trans;
0680 error = xfs_da_get_buf(tp, dp, blkno, &bp, args->whichfork);
0681 if (error)
0682 return error;
0683 node = bp->b_addr;
0684 oldroot = blk1->bp->b_addr;
0685 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
0686 oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
0687 struct xfs_da3_icnode_hdr icnodehdr;
0688
0689 xfs_da3_node_hdr_from_disk(dp->i_mount, &icnodehdr, oldroot);
0690 btree = icnodehdr.btree;
0691 size = (int)((char *)&btree[icnodehdr.count] - (char *)oldroot);
0692 level = icnodehdr.level;
0693
0694
0695
0696
0697
0698 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
0699 } else {
0700 struct xfs_dir3_icleaf_hdr leafhdr;
0701
0702 leaf = (xfs_dir2_leaf_t *)oldroot;
0703 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
0704
0705 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
0706 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
0707 size = (int)((char *)&leafhdr.ents[leafhdr.count] -
0708 (char *)leaf);
0709 level = 0;
0710
0711
0712
0713
0714
0715 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
0716 }
0717
0718
0719
0720
0721
0722
0723
0724 memcpy(node, oldroot, size);
0725 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
0726 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
0727 struct xfs_da3_intnode *node3 = (struct xfs_da3_intnode *)node;
0728
0729 node3->hdr.info.blkno = cpu_to_be64(xfs_buf_daddr(bp));
0730 }
0731 xfs_trans_log_buf(tp, bp, 0, size - 1);
0732
0733 bp->b_ops = blk1->bp->b_ops;
0734 xfs_trans_buf_copy_type(bp, blk1->bp);
0735 blk1->bp = bp;
0736 blk1->blkno = blkno;
0737
0738
0739
0740
0741 error = xfs_da3_node_create(args,
0742 (args->whichfork == XFS_DATA_FORK) ? args->geo->leafblk : 0,
0743 level + 1, &bp, args->whichfork);
0744 if (error)
0745 return error;
0746
0747 node = bp->b_addr;
0748 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
0749 btree = nodehdr.btree;
0750 btree[0].hashval = cpu_to_be32(blk1->hashval);
0751 btree[0].before = cpu_to_be32(blk1->blkno);
0752 btree[1].hashval = cpu_to_be32(blk2->hashval);
0753 btree[1].before = cpu_to_be32(blk2->blkno);
0754 nodehdr.count = 2;
0755 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
0756
0757 #ifdef DEBUG
0758 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
0759 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
0760 ASSERT(blk1->blkno >= args->geo->leafblk &&
0761 blk1->blkno < args->geo->freeblk);
0762 ASSERT(blk2->blkno >= args->geo->leafblk &&
0763 blk2->blkno < args->geo->freeblk);
0764 }
0765 #endif
0766
0767
0768 xfs_trans_log_buf(tp, bp,
0769 XFS_DA_LOGRANGE(node, btree, sizeof(xfs_da_node_entry_t) * 2));
0770
0771 return 0;
0772 }
0773
0774
0775
0776
0777 STATIC int
0778 xfs_da3_node_split(
0779 struct xfs_da_state *state,
0780 struct xfs_da_state_blk *oldblk,
0781 struct xfs_da_state_blk *newblk,
0782 struct xfs_da_state_blk *addblk,
0783 int treelevel,
0784 int *result)
0785 {
0786 struct xfs_da_intnode *node;
0787 struct xfs_da3_icnode_hdr nodehdr;
0788 xfs_dablk_t blkno;
0789 int newcount;
0790 int error;
0791 int useextra;
0792 struct xfs_inode *dp = state->args->dp;
0793
0794 trace_xfs_da_node_split(state->args);
0795
0796 node = oldblk->bp->b_addr;
0797 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
0798
0799
0800
0801
0802 useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;
0803 newcount = 1 + useextra;
0804
0805
0806
0807 if (nodehdr.count + newcount > state->args->geo->node_ents) {
0808
0809
0810
0811
0812 error = xfs_da_grow_inode(state->args, &blkno);
0813 if (error)
0814 return error;
0815
0816 error = xfs_da3_node_create(state->args, blkno, treelevel,
0817 &newblk->bp, state->args->whichfork);
0818 if (error)
0819 return error;
0820 newblk->blkno = blkno;
0821 newblk->magic = XFS_DA_NODE_MAGIC;
0822 xfs_da3_node_rebalance(state, oldblk, newblk);
0823 error = xfs_da3_blk_link(state, oldblk, newblk);
0824 if (error)
0825 return error;
0826 *result = 1;
0827 } else {
0828 *result = 0;
0829 }
0830
0831
0832
0833
0834
0835
0836
0837
0838
0839
0840
0841
0842
0843 node = oldblk->bp->b_addr;
0844 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
0845 if (oldblk->index <= nodehdr.count) {
0846 oldblk->index++;
0847 xfs_da3_node_add(state, oldblk, addblk);
0848 if (useextra) {
0849 if (state->extraafter)
0850 oldblk->index++;
0851 xfs_da3_node_add(state, oldblk, &state->extrablk);
0852 state->extravalid = 0;
0853 }
0854 } else {
0855 newblk->index++;
0856 xfs_da3_node_add(state, newblk, addblk);
0857 if (useextra) {
0858 if (state->extraafter)
0859 newblk->index++;
0860 xfs_da3_node_add(state, newblk, &state->extrablk);
0861 state->extravalid = 0;
0862 }
0863 }
0864
0865 return 0;
0866 }
0867
0868
0869
0870
0871
0872
0873
0874 STATIC void
0875 xfs_da3_node_rebalance(
0876 struct xfs_da_state *state,
0877 struct xfs_da_state_blk *blk1,
0878 struct xfs_da_state_blk *blk2)
0879 {
0880 struct xfs_da_intnode *node1;
0881 struct xfs_da_intnode *node2;
0882 struct xfs_da_node_entry *btree1;
0883 struct xfs_da_node_entry *btree2;
0884 struct xfs_da_node_entry *btree_s;
0885 struct xfs_da_node_entry *btree_d;
0886 struct xfs_da3_icnode_hdr nodehdr1;
0887 struct xfs_da3_icnode_hdr nodehdr2;
0888 struct xfs_trans *tp;
0889 int count;
0890 int tmp;
0891 int swap = 0;
0892 struct xfs_inode *dp = state->args->dp;
0893
0894 trace_xfs_da_node_rebalance(state->args);
0895
0896 node1 = blk1->bp->b_addr;
0897 node2 = blk2->bp->b_addr;
0898 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
0899 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
0900 btree1 = nodehdr1.btree;
0901 btree2 = nodehdr2.btree;
0902
0903
0904
0905
0906
0907 if (nodehdr1.count > 0 && nodehdr2.count > 0 &&
0908 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
0909 (be32_to_cpu(btree2[nodehdr2.count - 1].hashval) <
0910 be32_to_cpu(btree1[nodehdr1.count - 1].hashval)))) {
0911 swap(node1, node2);
0912 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
0913 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
0914 btree1 = nodehdr1.btree;
0915 btree2 = nodehdr2.btree;
0916 swap = 1;
0917 }
0918
0919 count = (nodehdr1.count - nodehdr2.count) / 2;
0920 if (count == 0)
0921 return;
0922 tp = state->args->trans;
0923
0924
0925
0926 if (count > 0) {
0927
0928
0929
0930 tmp = nodehdr2.count;
0931 if (tmp > 0) {
0932 tmp *= (uint)sizeof(xfs_da_node_entry_t);
0933 btree_s = &btree2[0];
0934 btree_d = &btree2[count];
0935 memmove(btree_d, btree_s, tmp);
0936 }
0937
0938
0939
0940
0941
0942 nodehdr2.count += count;
0943 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
0944 btree_s = &btree1[nodehdr1.count - count];
0945 btree_d = &btree2[0];
0946 memcpy(btree_d, btree_s, tmp);
0947 nodehdr1.count -= count;
0948 } else {
0949
0950
0951
0952
0953 count = -count;
0954 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
0955 btree_s = &btree2[0];
0956 btree_d = &btree1[nodehdr1.count];
0957 memcpy(btree_d, btree_s, tmp);
0958 nodehdr1.count += count;
0959
0960 xfs_trans_log_buf(tp, blk1->bp,
0961 XFS_DA_LOGRANGE(node1, btree_d, tmp));
0962
0963
0964
0965
0966 tmp = nodehdr2.count - count;
0967 tmp *= (uint)sizeof(xfs_da_node_entry_t);
0968 btree_s = &btree2[count];
0969 btree_d = &btree2[0];
0970 memmove(btree_d, btree_s, tmp);
0971 nodehdr2.count -= count;
0972 }
0973
0974
0975
0976
0977 xfs_da3_node_hdr_to_disk(dp->i_mount, node1, &nodehdr1);
0978 xfs_trans_log_buf(tp, blk1->bp,
0979 XFS_DA_LOGRANGE(node1, &node1->hdr,
0980 state->args->geo->node_hdr_size));
0981
0982 xfs_da3_node_hdr_to_disk(dp->i_mount, node2, &nodehdr2);
0983 xfs_trans_log_buf(tp, blk2->bp,
0984 XFS_DA_LOGRANGE(node2, &node2->hdr,
0985 state->args->geo->node_hdr_size +
0986 (sizeof(btree2[0]) * nodehdr2.count)));
0987
0988
0989
0990
0991
0992 if (swap) {
0993 node1 = blk1->bp->b_addr;
0994 node2 = blk2->bp->b_addr;
0995 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
0996 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
0997 btree1 = nodehdr1.btree;
0998 btree2 = nodehdr2.btree;
0999 }
1000 blk1->hashval = be32_to_cpu(btree1[nodehdr1.count - 1].hashval);
1001 blk2->hashval = be32_to_cpu(btree2[nodehdr2.count - 1].hashval);
1002
1003
1004
1005
1006 if (blk1->index >= nodehdr1.count) {
1007 blk2->index = blk1->index - nodehdr1.count;
1008 blk1->index = nodehdr1.count + 1;
1009 }
1010 }
1011
1012
1013
1014
1015 STATIC void
1016 xfs_da3_node_add(
1017 struct xfs_da_state *state,
1018 struct xfs_da_state_blk *oldblk,
1019 struct xfs_da_state_blk *newblk)
1020 {
1021 struct xfs_da_intnode *node;
1022 struct xfs_da3_icnode_hdr nodehdr;
1023 struct xfs_da_node_entry *btree;
1024 int tmp;
1025 struct xfs_inode *dp = state->args->dp;
1026
1027 trace_xfs_da_node_add(state->args);
1028
1029 node = oldblk->bp->b_addr;
1030 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1031 btree = nodehdr.btree;
1032
1033 ASSERT(oldblk->index >= 0 && oldblk->index <= nodehdr.count);
1034 ASSERT(newblk->blkno != 0);
1035 if (state->args->whichfork == XFS_DATA_FORK)
1036 ASSERT(newblk->blkno >= state->args->geo->leafblk &&
1037 newblk->blkno < state->args->geo->freeblk);
1038
1039
1040
1041
1042 tmp = 0;
1043 if (oldblk->index < nodehdr.count) {
1044 tmp = (nodehdr.count - oldblk->index) * (uint)sizeof(*btree);
1045 memmove(&btree[oldblk->index + 1], &btree[oldblk->index], tmp);
1046 }
1047 btree[oldblk->index].hashval = cpu_to_be32(newblk->hashval);
1048 btree[oldblk->index].before = cpu_to_be32(newblk->blkno);
1049 xfs_trans_log_buf(state->args->trans, oldblk->bp,
1050 XFS_DA_LOGRANGE(node, &btree[oldblk->index],
1051 tmp + sizeof(*btree)));
1052
1053 nodehdr.count += 1;
1054 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
1055 xfs_trans_log_buf(state->args->trans, oldblk->bp,
1056 XFS_DA_LOGRANGE(node, &node->hdr,
1057 state->args->geo->node_hdr_size));
1058
1059
1060
1061
1062 oldblk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
1063 }
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073 int
1074 xfs_da3_join(
1075 struct xfs_da_state *state)
1076 {
1077 struct xfs_da_state_blk *drop_blk;
1078 struct xfs_da_state_blk *save_blk;
1079 int action = 0;
1080 int error;
1081
1082 trace_xfs_da_join(state->args);
1083
1084 drop_blk = &state->path.blk[ state->path.active-1 ];
1085 save_blk = &state->altpath.blk[ state->path.active-1 ];
1086 ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
1087 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
1088 drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1089
1090
1091
1092
1093
1094 for ( ; state->path.active >= 2; drop_blk--, save_blk--,
1095 state->path.active--) {
1096
1097
1098
1099
1100
1101
1102 switch (drop_blk->magic) {
1103 case XFS_ATTR_LEAF_MAGIC:
1104 error = xfs_attr3_leaf_toosmall(state, &action);
1105 if (error)
1106 return error;
1107 if (action == 0)
1108 return 0;
1109 xfs_attr3_leaf_unbalance(state, drop_blk, save_blk);
1110 break;
1111 case XFS_DIR2_LEAFN_MAGIC:
1112 error = xfs_dir2_leafn_toosmall(state, &action);
1113 if (error)
1114 return error;
1115 if (action == 0)
1116 return 0;
1117 xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
1118 break;
1119 case XFS_DA_NODE_MAGIC:
1120
1121
1122
1123
1124 xfs_da3_node_remove(state, drop_blk);
1125 xfs_da3_fixhashpath(state, &state->path);
1126 error = xfs_da3_node_toosmall(state, &action);
1127 if (error)
1128 return error;
1129 if (action == 0)
1130 return 0;
1131 xfs_da3_node_unbalance(state, drop_blk, save_blk);
1132 break;
1133 }
1134 xfs_da3_fixhashpath(state, &state->altpath);
1135 error = xfs_da3_blk_unlink(state, drop_blk, save_blk);
1136 xfs_da_state_kill_altpath(state);
1137 if (error)
1138 return error;
1139 error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
1140 drop_blk->bp);
1141 drop_blk->bp = NULL;
1142 if (error)
1143 return error;
1144 }
1145
1146
1147
1148
1149
1150 xfs_da3_node_remove(state, drop_blk);
1151 xfs_da3_fixhashpath(state, &state->path);
1152 error = xfs_da3_root_join(state, &state->path.blk[0]);
1153 return error;
1154 }
1155
1156 #ifdef DEBUG
1157 static void
1158 xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo *blkinfo, __u16 level)
1159 {
1160 __be16 magic = blkinfo->magic;
1161
1162 if (level == 1) {
1163 ASSERT(magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1164 magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
1165 magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
1166 magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
1167 } else {
1168 ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
1169 magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
1170 }
1171 ASSERT(!blkinfo->forw);
1172 ASSERT(!blkinfo->back);
1173 }
1174 #else
1175 #define xfs_da_blkinfo_onlychild_validate(blkinfo, level)
1176 #endif
1177
1178
1179
1180
1181
1182 STATIC int
1183 xfs_da3_root_join(
1184 struct xfs_da_state *state,
1185 struct xfs_da_state_blk *root_blk)
1186 {
1187 struct xfs_da_intnode *oldroot;
1188 struct xfs_da_args *args;
1189 xfs_dablk_t child;
1190 struct xfs_buf *bp;
1191 struct xfs_da3_icnode_hdr oldroothdr;
1192 int error;
1193 struct xfs_inode *dp = state->args->dp;
1194
1195 trace_xfs_da_root_join(state->args);
1196
1197 ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
1198
1199 args = state->args;
1200 oldroot = root_blk->bp->b_addr;
1201 xfs_da3_node_hdr_from_disk(dp->i_mount, &oldroothdr, oldroot);
1202 ASSERT(oldroothdr.forw == 0);
1203 ASSERT(oldroothdr.back == 0);
1204
1205
1206
1207
1208 if (oldroothdr.count > 1)
1209 return 0;
1210
1211
1212
1213
1214
1215 child = be32_to_cpu(oldroothdr.btree[0].before);
1216 ASSERT(child != 0);
1217 error = xfs_da3_node_read(args->trans, dp, child, &bp, args->whichfork);
1218 if (error)
1219 return error;
1220 xfs_da_blkinfo_onlychild_validate(bp->b_addr, oldroothdr.level);
1221
1222
1223
1224
1225
1226
1227
1228
1229 memcpy(root_blk->bp->b_addr, bp->b_addr, args->geo->blksize);
1230 root_blk->bp->b_ops = bp->b_ops;
1231 xfs_trans_buf_copy_type(root_blk->bp, bp);
1232 if (oldroothdr.magic == XFS_DA3_NODE_MAGIC) {
1233 struct xfs_da3_blkinfo *da3 = root_blk->bp->b_addr;
1234 da3->blkno = cpu_to_be64(xfs_buf_daddr(root_blk->bp));
1235 }
1236 xfs_trans_log_buf(args->trans, root_blk->bp, 0,
1237 args->geo->blksize - 1);
1238 error = xfs_da_shrink_inode(args, child, bp);
1239 return error;
1240 }
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251 STATIC int
1252 xfs_da3_node_toosmall(
1253 struct xfs_da_state *state,
1254 int *action)
1255 {
1256 struct xfs_da_intnode *node;
1257 struct xfs_da_state_blk *blk;
1258 struct xfs_da_blkinfo *info;
1259 xfs_dablk_t blkno;
1260 struct xfs_buf *bp;
1261 struct xfs_da3_icnode_hdr nodehdr;
1262 int count;
1263 int forward;
1264 int error;
1265 int retval;
1266 int i;
1267 struct xfs_inode *dp = state->args->dp;
1268
1269 trace_xfs_da_node_toosmall(state->args);
1270
1271
1272
1273
1274
1275
1276 blk = &state->path.blk[ state->path.active-1 ];
1277 info = blk->bp->b_addr;
1278 node = (xfs_da_intnode_t *)info;
1279 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1280 if (nodehdr.count > (state->args->geo->node_ents >> 1)) {
1281 *action = 0;
1282 return 0;
1283 }
1284
1285
1286
1287
1288
1289
1290
1291 if (nodehdr.count == 0) {
1292
1293
1294
1295
1296 forward = (info->forw != 0);
1297 memcpy(&state->altpath, &state->path, sizeof(state->path));
1298 error = xfs_da3_path_shift(state, &state->altpath, forward,
1299 0, &retval);
1300 if (error)
1301 return error;
1302 if (retval) {
1303 *action = 0;
1304 } else {
1305 *action = 2;
1306 }
1307 return 0;
1308 }
1309
1310
1311
1312
1313
1314
1315
1316
1317 count = state->args->geo->node_ents;
1318 count -= state->args->geo->node_ents >> 2;
1319 count -= nodehdr.count;
1320
1321
1322 forward = nodehdr.forw < nodehdr.back;
1323 for (i = 0; i < 2; forward = !forward, i++) {
1324 struct xfs_da3_icnode_hdr thdr;
1325 if (forward)
1326 blkno = nodehdr.forw;
1327 else
1328 blkno = nodehdr.back;
1329 if (blkno == 0)
1330 continue;
1331 error = xfs_da3_node_read(state->args->trans, dp, blkno, &bp,
1332 state->args->whichfork);
1333 if (error)
1334 return error;
1335
1336 node = bp->b_addr;
1337 xfs_da3_node_hdr_from_disk(dp->i_mount, &thdr, node);
1338 xfs_trans_brelse(state->args->trans, bp);
1339
1340 if (count - thdr.count >= 0)
1341 break;
1342 }
1343 if (i >= 2) {
1344 *action = 0;
1345 return 0;
1346 }
1347
1348
1349
1350
1351
1352 memcpy(&state->altpath, &state->path, sizeof(state->path));
1353 if (blkno < blk->blkno) {
1354 error = xfs_da3_path_shift(state, &state->altpath, forward,
1355 0, &retval);
1356 } else {
1357 error = xfs_da3_path_shift(state, &state->path, forward,
1358 0, &retval);
1359 }
1360 if (error)
1361 return error;
1362 if (retval) {
1363 *action = 0;
1364 return 0;
1365 }
1366 *action = 1;
1367 return 0;
1368 }
1369
1370
1371
1372
1373 STATIC uint
1374 xfs_da3_node_lasthash(
1375 struct xfs_inode *dp,
1376 struct xfs_buf *bp,
1377 int *count)
1378 {
1379 struct xfs_da3_icnode_hdr nodehdr;
1380
1381 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, bp->b_addr);
1382 if (count)
1383 *count = nodehdr.count;
1384 if (!nodehdr.count)
1385 return 0;
1386 return be32_to_cpu(nodehdr.btree[nodehdr.count - 1].hashval);
1387 }
1388
1389
1390
1391
1392
1393 void
1394 xfs_da3_fixhashpath(
1395 struct xfs_da_state *state,
1396 struct xfs_da_state_path *path)
1397 {
1398 struct xfs_da_state_blk *blk;
1399 struct xfs_da_intnode *node;
1400 struct xfs_da_node_entry *btree;
1401 xfs_dahash_t lasthash=0;
1402 int level;
1403 int count;
1404 struct xfs_inode *dp = state->args->dp;
1405
1406 trace_xfs_da_fixhashpath(state->args);
1407
1408 level = path->active-1;
1409 blk = &path->blk[ level ];
1410 switch (blk->magic) {
1411 case XFS_ATTR_LEAF_MAGIC:
1412 lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
1413 if (count == 0)
1414 return;
1415 break;
1416 case XFS_DIR2_LEAFN_MAGIC:
1417 lasthash = xfs_dir2_leaf_lasthash(dp, blk->bp, &count);
1418 if (count == 0)
1419 return;
1420 break;
1421 case XFS_DA_NODE_MAGIC:
1422 lasthash = xfs_da3_node_lasthash(dp, blk->bp, &count);
1423 if (count == 0)
1424 return;
1425 break;
1426 }
1427 for (blk--, level--; level >= 0; blk--, level--) {
1428 struct xfs_da3_icnode_hdr nodehdr;
1429
1430 node = blk->bp->b_addr;
1431 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1432 btree = nodehdr.btree;
1433 if (be32_to_cpu(btree[blk->index].hashval) == lasthash)
1434 break;
1435 blk->hashval = lasthash;
1436 btree[blk->index].hashval = cpu_to_be32(lasthash);
1437 xfs_trans_log_buf(state->args->trans, blk->bp,
1438 XFS_DA_LOGRANGE(node, &btree[blk->index],
1439 sizeof(*btree)));
1440
1441 lasthash = be32_to_cpu(btree[nodehdr.count - 1].hashval);
1442 }
1443 }
1444
1445
1446
1447
1448 STATIC void
1449 xfs_da3_node_remove(
1450 struct xfs_da_state *state,
1451 struct xfs_da_state_blk *drop_blk)
1452 {
1453 struct xfs_da_intnode *node;
1454 struct xfs_da3_icnode_hdr nodehdr;
1455 struct xfs_da_node_entry *btree;
1456 int index;
1457 int tmp;
1458 struct xfs_inode *dp = state->args->dp;
1459
1460 trace_xfs_da_node_remove(state->args);
1461
1462 node = drop_blk->bp->b_addr;
1463 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1464 ASSERT(drop_blk->index < nodehdr.count);
1465 ASSERT(drop_blk->index >= 0);
1466
1467
1468
1469
1470 index = drop_blk->index;
1471 btree = nodehdr.btree;
1472 if (index < nodehdr.count - 1) {
1473 tmp = nodehdr.count - index - 1;
1474 tmp *= (uint)sizeof(xfs_da_node_entry_t);
1475 memmove(&btree[index], &btree[index + 1], tmp);
1476 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1477 XFS_DA_LOGRANGE(node, &btree[index], tmp));
1478 index = nodehdr.count - 1;
1479 }
1480 memset(&btree[index], 0, sizeof(xfs_da_node_entry_t));
1481 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1482 XFS_DA_LOGRANGE(node, &btree[index], sizeof(btree[index])));
1483 nodehdr.count -= 1;
1484 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
1485 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1486 XFS_DA_LOGRANGE(node, &node->hdr, state->args->geo->node_hdr_size));
1487
1488
1489
1490
1491 drop_blk->hashval = be32_to_cpu(btree[index - 1].hashval);
1492 }
1493
1494
1495
1496
1497
1498 STATIC void
1499 xfs_da3_node_unbalance(
1500 struct xfs_da_state *state,
1501 struct xfs_da_state_blk *drop_blk,
1502 struct xfs_da_state_blk *save_blk)
1503 {
1504 struct xfs_da_intnode *drop_node;
1505 struct xfs_da_intnode *save_node;
1506 struct xfs_da_node_entry *drop_btree;
1507 struct xfs_da_node_entry *save_btree;
1508 struct xfs_da3_icnode_hdr drop_hdr;
1509 struct xfs_da3_icnode_hdr save_hdr;
1510 struct xfs_trans *tp;
1511 int sindex;
1512 int tmp;
1513 struct xfs_inode *dp = state->args->dp;
1514
1515 trace_xfs_da_node_unbalance(state->args);
1516
1517 drop_node = drop_blk->bp->b_addr;
1518 save_node = save_blk->bp->b_addr;
1519 xfs_da3_node_hdr_from_disk(dp->i_mount, &drop_hdr, drop_node);
1520 xfs_da3_node_hdr_from_disk(dp->i_mount, &save_hdr, save_node);
1521 drop_btree = drop_hdr.btree;
1522 save_btree = save_hdr.btree;
1523 tp = state->args->trans;
1524
1525
1526
1527
1528
1529 if ((be32_to_cpu(drop_btree[0].hashval) <
1530 be32_to_cpu(save_btree[0].hashval)) ||
1531 (be32_to_cpu(drop_btree[drop_hdr.count - 1].hashval) <
1532 be32_to_cpu(save_btree[save_hdr.count - 1].hashval))) {
1533
1534 tmp = save_hdr.count * sizeof(xfs_da_node_entry_t);
1535 memmove(&save_btree[drop_hdr.count], &save_btree[0], tmp);
1536
1537 sindex = 0;
1538 xfs_trans_log_buf(tp, save_blk->bp,
1539 XFS_DA_LOGRANGE(save_node, &save_btree[0],
1540 (save_hdr.count + drop_hdr.count) *
1541 sizeof(xfs_da_node_entry_t)));
1542 } else {
1543 sindex = save_hdr.count;
1544 xfs_trans_log_buf(tp, save_blk->bp,
1545 XFS_DA_LOGRANGE(save_node, &save_btree[sindex],
1546 drop_hdr.count * sizeof(xfs_da_node_entry_t)));
1547 }
1548
1549
1550
1551
1552 tmp = drop_hdr.count * (uint)sizeof(xfs_da_node_entry_t);
1553 memcpy(&save_btree[sindex], &drop_btree[0], tmp);
1554 save_hdr.count += drop_hdr.count;
1555
1556 xfs_da3_node_hdr_to_disk(dp->i_mount, save_node, &save_hdr);
1557 xfs_trans_log_buf(tp, save_blk->bp,
1558 XFS_DA_LOGRANGE(save_node, &save_node->hdr,
1559 state->args->geo->node_hdr_size));
1560
1561
1562
1563
1564 save_blk->hashval = be32_to_cpu(save_btree[save_hdr.count - 1].hashval);
1565 }
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582 int
1583 xfs_da3_node_lookup_int(
1584 struct xfs_da_state *state,
1585 int *result)
1586 {
1587 struct xfs_da_state_blk *blk;
1588 struct xfs_da_blkinfo *curr;
1589 struct xfs_da_intnode *node;
1590 struct xfs_da_node_entry *btree;
1591 struct xfs_da3_icnode_hdr nodehdr;
1592 struct xfs_da_args *args;
1593 xfs_dablk_t blkno;
1594 xfs_dahash_t hashval;
1595 xfs_dahash_t btreehashval;
1596 int probe;
1597 int span;
1598 int max;
1599 int error;
1600 int retval;
1601 unsigned int expected_level = 0;
1602 uint16_t magic;
1603 struct xfs_inode *dp = state->args->dp;
1604
1605 args = state->args;
1606
1607
1608
1609
1610
1611 blkno = args->geo->leafblk;
1612 for (blk = &state->path.blk[0], state->path.active = 1;
1613 state->path.active <= XFS_DA_NODE_MAXDEPTH;
1614 blk++, state->path.active++) {
1615
1616
1617
1618 blk->blkno = blkno;
1619 error = xfs_da3_node_read(args->trans, args->dp, blkno,
1620 &blk->bp, args->whichfork);
1621 if (error) {
1622 blk->blkno = 0;
1623 state->path.active--;
1624 return error;
1625 }
1626 curr = blk->bp->b_addr;
1627 magic = be16_to_cpu(curr->magic);
1628
1629 if (magic == XFS_ATTR_LEAF_MAGIC ||
1630 magic == XFS_ATTR3_LEAF_MAGIC) {
1631 blk->magic = XFS_ATTR_LEAF_MAGIC;
1632 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1633 break;
1634 }
1635
1636 if (magic == XFS_DIR2_LEAFN_MAGIC ||
1637 magic == XFS_DIR3_LEAFN_MAGIC) {
1638 blk->magic = XFS_DIR2_LEAFN_MAGIC;
1639 blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
1640 blk->bp, NULL);
1641 break;
1642 }
1643
1644 if (magic != XFS_DA_NODE_MAGIC && magic != XFS_DA3_NODE_MAGIC) {
1645 xfs_buf_mark_corrupt(blk->bp);
1646 return -EFSCORRUPTED;
1647 }
1648
1649 blk->magic = XFS_DA_NODE_MAGIC;
1650
1651
1652
1653
1654 node = blk->bp->b_addr;
1655 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1656 btree = nodehdr.btree;
1657
1658
1659 if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH) {
1660 xfs_buf_mark_corrupt(blk->bp);
1661 return -EFSCORRUPTED;
1662 }
1663
1664
1665 if (blkno == args->geo->leafblk)
1666 expected_level = nodehdr.level - 1;
1667 else if (expected_level != nodehdr.level) {
1668 xfs_buf_mark_corrupt(blk->bp);
1669 return -EFSCORRUPTED;
1670 } else
1671 expected_level--;
1672
1673 max = nodehdr.count;
1674 blk->hashval = be32_to_cpu(btree[max - 1].hashval);
1675
1676
1677
1678
1679 probe = span = max / 2;
1680 hashval = args->hashval;
1681 while (span > 4) {
1682 span /= 2;
1683 btreehashval = be32_to_cpu(btree[probe].hashval);
1684 if (btreehashval < hashval)
1685 probe += span;
1686 else if (btreehashval > hashval)
1687 probe -= span;
1688 else
1689 break;
1690 }
1691 ASSERT((probe >= 0) && (probe < max));
1692 ASSERT((span <= 4) ||
1693 (be32_to_cpu(btree[probe].hashval) == hashval));
1694
1695
1696
1697
1698
1699 while (probe > 0 &&
1700 be32_to_cpu(btree[probe].hashval) >= hashval) {
1701 probe--;
1702 }
1703 while (probe < max &&
1704 be32_to_cpu(btree[probe].hashval) < hashval) {
1705 probe++;
1706 }
1707
1708
1709
1710
1711 if (probe == max) {
1712 blk->index = max - 1;
1713 blkno = be32_to_cpu(btree[max - 1].before);
1714 } else {
1715 blk->index = probe;
1716 blkno = be32_to_cpu(btree[probe].before);
1717 }
1718
1719
1720 if (XFS_IS_CORRUPT(dp->i_mount, blkno == args->geo->leafblk))
1721 return -EFSCORRUPTED;
1722 }
1723
1724 if (XFS_IS_CORRUPT(dp->i_mount, expected_level != 0))
1725 return -EFSCORRUPTED;
1726
1727
1728
1729
1730
1731
1732
1733 for (;;) {
1734 if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
1735 retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
1736 &blk->index, state);
1737 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
1738 retval = xfs_attr3_leaf_lookup_int(blk->bp, args);
1739 blk->index = args->index;
1740 args->blkno = blk->blkno;
1741 } else {
1742 ASSERT(0);
1743 return -EFSCORRUPTED;
1744 }
1745 if (((retval == -ENOENT) || (retval == -ENOATTR)) &&
1746 (blk->hashval == args->hashval)) {
1747 error = xfs_da3_path_shift(state, &state->path, 1, 1,
1748 &retval);
1749 if (error)
1750 return error;
1751 if (retval == 0) {
1752 continue;
1753 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
1754
1755 retval = -ENOATTR;
1756 }
1757 }
1758 break;
1759 }
1760 *result = retval;
1761 return 0;
1762 }
1763
1764
1765
1766
1767
1768
1769
1770
1771 STATIC int
1772 xfs_da3_node_order(
1773 struct xfs_inode *dp,
1774 struct xfs_buf *node1_bp,
1775 struct xfs_buf *node2_bp)
1776 {
1777 struct xfs_da_intnode *node1;
1778 struct xfs_da_intnode *node2;
1779 struct xfs_da_node_entry *btree1;
1780 struct xfs_da_node_entry *btree2;
1781 struct xfs_da3_icnode_hdr node1hdr;
1782 struct xfs_da3_icnode_hdr node2hdr;
1783
1784 node1 = node1_bp->b_addr;
1785 node2 = node2_bp->b_addr;
1786 xfs_da3_node_hdr_from_disk(dp->i_mount, &node1hdr, node1);
1787 xfs_da3_node_hdr_from_disk(dp->i_mount, &node2hdr, node2);
1788 btree1 = node1hdr.btree;
1789 btree2 = node2hdr.btree;
1790
1791 if (node1hdr.count > 0 && node2hdr.count > 0 &&
1792 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
1793 (be32_to_cpu(btree2[node2hdr.count - 1].hashval) <
1794 be32_to_cpu(btree1[node1hdr.count - 1].hashval)))) {
1795 return 1;
1796 }
1797 return 0;
1798 }
1799
1800
1801
1802
1803 int
1804 xfs_da3_blk_link(
1805 struct xfs_da_state *state,
1806 struct xfs_da_state_blk *old_blk,
1807 struct xfs_da_state_blk *new_blk)
1808 {
1809 struct xfs_da_blkinfo *old_info;
1810 struct xfs_da_blkinfo *new_info;
1811 struct xfs_da_blkinfo *tmp_info;
1812 struct xfs_da_args *args;
1813 struct xfs_buf *bp;
1814 int before = 0;
1815 int error;
1816 struct xfs_inode *dp = state->args->dp;
1817
1818
1819
1820
1821 args = state->args;
1822 ASSERT(args != NULL);
1823 old_info = old_blk->bp->b_addr;
1824 new_info = new_blk->bp->b_addr;
1825 ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
1826 old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1827 old_blk->magic == XFS_ATTR_LEAF_MAGIC);
1828
1829 switch (old_blk->magic) {
1830 case XFS_ATTR_LEAF_MAGIC:
1831 before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
1832 break;
1833 case XFS_DIR2_LEAFN_MAGIC:
1834 before = xfs_dir2_leafn_order(dp, old_blk->bp, new_blk->bp);
1835 break;
1836 case XFS_DA_NODE_MAGIC:
1837 before = xfs_da3_node_order(dp, old_blk->bp, new_blk->bp);
1838 break;
1839 }
1840
1841
1842
1843
1844 if (before) {
1845
1846
1847
1848 trace_xfs_da_link_before(args);
1849 new_info->forw = cpu_to_be32(old_blk->blkno);
1850 new_info->back = old_info->back;
1851 if (old_info->back) {
1852 error = xfs_da3_node_read(args->trans, dp,
1853 be32_to_cpu(old_info->back),
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->forw) == old_blk->blkno);
1861 tmp_info->forw = cpu_to_be32(new_blk->blkno);
1862 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
1863 }
1864 old_info->back = cpu_to_be32(new_blk->blkno);
1865 } else {
1866
1867
1868
1869 trace_xfs_da_link_after(args);
1870 new_info->forw = old_info->forw;
1871 new_info->back = cpu_to_be32(old_blk->blkno);
1872 if (old_info->forw) {
1873 error = xfs_da3_node_read(args->trans, dp,
1874 be32_to_cpu(old_info->forw),
1875 &bp, args->whichfork);
1876 if (error)
1877 return error;
1878 ASSERT(bp != NULL);
1879 tmp_info = bp->b_addr;
1880 ASSERT(tmp_info->magic == old_info->magic);
1881 ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
1882 tmp_info->back = cpu_to_be32(new_blk->blkno);
1883 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
1884 }
1885 old_info->forw = cpu_to_be32(new_blk->blkno);
1886 }
1887
1888 xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
1889 xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
1890 return 0;
1891 }
1892
1893
1894
1895
1896 STATIC int
1897 xfs_da3_blk_unlink(
1898 struct xfs_da_state *state,
1899 struct xfs_da_state_blk *drop_blk,
1900 struct xfs_da_state_blk *save_blk)
1901 {
1902 struct xfs_da_blkinfo *drop_info;
1903 struct xfs_da_blkinfo *save_info;
1904 struct xfs_da_blkinfo *tmp_info;
1905 struct xfs_da_args *args;
1906 struct xfs_buf *bp;
1907 int error;
1908
1909
1910
1911
1912 args = state->args;
1913 ASSERT(args != NULL);
1914 save_info = save_blk->bp->b_addr;
1915 drop_info = drop_blk->bp->b_addr;
1916 ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
1917 save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1918 save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1919 ASSERT(save_blk->magic == drop_blk->magic);
1920 ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
1921 (be32_to_cpu(save_info->back) == drop_blk->blkno));
1922 ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
1923 (be32_to_cpu(drop_info->back) == save_blk->blkno));
1924
1925
1926
1927
1928 if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
1929 trace_xfs_da_unlink_back(args);
1930 save_info->back = drop_info->back;
1931 if (drop_info->back) {
1932 error = xfs_da3_node_read(args->trans, args->dp,
1933 be32_to_cpu(drop_info->back),
1934 &bp, args->whichfork);
1935 if (error)
1936 return error;
1937 ASSERT(bp != NULL);
1938 tmp_info = bp->b_addr;
1939 ASSERT(tmp_info->magic == save_info->magic);
1940 ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
1941 tmp_info->forw = cpu_to_be32(save_blk->blkno);
1942 xfs_trans_log_buf(args->trans, bp, 0,
1943 sizeof(*tmp_info) - 1);
1944 }
1945 } else {
1946 trace_xfs_da_unlink_forward(args);
1947 save_info->forw = drop_info->forw;
1948 if (drop_info->forw) {
1949 error = xfs_da3_node_read(args->trans, args->dp,
1950 be32_to_cpu(drop_info->forw),
1951 &bp, args->whichfork);
1952 if (error)
1953 return error;
1954 ASSERT(bp != NULL);
1955 tmp_info = bp->b_addr;
1956 ASSERT(tmp_info->magic == save_info->magic);
1957 ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
1958 tmp_info->back = cpu_to_be32(save_blk->blkno);
1959 xfs_trans_log_buf(args->trans, bp, 0,
1960 sizeof(*tmp_info) - 1);
1961 }
1962 }
1963
1964 xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
1965 return 0;
1966 }
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976 int
1977 xfs_da3_path_shift(
1978 struct xfs_da_state *state,
1979 struct xfs_da_state_path *path,
1980 int forward,
1981 int release,
1982 int *result)
1983 {
1984 struct xfs_da_state_blk *blk;
1985 struct xfs_da_blkinfo *info;
1986 struct xfs_da_args *args;
1987 struct xfs_da_node_entry *btree;
1988 struct xfs_da3_icnode_hdr nodehdr;
1989 struct xfs_buf *bp;
1990 xfs_dablk_t blkno = 0;
1991 int level;
1992 int error;
1993 struct xfs_inode *dp = state->args->dp;
1994
1995 trace_xfs_da_path_shift(state->args);
1996
1997
1998
1999
2000
2001
2002 args = state->args;
2003 ASSERT(args != NULL);
2004 ASSERT(path != NULL);
2005 ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
2006 level = (path->active-1) - 1;
2007 for (; level >= 0; level--) {
2008 blk = &path->blk[level];
2009 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr,
2010 blk->bp->b_addr);
2011
2012 if (forward && (blk->index < nodehdr.count - 1)) {
2013 blk->index++;
2014 blkno = be32_to_cpu(nodehdr.btree[blk->index].before);
2015 break;
2016 } else if (!forward && (blk->index > 0)) {
2017 blk->index--;
2018 blkno = be32_to_cpu(nodehdr.btree[blk->index].before);
2019 break;
2020 }
2021 }
2022 if (level < 0) {
2023 *result = -ENOENT;
2024 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
2025 return 0;
2026 }
2027
2028
2029
2030
2031
2032 for (blk++, level++; level < path->active; blk++, level++) {
2033
2034
2035
2036 error = xfs_da3_node_read(args->trans, dp, blkno, &bp,
2037 args->whichfork);
2038 if (error)
2039 return error;
2040
2041
2042
2043
2044
2045
2046
2047 if (release)
2048 xfs_trans_brelse(args->trans, blk->bp);
2049 blk->blkno = blkno;
2050 blk->bp = bp;
2051
2052 info = blk->bp->b_addr;
2053 ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
2054 info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
2055 info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
2056 info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
2057 info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
2058 info->magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
2059
2060
2061
2062
2063
2064
2065 switch (be16_to_cpu(info->magic)) {
2066 case XFS_DA_NODE_MAGIC:
2067 case XFS_DA3_NODE_MAGIC:
2068 blk->magic = XFS_DA_NODE_MAGIC;
2069 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr,
2070 bp->b_addr);
2071 btree = nodehdr.btree;
2072 blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
2073 if (forward)
2074 blk->index = 0;
2075 else
2076 blk->index = nodehdr.count - 1;
2077 blkno = be32_to_cpu(btree[blk->index].before);
2078 break;
2079 case XFS_ATTR_LEAF_MAGIC:
2080 case XFS_ATTR3_LEAF_MAGIC:
2081 blk->magic = XFS_ATTR_LEAF_MAGIC;
2082 ASSERT(level == path->active-1);
2083 blk->index = 0;
2084 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
2085 break;
2086 case XFS_DIR2_LEAFN_MAGIC:
2087 case XFS_DIR3_LEAFN_MAGIC:
2088 blk->magic = XFS_DIR2_LEAFN_MAGIC;
2089 ASSERT(level == path->active-1);
2090 blk->index = 0;
2091 blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
2092 blk->bp, NULL);
2093 break;
2094 default:
2095 ASSERT(0);
2096 break;
2097 }
2098 }
2099 *result = 0;
2100 return 0;
2101 }
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113 xfs_dahash_t
2114 xfs_da_hashname(const uint8_t *name, int namelen)
2115 {
2116 xfs_dahash_t hash;
2117
2118
2119
2120
2121 for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
2122 hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
2123 (name[3] << 0) ^ rol32(hash, 7 * 4);
2124
2125
2126
2127
2128 switch (namelen) {
2129 case 3:
2130 return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
2131 rol32(hash, 7 * 3);
2132 case 2:
2133 return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
2134 case 1:
2135 return (name[0] << 0) ^ rol32(hash, 7 * 1);
2136 default:
2137 return hash;
2138 }
2139 }
2140
2141 enum xfs_dacmp
2142 xfs_da_compname(
2143 struct xfs_da_args *args,
2144 const unsigned char *name,
2145 int len)
2146 {
2147 return (args->namelen == len && memcmp(args->name, name, len) == 0) ?
2148 XFS_CMP_EXACT : XFS_CMP_DIFFERENT;
2149 }
2150
2151 int
2152 xfs_da_grow_inode_int(
2153 struct xfs_da_args *args,
2154 xfs_fileoff_t *bno,
2155 int count)
2156 {
2157 struct xfs_trans *tp = args->trans;
2158 struct xfs_inode *dp = args->dp;
2159 int w = args->whichfork;
2160 xfs_rfsblock_t nblks = dp->i_nblocks;
2161 struct xfs_bmbt_irec map, *mapp;
2162 int nmap, error, got, i, mapi;
2163
2164
2165
2166
2167 error = xfs_bmap_first_unused(tp, dp, count, bno, w);
2168 if (error)
2169 return error;
2170
2171
2172
2173
2174 nmap = 1;
2175 error = xfs_bmapi_write(tp, dp, *bno, count,
2176 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
2177 args->total, &map, &nmap);
2178 if (error)
2179 return error;
2180
2181 ASSERT(nmap <= 1);
2182 if (nmap == 1) {
2183 mapp = ↦
2184 mapi = 1;
2185 } else if (nmap == 0 && count > 1) {
2186 xfs_fileoff_t b;
2187 int c;
2188
2189
2190
2191
2192
2193 mapp = kmem_alloc(sizeof(*mapp) * count, 0);
2194 for (b = *bno, mapi = 0; b < *bno + count; ) {
2195 nmap = min(XFS_BMAP_MAX_NMAP, count);
2196 c = (int)(*bno + count - b);
2197 error = xfs_bmapi_write(tp, dp, b, c,
2198 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
2199 args->total, &mapp[mapi], &nmap);
2200 if (error)
2201 goto out_free_map;
2202 if (nmap < 1)
2203 break;
2204 mapi += nmap;
2205 b = mapp[mapi - 1].br_startoff +
2206 mapp[mapi - 1].br_blockcount;
2207 }
2208 } else {
2209 mapi = 0;
2210 mapp = NULL;
2211 }
2212
2213
2214
2215
2216 for (i = 0, got = 0; i < mapi; i++)
2217 got += mapp[i].br_blockcount;
2218 if (got != count || mapp[0].br_startoff != *bno ||
2219 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
2220 *bno + count) {
2221 error = -ENOSPC;
2222 goto out_free_map;
2223 }
2224
2225
2226 args->total -= dp->i_nblocks - nblks;
2227
2228 out_free_map:
2229 if (mapp != &map)
2230 kmem_free(mapp);
2231 return error;
2232 }
2233
2234
2235
2236
2237
2238 int
2239 xfs_da_grow_inode(
2240 struct xfs_da_args *args,
2241 xfs_dablk_t *new_blkno)
2242 {
2243 xfs_fileoff_t bno;
2244 int error;
2245
2246 trace_xfs_da_grow_inode(args);
2247
2248 bno = args->geo->leafblk;
2249 error = xfs_da_grow_inode_int(args, &bno, args->geo->fsbcount);
2250 if (!error)
2251 *new_blkno = (xfs_dablk_t)bno;
2252 return error;
2253 }
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263 STATIC int
2264 xfs_da3_swap_lastblock(
2265 struct xfs_da_args *args,
2266 xfs_dablk_t *dead_blknop,
2267 struct xfs_buf **dead_bufp)
2268 {
2269 struct xfs_da_blkinfo *dead_info;
2270 struct xfs_da_blkinfo *sib_info;
2271 struct xfs_da_intnode *par_node;
2272 struct xfs_da_intnode *dead_node;
2273 struct xfs_dir2_leaf *dead_leaf2;
2274 struct xfs_da_node_entry *btree;
2275 struct xfs_da3_icnode_hdr par_hdr;
2276 struct xfs_inode *dp;
2277 struct xfs_trans *tp;
2278 struct xfs_mount *mp;
2279 struct xfs_buf *dead_buf;
2280 struct xfs_buf *last_buf;
2281 struct xfs_buf *sib_buf;
2282 struct xfs_buf *par_buf;
2283 xfs_dahash_t dead_hash;
2284 xfs_fileoff_t lastoff;
2285 xfs_dablk_t dead_blkno;
2286 xfs_dablk_t last_blkno;
2287 xfs_dablk_t sib_blkno;
2288 xfs_dablk_t par_blkno;
2289 int error;
2290 int w;
2291 int entno;
2292 int level;
2293 int dead_level;
2294
2295 trace_xfs_da_swap_lastblock(args);
2296
2297 dead_buf = *dead_bufp;
2298 dead_blkno = *dead_blknop;
2299 tp = args->trans;
2300 dp = args->dp;
2301 w = args->whichfork;
2302 ASSERT(w == XFS_DATA_FORK);
2303 mp = dp->i_mount;
2304 lastoff = args->geo->freeblk;
2305 error = xfs_bmap_last_before(tp, dp, &lastoff, w);
2306 if (error)
2307 return error;
2308 if (XFS_IS_CORRUPT(mp, lastoff == 0))
2309 return -EFSCORRUPTED;
2310
2311
2312
2313 last_blkno = (xfs_dablk_t)lastoff - args->geo->fsbcount;
2314 error = xfs_da3_node_read(tp, dp, last_blkno, &last_buf, w);
2315 if (error)
2316 return error;
2317
2318
2319
2320 memcpy(dead_buf->b_addr, last_buf->b_addr, args->geo->blksize);
2321 xfs_trans_log_buf(tp, dead_buf, 0, args->geo->blksize - 1);
2322 dead_info = dead_buf->b_addr;
2323
2324
2325
2326 if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
2327 dead_info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
2328 struct xfs_dir3_icleaf_hdr leafhdr;
2329 struct xfs_dir2_leaf_entry *ents;
2330
2331 dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
2332 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr,
2333 dead_leaf2);
2334 ents = leafhdr.ents;
2335 dead_level = 0;
2336 dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval);
2337 } else {
2338 struct xfs_da3_icnode_hdr deadhdr;
2339
2340 dead_node = (xfs_da_intnode_t *)dead_info;
2341 xfs_da3_node_hdr_from_disk(dp->i_mount, &deadhdr, dead_node);
2342 btree = deadhdr.btree;
2343 dead_level = deadhdr.level;
2344 dead_hash = be32_to_cpu(btree[deadhdr.count - 1].hashval);
2345 }
2346 sib_buf = par_buf = NULL;
2347
2348
2349
2350 if ((sib_blkno = be32_to_cpu(dead_info->back))) {
2351 error = xfs_da3_node_read(tp, dp, sib_blkno, &sib_buf, w);
2352 if (error)
2353 goto done;
2354 sib_info = sib_buf->b_addr;
2355 if (XFS_IS_CORRUPT(mp,
2356 be32_to_cpu(sib_info->forw) != last_blkno ||
2357 sib_info->magic != dead_info->magic)) {
2358 error = -EFSCORRUPTED;
2359 goto done;
2360 }
2361 sib_info->forw = cpu_to_be32(dead_blkno);
2362 xfs_trans_log_buf(tp, sib_buf,
2363 XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
2364 sizeof(sib_info->forw)));
2365 sib_buf = NULL;
2366 }
2367
2368
2369
2370 if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
2371 error = xfs_da3_node_read(tp, dp, sib_blkno, &sib_buf, w);
2372 if (error)
2373 goto done;
2374 sib_info = sib_buf->b_addr;
2375 if (XFS_IS_CORRUPT(mp,
2376 be32_to_cpu(sib_info->back) != last_blkno ||
2377 sib_info->magic != dead_info->magic)) {
2378 error = -EFSCORRUPTED;
2379 goto done;
2380 }
2381 sib_info->back = cpu_to_be32(dead_blkno);
2382 xfs_trans_log_buf(tp, sib_buf,
2383 XFS_DA_LOGRANGE(sib_info, &sib_info->back,
2384 sizeof(sib_info->back)));
2385 sib_buf = NULL;
2386 }
2387 par_blkno = args->geo->leafblk;
2388 level = -1;
2389
2390
2391
2392 for (;;) {
2393 error = xfs_da3_node_read(tp, dp, par_blkno, &par_buf, w);
2394 if (error)
2395 goto done;
2396 par_node = par_buf->b_addr;
2397 xfs_da3_node_hdr_from_disk(dp->i_mount, &par_hdr, par_node);
2398 if (XFS_IS_CORRUPT(mp,
2399 level >= 0 && level != par_hdr.level + 1)) {
2400 error = -EFSCORRUPTED;
2401 goto done;
2402 }
2403 level = par_hdr.level;
2404 btree = par_hdr.btree;
2405 for (entno = 0;
2406 entno < par_hdr.count &&
2407 be32_to_cpu(btree[entno].hashval) < dead_hash;
2408 entno++)
2409 continue;
2410 if (XFS_IS_CORRUPT(mp, entno == par_hdr.count)) {
2411 error = -EFSCORRUPTED;
2412 goto done;
2413 }
2414 par_blkno = be32_to_cpu(btree[entno].before);
2415 if (level == dead_level + 1)
2416 break;
2417 xfs_trans_brelse(tp, par_buf);
2418 par_buf = NULL;
2419 }
2420
2421
2422
2423
2424 for (;;) {
2425 for (;
2426 entno < par_hdr.count &&
2427 be32_to_cpu(btree[entno].before) != last_blkno;
2428 entno++)
2429 continue;
2430 if (entno < par_hdr.count)
2431 break;
2432 par_blkno = par_hdr.forw;
2433 xfs_trans_brelse(tp, par_buf);
2434 par_buf = NULL;
2435 if (XFS_IS_CORRUPT(mp, par_blkno == 0)) {
2436 error = -EFSCORRUPTED;
2437 goto done;
2438 }
2439 error = xfs_da3_node_read(tp, dp, par_blkno, &par_buf, w);
2440 if (error)
2441 goto done;
2442 par_node = par_buf->b_addr;
2443 xfs_da3_node_hdr_from_disk(dp->i_mount, &par_hdr, par_node);
2444 if (XFS_IS_CORRUPT(mp, par_hdr.level != level)) {
2445 error = -EFSCORRUPTED;
2446 goto done;
2447 }
2448 btree = par_hdr.btree;
2449 entno = 0;
2450 }
2451
2452
2453
2454 btree[entno].before = cpu_to_be32(dead_blkno);
2455 xfs_trans_log_buf(tp, par_buf,
2456 XFS_DA_LOGRANGE(par_node, &btree[entno].before,
2457 sizeof(btree[entno].before)));
2458 *dead_blknop = last_blkno;
2459 *dead_bufp = last_buf;
2460 return 0;
2461 done:
2462 if (par_buf)
2463 xfs_trans_brelse(tp, par_buf);
2464 if (sib_buf)
2465 xfs_trans_brelse(tp, sib_buf);
2466 xfs_trans_brelse(tp, last_buf);
2467 return error;
2468 }
2469
2470
2471
2472
2473 int
2474 xfs_da_shrink_inode(
2475 struct xfs_da_args *args,
2476 xfs_dablk_t dead_blkno,
2477 struct xfs_buf *dead_buf)
2478 {
2479 struct xfs_inode *dp;
2480 int done, error, w, count;
2481 struct xfs_trans *tp;
2482
2483 trace_xfs_da_shrink_inode(args);
2484
2485 dp = args->dp;
2486 w = args->whichfork;
2487 tp = args->trans;
2488 count = args->geo->fsbcount;
2489 for (;;) {
2490
2491
2492
2493
2494 error = xfs_bunmapi(tp, dp, dead_blkno, count,
2495 xfs_bmapi_aflag(w), 0, &done);
2496 if (error == -ENOSPC) {
2497 if (w != XFS_DATA_FORK)
2498 break;
2499 error = xfs_da3_swap_lastblock(args, &dead_blkno,
2500 &dead_buf);
2501 if (error)
2502 break;
2503 } else {
2504 break;
2505 }
2506 }
2507 xfs_trans_binval(tp, dead_buf);
2508 return error;
2509 }
2510
2511 static int
2512 xfs_dabuf_map(
2513 struct xfs_inode *dp,
2514 xfs_dablk_t bno,
2515 unsigned int flags,
2516 int whichfork,
2517 struct xfs_buf_map **mapp,
2518 int *nmaps)
2519 {
2520 struct xfs_mount *mp = dp->i_mount;
2521 int nfsb = xfs_dabuf_nfsb(mp, whichfork);
2522 struct xfs_bmbt_irec irec, *irecs = &irec;
2523 struct xfs_buf_map *map = *mapp;
2524 xfs_fileoff_t off = bno;
2525 int error = 0, nirecs, i;
2526
2527 if (nfsb > 1)
2528 irecs = kmem_zalloc(sizeof(irec) * nfsb, KM_NOFS);
2529
2530 nirecs = nfsb;
2531 error = xfs_bmapi_read(dp, bno, nfsb, irecs, &nirecs,
2532 xfs_bmapi_aflag(whichfork));
2533 if (error)
2534 goto out_free_irecs;
2535
2536
2537
2538
2539
2540 if (nirecs > 1) {
2541 map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map), KM_NOFS);
2542 if (!map) {
2543 error = -ENOMEM;
2544 goto out_free_irecs;
2545 }
2546 *mapp = map;
2547 }
2548
2549 for (i = 0; i < nirecs; i++) {
2550 if (irecs[i].br_startblock == HOLESTARTBLOCK ||
2551 irecs[i].br_startblock == DELAYSTARTBLOCK)
2552 goto invalid_mapping;
2553 if (off != irecs[i].br_startoff)
2554 goto invalid_mapping;
2555
2556 map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock);
2557 map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount);
2558 off += irecs[i].br_blockcount;
2559 }
2560
2561 if (off != bno + nfsb)
2562 goto invalid_mapping;
2563
2564 *nmaps = nirecs;
2565 out_free_irecs:
2566 if (irecs != &irec)
2567 kmem_free(irecs);
2568 return error;
2569
2570 invalid_mapping:
2571
2572 if (XFS_IS_CORRUPT(mp, !(flags & XFS_DABUF_MAP_HOLE_OK))) {
2573 error = -EFSCORRUPTED;
2574 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
2575 xfs_alert(mp, "%s: bno %u inode %llu",
2576 __func__, bno, dp->i_ino);
2577
2578 for (i = 0; i < nirecs; i++) {
2579 xfs_alert(mp,
2580 "[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
2581 i, irecs[i].br_startoff,
2582 irecs[i].br_startblock,
2583 irecs[i].br_blockcount,
2584 irecs[i].br_state);
2585 }
2586 }
2587 } else {
2588 *nmaps = 0;
2589 }
2590 goto out_free_irecs;
2591 }
2592
2593
2594
2595
2596 int
2597 xfs_da_get_buf(
2598 struct xfs_trans *tp,
2599 struct xfs_inode *dp,
2600 xfs_dablk_t bno,
2601 struct xfs_buf **bpp,
2602 int whichfork)
2603 {
2604 struct xfs_mount *mp = dp->i_mount;
2605 struct xfs_buf *bp;
2606 struct xfs_buf_map map, *mapp = ↦
2607 int nmap = 1;
2608 int error;
2609
2610 *bpp = NULL;
2611 error = xfs_dabuf_map(dp, bno, 0, whichfork, &mapp, &nmap);
2612 if (error || nmap == 0)
2613 goto out_free;
2614
2615 error = xfs_trans_get_buf_map(tp, mp->m_ddev_targp, mapp, nmap, 0, &bp);
2616 if (error)
2617 goto out_free;
2618
2619 *bpp = bp;
2620
2621 out_free:
2622 if (mapp != &map)
2623 kmem_free(mapp);
2624
2625 return error;
2626 }
2627
2628
2629
2630
2631 int
2632 xfs_da_read_buf(
2633 struct xfs_trans *tp,
2634 struct xfs_inode *dp,
2635 xfs_dablk_t bno,
2636 unsigned int flags,
2637 struct xfs_buf **bpp,
2638 int whichfork,
2639 const struct xfs_buf_ops *ops)
2640 {
2641 struct xfs_mount *mp = dp->i_mount;
2642 struct xfs_buf *bp;
2643 struct xfs_buf_map map, *mapp = ↦
2644 int nmap = 1;
2645 int error;
2646
2647 *bpp = NULL;
2648 error = xfs_dabuf_map(dp, bno, flags, whichfork, &mapp, &nmap);
2649 if (error || !nmap)
2650 goto out_free;
2651
2652 error = xfs_trans_read_buf_map(mp, tp, mp->m_ddev_targp, mapp, nmap, 0,
2653 &bp, ops);
2654 if (error)
2655 goto out_free;
2656
2657 if (whichfork == XFS_ATTR_FORK)
2658 xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
2659 else
2660 xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
2661 *bpp = bp;
2662 out_free:
2663 if (mapp != &map)
2664 kmem_free(mapp);
2665
2666 return error;
2667 }
2668
2669
2670
2671
2672 int
2673 xfs_da_reada_buf(
2674 struct xfs_inode *dp,
2675 xfs_dablk_t bno,
2676 unsigned int flags,
2677 int whichfork,
2678 const struct xfs_buf_ops *ops)
2679 {
2680 struct xfs_buf_map map;
2681 struct xfs_buf_map *mapp;
2682 int nmap;
2683 int error;
2684
2685 mapp = ↦
2686 nmap = 1;
2687 error = xfs_dabuf_map(dp, bno, flags, whichfork, &mapp, &nmap);
2688 if (error || !nmap)
2689 goto out_free;
2690
2691 xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, ops);
2692
2693 out_free:
2694 if (mapp != &map)
2695 kmem_free(mapp);
2696
2697 return error;
2698 }