Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_log.h" |
| 22 | #include "xfs_trans.h" |
| 23 | #include "xfs_sb.h" |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 24 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_mount.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 26 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_dinode.h" |
| 29 | #include "xfs_inode.h" |
| 30 | #include "xfs_bmap.h" |
Christoph Hellwig | 5792664 | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 31 | #include "xfs_dir2_format.h" |
| 32 | #include "xfs_dir2_priv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_error.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 34 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * Function declarations. |
| 38 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 39 | static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args, |
| 40 | int index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #ifdef DEBUG |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 42 | static void xfs_dir2_leafn_check(struct xfs_inode *dp, struct xfs_buf *bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #else |
| 44 | #define xfs_dir2_leafn_check(dp, bp) |
| 45 | #endif |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 46 | static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, struct xfs_buf *bp_s, |
| 47 | int start_s, struct xfs_buf *bp_d, |
| 48 | int start_d, int count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state, |
| 50 | xfs_da_state_blk_t *blk1, |
| 51 | xfs_da_state_blk_t *blk2); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 52 | static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | int index, xfs_da_state_blk_t *dblk, |
| 54 | int *rval); |
| 55 | static int xfs_dir2_node_addname_int(xfs_da_args_t *args, |
| 56 | xfs_da_state_blk_t *fblk); |
| 57 | |
| 58 | /* |
| 59 | * Log entries from a freespace block. |
| 60 | */ |
Eric Sandeen | 5d77c0d | 2009-11-19 15:52:00 +0000 | [diff] [blame] | 61 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | xfs_dir2_free_log_bests( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 63 | struct xfs_trans *tp, |
| 64 | struct xfs_buf *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | int first, /* first entry to log */ |
| 66 | int last) /* last entry to log */ |
| 67 | { |
| 68 | xfs_dir2_free_t *free; /* freespace structure */ |
| 69 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 70 | free = bp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 71 | ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC)); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 72 | xfs_trans_log_buf(tp, bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | (uint)((char *)&free->bests[first] - (char *)free), |
| 74 | (uint)((char *)&free->bests[last] - (char *)free + |
| 75 | sizeof(free->bests[0]) - 1)); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Log header from a freespace block. |
| 80 | */ |
| 81 | static void |
| 82 | xfs_dir2_free_log_header( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 83 | struct xfs_trans *tp, |
| 84 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | xfs_dir2_free_t *free; /* freespace structure */ |
| 87 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 88 | free = bp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 89 | ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC)); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 90 | xfs_trans_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | (uint)(sizeof(xfs_dir2_free_hdr_t) - 1)); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Convert a leaf-format directory to a node-format directory. |
| 96 | * We need to change the magic number of the leaf block, and copy |
| 97 | * the freespace table out of the leaf block into its own block. |
| 98 | */ |
| 99 | int /* error */ |
| 100 | xfs_dir2_leaf_to_node( |
| 101 | xfs_da_args_t *args, /* operation arguments */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 102 | struct xfs_buf *lbp) /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
| 104 | xfs_inode_t *dp; /* incore directory inode */ |
| 105 | int error; /* error return value */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 106 | struct xfs_buf *fbp; /* freespace buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | xfs_dir2_db_t fdb; /* freespace block number */ |
| 108 | xfs_dir2_free_t *free; /* freespace structure */ |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 109 | __be16 *from; /* pointer to freespace entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | int i; /* leaf freespace index */ |
| 111 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 112 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
| 113 | xfs_mount_t *mp; /* filesystem mount point */ |
| 114 | int n; /* count of live freespc ents */ |
| 115 | xfs_dir2_data_off_t off; /* freespace entry value */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 116 | __be16 *to; /* pointer to freespace entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | xfs_trans_t *tp; /* transaction pointer */ |
| 118 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 119 | trace_xfs_dir2_leaf_to_node(args); |
| 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | dp = args->dp; |
| 122 | mp = dp->i_mount; |
| 123 | tp = args->trans; |
| 124 | /* |
| 125 | * Add a freespace block to the directory. |
| 126 | */ |
| 127 | if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) { |
| 128 | return error; |
| 129 | } |
| 130 | ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp)); |
| 131 | /* |
| 132 | * Get the buffer for the new freespace block. |
| 133 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 134 | if ((error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | XFS_DATA_FORK))) { |
| 136 | return error; |
| 137 | } |
| 138 | ASSERT(fbp != NULL); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 139 | free = fbp->b_addr; |
| 140 | leaf = lbp->b_addr; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 141 | ltp = xfs_dir2_leaf_tail_p(mp, leaf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | /* |
| 143 | * Initialize the freespace block header. |
| 144 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 145 | free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | free->hdr.firstdb = 0; |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 147 | ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize); |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 148 | free->hdr.nvalid = ltp->bestcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | /* |
| 150 | * Copy freespace entries from the leaf block to the new block. |
| 151 | * Count active entries. |
| 152 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 153 | for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests; |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 154 | i < be32_to_cpu(ltp->bestcount); i++, from++, to++) { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 155 | if ((off = be16_to_cpu(*from)) != NULLDATAOFF) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | n++; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 157 | *to = cpu_to_be16(off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 159 | free->hdr.nused = cpu_to_be32(n); |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 160 | leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | /* |
| 162 | * Log everything. |
| 163 | */ |
| 164 | xfs_dir2_leaf_log_header(tp, lbp); |
| 165 | xfs_dir2_free_log_header(tp, fbp); |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 166 | xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | xfs_dir2_leafn_check(dp, lbp); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Add a leaf entry to a leaf block in a node-form directory. |
| 173 | * The other work necessary is done from the caller. |
| 174 | */ |
| 175 | static int /* error */ |
| 176 | xfs_dir2_leafn_add( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 177 | struct xfs_buf *bp, /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | xfs_da_args_t *args, /* operation arguments */ |
| 179 | int index) /* insertion pt for new entry */ |
| 180 | { |
| 181 | int compact; /* compacting stale leaves */ |
| 182 | xfs_inode_t *dp; /* incore directory inode */ |
| 183 | int highstale; /* next stale entry */ |
| 184 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 185 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 186 | int lfloghigh; /* high leaf entry logging */ |
| 187 | int lfloglow; /* low leaf entry logging */ |
| 188 | int lowstale; /* previous stale entry */ |
| 189 | xfs_mount_t *mp; /* filesystem mount point */ |
| 190 | xfs_trans_t *tp; /* transaction pointer */ |
| 191 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 192 | trace_xfs_dir2_leafn_add(args, index); |
| 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | dp = args->dp; |
| 195 | mp = dp->i_mount; |
| 196 | tp = args->trans; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 197 | leaf = bp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * Quick check just to make sure we are not going to index |
| 201 | * into other peoples memory |
| 202 | */ |
| 203 | if (index < 0) |
| 204 | return XFS_ERROR(EFSCORRUPTED); |
| 205 | |
| 206 | /* |
| 207 | * If there are already the maximum number of leaf entries in |
| 208 | * the block, if there are no stale entries it won't fit. |
| 209 | * Caller will do a split. If there are stale entries we'll do |
| 210 | * a compact. |
| 211 | */ |
| 212 | |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 213 | if (be16_to_cpu(leaf->hdr.count) == xfs_dir2_max_leaf_ents(mp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (!leaf->hdr.stale) |
| 215 | return XFS_ERROR(ENOSPC); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 216 | compact = be16_to_cpu(leaf->hdr.stale) > 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } else |
| 218 | compact = 0; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 219 | ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 220 | ASSERT(index == be16_to_cpu(leaf->hdr.count) || |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 221 | be32_to_cpu(leaf->ents[index].hashval) >= args->hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 223 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | return 0; |
| 225 | |
| 226 | /* |
| 227 | * Compact out all but one stale leaf entry. Leaves behind |
| 228 | * the entry closest to index. |
| 229 | */ |
| 230 | if (compact) { |
| 231 | xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale, |
| 232 | &lfloglow, &lfloghigh); |
| 233 | } |
| 234 | /* |
| 235 | * Set impossible logging indices for this case. |
| 236 | */ |
| 237 | else if (leaf->hdr.stale) { |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 238 | lfloglow = be16_to_cpu(leaf->hdr.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | lfloghigh = -1; |
| 240 | } |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | /* |
| 243 | * Insert the new entry, log everything. |
| 244 | */ |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 245 | lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale, |
| 246 | highstale, &lfloglow, &lfloghigh); |
| 247 | |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 248 | lep->hashval = cpu_to_be32(args->hashval); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 249 | lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 250 | args->blkno, args->index)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | xfs_dir2_leaf_log_header(tp, bp); |
| 252 | xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh); |
| 253 | xfs_dir2_leafn_check(dp, bp); |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | #ifdef DEBUG |
| 258 | /* |
| 259 | * Check internal consistency of a leafn block. |
| 260 | */ |
| 261 | void |
| 262 | xfs_dir2_leafn_check( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 263 | struct xfs_inode *dp, |
| 264 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { |
| 266 | int i; /* leaf index */ |
| 267 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 268 | xfs_mount_t *mp; /* filesystem mount point */ |
| 269 | int stale; /* count of stale leaves */ |
| 270 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 271 | leaf = bp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | mp = dp->i_mount; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 273 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 274 | ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp)); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 275 | for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) { |
| 276 | if (i + 1 < be16_to_cpu(leaf->hdr.count)) { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 277 | ASSERT(be32_to_cpu(leaf->ents[i].hashval) <= |
| 278 | be32_to_cpu(leaf->ents[i + 1].hashval)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 280 | if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | stale++; |
| 282 | } |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 283 | ASSERT(be16_to_cpu(leaf->hdr.stale) == stale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
| 285 | #endif /* DEBUG */ |
| 286 | |
| 287 | /* |
| 288 | * Return the last hash value in the leaf. |
| 289 | * Stale entries are ok. |
| 290 | */ |
| 291 | xfs_dahash_t /* hash value */ |
| 292 | xfs_dir2_leafn_lasthash( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 293 | struct xfs_buf *bp, /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | int *count) /* count of entries in leaf */ |
| 295 | { |
| 296 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 297 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 298 | leaf = bp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 299 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | if (count) |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 301 | *count = be16_to_cpu(leaf->hdr.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | if (!leaf->hdr.count) |
| 303 | return 0; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 304 | return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 308 | * Look up a leaf entry for space to add a name in a node-format leaf block. |
| 309 | * The extrablk in state is a freespace block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 311 | STATIC int |
| 312 | xfs_dir2_leafn_lookup_for_addname( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 313 | struct xfs_buf *bp, /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | xfs_da_args_t *args, /* operation arguments */ |
| 315 | int *indexp, /* out: leaf entry index */ |
| 316 | xfs_da_state_t *state) /* state to fill in */ |
| 317 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 318 | struct xfs_buf *curbp = NULL; /* current data/free buffer */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 319 | xfs_dir2_db_t curdb = -1; /* current data block number */ |
| 320 | xfs_dir2_db_t curfdb = -1; /* current free block number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | xfs_inode_t *dp; /* incore directory inode */ |
| 322 | int error; /* error return value */ |
| 323 | int fi; /* free entry index */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 324 | xfs_dir2_free_t *free = NULL; /* free block structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | int index; /* leaf entry index */ |
| 326 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 327 | int length; /* length of new data entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 329 | xfs_mount_t *mp; /* filesystem mount point */ |
| 330 | xfs_dir2_db_t newdb; /* new data block number */ |
| 331 | xfs_dir2_db_t newfdb; /* new free block number */ |
| 332 | xfs_trans_t *tp; /* transaction pointer */ |
| 333 | |
| 334 | dp = args->dp; |
| 335 | tp = args->trans; |
| 336 | mp = dp->i_mount; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 337 | leaf = bp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 338 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | #ifdef __KERNEL__ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 340 | ASSERT(be16_to_cpu(leaf->hdr.count) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | #endif |
| 342 | xfs_dir2_leafn_check(dp, bp); |
| 343 | /* |
| 344 | * Look up the hash value in the leaf entries. |
| 345 | */ |
| 346 | index = xfs_dir2_leaf_search_hash(args, bp); |
| 347 | /* |
| 348 | * Do we have a buffer coming in? |
| 349 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 350 | if (state->extravalid) { |
| 351 | /* If so, it's a free block buffer, get the block number. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | curbp = state->extrablk.bp; |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 353 | curfdb = state->extrablk.blkno; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 354 | free = curbp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 355 | ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 357 | length = xfs_dir2_data_entsize(args->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | /* |
| 359 | * Loop over leaf entries with the right hash value. |
| 360 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 361 | for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) && |
| 362 | be32_to_cpu(lep->hashval) == args->hashval; |
| 363 | lep++, index++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | /* |
| 365 | * Skip stale leaf entries. |
| 366 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 367 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | continue; |
| 369 | /* |
| 370 | * Pull the data block number from the entry. |
| 371 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 372 | newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | /* |
| 374 | * For addname, we're looking for a place to put the new entry. |
| 375 | * We want to use a data block with an entry of equal |
| 376 | * hash value to ours if there is one with room. |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 377 | * |
| 378 | * If this block isn't the data block we already have |
| 379 | * in hand, take a look at it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 381 | if (newdb != curdb) { |
| 382 | curdb = newdb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 384 | * Convert the data block to the free block |
| 385 | * holding its freespace information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 387 | newfdb = xfs_dir2_db_to_fdb(mp, newdb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 389 | * If it's not the one we have in hand, read it in. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 391 | if (newfdb != curfdb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 393 | * If we had one before, drop it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | */ |
| 395 | if (curbp) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 396 | xfs_trans_brelse(tp, curbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 398 | * Read the free block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 400 | error = xfs_da_read_buf(tp, dp, |
| 401 | xfs_dir2_db_to_da(mp, newfdb), |
| 402 | -1, &curbp, XFS_DATA_FORK); |
| 403 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | return error; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 405 | free = curbp->b_addr; |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 406 | ASSERT(be32_to_cpu(free->hdr.magic) == |
| 407 | XFS_DIR2_FREE_MAGIC); |
| 408 | ASSERT((be32_to_cpu(free->hdr.firstdb) % |
Christoph Hellwig | a00b774 | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 409 | xfs_dir2_free_max_bests(mp)) == 0); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 410 | ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb); |
| 411 | ASSERT(curdb < be32_to_cpu(free->hdr.firstdb) + |
| 412 | be32_to_cpu(free->hdr.nvalid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 415 | * Get the index for our entry. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 417 | fi = xfs_dir2_db_to_fdindex(mp, curdb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 419 | * If it has room, return it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | */ |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 421 | if (unlikely(free->bests[fi] == |
| 422 | cpu_to_be16(NULLDATAOFF))) { |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 423 | XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int", |
| 424 | XFS_ERRLEVEL_LOW, mp); |
| 425 | if (curfdb != newfdb) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 426 | xfs_trans_brelse(tp, curbp); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 427 | return XFS_ERROR(EFSCORRUPTED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | } |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 429 | curfdb = newfdb; |
| 430 | if (be16_to_cpu(free->bests[fi]) >= length) |
| 431 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 434 | /* Didn't find any space */ |
| 435 | fi = -1; |
| 436 | out: |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 437 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 438 | if (curbp) { |
| 439 | /* Giving back a free block. */ |
| 440 | state->extravalid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | state->extrablk.bp = curbp; |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 442 | state->extrablk.index = fi; |
| 443 | state->extrablk.blkno = curfdb; |
| 444 | state->extrablk.magic = XFS_DIR2_FREE_MAGIC; |
| 445 | } else { |
| 446 | state->extravalid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } |
| 448 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 449 | * Return the index, that will be the insertion point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | */ |
| 451 | *indexp = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | return XFS_ERROR(ENOENT); |
| 453 | } |
| 454 | |
| 455 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 456 | * Look up a leaf entry in a node-format leaf block. |
| 457 | * The extrablk in state a data block. |
| 458 | */ |
| 459 | STATIC int |
| 460 | xfs_dir2_leafn_lookup_for_entry( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 461 | struct xfs_buf *bp, /* leaf buffer */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 462 | xfs_da_args_t *args, /* operation arguments */ |
| 463 | int *indexp, /* out: leaf entry index */ |
| 464 | xfs_da_state_t *state) /* state to fill in */ |
| 465 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 466 | struct xfs_buf *curbp = NULL; /* current data/free buffer */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 467 | xfs_dir2_db_t curdb = -1; /* current data block number */ |
| 468 | xfs_dir2_data_entry_t *dep; /* data block entry */ |
| 469 | xfs_inode_t *dp; /* incore directory inode */ |
| 470 | int error; /* error return value */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 471 | int index; /* leaf entry index */ |
| 472 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 473 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 474 | xfs_mount_t *mp; /* filesystem mount point */ |
| 475 | xfs_dir2_db_t newdb; /* new data block number */ |
| 476 | xfs_trans_t *tp; /* transaction pointer */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 477 | enum xfs_dacmp cmp; /* comparison result */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 478 | |
| 479 | dp = args->dp; |
| 480 | tp = args->trans; |
| 481 | mp = dp->i_mount; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 482 | leaf = bp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 483 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 484 | #ifdef __KERNEL__ |
| 485 | ASSERT(be16_to_cpu(leaf->hdr.count) > 0); |
| 486 | #endif |
| 487 | xfs_dir2_leafn_check(dp, bp); |
| 488 | /* |
| 489 | * Look up the hash value in the leaf entries. |
| 490 | */ |
| 491 | index = xfs_dir2_leaf_search_hash(args, bp); |
| 492 | /* |
| 493 | * Do we have a buffer coming in? |
| 494 | */ |
| 495 | if (state->extravalid) { |
| 496 | curbp = state->extrablk.bp; |
| 497 | curdb = state->extrablk.blkno; |
| 498 | } |
| 499 | /* |
| 500 | * Loop over leaf entries with the right hash value. |
| 501 | */ |
| 502 | for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) && |
| 503 | be32_to_cpu(lep->hashval) == args->hashval; |
| 504 | lep++, index++) { |
| 505 | /* |
| 506 | * Skip stale leaf entries. |
| 507 | */ |
| 508 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
| 509 | continue; |
| 510 | /* |
| 511 | * Pull the data block number from the entry. |
| 512 | */ |
| 513 | newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address)); |
| 514 | /* |
| 515 | * Not adding a new entry, so we really want to find |
| 516 | * the name given to us. |
| 517 | * |
| 518 | * If it's a different data block, go get it. |
| 519 | */ |
| 520 | if (newdb != curdb) { |
| 521 | /* |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 522 | * If we had a block before that we aren't saving |
| 523 | * for a CI name, drop it |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 524 | */ |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 525 | if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT || |
| 526 | curdb != state->extrablk.blkno)) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 527 | xfs_trans_brelse(tp, curbp); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 528 | /* |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 529 | * If needing the block that is saved with a CI match, |
| 530 | * use it otherwise read in the new data block. |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 531 | */ |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 532 | if (args->cmpresult != XFS_CMP_DIFFERENT && |
| 533 | newdb == state->extrablk.blkno) { |
| 534 | ASSERT(state->extravalid); |
| 535 | curbp = state->extrablk.bp; |
| 536 | } else { |
| 537 | error = xfs_da_read_buf(tp, dp, |
| 538 | xfs_dir2_db_to_da(mp, newdb), |
| 539 | -1, &curbp, XFS_DATA_FORK); |
| 540 | if (error) |
| 541 | return error; |
| 542 | } |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 543 | xfs_dir2_data_check(dp, curbp); |
| 544 | curdb = newdb; |
| 545 | } |
| 546 | /* |
| 547 | * Point to the data entry. |
| 548 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 549 | dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr + |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 550 | xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address))); |
| 551 | /* |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 552 | * Compare the entry and if it's an exact match, return |
| 553 | * EEXIST immediately. If it's the first case-insensitive |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 554 | * match, store the block & inode number and continue looking. |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 555 | */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 556 | cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen); |
| 557 | if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 558 | /* If there is a CI match block, drop it */ |
| 559 | if (args->cmpresult != XFS_CMP_DIFFERENT && |
| 560 | curdb != state->extrablk.blkno) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 561 | xfs_trans_brelse(tp, state->extrablk.bp); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 562 | args->cmpresult = cmp; |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 563 | args->inumber = be64_to_cpu(dep->inumber); |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 564 | *indexp = index; |
| 565 | state->extravalid = 1; |
| 566 | state->extrablk.bp = curbp; |
| 567 | state->extrablk.blkno = curdb; |
| 568 | state->extrablk.index = (int)((char *)dep - |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 569 | (char *)curbp->b_addr); |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 570 | state->extrablk.magic = XFS_DIR2_DATA_MAGIC; |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 571 | if (cmp == XFS_CMP_EXACT) |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 572 | return XFS_ERROR(EEXIST); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 573 | } |
| 574 | } |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 575 | ASSERT(index == be16_to_cpu(leaf->hdr.count) || |
| 576 | (args->op_flags & XFS_DA_OP_OKNOENT)); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 577 | if (curbp) { |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 578 | if (args->cmpresult == XFS_CMP_DIFFERENT) { |
| 579 | /* Giving back last used data block. */ |
| 580 | state->extravalid = 1; |
| 581 | state->extrablk.bp = curbp; |
| 582 | state->extrablk.index = -1; |
| 583 | state->extrablk.blkno = curdb; |
| 584 | state->extrablk.magic = XFS_DIR2_DATA_MAGIC; |
| 585 | } else { |
| 586 | /* If the curbp is not the CI match block, drop it */ |
| 587 | if (state->extrablk.bp != curbp) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 588 | xfs_trans_brelse(tp, curbp); |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 589 | } |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 590 | } else { |
| 591 | state->extravalid = 0; |
| 592 | } |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 593 | *indexp = index; |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 594 | return XFS_ERROR(ENOENT); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Look up a leaf entry in a node-format leaf block. |
| 599 | * If this is an addname then the extrablk in state is a freespace block, |
| 600 | * otherwise it's a data block. |
| 601 | */ |
| 602 | int |
| 603 | xfs_dir2_leafn_lookup_int( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 604 | struct xfs_buf *bp, /* leaf buffer */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 605 | xfs_da_args_t *args, /* operation arguments */ |
| 606 | int *indexp, /* out: leaf entry index */ |
| 607 | xfs_da_state_t *state) /* state to fill in */ |
| 608 | { |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 609 | if (args->op_flags & XFS_DA_OP_ADDNAME) |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 610 | return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp, |
| 611 | state); |
| 612 | return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state); |
| 613 | } |
| 614 | |
| 615 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | * Move count leaf entries from source to destination leaf. |
| 617 | * Log entries and headers. Stale entries are preserved. |
| 618 | */ |
| 619 | static void |
| 620 | xfs_dir2_leafn_moveents( |
| 621 | xfs_da_args_t *args, /* operation arguments */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 622 | struct xfs_buf *bp_s, /* source leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | int start_s, /* source leaf index */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 624 | struct xfs_buf *bp_d, /* destination leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | int start_d, /* destination leaf index */ |
| 626 | int count) /* count of leaves to copy */ |
| 627 | { |
| 628 | xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */ |
| 629 | xfs_dir2_leaf_t *leaf_s; /* source leaf structure */ |
| 630 | int stale; /* count stale leaves copied */ |
| 631 | xfs_trans_t *tp; /* transaction pointer */ |
| 632 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 633 | trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count); |
| 634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | /* |
| 636 | * Silently return if nothing to do. |
| 637 | */ |
| 638 | if (count == 0) { |
| 639 | return; |
| 640 | } |
| 641 | tp = args->trans; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 642 | leaf_s = bp_s->b_addr; |
| 643 | leaf_d = bp_d->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | /* |
| 645 | * If the destination index is not the end of the current |
| 646 | * destination leaf entries, open up a hole in the destination |
| 647 | * to hold the new entries. |
| 648 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 649 | if (start_d < be16_to_cpu(leaf_d->hdr.count)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d], |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 651 | (be16_to_cpu(leaf_d->hdr.count) - start_d) * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | sizeof(xfs_dir2_leaf_entry_t)); |
| 653 | xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count, |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 654 | count + be16_to_cpu(leaf_d->hdr.count) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | } |
| 656 | /* |
| 657 | * If the source has stale leaves, count the ones in the copy range |
| 658 | * so we can update the header correctly. |
| 659 | */ |
| 660 | if (leaf_s->hdr.stale) { |
| 661 | int i; /* temp leaf index */ |
| 662 | |
| 663 | for (i = start_s, stale = 0; i < start_s + count; i++) { |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 664 | if (leaf_s->ents[i].address == |
| 665 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | stale++; |
| 667 | } |
| 668 | } else |
| 669 | stale = 0; |
| 670 | /* |
| 671 | * Copy the leaf entries from source to destination. |
| 672 | */ |
| 673 | memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s], |
| 674 | count * sizeof(xfs_dir2_leaf_entry_t)); |
| 675 | xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1); |
| 676 | /* |
| 677 | * If there are source entries after the ones we copied, |
| 678 | * delete the ones we copied by sliding the next ones down. |
| 679 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 680 | if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count], |
| 682 | count * sizeof(xfs_dir2_leaf_entry_t)); |
| 683 | xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1); |
| 684 | } |
| 685 | /* |
| 686 | * Update the headers and log them. |
| 687 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 688 | be16_add_cpu(&leaf_s->hdr.count, -(count)); |
| 689 | be16_add_cpu(&leaf_s->hdr.stale, -(stale)); |
| 690 | be16_add_cpu(&leaf_d->hdr.count, count); |
| 691 | be16_add_cpu(&leaf_d->hdr.stale, stale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | xfs_dir2_leaf_log_header(tp, bp_s); |
| 693 | xfs_dir2_leaf_log_header(tp, bp_d); |
| 694 | xfs_dir2_leafn_check(args->dp, bp_s); |
| 695 | xfs_dir2_leafn_check(args->dp, bp_d); |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Determine the sort order of two leaf blocks. |
| 700 | * Returns 1 if both are valid and leaf2 should be before leaf1, else 0. |
| 701 | */ |
| 702 | int /* sort order */ |
| 703 | xfs_dir2_leafn_order( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 704 | struct xfs_buf *leaf1_bp, /* leaf1 buffer */ |
| 705 | struct xfs_buf *leaf2_bp) /* leaf2 buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | { |
| 707 | xfs_dir2_leaf_t *leaf1; /* leaf1 structure */ |
| 708 | xfs_dir2_leaf_t *leaf2; /* leaf2 structure */ |
| 709 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 710 | leaf1 = leaf1_bp->b_addr; |
| 711 | leaf2 = leaf2_bp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 712 | ASSERT(leaf1->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)); |
| 713 | ASSERT(leaf2->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 714 | if (be16_to_cpu(leaf1->hdr.count) > 0 && |
| 715 | be16_to_cpu(leaf2->hdr.count) > 0 && |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 716 | (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) || |
| 717 | be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) < |
| 718 | be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | return 1; |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * Rebalance leaf entries between two leaf blocks. |
| 725 | * This is actually only called when the second block is new, |
| 726 | * though the code deals with the general case. |
| 727 | * A new entry will be inserted in one of the blocks, and that |
| 728 | * entry is taken into account when balancing. |
| 729 | */ |
| 730 | static void |
| 731 | xfs_dir2_leafn_rebalance( |
| 732 | xfs_da_state_t *state, /* btree cursor */ |
| 733 | xfs_da_state_blk_t *blk1, /* first btree block */ |
| 734 | xfs_da_state_blk_t *blk2) /* second btree block */ |
| 735 | { |
| 736 | xfs_da_args_t *args; /* operation arguments */ |
| 737 | int count; /* count (& direction) leaves */ |
| 738 | int isleft; /* new goes in left leaf */ |
| 739 | xfs_dir2_leaf_t *leaf1; /* first leaf structure */ |
| 740 | xfs_dir2_leaf_t *leaf2; /* second leaf structure */ |
| 741 | int mid; /* midpoint leaf index */ |
| 742 | #ifdef DEBUG |
| 743 | int oldstale; /* old count of stale leaves */ |
| 744 | #endif |
| 745 | int oldsum; /* old total leaf count */ |
| 746 | int swap; /* swapped leaf blocks */ |
| 747 | |
| 748 | args = state->args; |
| 749 | /* |
| 750 | * If the block order is wrong, swap the arguments. |
| 751 | */ |
| 752 | if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) { |
| 753 | xfs_da_state_blk_t *tmp; /* temp for block swap */ |
| 754 | |
| 755 | tmp = blk1; |
| 756 | blk1 = blk2; |
| 757 | blk2 = tmp; |
| 758 | } |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 759 | leaf1 = blk1->bp->b_addr; |
| 760 | leaf2 = blk2->bp->b_addr; |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 761 | oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | #ifdef DEBUG |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 763 | oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | #endif |
| 765 | mid = oldsum >> 1; |
| 766 | /* |
| 767 | * If the old leaf count was odd then the new one will be even, |
| 768 | * so we need to divide the new count evenly. |
| 769 | */ |
| 770 | if (oldsum & 1) { |
| 771 | xfs_dahash_t midhash; /* middle entry hash value */ |
| 772 | |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 773 | if (mid >= be16_to_cpu(leaf1->hdr.count)) |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 774 | midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | else |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 776 | midhash = be32_to_cpu(leaf1->ents[mid].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | isleft = args->hashval <= midhash; |
| 778 | } |
| 779 | /* |
| 780 | * If the old count is even then the new count is odd, so there's |
| 781 | * no preferred side for the new entry. |
| 782 | * Pick the left one. |
| 783 | */ |
| 784 | else |
| 785 | isleft = 1; |
| 786 | /* |
| 787 | * Calculate moved entry count. Positive means left-to-right, |
| 788 | * negative means right-to-left. Then move the entries. |
| 789 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 790 | count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | if (count > 0) |
| 792 | xfs_dir2_leafn_moveents(args, blk1->bp, |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 793 | be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | else if (count < 0) |
| 795 | xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp, |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 796 | be16_to_cpu(leaf1->hdr.count), count); |
| 797 | ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum); |
| 798 | ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | /* |
| 800 | * Mark whether we're inserting into the old or new leaf. |
| 801 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 802 | if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | state->inleaf = swap; |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 804 | else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | state->inleaf = !swap; |
| 806 | else |
| 807 | state->inleaf = |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 808 | swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | /* |
| 810 | * Adjust the expected index for insertion. |
| 811 | */ |
| 812 | if (!state->inleaf) |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 813 | blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 814 | |
| 815 | /* |
| 816 | * Finally sanity check just to make sure we are not returning a |
| 817 | * negative index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | */ |
| 819 | if(blk2->index < 0) { |
| 820 | state->inleaf = 1; |
| 821 | blk2->index = 0; |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 822 | xfs_alert(args->dp->i_mount, |
| 823 | "%s: picked the wrong leaf? reverting original leaf: blk1->index %d\n", |
| 824 | __func__, blk1->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | |
| 828 | /* |
| 829 | * Remove an entry from a node directory. |
| 830 | * This removes the leaf entry and the data entry, |
| 831 | * and updates the free block if necessary. |
| 832 | */ |
| 833 | static int /* error */ |
| 834 | xfs_dir2_leafn_remove( |
| 835 | xfs_da_args_t *args, /* operation arguments */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 836 | struct xfs_buf *bp, /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | int index, /* leaf entry index */ |
| 838 | xfs_da_state_blk_t *dblk, /* data block */ |
| 839 | int *rval) /* resulting block needs join */ |
| 840 | { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 841 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | xfs_dir2_db_t db; /* data block number */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 843 | struct xfs_buf *dbp; /* data block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | xfs_dir2_data_entry_t *dep; /* data block entry */ |
| 845 | xfs_inode_t *dp; /* incore directory inode */ |
| 846 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 847 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 848 | int longest; /* longest data free entry */ |
| 849 | int off; /* data block entry offset */ |
| 850 | xfs_mount_t *mp; /* filesystem mount point */ |
| 851 | int needlog; /* need to log data header */ |
| 852 | int needscan; /* need to rescan data frees */ |
| 853 | xfs_trans_t *tp; /* transaction pointer */ |
| 854 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 855 | trace_xfs_dir2_leafn_remove(args, index); |
| 856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | dp = args->dp; |
| 858 | tp = args->trans; |
| 859 | mp = dp->i_mount; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 860 | leaf = bp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 861 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | /* |
| 863 | * Point to the entry we're removing. |
| 864 | */ |
| 865 | lep = &leaf->ents[index]; |
| 866 | /* |
| 867 | * Extract the data block and offset from the entry. |
| 868 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 869 | db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | ASSERT(dblk->blkno == db); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 871 | off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | ASSERT(dblk->index == off); |
| 873 | /* |
| 874 | * Kill the leaf entry by marking it stale. |
| 875 | * Log the leaf block changes. |
| 876 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 877 | be16_add_cpu(&leaf->hdr.stale, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | xfs_dir2_leaf_log_header(tp, bp); |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 879 | lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | xfs_dir2_leaf_log_ents(tp, bp, index, index); |
| 881 | /* |
| 882 | * Make the data entry free. Keep track of the longest freespace |
| 883 | * in the data block in case it changes. |
| 884 | */ |
| 885 | dbp = dblk->bp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 886 | hdr = dbp->b_addr; |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 887 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + off); |
| 888 | longest = be16_to_cpu(hdr->bestfree[0].length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | needlog = needscan = 0; |
| 890 | xfs_dir2_data_make_free(tp, dbp, off, |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 891 | xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | /* |
| 893 | * Rescan the data block freespaces for bestfree. |
| 894 | * Log the data block header if needed. |
| 895 | */ |
| 896 | if (needscan) |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 897 | xfs_dir2_data_freescan(mp, hdr, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | if (needlog) |
| 899 | xfs_dir2_data_log_header(tp, dbp); |
| 900 | xfs_dir2_data_check(dp, dbp); |
| 901 | /* |
| 902 | * If the longest data block freespace changes, need to update |
| 903 | * the corresponding freeblock entry. |
| 904 | */ |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 905 | if (longest < be16_to_cpu(hdr->bestfree[0].length)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | int error; /* error return value */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 907 | struct xfs_buf *fbp; /* freeblock buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | xfs_dir2_db_t fdb; /* freeblock block number */ |
| 909 | int findex; /* index in freeblock entries */ |
| 910 | xfs_dir2_free_t *free; /* freeblock structure */ |
| 911 | int logfree; /* need to log free entry */ |
| 912 | |
| 913 | /* |
| 914 | * Convert the data block number to a free block, |
| 915 | * read in the free block. |
| 916 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 917 | fdb = xfs_dir2_db_to_fdb(mp, db); |
| 918 | if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | -1, &fbp, XFS_DATA_FORK))) { |
| 920 | return error; |
| 921 | } |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 922 | free = fbp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 923 | ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC)); |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 924 | ASSERT(be32_to_cpu(free->hdr.firstdb) == |
Christoph Hellwig | a00b774 | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 925 | xfs_dir2_free_max_bests(mp) * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | (fdb - XFS_DIR2_FREE_FIRSTDB(mp))); |
| 927 | /* |
| 928 | * Calculate which entry we need to fix. |
| 929 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 930 | findex = xfs_dir2_db_to_fdindex(mp, db); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 931 | longest = be16_to_cpu(hdr->bestfree[0].length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | /* |
| 933 | * If the data block is now empty we can get rid of it |
| 934 | * (usually). |
| 935 | */ |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 936 | if (longest == mp->m_dirblksize - (uint)sizeof(*hdr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | /* |
| 938 | * Try to punch out the data block. |
| 939 | */ |
| 940 | error = xfs_dir2_shrink_inode(args, db, dbp); |
| 941 | if (error == 0) { |
| 942 | dblk->bp = NULL; |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 943 | hdr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | } |
| 945 | /* |
| 946 | * We can get ENOSPC if there's no space reservation. |
| 947 | * In this case just drop the buffer and some one else |
| 948 | * will eventually get rid of the empty block. |
| 949 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 950 | else if (!(error == ENOSPC && args->total == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | return error; |
| 952 | } |
| 953 | /* |
| 954 | * If we got rid of the data block, we can eliminate that entry |
| 955 | * in the free block. |
| 956 | */ |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 957 | if (hdr == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | /* |
| 959 | * One less used entry in the free table. |
| 960 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 961 | be32_add_cpu(&free->hdr.nused, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | xfs_dir2_free_log_header(tp, fbp); |
| 963 | /* |
| 964 | * If this was the last entry in the table, we can |
| 965 | * trim the table size back. There might be other |
| 966 | * entries at the end referring to non-existent |
| 967 | * data blocks, get those too. |
| 968 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 969 | if (findex == be32_to_cpu(free->hdr.nvalid) - 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | int i; /* free entry index */ |
| 971 | |
| 972 | for (i = findex - 1; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 973 | i >= 0 && |
| 974 | free->bests[i] == cpu_to_be16(NULLDATAOFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | i--) |
| 976 | continue; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 977 | free->hdr.nvalid = cpu_to_be32(i + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | logfree = 0; |
| 979 | } |
| 980 | /* |
| 981 | * Not the last entry, just punch it out. |
| 982 | */ |
| 983 | else { |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 984 | free->bests[findex] = cpu_to_be16(NULLDATAOFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | logfree = 1; |
| 986 | } |
| 987 | /* |
| 988 | * If there are no useful entries left in the block, |
| 989 | * get rid of the block if we can. |
| 990 | */ |
| 991 | if (!free->hdr.nused) { |
| 992 | error = xfs_dir2_shrink_inode(args, fdb, fbp); |
| 993 | if (error == 0) { |
| 994 | fbp = NULL; |
| 995 | logfree = 0; |
| 996 | } else if (error != ENOSPC || args->total != 0) |
| 997 | return error; |
| 998 | /* |
| 999 | * It's possible to get ENOSPC if there is no |
| 1000 | * space reservation. In this case some one |
| 1001 | * else will eventually get rid of this block. |
| 1002 | */ |
| 1003 | } |
| 1004 | } |
| 1005 | /* |
| 1006 | * Data block is not empty, just set the free entry to |
| 1007 | * the new value. |
| 1008 | */ |
| 1009 | else { |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1010 | free->bests[findex] = cpu_to_be16(longest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | logfree = 1; |
| 1012 | } |
| 1013 | /* |
| 1014 | * Log the free entry that changed, unless we got rid of it. |
| 1015 | */ |
| 1016 | if (logfree) |
| 1017 | xfs_dir2_free_log_bests(tp, fbp, findex, findex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | } |
| 1019 | xfs_dir2_leafn_check(dp, bp); |
| 1020 | /* |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 1021 | * Return indication of whether this leaf block is empty enough |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | * to justify trying to join it with a neighbor. |
| 1023 | */ |
| 1024 | *rval = |
| 1025 | ((uint)sizeof(leaf->hdr) + |
| 1026 | (uint)sizeof(leaf->ents[0]) * |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1027 | (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) < |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | mp->m_dir_magicpct; |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * Split the leaf entries in the old block into old and new blocks. |
| 1034 | */ |
| 1035 | int /* error */ |
| 1036 | xfs_dir2_leafn_split( |
| 1037 | xfs_da_state_t *state, /* btree cursor */ |
| 1038 | xfs_da_state_blk_t *oldblk, /* original block */ |
| 1039 | xfs_da_state_blk_t *newblk) /* newly created block */ |
| 1040 | { |
| 1041 | xfs_da_args_t *args; /* operation arguments */ |
| 1042 | xfs_dablk_t blkno; /* new leaf block number */ |
| 1043 | int error; /* error return value */ |
| 1044 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1045 | |
| 1046 | /* |
| 1047 | * Allocate space for a new leaf node. |
| 1048 | */ |
| 1049 | args = state->args; |
| 1050 | mp = args->dp->i_mount; |
| 1051 | ASSERT(args != NULL); |
| 1052 | ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC); |
| 1053 | error = xfs_da_grow_inode(args, &blkno); |
| 1054 | if (error) { |
| 1055 | return error; |
| 1056 | } |
| 1057 | /* |
| 1058 | * Initialize the new leaf block. |
| 1059 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1060 | error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | &newblk->bp, XFS_DIR2_LEAFN_MAGIC); |
| 1062 | if (error) { |
| 1063 | return error; |
| 1064 | } |
| 1065 | newblk->blkno = blkno; |
| 1066 | newblk->magic = XFS_DIR2_LEAFN_MAGIC; |
| 1067 | /* |
| 1068 | * Rebalance the entries across the two leaves, link the new |
| 1069 | * block into the leaves. |
| 1070 | */ |
| 1071 | xfs_dir2_leafn_rebalance(state, oldblk, newblk); |
| 1072 | error = xfs_da_blk_link(state, oldblk, newblk); |
| 1073 | if (error) { |
| 1074 | return error; |
| 1075 | } |
| 1076 | /* |
| 1077 | * Insert the new entry in the correct block. |
| 1078 | */ |
| 1079 | if (state->inleaf) |
| 1080 | error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index); |
| 1081 | else |
| 1082 | error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index); |
| 1083 | /* |
| 1084 | * Update last hashval in each block since we added the name. |
| 1085 | */ |
| 1086 | oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL); |
| 1087 | newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL); |
| 1088 | xfs_dir2_leafn_check(args->dp, oldblk->bp); |
| 1089 | xfs_dir2_leafn_check(args->dp, newblk->bp); |
| 1090 | return error; |
| 1091 | } |
| 1092 | |
| 1093 | /* |
| 1094 | * Check a leaf block and its neighbors to see if the block should be |
| 1095 | * collapsed into one or the other neighbor. Always keep the block |
| 1096 | * with the smaller block number. |
| 1097 | * If the current block is over 50% full, don't try to join it, return 0. |
| 1098 | * If the block is empty, fill in the state structure and return 2. |
| 1099 | * If it can be collapsed, fill in the state structure and return 1. |
| 1100 | * If nothing can be done, return 0. |
| 1101 | */ |
| 1102 | int /* error */ |
| 1103 | xfs_dir2_leafn_toosmall( |
| 1104 | xfs_da_state_t *state, /* btree cursor */ |
| 1105 | int *action) /* resulting action to take */ |
| 1106 | { |
| 1107 | xfs_da_state_blk_t *blk; /* leaf block */ |
| 1108 | xfs_dablk_t blkno; /* leaf block number */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1109 | struct xfs_buf *bp; /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | int bytes; /* bytes in use */ |
| 1111 | int count; /* leaf live entry count */ |
| 1112 | int error; /* error return value */ |
| 1113 | int forward; /* sibling block direction */ |
| 1114 | int i; /* sibling counter */ |
| 1115 | xfs_da_blkinfo_t *info; /* leaf block header */ |
| 1116 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1117 | int rval; /* result from path_shift */ |
| 1118 | |
| 1119 | /* |
| 1120 | * Check for the degenerate case of the block being over 50% full. |
| 1121 | * If so, it's not worth even looking to see if we might be able |
| 1122 | * to coalesce with a sibling. |
| 1123 | */ |
| 1124 | blk = &state->path.blk[state->path.active - 1]; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1125 | info = blk->bp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1126 | ASSERT(info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | leaf = (xfs_dir2_leaf_t *)info; |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1128 | count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]); |
| 1130 | if (bytes > (state->blocksize >> 1)) { |
| 1131 | /* |
| 1132 | * Blk over 50%, don't try to join. |
| 1133 | */ |
| 1134 | *action = 0; |
| 1135 | return 0; |
| 1136 | } |
| 1137 | /* |
| 1138 | * Check for the degenerate case of the block being empty. |
| 1139 | * If the block is empty, we'll simply delete it, no need to |
| 1140 | * coalesce it with a sibling block. We choose (arbitrarily) |
| 1141 | * to merge with the forward block unless it is NULL. |
| 1142 | */ |
| 1143 | if (count == 0) { |
| 1144 | /* |
| 1145 | * Make altpath point to the block we want to keep and |
| 1146 | * path point to the block we want to drop (this one). |
| 1147 | */ |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1148 | forward = (info->forw != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | memcpy(&state->altpath, &state->path, sizeof(state->path)); |
| 1150 | error = xfs_da_path_shift(state, &state->altpath, forward, 0, |
| 1151 | &rval); |
| 1152 | if (error) |
| 1153 | return error; |
| 1154 | *action = rval ? 2 : 0; |
| 1155 | return 0; |
| 1156 | } |
| 1157 | /* |
| 1158 | * Examine each sibling block to see if we can coalesce with |
| 1159 | * at least 25% free space to spare. We need to figure out |
| 1160 | * whether to merge with the forward or the backward block. |
| 1161 | * We prefer coalescing with the lower numbered sibling so as |
| 1162 | * to shrink a directory over time. |
| 1163 | */ |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1164 | forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | for (i = 0, bp = NULL; i < 2; forward = !forward, i++) { |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1166 | blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | if (blkno == 0) |
| 1168 | continue; |
| 1169 | /* |
| 1170 | * Read the sibling leaf block. |
| 1171 | */ |
| 1172 | if ((error = |
| 1173 | xfs_da_read_buf(state->args->trans, state->args->dp, blkno, |
| 1174 | -1, &bp, XFS_DATA_FORK))) { |
| 1175 | return error; |
| 1176 | } |
| 1177 | ASSERT(bp != NULL); |
| 1178 | /* |
| 1179 | * Count bytes in the two blocks combined. |
| 1180 | */ |
| 1181 | leaf = (xfs_dir2_leaf_t *)info; |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1182 | count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | bytes = state->blocksize - (state->blocksize >> 2); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1184 | leaf = bp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1185 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1186 | count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | bytes -= count * (uint)sizeof(leaf->ents[0]); |
| 1188 | /* |
| 1189 | * Fits with at least 25% to spare. |
| 1190 | */ |
| 1191 | if (bytes >= 0) |
| 1192 | break; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1193 | xfs_trans_brelse(state->args->trans, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | } |
| 1195 | /* |
| 1196 | * Didn't like either block, give up. |
| 1197 | */ |
| 1198 | if (i >= 2) { |
| 1199 | *action = 0; |
| 1200 | return 0; |
| 1201 | } |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | /* |
| 1204 | * Make altpath point to the block we want to keep (the lower |
| 1205 | * numbered block) and path point to the block we want to drop. |
| 1206 | */ |
| 1207 | memcpy(&state->altpath, &state->path, sizeof(state->path)); |
| 1208 | if (blkno < blk->blkno) |
| 1209 | error = xfs_da_path_shift(state, &state->altpath, forward, 0, |
| 1210 | &rval); |
| 1211 | else |
| 1212 | error = xfs_da_path_shift(state, &state->path, forward, 0, |
| 1213 | &rval); |
| 1214 | if (error) { |
| 1215 | return error; |
| 1216 | } |
| 1217 | *action = rval ? 0 : 1; |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * Move all the leaf entries from drop_blk to save_blk. |
| 1223 | * This is done as part of a join operation. |
| 1224 | */ |
| 1225 | void |
| 1226 | xfs_dir2_leafn_unbalance( |
| 1227 | xfs_da_state_t *state, /* cursor */ |
| 1228 | xfs_da_state_blk_t *drop_blk, /* dead block */ |
| 1229 | xfs_da_state_blk_t *save_blk) /* surviving block */ |
| 1230 | { |
| 1231 | xfs_da_args_t *args; /* operation arguments */ |
| 1232 | xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */ |
| 1233 | xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */ |
| 1234 | |
| 1235 | args = state->args; |
| 1236 | ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC); |
| 1237 | ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1238 | drop_leaf = drop_blk->bp->b_addr; |
| 1239 | save_leaf = save_blk->bp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1240 | ASSERT(drop_leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)); |
| 1241 | ASSERT(save_leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | /* |
| 1243 | * If there are any stale leaf entries, take this opportunity |
| 1244 | * to purge them. |
| 1245 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1246 | if (drop_leaf->hdr.stale) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | xfs_dir2_leaf_compact(args, drop_blk->bp); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1248 | if (save_leaf->hdr.stale) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | xfs_dir2_leaf_compact(args, save_blk->bp); |
| 1250 | /* |
| 1251 | * Move the entries from drop to the appropriate end of save. |
| 1252 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1253 | drop_blk->hashval = be32_to_cpu(drop_leaf->ents[be16_to_cpu(drop_leaf->hdr.count) - 1].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp)) |
| 1255 | xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0, |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1256 | be16_to_cpu(drop_leaf->hdr.count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | else |
| 1258 | xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1259 | be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count)); |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1260 | save_blk->hashval = be32_to_cpu(save_leaf->ents[be16_to_cpu(save_leaf->hdr.count) - 1].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | xfs_dir2_leafn_check(args->dp, save_blk->bp); |
| 1262 | } |
| 1263 | |
| 1264 | /* |
| 1265 | * Top-level node form directory addname routine. |
| 1266 | */ |
| 1267 | int /* error */ |
| 1268 | xfs_dir2_node_addname( |
| 1269 | xfs_da_args_t *args) /* operation arguments */ |
| 1270 | { |
| 1271 | xfs_da_state_blk_t *blk; /* leaf block for insert */ |
| 1272 | int error; /* error return value */ |
| 1273 | int rval; /* sub-return value */ |
| 1274 | xfs_da_state_t *state; /* btree cursor */ |
| 1275 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1276 | trace_xfs_dir2_node_addname(args); |
| 1277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | /* |
| 1279 | * Allocate and initialize the state (btree cursor). |
| 1280 | */ |
| 1281 | state = xfs_da_state_alloc(); |
| 1282 | state->args = args; |
| 1283 | state->mp = args->dp->i_mount; |
| 1284 | state->blocksize = state->mp->m_dirblksize; |
| 1285 | state->node_ents = state->mp->m_dir_node_ents; |
| 1286 | /* |
| 1287 | * Look up the name. We're not supposed to find it, but |
| 1288 | * this gives us the insertion point. |
| 1289 | */ |
| 1290 | error = xfs_da_node_lookup_int(state, &rval); |
| 1291 | if (error) |
| 1292 | rval = error; |
| 1293 | if (rval != ENOENT) { |
| 1294 | goto done; |
| 1295 | } |
| 1296 | /* |
| 1297 | * Add the data entry to a data block. |
| 1298 | * Extravalid is set to a freeblock found by lookup. |
| 1299 | */ |
| 1300 | rval = xfs_dir2_node_addname_int(args, |
| 1301 | state->extravalid ? &state->extrablk : NULL); |
| 1302 | if (rval) { |
| 1303 | goto done; |
| 1304 | } |
| 1305 | blk = &state->path.blk[state->path.active - 1]; |
| 1306 | ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); |
| 1307 | /* |
| 1308 | * Add the new leaf entry. |
| 1309 | */ |
| 1310 | rval = xfs_dir2_leafn_add(blk->bp, args, blk->index); |
| 1311 | if (rval == 0) { |
| 1312 | /* |
| 1313 | * It worked, fix the hash values up the btree. |
| 1314 | */ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 1315 | if (!(args->op_flags & XFS_DA_OP_JUSTCHECK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | xfs_da_fixhashpath(state, &state->path); |
| 1317 | } else { |
| 1318 | /* |
| 1319 | * It didn't work, we need to split the leaf block. |
| 1320 | */ |
| 1321 | if (args->total == 0) { |
| 1322 | ASSERT(rval == ENOSPC); |
| 1323 | goto done; |
| 1324 | } |
| 1325 | /* |
| 1326 | * Split the leaf block and insert the new entry. |
| 1327 | */ |
| 1328 | rval = xfs_da_split(state); |
| 1329 | } |
| 1330 | done: |
| 1331 | xfs_da_state_free(state); |
| 1332 | return rval; |
| 1333 | } |
| 1334 | |
| 1335 | /* |
| 1336 | * Add the data entry for a node-format directory name addition. |
| 1337 | * The leaf entry is added in xfs_dir2_leafn_add. |
| 1338 | * We may enter with a freespace block that the lookup found. |
| 1339 | */ |
| 1340 | static int /* error */ |
| 1341 | xfs_dir2_node_addname_int( |
| 1342 | xfs_da_args_t *args, /* operation arguments */ |
| 1343 | xfs_da_state_blk_t *fblk) /* optional freespace block */ |
| 1344 | { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1345 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | xfs_dir2_db_t dbno; /* data block number */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1347 | struct xfs_buf *dbp; /* data block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | xfs_dir2_data_entry_t *dep; /* data entry pointer */ |
| 1349 | xfs_inode_t *dp; /* incore directory inode */ |
| 1350 | xfs_dir2_data_unused_t *dup; /* data unused entry pointer */ |
| 1351 | int error; /* error return value */ |
| 1352 | xfs_dir2_db_t fbno; /* freespace block number */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1353 | struct xfs_buf *fbp; /* freespace buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | int findex; /* freespace entry index */ |
| 1355 | xfs_dir2_free_t *free=NULL; /* freespace block structure */ |
| 1356 | xfs_dir2_db_t ifbno; /* initial freespace block no */ |
| 1357 | xfs_dir2_db_t lastfbno=0; /* highest freespace block no */ |
| 1358 | int length; /* length of the new entry */ |
| 1359 | int logfree; /* need to log free entry */ |
| 1360 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1361 | int needlog; /* need to log data header */ |
| 1362 | int needscan; /* need to rescan data frees */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 1363 | __be16 *tagp; /* data entry tag pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | xfs_trans_t *tp; /* transaction pointer */ |
| 1365 | |
| 1366 | dp = args->dp; |
| 1367 | mp = dp->i_mount; |
| 1368 | tp = args->trans; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1369 | length = xfs_dir2_data_entsize(args->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | /* |
| 1371 | * If we came in with a freespace block that means that lookup |
| 1372 | * found an entry with our hash value. This is the freespace |
| 1373 | * block for that data entry. |
| 1374 | */ |
| 1375 | if (fblk) { |
| 1376 | fbp = fblk->bp; |
| 1377 | /* |
| 1378 | * Remember initial freespace block number. |
| 1379 | */ |
| 1380 | ifbno = fblk->blkno; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1381 | free = fbp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1382 | ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | findex = fblk->index; |
| 1384 | /* |
| 1385 | * This means the free entry showed that the data block had |
| 1386 | * space for our entry, so we remembered it. |
| 1387 | * Use that data block. |
| 1388 | */ |
| 1389 | if (findex >= 0) { |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1390 | ASSERT(findex < be32_to_cpu(free->hdr.nvalid)); |
| 1391 | ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF); |
| 1392 | ASSERT(be16_to_cpu(free->bests[findex]) >= length); |
| 1393 | dbno = be32_to_cpu(free->hdr.firstdb) + findex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | } |
| 1395 | /* |
| 1396 | * The data block looked at didn't have enough room. |
| 1397 | * We'll start at the beginning of the freespace entries. |
| 1398 | */ |
| 1399 | else { |
| 1400 | dbno = -1; |
| 1401 | findex = 0; |
| 1402 | } |
| 1403 | } |
| 1404 | /* |
| 1405 | * Didn't come in with a freespace block, so don't have a data block. |
| 1406 | */ |
| 1407 | else { |
| 1408 | ifbno = dbno = -1; |
| 1409 | fbp = NULL; |
| 1410 | findex = 0; |
| 1411 | } |
| 1412 | /* |
| 1413 | * If we don't have a data block yet, we're going to scan the |
| 1414 | * freespace blocks looking for one. Figure out what the |
| 1415 | * highest freespace block number is. |
| 1416 | */ |
| 1417 | if (dbno == -1) { |
| 1418 | xfs_fileoff_t fo; /* freespace block number */ |
| 1419 | |
| 1420 | if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) |
| 1421 | return error; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1422 | lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | fbno = ifbno; |
| 1424 | } |
| 1425 | /* |
| 1426 | * While we haven't identified a data block, search the freeblock |
| 1427 | * data for a good data block. If we find a null freeblock entry, |
| 1428 | * indicating a hole in the data blocks, remember that. |
| 1429 | */ |
| 1430 | while (dbno == -1) { |
| 1431 | /* |
| 1432 | * If we don't have a freeblock in hand, get the next one. |
| 1433 | */ |
| 1434 | if (fbp == NULL) { |
| 1435 | /* |
| 1436 | * Happens the first time through unless lookup gave |
| 1437 | * us a freespace block to start with. |
| 1438 | */ |
| 1439 | if (++fbno == 0) |
| 1440 | fbno = XFS_DIR2_FREE_FIRSTDB(mp); |
| 1441 | /* |
| 1442 | * If it's ifbno we already looked at it. |
| 1443 | */ |
| 1444 | if (fbno == ifbno) |
| 1445 | fbno++; |
| 1446 | /* |
| 1447 | * If it's off the end we're done. |
| 1448 | */ |
| 1449 | if (fbno >= lastfbno) |
| 1450 | break; |
| 1451 | /* |
| 1452 | * Read the block. There can be holes in the |
| 1453 | * freespace blocks, so this might not succeed. |
| 1454 | * This should be really rare, so there's no reason |
| 1455 | * to avoid it. |
| 1456 | */ |
| 1457 | if ((error = xfs_da_read_buf(tp, dp, |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1458 | xfs_dir2_db_to_da(mp, fbno), -2, &fbp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | XFS_DATA_FORK))) { |
| 1460 | return error; |
| 1461 | } |
| 1462 | if (unlikely(fbp == NULL)) { |
| 1463 | continue; |
| 1464 | } |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1465 | free = fbp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1466 | ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | findex = 0; |
| 1468 | } |
| 1469 | /* |
| 1470 | * Look at the current free entry. Is it good enough? |
| 1471 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1472 | if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF && |
| 1473 | be16_to_cpu(free->bests[findex]) >= length) |
| 1474 | dbno = be32_to_cpu(free->hdr.firstdb) + findex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | else { |
| 1476 | /* |
| 1477 | * Are we done with the freeblock? |
| 1478 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1479 | if (++findex == be32_to_cpu(free->hdr.nvalid)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | /* |
| 1481 | * Drop the block. |
| 1482 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1483 | xfs_trans_brelse(tp, fbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | fbp = NULL; |
| 1485 | if (fblk && fblk->bp) |
| 1486 | fblk->bp = NULL; |
| 1487 | } |
| 1488 | } |
| 1489 | } |
| 1490 | /* |
| 1491 | * If we don't have a data block, we need to allocate one and make |
| 1492 | * the freespace entries refer to it. |
| 1493 | */ |
| 1494 | if (unlikely(dbno == -1)) { |
| 1495 | /* |
| 1496 | * Not allowed to allocate, return failure. |
| 1497 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1498 | if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | return XFS_ERROR(ENOSPC); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | /* |
| 1502 | * Allocate and initialize the new data block. |
| 1503 | */ |
| 1504 | if (unlikely((error = xfs_dir2_grow_inode(args, |
| 1505 | XFS_DIR2_DATA_SPACE, |
| 1506 | &dbno)) || |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1507 | (error = xfs_dir2_data_init(args, dbno, &dbp)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | return error; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | /* |
| 1511 | * If (somehow) we have a freespace block, get rid of it. |
| 1512 | */ |
| 1513 | if (fbp) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1514 | xfs_trans_brelse(tp, fbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | if (fblk && fblk->bp) |
| 1516 | fblk->bp = NULL; |
| 1517 | |
| 1518 | /* |
| 1519 | * Get the freespace block corresponding to the data block |
| 1520 | * that was just allocated. |
| 1521 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1522 | fbno = xfs_dir2_db_to_fdb(mp, dbno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | if (unlikely(error = xfs_da_read_buf(tp, dp, |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1524 | xfs_dir2_db_to_da(mp, fbno), -2, &fbp, |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1525 | XFS_DATA_FORK))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | return error; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | /* |
| 1529 | * If there wasn't a freespace block, the read will |
| 1530 | * return a NULL fbp. Allocate and initialize a new one. |
| 1531 | */ |
| 1532 | if( fbp == NULL ) { |
| 1533 | if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, |
| 1534 | &fbno))) { |
| 1535 | return error; |
| 1536 | } |
| 1537 | |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1538 | if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1539 | xfs_alert(mp, |
Jean Delvare | df2e301 | 2011-07-16 18:10:35 +0200 | [diff] [blame] | 1540 | "%s: dir ino %llu needed freesp block %lld for\n" |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1541 | " data block %lld, got %lld ifbno %llu lastfbno %d", |
| 1542 | __func__, (unsigned long long)dp->i_ino, |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1543 | (long long)xfs_dir2_db_to_fdb(mp, dbno), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | (long long)dbno, (long long)fbno, |
| 1545 | (unsigned long long)ifbno, lastfbno); |
| 1546 | if (fblk) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1547 | xfs_alert(mp, |
| 1548 | " fblk 0x%p blkno %llu index %d magic 0x%x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | fblk, |
| 1550 | (unsigned long long)fblk->blkno, |
| 1551 | fblk->index, |
| 1552 | fblk->magic); |
| 1553 | } else { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1554 | xfs_alert(mp, " ... fblk is NULL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | } |
| 1556 | XFS_ERROR_REPORT("xfs_dir2_node_addname_int", |
| 1557 | XFS_ERRLEVEL_LOW, mp); |
| 1558 | return XFS_ERROR(EFSCORRUPTED); |
| 1559 | } |
| 1560 | |
| 1561 | /* |
| 1562 | * Get a buffer for the new block. |
| 1563 | */ |
| 1564 | if ((error = xfs_da_get_buf(tp, dp, |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1565 | xfs_dir2_db_to_da(mp, fbno), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | -1, &fbp, XFS_DATA_FORK))) { |
| 1567 | return error; |
| 1568 | } |
| 1569 | ASSERT(fbp != NULL); |
| 1570 | |
| 1571 | /* |
| 1572 | * Initialize the new block to be empty, and remember |
| 1573 | * its first slot as our empty slot. |
| 1574 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1575 | free = fbp->b_addr; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1576 | free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC); |
| 1577 | free->hdr.firstdb = cpu_to_be32( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) * |
Christoph Hellwig | a00b774 | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 1579 | xfs_dir2_free_max_bests(mp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | free->hdr.nvalid = 0; |
| 1581 | free->hdr.nused = 0; |
| 1582 | } else { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1583 | free = fbp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1584 | ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | /* |
| 1588 | * Set the freespace block index from the data block number. |
| 1589 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1590 | findex = xfs_dir2_db_to_fdindex(mp, dbno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | /* |
| 1592 | * If it's after the end of the current entries in the |
| 1593 | * freespace block, extend that table. |
| 1594 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1595 | if (findex >= be32_to_cpu(free->hdr.nvalid)) { |
Christoph Hellwig | a00b774 | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 1596 | ASSERT(findex < xfs_dir2_free_max_bests(mp)); |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1597 | free->hdr.nvalid = cpu_to_be32(findex + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | /* |
| 1599 | * Tag new entry so nused will go up. |
| 1600 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1601 | free->bests[findex] = cpu_to_be16(NULLDATAOFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | } |
| 1603 | /* |
| 1604 | * If this entry was for an empty data block |
| 1605 | * (this should always be true) then update the header. |
| 1606 | */ |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1607 | if (free->bests[findex] == cpu_to_be16(NULLDATAOFF)) { |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1608 | be32_add_cpu(&free->hdr.nused, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | xfs_dir2_free_log_header(tp, fbp); |
| 1610 | } |
| 1611 | /* |
| 1612 | * Update the real value in the table. |
| 1613 | * We haven't allocated the data entry yet so this will |
| 1614 | * change again. |
| 1615 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1616 | hdr = dbp->b_addr; |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1617 | free->bests[findex] = hdr->bestfree[0].length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | logfree = 1; |
| 1619 | } |
| 1620 | /* |
| 1621 | * We had a data block so we don't have to make a new one. |
| 1622 | */ |
| 1623 | else { |
| 1624 | /* |
| 1625 | * If just checking, we succeeded. |
| 1626 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1627 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | return 0; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | /* |
| 1631 | * Read the data block in. |
| 1632 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1633 | error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno), |
| 1634 | -1, &dbp, XFS_DATA_FORK); |
| 1635 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | return error; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1637 | hdr = dbp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | logfree = 0; |
| 1639 | } |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1640 | ASSERT(be16_to_cpu(hdr->bestfree[0].length) >= length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | /* |
| 1642 | * Point to the existing unused space. |
| 1643 | */ |
| 1644 | dup = (xfs_dir2_data_unused_t *) |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1645 | ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | needscan = needlog = 0; |
| 1647 | /* |
| 1648 | * Mark the first part of the unused space, inuse for us. |
| 1649 | */ |
| 1650 | xfs_dir2_data_use_free(tp, dbp, dup, |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1651 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | &needlog, &needscan); |
| 1653 | /* |
| 1654 | * Fill in the new entry and log it. |
| 1655 | */ |
| 1656 | dep = (xfs_dir2_data_entry_t *)dup; |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 1657 | dep->inumber = cpu_to_be64(args->inumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | dep->namelen = args->namelen; |
| 1659 | memcpy(dep->name, args->name, dep->namelen); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1660 | tagp = xfs_dir2_data_entry_tag_p(dep); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1661 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | xfs_dir2_data_log_entry(tp, dbp, dep); |
| 1663 | /* |
| 1664 | * Rescan the block for bestfree if needed. |
| 1665 | */ |
| 1666 | if (needscan) |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1667 | xfs_dir2_data_freescan(mp, hdr, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | /* |
| 1669 | * Log the data block header if needed. |
| 1670 | */ |
| 1671 | if (needlog) |
| 1672 | xfs_dir2_data_log_header(tp, dbp); |
| 1673 | /* |
| 1674 | * If the freespace entry is now wrong, update it. |
| 1675 | */ |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1676 | if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(hdr->bestfree[0].length)) { |
| 1677 | free->bests[findex] = hdr->bestfree[0].length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | logfree = 1; |
| 1679 | } |
| 1680 | /* |
| 1681 | * Log the freespace entry if needed. |
| 1682 | */ |
| 1683 | if (logfree) |
| 1684 | xfs_dir2_free_log_bests(tp, fbp, findex, findex); |
| 1685 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | * Return the data block and offset in args, then drop the data block. |
| 1687 | */ |
| 1688 | args->blkno = (xfs_dablk_t)dbno; |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 1689 | args->index = be16_to_cpu(*tagp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | return 0; |
| 1691 | } |
| 1692 | |
| 1693 | /* |
| 1694 | * Lookup an entry in a node-format directory. |
| 1695 | * All the real work happens in xfs_da_node_lookup_int. |
| 1696 | * The only real output is the inode number of the entry. |
| 1697 | */ |
| 1698 | int /* error */ |
| 1699 | xfs_dir2_node_lookup( |
| 1700 | xfs_da_args_t *args) /* operation arguments */ |
| 1701 | { |
| 1702 | int error; /* error return value */ |
| 1703 | int i; /* btree level */ |
| 1704 | int rval; /* operation return value */ |
| 1705 | xfs_da_state_t *state; /* btree cursor */ |
| 1706 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1707 | trace_xfs_dir2_node_lookup(args); |
| 1708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | /* |
| 1710 | * Allocate and initialize the btree cursor. |
| 1711 | */ |
| 1712 | state = xfs_da_state_alloc(); |
| 1713 | state->args = args; |
| 1714 | state->mp = args->dp->i_mount; |
| 1715 | state->blocksize = state->mp->m_dirblksize; |
| 1716 | state->node_ents = state->mp->m_dir_node_ents; |
| 1717 | /* |
| 1718 | * Fill in the path to the entry in the cursor. |
| 1719 | */ |
| 1720 | error = xfs_da_node_lookup_int(state, &rval); |
| 1721 | if (error) |
| 1722 | rval = error; |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 1723 | else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) { |
| 1724 | /* If a CI match, dup the actual name and return EEXIST */ |
| 1725 | xfs_dir2_data_entry_t *dep; |
| 1726 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1727 | dep = (xfs_dir2_data_entry_t *) |
| 1728 | ((char *)state->extrablk.bp->b_addr + |
| 1729 | state->extrablk.index); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 1730 | rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen); |
| 1731 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | /* |
| 1733 | * Release the btree blocks and leaf block. |
| 1734 | */ |
| 1735 | for (i = 0; i < state->path.active; i++) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1736 | xfs_trans_brelse(args->trans, state->path.blk[i].bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | state->path.blk[i].bp = NULL; |
| 1738 | } |
| 1739 | /* |
| 1740 | * Release the data block if we have it. |
| 1741 | */ |
| 1742 | if (state->extravalid && state->extrablk.bp) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1743 | xfs_trans_brelse(args->trans, state->extrablk.bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | state->extrablk.bp = NULL; |
| 1745 | } |
| 1746 | xfs_da_state_free(state); |
| 1747 | return rval; |
| 1748 | } |
| 1749 | |
| 1750 | /* |
| 1751 | * Remove an entry from a node-format directory. |
| 1752 | */ |
| 1753 | int /* error */ |
| 1754 | xfs_dir2_node_removename( |
| 1755 | xfs_da_args_t *args) /* operation arguments */ |
| 1756 | { |
| 1757 | xfs_da_state_blk_t *blk; /* leaf block */ |
| 1758 | int error; /* error return value */ |
| 1759 | int rval; /* operation return value */ |
| 1760 | xfs_da_state_t *state; /* btree cursor */ |
| 1761 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1762 | trace_xfs_dir2_node_removename(args); |
| 1763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | /* |
| 1765 | * Allocate and initialize the btree cursor. |
| 1766 | */ |
| 1767 | state = xfs_da_state_alloc(); |
| 1768 | state->args = args; |
| 1769 | state->mp = args->dp->i_mount; |
| 1770 | state->blocksize = state->mp->m_dirblksize; |
| 1771 | state->node_ents = state->mp->m_dir_node_ents; |
| 1772 | /* |
| 1773 | * Look up the entry we're deleting, set up the cursor. |
| 1774 | */ |
| 1775 | error = xfs_da_node_lookup_int(state, &rval); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1776 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | rval = error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | /* |
| 1779 | * Didn't find it, upper layer screwed up. |
| 1780 | */ |
| 1781 | if (rval != EEXIST) { |
| 1782 | xfs_da_state_free(state); |
| 1783 | return rval; |
| 1784 | } |
| 1785 | blk = &state->path.blk[state->path.active - 1]; |
| 1786 | ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); |
| 1787 | ASSERT(state->extravalid); |
| 1788 | /* |
| 1789 | * Remove the leaf and data entries. |
| 1790 | * Extrablk refers to the data block. |
| 1791 | */ |
| 1792 | error = xfs_dir2_leafn_remove(args, blk->bp, blk->index, |
| 1793 | &state->extrablk, &rval); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1794 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | /* |
| 1797 | * Fix the hash values up the btree. |
| 1798 | */ |
| 1799 | xfs_da_fixhashpath(state, &state->path); |
| 1800 | /* |
| 1801 | * If we need to join leaf blocks, do it. |
| 1802 | */ |
| 1803 | if (rval && state->path.active > 1) |
| 1804 | error = xfs_da_join(state); |
| 1805 | /* |
| 1806 | * If no errors so far, try conversion to leaf format. |
| 1807 | */ |
| 1808 | if (!error) |
| 1809 | error = xfs_dir2_node_to_leaf(state); |
| 1810 | xfs_da_state_free(state); |
| 1811 | return error; |
| 1812 | } |
| 1813 | |
| 1814 | /* |
| 1815 | * Replace an entry's inode number in a node-format directory. |
| 1816 | */ |
| 1817 | int /* error */ |
| 1818 | xfs_dir2_node_replace( |
| 1819 | xfs_da_args_t *args) /* operation arguments */ |
| 1820 | { |
| 1821 | xfs_da_state_blk_t *blk; /* leaf block */ |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1822 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | xfs_dir2_data_entry_t *dep; /* data entry changed */ |
| 1824 | int error; /* error return value */ |
| 1825 | int i; /* btree level */ |
| 1826 | xfs_ino_t inum; /* new inode number */ |
| 1827 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1828 | xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */ |
| 1829 | int rval; /* internal return value */ |
| 1830 | xfs_da_state_t *state; /* btree cursor */ |
| 1831 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1832 | trace_xfs_dir2_node_replace(args); |
| 1833 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | /* |
| 1835 | * Allocate and initialize the btree cursor. |
| 1836 | */ |
| 1837 | state = xfs_da_state_alloc(); |
| 1838 | state->args = args; |
| 1839 | state->mp = args->dp->i_mount; |
| 1840 | state->blocksize = state->mp->m_dirblksize; |
| 1841 | state->node_ents = state->mp->m_dir_node_ents; |
| 1842 | inum = args->inumber; |
| 1843 | /* |
| 1844 | * Lookup the entry to change in the btree. |
| 1845 | */ |
| 1846 | error = xfs_da_node_lookup_int(state, &rval); |
| 1847 | if (error) { |
| 1848 | rval = error; |
| 1849 | } |
| 1850 | /* |
| 1851 | * It should be found, since the vnodeops layer has looked it up |
| 1852 | * and locked it. But paranoia is good. |
| 1853 | */ |
| 1854 | if (rval == EEXIST) { |
| 1855 | /* |
| 1856 | * Find the leaf entry. |
| 1857 | */ |
| 1858 | blk = &state->path.blk[state->path.active - 1]; |
| 1859 | ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1860 | leaf = blk->bp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | lep = &leaf->ents[blk->index]; |
| 1862 | ASSERT(state->extravalid); |
| 1863 | /* |
| 1864 | * Point to the data entry. |
| 1865 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1866 | hdr = state->extrablk.bp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1867 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | dep = (xfs_dir2_data_entry_t *) |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1869 | ((char *)hdr + |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1870 | xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address))); |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 1871 | ASSERT(inum != be64_to_cpu(dep->inumber)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | /* |
| 1873 | * Fill in the new inode number and log the entry. |
| 1874 | */ |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 1875 | dep->inumber = cpu_to_be64(inum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep); |
| 1877 | rval = 0; |
| 1878 | } |
| 1879 | /* |
| 1880 | * Didn't find it, and we're holding a data block. Drop it. |
| 1881 | */ |
| 1882 | else if (state->extravalid) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1883 | xfs_trans_brelse(args->trans, state->extrablk.bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | state->extrablk.bp = NULL; |
| 1885 | } |
| 1886 | /* |
| 1887 | * Release all the buffers in the cursor. |
| 1888 | */ |
| 1889 | for (i = 0; i < state->path.active; i++) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1890 | xfs_trans_brelse(args->trans, state->path.blk[i].bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | state->path.blk[i].bp = NULL; |
| 1892 | } |
| 1893 | xfs_da_state_free(state); |
| 1894 | return rval; |
| 1895 | } |
| 1896 | |
| 1897 | /* |
| 1898 | * Trim off a trailing empty freespace block. |
| 1899 | * Return (in rvalp) 1 if we did it, 0 if not. |
| 1900 | */ |
| 1901 | int /* error */ |
| 1902 | xfs_dir2_node_trim_free( |
| 1903 | xfs_da_args_t *args, /* operation arguments */ |
| 1904 | xfs_fileoff_t fo, /* free block number */ |
| 1905 | int *rvalp) /* out: did something */ |
| 1906 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1907 | struct xfs_buf *bp; /* freespace buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | xfs_inode_t *dp; /* incore directory inode */ |
| 1909 | int error; /* error return code */ |
| 1910 | xfs_dir2_free_t *free; /* freespace structure */ |
| 1911 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1912 | xfs_trans_t *tp; /* transaction pointer */ |
| 1913 | |
| 1914 | dp = args->dp; |
| 1915 | mp = dp->i_mount; |
| 1916 | tp = args->trans; |
| 1917 | /* |
| 1918 | * Read the freespace block. |
| 1919 | */ |
| 1920 | if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp, |
| 1921 | XFS_DATA_FORK))) { |
| 1922 | return error; |
| 1923 | } |
| 1924 | |
| 1925 | /* |
| 1926 | * There can be holes in freespace. If fo is a hole, there's |
| 1927 | * nothing to do. |
| 1928 | */ |
| 1929 | if (bp == NULL) { |
| 1930 | return 0; |
| 1931 | } |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1932 | free = bp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1933 | ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | /* |
| 1935 | * If there are used entries, there's nothing to do. |
| 1936 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1937 | if (be32_to_cpu(free->hdr.nused) > 0) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1938 | xfs_trans_brelse(tp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | *rvalp = 0; |
| 1940 | return 0; |
| 1941 | } |
| 1942 | /* |
| 1943 | * Blow the block away. |
| 1944 | */ |
| 1945 | if ((error = |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1946 | xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | bp))) { |
| 1948 | /* |
| 1949 | * Can't fail with ENOSPC since that only happens with no |
| 1950 | * space reservation, when breaking up an extent into two |
| 1951 | * pieces. This is the last block of an extent. |
| 1952 | */ |
| 1953 | ASSERT(error != ENOSPC); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1954 | xfs_trans_brelse(tp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | return error; |
| 1956 | } |
| 1957 | /* |
| 1958 | * Return that we succeeded. |
| 1959 | */ |
| 1960 | *rvalp = 1; |
| 1961 | return 0; |
| 1962 | } |