LCOV - code coverage report
Current view: top level - fs/xfs/libxfs - xfs_dir2_node.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-djwx @ Mon Jul 31 20:08:22 PDT 2023 Lines: 939 1037 90.5 %
Date: 2023-07-31 20:08:22 Functions: 38 38 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
       4             :  * Copyright (c) 2013 Red Hat, Inc.
       5             :  * All Rights Reserved.
       6             :  */
       7             : #include "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             : #include "xfs_log.h"
      23             : 
      24             : /*
      25             :  * Function declarations.
      26             :  */
      27             : static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
      28             :                               int index);
      29             : static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
      30             :                                      xfs_da_state_blk_t *blk1,
      31             :                                      xfs_da_state_blk_t *blk2);
      32             : static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
      33             :                                  int index, xfs_da_state_blk_t *dblk,
      34             :                                  int *rval);
      35             : 
      36             : /*
      37             :  * Convert data space db to the corresponding free db.
      38             :  */
      39             : static xfs_dir2_db_t
      40    31606730 : xfs_dir2_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
      41             : {
      42    31606730 :         return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) +
      43    31606730 :                         (db / geo->free_max_bests);
      44             : }
      45             : 
      46             : /*
      47             :  * Convert data space db to the corresponding index in a free db.
      48             :  */
      49             : static int
      50    31602529 : xfs_dir2_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
      51             : {
      52    31602529 :         return db % geo->free_max_bests;
      53             : }
      54             : 
      55             : /*
      56             :  * Check internal consistency of a leafn block.
      57             :  */
      58             : #ifdef DEBUG
      59             : static xfs_failaddr_t
      60   301984573 : xfs_dir3_leafn_check(
      61             :         struct xfs_inode        *dp,
      62             :         struct xfs_buf          *bp)
      63             : {
      64   301984573 :         struct xfs_dir2_leaf    *leaf = bp->b_addr;
      65   301984573 :         struct xfs_dir3_icleaf_hdr leafhdr;
      66             : 
      67   301984573 :         xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
      68             : 
      69   301979861 :         if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
      70   301979861 :                 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
      71   301979861 :                 if (be64_to_cpu(leaf3->info.blkno) != xfs_buf_daddr(bp))
      72           0 :                         return __this_address;
      73           0 :         } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
      74           0 :                 return __this_address;
      75             : 
      76   301979861 :         return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf, false);
      77             : }
      78             : 
      79             : static inline void
      80   302006252 : xfs_dir3_leaf_check(
      81             :         struct xfs_inode        *dp,
      82             :         struct xfs_buf          *bp)
      83             : {
      84   302006252 :         xfs_failaddr_t          fa;
      85             : 
      86   302006252 :         fa = xfs_dir3_leafn_check(dp, bp);
      87   302096078 :         if (!fa)
      88             :                 return;
      89           0 :         xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
      90           0 :                         bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
      91             :                         fa);
      92           0 :         ASSERT(0);
      93             : }
      94             : #else
      95             : #define xfs_dir3_leaf_check(dp, bp)
      96             : #endif
      97             : 
      98             : static xfs_failaddr_t
      99      238557 : xfs_dir3_free_verify(
     100             :         struct xfs_buf          *bp)
     101             : {
     102      238557 :         struct xfs_mount        *mp = bp->b_mount;
     103      238557 :         struct xfs_dir2_free_hdr *hdr = bp->b_addr;
     104             : 
     105      238557 :         if (!xfs_verify_magic(bp, hdr->magic))
     106           0 :                 return __this_address;
     107             : 
     108      238557 :         if (xfs_has_crc(mp)) {
     109      238557 :                 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
     110             : 
     111      238557 :                 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
     112           0 :                         return __this_address;
     113      238557 :                 if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
     114           0 :                         return __this_address;
     115      238557 :                 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
     116           0 :                         return __this_address;
     117             :         }
     118             : 
     119             :         /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
     120             : 
     121             :         return NULL;
     122             : }
     123             : 
     124             : static void
     125       36504 : xfs_dir3_free_read_verify(
     126             :         struct xfs_buf  *bp)
     127             : {
     128       36504 :         struct xfs_mount        *mp = bp->b_mount;
     129       36504 :         xfs_failaddr_t          fa;
     130             : 
     131       73008 :         if (xfs_has_crc(mp) &&
     132             :             !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
     133          10 :                 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
     134             :         else {
     135       36494 :                 fa = xfs_dir3_free_verify(bp);
     136       36494 :                 if (fa)
     137           0 :                         xfs_verifier_error(bp, -EFSCORRUPTED, fa);
     138             :         }
     139       36504 : }
     140             : 
     141             : static void
     142      150402 : xfs_dir3_free_write_verify(
     143             :         struct xfs_buf  *bp)
     144             : {
     145      150402 :         struct xfs_mount        *mp = bp->b_mount;
     146      150402 :         struct xfs_buf_log_item *bip = bp->b_log_item;
     147      150402 :         struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
     148      150402 :         xfs_failaddr_t          fa;
     149             : 
     150      150402 :         fa = xfs_dir3_free_verify(bp);
     151      150402 :         if (fa) {
     152           0 :                 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
     153           0 :                 return;
     154             :         }
     155             : 
     156      150402 :         if (!xfs_has_crc(mp))
     157             :                 return;
     158             : 
     159      150402 :         if (bip)
     160      150402 :                 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
     161             : 
     162      150402 :         xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
     163             : }
     164             : 
     165             : const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
     166             :         .name = "xfs_dir3_free",
     167             :         .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC),
     168             :                    cpu_to_be32(XFS_DIR3_FREE_MAGIC) },
     169             :         .verify_read = xfs_dir3_free_read_verify,
     170             :         .verify_write = xfs_dir3_free_write_verify,
     171             :         .verify_struct = xfs_dir3_free_verify,
     172             : };
     173             : 
     174             : /* Everything ok in the free block header? */
     175             : static xfs_failaddr_t
     176    62699048 : xfs_dir3_free_header_check(
     177             :         struct xfs_inode        *dp,
     178             :         xfs_dablk_t             fbno,
     179             :         struct xfs_buf          *bp)
     180             : {
     181    62699048 :         struct xfs_mount        *mp = dp->i_mount;
     182    62699048 :         int                     maxbests = mp->m_dir_geo->free_max_bests;
     183    62699048 :         unsigned int            firstdb;
     184             : 
     185    62699048 :         firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
     186    62664783 :                    xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
     187             :                         maxbests;
     188    62671925 :         if (xfs_has_crc(mp)) {
     189    62671925 :                 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
     190             : 
     191    62671925 :                 if (be32_to_cpu(hdr3->firstdb) != firstdb)
     192           0 :                         return __this_address;
     193    62671925 :                 if (be32_to_cpu(hdr3->nvalid) > maxbests)
     194           0 :                         return __this_address;
     195    62671925 :                 if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
     196           0 :                         return __this_address;
     197    62671925 :                 if (be64_to_cpu(hdr3->hdr.owner) != dp->i_ino)
     198           0 :                         return __this_address;
     199             :         } else {
     200           0 :                 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
     201             : 
     202           0 :                 if (be32_to_cpu(hdr->firstdb) != firstdb)
     203           0 :                         return __this_address;
     204           0 :                 if (be32_to_cpu(hdr->nvalid) > maxbests)
     205           0 :                         return __this_address;
     206           0 :                 if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
     207           0 :                         return __this_address;
     208             :         }
     209             :         return NULL;
     210             : }
     211             : 
     212             : static int
     213    62624789 : __xfs_dir3_free_read(
     214             :         struct xfs_trans        *tp,
     215             :         struct xfs_inode        *dp,
     216             :         xfs_dablk_t             fbno,
     217             :         unsigned int            flags,
     218             :         struct xfs_buf          **bpp)
     219             : {
     220    62624789 :         xfs_failaddr_t          fa;
     221    62624789 :         int                     err;
     222             : 
     223    62624789 :         err = xfs_da_read_buf(tp, dp, fbno, flags, bpp, XFS_DATA_FORK,
     224             :                         &xfs_dir3_free_buf_ops);
     225    62721695 :         if (err || !*bpp)
     226             :                 return err;
     227             : 
     228             :         /* Check things that we can't do in the verifier. */
     229    62721573 :         fa = xfs_dir3_free_header_check(dp, fbno, *bpp);
     230    62674446 :         if (fa) {
     231           0 :                 __xfs_buf_mark_corrupt(*bpp, fa);
     232           0 :                 xfs_trans_brelse(tp, *bpp);
     233           0 :                 *bpp = NULL;
     234           0 :                 return -EFSCORRUPTED;
     235             :         }
     236             : 
     237             :         /* try read returns without an error or *bpp if it lands in a hole */
     238    62674446 :         if (tp)
     239    62674446 :                 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
     240             : 
     241             :         return 0;
     242             : }
     243             : 
     244             : void
     245    96030905 : xfs_dir2_free_hdr_from_disk(
     246             :         struct xfs_mount                *mp,
     247             :         struct xfs_dir3_icfree_hdr      *to,
     248             :         struct xfs_dir2_free            *from)
     249             : {
     250    96030905 :         if (xfs_has_crc(mp)) {
     251    96030905 :                 struct xfs_dir3_free    *from3 = (struct xfs_dir3_free *)from;
     252             : 
     253    96030905 :                 to->magic = be32_to_cpu(from3->hdr.hdr.magic);
     254    96030905 :                 to->firstdb = be32_to_cpu(from3->hdr.firstdb);
     255    96030905 :                 to->nvalid = be32_to_cpu(from3->hdr.nvalid);
     256    96030905 :                 to->nused = be32_to_cpu(from3->hdr.nused);
     257    96030905 :                 to->bests = from3->bests;
     258             : 
     259    96030905 :                 ASSERT(to->magic == XFS_DIR3_FREE_MAGIC);
     260             :         } else {
     261           0 :                 to->magic = be32_to_cpu(from->hdr.magic);
     262           0 :                 to->firstdb = be32_to_cpu(from->hdr.firstdb);
     263           0 :                 to->nvalid = be32_to_cpu(from->hdr.nvalid);
     264           0 :                 to->nused = be32_to_cpu(from->hdr.nused);
     265           0 :                 to->bests = from->bests;
     266             : 
     267           0 :                 ASSERT(to->magic == XFS_DIR2_FREE_MAGIC);
     268             :         }
     269    96030905 : }
     270             : 
     271             : static void
     272      248369 : xfs_dir2_free_hdr_to_disk(
     273             :         struct xfs_mount                *mp,
     274             :         struct xfs_dir2_free            *to,
     275             :         struct xfs_dir3_icfree_hdr      *from)
     276             : {
     277      248369 :         if (xfs_has_crc(mp)) {
     278      248369 :                 struct xfs_dir3_free    *to3 = (struct xfs_dir3_free *)to;
     279             : 
     280      248369 :                 ASSERT(from->magic == XFS_DIR3_FREE_MAGIC);
     281             : 
     282      248369 :                 to3->hdr.hdr.magic = cpu_to_be32(from->magic);
     283      248369 :                 to3->hdr.firstdb = cpu_to_be32(from->firstdb);
     284      248369 :                 to3->hdr.nvalid = cpu_to_be32(from->nvalid);
     285      248369 :                 to3->hdr.nused = cpu_to_be32(from->nused);
     286             :         } else {
     287           0 :                 ASSERT(from->magic == XFS_DIR2_FREE_MAGIC);
     288             : 
     289           0 :                 to->hdr.magic = cpu_to_be32(from->magic);
     290           0 :                 to->hdr.firstdb = cpu_to_be32(from->firstdb);
     291           0 :                 to->hdr.nvalid = cpu_to_be32(from->nvalid);
     292           0 :                 to->hdr.nused = cpu_to_be32(from->nused);
     293             :         }
     294      248369 : }
     295             : 
     296             : int
     297       59474 : xfs_dir2_free_read(
     298             :         struct xfs_trans        *tp,
     299             :         struct xfs_inode        *dp,
     300             :         xfs_dablk_t             fbno,
     301             :         struct xfs_buf          **bpp)
     302             : {
     303    22804716 :         return __xfs_dir3_free_read(tp, dp, fbno, 0, bpp);
     304             : }
     305             : 
     306             : static int
     307             : xfs_dir2_free_try_read(
     308             :         struct xfs_trans        *tp,
     309             :         struct xfs_inode        *dp,
     310             :         xfs_dablk_t             fbno,
     311             :         struct xfs_buf          **bpp)
     312             : {
     313    39802539 :         return __xfs_dir3_free_read(tp, dp, fbno, XFS_DABUF_MAP_HOLE_OK, bpp);
     314             : }
     315             : 
     316             : static int
     317        9289 : xfs_dir3_free_get_buf(
     318             :         xfs_da_args_t           *args,
     319             :         xfs_dir2_db_t           fbno,
     320             :         struct xfs_buf          **bpp)
     321             : {
     322        9289 :         struct xfs_trans        *tp = args->trans;
     323        9289 :         struct xfs_inode        *dp = args->dp;
     324        9289 :         struct xfs_mount        *mp = dp->i_mount;
     325        9289 :         struct xfs_buf          *bp;
     326        9289 :         int                     error;
     327        9289 :         struct xfs_dir3_icfree_hdr hdr;
     328             : 
     329        9289 :         error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
     330             :                         &bp, XFS_DATA_FORK);
     331        9289 :         if (error)
     332             :                 return error;
     333             : 
     334        9289 :         xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
     335        9289 :         bp->b_ops = &xfs_dir3_free_buf_ops;
     336             : 
     337             :         /*
     338             :          * Initialize the new block to be empty, and remember
     339             :          * its first slot as our empty slot.
     340             :          */
     341        9289 :         memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
     342        9289 :         memset(&hdr, 0, sizeof(hdr));
     343             : 
     344        9289 :         if (xfs_has_crc(mp)) {
     345        9289 :                 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
     346             : 
     347        9289 :                 hdr.magic = XFS_DIR3_FREE_MAGIC;
     348             : 
     349        9289 :                 hdr3->hdr.blkno = cpu_to_be64(xfs_buf_daddr(bp));
     350        9289 :                 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
     351        9289 :                 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
     352             :         } else
     353           0 :                 hdr.magic = XFS_DIR2_FREE_MAGIC;
     354        9289 :         xfs_dir2_free_hdr_to_disk(mp, bp->b_addr, &hdr);
     355        9289 :         *bpp = bp;
     356        9289 :         return 0;
     357             : }
     358             : 
     359             : /*
     360             :  * Log entries from a freespace block.
     361             :  */
     362             : STATIC void
     363    48279696 : xfs_dir2_free_log_bests(
     364             :         struct xfs_da_args      *args,
     365             :         struct xfs_dir3_icfree_hdr *hdr,
     366             :         struct xfs_buf          *bp,
     367             :         int                     first,          /* first entry to log */
     368             :         int                     last)           /* last entry to log */
     369             : {
     370    48279696 :         struct xfs_dir2_free    *free = bp->b_addr;
     371             : 
     372    48279696 :         ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
     373             :                free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
     374    48279696 :         xfs_trans_log_buf(args->trans, bp,
     375    48279696 :                           (char *)&hdr->bests[first] - (char *)free,
     376    48279696 :                           (char *)&hdr->bests[last] - (char *)free +
     377             :                            sizeof(hdr->bests[0]) - 1);
     378    48362712 : }
     379             : 
     380             : /*
     381             :  * Log header from a freespace block.
     382             :  */
     383             : static void
     384      239078 : xfs_dir2_free_log_header(
     385             :         struct xfs_da_args      *args,
     386             :         struct xfs_buf          *bp)
     387             : {
     388             : #ifdef DEBUG
     389      239078 :         xfs_dir2_free_t         *free;          /* freespace structure */
     390             : 
     391      239078 :         free = bp->b_addr;
     392      239078 :         ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
     393             :                free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
     394             : #endif
     395      239078 :         xfs_trans_log_buf(args->trans, bp, 0,
     396      239078 :                           args->geo->free_hdr_size - 1);
     397      239102 : }
     398             : 
     399             : /*
     400             :  * Convert a leaf-format directory to a node-format directory.
     401             :  * We need to change the magic number of the leaf block, and copy
     402             :  * the freespace table out of the leaf block into its own block.
     403             :  */
     404             : int                                             /* error */
     405        9268 : xfs_dir2_leaf_to_node(
     406             :         xfs_da_args_t           *args,          /* operation arguments */
     407             :         struct xfs_buf          *lbp)           /* leaf buffer */
     408             : {
     409        9268 :         xfs_inode_t             *dp;            /* incore directory inode */
     410        9268 :         int                     error;          /* error return value */
     411        9268 :         struct xfs_buf          *fbp;           /* freespace buffer */
     412        9268 :         xfs_dir2_db_t           fdb;            /* freespace block number */
     413        9268 :         __be16                  *from;          /* pointer to freespace entry */
     414        9268 :         int                     i;              /* leaf freespace index */
     415        9268 :         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
     416        9268 :         xfs_dir2_leaf_tail_t    *ltp;           /* leaf tail structure */
     417        9268 :         int                     n;              /* count of live freespc ents */
     418        9268 :         xfs_dir2_data_off_t     off;            /* freespace entry value */
     419        9268 :         xfs_trans_t             *tp;            /* transaction pointer */
     420        9268 :         struct xfs_dir3_icfree_hdr freehdr;
     421             : 
     422        9268 :         trace_xfs_dir2_leaf_to_node(args);
     423             : 
     424        9268 :         dp = args->dp;
     425        9268 :         tp = args->trans;
     426             :         /*
     427             :          * Add a freespace block to the directory.
     428             :          */
     429        9268 :         if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
     430             :                 return error;
     431             :         }
     432        9268 :         ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
     433             :         /*
     434             :          * Get the buffer for the new freespace block.
     435             :          */
     436        9268 :         error = xfs_dir3_free_get_buf(args, fdb, &fbp);
     437        9268 :         if (error)
     438             :                 return error;
     439             : 
     440        9268 :         xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, fbp->b_addr);
     441        9268 :         leaf = lbp->b_addr;
     442        9268 :         ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
     443       18536 :         if (be32_to_cpu(ltp->bestcount) >
     444        9268 :                                 (uint)dp->i_disk_size / args->geo->blksize) {
     445           0 :                 xfs_buf_mark_corrupt(lbp);
     446           0 :                 return -EFSCORRUPTED;
     447             :         }
     448             : 
     449             :         /*
     450             :          * Copy freespace entries from the leaf block to the new block.
     451             :          * Count active entries.
     452             :          */
     453        9268 :         from = xfs_dir2_leaf_bests_p(ltp);
     454       38175 :         for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++) {
     455       28907 :                 off = be16_to_cpu(*from);
     456       28907 :                 if (off != NULLDATAOFF)
     457       28903 :                         n++;
     458       28907 :                 freehdr.bests[i] = cpu_to_be16(off);
     459             :         }
     460             : 
     461             :         /*
     462             :          * Now initialize the freespace block header.
     463             :          */
     464        9268 :         freehdr.nused = n;
     465        9268 :         freehdr.nvalid = be32_to_cpu(ltp->bestcount);
     466             : 
     467        9268 :         xfs_dir2_free_hdr_to_disk(dp->i_mount, fbp->b_addr, &freehdr);
     468        9268 :         xfs_dir2_free_log_bests(args, &freehdr, fbp, 0, freehdr.nvalid - 1);
     469        9268 :         xfs_dir2_free_log_header(args, fbp);
     470             : 
     471             :         /*
     472             :          * Converting the leaf to a leafnode is just a matter of changing the
     473             :          * magic number and the ops. Do the change directly to the buffer as
     474             :          * it's less work (and less code) than decoding the header to host
     475             :          * format and back again.
     476             :          */
     477        9268 :         if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
     478           0 :                 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
     479             :         else
     480        9268 :                 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
     481        9268 :         lbp->b_ops = &xfs_dir3_leafn_buf_ops;
     482        9268 :         xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
     483        9268 :         xfs_dir3_leaf_log_header(args, lbp);
     484        9268 :         xfs_dir3_leaf_check(dp, lbp);
     485        9268 :         return 0;
     486             : }
     487             : 
     488             : /*
     489             :  * Add a leaf entry to a leaf block in a node-form directory.
     490             :  * The other work necessary is done from the caller.
     491             :  */
     492             : static int                                      /* error */
     493    39103889 : xfs_dir2_leafn_add(
     494             :         struct xfs_buf          *bp,            /* leaf buffer */
     495             :         struct xfs_da_args      *args,          /* operation arguments */
     496             :         int                     index)          /* insertion pt for new entry */
     497             : {
     498    39103889 :         struct xfs_dir3_icleaf_hdr leafhdr;
     499    39103889 :         struct xfs_inode        *dp = args->dp;
     500    39103889 :         struct xfs_dir2_leaf    *leaf = bp->b_addr;
     501    39103889 :         struct xfs_dir2_leaf_entry *lep;
     502    39103889 :         struct xfs_dir2_leaf_entry *ents;
     503    39103889 :         int                     compact;        /* compacting stale leaves */
     504    39103889 :         int                     highstale = 0;  /* next stale entry */
     505    39103889 :         int                     lfloghigh;      /* high leaf entry logging */
     506    39103889 :         int                     lfloglow;       /* low leaf entry logging */
     507    39103889 :         int                     lowstale = 0;   /* previous stale entry */
     508             : 
     509    39103889 :         trace_xfs_dir2_leafn_add(args, index);
     510             : 
     511    38971725 :         xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
     512    38961446 :         ents = leafhdr.ents;
     513             : 
     514             :         /*
     515             :          * Quick check just to make sure we are not going to index
     516             :          * into other peoples memory
     517             :          */
     518    38961446 :         if (index < 0) {
     519           0 :                 xfs_buf_mark_corrupt(bp);
     520           0 :                 return -EFSCORRUPTED;
     521             :         }
     522             : 
     523             :         /*
     524             :          * If there are already the maximum number of leaf entries in
     525             :          * the block, if there are no stale entries it won't fit.
     526             :          * Caller will do a split.  If there are stale entries we'll do
     527             :          * a compact.
     528             :          */
     529             : 
     530    38961446 :         if (leafhdr.count == args->geo->leaf_max_ents) {
     531       79895 :                 if (!leafhdr.stale)
     532             :                         return -ENOSPC;
     533        6452 :                 compact = leafhdr.stale > 1;
     534             :         } else
     535             :                 compact = 0;
     536    38888003 :         ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
     537    38888003 :         ASSERT(index == leafhdr.count ||
     538             :                be32_to_cpu(ents[index].hashval) >= args->hashval);
     539             : 
     540    38888003 :         if (args->op_flags & XFS_DA_OP_JUSTCHECK)
     541             :                 return 0;
     542             : 
     543             :         /*
     544             :          * Compact out all but one stale leaf entry.  Leaves behind
     545             :          * the entry closest to index.
     546             :          */
     547    38888003 :         if (compact)
     548        2409 :                 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
     549             :                                          &highstale, &lfloglow, &lfloghigh);
     550    38885594 :         else if (leafhdr.stale) {
     551             :                 /*
     552             :                  * Set impossible logging indices for this case.
     553             :                  */
     554    16689610 :                 lfloglow = leafhdr.count;
     555    16689610 :                 lfloghigh = -1;
     556             :         }
     557             : 
     558             :         /*
     559             :          * Insert the new entry, log everything.
     560             :          */
     561    38888003 :         lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
     562             :                                        highstale, &lfloglow, &lfloghigh);
     563             : 
     564    39072180 :         lep->hashval = cpu_to_be32(args->hashval);
     565    39072180 :         lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
     566             :                                 args->blkno, args->index));
     567             : 
     568    39051734 :         xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
     569    38999662 :         xfs_dir3_leaf_log_header(args, bp);
     570    39026048 :         xfs_dir3_leaf_log_ents(args, &leafhdr, bp, lfloglow, lfloghigh);
     571    39107580 :         xfs_dir3_leaf_check(dp, bp);
     572    39107580 :         return 0;
     573             : }
     574             : 
     575             : #ifdef DEBUG
     576             : static void
     577     2272060 : xfs_dir2_free_hdr_check(
     578             :         struct xfs_inode *dp,
     579             :         struct xfs_buf  *bp,
     580             :         xfs_dir2_db_t   db)
     581             : {
     582     2272060 :         struct xfs_dir3_icfree_hdr hdr;
     583             : 
     584     2272060 :         xfs_dir2_free_hdr_from_disk(dp->i_mount, &hdr, bp->b_addr);
     585             : 
     586     2272060 :         ASSERT((hdr.firstdb % dp->i_mount->m_dir_geo->free_max_bests) == 0);
     587     2272060 :         ASSERT(hdr.firstdb <= db);
     588     2272060 :         ASSERT(db < hdr.firstdb + hdr.nvalid);
     589     2272060 : }
     590             : #else
     591             : #define xfs_dir2_free_hdr_check(dp, bp, db)
     592             : #endif  /* DEBUG */
     593             : 
     594             : /*
     595             :  * Return the last hash value in the leaf.
     596             :  * Stale entries are ok.
     597             :  */
     598             : xfs_dahash_t                                    /* hash value */
     599   301514853 : xfs_dir2_leaf_lasthash(
     600             :         struct xfs_inode *dp,
     601             :         struct xfs_buf  *bp,                    /* leaf buffer */
     602             :         int             *count)                 /* count of entries in leaf */
     603             : {
     604   301514853 :         struct xfs_dir3_icleaf_hdr leafhdr;
     605             : 
     606   301514853 :         xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, bp->b_addr);
     607             : 
     608   300483359 :         ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
     609             :                leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
     610             :                leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
     611             :                leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
     612             : 
     613   300483359 :         if (count)
     614    74588986 :                 *count = leafhdr.count;
     615   300483359 :         if (!leafhdr.count)
     616             :                 return 0;
     617   300483359 :         return be32_to_cpu(leafhdr.ents[leafhdr.count - 1].hashval);
     618             : }
     619             : 
     620             : /*
     621             :  * Look up a leaf entry for space to add a name in a node-format leaf block.
     622             :  * The extrablk in state is a freespace block.
     623             :  */
     624             : STATIC int
     625    40127639 : xfs_dir2_leafn_lookup_for_addname(
     626             :         struct xfs_buf          *bp,            /* leaf buffer */
     627             :         xfs_da_args_t           *args,          /* operation arguments */
     628             :         int                     *indexp,        /* out: leaf entry index */
     629             :         xfs_da_state_t          *state)         /* state to fill in */
     630             : {
     631    40127639 :         struct xfs_buf          *curbp = NULL;  /* current data/free buffer */
     632    40127639 :         xfs_dir2_db_t           curdb = -1;     /* current data block number */
     633    40127639 :         xfs_dir2_db_t           curfdb = -1;    /* current free block number */
     634    40127639 :         xfs_inode_t             *dp;            /* incore directory inode */
     635    40127639 :         int                     error;          /* error return value */
     636    40127639 :         int                     fi;             /* free entry index */
     637    40127639 :         xfs_dir2_free_t         *free = NULL;   /* free block structure */
     638    40127639 :         int                     index;          /* leaf entry index */
     639    40127639 :         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
     640    40127639 :         int                     length;         /* length of new data entry */
     641    40127639 :         xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
     642    40127639 :         xfs_mount_t             *mp;            /* filesystem mount point */
     643    40127639 :         xfs_dir2_db_t           newdb;          /* new data block number */
     644    40127639 :         xfs_dir2_db_t           newfdb;         /* new free block number */
     645    40127639 :         xfs_trans_t             *tp;            /* transaction pointer */
     646    40127639 :         struct xfs_dir3_icleaf_hdr leafhdr;
     647             : 
     648    40127639 :         dp = args->dp;
     649    40127639 :         tp = args->trans;
     650    40127639 :         mp = dp->i_mount;
     651    40127639 :         leaf = bp->b_addr;
     652    40127639 :         xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
     653             : 
     654    40098483 :         xfs_dir3_leaf_check(dp, bp);
     655    40074386 :         ASSERT(leafhdr.count > 0);
     656             : 
     657             :         /*
     658             :          * Look up the hash value in the leaf entries.
     659             :          */
     660    40074386 :         index = xfs_dir2_leaf_search_hash(args, bp);
     661             :         /*
     662             :          * Do we have a buffer coming in?
     663             :          */
     664    40238017 :         if (state->extravalid) {
     665             :                 /* If so, it's a free block buffer, get the block number. */
     666     1159313 :                 curbp = state->extrablk.bp;
     667     1159313 :                 curfdb = state->extrablk.blkno;
     668     1159313 :                 free = curbp->b_addr;
     669     1159313 :                 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
     670             :                        free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
     671             :         }
     672    40238017 :         length = xfs_dir2_data_entsize(mp, args->namelen);
     673             :         /*
     674             :          * Loop over leaf entries with the right hash value.
     675             :          */
     676    40238017 :         for (lep = &leafhdr.ents[index];
     677   166914099 :              index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
     678   126711235 :              lep++, index++) {
     679             :                 /*
     680             :                  * Skip stale leaf entries.
     681             :                  */
     682   128125472 :                 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
     683     1068364 :                         continue;
     684             :                 /*
     685             :                  * Pull the data block number from the entry.
     686             :                  */
     687   127057108 :                 newdb = xfs_dir2_dataptr_to_db(args->geo,
     688             :                                                be32_to_cpu(lep->address));
     689             :                 /*
     690             :                  * For addname, we're looking for a place to put the new entry.
     691             :                  * We want to use a data block with an entry of equal
     692             :                  * hash value to ours if there is one with room.
     693             :                  *
     694             :                  * If this block isn't the data block we already have
     695             :                  * in hand, take a look at it.
     696             :                  */
     697   126921794 :                 if (newdb != curdb) {
     698    10983940 :                         struct xfs_dir3_icfree_hdr freehdr;
     699             : 
     700    10983940 :                         curdb = newdb;
     701             :                         /*
     702             :                          * Convert the data block to the free block
     703             :                          * holding its freespace information.
     704             :                          */
     705    10983940 :                         newfdb = xfs_dir2_db_to_fdb(args->geo, newdb);
     706             :                         /*
     707             :                          * If it's not the one we have in hand, read it in.
     708             :                          */
     709    10983941 :                         if (newfdb != curfdb) {
     710             :                                 /*
     711             :                                  * If we had one before, drop it.
     712             :                                  */
     713     2272059 :                                 if (curbp)
     714      358801 :                                         xfs_trans_brelse(tp, curbp);
     715             : 
     716     2272059 :                                 error = xfs_dir2_free_read(tp, dp,
     717             :                                                 xfs_dir2_db_to_da(args->geo,
     718             :                                                                   newfdb),
     719             :                                                 &curbp);
     720     2272060 :                                 if (error)
     721           0 :                                         return error;
     722     2272060 :                                 free = curbp->b_addr;
     723             : 
     724     2272060 :                                 xfs_dir2_free_hdr_check(dp, curbp, curdb);
     725             :                         }
     726             :                         /*
     727             :                          * Get the index for our entry.
     728             :                          */
     729    10983942 :                         fi = xfs_dir2_db_to_fdindex(args->geo, curdb);
     730             :                         /*
     731             :                          * If it has room, return it.
     732             :                          */
     733    10983942 :                         xfs_dir2_free_hdr_from_disk(mp, &freehdr, free);
     734    10983942 :                         if (XFS_IS_CORRUPT(mp,
     735             :                                            freehdr.bests[fi] ==
     736             :                                            cpu_to_be16(NULLDATAOFF))) {
     737           0 :                                 if (curfdb != newfdb)
     738           0 :                                         xfs_trans_brelse(tp, curbp);
     739           0 :                                 return -EFSCORRUPTED;
     740             :                         }
     741    10983942 :                         curfdb = newfdb;
     742    10983942 :                         if (be16_to_cpu(freehdr.bests[fi]) >= length)
     743     1268916 :                                 goto out;
     744             :                 }
     745             :         }
     746             :         /* Didn't find any space */
     747             :         fi = -1;
     748    40057543 : out:
     749    40057543 :         ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
     750    40057543 :         if (curbp) {
     751             :                 /* Giving back a free block. */
     752     3072572 :                 state->extravalid = 1;
     753     3072572 :                 state->extrablk.bp = curbp;
     754     3072572 :                 state->extrablk.index = fi;
     755     3072572 :                 state->extrablk.blkno = curfdb;
     756             : 
     757             :                 /*
     758             :                  * Important: this magic number is not in the buffer - it's for
     759             :                  * buffer type information and therefore only the free/data type
     760             :                  * matters here, not whether CRCs are enabled or not.
     761             :                  */
     762     3072572 :                 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
     763             :         } else {
     764    36984971 :                 state->extravalid = 0;
     765             :         }
     766             :         /*
     767             :          * Return the index, that will be the insertion point.
     768             :          */
     769    40057543 :         *indexp = index;
     770    40057543 :         return -ENOENT;
     771             : }
     772             : 
     773             : /*
     774             :  * Look up a leaf entry in a node-format leaf block.
     775             :  * The extrablk in state a data block.
     776             :  */
     777             : STATIC int
     778   185454179 : xfs_dir2_leafn_lookup_for_entry(
     779             :         struct xfs_buf          *bp,            /* leaf buffer */
     780             :         xfs_da_args_t           *args,          /* operation arguments */
     781             :         int                     *indexp,        /* out: leaf entry index */
     782             :         xfs_da_state_t          *state)         /* state to fill in */
     783             : {
     784   185454179 :         struct xfs_buf          *curbp = NULL;  /* current data/free buffer */
     785   185454179 :         xfs_dir2_db_t           curdb = -1;     /* current data block number */
     786   185454179 :         xfs_dir2_data_entry_t   *dep;           /* data block entry */
     787   185454179 :         xfs_inode_t             *dp;            /* incore directory inode */
     788   185454179 :         int                     error;          /* error return value */
     789   185454179 :         int                     index;          /* leaf entry index */
     790   185454179 :         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
     791   185454179 :         xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
     792   185454179 :         xfs_mount_t             *mp;            /* filesystem mount point */
     793   185454179 :         xfs_dir2_db_t           newdb;          /* new data block number */
     794   185454179 :         xfs_trans_t             *tp;            /* transaction pointer */
     795   185454179 :         enum xfs_dacmp          cmp;            /* comparison result */
     796   185454179 :         struct xfs_dir3_icleaf_hdr leafhdr;
     797             : 
     798   185454179 :         dp = args->dp;
     799   185454179 :         tp = args->trans;
     800   185454179 :         mp = dp->i_mount;
     801   185454179 :         leaf = bp->b_addr;
     802   185454179 :         xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
     803             : 
     804   184332133 :         xfs_dir3_leaf_check(dp, bp);
     805   184725278 :         if (leafhdr.count <= 0) {
     806           0 :                 xfs_buf_mark_corrupt(bp);
     807           0 :                 return -EFSCORRUPTED;
     808             :         }
     809             : 
     810             :         /*
     811             :          * Look up the hash value in the leaf entries.
     812             :          */
     813   184725278 :         index = xfs_dir2_leaf_search_hash(args, bp);
     814             :         /*
     815             :          * Do we have a buffer coming in?
     816             :          */
     817   185678002 :         if (state->extravalid) {
     818     6042964 :                 curbp = state->extrablk.bp;
     819     6042964 :                 curdb = state->extrablk.blkno;
     820             :         }
     821             :         /*
     822             :          * Loop over leaf entries with the right hash value.
     823             :          */
     824   185678002 :         for (lep = &leafhdr.ents[index];
     825  2588113442 :              index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
     826  2402451168 :              lep++, index++) {
     827             :                 /*
     828             :                  * Skip stale leaf entries.
     829             :                  */
     830  2557363717 :                 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
     831   457757410 :                         continue;
     832             :                 /*
     833             :                  * Pull the data block number from the entry.
     834             :                  */
     835  2099606307 :                 newdb = xfs_dir2_dataptr_to_db(args->geo,
     836             :                                                be32_to_cpu(lep->address));
     837             :                 /*
     838             :                  * Not adding a new entry, so we really want to find
     839             :                  * the name given to us.
     840             :                  *
     841             :                  * If it's a different data block, go get it.
     842             :                  */
     843  2098543450 :                 if (newdb != curdb) {
     844             :                         /*
     845             :                          * If we had a block before that we aren't saving
     846             :                          * for a CI name, drop it
     847             :                          */
     848   210018980 :                         if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
     849     5268720 :                                                 curdb != state->extrablk.blkno))
     850    52478213 :                                 xfs_trans_brelse(tp, curbp);
     851             :                         /*
     852             :                          * If needing the block that is saved with a CI match,
     853             :                          * use it otherwise read in the new data block.
     854             :                          */
     855   209964513 :                         if (args->cmpresult != XFS_CMP_DIFFERENT &&
     856     5268720 :                                         newdb == state->extrablk.blkno) {
     857        6680 :                                 ASSERT(state->extravalid);
     858        6680 :                                 curbp = state->extrablk.bp;
     859             :                         } else {
     860   209957833 :                                 error = xfs_dir3_data_read(tp, dp,
     861             :                                                 xfs_dir2_db_to_da(args->geo,
     862             :                                                                   newdb),
     863             :                                                 0, &curbp);
     864   210712789 :                                 if (error)
     865         365 :                                         return error;
     866             :                         }
     867   210719104 :                         xfs_dir3_data_check(dp, curbp);
     868   210719104 :                         curdb = newdb;
     869             :                 }
     870             :                 /*
     871             :                  * Point to the data entry.
     872             :                  */
     873  2099748177 :                 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
     874  2099748177 :                         xfs_dir2_dataptr_to_off(args->geo,
     875  2099748177 :                                                 be32_to_cpu(lep->address)));
     876             :                 /*
     877             :                  * Compare the entry and if it's an exact match, return
     878             :                  * EEXIST immediately. If it's the first case-insensitive
     879             :                  * match, store the block & inode number and continue looking.
     880             :                  */
     881  2099748177 :                 cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
     882  2099638692 :                 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
     883             :                         /* If there is a CI match block, drop it */
     884   155327743 :                         if (args->cmpresult != XFS_CMP_DIFFERENT &&
     885           0 :                                                 curdb != state->extrablk.blkno)
     886           0 :                                 xfs_trans_brelse(tp, state->extrablk.bp);
     887   155327743 :                         args->cmpresult = cmp;
     888   155327743 :                         args->inumber = be64_to_cpu(dep->inumber);
     889   155327743 :                         args->filetype = xfs_dir2_data_get_ftype(mp, dep);
     890   155241401 :                         *indexp = index;
     891   155241401 :                         state->extravalid = 1;
     892   155241401 :                         state->extrablk.bp = curbp;
     893   155241401 :                         state->extrablk.blkno = curdb;
     894   155241401 :                         state->extrablk.index = (int)((char *)dep -
     895   155241401 :                                                         (char *)curbp->b_addr);
     896   155241401 :                         state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
     897   155241401 :                         curbp->b_ops = &xfs_dir3_data_buf_ops;
     898   155241401 :                         xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
     899   155146382 :                         if (cmp == XFS_CMP_EXACT)
     900             :                                 return -EEXIST;
     901             :                 }
     902             :         }
     903    30749725 :         ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
     904    30749725 :         if (curbp) {
     905     8569845 :                 if (args->cmpresult == XFS_CMP_DIFFERENT) {
     906             :                         /* Giving back last used data block. */
     907     7295237 :                         state->extravalid = 1;
     908     7295237 :                         state->extrablk.bp = curbp;
     909     7295237 :                         state->extrablk.index = -1;
     910     7295237 :                         state->extrablk.blkno = curdb;
     911     7295237 :                         state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
     912     7295237 :                         curbp->b_ops = &xfs_dir3_data_buf_ops;
     913     7295237 :                         xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
     914             :                 } else {
     915             :                         /* If the curbp is not the CI match block, drop it */
     916     1274608 :                         if (state->extrablk.bp != curbp)
     917     1166637 :                                 xfs_trans_brelse(tp, curbp);
     918             :                 }
     919             :         } else {
     920    22179880 :                 state->extravalid = 0;
     921             :         }
     922    30749725 :         *indexp = index;
     923    30749725 :         return -ENOENT;
     924             : }
     925             : 
     926             : /*
     927             :  * Look up a leaf entry in a node-format leaf block.
     928             :  * If this is an addname then the extrablk in state is a freespace block,
     929             :  * otherwise it's a data block.
     930             :  */
     931             : int
     932   225324109 : xfs_dir2_leafn_lookup_int(
     933             :         struct xfs_buf          *bp,            /* leaf buffer */
     934             :         xfs_da_args_t           *args,          /* operation arguments */
     935             :         int                     *indexp,        /* out: leaf entry index */
     936             :         xfs_da_state_t          *state)         /* state to fill in */
     937             : {
     938   225324109 :         if (args->op_flags & XFS_DA_OP_ADDNAME)
     939    40180371 :                 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
     940             :                                                         state);
     941   185143738 :         return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
     942             : }
     943             : 
     944             : /*
     945             :  * Move count leaf entries from source to destination leaf.
     946             :  * Log entries and headers.  Stale entries are preserved.
     947             :  */
     948             : static void
     949      129728 : xfs_dir3_leafn_moveents(
     950             :         xfs_da_args_t                   *args,  /* operation arguments */
     951             :         struct xfs_buf                  *bp_s,  /* source */
     952             :         struct xfs_dir3_icleaf_hdr      *shdr,
     953             :         struct xfs_dir2_leaf_entry      *sents,
     954             :         int                             start_s,/* source leaf index */
     955             :         struct xfs_buf                  *bp_d,  /* destination */
     956             :         struct xfs_dir3_icleaf_hdr      *dhdr,
     957             :         struct xfs_dir2_leaf_entry      *dents,
     958             :         int                             start_d,/* destination leaf index */
     959             :         int                             count)  /* count of leaves to copy */
     960             : {
     961      129728 :         int                             stale;  /* count stale leaves copied */
     962             : 
     963      129728 :         trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
     964             : 
     965             :         /*
     966             :          * Silently return if nothing to do.
     967             :          */
     968      129725 :         if (count == 0)
     969             :                 return;
     970             : 
     971             :         /*
     972             :          * If the destination index is not the end of the current
     973             :          * destination leaf entries, open up a hole in the destination
     974             :          * to hold the new entries.
     975             :          */
     976      129719 :         if (start_d < dhdr->count) {
     977       35930 :                 memmove(&dents[start_d + count], &dents[start_d],
     978             :                         (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
     979       17965 :                 xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d + count,
     980       17965 :                                        count + dhdr->count - 1);
     981             :         }
     982             :         /*
     983             :          * If the source has stale leaves, count the ones in the copy range
     984             :          * so we can update the header correctly.
     985             :          */
     986      129719 :         if (shdr->stale) {
     987             :                 int     i;                      /* temp leaf index */
     988             : 
     989           0 :                 for (i = start_s, stale = 0; i < start_s + count; i++) {
     990           0 :                         if (sents[i].address ==
     991             :                                         cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
     992           0 :                                 stale++;
     993             :                 }
     994             :         } else
     995             :                 stale = 0;
     996             :         /*
     997             :          * Copy the leaf entries from source to destination.
     998             :          */
     999      259438 :         memcpy(&dents[start_d], &sents[start_s],
    1000             :                 count * sizeof(xfs_dir2_leaf_entry_t));
    1001      129719 :         xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d, start_d + count - 1);
    1002             : 
    1003             :         /*
    1004             :          * If there are source entries after the ones we copied,
    1005             :          * delete the ones we copied by sliding the next ones down.
    1006             :          */
    1007      129720 :         if (start_s + count < shdr->count) {
    1008           0 :                 memmove(&sents[start_s], &sents[start_s + count],
    1009             :                         count * sizeof(xfs_dir2_leaf_entry_t));
    1010           0 :                 xfs_dir3_leaf_log_ents(args, shdr, bp_s, start_s,
    1011             :                                        start_s + count - 1);
    1012             :         }
    1013             : 
    1014             :         /*
    1015             :          * Update the headers and log them.
    1016             :          */
    1017      129719 :         shdr->count -= count;
    1018      129719 :         shdr->stale -= stale;
    1019      129719 :         dhdr->count += count;
    1020      129719 :         dhdr->stale += stale;
    1021             : }
    1022             : 
    1023             : /*
    1024             :  * Determine the sort order of two leaf blocks.
    1025             :  * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
    1026             :  */
    1027             : int                                             /* sort order */
    1028      203170 : xfs_dir2_leafn_order(
    1029             :         struct xfs_inode        *dp,
    1030             :         struct xfs_buf          *leaf1_bp,              /* leaf1 buffer */
    1031             :         struct xfs_buf          *leaf2_bp)              /* leaf2 buffer */
    1032             : {
    1033      203170 :         struct xfs_dir2_leaf    *leaf1 = leaf1_bp->b_addr;
    1034      203170 :         struct xfs_dir2_leaf    *leaf2 = leaf2_bp->b_addr;
    1035      203170 :         struct xfs_dir2_leaf_entry *ents1;
    1036      203170 :         struct xfs_dir2_leaf_entry *ents2;
    1037      203170 :         struct xfs_dir3_icleaf_hdr hdr1;
    1038      203170 :         struct xfs_dir3_icleaf_hdr hdr2;
    1039             : 
    1040      203170 :         xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
    1041      203170 :         xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
    1042      203171 :         ents1 = hdr1.ents;
    1043      203171 :         ents2 = hdr2.ents;
    1044             : 
    1045      203171 :         if (hdr1.count > 0 && hdr2.count > 0 &&
    1046      129707 :             (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
    1047      111745 :              be32_to_cpu(ents2[hdr2.count - 1].hashval) <
    1048      111745 :                                 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
    1049       17965 :                 return 1;
    1050             :         return 0;
    1051             : }
    1052             : 
    1053             : /*
    1054             :  * Rebalance leaf entries between two leaf blocks.
    1055             :  * This is actually only called when the second block is new,
    1056             :  * though the code deals with the general case.
    1057             :  * A new entry will be inserted in one of the blocks, and that
    1058             :  * entry is taken into account when balancing.
    1059             :  */
    1060             : static void
    1061       73445 : xfs_dir2_leafn_rebalance(
    1062             :         xfs_da_state_t          *state,         /* btree cursor */
    1063             :         xfs_da_state_blk_t      *blk1,          /* first btree block */
    1064             :         xfs_da_state_blk_t      *blk2)          /* second btree block */
    1065             : {
    1066       73445 :         xfs_da_args_t           *args;          /* operation arguments */
    1067       73445 :         int                     count;          /* count (& direction) leaves */
    1068       73445 :         int                     isleft;         /* new goes in left leaf */
    1069       73445 :         xfs_dir2_leaf_t         *leaf1;         /* first leaf structure */
    1070       73445 :         xfs_dir2_leaf_t         *leaf2;         /* second leaf structure */
    1071       73445 :         int                     mid;            /* midpoint leaf index */
    1072             : #if defined(DEBUG) || defined(XFS_WARN)
    1073       73445 :         int                     oldstale;       /* old count of stale leaves */
    1074             : #endif
    1075       73445 :         int                     oldsum;         /* old total leaf count */
    1076       73445 :         int                     swap_blocks;    /* swapped leaf blocks */
    1077       73445 :         struct xfs_dir2_leaf_entry *ents1;
    1078       73445 :         struct xfs_dir2_leaf_entry *ents2;
    1079       73445 :         struct xfs_dir3_icleaf_hdr hdr1;
    1080       73445 :         struct xfs_dir3_icleaf_hdr hdr2;
    1081       73445 :         struct xfs_inode        *dp = state->args->dp;
    1082             : 
    1083       73445 :         args = state->args;
    1084             :         /*
    1085             :          * If the block order is wrong, swap the arguments.
    1086             :          */
    1087       73445 :         swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
    1088       73445 :         if (swap_blocks)
    1089           0 :                 swap(blk1, blk2);
    1090             : 
    1091       73445 :         leaf1 = blk1->bp->b_addr;
    1092       73445 :         leaf2 = blk2->bp->b_addr;
    1093       73445 :         xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
    1094       73445 :         xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
    1095       73445 :         ents1 = hdr1.ents;
    1096       73445 :         ents2 = hdr2.ents;
    1097             : 
    1098       73445 :         oldsum = hdr1.count + hdr2.count;
    1099             : #if defined(DEBUG) || defined(XFS_WARN)
    1100       73445 :         oldstale = hdr1.stale + hdr2.stale;
    1101             : #endif
    1102       73445 :         mid = oldsum >> 1;
    1103             : 
    1104             :         /*
    1105             :          * If the old leaf count was odd then the new one will be even,
    1106             :          * so we need to divide the new count evenly.
    1107             :          */
    1108       73445 :         if (oldsum & 1) {
    1109           0 :                 xfs_dahash_t    midhash;        /* middle entry hash value */
    1110             : 
    1111           0 :                 if (mid >= hdr1.count)
    1112           0 :                         midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
    1113             :                 else
    1114           0 :                         midhash = be32_to_cpu(ents1[mid].hashval);
    1115           0 :                 isleft = args->hashval <= midhash;
    1116             :         }
    1117             :         /*
    1118             :          * If the old count is even then the new count is odd, so there's
    1119             :          * no preferred side for the new entry.
    1120             :          * Pick the left one.
    1121             :          */
    1122             :         else
    1123             :                 isleft = 1;
    1124             :         /*
    1125             :          * Calculate moved entry count.  Positive means left-to-right,
    1126             :          * negative means right-to-left.  Then move the entries.
    1127             :          */
    1128       73445 :         count = hdr1.count - mid + (isleft == 0);
    1129       73445 :         if (count > 0)
    1130       73445 :                 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
    1131             :                                         hdr1.count - count, blk2->bp,
    1132             :                                         &hdr2, ents2, 0, count);
    1133           0 :         else if (count < 0)
    1134           0 :                 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
    1135             :                                         blk1->bp, &hdr1, ents1,
    1136             :                                         hdr1.count, count);
    1137             : 
    1138       73444 :         ASSERT(hdr1.count + hdr2.count == oldsum);
    1139       73444 :         ASSERT(hdr1.stale + hdr2.stale == oldstale);
    1140             : 
    1141             :         /* log the changes made when moving the entries */
    1142       73444 :         xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf1, &hdr1);
    1143       73445 :         xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf2, &hdr2);
    1144       73447 :         xfs_dir3_leaf_log_header(args, blk1->bp);
    1145       73447 :         xfs_dir3_leaf_log_header(args, blk2->bp);
    1146             : 
    1147       73447 :         xfs_dir3_leaf_check(dp, blk1->bp);
    1148       73447 :         xfs_dir3_leaf_check(dp, blk2->bp);
    1149             : 
    1150             :         /*
    1151             :          * Mark whether we're inserting into the old or new leaf.
    1152             :          */
    1153       73445 :         if (hdr1.count < hdr2.count)
    1154           0 :                 state->inleaf = swap_blocks;
    1155       73445 :         else if (hdr1.count > hdr2.count)
    1156           0 :                 state->inleaf = !swap_blocks;
    1157             :         else
    1158       73445 :                 state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
    1159             :         /*
    1160             :          * Adjust the expected index for insertion.
    1161             :          */
    1162       73445 :         if (!state->inleaf)
    1163       48658 :                 blk2->index = blk1->index - hdr1.count;
    1164             : 
    1165             :         /*
    1166             :          * Finally sanity check just to make sure we are not returning a
    1167             :          * negative index
    1168             :          */
    1169       73445 :         if (blk2->index < 0) {
    1170           0 :                 state->inleaf = 1;
    1171           0 :                 blk2->index = 0;
    1172           0 :                 xfs_alert(dp->i_mount,
    1173             :         "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
    1174             :                         __func__, blk1->index);
    1175             :         }
    1176       73445 : }
    1177             : 
    1178             : static int
    1179    20468563 : xfs_dir3_data_block_free(
    1180             :         xfs_da_args_t           *args,
    1181             :         struct xfs_dir2_data_hdr *hdr,
    1182             :         struct xfs_dir2_free    *free,
    1183             :         xfs_dir2_db_t           fdb,
    1184             :         int                     findex,
    1185             :         struct xfs_buf          *fbp,
    1186             :         int                     longest)
    1187             : {
    1188    20468563 :         int                     logfree = 0;
    1189    20468563 :         struct xfs_dir3_icfree_hdr freehdr;
    1190    20468563 :         struct xfs_inode        *dp = args->dp;
    1191             : 
    1192    20468563 :         xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
    1193    20470086 :         if (hdr) {
    1194             :                 /*
    1195             :                  * Data block is not empty, just set the free entry to the new
    1196             :                  * value.
    1197             :                  */
    1198    20389369 :                 freehdr.bests[findex] = cpu_to_be16(longest);
    1199    20389369 :                 xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
    1200    20389369 :                 return 0;
    1201             :         }
    1202             : 
    1203             :         /* One less used entry in the free table. */
    1204       80717 :         freehdr.nused--;
    1205             : 
    1206             :         /*
    1207             :          * If this was the last entry in the table, we can trim the table size
    1208             :          * back.  There might be other entries at the end referring to
    1209             :          * non-existent data blocks, get those too.
    1210             :          */
    1211       80717 :         if (findex == freehdr.nvalid - 1) {
    1212        1908 :                 int     i;              /* free entry index */
    1213             : 
    1214       42831 :                 for (i = findex - 1; i >= 0; i--) {
    1215       42820 :                         if (freehdr.bests[i] != cpu_to_be16(NULLDATAOFF))
    1216             :                                 break;
    1217             :                 }
    1218        1908 :                 freehdr.nvalid = i + 1;
    1219        1908 :                 logfree = 0;
    1220             :         } else {
    1221             :                 /* Not the last entry, just punch it out.  */
    1222       78809 :                 freehdr.bests[findex] = cpu_to_be16(NULLDATAOFF);
    1223       78809 :                 logfree = 1;
    1224             :         }
    1225             : 
    1226       80717 :         xfs_dir2_free_hdr_to_disk(dp->i_mount, free, &freehdr);
    1227       80717 :         xfs_dir2_free_log_header(args, fbp);
    1228             : 
    1229             :         /*
    1230             :          * If there are no useful entries left in the block, get rid of the
    1231             :          * block if we can.
    1232             :          */
    1233       80717 :         if (!freehdr.nused) {
    1234          11 :                 int error;
    1235             : 
    1236          11 :                 error = xfs_dir2_shrink_inode(args, fdb, fbp);
    1237          11 :                 if (error == 0) {
    1238             :                         fbp = NULL;
    1239             :                         logfree = 0;
    1240           0 :                 } else if (error != -ENOSPC || args->total != 0)
    1241             :                         return error;
    1242             :                 /*
    1243             :                  * It's possible to get ENOSPC if there is no
    1244             :                  * space reservation.  In this case some one
    1245             :                  * else will eventually get rid of this block.
    1246             :                  */
    1247             :         }
    1248             : 
    1249             :         /* Log the free entry that changed, unless we got rid of it.  */
    1250       80706 :         if (logfree)
    1251       78809 :                 xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
    1252             :         return 0;
    1253             : }
    1254             : 
    1255             : /*
    1256             :  * Remove an entry from a node directory.
    1257             :  * This removes the leaf entry and the data entry,
    1258             :  * and updates the free block if necessary.
    1259             :  */
    1260             : static int                                      /* error */
    1261    35248896 : xfs_dir2_leafn_remove(
    1262             :         xfs_da_args_t           *args,          /* operation arguments */
    1263             :         struct xfs_buf          *bp,            /* leaf buffer */
    1264             :         int                     index,          /* leaf entry index */
    1265             :         xfs_da_state_blk_t      *dblk,          /* data block */
    1266             :         int                     *rval)          /* resulting block needs join */
    1267             : {
    1268    35248896 :         struct xfs_da_geometry  *geo = args->geo;
    1269    35248896 :         xfs_dir2_data_hdr_t     *hdr;           /* data block header */
    1270    35248896 :         xfs_dir2_db_t           db;             /* data block number */
    1271    35248896 :         struct xfs_buf          *dbp;           /* data block buffer */
    1272    35248896 :         xfs_dir2_data_entry_t   *dep;           /* data block entry */
    1273    35248896 :         xfs_inode_t             *dp;            /* incore directory inode */
    1274    35248896 :         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
    1275    35248896 :         xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
    1276    35248896 :         int                     longest;        /* longest data free entry */
    1277    35248896 :         int                     off;            /* data block entry offset */
    1278    35248896 :         int                     needlog;        /* need to log data header */
    1279    35248896 :         int                     needscan;       /* need to rescan data frees */
    1280    35248896 :         xfs_trans_t             *tp;            /* transaction pointer */
    1281    35248896 :         struct xfs_dir2_data_free *bf;          /* bestfree table */
    1282    35248896 :         struct xfs_dir3_icleaf_hdr leafhdr;
    1283             : 
    1284    35248896 :         trace_xfs_dir2_leafn_remove(args, index);
    1285             : 
    1286    35236497 :         dp = args->dp;
    1287    35236497 :         tp = args->trans;
    1288    35236497 :         leaf = bp->b_addr;
    1289    35236497 :         xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
    1290             : 
    1291             :         /*
    1292             :          * Point to the entry we're removing.
    1293             :          */
    1294    35244011 :         lep = &leafhdr.ents[index];
    1295             : 
    1296             :         /*
    1297             :          * Extract the data block and offset from the entry.
    1298             :          */
    1299    35244011 :         db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
    1300    35245263 :         ASSERT(dblk->blkno == db);
    1301    35245263 :         off = xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address));
    1302    35245263 :         ASSERT(dblk->index == off);
    1303             : 
    1304             :         /*
    1305             :          * Kill the leaf entry by marking it stale.
    1306             :          * Log the leaf block changes.
    1307             :          */
    1308    35245263 :         leafhdr.stale++;
    1309    35245263 :         xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
    1310    35246935 :         xfs_dir3_leaf_log_header(args, bp);
    1311             : 
    1312    35254613 :         lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
    1313    35254613 :         xfs_dir3_leaf_log_ents(args, &leafhdr, bp, index, index);
    1314             : 
    1315             :         /*
    1316             :          * Make the data entry free.  Keep track of the longest freespace
    1317             :          * in the data block in case it changes.
    1318             :          */
    1319    35259305 :         dbp = dblk->bp;
    1320    35259305 :         hdr = dbp->b_addr;
    1321    35259305 :         dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
    1322    35259305 :         bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
    1323    35247796 :         longest = be16_to_cpu(bf[0].length);
    1324    35247796 :         needlog = needscan = 0;
    1325    35247796 :         xfs_dir2_data_make_free(args, dbp, off,
    1326    35247796 :                 xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
    1327             :                 &needscan);
    1328             :         /*
    1329             :          * Rescan the data block freespaces for bestfree.
    1330             :          * Log the data block header if needed.
    1331             :          */
    1332    35231526 :         if (needscan)
    1333     2489258 :                 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
    1334    35231667 :         if (needlog)
    1335    26194653 :                 xfs_dir2_data_log_header(args, dbp);
    1336    35242450 :         xfs_dir3_data_check(dp, dbp);
    1337             :         /*
    1338             :          * If the longest data block freespace changes, need to update
    1339             :          * the corresponding freeblock entry.
    1340             :          */
    1341    35266812 :         if (longest < be16_to_cpu(bf[0].length)) {
    1342    20474774 :                 int             error;          /* error return value */
    1343    20474774 :                 struct xfs_buf  *fbp;           /* freeblock buffer */
    1344    20474774 :                 xfs_dir2_db_t   fdb;            /* freeblock block number */
    1345    20474774 :                 int             findex;         /* index in freeblock entries */
    1346    20474774 :                 xfs_dir2_free_t *free;          /* freeblock structure */
    1347             : 
    1348             :                 /*
    1349             :                  * Convert the data block number to a free block,
    1350             :                  * read in the free block.
    1351             :                  */
    1352    20474774 :                 fdb = xfs_dir2_db_to_fdb(geo, db);
    1353    20473236 :                 error = xfs_dir2_free_read(tp, dp, xfs_dir2_db_to_da(geo, fdb),
    1354             :                                            &fbp);
    1355    20469574 :                 if (error)
    1356          10 :                         return error;
    1357    20469564 :                 free = fbp->b_addr;
    1358             : #ifdef DEBUG
    1359             :         {
    1360    20469564 :                 struct xfs_dir3_icfree_hdr freehdr;
    1361             : 
    1362    20469564 :                 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
    1363    20469204 :                 ASSERT(freehdr.firstdb == geo->free_max_bests *
    1364             :                         (fdb - xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET)));
    1365             :         }
    1366             : #endif
    1367             :                 /*
    1368             :                  * Calculate which entry we need to fix.
    1369             :                  */
    1370    20469307 :                 findex = xfs_dir2_db_to_fdindex(geo, db);
    1371    20469664 :                 longest = be16_to_cpu(bf[0].length);
    1372             :                 /*
    1373             :                  * If the data block is now empty we can get rid of it
    1374             :                  * (usually).
    1375             :                  */
    1376    20469664 :                 if (longest == geo->blksize - geo->data_entry_offset) {
    1377             :                         /*
    1378             :                          * Try to punch out the data block.
    1379             :                          */
    1380       80718 :                         error = xfs_dir2_shrink_inode(args, db, dbp);
    1381       80718 :                         if (error == 0) {
    1382       80718 :                                 dblk->bp = NULL;
    1383       80718 :                                 hdr = NULL;
    1384             :                         }
    1385             :                         /*
    1386             :                          * We can get ENOSPC if there's no space reservation.
    1387             :                          * In this case just drop the buffer and some one else
    1388             :                          * will eventually get rid of the empty block.
    1389             :                          */
    1390           0 :                         else if (!(error == -ENOSPC && args->total == 0))
    1391             :                                 return error;
    1392             :                 }
    1393             :                 /*
    1394             :                  * If we got rid of the data block, we can eliminate that entry
    1395             :                  * in the free block.
    1396             :                  */
    1397    20469664 :                 error = xfs_dir3_data_block_free(args, hdr, free,
    1398             :                                                  fdb, findex, fbp, longest);
    1399    20470519 :                 if (error)
    1400             :                         return error;
    1401             :         }
    1402             : 
    1403    35262557 :         xfs_dir3_leaf_check(dp, bp);
    1404             :         /*
    1405             :          * Return indication of whether this leaf block is empty enough
    1406             :          * to justify trying to join it with a neighbor.
    1407             :          */
    1408    35262567 :         *rval = (geo->leaf_hdr_size +
    1409    35262567 :                  (uint)sizeof(leafhdr.ents) * (leafhdr.count - leafhdr.stale)) <
    1410    35262567 :                 geo->magicpct;
    1411    35262567 :         return 0;
    1412             : }
    1413             : 
    1414             : /*
    1415             :  * Split the leaf entries in the old block into old and new blocks.
    1416             :  */
    1417             : int                                             /* error */
    1418       73444 : xfs_dir2_leafn_split(
    1419             :         xfs_da_state_t          *state,         /* btree cursor */
    1420             :         xfs_da_state_blk_t      *oldblk,        /* original block */
    1421             :         xfs_da_state_blk_t      *newblk)        /* newly created block */
    1422             : {
    1423       73444 :         xfs_da_args_t           *args;          /* operation arguments */
    1424       73444 :         xfs_dablk_t             blkno;          /* new leaf block number */
    1425       73444 :         int                     error;          /* error return value */
    1426       73444 :         struct xfs_inode        *dp;
    1427             : 
    1428             :         /*
    1429             :          * Allocate space for a new leaf node.
    1430             :          */
    1431       73444 :         args = state->args;
    1432       73444 :         dp = args->dp;
    1433       73444 :         ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
    1434       73444 :         error = xfs_da_grow_inode(args, &blkno);
    1435       73445 :         if (error) {
    1436             :                 return error;
    1437             :         }
    1438             :         /*
    1439             :          * Initialize the new leaf block.
    1440             :          */
    1441       73444 :         error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
    1442             :                                       &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
    1443       73447 :         if (error)
    1444             :                 return error;
    1445             : 
    1446       73447 :         newblk->blkno = blkno;
    1447       73447 :         newblk->magic = XFS_DIR2_LEAFN_MAGIC;
    1448             :         /*
    1449             :          * Rebalance the entries across the two leaves, link the new
    1450             :          * block into the leaves.
    1451             :          */
    1452       73447 :         xfs_dir2_leafn_rebalance(state, oldblk, newblk);
    1453       73446 :         error = xfs_da3_blk_link(state, oldblk, newblk);
    1454       73447 :         if (error) {
    1455             :                 return error;
    1456             :         }
    1457             :         /*
    1458             :          * Insert the new entry in the correct block.
    1459             :          */
    1460       73447 :         if (state->inleaf)
    1461       24788 :                 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
    1462             :         else
    1463       48659 :                 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
    1464             :         /*
    1465             :          * Update last hashval in each block since we added the name.
    1466             :          */
    1467       73447 :         oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
    1468       73446 :         newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
    1469       73444 :         xfs_dir3_leaf_check(dp, oldblk->bp);
    1470       73446 :         xfs_dir3_leaf_check(dp, newblk->bp);
    1471       73446 :         return error;
    1472             : }
    1473             : 
    1474             : /*
    1475             :  * Check a leaf block and its neighbors to see if the block should be
    1476             :  * collapsed into one or the other neighbor.  Always keep the block
    1477             :  * with the smaller block number.
    1478             :  * If the current block is over 50% full, don't try to join it, return 0.
    1479             :  * If the block is empty, fill in the state structure and return 2.
    1480             :  * If it can be collapsed, fill in the state structure and return 1.
    1481             :  * If nothing can be done, return 0.
    1482             :  */
    1483             : int                                             /* error */
    1484     2688373 : xfs_dir2_leafn_toosmall(
    1485             :         xfs_da_state_t          *state,         /* btree cursor */
    1486             :         int                     *action)        /* resulting action to take */
    1487             : {
    1488     2688373 :         xfs_da_state_blk_t      *blk;           /* leaf block */
    1489     2688373 :         xfs_dablk_t             blkno;          /* leaf block number */
    1490     2688373 :         struct xfs_buf          *bp;            /* leaf buffer */
    1491     2688373 :         int                     bytes;          /* bytes in use */
    1492     2688373 :         int                     count;          /* leaf live entry count */
    1493     2688373 :         int                     error;          /* error return value */
    1494     2688373 :         int                     forward;        /* sibling block direction */
    1495     2688373 :         int                     i;              /* sibling counter */
    1496     2688373 :         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
    1497     2688373 :         int                     rval;           /* result from path_shift */
    1498     2688373 :         struct xfs_dir3_icleaf_hdr leafhdr;
    1499     2688373 :         struct xfs_dir2_leaf_entry *ents;
    1500     2688373 :         struct xfs_inode        *dp = state->args->dp;
    1501             : 
    1502             :         /*
    1503             :          * Check for the degenerate case of the block being over 50% full.
    1504             :          * If so, it's not worth even looking to see if we might be able
    1505             :          * to coalesce with a sibling.
    1506             :          */
    1507     2688373 :         blk = &state->path.blk[state->path.active - 1];
    1508     2688351 :         leaf = blk->bp->b_addr;
    1509     2688351 :         xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
    1510     2688334 :         ents = leafhdr.ents;
    1511     2688334 :         xfs_dir3_leaf_check(dp, blk->bp);
    1512             : 
    1513     2688354 :         count = leafhdr.count - leafhdr.stale;
    1514     2688354 :         bytes = state->args->geo->leaf_hdr_size + count * sizeof(ents[0]);
    1515     2688354 :         if (bytes > (state->args->geo->blksize >> 1)) {
    1516             :                 /*
    1517             :                  * Blk over 50%, don't try to join.
    1518             :                  */
    1519           0 :                 *action = 0;
    1520           0 :                 return 0;
    1521             :         }
    1522             :         /*
    1523             :          * Check for the degenerate case of the block being empty.
    1524             :          * If the block is empty, we'll simply delete it, no need to
    1525             :          * coalesce it with a sibling block.  We choose (arbitrarily)
    1526             :          * to merge with the forward block unless it is NULL.
    1527             :          */
    1528     2688354 :         if (count == 0) {
    1529             :                 /*
    1530             :                  * Make altpath point to the block we want to keep and
    1531             :                  * path point to the block we want to drop (this one).
    1532             :                  */
    1533          19 :                 forward = (leafhdr.forw != 0);
    1534          38 :                 memcpy(&state->altpath, &state->path, sizeof(state->path));
    1535          19 :                 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
    1536             :                         &rval);
    1537          19 :                 if (error)
    1538             :                         return error;
    1539          19 :                 *action = rval ? 2 : 0;
    1540          19 :                 return 0;
    1541             :         }
    1542             :         /*
    1543             :          * Examine each sibling block to see if we can coalesce with
    1544             :          * at least 25% free space to spare.  We need to figure out
    1545             :          * whether to merge with the forward or the backward block.
    1546             :          * We prefer coalescing with the lower numbered sibling so as
    1547             :          * to shrink a directory over time.
    1548             :          */
    1549     2688335 :         forward = leafhdr.forw < leafhdr.back;
    1550     7990302 :         for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
    1551     5358153 :                 struct xfs_dir3_icleaf_hdr hdr2;
    1552             : 
    1553     5358153 :                 blkno = forward ? leafhdr.forw : leafhdr.back;
    1554     5358153 :                 if (blkno == 0)
    1555     1844622 :                         continue;
    1556             :                 /*
    1557             :                  * Read the sibling leaf block.
    1558             :                  */
    1559     3513531 :                 error = xfs_dir3_leafn_read(state->args->trans, dp, blkno, &bp);
    1560     3513650 :                 if (error)
    1561           0 :                         return error;
    1562             : 
    1563             :                 /*
    1564             :                  * Count bytes in the two blocks combined.
    1565             :                  */
    1566     3513650 :                 count = leafhdr.count - leafhdr.stale;
    1567     3513650 :                 bytes = state->args->geo->blksize -
    1568     3513650 :                         (state->args->geo->blksize >> 2);
    1569             : 
    1570     3513650 :                 leaf = bp->b_addr;
    1571     3513650 :                 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf);
    1572     3513663 :                 ents = hdr2.ents;
    1573     3513663 :                 count += hdr2.count - hdr2.stale;
    1574     3513663 :                 bytes -= count * sizeof(ents[0]);
    1575             : 
    1576             :                 /*
    1577             :                  * Fits with at least 25% to spare.
    1578             :                  */
    1579     3513663 :                 if (bytes >= 0)
    1580             :                         break;
    1581     3457382 :                 xfs_trans_brelse(state->args->trans, bp);
    1582             :         }
    1583             :         /*
    1584             :          * Didn't like either block, give up.
    1585             :          */
    1586     2688430 :         if (i >= 2) {
    1587     2632149 :                 *action = 0;
    1588     2632149 :                 return 0;
    1589             :         }
    1590             : 
    1591             :         /*
    1592             :          * Make altpath point to the block we want to keep (the lower
    1593             :          * numbered block) and path point to the block we want to drop.
    1594             :          */
    1595      112562 :         memcpy(&state->altpath, &state->path, sizeof(state->path));
    1596       56281 :         if (blkno < blk->blkno)
    1597       23917 :                 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
    1598             :                         &rval);
    1599             :         else
    1600       32364 :                 error = xfs_da3_path_shift(state, &state->path, forward, 0,
    1601             :                         &rval);
    1602       56281 :         if (error) {
    1603             :                 return error;
    1604             :         }
    1605       56281 :         *action = rval ? 0 : 1;
    1606       56281 :         return 0;
    1607             : }
    1608             : 
    1609             : /*
    1610             :  * Move all the leaf entries from drop_blk to save_blk.
    1611             :  * This is done as part of a join operation.
    1612             :  */
    1613             : void
    1614       56281 : xfs_dir2_leafn_unbalance(
    1615             :         xfs_da_state_t          *state,         /* cursor */
    1616             :         xfs_da_state_blk_t      *drop_blk,      /* dead block */
    1617             :         xfs_da_state_blk_t      *save_blk)      /* surviving block */
    1618             : {
    1619       56281 :         xfs_da_args_t           *args;          /* operation arguments */
    1620       56281 :         xfs_dir2_leaf_t         *drop_leaf;     /* dead leaf structure */
    1621       56281 :         xfs_dir2_leaf_t         *save_leaf;     /* surviving leaf structure */
    1622       56281 :         struct xfs_dir3_icleaf_hdr savehdr;
    1623       56281 :         struct xfs_dir3_icleaf_hdr drophdr;
    1624       56281 :         struct xfs_dir2_leaf_entry *sents;
    1625       56281 :         struct xfs_dir2_leaf_entry *dents;
    1626       56281 :         struct xfs_inode        *dp = state->args->dp;
    1627             : 
    1628       56281 :         args = state->args;
    1629       56281 :         ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
    1630       56281 :         ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
    1631       56281 :         drop_leaf = drop_blk->bp->b_addr;
    1632       56281 :         save_leaf = save_blk->bp->b_addr;
    1633             : 
    1634       56281 :         xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &savehdr, save_leaf);
    1635       56281 :         xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &drophdr, drop_leaf);
    1636       56281 :         sents = savehdr.ents;
    1637       56281 :         dents = drophdr.ents;
    1638             : 
    1639             :         /*
    1640             :          * If there are any stale leaf entries, take this opportunity
    1641             :          * to purge them.
    1642             :          */
    1643       56281 :         if (drophdr.stale)
    1644       50800 :                 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
    1645       56281 :         if (savehdr.stale)
    1646       56112 :                 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
    1647             : 
    1648             :         /*
    1649             :          * Move the entries from drop to the appropriate end of save.
    1650             :          */
    1651       56281 :         drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
    1652       56281 :         if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
    1653       17965 :                 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
    1654             :                                         save_blk->bp, &savehdr, sents, 0,
    1655       17965 :                                         drophdr.count);
    1656             :         else
    1657       38316 :                 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
    1658             :                                         save_blk->bp, &savehdr, sents,
    1659       38316 :                                         savehdr.count, drophdr.count);
    1660       56281 :         save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
    1661             : 
    1662             :         /* log the changes made when moving the entries */
    1663       56281 :         xfs_dir2_leaf_hdr_to_disk(dp->i_mount, save_leaf, &savehdr);
    1664       56281 :         xfs_dir2_leaf_hdr_to_disk(dp->i_mount, drop_leaf, &drophdr);
    1665       56281 :         xfs_dir3_leaf_log_header(args, save_blk->bp);
    1666       56281 :         xfs_dir3_leaf_log_header(args, drop_blk->bp);
    1667             : 
    1668       56281 :         xfs_dir3_leaf_check(dp, save_blk->bp);
    1669       56281 :         xfs_dir3_leaf_check(dp, drop_blk->bp);
    1670       56281 : }
    1671             : 
    1672             : /*
    1673             :  * Add a new data block to the directory at the free space index that the caller
    1674             :  * has specified.
    1675             :  */
    1676             : static int
    1677      149126 : xfs_dir2_node_add_datablk(
    1678             :         struct xfs_da_args      *args,
    1679             :         struct xfs_da_state_blk *fblk,
    1680             :         xfs_dir2_db_t           *dbno,
    1681             :         struct xfs_buf          **dbpp,
    1682             :         struct xfs_buf          **fbpp,
    1683             :         struct xfs_dir3_icfree_hdr *hdr,
    1684             :         int                     *findex)
    1685             : {
    1686      149126 :         struct xfs_inode        *dp = args->dp;
    1687      149126 :         struct xfs_trans        *tp = args->trans;
    1688      149126 :         struct xfs_mount        *mp = dp->i_mount;
    1689      149126 :         struct xfs_dir2_data_free *bf;
    1690      149126 :         xfs_dir2_db_t           fbno;
    1691      149126 :         struct xfs_buf          *fbp;
    1692      149126 :         struct xfs_buf          *dbp;
    1693      149126 :         int                     error;
    1694             : 
    1695             :         /* Not allowed to allocate, return failure. */
    1696      149126 :         if (args->total == 0)
    1697             :                 return -ENOSPC;
    1698             : 
    1699             :         /* Allocate and initialize the new data block.  */
    1700      149126 :         error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno);
    1701      149129 :         if (error)
    1702             :                 return error;
    1703      149126 :         error = xfs_dir3_data_init(args, *dbno, &dbp);
    1704      149120 :         if (error)
    1705             :                 return error;
    1706             : 
    1707             :         /*
    1708             :          * Get the freespace block corresponding to the data block
    1709             :          * that was just allocated.
    1710             :          */
    1711      149122 :         fbno = xfs_dir2_db_to_fdb(args->geo, *dbno);
    1712      149095 :         error = xfs_dir2_free_try_read(tp, dp,
    1713             :                                xfs_dir2_db_to_da(args->geo, fbno), &fbp);
    1714      149117 :         if (error)
    1715             :                 return error;
    1716             : 
    1717             :         /*
    1718             :          * If there wasn't a freespace block, the read will
    1719             :          * return a NULL fbp.  Allocate and initialize a new one.
    1720             :          */
    1721      149117 :         if (!fbp) {
    1722          21 :                 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno);
    1723          21 :                 if (error)
    1724             :                         return error;
    1725             : 
    1726          21 :                 if (XFS_IS_CORRUPT(mp,
    1727             :                                    xfs_dir2_db_to_fdb(args->geo, *dbno) !=
    1728             :                                    fbno)) {
    1729           0 :                         xfs_alert(mp,
    1730             : "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld",
    1731             :                                 __func__, (unsigned long long)dp->i_ino,
    1732             :                                 (long long)xfs_dir2_db_to_fdb(args->geo, *dbno),
    1733             :                                 (long long)*dbno, (long long)fbno);
    1734           0 :                         if (fblk) {
    1735           0 :                                 xfs_alert(mp,
    1736             :                         " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
    1737             :                                         fblk, (unsigned long long)fblk->blkno,
    1738             :                                         fblk->index, fblk->magic);
    1739             :                         } else {
    1740           0 :                                 xfs_alert(mp, " ... fblk is NULL");
    1741             :                         }
    1742           0 :                         return -EFSCORRUPTED;
    1743             :                 }
    1744             : 
    1745             :                 /* Get a buffer for the new block. */
    1746          21 :                 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
    1747          21 :                 if (error)
    1748             :                         return error;
    1749          21 :                 xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
    1750             : 
    1751             :                 /* Remember the first slot as our empty slot. */
    1752          21 :                 hdr->firstdb = (fbno - xfs_dir2_byte_to_db(args->geo,
    1753          21 :                                                         XFS_DIR2_FREE_OFFSET)) *
    1754          21 :                                 args->geo->free_max_bests;
    1755             :         } else {
    1756      149096 :                 xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
    1757             :         }
    1758             : 
    1759             :         /* Set the freespace block index from the data block number. */
    1760      149111 :         *findex = xfs_dir2_db_to_fdindex(args->geo, *dbno);
    1761             : 
    1762             :         /* Extend the freespace table if the new data block is off the end. */
    1763      149111 :         if (*findex >= hdr->nvalid) {
    1764      148617 :                 ASSERT(*findex < args->geo->free_max_bests);
    1765      148617 :                 hdr->nvalid = *findex + 1;
    1766      148617 :                 hdr->bests[*findex] = cpu_to_be16(NULLDATAOFF);
    1767             :         }
    1768             : 
    1769             :         /*
    1770             :          * If this entry was for an empty data block (this should always be
    1771             :          * true) then update the header.
    1772             :          */
    1773      149111 :         if (hdr->bests[*findex] == cpu_to_be16(NULLDATAOFF)) {
    1774      149123 :                 hdr->nused++;
    1775      149123 :                 xfs_dir2_free_hdr_to_disk(mp, fbp->b_addr, hdr);
    1776      149097 :                 xfs_dir2_free_log_header(args, fbp);
    1777             :         }
    1778             : 
    1779             :         /* Update the freespace value for the new block in the table. */
    1780      149105 :         bf = xfs_dir2_data_bestfree_p(mp, dbp->b_addr);
    1781      149108 :         hdr->bests[*findex] = bf[0].length;
    1782             : 
    1783      149108 :         *dbpp = dbp;
    1784      149108 :         *fbpp = fbp;
    1785      149108 :         return 0;
    1786             : }
    1787             : 
    1788             : static int
    1789    38846550 : xfs_dir2_node_find_freeblk(
    1790             :         struct xfs_da_args      *args,
    1791             :         struct xfs_da_state_blk *fblk,
    1792             :         xfs_dir2_db_t           *dbnop,
    1793             :         struct xfs_buf          **fbpp,
    1794             :         struct xfs_dir3_icfree_hdr *hdr,
    1795             :         int                     *findexp,
    1796             :         int                     length)
    1797             : {
    1798    38846550 :         struct xfs_inode        *dp = args->dp;
    1799    38846550 :         struct xfs_trans        *tp = args->trans;
    1800    38846550 :         struct xfs_buf          *fbp = NULL;
    1801    38846550 :         xfs_dir2_db_t           firstfbno;
    1802    38846550 :         xfs_dir2_db_t           lastfbno;
    1803    38846550 :         xfs_dir2_db_t           ifbno = -1;
    1804    38846550 :         xfs_dir2_db_t           dbno = -1;
    1805    38846550 :         xfs_dir2_db_t           fbno;
    1806    38846550 :         xfs_fileoff_t           fo;
    1807    38846550 :         int                     findex = 0;
    1808    38846550 :         int                     error;
    1809             : 
    1810             :         /*
    1811             :          * If we came in with a freespace block that means that lookup
    1812             :          * found an entry with our hash value.  This is the freespace
    1813             :          * block for that data entry.
    1814             :          */
    1815    38846550 :         if (fblk) {
    1816     1913259 :                 fbp = fblk->bp;
    1817     1913259 :                 findex = fblk->index;
    1818     1913259 :                 xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
    1819     1913259 :                 if (findex >= 0) {
    1820             :                         /* caller already found the freespace for us. */
    1821      339197 :                         ASSERT(findex < hdr->nvalid);
    1822      339197 :                         ASSERT(be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF);
    1823      339197 :                         ASSERT(be16_to_cpu(hdr->bests[findex]) >= length);
    1824      339197 :                         dbno = hdr->firstdb + findex;
    1825      339197 :                         goto found_block;
    1826             :                 }
    1827             : 
    1828             :                 /*
    1829             :                  * The data block looked at didn't have enough room.
    1830             :                  * We'll start at the beginning of the freespace entries.
    1831             :                  */
    1832     1574062 :                 ifbno = fblk->blkno;
    1833     1574062 :                 xfs_trans_brelse(tp, fbp);
    1834     1574062 :                 fbp = NULL;
    1835     1574062 :                 fblk->bp = NULL;
    1836             :         }
    1837             : 
    1838             :         /*
    1839             :          * If we don't have a data block yet, we're going to scan the freespace
    1840             :          * data for a data block with enough free space in it.
    1841             :          */
    1842    38507353 :         error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK);
    1843    38746046 :         if (error)
    1844             :                 return error;
    1845    38726950 :         lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
    1846    38654344 :         firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET);
    1847             : 
    1848    40900007 :         for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) {
    1849             :                 /* If it's ifbno we already looked at it. */
    1850    40750883 :                 if (fbno == ifbno)
    1851     1103801 :                         continue;
    1852             : 
    1853             :                 /*
    1854             :                  * Read the block.  There can be holes in the freespace blocks,
    1855             :                  * so this might not succeed.  This should be really rare, so
    1856             :                  * there's no reason to avoid it.
    1857             :                  */
    1858    39647082 :                 error = xfs_dir2_free_try_read(tp, dp,
    1859             :                                 xfs_dir2_db_to_da(args->geo, fbno),
    1860             :                                 &fbp);
    1861    39713022 :                 if (error)
    1862          91 :                         return error;
    1863    39712931 :                 if (!fbp)
    1864           0 :                         continue;
    1865             : 
    1866    39712931 :                 xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
    1867             : 
    1868             :                 /* Scan the free entry array for a large enough free space. */
    1869  2355475107 :                 for (findex = hdr->nvalid - 1; findex >= 0; findex--) {
    1870  2354328540 :                         if (be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF &&
    1871  2351326910 :                             be16_to_cpu(hdr->bests[findex]) >= length) {
    1872    38523922 :                                 dbno = hdr->firstdb + findex;
    1873    38523922 :                                 goto found_block;
    1874             :                         }
    1875             :                 }
    1876             : 
    1877             :                 /* Didn't find free space, go on to next free block */
    1878     1146567 :                 xfs_trans_brelse(tp, fbp);
    1879             :         }
    1880             : 
    1881      149124 : found_block:
    1882    39012243 :         *dbnop = dbno;
    1883    39012243 :         *fbpp = fbp;
    1884    39012243 :         *findexp = findex;
    1885    39012243 :         return 0;
    1886             : }
    1887             : 
    1888             : /*
    1889             :  * Add the data entry for a node-format directory name addition.
    1890             :  * The leaf entry is added in xfs_dir2_leafn_add.
    1891             :  * We may enter with a freespace block that the lookup found.
    1892             :  */
    1893             : static int
    1894    38941964 : xfs_dir2_node_addname_int(
    1895             :         struct xfs_da_args      *args,          /* operation arguments */
    1896             :         struct xfs_da_state_blk *fblk)          /* optional freespace block */
    1897             : {
    1898    38941964 :         struct xfs_dir2_data_unused *dup;       /* data unused entry pointer */
    1899    38941964 :         struct xfs_dir2_data_entry *dep;        /* data entry pointer */
    1900    38941964 :         struct xfs_dir2_data_hdr *hdr;          /* data block header */
    1901    38941964 :         struct xfs_dir2_data_free *bf;
    1902    38941964 :         struct xfs_trans        *tp = args->trans;
    1903    38941964 :         struct xfs_inode        *dp = args->dp;
    1904    38941964 :         struct xfs_dir3_icfree_hdr freehdr;
    1905    38941964 :         struct xfs_buf          *dbp;           /* data block buffer */
    1906    38941964 :         struct xfs_buf          *fbp;           /* freespace buffer */
    1907    38941964 :         xfs_dir2_data_aoff_t    aoff;
    1908    38941964 :         xfs_dir2_db_t           dbno;           /* data block number */
    1909    38941964 :         int                     error;          /* error return value */
    1910    38941964 :         int                     findex;         /* freespace entry index */
    1911    38941964 :         int                     length;         /* length of the new entry */
    1912    38941964 :         int                     logfree = 0;    /* need to log free entry */
    1913    38941964 :         int                     needlog = 0;    /* need to log data header */
    1914    38941964 :         int                     needscan = 0;   /* need to rescan data frees */
    1915    38941964 :         __be16                  *tagp;          /* data entry tag pointer */
    1916             : 
    1917    38941964 :         length = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
    1918    38941964 :         error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &freehdr,
    1919             :                                            &findex, length);
    1920    38956063 :         if (error)
    1921             :                 return error;
    1922             : 
    1923             :         /*
    1924             :          * Now we know if we must allocate blocks, so if we are checking whether
    1925             :          * we can insert without allocation then we can return now.
    1926             :          */
    1927    38955972 :         if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
    1928           0 :                 if (dbno == -1)
    1929             :                         return -ENOSPC;
    1930           0 :                 return 0;
    1931             :         }
    1932             : 
    1933             :         /*
    1934             :          * If we don't have a data block, we need to allocate one and make
    1935             :          * the freespace entries refer to it.
    1936             :          */
    1937    38955972 :         if (dbno == -1) {
    1938             :                 /* we're going to have to log the free block index later */
    1939      149120 :                 logfree = 1;
    1940      149120 :                 error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp,
    1941             :                                                   &freehdr, &findex);
    1942             :         } else {
    1943             :                 /* Read the data block in. */
    1944    38806852 :                 error = xfs_dir3_data_read(tp, dp,
    1945             :                                            xfs_dir2_db_to_da(args->geo, dbno),
    1946             :                                            0, &dbp);
    1947             :         }
    1948    39112804 :         if (error)
    1949             :                 return error;
    1950             : 
    1951             :         /* setup for data block up now */
    1952    39112744 :         hdr = dbp->b_addr;
    1953    39112744 :         bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
    1954    39062391 :         ASSERT(be16_to_cpu(bf[0].length) >= length);
    1955             : 
    1956             :         /* Point to the existing unused space. */
    1957    39062391 :         dup = (xfs_dir2_data_unused_t *)
    1958    39062391 :               ((char *)hdr + be16_to_cpu(bf[0].offset));
    1959             : 
    1960             :         /* Mark the first part of the unused space, inuse for us. */
    1961    39062391 :         aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
    1962    39062391 :         error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
    1963             :                         &needlog, &needscan);
    1964    38967044 :         if (error) {
    1965           0 :                 xfs_trans_brelse(tp, dbp);
    1966           0 :                 return error;
    1967             :         }
    1968             : 
    1969             :         /* Fill in the new entry and log it. */
    1970    38967044 :         dep = (xfs_dir2_data_entry_t *)dup;
    1971    38967044 :         dep->inumber = cpu_to_be64(args->inumber);
    1972    38967044 :         dep->namelen = args->namelen;
    1973    77934088 :         memcpy(dep->name, args->name, dep->namelen);
    1974    38967044 :         xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
    1975    38828931 :         tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
    1976    38828327 :         *tagp = cpu_to_be16((char *)dep - (char *)hdr);
    1977    38828327 :         xfs_dir2_data_log_entry(args, dbp, dep);
    1978             : 
    1979             :         /* Rescan the freespace and log the data block if needed. */
    1980    39054933 :         if (needscan)
    1981    10789369 :                 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
    1982    39062722 :         if (needlog)
    1983    39055260 :                 xfs_dir2_data_log_header(args, dbp);
    1984             : 
    1985             :         /* If the freespace block entry is now wrong, update it. */
    1986    39078710 :         if (freehdr.bests[findex] != bf[0].length) {
    1987    27847803 :                 freehdr.bests[findex] = bf[0].length;
    1988    27847803 :                 logfree = 1;
    1989             :         }
    1990             : 
    1991             :         /* Log the freespace entry if needed. */
    1992    39078710 :         if (logfree)
    1993    27847803 :                 xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
    1994             : 
    1995             :         /* Return the data block and offset in args. */
    1996    39104089 :         args->blkno = (xfs_dablk_t)dbno;
    1997    39104089 :         args->index = be16_to_cpu(*tagp);
    1998    39104089 :         return 0;
    1999             : }
    2000             : 
    2001             : /*
    2002             :  * Top-level node form directory addname routine.
    2003             :  */
    2004             : int                                             /* error */
    2005    39022376 : xfs_dir2_node_addname(
    2006             :         xfs_da_args_t           *args)          /* operation arguments */
    2007             : {
    2008    39022376 :         xfs_da_state_blk_t      *blk;           /* leaf block for insert */
    2009    39022376 :         int                     error;          /* error return value */
    2010    39022376 :         int                     rval;           /* sub-return value */
    2011    39022376 :         xfs_da_state_t          *state;         /* btree cursor */
    2012             : 
    2013    39022376 :         trace_xfs_dir2_node_addname(args);
    2014             : 
    2015             :         /*
    2016             :          * Allocate and initialize the state (btree cursor).
    2017             :          */
    2018    38833219 :         state = xfs_da_state_alloc(args);
    2019             :         /*
    2020             :          * Look up the name.  We're not supposed to find it, but
    2021             :          * this gives us the insertion point.
    2022             :          */
    2023    39024356 :         error = xfs_da3_node_lookup_int(state, &rval);
    2024    38858625 :         if (error)
    2025           9 :                 rval = error;
    2026    38858625 :         if (rval != -ENOENT) {
    2027           9 :                 goto done;
    2028             :         }
    2029             :         /*
    2030             :          * Add the data entry to a data block.
    2031             :          * Extravalid is set to a freeblock found by lookup.
    2032             :          */
    2033    38858616 :         rval = xfs_dir2_node_addname_int(args,
    2034    38858616 :                 state->extravalid ? &state->extrablk : NULL);
    2035    39102174 :         if (rval) {
    2036         151 :                 goto done;
    2037             :         }
    2038    39102023 :         blk = &state->path.blk[state->path.active - 1];
    2039    39090886 :         ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
    2040             :         /*
    2041             :          * Add the new leaf entry.
    2042             :          */
    2043    39090886 :         rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
    2044    39094432 :         if (rval == 0) {
    2045             :                 /*
    2046             :                  * It worked, fix the hash values up the btree.
    2047             :                  */
    2048    39020991 :                 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
    2049    39022815 :                         xfs_da3_fixhashpath(state, &state->path);
    2050             :         } else {
    2051             :                 /*
    2052             :                  * It didn't work, we need to split the leaf block.
    2053             :                  */
    2054       73441 :                 if (args->total == 0) {
    2055           0 :                         ASSERT(rval == -ENOSPC);
    2056           0 :                         goto done;
    2057             :                 }
    2058             :                 /*
    2059             :                  * Split the leaf block and insert the new entry.
    2060             :                  */
    2061       73441 :                 rval = xfs_da3_split(state);
    2062             :         }
    2063    39014458 : done:
    2064    39014458 :         xfs_da_state_free(state);
    2065    38950809 :         return rval;
    2066             : }
    2067             : 
    2068             : /*
    2069             :  * Lookup an entry in a node-format directory.
    2070             :  * All the real work happens in xfs_da3_node_lookup_int.
    2071             :  * The only real output is the inode number of the entry.
    2072             :  */
    2073             : int                                             /* error */
    2074   143685260 : xfs_dir2_node_lookup(
    2075             :         xfs_da_args_t   *args)                  /* operation arguments */
    2076             : {
    2077   143685260 :         int             error;                  /* error return value */
    2078   143685260 :         int             i;                      /* btree level */
    2079   143685260 :         int             rval;                   /* operation return value */
    2080   143685260 :         xfs_da_state_t  *state;                 /* btree cursor */
    2081             : 
    2082   143685260 :         trace_xfs_dir2_node_lookup(args);
    2083             : 
    2084             :         /*
    2085             :          * Allocate and initialize the btree cursor.
    2086             :          */
    2087   142572907 :         state = xfs_da_state_alloc(args);
    2088             : 
    2089             :         /*
    2090             :          * Fill in the path to the entry in the cursor.
    2091             :          */
    2092   143734254 :         error = xfs_da3_node_lookup_int(state, &rval);
    2093   143706569 :         if (error)
    2094        1102 :                 rval = error;
    2095   143705467 :         else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
    2096             :                 /* If a CI match, dup the actual name and return -EEXIST */
    2097      373053 :                 xfs_dir2_data_entry_t   *dep;
    2098             : 
    2099      373053 :                 dep = (xfs_dir2_data_entry_t *)
    2100      373053 :                         ((char *)state->extrablk.bp->b_addr +
    2101      373053 :                                                  state->extrablk.index);
    2102      373053 :                 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
    2103             :         }
    2104             :         /*
    2105             :          * Release the btree blocks and leaf block.
    2106             :          */
    2107   445967727 :         for (i = 0; i < state->path.active; i++) {
    2108   301776508 :                 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
    2109   302549565 :                 state->path.blk[i].bp = NULL;
    2110             :         }
    2111             :         /*
    2112             :          * Release the data block if we have it.
    2113             :          */
    2114   144191219 :         if (state->extravalid && state->extrablk.bp) {
    2115   121799188 :                 xfs_trans_brelse(args->trans, state->extrablk.bp);
    2116   121191443 :                 state->extrablk.bp = NULL;
    2117             :         }
    2118   143583474 :         xfs_da_state_free(state);
    2119   143630898 :         return rval;
    2120             : }
    2121             : 
    2122             : /*
    2123             :  * Remove an entry from a node-format directory.
    2124             :  */
    2125             : int                                             /* error */
    2126    35239996 : xfs_dir2_node_removename(
    2127             :         struct xfs_da_args      *args)          /* operation arguments */
    2128             : {
    2129    35239996 :         struct xfs_da_state_blk *blk;           /* leaf block */
    2130    35239996 :         int                     error;          /* error return value */
    2131    35239996 :         int                     rval;           /* operation return value */
    2132    35239996 :         struct xfs_da_state     *state;         /* btree cursor */
    2133             : 
    2134    35239996 :         trace_xfs_dir2_node_removename(args);
    2135             : 
    2136             :         /*
    2137             :          * Allocate and initialize the btree cursor.
    2138             :          */
    2139    35221235 :         state = xfs_da_state_alloc(args);
    2140             : 
    2141             :         /* Look up the entry we're deleting, set up the cursor. */
    2142    35254605 :         error = xfs_da3_node_lookup_int(state, &rval);
    2143    35254678 :         if (error)
    2144           2 :                 goto out_free;
    2145             : 
    2146             :         /* Didn't find it, upper layer screwed up. */
    2147    35254676 :         if (rval != -EEXIST) {
    2148           2 :                 error = rval;
    2149           2 :                 goto out_free;
    2150             :         }
    2151             : 
    2152    35254674 :         blk = &state->path.blk[state->path.active - 1];
    2153    35253614 :         ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
    2154    35253614 :         ASSERT(state->extravalid);
    2155             :         /*
    2156             :          * Remove the leaf and data entries.
    2157             :          * Extrablk refers to the data block.
    2158             :          */
    2159    35253614 :         error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
    2160             :                 &state->extrablk, &rval);
    2161    35257748 :         if (error)
    2162          10 :                 goto out_free;
    2163             :         /*
    2164             :          * Fix the hash values up the btree.
    2165             :          */
    2166    35257738 :         xfs_da3_fixhashpath(state, &state->path);
    2167             :         /*
    2168             :          * If we need to join leaf blocks, do it.
    2169             :          */
    2170    35256494 :         if (rval && state->path.active > 1)
    2171     2688448 :                 error = xfs_da3_join(state);
    2172             :         /*
    2173             :          * If no errors so far, try conversion to leaf format.
    2174             :          */
    2175    35256481 :         if (!error)
    2176    35256481 :                 error = xfs_dir2_node_to_leaf(state);
    2177           0 : out_free:
    2178    35248821 :         xfs_da_state_free(state);
    2179    35253272 :         return error;
    2180             : }
    2181             : 
    2182             : /*
    2183             :  * Replace an entry's inode number in a node-format directory.
    2184             :  */
    2185             : int                                             /* error */
    2186      688907 : xfs_dir2_node_replace(
    2187             :         xfs_da_args_t           *args)          /* operation arguments */
    2188             : {
    2189      688907 :         xfs_da_state_blk_t      *blk;           /* leaf block */
    2190      688907 :         xfs_dir2_data_hdr_t     *hdr;           /* data block header */
    2191      688907 :         xfs_dir2_data_entry_t   *dep;           /* data entry changed */
    2192      688907 :         int                     error;          /* error return value */
    2193      688907 :         int                     i;              /* btree level */
    2194      688907 :         xfs_ino_t               inum;           /* new inode number */
    2195      688907 :         int                     ftype;          /* new file type */
    2196      688907 :         int                     rval;           /* internal return value */
    2197      688907 :         xfs_da_state_t          *state;         /* btree cursor */
    2198             : 
    2199      688907 :         trace_xfs_dir2_node_replace(args);
    2200             : 
    2201             :         /*
    2202             :          * Allocate and initialize the btree cursor.
    2203             :          */
    2204      688889 :         state = xfs_da_state_alloc(args);
    2205             : 
    2206             :         /*
    2207             :          * We have to save new inode number and ftype since
    2208             :          * xfs_da3_node_lookup_int() is going to overwrite them
    2209             :          */
    2210      688920 :         inum = args->inumber;
    2211      688920 :         ftype = args->filetype;
    2212             : 
    2213             :         /*
    2214             :          * Lookup the entry to change in the btree.
    2215             :          */
    2216      688920 :         error = xfs_da3_node_lookup_int(state, &rval);
    2217      688919 :         if (error) {
    2218         192 :                 rval = error;
    2219             :         }
    2220             :         /*
    2221             :          * It should be found, since the vnodeops layer has looked it up
    2222             :          * and locked it.  But paranoia is good.
    2223             :          */
    2224      688919 :         if (rval == -EEXIST) {
    2225      688725 :                 struct xfs_dir3_icleaf_hdr      leafhdr;
    2226             : 
    2227             :                 /*
    2228             :                  * Find the leaf entry.
    2229             :                  */
    2230      688725 :                 blk = &state->path.blk[state->path.active - 1];
    2231      688722 :                 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
    2232      688722 :                 ASSERT(state->extravalid);
    2233             : 
    2234      688722 :                 xfs_dir2_leaf_hdr_from_disk(state->mp, &leafhdr,
    2235      688722 :                                             blk->bp->b_addr);
    2236             :                 /*
    2237             :                  * Point to the data entry.
    2238             :                  */
    2239      688721 :                 hdr = state->extrablk.bp->b_addr;
    2240      688721 :                 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
    2241             :                        hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
    2242      688721 :                 dep = (xfs_dir2_data_entry_t *)
    2243             :                       ((char *)hdr +
    2244      688721 :                        xfs_dir2_dataptr_to_off(args->geo,
    2245      688721 :                                 be32_to_cpu(leafhdr.ents[blk->index].address)));
    2246      688721 :                 ASSERT(inum != be64_to_cpu(dep->inumber));
    2247             :                 /*
    2248             :                  * Fill in the new inode number and log the entry.
    2249             :                  */
    2250      688721 :                 dep->inumber = cpu_to_be64(inum);
    2251      688721 :                 xfs_dir2_data_put_ftype(state->mp, dep, ftype);
    2252      688715 :                 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
    2253      688712 :                 rval = 0;
    2254             :         }
    2255             :         /*
    2256             :          * Didn't find it, and we're holding a data block.  Drop it.
    2257             :          */
    2258         194 :         else if (state->extravalid) {
    2259           0 :                 xfs_trans_brelse(args->trans, state->extrablk.bp);
    2260           0 :                 state->extrablk.bp = NULL;
    2261             :         }
    2262             :         /*
    2263             :          * Release all the buffers in the cursor.
    2264             :          */
    2265     2066351 :         for (i = 0; i < state->path.active; i++) {
    2266     1377428 :                 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
    2267     1377445 :                 state->path.blk[i].bp = NULL;
    2268             :         }
    2269      688923 :         xfs_da_state_free(state);
    2270      688916 :         return rval;
    2271             : }
    2272             : 
    2273             : /*
    2274             :  * Trim off a trailing empty freespace block.
    2275             :  * Return (in rvalp) 1 if we did it, 0 if not.
    2276             :  */
    2277             : int                                             /* error */
    2278        9052 : xfs_dir2_node_trim_free(
    2279             :         xfs_da_args_t           *args,          /* operation arguments */
    2280             :         xfs_fileoff_t           fo,             /* free block number */
    2281             :         int                     *rvalp)         /* out: did something */
    2282             : {
    2283        9052 :         struct xfs_buf          *bp;            /* freespace buffer */
    2284        9052 :         xfs_inode_t             *dp;            /* incore directory inode */
    2285        9052 :         int                     error;          /* error return code */
    2286        9052 :         xfs_dir2_free_t         *free;          /* freespace structure */
    2287        9052 :         xfs_trans_t             *tp;            /* transaction pointer */
    2288        9052 :         struct xfs_dir3_icfree_hdr freehdr;
    2289             : 
    2290        9052 :         dp = args->dp;
    2291        9052 :         tp = args->trans;
    2292             : 
    2293        9052 :         *rvalp = 0;
    2294             : 
    2295             :         /*
    2296             :          * Read the freespace block.
    2297             :          */
    2298        9052 :         error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
    2299        9052 :         if (error)
    2300             :                 return error;
    2301             :         /*
    2302             :          * There can be holes in freespace.  If fo is a hole, there's
    2303             :          * nothing to do.
    2304             :          */
    2305        9052 :         if (!bp)
    2306             :                 return 0;
    2307        9052 :         free = bp->b_addr;
    2308        9052 :         xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
    2309             : 
    2310             :         /*
    2311             :          * If there are used entries, there's nothing to do.
    2312             :          */
    2313        9052 :         if (freehdr.nused > 0) {
    2314        9052 :                 xfs_trans_brelse(tp, bp);
    2315        9052 :                 return 0;
    2316             :         }
    2317             :         /*
    2318             :          * Blow the block away.
    2319             :          */
    2320           0 :         error = xfs_dir2_shrink_inode(args,
    2321             :                         xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
    2322           0 :         if (error) {
    2323             :                 /*
    2324             :                  * Can't fail with ENOSPC since that only happens with no
    2325             :                  * space reservation, when breaking up an extent into two
    2326             :                  * pieces.  This is the last block of an extent.
    2327             :                  */
    2328           0 :                 ASSERT(error != -ENOSPC);
    2329           0 :                 xfs_trans_brelse(tp, bp);
    2330           0 :                 return error;
    2331             :         }
    2332             :         /*
    2333             :          * Return that we succeeded.
    2334             :          */
    2335           0 :         *rvalp = 1;
    2336           0 :         return 0;
    2337             : }

Generated by: LCOV version 1.14