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-2003,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" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 22 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "xfs_trans.h" |
| 24 | #include "xfs_sb.h" |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 25 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_dir2.h" |
| 27 | #include "xfs_dmapi.h" |
| 28 | #include "xfs_mount.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_dinode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_dir2_data.h" |
| 37 | #include "xfs_dir2_leaf.h" |
| 38 | #include "xfs_dir2_block.h" |
| 39 | #include "xfs_dir2_trace.h" |
| 40 | #include "xfs_error.h" |
| 41 | |
| 42 | /* |
| 43 | * Local function prototypes. |
| 44 | */ |
| 45 | static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, xfs_dabuf_t *bp, int first, |
| 46 | int last); |
| 47 | static void xfs_dir2_block_log_tail(xfs_trans_t *tp, xfs_dabuf_t *bp); |
| 48 | static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **bpp, |
| 49 | int *entno); |
| 50 | static int xfs_dir2_block_sort(const void *a, const void *b); |
| 51 | |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 52 | static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot; |
| 53 | |
| 54 | /* |
| 55 | * One-time startup routine called from xfs_init(). |
| 56 | */ |
| 57 | void |
| 58 | xfs_dir_startup(void) |
| 59 | { |
| 60 | xfs_dir_hash_dot = xfs_da_hashname(".", 1); |
| 61 | xfs_dir_hash_dotdot = xfs_da_hashname("..", 2); |
| 62 | } |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | /* |
| 65 | * Add an entry to a block directory. |
| 66 | */ |
| 67 | int /* error */ |
| 68 | xfs_dir2_block_addname( |
| 69 | xfs_da_args_t *args) /* directory op arguments */ |
| 70 | { |
| 71 | xfs_dir2_data_free_t *bf; /* bestfree table in block */ |
| 72 | xfs_dir2_block_t *block; /* directory block structure */ |
| 73 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
| 74 | xfs_dabuf_t *bp; /* buffer for block */ |
| 75 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 76 | int compact; /* need to compact leaf ents */ |
| 77 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 78 | xfs_inode_t *dp; /* directory inode */ |
| 79 | xfs_dir2_data_unused_t *dup; /* block unused entry */ |
| 80 | int error; /* error return value */ |
| 81 | xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */ |
| 82 | xfs_dahash_t hash; /* hash value of found entry */ |
| 83 | int high; /* high index for binary srch */ |
| 84 | int highstale; /* high stale index */ |
| 85 | int lfloghigh=0; /* last final leaf to log */ |
| 86 | int lfloglow=0; /* first final leaf to log */ |
| 87 | int len; /* length of the new entry */ |
| 88 | int low; /* low index for binary srch */ |
| 89 | int lowstale; /* low stale index */ |
| 90 | int mid=0; /* midpoint for binary srch */ |
| 91 | xfs_mount_t *mp; /* filesystem mount point */ |
| 92 | int needlog; /* need to log header */ |
| 93 | int needscan; /* need to rescan freespace */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 94 | __be16 *tagp; /* pointer to tag value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | xfs_trans_t *tp; /* transaction structure */ |
| 96 | |
| 97 | xfs_dir2_trace_args("block_addname", args); |
| 98 | dp = args->dp; |
| 99 | tp = args->trans; |
| 100 | mp = dp->i_mount; |
| 101 | /* |
| 102 | * Read the (one and only) directory block into dabuf bp. |
| 103 | */ |
| 104 | if ((error = |
| 105 | xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) { |
| 106 | return error; |
| 107 | } |
| 108 | ASSERT(bp != NULL); |
| 109 | block = bp->data; |
| 110 | /* |
| 111 | * Check the magic number, corrupted if wrong. |
| 112 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 113 | if (unlikely(be32_to_cpu(block->hdr.magic) != XFS_DIR2_BLOCK_MAGIC)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | XFS_CORRUPTION_ERROR("xfs_dir2_block_addname", |
| 115 | XFS_ERRLEVEL_LOW, mp, block); |
| 116 | xfs_da_brelse(tp, bp); |
| 117 | return XFS_ERROR(EFSCORRUPTED); |
| 118 | } |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 119 | len = xfs_dir2_data_entsize(args->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | /* |
| 121 | * Set up pointers to parts of the block. |
| 122 | */ |
| 123 | bf = block->hdr.bestfree; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 124 | btp = xfs_dir2_block_tail_p(mp, block); |
| 125 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | /* |
| 127 | * No stale entries? Need space for entry and new leaf. |
| 128 | */ |
| 129 | if (!btp->stale) { |
| 130 | /* |
| 131 | * Tag just before the first leaf entry. |
| 132 | */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 133 | tagp = (__be16 *)blp - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | /* |
| 135 | * Data object just before the first leaf entry. |
| 136 | */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 137 | enddup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /* |
| 139 | * If it's not free then can't do this add without cleaning up: |
| 140 | * the space before the first leaf entry needs to be free so it |
| 141 | * can be expanded to hold the pointer to the new entry. |
| 142 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 143 | if (be16_to_cpu(enddup->freetag) != XFS_DIR2_DATA_FREE_TAG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | dup = enddup = NULL; |
| 145 | /* |
| 146 | * Check out the biggest freespace and see if it's the same one. |
| 147 | */ |
| 148 | else { |
| 149 | dup = (xfs_dir2_data_unused_t *) |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 150 | ((char *)block + be16_to_cpu(bf[0].offset)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | if (dup == enddup) { |
| 152 | /* |
| 153 | * It is the biggest freespace, is it too small |
| 154 | * to hold the new leaf too? |
| 155 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 156 | if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | /* |
| 158 | * Yes, we use the second-largest |
| 159 | * entry instead if it works. |
| 160 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 161 | if (be16_to_cpu(bf[1].length) >= len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | dup = (xfs_dir2_data_unused_t *) |
| 163 | ((char *)block + |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 164 | be16_to_cpu(bf[1].offset)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | else |
| 166 | dup = NULL; |
| 167 | } |
| 168 | } else { |
| 169 | /* |
| 170 | * Not the same free entry, |
| 171 | * just check its length. |
| 172 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 173 | if (be16_to_cpu(dup->length) < len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | dup = NULL; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | compact = 0; |
| 179 | } |
| 180 | /* |
| 181 | * If there are stale entries we'll use one for the leaf. |
| 182 | * Is the biggest entry enough to avoid compaction? |
| 183 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 184 | else if (be16_to_cpu(bf[0].length) >= len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | dup = (xfs_dir2_data_unused_t *) |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 186 | ((char *)block + be16_to_cpu(bf[0].offset)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | compact = 0; |
| 188 | } |
| 189 | /* |
| 190 | * Will need to compact to make this work. |
| 191 | */ |
| 192 | else { |
| 193 | /* |
| 194 | * Tag just before the first leaf entry. |
| 195 | */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 196 | tagp = (__be16 *)blp - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | /* |
| 198 | * Data object just before the first leaf entry. |
| 199 | */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 200 | dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | /* |
| 202 | * If it's not free then the data will go where the |
| 203 | * leaf data starts now, if it works at all. |
| 204 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 205 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 206 | if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | (uint)sizeof(*blp) < len) |
| 208 | dup = NULL; |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 209 | } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | dup = NULL; |
| 211 | else |
| 212 | dup = (xfs_dir2_data_unused_t *)blp; |
| 213 | compact = 1; |
| 214 | } |
| 215 | /* |
| 216 | * If this isn't a real add, we're done with the buffer. |
| 217 | */ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 218 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | xfs_da_brelse(tp, bp); |
| 220 | /* |
| 221 | * If we don't have space for the new entry & leaf ... |
| 222 | */ |
| 223 | if (!dup) { |
| 224 | /* |
| 225 | * Not trying to actually do anything, or don't have |
| 226 | * a space reservation: return no-space. |
| 227 | */ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 228 | if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return XFS_ERROR(ENOSPC); |
| 230 | /* |
| 231 | * Convert to the next larger format. |
| 232 | * Then add the new entry in that format. |
| 233 | */ |
| 234 | error = xfs_dir2_block_to_leaf(args, bp); |
| 235 | xfs_da_buf_done(bp); |
| 236 | if (error) |
| 237 | return error; |
| 238 | return xfs_dir2_leaf_addname(args); |
| 239 | } |
| 240 | /* |
| 241 | * Just checking, and it would work, so say so. |
| 242 | */ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 243 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | return 0; |
| 245 | needlog = needscan = 0; |
| 246 | /* |
| 247 | * If need to compact the leaf entries, do it now. |
| 248 | * Leave the highest-numbered stale entry stale. |
| 249 | * XXX should be the one closest to mid but mid is not yet computed. |
| 250 | */ |
| 251 | if (compact) { |
| 252 | int fromidx; /* source leaf index */ |
| 253 | int toidx; /* target leaf index */ |
| 254 | |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 255 | for (fromidx = toidx = be32_to_cpu(btp->count) - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | highstale = lfloghigh = -1; |
| 257 | fromidx >= 0; |
| 258 | fromidx--) { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 259 | if (be32_to_cpu(blp[fromidx].address) == XFS_DIR2_NULL_DATAPTR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | if (highstale == -1) |
| 261 | highstale = toidx; |
| 262 | else { |
| 263 | if (lfloghigh == -1) |
| 264 | lfloghigh = toidx; |
| 265 | continue; |
| 266 | } |
| 267 | } |
| 268 | if (fromidx < toidx) |
| 269 | blp[toidx] = blp[fromidx]; |
| 270 | toidx--; |
| 271 | } |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 272 | lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1); |
| 273 | lfloghigh -= be32_to_cpu(btp->stale) - 1; |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 274 | be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | xfs_dir2_data_make_free(tp, bp, |
| 276 | (xfs_dir2_data_aoff_t)((char *)blp - (char *)block), |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 277 | (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | &needlog, &needscan); |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 279 | blp += be32_to_cpu(btp->stale) - 1; |
| 280 | btp->stale = cpu_to_be32(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | /* |
| 282 | * If we now need to rebuild the bestfree map, do so. |
| 283 | * This needs to happen before the next call to use_free. |
| 284 | */ |
| 285 | if (needscan) { |
Eric Sandeen | ef497f8 | 2007-05-08 13:48:49 +1000 | [diff] [blame] | 286 | xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | needscan = 0; |
| 288 | } |
| 289 | } |
| 290 | /* |
| 291 | * Set leaf logging boundaries to impossible state. |
| 292 | * For the no-stale case they're set explicitly. |
| 293 | */ |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 294 | else if (btp->stale) { |
| 295 | lfloglow = be32_to_cpu(btp->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | lfloghigh = -1; |
| 297 | } |
| 298 | /* |
| 299 | * Find the slot that's first lower than our hash value, -1 if none. |
| 300 | */ |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 301 | for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | mid = (low + high) >> 1; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 303 | if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | break; |
| 305 | if (hash < args->hashval) |
| 306 | low = mid + 1; |
| 307 | else |
| 308 | high = mid - 1; |
| 309 | } |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 310 | while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | mid--; |
| 312 | } |
| 313 | /* |
| 314 | * No stale entries, will use enddup space to hold new leaf. |
| 315 | */ |
| 316 | if (!btp->stale) { |
| 317 | /* |
| 318 | * Mark the space needed for the new leaf entry, now in use. |
| 319 | */ |
| 320 | xfs_dir2_data_use_free(tp, bp, enddup, |
| 321 | (xfs_dir2_data_aoff_t) |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 322 | ((char *)enddup - (char *)block + be16_to_cpu(enddup->length) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | sizeof(*blp)), |
| 324 | (xfs_dir2_data_aoff_t)sizeof(*blp), |
| 325 | &needlog, &needscan); |
| 326 | /* |
| 327 | * Update the tail (entry count). |
| 328 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 329 | be32_add_cpu(&btp->count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | /* |
| 331 | * If we now need to rebuild the bestfree map, do so. |
| 332 | * This needs to happen before the next call to use_free. |
| 333 | */ |
| 334 | if (needscan) { |
| 335 | xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, |
Eric Sandeen | ef497f8 | 2007-05-08 13:48:49 +1000 | [diff] [blame] | 336 | &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | needscan = 0; |
| 338 | } |
| 339 | /* |
| 340 | * Adjust pointer to the first leaf entry, we're about to move |
| 341 | * the table up one to open up space for the new leaf entry. |
| 342 | * Then adjust our index to match. |
| 343 | */ |
| 344 | blp--; |
| 345 | mid++; |
| 346 | if (mid) |
| 347 | memmove(blp, &blp[1], mid * sizeof(*blp)); |
| 348 | lfloglow = 0; |
| 349 | lfloghigh = mid; |
| 350 | } |
| 351 | /* |
| 352 | * Use a stale leaf for our new entry. |
| 353 | */ |
| 354 | else { |
| 355 | for (lowstale = mid; |
| 356 | lowstale >= 0 && |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 357 | be32_to_cpu(blp[lowstale].address) != XFS_DIR2_NULL_DATAPTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | lowstale--) |
| 359 | continue; |
| 360 | for (highstale = mid + 1; |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 361 | highstale < be32_to_cpu(btp->count) && |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 362 | be32_to_cpu(blp[highstale].address) != XFS_DIR2_NULL_DATAPTR && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | (lowstale < 0 || mid - lowstale > highstale - mid); |
| 364 | highstale++) |
| 365 | continue; |
| 366 | /* |
| 367 | * Move entries toward the low-numbered stale entry. |
| 368 | */ |
| 369 | if (lowstale >= 0 && |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 370 | (highstale == be32_to_cpu(btp->count) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | mid - lowstale <= highstale - mid)) { |
| 372 | if (mid - lowstale) |
| 373 | memmove(&blp[lowstale], &blp[lowstale + 1], |
| 374 | (mid - lowstale) * sizeof(*blp)); |
| 375 | lfloglow = MIN(lowstale, lfloglow); |
| 376 | lfloghigh = MAX(mid, lfloghigh); |
| 377 | } |
| 378 | /* |
| 379 | * Move entries toward the high-numbered stale entry. |
| 380 | */ |
| 381 | else { |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 382 | ASSERT(highstale < be32_to_cpu(btp->count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | mid++; |
| 384 | if (highstale - mid) |
| 385 | memmove(&blp[mid + 1], &blp[mid], |
| 386 | (highstale - mid) * sizeof(*blp)); |
| 387 | lfloglow = MIN(mid, lfloglow); |
| 388 | lfloghigh = MAX(highstale, lfloghigh); |
| 389 | } |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 390 | be32_add_cpu(&btp->stale, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
| 392 | /* |
| 393 | * Point to the new data entry. |
| 394 | */ |
| 395 | dep = (xfs_dir2_data_entry_t *)dup; |
| 396 | /* |
| 397 | * Fill in the leaf entry. |
| 398 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 399 | blp[mid].hashval = cpu_to_be32(args->hashval); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 400 | blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 401 | (char *)dep - (char *)block)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh); |
| 403 | /* |
| 404 | * Mark space for the data entry used. |
| 405 | */ |
| 406 | xfs_dir2_data_use_free(tp, bp, dup, |
| 407 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)block), |
| 408 | (xfs_dir2_data_aoff_t)len, &needlog, &needscan); |
| 409 | /* |
| 410 | * Create the new data entry. |
| 411 | */ |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 412 | dep->inumber = cpu_to_be64(args->inumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | dep->namelen = args->namelen; |
| 414 | memcpy(dep->name, args->name, args->namelen); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 415 | tagp = xfs_dir2_data_entry_tag_p(dep); |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 416 | *tagp = cpu_to_be16((char *)dep - (char *)block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | /* |
| 418 | * Clean up the bestfree array and log the header, tail, and entry. |
| 419 | */ |
| 420 | if (needscan) |
Eric Sandeen | ef497f8 | 2007-05-08 13:48:49 +1000 | [diff] [blame] | 421 | xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | if (needlog) |
| 423 | xfs_dir2_data_log_header(tp, bp); |
| 424 | xfs_dir2_block_log_tail(tp, bp); |
| 425 | xfs_dir2_data_log_entry(tp, bp, dep); |
| 426 | xfs_dir2_data_check(dp, bp); |
| 427 | xfs_da_buf_done(bp); |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | * Readdir for block directories. |
| 433 | */ |
| 434 | int /* error */ |
| 435 | xfs_dir2_block_getdents( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | xfs_inode_t *dp, /* incore inode */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 437 | void *dirent, |
| 438 | xfs_off_t *offset, |
| 439 | filldir_t filldir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
| 441 | xfs_dir2_block_t *block; /* directory block structure */ |
| 442 | xfs_dabuf_t *bp; /* buffer for block */ |
| 443 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 444 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 445 | xfs_dir2_data_unused_t *dup; /* block unused entry */ |
| 446 | char *endptr; /* end of the data entries */ |
| 447 | int error; /* error return value */ |
| 448 | xfs_mount_t *mp; /* filesystem mount point */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | char *ptr; /* current data entry */ |
| 450 | int wantoff; /* starting block offset */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 451 | xfs_ino_t ino; |
| 452 | xfs_off_t cook; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
| 454 | mp = dp->i_mount; |
| 455 | /* |
| 456 | * If the block number in the offset is out of range, we're done. |
| 457 | */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 458 | if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | /* |
| 462 | * Can't read the block, give up, else get dabuf in bp. |
| 463 | */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 464 | error = xfs_da_read_buf(NULL, dp, mp->m_dirdatablk, -1, |
| 465 | &bp, XFS_DATA_FORK); |
| 466 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | return error; |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | ASSERT(bp != NULL); |
| 470 | /* |
| 471 | * Extract the byte offset we start at from the seek pointer. |
| 472 | * We'll skip entries before this. |
| 473 | */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 474 | wantoff = xfs_dir2_dataptr_to_off(mp, *offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | block = bp->data; |
| 476 | xfs_dir2_data_check(dp, bp); |
| 477 | /* |
| 478 | * Set up values for the loop. |
| 479 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 480 | btp = xfs_dir2_block_tail_p(mp, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | ptr = (char *)block->u; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 482 | endptr = (char *)xfs_dir2_block_leaf_p(btp); |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | /* |
| 485 | * Loop over the data portion of the block. |
| 486 | * Each object is a real entry (dep) or an unused one (dup). |
| 487 | */ |
| 488 | while (ptr < endptr) { |
| 489 | dup = (xfs_dir2_data_unused_t *)ptr; |
| 490 | /* |
| 491 | * Unused, skip it. |
| 492 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 493 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
| 494 | ptr += be16_to_cpu(dup->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | continue; |
| 496 | } |
| 497 | |
| 498 | dep = (xfs_dir2_data_entry_t *)ptr; |
| 499 | |
| 500 | /* |
| 501 | * Bump pointer for the next iteration. |
| 502 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 503 | ptr += xfs_dir2_data_entsize(dep->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | /* |
| 505 | * The entry is before the desired starting point, skip it. |
| 506 | */ |
| 507 | if ((char *)dep - (char *)block < wantoff) |
| 508 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 510 | cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk, |
Lachlan McIlroy | 041388b | 2007-12-18 16:19:34 +1100 | [diff] [blame] | 511 | (char *)dep - (char *)block); |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 512 | ino = be64_to_cpu(dep->inumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | #if XFS_BIG_INUMS |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 514 | ino += mp->m_inoadd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
| 517 | /* |
| 518 | * If it didn't fit, set the final offset to here & return. |
| 519 | */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 520 | if (filldir(dirent, dep->name, dep->namelen, cook, |
| 521 | ino, DT_UNKNOWN)) { |
Lachlan McIlroy | 041388b | 2007-12-18 16:19:34 +1100 | [diff] [blame] | 522 | *offset = cook; |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 523 | xfs_da_brelse(NULL, bp); |
| 524 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * Reached the end of the block. |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 530 | * Set the offset to a non-existent block 1 and return. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 532 | *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0); |
| 533 | xfs_da_brelse(NULL, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * Log leaf entries from the block. |
| 539 | */ |
| 540 | static void |
| 541 | xfs_dir2_block_log_leaf( |
| 542 | xfs_trans_t *tp, /* transaction structure */ |
| 543 | xfs_dabuf_t *bp, /* block buffer */ |
| 544 | int first, /* index of first logged leaf */ |
| 545 | int last) /* index of last logged leaf */ |
| 546 | { |
| 547 | xfs_dir2_block_t *block; /* directory block structure */ |
| 548 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
| 549 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 550 | xfs_mount_t *mp; /* filesystem mount point */ |
| 551 | |
| 552 | mp = tp->t_mountp; |
| 553 | block = bp->data; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 554 | btp = xfs_dir2_block_tail_p(mp, block); |
| 555 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)block), |
| 557 | (uint)((char *)&blp[last + 1] - (char *)block - 1)); |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Log the block tail. |
| 562 | */ |
| 563 | static void |
| 564 | xfs_dir2_block_log_tail( |
| 565 | xfs_trans_t *tp, /* transaction structure */ |
| 566 | xfs_dabuf_t *bp) /* block buffer */ |
| 567 | { |
| 568 | xfs_dir2_block_t *block; /* directory block structure */ |
| 569 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 570 | xfs_mount_t *mp; /* filesystem mount point */ |
| 571 | |
| 572 | mp = tp->t_mountp; |
| 573 | block = bp->data; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 574 | btp = xfs_dir2_block_tail_p(mp, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)block), |
| 576 | (uint)((char *)(btp + 1) - (char *)block - 1)); |
| 577 | } |
| 578 | |
| 579 | /* |
| 580 | * Look up an entry in the block. This is the external routine, |
| 581 | * xfs_dir2_block_lookup_int does the real work. |
| 582 | */ |
| 583 | int /* error */ |
| 584 | xfs_dir2_block_lookup( |
| 585 | xfs_da_args_t *args) /* dir lookup arguments */ |
| 586 | { |
| 587 | xfs_dir2_block_t *block; /* block structure */ |
| 588 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
| 589 | xfs_dabuf_t *bp; /* block buffer */ |
| 590 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 591 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 592 | xfs_inode_t *dp; /* incore inode */ |
| 593 | int ent; /* entry index */ |
| 594 | int error; /* error return value */ |
| 595 | xfs_mount_t *mp; /* filesystem mount point */ |
| 596 | |
| 597 | xfs_dir2_trace_args("block_lookup", args); |
| 598 | /* |
| 599 | * Get the buffer, look up the entry. |
| 600 | * If not found (ENOENT) then return, have no buffer. |
| 601 | */ |
| 602 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) |
| 603 | return error; |
| 604 | dp = args->dp; |
| 605 | mp = dp->i_mount; |
| 606 | block = bp->data; |
| 607 | xfs_dir2_data_check(dp, bp); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 608 | btp = xfs_dir2_block_tail_p(mp, block); |
| 609 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | /* |
| 611 | * Get the offset from the leaf entry, to point to the data. |
| 612 | */ |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 613 | dep = (xfs_dir2_data_entry_t *)((char *)block + |
| 614 | xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | /* |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 616 | * Fill in inode number, CI name if appropriate, release the block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | */ |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 618 | args->inumber = be64_to_cpu(dep->inumber); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 619 | error = xfs_dir_cilookup_result(args, dep->name, dep->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | xfs_da_brelse(args->trans, bp); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 621 | return XFS_ERROR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* |
| 625 | * Internal block lookup routine. |
| 626 | */ |
| 627 | static int /* error */ |
| 628 | xfs_dir2_block_lookup_int( |
| 629 | xfs_da_args_t *args, /* dir lookup arguments */ |
| 630 | xfs_dabuf_t **bpp, /* returned block buffer */ |
| 631 | int *entno) /* returned entry number */ |
| 632 | { |
| 633 | xfs_dir2_dataptr_t addr; /* data entry address */ |
| 634 | xfs_dir2_block_t *block; /* block structure */ |
| 635 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
| 636 | xfs_dabuf_t *bp; /* block buffer */ |
| 637 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 638 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 639 | xfs_inode_t *dp; /* incore inode */ |
| 640 | int error; /* error return value */ |
| 641 | xfs_dahash_t hash; /* found hash value */ |
| 642 | int high; /* binary search high index */ |
| 643 | int low; /* binary search low index */ |
| 644 | int mid; /* binary search current idx */ |
| 645 | xfs_mount_t *mp; /* filesystem mount point */ |
| 646 | xfs_trans_t *tp; /* transaction pointer */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 647 | enum xfs_dacmp cmp; /* comparison result */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
| 649 | dp = args->dp; |
| 650 | tp = args->trans; |
| 651 | mp = dp->i_mount; |
| 652 | /* |
| 653 | * Read the buffer, return error if we can't get it. |
| 654 | */ |
| 655 | if ((error = |
| 656 | xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) { |
| 657 | return error; |
| 658 | } |
| 659 | ASSERT(bp != NULL); |
| 660 | block = bp->data; |
| 661 | xfs_dir2_data_check(dp, bp); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 662 | btp = xfs_dir2_block_tail_p(mp, block); |
| 663 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | /* |
| 665 | * Loop doing a binary search for our hash value. |
| 666 | * Find our entry, ENOENT if it's not there. |
| 667 | */ |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 668 | for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | ASSERT(low <= high); |
| 670 | mid = (low + high) >> 1; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 671 | if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | break; |
| 673 | if (hash < args->hashval) |
| 674 | low = mid + 1; |
| 675 | else |
| 676 | high = mid - 1; |
| 677 | if (low > high) { |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 678 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | xfs_da_brelse(tp, bp); |
| 680 | return XFS_ERROR(ENOENT); |
| 681 | } |
| 682 | } |
| 683 | /* |
| 684 | * Back up to the first one with the right hash value. |
| 685 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 686 | while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | mid--; |
| 688 | } |
| 689 | /* |
| 690 | * Now loop forward through all the entries with the |
| 691 | * right hash value looking for our name. |
| 692 | */ |
| 693 | do { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 694 | if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | continue; |
| 696 | /* |
| 697 | * Get pointer to the entry from the leaf. |
| 698 | */ |
| 699 | dep = (xfs_dir2_data_entry_t *) |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 700 | ((char *)block + xfs_dir2_dataptr_to_off(mp, addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | /* |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 702 | * Compare name and if it's an exact match, return the index |
| 703 | * and buffer. If it's the first case-insensitive match, store |
| 704 | * the index and buffer and continue looking for an exact match. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 706 | cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen); |
| 707 | if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { |
| 708 | args->cmpresult = cmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | *bpp = bp; |
| 710 | *entno = mid; |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 711 | if (cmp == XFS_CMP_EXACT) |
| 712 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 714 | } while (++mid < be32_to_cpu(btp->count) && |
| 715 | be32_to_cpu(blp[mid].hashval) == hash); |
| 716 | |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 717 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 718 | /* |
| 719 | * Here, we can only be doing a lookup (not a rename or replace). |
| 720 | * If a case-insensitive match was found earlier, return success. |
| 721 | */ |
| 722 | if (args->cmpresult == XFS_CMP_CASE) |
| 723 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | /* |
| 725 | * No match, release the buffer and return ENOENT. |
| 726 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | xfs_da_brelse(tp, bp); |
| 728 | return XFS_ERROR(ENOENT); |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * Remove an entry from a block format directory. |
| 733 | * If that makes the block small enough to fit in shortform, transform it. |
| 734 | */ |
| 735 | int /* error */ |
| 736 | xfs_dir2_block_removename( |
| 737 | xfs_da_args_t *args) /* directory operation args */ |
| 738 | { |
| 739 | xfs_dir2_block_t *block; /* block structure */ |
| 740 | xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */ |
| 741 | xfs_dabuf_t *bp; /* block buffer */ |
| 742 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 743 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 744 | xfs_inode_t *dp; /* incore inode */ |
| 745 | int ent; /* block leaf entry index */ |
| 746 | int error; /* error return value */ |
| 747 | xfs_mount_t *mp; /* filesystem mount point */ |
| 748 | int needlog; /* need to log block header */ |
| 749 | int needscan; /* need to fixup bestfree */ |
| 750 | xfs_dir2_sf_hdr_t sfh; /* shortform header */ |
| 751 | int size; /* shortform size */ |
| 752 | xfs_trans_t *tp; /* transaction pointer */ |
| 753 | |
| 754 | xfs_dir2_trace_args("block_removename", args); |
| 755 | /* |
| 756 | * Look up the entry in the block. Gets the buffer and entry index. |
| 757 | * It will always be there, the vnodeops level does a lookup first. |
| 758 | */ |
| 759 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) { |
| 760 | return error; |
| 761 | } |
| 762 | dp = args->dp; |
| 763 | tp = args->trans; |
| 764 | mp = dp->i_mount; |
| 765 | block = bp->data; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 766 | btp = xfs_dir2_block_tail_p(mp, block); |
| 767 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | /* |
| 769 | * Point to the data entry using the leaf entry. |
| 770 | */ |
| 771 | dep = (xfs_dir2_data_entry_t *) |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 772 | ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | /* |
| 774 | * Mark the data entry's space free. |
| 775 | */ |
| 776 | needlog = needscan = 0; |
| 777 | xfs_dir2_data_make_free(tp, bp, |
| 778 | (xfs_dir2_data_aoff_t)((char *)dep - (char *)block), |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 779 | xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | /* |
| 781 | * Fix up the block tail. |
| 782 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 783 | be32_add_cpu(&btp->stale, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | xfs_dir2_block_log_tail(tp, bp); |
| 785 | /* |
| 786 | * Remove the leaf entry by marking it stale. |
| 787 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 788 | blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | xfs_dir2_block_log_leaf(tp, bp, ent, ent); |
| 790 | /* |
| 791 | * Fix up bestfree, log the header if necessary. |
| 792 | */ |
| 793 | if (needscan) |
Eric Sandeen | ef497f8 | 2007-05-08 13:48:49 +1000 | [diff] [blame] | 794 | xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | if (needlog) |
| 796 | xfs_dir2_data_log_header(tp, bp); |
| 797 | xfs_dir2_data_check(dp, bp); |
| 798 | /* |
| 799 | * See if the size as a shortform is good enough. |
| 800 | */ |
| 801 | if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) > |
| 802 | XFS_IFORK_DSIZE(dp)) { |
| 803 | xfs_da_buf_done(bp); |
| 804 | return 0; |
| 805 | } |
| 806 | /* |
| 807 | * If it works, do the conversion. |
| 808 | */ |
| 809 | return xfs_dir2_block_to_sf(args, bp, size, &sfh); |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * Replace an entry in a V2 block directory. |
| 814 | * Change the inode number to the new value. |
| 815 | */ |
| 816 | int /* error */ |
| 817 | xfs_dir2_block_replace( |
| 818 | xfs_da_args_t *args) /* directory operation args */ |
| 819 | { |
| 820 | xfs_dir2_block_t *block; /* block structure */ |
| 821 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
| 822 | xfs_dabuf_t *bp; /* block buffer */ |
| 823 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 824 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 825 | xfs_inode_t *dp; /* incore inode */ |
| 826 | int ent; /* leaf entry index */ |
| 827 | int error; /* error return value */ |
| 828 | xfs_mount_t *mp; /* filesystem mount point */ |
| 829 | |
| 830 | xfs_dir2_trace_args("block_replace", args); |
| 831 | /* |
| 832 | * Lookup the entry in the directory. Get buffer and entry index. |
| 833 | * This will always succeed since the caller has already done a lookup. |
| 834 | */ |
| 835 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) { |
| 836 | return error; |
| 837 | } |
| 838 | dp = args->dp; |
| 839 | mp = dp->i_mount; |
| 840 | block = bp->data; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 841 | btp = xfs_dir2_block_tail_p(mp, block); |
| 842 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | /* |
| 844 | * Point to the data entry we need to change. |
| 845 | */ |
| 846 | dep = (xfs_dir2_data_entry_t *) |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 847 | ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address))); |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 848 | ASSERT(be64_to_cpu(dep->inumber) != args->inumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | /* |
| 850 | * Change the inode number to the new value. |
| 851 | */ |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 852 | dep->inumber = cpu_to_be64(args->inumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | xfs_dir2_data_log_entry(args->trans, bp, dep); |
| 854 | xfs_dir2_data_check(dp, bp); |
| 855 | xfs_da_buf_done(bp); |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | * Qsort comparison routine for the block leaf entries. |
| 861 | */ |
| 862 | static int /* sort order */ |
| 863 | xfs_dir2_block_sort( |
| 864 | const void *a, /* first leaf entry */ |
| 865 | const void *b) /* second leaf entry */ |
| 866 | { |
| 867 | const xfs_dir2_leaf_entry_t *la; /* first leaf entry */ |
| 868 | const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */ |
| 869 | |
| 870 | la = a; |
| 871 | lb = b; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 872 | return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 : |
| 873 | (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | /* |
| 877 | * Convert a V2 leaf directory to a V2 block directory if possible. |
| 878 | */ |
| 879 | int /* error */ |
| 880 | xfs_dir2_leaf_to_block( |
| 881 | xfs_da_args_t *args, /* operation arguments */ |
| 882 | xfs_dabuf_t *lbp, /* leaf buffer */ |
| 883 | xfs_dabuf_t *dbp) /* data buffer */ |
| 884 | { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 885 | __be16 *bestsp; /* leaf bests table */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | xfs_dir2_block_t *block; /* block structure */ |
| 887 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 888 | xfs_inode_t *dp; /* incore directory inode */ |
| 889 | xfs_dir2_data_unused_t *dup; /* unused data entry */ |
| 890 | int error; /* error return value */ |
| 891 | int from; /* leaf from index */ |
| 892 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 893 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 894 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
| 895 | xfs_mount_t *mp; /* file system mount point */ |
| 896 | int needlog; /* need to log data header */ |
| 897 | int needscan; /* need to scan for bestfree */ |
| 898 | xfs_dir2_sf_hdr_t sfh; /* shortform header */ |
| 899 | int size; /* bytes used */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 900 | __be16 *tagp; /* end of entry (tag) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | int to; /* block/leaf to index */ |
| 902 | xfs_trans_t *tp; /* transaction pointer */ |
| 903 | |
| 904 | xfs_dir2_trace_args_bb("leaf_to_block", args, lbp, dbp); |
| 905 | dp = args->dp; |
| 906 | tp = args->trans; |
| 907 | mp = dp->i_mount; |
| 908 | leaf = lbp->data; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 909 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 910 | ltp = xfs_dir2_leaf_tail_p(mp, leaf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | /* |
| 912 | * If there are data blocks other than the first one, take this |
| 913 | * opportunity to remove trailing empty data blocks that may have |
| 914 | * been left behind during no-space-reservation operations. |
| 915 | * These will show up in the leaf bests table. |
| 916 | */ |
| 917 | while (dp->i_d.di_size > mp->m_dirblksize) { |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 918 | bestsp = xfs_dir2_leaf_bests_p(ltp); |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 919 | if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | mp->m_dirblksize - (uint)sizeof(block->hdr)) { |
| 921 | if ((error = |
| 922 | xfs_dir2_leaf_trim_data(args, lbp, |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 923 | (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | goto out; |
| 925 | } else { |
| 926 | error = 0; |
| 927 | goto out; |
| 928 | } |
| 929 | } |
| 930 | /* |
| 931 | * Read the data block if we don't already have it, give up if it fails. |
| 932 | */ |
| 933 | if (dbp == NULL && |
| 934 | (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp, |
| 935 | XFS_DATA_FORK))) { |
| 936 | goto out; |
| 937 | } |
| 938 | block = dbp->data; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 939 | ASSERT(be32_to_cpu(block->hdr.magic) == XFS_DIR2_DATA_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | /* |
| 941 | * Size of the "leaf" area in the block. |
| 942 | */ |
| 943 | size = (uint)sizeof(block->tail) + |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 944 | (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | /* |
| 946 | * Look at the last data entry. |
| 947 | */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 948 | tagp = (__be16 *)((char *)block + mp->m_dirblksize) - 1; |
| 949 | dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | /* |
| 951 | * If it's not free or is too short we can't do it. |
| 952 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 953 | if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG || |
| 954 | be16_to_cpu(dup->length) < size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | error = 0; |
| 956 | goto out; |
| 957 | } |
| 958 | /* |
| 959 | * Start converting it to block form. |
| 960 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 961 | block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | needlog = 1; |
| 963 | needscan = 0; |
| 964 | /* |
| 965 | * Use up the space at the end of the block (blp/btp). |
| 966 | */ |
| 967 | xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size, |
| 968 | &needlog, &needscan); |
| 969 | /* |
| 970 | * Initialize the block tail. |
| 971 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 972 | btp = xfs_dir2_block_tail_p(mp, block); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 973 | btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | btp->stale = 0; |
| 975 | xfs_dir2_block_log_tail(tp, dbp); |
| 976 | /* |
| 977 | * Initialize the block leaf area. We compact out stale entries. |
| 978 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 979 | lep = xfs_dir2_block_leaf_p(btp); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 980 | for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 981 | if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | continue; |
| 983 | lep[to++] = leaf->ents[from]; |
| 984 | } |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 985 | ASSERT(to == be32_to_cpu(btp->count)); |
| 986 | xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | /* |
| 988 | * Scan the bestfree if we need it and log the data block header. |
| 989 | */ |
| 990 | if (needscan) |
Eric Sandeen | ef497f8 | 2007-05-08 13:48:49 +1000 | [diff] [blame] | 991 | xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | if (needlog) |
| 993 | xfs_dir2_data_log_header(tp, dbp); |
| 994 | /* |
| 995 | * Pitch the old leaf block. |
| 996 | */ |
| 997 | error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp); |
| 998 | lbp = NULL; |
| 999 | if (error) { |
| 1000 | goto out; |
| 1001 | } |
| 1002 | /* |
| 1003 | * Now see if the resulting block can be shrunken to shortform. |
| 1004 | */ |
| 1005 | if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) > |
| 1006 | XFS_IFORK_DSIZE(dp)) { |
| 1007 | error = 0; |
| 1008 | goto out; |
| 1009 | } |
| 1010 | return xfs_dir2_block_to_sf(args, dbp, size, &sfh); |
| 1011 | out: |
| 1012 | if (lbp) |
| 1013 | xfs_da_buf_done(lbp); |
| 1014 | if (dbp) |
| 1015 | xfs_da_buf_done(dbp); |
| 1016 | return error; |
| 1017 | } |
| 1018 | |
| 1019 | /* |
| 1020 | * Convert the shortform directory to block form. |
| 1021 | */ |
| 1022 | int /* error */ |
| 1023 | xfs_dir2_sf_to_block( |
| 1024 | xfs_da_args_t *args) /* operation arguments */ |
| 1025 | { |
| 1026 | xfs_dir2_db_t blkno; /* dir-relative block # (0) */ |
| 1027 | xfs_dir2_block_t *block; /* block structure */ |
| 1028 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
| 1029 | xfs_dabuf_t *bp; /* block buffer */ |
| 1030 | xfs_dir2_block_tail_t *btp; /* block tail pointer */ |
| 1031 | char *buf; /* sf buffer */ |
| 1032 | int buf_len; |
| 1033 | xfs_dir2_data_entry_t *dep; /* data entry pointer */ |
| 1034 | xfs_inode_t *dp; /* incore directory inode */ |
| 1035 | int dummy; /* trash */ |
| 1036 | xfs_dir2_data_unused_t *dup; /* unused entry pointer */ |
| 1037 | int endoffset; /* end of data objects */ |
| 1038 | int error; /* error return value */ |
| 1039 | int i; /* index */ |
| 1040 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1041 | int needlog; /* need to log block header */ |
| 1042 | int needscan; /* need to scan block freespc */ |
| 1043 | int newoffset; /* offset from current entry */ |
| 1044 | int offset; /* target block offset */ |
| 1045 | xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */ |
| 1046 | xfs_dir2_sf_t *sfp; /* shortform structure */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 1047 | __be16 *tagp; /* end of data entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | xfs_trans_t *tp; /* transaction pointer */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1049 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
| 1051 | xfs_dir2_trace_args("sf_to_block", args); |
| 1052 | dp = args->dp; |
| 1053 | tp = args->trans; |
| 1054 | mp = dp->i_mount; |
| 1055 | ASSERT(dp->i_df.if_flags & XFS_IFINLINE); |
| 1056 | /* |
| 1057 | * Bomb out if the shortform directory is way too short. |
| 1058 | */ |
| 1059 | if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) { |
| 1060 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1061 | return XFS_ERROR(EIO); |
| 1062 | } |
| 1063 | ASSERT(dp->i_df.if_bytes == dp->i_d.di_size); |
| 1064 | ASSERT(dp->i_df.if_u1.if_data != NULL); |
| 1065 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1066 | ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | /* |
| 1068 | * Copy the directory into the stack buffer. |
| 1069 | * Then pitch the incore inode data so we can make extents. |
| 1070 | */ |
| 1071 | |
| 1072 | buf_len = dp->i_df.if_bytes; |
| 1073 | buf = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP); |
| 1074 | |
| 1075 | memcpy(buf, sfp, dp->i_df.if_bytes); |
| 1076 | xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK); |
| 1077 | dp->i_d.di_size = 0; |
| 1078 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); |
| 1079 | /* |
| 1080 | * Reset pointer - old sfp is gone. |
| 1081 | */ |
| 1082 | sfp = (xfs_dir2_sf_t *)buf; |
| 1083 | /* |
| 1084 | * Add block 0 to the inode. |
| 1085 | */ |
| 1086 | error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno); |
| 1087 | if (error) { |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1088 | kmem_free(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | return error; |
| 1090 | } |
| 1091 | /* |
| 1092 | * Initialize the data block. |
| 1093 | */ |
| 1094 | error = xfs_dir2_data_init(args, blkno, &bp); |
| 1095 | if (error) { |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1096 | kmem_free(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | return error; |
| 1098 | } |
| 1099 | block = bp->data; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 1100 | block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | /* |
| 1102 | * Compute size of block "tail" area. |
| 1103 | */ |
| 1104 | i = (uint)sizeof(*btp) + |
Nathan Scott | 8f44e04 | 2006-03-17 17:28:47 +1100 | [diff] [blame] | 1105 | (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | /* |
| 1107 | * The whole thing is initialized to free by the init routine. |
| 1108 | * Say we're using the leaf and tail area. |
| 1109 | */ |
| 1110 | dup = (xfs_dir2_data_unused_t *)block->u; |
| 1111 | needlog = needscan = 0; |
| 1112 | xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog, |
| 1113 | &needscan); |
| 1114 | ASSERT(needscan == 0); |
| 1115 | /* |
| 1116 | * Fill in the tail. |
| 1117 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1118 | btp = xfs_dir2_block_tail_p(mp, block); |
Nathan Scott | 8f44e04 | 2006-03-17 17:28:47 +1100 | [diff] [blame] | 1119 | btp->count = cpu_to_be32(sfp->hdr.count + 2); /* ., .. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | btp->stale = 0; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1121 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | endoffset = (uint)((char *)blp - (char *)block); |
| 1123 | /* |
| 1124 | * Remove the freespace, we'll manage it. |
| 1125 | */ |
| 1126 | xfs_dir2_data_use_free(tp, bp, dup, |
| 1127 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)block), |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1128 | be16_to_cpu(dup->length), &needlog, &needscan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | /* |
| 1130 | * Create entry for . |
| 1131 | */ |
| 1132 | dep = (xfs_dir2_data_entry_t *) |
| 1133 | ((char *)block + XFS_DIR2_DATA_DOT_OFFSET); |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 1134 | dep->inumber = cpu_to_be64(dp->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | dep->namelen = 1; |
| 1136 | dep->name[0] = '.'; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1137 | tagp = xfs_dir2_data_entry_tag_p(dep); |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 1138 | *tagp = cpu_to_be16((char *)dep - (char *)block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | xfs_dir2_data_log_entry(tp, bp, dep); |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1140 | blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1141 | blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1142 | (char *)dep - (char *)block)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | /* |
| 1144 | * Create entry for .. |
| 1145 | */ |
| 1146 | dep = (xfs_dir2_data_entry_t *) |
| 1147 | ((char *)block + XFS_DIR2_DATA_DOTDOT_OFFSET); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1148 | dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | dep->namelen = 2; |
| 1150 | dep->name[0] = dep->name[1] = '.'; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1151 | tagp = xfs_dir2_data_entry_tag_p(dep); |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 1152 | *tagp = cpu_to_be16((char *)dep - (char *)block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | xfs_dir2_data_log_entry(tp, bp, dep); |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1154 | blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1155 | blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1156 | (char *)dep - (char *)block)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | offset = XFS_DIR2_DATA_FIRST_OFFSET; |
| 1158 | /* |
| 1159 | * Loop over existing entries, stuff them in. |
| 1160 | */ |
Nathan Scott | 8f44e04 | 2006-03-17 17:28:47 +1100 | [diff] [blame] | 1161 | if ((i = 0) == sfp->hdr.count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | sfep = NULL; |
| 1163 | else |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1164 | sfep = xfs_dir2_sf_firstentry(sfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | /* |
| 1166 | * Need to preserve the existing offset values in the sf directory. |
| 1167 | * Insert holes (unused entries) where necessary. |
| 1168 | */ |
| 1169 | while (offset < endoffset) { |
| 1170 | /* |
| 1171 | * sfep is null when we reach the end of the list. |
| 1172 | */ |
| 1173 | if (sfep == NULL) |
| 1174 | newoffset = endoffset; |
| 1175 | else |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1176 | newoffset = xfs_dir2_sf_get_offset(sfep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | /* |
| 1178 | * There should be a hole here, make one. |
| 1179 | */ |
| 1180 | if (offset < newoffset) { |
| 1181 | dup = (xfs_dir2_data_unused_t *) |
| 1182 | ((char *)block + offset); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1183 | dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 1184 | dup->length = cpu_to_be16(newoffset - offset); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1185 | *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | ((char *)dup - (char *)block)); |
| 1187 | xfs_dir2_data_log_unused(tp, bp, dup); |
| 1188 | (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t *)block, |
| 1189 | dup, &dummy); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1190 | offset += be16_to_cpu(dup->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | continue; |
| 1192 | } |
| 1193 | /* |
| 1194 | * Copy a real entry. |
| 1195 | */ |
| 1196 | dep = (xfs_dir2_data_entry_t *)((char *)block + newoffset); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1197 | dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp, |
| 1198 | xfs_dir2_sf_inumberp(sfep))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | dep->namelen = sfep->namelen; |
| 1200 | memcpy(dep->name, sfep->name, dep->namelen); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1201 | tagp = xfs_dir2_data_entry_tag_p(dep); |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 1202 | *tagp = cpu_to_be16((char *)dep - (char *)block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | xfs_dir2_data_log_entry(tp, bp, dep); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1204 | name.name = sfep->name; |
| 1205 | name.len = sfep->namelen; |
| 1206 | blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops-> |
| 1207 | hashname(&name)); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1208 | blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | (char *)dep - (char *)block)); |
| 1210 | offset = (int)((char *)(tagp + 1) - (char *)block); |
Nathan Scott | 8f44e04 | 2006-03-17 17:28:47 +1100 | [diff] [blame] | 1211 | if (++i == sfp->hdr.count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | sfep = NULL; |
| 1213 | else |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1214 | sfep = xfs_dir2_sf_nextentry(sfp, sfep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | } |
| 1216 | /* Done with the temporary buffer */ |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1217 | kmem_free(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | /* |
| 1219 | * Sort the leaf entries by hash value. |
| 1220 | */ |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 1221 | xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | /* |
| 1223 | * Log the leaf entry area and tail. |
| 1224 | * Already logged the header in data_init, ignore needlog. |
| 1225 | */ |
| 1226 | ASSERT(needscan == 0); |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 1227 | xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | xfs_dir2_block_log_tail(tp, bp); |
| 1229 | xfs_dir2_data_check(dp, bp); |
| 1230 | xfs_da_buf_done(bp); |
| 1231 | return 0; |
| 1232 | } |