LCOV - code coverage report
Current view: top level - fs/xfs/libxfs - xfs_dir2_leaf.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-djwa @ Mon Jul 31 20:08:17 PDT 2023 Lines: 731 816 89.6 %
Date: 2023-07-31 20:08:17 Functions: 29 30 96.7 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
       4             :  * Copyright (c) 2013 Red Hat, Inc.
       5             :  * All Rights Reserved.
       6             :  */
       7             : #include "xfs.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_mount.h"
      14             : #include "xfs_inode.h"
      15             : #include "xfs_bmap.h"
      16             : #include "xfs_dir2.h"
      17             : #include "xfs_dir2_priv.h"
      18             : #include "xfs_error.h"
      19             : #include "xfs_trace.h"
      20             : #include "xfs_trans.h"
      21             : #include "xfs_buf_item.h"
      22             : 
      23             : /*
      24             :  * Local function declarations.
      25             :  */
      26             : static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
      27             :                                     int *indexp, struct xfs_buf **dbpp,
      28             :                                     struct xfs_dir3_icleaf_hdr *leafhdr);
      29             : static void xfs_dir3_leaf_log_bests(struct xfs_da_args *args,
      30             :                                     struct xfs_buf *bp, int first, int last);
      31             : static void xfs_dir3_leaf_log_tail(struct xfs_da_args *args,
      32             :                                    struct xfs_buf *bp);
      33             : 
      34             : void
      35  1108659061 : xfs_dir2_leaf_hdr_from_disk(
      36             :         struct xfs_mount                *mp,
      37             :         struct xfs_dir3_icleaf_hdr      *to,
      38             :         struct xfs_dir2_leaf            *from)
      39             : {
      40  1108659061 :         if (xfs_has_crc(mp)) {
      41  1108659061 :                 struct xfs_dir3_leaf *from3 = (struct xfs_dir3_leaf *)from;
      42             : 
      43  1108659061 :                 to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
      44  1108659061 :                 to->back = be32_to_cpu(from3->hdr.info.hdr.back);
      45  1108659061 :                 to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
      46  1108659061 :                 to->count = be16_to_cpu(from3->hdr.count);
      47  1108659061 :                 to->stale = be16_to_cpu(from3->hdr.stale);
      48  1108659061 :                 to->ents = from3->__ents;
      49             : 
      50  1108659061 :                 ASSERT(to->magic == XFS_DIR3_LEAF1_MAGIC ||
      51             :                        to->magic == XFS_DIR3_LEAFN_MAGIC);
      52             :         } else {
      53           0 :                 to->forw = be32_to_cpu(from->hdr.info.forw);
      54           0 :                 to->back = be32_to_cpu(from->hdr.info.back);
      55           0 :                 to->magic = be16_to_cpu(from->hdr.info.magic);
      56           0 :                 to->count = be16_to_cpu(from->hdr.count);
      57           0 :                 to->stale = be16_to_cpu(from->hdr.stale);
      58           0 :                 to->ents = from->__ents;
      59             : 
      60           0 :                 ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
      61             :                        to->magic == XFS_DIR2_LEAFN_MAGIC);
      62             :         }
      63  1108659061 : }
      64             : 
      65             : void
      66    65562260 : xfs_dir2_leaf_hdr_to_disk(
      67             :         struct xfs_mount                *mp,
      68             :         struct xfs_dir2_leaf            *to,
      69             :         struct xfs_dir3_icleaf_hdr      *from)
      70             : {
      71    65562260 :         if (xfs_has_crc(mp)) {
      72    65562260 :                 struct xfs_dir3_leaf *to3 = (struct xfs_dir3_leaf *)to;
      73             : 
      74    65562260 :                 ASSERT(from->magic == XFS_DIR3_LEAF1_MAGIC ||
      75             :                        from->magic == XFS_DIR3_LEAFN_MAGIC);
      76             : 
      77    65562260 :                 to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
      78    65562260 :                 to3->hdr.info.hdr.back = cpu_to_be32(from->back);
      79    65562260 :                 to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
      80    65562260 :                 to3->hdr.count = cpu_to_be16(from->count);
      81   131124520 :                 to3->hdr.stale = cpu_to_be16(from->stale);
      82             :         } else {
      83           0 :                 ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
      84             :                        from->magic == XFS_DIR2_LEAFN_MAGIC);
      85             : 
      86           0 :                 to->hdr.info.forw = cpu_to_be32(from->forw);
      87           0 :                 to->hdr.info.back = cpu_to_be32(from->back);
      88           0 :                 to->hdr.info.magic = cpu_to_be16(from->magic);
      89           0 :                 to->hdr.count = cpu_to_be16(from->count);
      90           0 :                 to->hdr.stale = cpu_to_be16(from->stale);
      91             :         }
      92    65562260 : }
      93             : 
      94             : /*
      95             :  * Check the internal consistency of a leaf1 block.
      96             :  * Pop an assert if something is wrong.
      97             :  */
      98             : #ifdef DEBUG
      99             : static xfs_failaddr_t
     100    31286724 : xfs_dir3_leaf1_check(
     101             :         struct xfs_inode        *dp,
     102             :         struct xfs_buf          *bp)
     103             : {
     104    31286724 :         struct xfs_dir2_leaf    *leaf = bp->b_addr;
     105    31286724 :         struct xfs_dir3_icleaf_hdr leafhdr;
     106             : 
     107    31286724 :         xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
     108             : 
     109    31283643 :         if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
     110    31283643 :                 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
     111    31283643 :                 if (be64_to_cpu(leaf3->info.blkno) != xfs_buf_daddr(bp))
     112           0 :                         return __this_address;
     113           0 :         } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
     114           0 :                 return __this_address;
     115             : 
     116    31283643 :         return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf, false);
     117             : }
     118             : 
     119             : static inline void
     120    31280513 : xfs_dir3_leaf_check(
     121             :         struct xfs_inode        *dp,
     122             :         struct xfs_buf          *bp)
     123             : {
     124    31280513 :         xfs_failaddr_t          fa;
     125             : 
     126    31280513 :         fa = xfs_dir3_leaf1_check(dp, bp);
     127    31289170 :         if (!fa)
     128             :                 return;
     129           0 :         xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
     130           0 :                         bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
     131             :                         fa);
     132           0 :         ASSERT(0);
     133             : }
     134             : #else
     135             : #define xfs_dir3_leaf_check(dp, bp)
     136             : #endif
     137             : 
     138             : xfs_failaddr_t
     139   268950664 : xfs_dir3_leaf_check_int(
     140             :         struct xfs_mount                *mp,
     141             :         struct xfs_dir3_icleaf_hdr      *hdr,
     142             :         struct xfs_dir2_leaf            *leaf,
     143             :         bool                            expensive_checking)
     144             : {
     145   268950664 :         struct xfs_da_geometry          *geo = mp->m_dir_geo;
     146   268950664 :         xfs_dir2_leaf_tail_t            *ltp;
     147   268950664 :         int                             stale;
     148   268950664 :         int                             i;
     149   268950664 :         bool                            isleaf1 = (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
     150             :                                                    hdr->magic == XFS_DIR3_LEAF1_MAGIC);
     151             : 
     152   268950664 :         ltp = xfs_dir2_leaf_tail_p(geo, leaf);
     153             : 
     154             :         /*
     155             :          * XXX (dgc): This value is not restrictive enough.
     156             :          * Should factor in the size of the bests table as well.
     157             :          * We can deduce a value for that from i_disk_size.
     158             :          */
     159   268950664 :         if (hdr->count > geo->leaf_max_ents)
     160           0 :                 return __this_address;
     161             : 
     162             :         /* Leaves and bests don't overlap in leaf format. */
     163   268950664 :         if (isleaf1 &&
     164    31403077 :             (char *)&hdr->ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
     165           0 :                 return __this_address;
     166             : 
     167   268950664 :         if (!expensive_checking)
     168             :                 return NULL;
     169             : 
     170             :         /* Check hash value order, count stale entries.  */
     171   637661340 :         for (i = stale = 0; i < hdr->count; i++) {
     172   635711526 :                 if (i + 1 < hdr->count) {
     173  1267629368 :                         if (be32_to_cpu(hdr->ents[i].hashval) >
     174   633814684 :                                         be32_to_cpu(hdr->ents[i + 1].hashval))
     175           0 :                                 return __this_address;
     176             :                 }
     177   635711526 :                 if (hdr->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
     178    79040758 :                         stale++;
     179   673835987 :                 if (isleaf1 && xfs_dir2_dataptr_to_db(geo,
     180    38074121 :                                 be32_to_cpu(hdr->ents[i].address)) >=
     181    38124461 :                                 be32_to_cpu(ltp->bestcount))
     182           0 :                         return __this_address;
     183             :         }
     184     1949814 :         if (hdr->stale != stale)
     185           0 :                 return __this_address;
     186             :         return NULL;
     187             : }
     188             : 
     189             : /*
     190             :  * We verify the magic numbers before decoding the leaf header so that on debug
     191             :  * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
     192             :  * to incorrect magic numbers.
     193             :  */
     194             : static xfs_failaddr_t
     195     1949814 : xfs_dir3_leaf_verify(
     196             :         struct xfs_buf                  *bp)
     197             : {
     198     1949814 :         struct xfs_mount                *mp = bp->b_mount;
     199     1949814 :         struct xfs_dir3_icleaf_hdr      leafhdr;
     200     1949814 :         xfs_failaddr_t                  fa;
     201             : 
     202     1949814 :         fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
     203     1949814 :         if (fa)
     204             :                 return fa;
     205             : 
     206     1949814 :         xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, bp->b_addr);
     207     1949814 :         return xfs_dir3_leaf_check_int(mp, &leafhdr, bp->b_addr, true);
     208             : }
     209             : 
     210             : static void
     211      114509 : xfs_dir3_leaf_read_verify(
     212             :         struct xfs_buf  *bp)
     213             : {
     214      114509 :         struct xfs_mount        *mp = bp->b_mount;
     215      114509 :         xfs_failaddr_t          fa;
     216             : 
     217      229018 :         if (xfs_has_crc(mp) &&
     218             :              !xfs_buf_verify_cksum(bp, XFS_DIR3_LEAF_CRC_OFF))
     219           6 :                 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
     220             :         else {
     221      114503 :                 fa = xfs_dir3_leaf_verify(bp);
     222      114503 :                 if (fa)
     223           0 :                         xfs_verifier_error(bp, -EFSCORRUPTED, fa);
     224             :         }
     225      114509 : }
     226             : 
     227             : static void
     228      599770 : xfs_dir3_leaf_write_verify(
     229             :         struct xfs_buf  *bp)
     230             : {
     231      599770 :         struct xfs_mount        *mp = bp->b_mount;
     232      599770 :         struct xfs_buf_log_item *bip = bp->b_log_item;
     233      599770 :         struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
     234      599770 :         xfs_failaddr_t          fa;
     235             : 
     236      599770 :         fa = xfs_dir3_leaf_verify(bp);
     237      599770 :         if (fa) {
     238           0 :                 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
     239           0 :                 return;
     240             :         }
     241             : 
     242      599770 :         if (!xfs_has_crc(mp))
     243             :                 return;
     244             : 
     245      599770 :         if (bip)
     246      599770 :                 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
     247             : 
     248      599770 :         xfs_buf_update_cksum(bp, XFS_DIR3_LEAF_CRC_OFF);
     249             : }
     250             : 
     251             : const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
     252             :         .name = "xfs_dir3_leaf1",
     253             :         .magic16 = { cpu_to_be16(XFS_DIR2_LEAF1_MAGIC),
     254             :                      cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) },
     255             :         .verify_read = xfs_dir3_leaf_read_verify,
     256             :         .verify_write = xfs_dir3_leaf_write_verify,
     257             :         .verify_struct = xfs_dir3_leaf_verify,
     258             : };
     259             : 
     260             : const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
     261             :         .name = "xfs_dir3_leafn",
     262             :         .magic16 = { cpu_to_be16(XFS_DIR2_LEAFN_MAGIC),
     263             :                      cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) },
     264             :         .verify_read = xfs_dir3_leaf_read_verify,
     265             :         .verify_write = xfs_dir3_leaf_write_verify,
     266             :         .verify_struct = xfs_dir3_leaf_verify,
     267             : };
     268             : 
     269             : int
     270    22777833 : xfs_dir3_leaf_read(
     271             :         struct xfs_trans        *tp,
     272             :         struct xfs_inode        *dp,
     273             :         xfs_dablk_t             fbno,
     274             :         struct xfs_buf          **bpp)
     275             : {
     276    22777833 :         int                     err;
     277             : 
     278    22777833 :         err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK,
     279             :                         &xfs_dir3_leaf1_buf_ops);
     280    22782519 :         if (!err && tp && *bpp)
     281    13438318 :                 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
     282    22782530 :         return err;
     283             : }
     284             : 
     285             : int
     286     1866049 : xfs_dir3_leafn_read(
     287             :         struct xfs_trans        *tp,
     288             :         struct xfs_inode        *dp,
     289             :         xfs_dablk_t             fbno,
     290             :         struct xfs_buf          **bpp)
     291             : {
     292     1866049 :         int                     err;
     293             : 
     294     1866049 :         err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK,
     295             :                         &xfs_dir3_leafn_buf_ops);
     296     1866055 :         if (!err && tp && *bpp)
     297     1866055 :                 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
     298     1866055 :         return err;
     299             : }
     300             : 
     301             : /*
     302             :  * Initialize a new leaf block, leaf1 or leafn magic accepted.
     303             :  */
     304             : static void
     305       35682 : xfs_dir3_leaf_init(
     306             :         struct xfs_mount        *mp,
     307             :         struct xfs_trans        *tp,
     308             :         struct xfs_buf          *bp,
     309             :         xfs_ino_t               owner,
     310             :         uint16_t                type)
     311             : {
     312       35682 :         struct xfs_dir2_leaf    *leaf = bp->b_addr;
     313             : 
     314       35682 :         ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
     315             : 
     316       35682 :         if (xfs_has_crc(mp)) {
     317       35682 :                 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
     318             : 
     319       35682 :                 memset(leaf3, 0, sizeof(*leaf3));
     320             : 
     321       35682 :                 leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
     322             :                                          ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
     323             :                                          : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
     324       35682 :                 leaf3->info.blkno = cpu_to_be64(xfs_buf_daddr(bp));
     325       35682 :                 leaf3->info.owner = cpu_to_be64(owner);
     326       35682 :                 uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_meta_uuid);
     327             :         } else {
     328           0 :                 memset(leaf, 0, sizeof(*leaf));
     329           0 :                 leaf->hdr.info.magic = cpu_to_be16(type);
     330             :         }
     331             : 
     332             :         /*
     333             :          * If it's a leaf-format directory initialize the tail.
     334             :          * Caller is responsible for initialising the bests table.
     335             :          */
     336       35682 :         if (type == XFS_DIR2_LEAF1_MAGIC) {
     337       11283 :                 struct xfs_dir2_leaf_tail *ltp;
     338             : 
     339       11283 :                 ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf);
     340       11283 :                 ltp->bestcount = 0;
     341       11283 :                 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
     342       11283 :                 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
     343             :         } else {
     344       24399 :                 bp->b_ops = &xfs_dir3_leafn_buf_ops;
     345       24399 :                 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
     346             :         }
     347       35682 : }
     348             : 
     349             : int
     350       35682 : xfs_dir3_leaf_get_buf(
     351             :         xfs_da_args_t           *args,
     352             :         xfs_dir2_db_t           bno,
     353             :         struct xfs_buf          **bpp,
     354             :         uint16_t                magic)
     355             : {
     356       35682 :         struct xfs_inode        *dp = args->dp;
     357       35682 :         struct xfs_trans        *tp = args->trans;
     358       35682 :         struct xfs_mount        *mp = dp->i_mount;
     359       35682 :         struct xfs_buf          *bp;
     360       35682 :         int                     error;
     361             : 
     362       35682 :         ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
     363       35682 :         ASSERT(bno >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET) &&
     364             :                bno < xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
     365             : 
     366       35682 :         error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, bno),
     367             :                                &bp, XFS_DATA_FORK);
     368       35682 :         if (error)
     369             :                 return error;
     370             : 
     371       35682 :         xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
     372       35682 :         xfs_dir3_leaf_log_header(args, bp);
     373       35682 :         if (magic == XFS_DIR2_LEAF1_MAGIC)
     374       11283 :                 xfs_dir3_leaf_log_tail(args, bp);
     375       35682 :         *bpp = bp;
     376       35682 :         return 0;
     377             : }
     378             : 
     379             : /*
     380             :  * Convert a block form directory to a leaf form directory.
     381             :  */
     382             : int                                             /* error */
     383       11283 : xfs_dir2_block_to_leaf(
     384             :         xfs_da_args_t           *args,          /* operation arguments */
     385             :         struct xfs_buf          *dbp)           /* input block's buffer */
     386             : {
     387       11283 :         __be16                  *bestsp;        /* leaf's bestsp entries */
     388       11283 :         xfs_dablk_t             blkno;          /* leaf block's bno */
     389       11283 :         xfs_dir2_data_hdr_t     *hdr;           /* block header */
     390       11283 :         xfs_dir2_leaf_entry_t   *blp;           /* block's leaf entries */
     391       11283 :         xfs_dir2_block_tail_t   *btp;           /* block's tail */
     392       11283 :         xfs_inode_t             *dp;            /* incore directory inode */
     393       11283 :         int                     error;          /* error return code */
     394       11283 :         struct xfs_buf          *lbp;           /* leaf block's buffer */
     395       11283 :         xfs_dir2_db_t           ldb;            /* leaf block's bno */
     396       11283 :         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
     397       11283 :         xfs_dir2_leaf_tail_t    *ltp;           /* leaf's tail */
     398       11283 :         int                     needlog;        /* need to log block header */
     399       11283 :         int                     needscan;       /* need to rescan bestfree */
     400       11283 :         xfs_trans_t             *tp;            /* transaction pointer */
     401       11283 :         struct xfs_dir2_data_free *bf;
     402       11283 :         struct xfs_dir3_icleaf_hdr leafhdr;
     403             : 
     404       11283 :         trace_xfs_dir2_block_to_leaf(args);
     405             : 
     406       11283 :         dp = args->dp;
     407       11283 :         tp = args->trans;
     408             :         /*
     409             :          * Add the leaf block to the inode.
     410             :          * This interface will only put blocks in the leaf/node range.
     411             :          * Since that's empty now, we'll get the root (block 0 in range).
     412             :          */
     413       11283 :         if ((error = xfs_da_grow_inode(args, &blkno))) {
     414             :                 return error;
     415             :         }
     416       11283 :         ldb = xfs_dir2_da_to_db(args->geo, blkno);
     417       11283 :         ASSERT(ldb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET));
     418             :         /*
     419             :          * Initialize the leaf block, get a buffer for it.
     420             :          */
     421       11283 :         error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
     422       11283 :         if (error)
     423             :                 return error;
     424             : 
     425       11283 :         leaf = lbp->b_addr;
     426       11283 :         hdr = dbp->b_addr;
     427       11283 :         xfs_dir3_data_check(dp, dbp);
     428       11283 :         btp = xfs_dir2_block_tail_p(args->geo, hdr);
     429       11283 :         blp = xfs_dir2_block_leaf_p(btp);
     430       11283 :         bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
     431             : 
     432             :         /*
     433             :          * Set the counts in the leaf header.
     434             :          */
     435       11283 :         xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
     436       11283 :         leafhdr.count = be32_to_cpu(btp->count);
     437       11283 :         leafhdr.stale = be32_to_cpu(btp->stale);
     438       11283 :         xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
     439       11283 :         xfs_dir3_leaf_log_header(args, lbp);
     440             : 
     441             :         /*
     442             :          * Could compact these but I think we always do the conversion
     443             :          * after squeezing out stale entries.
     444             :          */
     445       22566 :         memcpy(leafhdr.ents, blp,
     446             :                 be32_to_cpu(btp->count) * sizeof(struct xfs_dir2_leaf_entry));
     447       11283 :         xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, 0, leafhdr.count - 1);
     448       11283 :         needscan = 0;
     449       11283 :         needlog = 1;
     450             :         /*
     451             :          * Make the space formerly occupied by the leaf entries and block
     452             :          * tail be free.
     453             :          */
     454       11283 :         xfs_dir2_data_make_free(args, dbp,
     455       11283 :                 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
     456       11283 :                 (xfs_dir2_data_aoff_t)((char *)hdr + args->geo->blksize -
     457             :                                        (char *)blp),
     458             :                 &needlog, &needscan);
     459             :         /*
     460             :          * Fix up the block header, make it a data block.
     461             :          */
     462       11283 :         dbp->b_ops = &xfs_dir3_data_buf_ops;
     463       11283 :         xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
     464       11283 :         if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
     465           0 :                 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
     466             :         else
     467       11283 :                 hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
     468             : 
     469       11283 :         if (needscan)
     470           3 :                 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
     471             :         /*
     472             :          * Set up leaf tail and bests table.
     473             :          */
     474       11283 :         ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
     475       11283 :         ltp->bestcount = cpu_to_be32(1);
     476       11283 :         bestsp = xfs_dir2_leaf_bests_p(ltp);
     477       11283 :         bestsp[0] =  bf[0].length;
     478             :         /*
     479             :          * Log the data header and leaf bests table.
     480             :          */
     481       11283 :         if (needlog)
     482       11283 :                 xfs_dir2_data_log_header(args, dbp);
     483       11283 :         xfs_dir3_leaf_check(dp, lbp);
     484       11283 :         xfs_dir3_data_check(dp, dbp);
     485       11283 :         xfs_dir3_leaf_log_bests(args, lbp, 0, 0);
     486       11283 :         return 0;
     487             : }
     488             : 
     489             : STATIC void
     490    24964035 : xfs_dir3_leaf_find_stale(
     491             :         struct xfs_dir3_icleaf_hdr *leafhdr,
     492             :         struct xfs_dir2_leaf_entry *ents,
     493             :         int                     index,
     494             :         int                     *lowstale,
     495             :         int                     *highstale)
     496             : {
     497             :         /*
     498             :          * Find the first stale entry before our index, if any.
     499             :          */
     500  2024370632 :         for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
     501  2023767884 :                 if (ents[*lowstale].address ==
     502             :                     cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
     503             :                         break;
     504             :         }
     505             : 
     506             :         /*
     507             :          * Find the first stale entry at or after our index, if any.
     508             :          * Stop if the result would require moving more entries than using
     509             :          * lowstale.
     510             :          */
     511  1005651857 :         for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
     512   995830735 :                 if (ents[*highstale].address ==
     513             :                     cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
     514             :                         break;
     515   992680513 :                 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
     516             :                         break;
     517             :         }
     518    24964035 : }
     519             : 
     520             : struct xfs_dir2_leaf_entry *
     521    33497862 : xfs_dir3_leaf_find_entry(
     522             :         struct xfs_dir3_icleaf_hdr *leafhdr,
     523             :         struct xfs_dir2_leaf_entry *ents,
     524             :         int                     index,          /* leaf table position */
     525             :         int                     compact,        /* need to compact leaves */
     526             :         int                     lowstale,       /* index of prev stale leaf */
     527             :         int                     highstale,      /* index of next stale leaf */
     528             :         int                     *lfloglow,      /* low leaf logging index */
     529             :         int                     *lfloghigh)     /* high leaf logging index */
     530             : {
     531    33497862 :         if (!leafhdr->stale) {
     532     8536044 :                 xfs_dir2_leaf_entry_t   *lep;   /* leaf entry table pointer */
     533             : 
     534             :                 /*
     535             :                  * Now we need to make room to insert the leaf entry.
     536             :                  *
     537             :                  * If there are no stale entries, just insert a hole at index.
     538             :                  */
     539     8536044 :                 lep = &ents[index];
     540     8536044 :                 if (index < leafhdr->count)
     541    16106214 :                         memmove(lep + 1, lep,
     542             :                                 (leafhdr->count - index) * sizeof(*lep));
     543             : 
     544             :                 /*
     545             :                  * Record low and high logging indices for the leaf.
     546             :                  */
     547     8536044 :                 *lfloglow = index;
     548     8536044 :                 *lfloghigh = leafhdr->count++;
     549     8536044 :                 return lep;
     550             :         }
     551             : 
     552             :         /*
     553             :          * There are stale entries.
     554             :          *
     555             :          * We will use one of them for the new entry.  It's probably not at
     556             :          * the right location, so we'll have to shift some up or down first.
     557             :          *
     558             :          * If we didn't compact before, we need to find the nearest stale
     559             :          * entries before and after our insertion point.
     560             :          */
     561    24961818 :         if (compact == 0)
     562    24959134 :                 xfs_dir3_leaf_find_stale(leafhdr, ents, index,
     563             :                                          &lowstale, &highstale);
     564             : 
     565             :         /*
     566             :          * If the low one is better, use it.
     567             :          */
     568    24963347 :         if (lowstale >= 0 &&
     569    24360836 :             (highstale == leafhdr->count ||
     570    14538633 :              index - lowstale - 1 < highstale - index)) {
     571    21983740 :                 ASSERT(index - lowstale - 1 >= 0);
     572    21983740 :                 ASSERT(ents[lowstale].address ==
     573             :                        cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
     574             : 
     575             :                 /*
     576             :                  * Copy entries up to cover the stale entry and make room
     577             :                  * for the new entry.
     578             :                  */
     579    21983740 :                 if (index - lowstale - 1 > 0) {
     580    42163338 :                         memmove(&ents[lowstale], &ents[lowstale + 1],
     581             :                                 (index - lowstale - 1) *
     582             :                                         sizeof(xfs_dir2_leaf_entry_t));
     583             :                 }
     584    21983740 :                 *lfloglow = min(lowstale, *lfloglow);
     585    21983740 :                 *lfloghigh = max(index - 1, *lfloghigh);
     586    21983740 :                 leafhdr->stale--;
     587    21983740 :                 return &ents[index - 1];
     588             :         }
     589             : 
     590             :         /*
     591             :          * The high one is better, so use that one.
     592             :          */
     593     2979607 :         ASSERT(highstale - index >= 0);
     594     2979607 :         ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
     595             : 
     596             :         /*
     597             :          * Copy entries down to cover the stale entry and make room for the
     598             :          * new entry.
     599             :          */
     600     2979607 :         if (highstale - index > 0) {
     601     4661868 :                 memmove(&ents[index + 1], &ents[index],
     602             :                         (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
     603             :         }
     604     2979607 :         *lfloglow = min(index, *lfloglow);
     605     2979607 :         *lfloghigh = max(highstale, *lfloghigh);
     606     2979607 :         leafhdr->stale--;
     607     2979607 :         return &ents[index];
     608             : }
     609             : 
     610             : /*
     611             :  * Add an entry to a leaf form directory.
     612             :  */
     613             : int                                             /* error */
     614     5283119 : xfs_dir2_leaf_addname(
     615             :         struct xfs_da_args      *args)          /* operation arguments */
     616             : {
     617     5283119 :         struct xfs_dir3_icleaf_hdr leafhdr;
     618     5283119 :         struct xfs_trans        *tp = args->trans;
     619     5283119 :         __be16                  *bestsp;        /* freespace table in leaf */
     620     5283119 :         __be16                  *tagp;          /* end of data entry */
     621     5283119 :         struct xfs_buf          *dbp;           /* data block buffer */
     622     5283119 :         struct xfs_buf          *lbp;           /* leaf's buffer */
     623     5283119 :         struct xfs_dir2_leaf    *leaf;          /* leaf structure */
     624     5283119 :         struct xfs_inode        *dp = args->dp;      /* incore directory inode */
     625     5283119 :         struct xfs_dir2_data_hdr *hdr;          /* data block header */
     626     5283119 :         struct xfs_dir2_data_entry *dep;        /* data block entry */
     627     5283119 :         struct xfs_dir2_leaf_entry *lep;        /* leaf entry table pointer */
     628     5283119 :         struct xfs_dir2_leaf_entry *ents;
     629     5283119 :         struct xfs_dir2_data_unused *dup;       /* data unused entry */
     630     5283119 :         struct xfs_dir2_leaf_tail *ltp;         /* leaf tail pointer */
     631     5283119 :         struct xfs_dir2_data_free *bf;          /* bestfree table */
     632     5283119 :         int                     compact;        /* need to compact leaves */
     633     5283119 :         int                     error;          /* error return value */
     634     5283119 :         int                     grown;          /* allocated new data block */
     635     5283119 :         int                     highstale = 0;  /* index of next stale leaf */
     636     5283119 :         int                     i;              /* temporary, index */
     637     5283119 :         int                     index;          /* leaf table position */
     638     5283119 :         int                     length;         /* length of new entry */
     639     5283119 :         int                     lfloglow;       /* low leaf logging index */
     640     5283119 :         int                     lfloghigh;      /* high leaf logging index */
     641     5283119 :         int                     lowstale = 0;   /* index of prev stale leaf */
     642     5283119 :         int                     needbytes;      /* leaf block bytes needed */
     643     5283119 :         int                     needlog;        /* need to log data header */
     644     5283119 :         int                     needscan;       /* need to rescan data free */
     645     5283119 :         xfs_dir2_db_t           use_block;      /* data block number */
     646             : 
     647     5283119 :         trace_xfs_dir2_leaf_addname(args);
     648             : 
     649     5283413 :         error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, &lbp);
     650     5283448 :         if (error)
     651             :                 return error;
     652             : 
     653             :         /*
     654             :          * Look up the entry by hash value and name.
     655             :          * We know it's not there, our caller has already done a lookup.
     656             :          * So the index is of the entry to insert in front of.
     657             :          * But if there are dup hash values the index is of the first of those.
     658             :          */
     659     5283432 :         index = xfs_dir2_leaf_search_hash(args, lbp);
     660     5283430 :         leaf = lbp->b_addr;
     661     5283430 :         ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
     662     5283430 :         xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
     663     5283371 :         ents = leafhdr.ents;
     664     5283371 :         bestsp = xfs_dir2_leaf_bests_p(ltp);
     665     5283371 :         length = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
     666             : 
     667             :         /*
     668             :          * See if there are any entries with the same hash value
     669             :          * and space in their block for the new entry.
     670             :          * This is good because it puts multiple same-hash value entries
     671             :          * in a data block, improving the lookup of those entries.
     672             :          */
     673     5283371 :         for (use_block = -1, lep = &ents[index];
     674    16118989 :              index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
     675     3107247 :              index++, lep++) {
     676     3119929 :                 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
     677       31261 :                         continue;
     678     3088668 :                 i = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
     679     6177302 :                 ASSERT(i < be32_to_cpu(ltp->bestcount));
     680     3088651 :                 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
     681     3088651 :                 if (be16_to_cpu(bestsp[i]) >= length) {
     682       12648 :                         use_block = i;
     683       12648 :                         break;
     684             :                 }
     685             :         }
     686             :         /*
     687             :          * Didn't find a block yet, linear search all the data blocks.
     688             :          */
     689     5283337 :         if (use_block == -1) {
     690    19757262 :                 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
     691             :                         /*
     692             :                          * Remember a block we see that's missing.
     693             :                          */
     694     9872675 :                         if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
     695       24259 :                             use_block == -1)
     696       24204 :                                 use_block = i;
     697     9848471 :                         else if (be16_to_cpu(bestsp[i]) >= length) {
     698     5264766 :                                 use_block = i;
     699     5264766 :                                 break;
     700             :                         }
     701             :                 }
     702             :         }
     703             :         /*
     704             :          * How many bytes do we need in the leaf block?
     705             :          */
     706     5283337 :         needbytes = 0;
     707     5283337 :         if (!leafhdr.stale)
     708     1109661 :                 needbytes += sizeof(xfs_dir2_leaf_entry_t);
     709     5283337 :         if (use_block == -1)
     710        5954 :                 needbytes += sizeof(xfs_dir2_data_off_t);
     711             : 
     712             :         /*
     713             :          * Now kill use_block if it refers to a missing block, so we
     714             :          * can use it as an indication of allocation needed.
     715             :          */
     716     5283337 :         if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
     717          57 :                 use_block = -1;
     718             :         /*
     719             :          * If we don't have enough free bytes but we can make enough
     720             :          * by compacting out stale entries, we'll do that.
     721             :          */
     722     5283337 :         if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
     723             :             leafhdr.stale > 1)
     724             :                 compact = 1;
     725             : 
     726             :         /*
     727             :          * Otherwise if we don't have enough free bytes we need to
     728             :          * convert to node form.
     729             :          */
     730     5283337 :         else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
     731             :                 /*
     732             :                  * Just checking or no space reservation, give up.
     733             :                  */
     734        1725 :                 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
     735        1725 :                                                         args->total == 0) {
     736           0 :                         xfs_trans_brelse(tp, lbp);
     737           0 :                         return -ENOSPC;
     738             :                 }
     739             :                 /*
     740             :                  * Convert to node form.
     741             :                  */
     742        1725 :                 error = xfs_dir2_leaf_to_node(args, lbp);
     743        1725 :                 if (error)
     744             :                         return error;
     745             :                 /*
     746             :                  * Then add the new entry.
     747             :                  */
     748        1723 :                 return xfs_dir2_node_addname(args);
     749             :         }
     750             :         /*
     751             :          * Otherwise it will fit without compaction.
     752             :          */
     753             :         else
     754             :                 compact = 0;
     755             :         /*
     756             :          * If just checking, then it will fit unless we needed to allocate
     757             :          * a new data block.
     758             :          */
     759     5281612 :         if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
     760         671 :                 xfs_trans_brelse(tp, lbp);
     761         671 :                 return use_block == -1 ? -ENOSPC : 0;
     762             :         }
     763             :         /*
     764             :          * If no allocations are allowed, return now before we've
     765             :          * changed anything.
     766             :          */
     767     5280941 :         if (args->total == 0 && use_block == -1) {
     768           0 :                 xfs_trans_brelse(tp, lbp);
     769           0 :                 return -ENOSPC;
     770             :         }
     771             :         /*
     772             :          * Need to compact the leaf entries, removing stale ones.
     773             :          * Leave one stale entry behind - the one closest to our
     774             :          * insertion index - and we'll shift that one to our insertion
     775             :          * point later.
     776             :          */
     777     5280941 :         if (compact) {
     778           0 :                 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
     779             :                         &highstale, &lfloglow, &lfloghigh);
     780             :         }
     781             :         /*
     782             :          * There are stale entries, so we'll need log-low and log-high
     783             :          * impossibly bad values later.
     784             :          */
     785     5280941 :         else if (leafhdr.stale) {
     786     4173592 :                 lfloglow = leafhdr.count;
     787     4173592 :                 lfloghigh = -1;
     788             :         }
     789             :         /*
     790             :          * If there was no data block space found, we need to allocate
     791             :          * a new one.
     792             :          */
     793     5280941 :         if (use_block == -1) {
     794             :                 /*
     795             :                  * Add the new data block.
     796             :                  */
     797        5635 :                 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
     798             :                                 &use_block))) {
     799           0 :                         xfs_trans_brelse(tp, lbp);
     800           0 :                         return error;
     801             :                 }
     802             :                 /*
     803             :                  * Initialize the block.
     804             :                  */
     805        5635 :                 if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
     806           0 :                         xfs_trans_brelse(tp, lbp);
     807           0 :                         return error;
     808             :                 }
     809             :                 /*
     810             :                  * If we're adding a new data block on the end we need to
     811             :                  * extend the bests table.  Copy it up one entry.
     812             :                  */
     813       11270 :                 if (use_block >= be32_to_cpu(ltp->bestcount)) {
     814        5578 :                         bestsp--;
     815       11156 :                         memmove(&bestsp[0], &bestsp[1],
     816             :                                 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
     817        5578 :                         be32_add_cpu(&ltp->bestcount, 1);
     818        5578 :                         xfs_dir3_leaf_log_tail(args, lbp);
     819        5578 :                         xfs_dir3_leaf_log_bests(args, lbp, 0,
     820        5578 :                                                 be32_to_cpu(ltp->bestcount) - 1);
     821             :                 }
     822             :                 /*
     823             :                  * If we're filling in a previously empty block just log it.
     824             :                  */
     825             :                 else
     826          57 :                         xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
     827        5635 :                 hdr = dbp->b_addr;
     828        5635 :                 bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
     829        5635 :                 bestsp[use_block] = bf[0].length;
     830        5635 :                 grown = 1;
     831             :         } else {
     832             :                 /*
     833             :                  * Already had space in some data block.
     834             :                  * Just read that one in.
     835             :                  */
     836     5275306 :                 error = xfs_dir3_data_read(tp, dp,
     837             :                                    xfs_dir2_db_to_da(args->geo, use_block),
     838             :                                    0, &dbp);
     839     5275408 :                 if (error) {
     840          41 :                         xfs_trans_brelse(tp, lbp);
     841          41 :                         return error;
     842             :                 }
     843     5275367 :                 hdr = dbp->b_addr;
     844     5275367 :                 bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
     845     5275367 :                 grown = 0;
     846             :         }
     847             :         /*
     848             :          * Point to the biggest freespace in our data block.
     849             :          */
     850    10562010 :         dup = (xfs_dir2_data_unused_t *)
     851     5281005 :               ((char *)hdr + be16_to_cpu(bf[0].offset));
     852     5281005 :         needscan = needlog = 0;
     853             :         /*
     854             :          * Mark the initial part of our freespace in use for the new entry.
     855             :          */
     856     5281005 :         error = xfs_dir2_data_use_free(args, dbp, dup,
     857             :                         (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
     858             :                         length, &needlog, &needscan);
     859     5280907 :         if (error) {
     860           0 :                 xfs_trans_brelse(tp, lbp);
     861           0 :                 return error;
     862             :         }
     863             :         /*
     864             :          * Initialize our new entry (at last).
     865             :          */
     866     5280907 :         dep = (xfs_dir2_data_entry_t *)dup;
     867     5280907 :         dep->inumber = cpu_to_be64(args->inumber);
     868     5280907 :         dep->namelen = args->namelen;
     869    10561814 :         memcpy(dep->name, args->name, dep->namelen);
     870     5280907 :         xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
     871     5280919 :         tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
     872     5280965 :         *tagp = cpu_to_be16((char *)dep - (char *)hdr);
     873             :         /*
     874             :          * Need to scan fix up the bestfree table.
     875             :          */
     876     5280965 :         if (needscan)
     877     2629255 :                 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
     878             :         /*
     879             :          * Need to log the data block's header.
     880             :          */
     881     5280958 :         if (needlog)
     882     5280940 :                 xfs_dir2_data_log_header(args, dbp);
     883     5280920 :         xfs_dir2_data_log_entry(args, dbp, dep);
     884             :         /*
     885             :          * If the bests table needs to be changed, do it.
     886             :          * Log the change unless we've already done that.
     887             :          */
     888     5280944 :         if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
     889     3023574 :                 bestsp[use_block] = bf[0].length;
     890     3023574 :                 if (!grown)
     891     3017933 :                         xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
     892             :         }
     893             : 
     894     5280931 :         lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
     895             :                                        highstale, &lfloglow, &lfloghigh);
     896             : 
     897             :         /*
     898             :          * Fill in the new leaf entry.
     899             :          */
     900     5280851 :         lep->hashval = cpu_to_be32(args->hashval);
     901     5280851 :         lep->address = cpu_to_be32(
     902             :                                 xfs_dir2_db_off_to_dataptr(args->geo, use_block,
     903             :                                 be16_to_cpu(*tagp)));
     904             :         /*
     905             :          * Log the leaf fields and give up the buffers.
     906             :          */
     907     5280952 :         xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
     908     5280955 :         xfs_dir3_leaf_log_header(args, lbp);
     909     5280942 :         xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, lfloglow, lfloghigh);
     910     5280787 :         xfs_dir3_leaf_check(dp, lbp);
     911     5280970 :         xfs_dir3_data_check(dp, dbp);
     912     5280970 :         return 0;
     913             : }
     914             : 
     915             : /*
     916             :  * Compact out any stale entries in the leaf.
     917             :  * Log the header and changed leaf entries, if any.
     918             :  */
     919             : void
     920       36058 : xfs_dir3_leaf_compact(
     921             :         xfs_da_args_t   *args,          /* operation arguments */
     922             :         struct xfs_dir3_icleaf_hdr *leafhdr,
     923             :         struct xfs_buf  *bp)            /* leaf buffer */
     924             : {
     925       36058 :         int             from;           /* source leaf index */
     926       36058 :         xfs_dir2_leaf_t *leaf;          /* leaf structure */
     927       36058 :         int             loglow;         /* first leaf entry to log */
     928       36058 :         int             to;             /* target leaf index */
     929       36058 :         struct xfs_inode *dp = args->dp;
     930             : 
     931       36058 :         leaf = bp->b_addr;
     932       36058 :         if (!leafhdr->stale)
     933             :                 return;
     934             : 
     935             :         /*
     936             :          * Compress out the stale entries in place.
     937             :          */
     938    12871832 :         for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
     939    12835774 :                 if (leafhdr->ents[from].address ==
     940             :                     cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
     941     5816081 :                         continue;
     942             :                 /*
     943             :                  * Only actually copy the entries that are different.
     944             :                  */
     945     7019693 :                 if (from > to) {
     946     6398359 :                         if (loglow == -1)
     947       35431 :                                 loglow = to;
     948     6398359 :                         leafhdr->ents[to] = leafhdr->ents[from];
     949             :                 }
     950     7019693 :                 to++;
     951             :         }
     952             :         /*
     953             :          * Update and log the header, log the leaf entries.
     954             :          */
     955       36058 :         ASSERT(leafhdr->stale == from - to);
     956       36058 :         leafhdr->count -= leafhdr->stale;
     957       36058 :         leafhdr->stale = 0;
     958             : 
     959       36058 :         xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, leafhdr);
     960       36058 :         xfs_dir3_leaf_log_header(args, bp);
     961       36058 :         if (loglow != -1)
     962       35431 :                 xfs_dir3_leaf_log_ents(args, leafhdr, bp, loglow, to - 1);
     963             : }
     964             : 
     965             : /*
     966             :  * Compact the leaf entries, removing stale ones.
     967             :  * Leave one stale entry behind - the one closest to our
     968             :  * insertion index - and the caller will shift that one to our insertion
     969             :  * point later.
     970             :  * Return new insertion index, where the remaining stale entry is,
     971             :  * and leaf logging indices.
     972             :  */
     973             : void
     974        3155 : xfs_dir3_leaf_compact_x1(
     975             :         struct xfs_dir3_icleaf_hdr *leafhdr,
     976             :         struct xfs_dir2_leaf_entry *ents,
     977             :         int             *indexp,        /* insertion index */
     978             :         int             *lowstalep,     /* out: stale entry before us */
     979             :         int             *highstalep,    /* out: stale entry after us */
     980             :         int             *lowlogp,       /* out: low log index */
     981             :         int             *highlogp)      /* out: high log index */
     982             : {
     983        3155 :         int             from;           /* source copy index */
     984        3155 :         int             highstale;      /* stale entry at/after index */
     985        3155 :         int             index;          /* insertion index */
     986        3155 :         int             keepstale;      /* source index of kept stale */
     987        3155 :         int             lowstale;       /* stale entry before index */
     988        3155 :         int             newindex=0;     /* new insertion index */
     989        3155 :         int             to;             /* destination copy index */
     990             : 
     991        3155 :         ASSERT(leafhdr->stale > 1);
     992        3155 :         index = *indexp;
     993             : 
     994        3155 :         xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
     995             : 
     996             :         /*
     997             :          * Pick the better of lowstale and highstale.
     998             :          */
     999        3155 :         if (lowstale >= 0 &&
    1000        3141 :             (highstale == leafhdr->count ||
    1001        1991 :              index - lowstale <= highstale - index))
    1002             :                 keepstale = lowstale;
    1003             :         else
    1004         239 :                 keepstale = highstale;
    1005             :         /*
    1006             :          * Copy the entries in place, removing all the stale entries
    1007             :          * except keepstale.
    1008             :          */
    1009     1593275 :         for (from = to = 0; from < leafhdr->count; from++) {
    1010             :                 /*
    1011             :                  * Notice the new value of index.
    1012             :                  */
    1013     1590120 :                 if (index == from)
    1014        3076 :                         newindex = to;
    1015     1590120 :                 if (from != keepstale &&
    1016     1586965 :                     ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
    1017        6601 :                         if (from == to)
    1018        3155 :                                 *lowlogp = to;
    1019        6601 :                         continue;
    1020             :                 }
    1021             :                 /*
    1022             :                  * Record the new keepstale value for the insertion.
    1023             :                  */
    1024     1583519 :                 if (from == keepstale)
    1025        3155 :                         lowstale = highstale = to;
    1026             :                 /*
    1027             :                  * Copy only the entries that have moved.
    1028             :                  */
    1029     1583519 :                 if (from > to)
    1030     1240975 :                         ents[to] = ents[from];
    1031     1583519 :                 to++;
    1032             :         }
    1033        3155 :         ASSERT(from > to);
    1034             :         /*
    1035             :          * If the insertion point was past the last entry,
    1036             :          * set the new insertion point accordingly.
    1037             :          */
    1038        3155 :         if (index == from)
    1039          79 :                 newindex = to;
    1040        3155 :         *indexp = newindex;
    1041             :         /*
    1042             :          * Adjust the leaf header values.
    1043             :          */
    1044        3155 :         leafhdr->count -= from - to;
    1045        3155 :         leafhdr->stale = 1;
    1046             :         /*
    1047             :          * Remember the low/high stale value only in the "right"
    1048             :          * direction.
    1049             :          */
    1050        3155 :         if (lowstale >= newindex)
    1051         239 :                 lowstale = -1;
    1052             :         else
    1053        2916 :                 highstale = leafhdr->count;
    1054        3155 :         *highlogp = leafhdr->count - 1;
    1055        3155 :         *lowstalep = lowstale;
    1056        3155 :         *highstalep = highstale;
    1057        3155 : }
    1058             : 
    1059             : /*
    1060             :  * Log the bests entries indicated from a leaf1 block.
    1061             :  */
    1062             : static void
    1063     4815076 : xfs_dir3_leaf_log_bests(
    1064             :         struct xfs_da_args      *args,
    1065             :         struct xfs_buf          *bp,            /* leaf buffer */
    1066             :         int                     first,          /* first entry to log */
    1067             :         int                     last)           /* last entry to log */
    1068             : {
    1069     4815076 :         __be16                  *firstb;        /* pointer to first entry */
    1070     4815076 :         __be16                  *lastb;         /* pointer to last entry */
    1071     4815076 :         struct xfs_dir2_leaf    *leaf = bp->b_addr;
    1072     4815076 :         xfs_dir2_leaf_tail_t    *ltp;           /* leaf tail structure */
    1073             : 
    1074     4815076 :         ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
    1075             :                leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
    1076             : 
    1077     4815076 :         ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
    1078     4815076 :         firstb = xfs_dir2_leaf_bests_p(ltp) + first;
    1079     4815076 :         lastb = xfs_dir2_leaf_bests_p(ltp) + last;
    1080     4815076 :         xfs_trans_log_buf(args->trans, bp,
    1081     4815076 :                 (uint)((char *)firstb - (char *)leaf),
    1082     4815076 :                 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
    1083     4815086 : }
    1084             : 
    1085             : /*
    1086             :  * Log the leaf entries indicated from a leaf1 or leafn block.
    1087             :  */
    1088             : void
    1089    65524589 : xfs_dir3_leaf_log_ents(
    1090             :         struct xfs_da_args      *args,
    1091             :         struct xfs_dir3_icleaf_hdr *hdr,
    1092             :         struct xfs_buf          *bp,
    1093             :         int                     first,
    1094             :         int                     last)
    1095             : {
    1096    65524589 :         xfs_dir2_leaf_entry_t   *firstlep;      /* pointer to first entry */
    1097    65524589 :         xfs_dir2_leaf_entry_t   *lastlep;       /* pointer to last entry */
    1098    65524589 :         struct xfs_dir2_leaf    *leaf = bp->b_addr;
    1099             : 
    1100    65524589 :         ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
    1101             :                leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
    1102             :                leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
    1103             :                leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
    1104             : 
    1105    65524589 :         firstlep = &hdr->ents[first];
    1106    65524589 :         lastlep = &hdr->ents[last];
    1107    65524589 :         xfs_trans_log_buf(args->trans, bp,
    1108    65524589 :                 (uint)((char *)firstlep - (char *)leaf),
    1109    65524589 :                 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
    1110    65529676 : }
    1111             : 
    1112             : /*
    1113             :  * Log the header of the leaf1 or leafn block.
    1114             :  */
    1115             : void
    1116    65602929 : xfs_dir3_leaf_log_header(
    1117             :         struct xfs_da_args      *args,
    1118             :         struct xfs_buf          *bp)
    1119             : {
    1120    65602929 :         struct xfs_dir2_leaf    *leaf = bp->b_addr;
    1121             : 
    1122    65602929 :         ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
    1123             :                leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
    1124             :                leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
    1125             :                leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
    1126             : 
    1127    65602929 :         xfs_trans_log_buf(args->trans, bp,
    1128             :                           (uint)((char *)&leaf->hdr - (char *)leaf),
    1129    65602929 :                           args->geo->leaf_hdr_size - 1);
    1130    65599152 : }
    1131             : 
    1132             : /*
    1133             :  * Log the tail of the leaf1 block.
    1134             :  */
    1135             : STATIC void
    1136       20691 : xfs_dir3_leaf_log_tail(
    1137             :         struct xfs_da_args      *args,
    1138             :         struct xfs_buf          *bp)
    1139             : {
    1140       20691 :         struct xfs_dir2_leaf    *leaf = bp->b_addr;
    1141       20691 :         xfs_dir2_leaf_tail_t    *ltp;           /* leaf tail structure */
    1142             : 
    1143       20691 :         ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
    1144             :                leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
    1145             :                leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
    1146             :                leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
    1147             : 
    1148       20691 :         ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
    1149       20691 :         xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf),
    1150             :                 (uint)(args->geo->blksize - 1));
    1151       20691 : }
    1152             : 
    1153             : /*
    1154             :  * Look up the entry referred to by args in the leaf format directory.
    1155             :  * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
    1156             :  * is also used by the node-format code.
    1157             :  */
    1158             : int
    1159    12088636 : xfs_dir2_leaf_lookup(
    1160             :         xfs_da_args_t           *args)          /* operation arguments */
    1161             : {
    1162    12088636 :         struct xfs_buf          *dbp;           /* data block buffer */
    1163    12088636 :         xfs_dir2_data_entry_t   *dep;           /* data block entry */
    1164    12088636 :         xfs_inode_t             *dp;            /* incore directory inode */
    1165    12088636 :         int                     error;          /* error return code */
    1166    12088636 :         int                     index;          /* found entry index */
    1167    12088636 :         struct xfs_buf          *lbp;           /* leaf buffer */
    1168    12088636 :         xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
    1169    12088636 :         xfs_trans_t             *tp;            /* transaction pointer */
    1170    12088636 :         struct xfs_dir3_icleaf_hdr leafhdr;
    1171             : 
    1172    12088636 :         trace_xfs_dir2_leaf_lookup(args);
    1173             : 
    1174             :         /*
    1175             :          * Look up name in the leaf block, returning both buffers and index.
    1176             :          */
    1177    12089419 :         error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
    1178    12088815 :         if (error)
    1179             :                 return error;
    1180             : 
    1181     3115745 :         tp = args->trans;
    1182     3115745 :         dp = args->dp;
    1183     3115745 :         xfs_dir3_leaf_check(dp, lbp);
    1184             : 
    1185             :         /*
    1186             :          * Get to the leaf entry and contained data entry address.
    1187             :          */
    1188     3115743 :         lep = &leafhdr.ents[index];
    1189             : 
    1190             :         /*
    1191             :          * Point to the data entry.
    1192             :          */
    1193     6231486 :         dep = (xfs_dir2_data_entry_t *)
    1194     3115743 :               ((char *)dbp->b_addr +
    1195     3115743 :                xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
    1196             :         /*
    1197             :          * Return the found inode number & CI name if appropriate
    1198             :          */
    1199     3115743 :         args->inumber = be64_to_cpu(dep->inumber);
    1200     3115743 :         args->filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep);
    1201     3115738 :         error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
    1202     3115739 :         xfs_trans_brelse(tp, dbp);
    1203     3115746 :         xfs_trans_brelse(tp, lbp);
    1204     3115746 :         return error;
    1205             : }
    1206             : 
    1207             : /*
    1208             :  * Look up name/hash in the leaf block.
    1209             :  * Fill in indexp with the found index, and dbpp with the data buffer.
    1210             :  * If not found dbpp will be NULL, and ENOENT comes back.
    1211             :  * lbpp will always be filled in with the leaf buffer unless there's an error.
    1212             :  */
    1213             : static int                                      /* error */
    1214    17485421 : xfs_dir2_leaf_lookup_int(
    1215             :         xfs_da_args_t           *args,          /* operation arguments */
    1216             :         struct xfs_buf          **lbpp,         /* out: leaf buffer */
    1217             :         int                     *indexp,        /* out: index in leaf block */
    1218             :         struct xfs_buf          **dbpp,         /* out: data buffer */
    1219             :         struct xfs_dir3_icleaf_hdr *leafhdr)
    1220             : {
    1221    17485421 :         xfs_dir2_db_t           curdb = -1;     /* current data block number */
    1222    17485421 :         struct xfs_buf          *dbp = NULL;    /* data buffer */
    1223    17485421 :         xfs_dir2_data_entry_t   *dep;           /* data entry */
    1224    17485421 :         xfs_inode_t             *dp;            /* incore directory inode */
    1225    17485421 :         int                     error;          /* error return code */
    1226    17485421 :         int                     index;          /* index in leaf block */
    1227    17485421 :         struct xfs_buf          *lbp;           /* leaf buffer */
    1228    17485421 :         xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
    1229    17485421 :         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
    1230    17485421 :         xfs_mount_t             *mp;            /* filesystem mount point */
    1231    17485421 :         xfs_dir2_db_t           newdb;          /* new data block number */
    1232    17485421 :         xfs_trans_t             *tp;            /* transaction pointer */
    1233    17485421 :         xfs_dir2_db_t           cidb = -1;      /* case match data block no. */
    1234    17485421 :         enum xfs_dacmp          cmp;            /* name compare result */
    1235             : 
    1236    17485421 :         dp = args->dp;
    1237    17485421 :         tp = args->trans;
    1238    17485421 :         mp = dp->i_mount;
    1239             : 
    1240    17485421 :         error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, &lbp);
    1241    17488638 :         if (error)
    1242             :                 return error;
    1243             : 
    1244    17488295 :         *lbpp = lbp;
    1245    17488295 :         leaf = lbp->b_addr;
    1246    17488295 :         xfs_dir3_leaf_check(dp, lbp);
    1247    17488251 :         xfs_dir2_leaf_hdr_from_disk(mp, leafhdr, leaf);
    1248             : 
    1249             :         /*
    1250             :          * Look for the first leaf entry with our hash value.
    1251             :          */
    1252    17487348 :         index = xfs_dir2_leaf_search_hash(args, lbp);
    1253             :         /*
    1254             :          * Loop over all the entries with the right hash value
    1255             :          * looking to match the name.
    1256             :          */
    1257    17486584 :         for (lep = &leafhdr->ents[index];
    1258    46887847 :              index < leafhdr->count &&
    1259    90358594 :                         be32_to_cpu(lep->hashval) == args->hashval;
    1260    29401263 :              lep++, index++) {
    1261             :                 /*
    1262             :                  * Skip over stale leaf entries.
    1263             :                  */
    1264    37890488 :                 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
    1265     3736673 :                         continue;
    1266             :                 /*
    1267             :                  * Get the new data block number.
    1268             :                  */
    1269    34153815 :                 newdb = xfs_dir2_dataptr_to_db(args->geo,
    1270           0 :                                                be32_to_cpu(lep->address));
    1271             :                 /*
    1272             :                  * If it's not the same as the old data block number,
    1273             :                  * need to pitch the old one and read the new one.
    1274             :                  */
    1275    34153828 :                 if (newdb != curdb) {
    1276     8802201 :                         if (dbp)
    1277      260311 :                                 xfs_trans_brelse(tp, dbp);
    1278     8802201 :                         error = xfs_dir3_data_read(tp, dp,
    1279             :                                            xfs_dir2_db_to_da(args->geo, newdb),
    1280             :                                            0, &dbp);
    1281     8802007 :                         if (error) {
    1282         133 :                                 xfs_trans_brelse(tp, lbp);
    1283         133 :                                 return error;
    1284             :                         }
    1285             :                         curdb = newdb;
    1286             :                 }
    1287             :                 /*
    1288             :                  * Point to the data entry.
    1289             :                  */
    1290    68307002 :                 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
    1291    68307002 :                         xfs_dir2_dataptr_to_off(args->geo,
    1292    34153501 :                                                 be32_to_cpu(lep->address)));
    1293             :                 /*
    1294             :                  * Compare name and if it's an exact match, return the index
    1295             :                  * and buffer. If it's the first case-insensitive match, store
    1296             :                  * the index and buffer and continue looking for an exact match.
    1297             :                  */
    1298    34153501 :                 cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
    1299    34155011 :                 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
    1300     8514270 :                         args->cmpresult = cmp;
    1301     8514270 :                         *indexp = index;
    1302             :                         /* case exact match: return the current buffer. */
    1303     8514270 :                         if (cmp == XFS_CMP_EXACT) {
    1304     8490421 :                                 *dbpp = dbp;
    1305     8490421 :                                 return 0;
    1306             :                         }
    1307             :                         cidb = curdb;
    1308             :                 }
    1309             :         }
    1310     8997359 :         ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
    1311             :         /*
    1312             :          * Here, we can only be doing a lookup (not a rename or remove).
    1313             :          * If a case-insensitive match was found earlier, re-read the
    1314             :          * appropriate data block if required and return it.
    1315             :          */
    1316     8997359 :         if (args->cmpresult == XFS_CMP_CASE) {
    1317       23688 :                 ASSERT(cidb != -1);
    1318       23688 :                 if (cidb != curdb) {
    1319       18301 :                         xfs_trans_brelse(tp, dbp);
    1320       18301 :                         error = xfs_dir3_data_read(tp, dp,
    1321             :                                            xfs_dir2_db_to_da(args->geo, cidb),
    1322             :                                            0, &dbp);
    1323       18301 :                         if (error) {
    1324           0 :                                 xfs_trans_brelse(tp, lbp);
    1325           0 :                                 return error;
    1326             :                         }
    1327             :                 }
    1328       23688 :                 *dbpp = dbp;
    1329       23688 :                 return 0;
    1330             :         }
    1331             :         /*
    1332             :          * No match found, return -ENOENT.
    1333             :          */
    1334     8973671 :         ASSERT(cidb == -1);
    1335     8973671 :         if (dbp)
    1336       27418 :                 xfs_trans_brelse(tp, dbp);
    1337     8973671 :         xfs_trans_brelse(tp, lbp);
    1338     8973671 :         return -ENOENT;
    1339             : }
    1340             : 
    1341             : /*
    1342             :  * Remove an entry from a leaf format directory.
    1343             :  */
    1344             : int                                             /* error */
    1345     5181322 : xfs_dir2_leaf_removename(
    1346             :         xfs_da_args_t           *args)          /* operation arguments */
    1347             : {
    1348     5181322 :         struct xfs_da_geometry  *geo = args->geo;
    1349     5181322 :         __be16                  *bestsp;        /* leaf block best freespace */
    1350     5181322 :         xfs_dir2_data_hdr_t     *hdr;           /* data block header */
    1351     5181322 :         xfs_dir2_db_t           db;             /* data block number */
    1352     5181322 :         struct xfs_buf          *dbp;           /* data block buffer */
    1353     5181322 :         xfs_dir2_data_entry_t   *dep;           /* data entry structure */
    1354     5181322 :         xfs_inode_t             *dp;            /* incore directory inode */
    1355     5181322 :         int                     error;          /* error return code */
    1356     5181322 :         xfs_dir2_db_t           i;              /* temporary data block # */
    1357     5181322 :         int                     index;          /* index into leaf entries */
    1358     5181322 :         struct xfs_buf          *lbp;           /* leaf buffer */
    1359     5181322 :         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
    1360     5181322 :         xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
    1361     5181322 :         xfs_dir2_leaf_tail_t    *ltp;           /* leaf tail structure */
    1362     5181322 :         int                     needlog;        /* need to log data header */
    1363     5181322 :         int                     needscan;       /* need to rescan data frees */
    1364     5181322 :         xfs_dir2_data_off_t     oldbest;        /* old value of best free */
    1365     5181322 :         struct xfs_dir2_data_free *bf;          /* bestfree table */
    1366     5181322 :         struct xfs_dir3_icleaf_hdr leafhdr;
    1367             : 
    1368     5181322 :         trace_xfs_dir2_leaf_removename(args);
    1369             : 
    1370             :         /*
    1371             :          * Lookup the leaf entry, get the leaf and data blocks read in.
    1372             :          */
    1373     5181422 :         error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
    1374     5181425 :         if (error)
    1375             :                 return error;
    1376             : 
    1377     5181434 :         dp = args->dp;
    1378     5181434 :         leaf = lbp->b_addr;
    1379     5181434 :         hdr = dbp->b_addr;
    1380     5181434 :         xfs_dir3_data_check(dp, dbp);
    1381     5181333 :         bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
    1382             : 
    1383             :         /*
    1384             :          * Point to the leaf entry, use that to point to the data entry.
    1385             :          */
    1386     5181420 :         lep = &leafhdr.ents[index];
    1387     5181420 :         db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
    1388    10362824 :         dep = (xfs_dir2_data_entry_t *)((char *)hdr +
    1389     5181412 :                 xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address)));
    1390     5181412 :         needscan = needlog = 0;
    1391     5181412 :         oldbest = be16_to_cpu(bf[0].length);
    1392     5181412 :         ltp = xfs_dir2_leaf_tail_p(geo, leaf);
    1393     5181412 :         bestsp = xfs_dir2_leaf_bests_p(ltp);
    1394     5181412 :         if (be16_to_cpu(bestsp[db]) != oldbest) {
    1395           0 :                 xfs_buf_mark_corrupt(lbp);
    1396           0 :                 return -EFSCORRUPTED;
    1397             :         }
    1398             :         /*
    1399             :          * Mark the former data entry unused.
    1400             :          */
    1401     5181412 :         xfs_dir2_data_make_free(args, dbp,
    1402             :                 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
    1403     5181412 :                 xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
    1404             :                 &needscan);
    1405             :         /*
    1406             :          * We just mark the leaf entry stale by putting a null in it.
    1407             :          */
    1408     5181427 :         leafhdr.stale++;
    1409     5181427 :         xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
    1410     5181442 :         xfs_dir3_leaf_log_header(args, lbp);
    1411             : 
    1412     5181377 :         lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
    1413     5181377 :         xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, index, index);
    1414             : 
    1415             :         /*
    1416             :          * Scan the freespace in the data block again if necessary,
    1417             :          * log the data block header if necessary.
    1418             :          */
    1419     5181092 :         if (needscan)
    1420      730317 :                 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
    1421     5181074 :         if (needlog)
    1422     3311323 :                 xfs_dir2_data_log_header(args, dbp);
    1423             :         /*
    1424             :          * If the longest freespace in the data block has changed,
    1425             :          * put the new value in the bests table and log that.
    1426             :          */
    1427     5181059 :         if (be16_to_cpu(bf[0].length) != oldbest) {
    1428     1776427 :                 bestsp[db] = bf[0].length;
    1429     1776427 :                 xfs_dir3_leaf_log_bests(args, lbp, db, db);
    1430             :         }
    1431     5181055 :         xfs_dir3_data_check(dp, dbp);
    1432             :         /*
    1433             :          * If the data block is now empty then get rid of the data block.
    1434             :          */
    1435     5181401 :         if (be16_to_cpu(bf[0].length) ==
    1436     5181401 :             geo->blksize - geo->data_entry_offset) {
    1437        6384 :                 ASSERT(db != geo->datablk);
    1438        6384 :                 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
    1439             :                         /*
    1440             :                          * Nope, can't get rid of it because it caused
    1441             :                          * allocation of a bmap btree block to do so.
    1442             :                          * Just go on, returning success, leaving the
    1443             :                          * empty block in place.
    1444             :                          */
    1445           0 :                         if (error == -ENOSPC && args->total == 0)
    1446           0 :                                 error = 0;
    1447           0 :                         xfs_dir3_leaf_check(dp, lbp);
    1448           0 :                         return error;
    1449             :                 }
    1450        6384 :                 dbp = NULL;
    1451             :                 /*
    1452             :                  * If this is the last data block then compact the
    1453             :                  * bests table by getting rid of entries.
    1454             :                  */
    1455        6384 :                 if (db == be32_to_cpu(ltp->bestcount) - 1) {
    1456             :                         /*
    1457             :                          * Look for the last active entry (i).
    1458             :                          */
    1459       12064 :                         for (i = db - 1; i > 0; i--) {
    1460        9944 :                                 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
    1461             :                                         break;
    1462             :                         }
    1463             :                         /*
    1464             :                          * Copy the table down so inactive entries at the
    1465             :                          * end are removed.
    1466             :                          */
    1467        7593 :                         memmove(&bestsp[db - i], bestsp,
    1468             :                                 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
    1469        2531 :                         be32_add_cpu(&ltp->bestcount, -(db - i));
    1470        2531 :                         xfs_dir3_leaf_log_tail(args, lbp);
    1471        2531 :                         xfs_dir3_leaf_log_bests(args, lbp, 0,
    1472        2531 :                                                 be32_to_cpu(ltp->bestcount) - 1);
    1473             :                 } else
    1474        3853 :                         bestsp[db] = cpu_to_be16(NULLDATAOFF);
    1475             :         }
    1476             :         /*
    1477             :          * If the data block was not the first one, drop it.
    1478             :          */
    1479     5175017 :         else if (db != geo->datablk)
    1480     2887320 :                 dbp = NULL;
    1481             : 
    1482     5181401 :         xfs_dir3_leaf_check(dp, lbp);
    1483             :         /*
    1484             :          * See if we can convert to block form.
    1485             :          */
    1486     5181423 :         return xfs_dir2_leaf_to_block(args, lbp, dbp);
    1487             : }
    1488             : 
    1489             : /*
    1490             :  * Replace the inode number in a leaf format directory entry.
    1491             :  */
    1492             : int                                             /* error */
    1493      217213 : xfs_dir2_leaf_replace(
    1494             :         xfs_da_args_t           *args)          /* operation arguments */
    1495             : {
    1496      217213 :         struct xfs_buf          *dbp;           /* data block buffer */
    1497      217213 :         xfs_dir2_data_entry_t   *dep;           /* data block entry */
    1498      217213 :         xfs_inode_t             *dp;            /* incore directory inode */
    1499      217213 :         int                     error;          /* error return code */
    1500      217213 :         int                     index;          /* index of leaf entry */
    1501      217213 :         struct xfs_buf          *lbp;           /* leaf buffer */
    1502      217213 :         xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
    1503      217213 :         xfs_trans_t             *tp;            /* transaction pointer */
    1504      217213 :         struct xfs_dir3_icleaf_hdr leafhdr;
    1505             : 
    1506      217213 :         trace_xfs_dir2_leaf_replace(args);
    1507             : 
    1508             :         /*
    1509             :          * Look up the entry.
    1510             :          */
    1511      217213 :         error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
    1512      217213 :         if (error)
    1513             :                 return error;
    1514             : 
    1515      217198 :         dp = args->dp;
    1516             :         /*
    1517             :          * Point to the leaf entry, get data address from it.
    1518             :          */
    1519      217198 :         lep = &leafhdr.ents[index];
    1520             :         /*
    1521             :          * Point to the data entry.
    1522             :          */
    1523      434396 :         dep = (xfs_dir2_data_entry_t *)
    1524      217198 :               ((char *)dbp->b_addr +
    1525      217198 :                xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
    1526      217198 :         ASSERT(args->inumber != be64_to_cpu(dep->inumber));
    1527             :         /*
    1528             :          * Put the new inode number in, log it.
    1529             :          */
    1530      217198 :         dep->inumber = cpu_to_be64(args->inumber);
    1531      217198 :         xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
    1532      217198 :         tp = args->trans;
    1533      217198 :         xfs_dir2_data_log_entry(args, dbp, dep);
    1534      217198 :         xfs_dir3_leaf_check(dp, lbp);
    1535      217198 :         xfs_trans_brelse(tp, lbp);
    1536      217198 :         return 0;
    1537             : }
    1538             : 
    1539             : /*
    1540             :  * Return index in the leaf block (lbp) which is either the first
    1541             :  * one with this hash value, or if there are none, the insert point
    1542             :  * for that hash value.
    1543             :  */
    1544             : int                                             /* index value */
    1545   201803006 : xfs_dir2_leaf_search_hash(
    1546             :         xfs_da_args_t           *args,          /* operation arguments */
    1547             :         struct xfs_buf          *lbp)           /* leaf buffer */
    1548             : {
    1549   201803006 :         xfs_dahash_t            hash=0;         /* hash from this entry */
    1550   201803006 :         xfs_dahash_t            hashwant;       /* hash value looking for */
    1551   201803006 :         int                     high;           /* high leaf index */
    1552   201803006 :         int                     low;            /* low leaf index */
    1553   201803006 :         xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
    1554   201803006 :         int                     mid=0;          /* current leaf index */
    1555   201803006 :         struct xfs_dir3_icleaf_hdr leafhdr;
    1556             : 
    1557   201803006 :         xfs_dir2_leaf_hdr_from_disk(args->dp->i_mount, &leafhdr, lbp->b_addr);
    1558             : 
    1559             :         /*
    1560             :          * Note, the table cannot be empty, so we have to go through the loop.
    1561             :          * Binary search the leaf entries looking for our hash value.
    1562             :          */
    1563   201819959 :         for (lep = leafhdr.ents, low = 0, high = leafhdr.count - 1,
    1564   201819959 :                 hashwant = args->hashval;
    1565  1604174790 :              low <= high; ) {
    1566  1554892628 :                 mid = (low + high) >> 1;
    1567  3109785256 :                 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
    1568             :                         break;
    1569  1402354831 :                 if (hash < hashwant)
    1570   777527318 :                         low = mid + 1;
    1571             :                 else
    1572   624827513 :                         high = mid - 1;
    1573             :         }
    1574             :         /*
    1575             :          * Found one, back up through all the equal hash values.
    1576             :          */
    1577   201819959 :         if (hash == hashwant) {
    1578   577191468 :                 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
    1579             :                         mid--;
    1580             :                 }
    1581             :         }
    1582             :         /*
    1583             :          * Need to point to an entry higher than ours.
    1584             :          */
    1585    49279687 :         else if (hash < hashwant)
    1586    23063547 :                 mid++;
    1587   201819959 :         return mid;
    1588             : }
    1589             : 
    1590             : /*
    1591             :  * Trim off a trailing data block.  We know it's empty since the leaf
    1592             :  * freespace table says so.
    1593             :  */
    1594             : int                                             /* error */
    1595           0 : xfs_dir2_leaf_trim_data(
    1596             :         xfs_da_args_t           *args,          /* operation arguments */
    1597             :         struct xfs_buf          *lbp,           /* leaf buffer */
    1598             :         xfs_dir2_db_t           db)             /* data block number */
    1599             : {
    1600           0 :         struct xfs_da_geometry  *geo = args->geo;
    1601           0 :         __be16                  *bestsp;        /* leaf bests table */
    1602           0 :         struct xfs_buf          *dbp;           /* data block buffer */
    1603           0 :         xfs_inode_t             *dp;            /* incore directory inode */
    1604           0 :         int                     error;          /* error return value */
    1605           0 :         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
    1606           0 :         xfs_dir2_leaf_tail_t    *ltp;           /* leaf tail structure */
    1607           0 :         xfs_trans_t             *tp;            /* transaction pointer */
    1608             : 
    1609           0 :         dp = args->dp;
    1610           0 :         tp = args->trans;
    1611             :         /*
    1612             :          * Read the offending data block.  We need its buffer.
    1613             :          */
    1614           0 :         error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(geo, db), 0, &dbp);
    1615           0 :         if (error)
    1616             :                 return error;
    1617             : 
    1618           0 :         leaf = lbp->b_addr;
    1619           0 :         ltp = xfs_dir2_leaf_tail_p(geo, leaf);
    1620             : 
    1621             : #ifdef DEBUG
    1622             : {
    1623           0 :         struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
    1624           0 :         struct xfs_dir2_data_free *bf =
    1625           0 :                 xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
    1626             : 
    1627           0 :         ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
    1628             :                hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
    1629           0 :         ASSERT(be16_to_cpu(bf[0].length) ==
    1630             :                geo->blksize - geo->data_entry_offset);
    1631           0 :         ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
    1632             : }
    1633             : #endif
    1634             : 
    1635             :         /*
    1636             :          * Get rid of the data block.
    1637             :          */
    1638           0 :         if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
    1639           0 :                 ASSERT(error != -ENOSPC);
    1640           0 :                 xfs_trans_brelse(tp, dbp);
    1641           0 :                 return error;
    1642             :         }
    1643             :         /*
    1644             :          * Eliminate the last bests entry from the table.
    1645             :          */
    1646           0 :         bestsp = xfs_dir2_leaf_bests_p(ltp);
    1647           0 :         be32_add_cpu(&ltp->bestcount, -1);
    1648           0 :         memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
    1649           0 :         xfs_dir3_leaf_log_tail(args, lbp);
    1650           0 :         xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
    1651           0 :         return 0;
    1652             : }
    1653             : 
    1654             : static inline size_t
    1655        1401 : xfs_dir3_leaf_size(
    1656             :         struct xfs_dir3_icleaf_hdr      *hdr,
    1657             :         int                             counts)
    1658             : {
    1659        1401 :         int     entries;
    1660        1401 :         int     hdrsize;
    1661             : 
    1662        1401 :         entries = hdr->count - hdr->stale;
    1663        1401 :         if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
    1664             :             hdr->magic == XFS_DIR2_LEAFN_MAGIC)
    1665             :                 hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
    1666             :         else
    1667        1401 :                 hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
    1668             : 
    1669        1401 :         return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
    1670        1401 :                        + counts * sizeof(xfs_dir2_data_off_t)
    1671        1401 :                        + sizeof(xfs_dir2_leaf_tail_t);
    1672             : }
    1673             : 
    1674             : /*
    1675             :  * Convert node form directory to leaf form directory.
    1676             :  * The root of the node form dir needs to already be a LEAFN block.
    1677             :  * Just return if we can't do anything.
    1678             :  */
    1679             : int                                             /* error */
    1680    26756856 : xfs_dir2_node_to_leaf(
    1681             :         xfs_da_state_t          *state)         /* directory operation state */
    1682             : {
    1683    26756856 :         xfs_da_args_t           *args;          /* operation arguments */
    1684    26756856 :         xfs_inode_t             *dp;            /* incore directory inode */
    1685    26756856 :         int                     error;          /* error return code */
    1686    26756856 :         struct xfs_buf          *fbp;           /* buffer for freespace block */
    1687    26756856 :         xfs_fileoff_t           fo;             /* freespace file offset */
    1688    26756856 :         struct xfs_buf          *lbp;           /* buffer for leaf block */
    1689    26756856 :         xfs_dir2_leaf_tail_t    *ltp;           /* tail of leaf structure */
    1690    26756856 :         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
    1691    26756856 :         xfs_mount_t             *mp;            /* filesystem mount point */
    1692    26756856 :         int                     rval;           /* successful free trim? */
    1693    26756856 :         xfs_trans_t             *tp;            /* transaction pointer */
    1694    26756856 :         struct xfs_dir3_icleaf_hdr leafhdr;
    1695    26756856 :         struct xfs_dir3_icfree_hdr freehdr;
    1696             : 
    1697             :         /*
    1698             :          * There's more than a leaf level in the btree, so there must
    1699             :          * be multiple leafn blocks.  Give up.
    1700             :          */
    1701    26756856 :         if (state->path.active > 1)
    1702             :                 return 0;
    1703       19206 :         args = state->args;
    1704             : 
    1705       19206 :         trace_xfs_dir2_node_to_leaf(args);
    1706             : 
    1707       19206 :         mp = state->mp;
    1708       19206 :         dp = args->dp;
    1709       19206 :         tp = args->trans;
    1710             :         /*
    1711             :          * Get the last offset in the file.
    1712             :          */
    1713       19206 :         if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) {
    1714             :                 return error;
    1715             :         }
    1716       19206 :         fo -= args->geo->fsbcount;
    1717             :         /*
    1718             :          * If there are freespace blocks other than the first one,
    1719             :          * take this opportunity to remove trailing empty freespace blocks
    1720             :          * that may have been left behind during no-space-reservation
    1721             :          * operations.
    1722             :          */
    1723       19206 :         while (fo > args->geo->freeblk) {
    1724        2150 :                 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
    1725           0 :                         return error;
    1726             :                 }
    1727        2150 :                 if (rval)
    1728           0 :                         fo -= args->geo->fsbcount;
    1729             :                 else
    1730             :                         return 0;
    1731             :         }
    1732             :         /*
    1733             :          * Now find the block just before the freespace block.
    1734             :          */
    1735       17056 :         if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
    1736             :                 return error;
    1737             :         }
    1738             :         /*
    1739             :          * If it's not the single leaf block, give up.
    1740             :          */
    1741       17056 :         if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize)
    1742             :                 return 0;
    1743        1401 :         lbp = state->path.blk[0].bp;
    1744        1401 :         leaf = lbp->b_addr;
    1745        1401 :         xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
    1746             : 
    1747        1401 :         ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
    1748             :                leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
    1749             : 
    1750             :         /*
    1751             :          * Read the freespace block.
    1752             :          */
    1753        1401 :         error = xfs_dir2_free_read(tp, dp,  args->geo->freeblk, &fbp);
    1754        1401 :         if (error)
    1755             :                 return error;
    1756        1401 :         xfs_dir2_free_hdr_from_disk(mp, &freehdr, fbp->b_addr);
    1757             : 
    1758        1401 :         ASSERT(!freehdr.firstdb);
    1759             : 
    1760             :         /*
    1761             :          * Now see if the leafn and free data will fit in a leaf1.
    1762             :          * If not, release the buffer and give up.
    1763             :          */
    1764        1401 :         if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) {
    1765         102 :                 xfs_trans_brelse(tp, fbp);
    1766         102 :                 return 0;
    1767             :         }
    1768             : 
    1769             :         /*
    1770             :          * If the leaf has any stale entries in it, compress them out.
    1771             :          */
    1772        1299 :         if (leafhdr.stale)
    1773         178 :                 xfs_dir3_leaf_compact(args, &leafhdr, lbp);
    1774             : 
    1775        1299 :         lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
    1776        1299 :         xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
    1777        1299 :         leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
    1778             :                                         ? XFS_DIR2_LEAF1_MAGIC
    1779             :                                         : XFS_DIR3_LEAF1_MAGIC;
    1780             : 
    1781             :         /*
    1782             :          * Set up the leaf tail from the freespace block.
    1783             :          */
    1784        1299 :         ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
    1785        1299 :         ltp->bestcount = cpu_to_be32(freehdr.nvalid);
    1786             : 
    1787             :         /*
    1788             :          * Set up the leaf bests table.
    1789             :          */
    1790        1299 :         memcpy(xfs_dir2_leaf_bests_p(ltp), freehdr.bests,
    1791             :                 freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
    1792             : 
    1793        1299 :         xfs_dir2_leaf_hdr_to_disk(mp, leaf, &leafhdr);
    1794        1299 :         xfs_dir3_leaf_log_header(args, lbp);
    1795        1299 :         xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
    1796        1299 :         xfs_dir3_leaf_log_tail(args, lbp);
    1797        1299 :         xfs_dir3_leaf_check(dp, lbp);
    1798             : 
    1799             :         /*
    1800             :          * Get rid of the freespace block.
    1801             :          */
    1802        1299 :         error = xfs_dir2_shrink_inode(args,
    1803             :                         xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET),
    1804             :                         fbp);
    1805        1299 :         if (error) {
    1806             :                 /*
    1807             :                  * This can't fail here because it can only happen when
    1808             :                  * punching out the middle of an extent, and this is an
    1809             :                  * isolated block.
    1810             :                  */
    1811           0 :                 ASSERT(error != -ENOSPC);
    1812           0 :                 return error;
    1813             :         }
    1814        1299 :         fbp = NULL;
    1815             :         /*
    1816             :          * Now see if we can convert the single-leaf directory
    1817             :          * down to a block form directory.
    1818             :          * This routine always kills the dabuf for the leaf, so
    1819             :          * eliminate it from the path.
    1820             :          */
    1821        1299 :         error = xfs_dir2_leaf_to_block(args, lbp, NULL);
    1822        1299 :         state->path.blk[0].bp = NULL;
    1823        1299 :         return error;
    1824             : }

Generated by: LCOV version 1.14