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_error.h" |
| 37 | #include "xfs_dir2_data.h" |
| 38 | #include "xfs_dir2_leaf.h" |
| 39 | #include "xfs_dir2_block.h" |
| 40 | #include "xfs_dir2_trace.h" |
| 41 | |
| 42 | /* |
| 43 | * Prototypes for internal functions. |
| 44 | */ |
| 45 | static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args, |
| 46 | xfs_dir2_sf_entry_t *sfep, |
| 47 | xfs_dir2_data_aoff_t offset, |
| 48 | int new_isize); |
| 49 | static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange, |
| 50 | int new_isize); |
| 51 | static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange, |
| 52 | xfs_dir2_sf_entry_t **sfepp, |
| 53 | xfs_dir2_data_aoff_t *offsetp); |
| 54 | #ifdef DEBUG |
| 55 | static void xfs_dir2_sf_check(xfs_da_args_t *args); |
| 56 | #else |
| 57 | #define xfs_dir2_sf_check(args) |
| 58 | #endif /* DEBUG */ |
| 59 | #if XFS_BIG_INUMS |
| 60 | static void xfs_dir2_sf_toino4(xfs_da_args_t *args); |
| 61 | static void xfs_dir2_sf_toino8(xfs_da_args_t *args); |
| 62 | #endif /* XFS_BIG_INUMS */ |
| 63 | |
| 64 | /* |
| 65 | * Given a block directory (dp/block), calculate its size as a shortform (sf) |
| 66 | * directory and a header for the sf directory, if it will fit it the |
| 67 | * space currently present in the inode. If it won't fit, the output |
| 68 | * size is too big (but not accurate). |
| 69 | */ |
| 70 | int /* size for sf form */ |
| 71 | xfs_dir2_block_sfsize( |
| 72 | xfs_inode_t *dp, /* incore inode pointer */ |
| 73 | xfs_dir2_block_t *block, /* block directory data */ |
| 74 | xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */ |
| 75 | { |
| 76 | xfs_dir2_dataptr_t addr; /* data entry address */ |
| 77 | xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */ |
| 78 | xfs_dir2_block_tail_t *btp; /* tail area of the block */ |
| 79 | int count; /* shortform entry count */ |
| 80 | xfs_dir2_data_entry_t *dep; /* data entry in the block */ |
| 81 | int i; /* block entry index */ |
| 82 | int i8count; /* count of big-inode entries */ |
| 83 | int isdot; /* entry is "." */ |
| 84 | int isdotdot; /* entry is ".." */ |
| 85 | xfs_mount_t *mp; /* mount structure pointer */ |
| 86 | int namelen; /* total name bytes */ |
Christoph Hellwig | 5bde1ba9 | 2005-11-02 15:06:18 +1100 | [diff] [blame] | 87 | xfs_ino_t parent = 0; /* parent inode number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | int size=0; /* total computed size */ |
| 89 | |
| 90 | mp = dp->i_mount; |
| 91 | |
| 92 | count = i8count = namelen = 0; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 93 | btp = xfs_dir2_block_tail_p(mp, block); |
| 94 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * Iterate over the block's data entries by using the leaf pointers. |
| 98 | */ |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 99 | for (i = 0; i < be32_to_cpu(btp->count); i++) { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 100 | if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | continue; |
| 102 | /* |
| 103 | * Calculate the pointer to the entry at hand. |
| 104 | */ |
| 105 | dep = (xfs_dir2_data_entry_t *) |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 106 | ((char *)block + xfs_dir2_dataptr_to_off(mp, addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | /* |
| 108 | * Detect . and .., so we can special-case them. |
| 109 | * . is not included in sf directories. |
| 110 | * .. is included by just the parent inode number. |
| 111 | */ |
| 112 | isdot = dep->namelen == 1 && dep->name[0] == '.'; |
| 113 | isdotdot = |
| 114 | dep->namelen == 2 && |
| 115 | dep->name[0] == '.' && dep->name[1] == '.'; |
| 116 | #if XFS_BIG_INUMS |
| 117 | if (!isdot) |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 118 | i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | #endif |
| 120 | if (!isdot && !isdotdot) { |
| 121 | count++; |
| 122 | namelen += dep->namelen; |
| 123 | } else if (isdotdot) |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 124 | parent = be64_to_cpu(dep->inumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | /* |
| 126 | * Calculate the new size, see if we should give up yet. |
| 127 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 128 | size = xfs_dir2_sf_hdr_size(i8count) + /* header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | count + /* namelen */ |
| 130 | count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */ |
| 131 | namelen + /* name */ |
| 132 | (i8count ? /* inumber */ |
| 133 | (uint)sizeof(xfs_dir2_ino8_t) * count : |
| 134 | (uint)sizeof(xfs_dir2_ino4_t) * count); |
| 135 | if (size > XFS_IFORK_DSIZE(dp)) |
| 136 | return size; /* size value is a failure */ |
| 137 | } |
| 138 | /* |
| 139 | * Create the output header, if it worked. |
| 140 | */ |
| 141 | sfhp->count = count; |
| 142 | sfhp->i8count = i8count; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 143 | xfs_dir2_sf_put_inumber((xfs_dir2_sf_t *)sfhp, &parent, &sfhp->parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | return size; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Convert a block format directory to shortform. |
| 149 | * Caller has already checked that it will fit, and built us a header. |
| 150 | */ |
| 151 | int /* error */ |
| 152 | xfs_dir2_block_to_sf( |
| 153 | xfs_da_args_t *args, /* operation arguments */ |
| 154 | xfs_dabuf_t *bp, /* block buffer */ |
| 155 | int size, /* shortform directory size */ |
| 156 | xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */ |
| 157 | { |
| 158 | xfs_dir2_block_t *block; /* block structure */ |
| 159 | xfs_dir2_block_tail_t *btp; /* block tail pointer */ |
| 160 | xfs_dir2_data_entry_t *dep; /* data entry pointer */ |
| 161 | xfs_inode_t *dp; /* incore directory inode */ |
| 162 | xfs_dir2_data_unused_t *dup; /* unused data pointer */ |
| 163 | char *endptr; /* end of data entries */ |
| 164 | int error; /* error return value */ |
| 165 | int logflags; /* inode logging flags */ |
| 166 | xfs_mount_t *mp; /* filesystem mount point */ |
| 167 | char *ptr; /* current data pointer */ |
| 168 | xfs_dir2_sf_entry_t *sfep; /* shortform entry */ |
| 169 | xfs_dir2_sf_t *sfp; /* shortform structure */ |
| 170 | xfs_ino_t temp; |
| 171 | |
| 172 | xfs_dir2_trace_args_sb("block_to_sf", args, size, bp); |
| 173 | dp = args->dp; |
| 174 | mp = dp->i_mount; |
| 175 | |
| 176 | /* |
| 177 | * Make a copy of the block data, so we can shrink the inode |
| 178 | * and add local data. |
| 179 | */ |
| 180 | block = kmem_alloc(mp->m_dirblksize, KM_SLEEP); |
| 181 | memcpy(block, bp->data, mp->m_dirblksize); |
| 182 | logflags = XFS_ILOG_CORE; |
| 183 | if ((error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp))) { |
| 184 | ASSERT(error != ENOSPC); |
| 185 | goto out; |
| 186 | } |
| 187 | /* |
| 188 | * The buffer is now unconditionally gone, whether |
| 189 | * xfs_dir2_shrink_inode worked or not. |
| 190 | * |
| 191 | * Convert the inode to local format. |
| 192 | */ |
| 193 | dp->i_df.if_flags &= ~XFS_IFEXTENTS; |
| 194 | dp->i_df.if_flags |= XFS_IFINLINE; |
| 195 | dp->i_d.di_format = XFS_DINODE_FMT_LOCAL; |
| 196 | ASSERT(dp->i_df.if_bytes == 0); |
| 197 | xfs_idata_realloc(dp, size, XFS_DATA_FORK); |
| 198 | logflags |= XFS_ILOG_DDATA; |
| 199 | /* |
| 200 | * Copy the header into the newly allocate local space. |
| 201 | */ |
| 202 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 203 | memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | dp->i_d.di_size = size; |
| 205 | /* |
| 206 | * Set up to loop over the block's entries. |
| 207 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 208 | btp = xfs_dir2_block_tail_p(mp, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | ptr = (char *)block->u; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 210 | endptr = (char *)xfs_dir2_block_leaf_p(btp); |
| 211 | sfep = xfs_dir2_sf_firstentry(sfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | /* |
| 213 | * Loop over the active and unused entries. |
| 214 | * Stop when we reach the leaf/tail portion of the block. |
| 215 | */ |
| 216 | while (ptr < endptr) { |
| 217 | /* |
| 218 | * If it's unused, just skip over it. |
| 219 | */ |
| 220 | dup = (xfs_dir2_data_unused_t *)ptr; |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 221 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
| 222 | ptr += be16_to_cpu(dup->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | continue; |
| 224 | } |
| 225 | dep = (xfs_dir2_data_entry_t *)ptr; |
| 226 | /* |
| 227 | * Skip . |
| 228 | */ |
| 229 | if (dep->namelen == 1 && dep->name[0] == '.') |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 230 | ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | /* |
| 232 | * Skip .., but make sure the inode number is right. |
| 233 | */ |
| 234 | else if (dep->namelen == 2 && |
| 235 | dep->name[0] == '.' && dep->name[1] == '.') |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 236 | ASSERT(be64_to_cpu(dep->inumber) == |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 237 | xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | /* |
| 239 | * Normal entry, copy it into shortform. |
| 240 | */ |
| 241 | else { |
| 242 | sfep->namelen = dep->namelen; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 243 | xfs_dir2_sf_put_offset(sfep, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | (xfs_dir2_data_aoff_t) |
| 245 | ((char *)dep - (char *)block)); |
| 246 | memcpy(sfep->name, dep->name, dep->namelen); |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 247 | temp = be64_to_cpu(dep->inumber); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 248 | xfs_dir2_sf_put_inumber(sfp, &temp, |
| 249 | xfs_dir2_sf_inumberp(sfep)); |
| 250 | sfep = xfs_dir2_sf_nextentry(sfp, sfep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 252 | ptr += xfs_dir2_data_entsize(dep->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | ASSERT((char *)sfep - (char *)sfp == size); |
| 255 | xfs_dir2_sf_check(args); |
| 256 | out: |
| 257 | xfs_trans_log_inode(args->trans, dp, logflags); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 258 | kmem_free(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return error; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Add a name to a shortform directory. |
| 264 | * There are two algorithms, "easy" and "hard" which we decide on |
| 265 | * before changing anything. |
| 266 | * Convert to block form if necessary, if the new entry won't fit. |
| 267 | */ |
| 268 | int /* error */ |
| 269 | xfs_dir2_sf_addname( |
| 270 | xfs_da_args_t *args) /* operation arguments */ |
| 271 | { |
| 272 | int add_entsize; /* size of the new entry */ |
| 273 | xfs_inode_t *dp; /* incore directory inode */ |
| 274 | int error; /* error return value */ |
| 275 | int incr_isize; /* total change in size */ |
| 276 | int new_isize; /* di_size after adding name */ |
| 277 | int objchange; /* changing to 8-byte inodes */ |
Christoph Hellwig | 5bde1ba9 | 2005-11-02 15:06:18 +1100 | [diff] [blame] | 278 | xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | int old_isize; /* di_size before adding name */ |
| 280 | int pick; /* which algorithm to use */ |
| 281 | xfs_dir2_sf_t *sfp; /* shortform structure */ |
Christoph Hellwig | 5bde1ba9 | 2005-11-02 15:06:18 +1100 | [diff] [blame] | 282 | xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
| 284 | xfs_dir2_trace_args("sf_addname", args); |
| 285 | ASSERT(xfs_dir2_sf_lookup(args) == ENOENT); |
| 286 | dp = args->dp; |
| 287 | ASSERT(dp->i_df.if_flags & XFS_IFINLINE); |
| 288 | /* |
| 289 | * Make sure the shortform value has some of its header. |
| 290 | */ |
| 291 | if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) { |
| 292 | ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount)); |
| 293 | return XFS_ERROR(EIO); |
| 294 | } |
| 295 | ASSERT(dp->i_df.if_bytes == dp->i_d.di_size); |
| 296 | ASSERT(dp->i_df.if_u1.if_data != NULL); |
| 297 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 298 | 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] | 299 | /* |
| 300 | * Compute entry (and change in) size. |
| 301 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 302 | add_entsize = xfs_dir2_sf_entsize_byname(sfp, args->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | incr_isize = add_entsize; |
| 304 | objchange = 0; |
| 305 | #if XFS_BIG_INUMS |
| 306 | /* |
| 307 | * Do we have to change to 8 byte inodes? |
| 308 | */ |
| 309 | if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) { |
| 310 | /* |
| 311 | * Yes, adjust the entry size and the total size. |
| 312 | */ |
| 313 | add_entsize += |
| 314 | (uint)sizeof(xfs_dir2_ino8_t) - |
| 315 | (uint)sizeof(xfs_dir2_ino4_t); |
| 316 | incr_isize += |
| 317 | (sfp->hdr.count + 2) * |
| 318 | ((uint)sizeof(xfs_dir2_ino8_t) - |
| 319 | (uint)sizeof(xfs_dir2_ino4_t)); |
| 320 | objchange = 1; |
| 321 | } |
| 322 | #endif |
| 323 | old_isize = (int)dp->i_d.di_size; |
| 324 | new_isize = old_isize + incr_isize; |
| 325 | /* |
| 326 | * Won't fit as shortform any more (due to size), |
| 327 | * or the pick routine says it won't (due to offset values). |
| 328 | */ |
| 329 | if (new_isize > XFS_IFORK_DSIZE(dp) || |
| 330 | (pick = |
| 331 | xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) { |
| 332 | /* |
| 333 | * Just checking or no space reservation, it doesn't fit. |
| 334 | */ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 335 | if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | return XFS_ERROR(ENOSPC); |
| 337 | /* |
| 338 | * Convert to block form then add the name. |
| 339 | */ |
| 340 | error = xfs_dir2_sf_to_block(args); |
| 341 | if (error) |
| 342 | return error; |
| 343 | return xfs_dir2_block_addname(args); |
| 344 | } |
| 345 | /* |
| 346 | * Just checking, it fits. |
| 347 | */ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 348 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | return 0; |
| 350 | /* |
| 351 | * Do it the easy way - just add it at the end. |
| 352 | */ |
| 353 | if (pick == 1) |
| 354 | xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize); |
| 355 | /* |
| 356 | * Do it the hard way - look for a place to insert the new entry. |
| 357 | * Convert to 8 byte inode numbers first if necessary. |
| 358 | */ |
| 359 | else { |
| 360 | ASSERT(pick == 2); |
| 361 | #if XFS_BIG_INUMS |
| 362 | if (objchange) |
| 363 | xfs_dir2_sf_toino8(args); |
| 364 | #endif |
| 365 | xfs_dir2_sf_addname_hard(args, objchange, new_isize); |
| 366 | } |
| 367 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA); |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Add the new entry the "easy" way. |
| 373 | * This is copying the old directory and adding the new entry at the end. |
| 374 | * Since it's sorted by "offset" we need room after the last offset |
| 375 | * that's already there, and then room to convert to a block directory. |
| 376 | * This is already checked by the pick routine. |
| 377 | */ |
| 378 | static void |
| 379 | xfs_dir2_sf_addname_easy( |
| 380 | xfs_da_args_t *args, /* operation arguments */ |
| 381 | xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */ |
| 382 | xfs_dir2_data_aoff_t offset, /* offset to use for new ent */ |
| 383 | int new_isize) /* new directory size */ |
| 384 | { |
| 385 | int byteoff; /* byte offset in sf dir */ |
| 386 | xfs_inode_t *dp; /* incore directory inode */ |
| 387 | xfs_dir2_sf_t *sfp; /* shortform structure */ |
| 388 | |
| 389 | dp = args->dp; |
| 390 | |
| 391 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
| 392 | byteoff = (int)((char *)sfep - (char *)sfp); |
| 393 | /* |
| 394 | * Grow the in-inode space. |
| 395 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 396 | xfs_idata_realloc(dp, xfs_dir2_sf_entsize_byname(sfp, args->namelen), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | XFS_DATA_FORK); |
| 398 | /* |
| 399 | * Need to set up again due to realloc of the inode data. |
| 400 | */ |
| 401 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
| 402 | sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff); |
| 403 | /* |
| 404 | * Fill in the new entry. |
| 405 | */ |
| 406 | sfep->namelen = args->namelen; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 407 | xfs_dir2_sf_put_offset(sfep, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | memcpy(sfep->name, args->name, sfep->namelen); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 409 | xfs_dir2_sf_put_inumber(sfp, &args->inumber, |
| 410 | xfs_dir2_sf_inumberp(sfep)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | /* |
| 412 | * Update the header and inode. |
| 413 | */ |
| 414 | sfp->hdr.count++; |
| 415 | #if XFS_BIG_INUMS |
| 416 | if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) |
| 417 | sfp->hdr.i8count++; |
| 418 | #endif |
| 419 | dp->i_d.di_size = new_isize; |
| 420 | xfs_dir2_sf_check(args); |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * Add the new entry the "hard" way. |
| 425 | * The caller has already converted to 8 byte inode numbers if necessary, |
| 426 | * in which case we need to leave the i8count at 1. |
| 427 | * Find a hole that the new entry will fit into, and copy |
| 428 | * the first part of the entries, the new entry, and the last part of |
| 429 | * the entries. |
| 430 | */ |
| 431 | /* ARGSUSED */ |
| 432 | static void |
| 433 | xfs_dir2_sf_addname_hard( |
| 434 | xfs_da_args_t *args, /* operation arguments */ |
| 435 | int objchange, /* changing inode number size */ |
| 436 | int new_isize) /* new directory size */ |
| 437 | { |
| 438 | int add_datasize; /* data size need for new ent */ |
| 439 | char *buf; /* buffer for old */ |
| 440 | xfs_inode_t *dp; /* incore directory inode */ |
| 441 | int eof; /* reached end of old dir */ |
| 442 | int nbytes; /* temp for byte copies */ |
| 443 | xfs_dir2_data_aoff_t new_offset; /* next offset value */ |
| 444 | xfs_dir2_data_aoff_t offset; /* current offset value */ |
| 445 | int old_isize; /* previous di_size */ |
| 446 | xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */ |
| 447 | xfs_dir2_sf_t *oldsfp; /* original shortform dir */ |
| 448 | xfs_dir2_sf_entry_t *sfep; /* entry in new dir */ |
| 449 | xfs_dir2_sf_t *sfp; /* new shortform dir */ |
| 450 | |
| 451 | /* |
| 452 | * Copy the old directory to the stack buffer. |
| 453 | */ |
| 454 | dp = args->dp; |
| 455 | |
| 456 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
| 457 | old_isize = (int)dp->i_d.di_size; |
| 458 | buf = kmem_alloc(old_isize, KM_SLEEP); |
| 459 | oldsfp = (xfs_dir2_sf_t *)buf; |
| 460 | memcpy(oldsfp, sfp, old_isize); |
| 461 | /* |
| 462 | * Loop over the old directory finding the place we're going |
| 463 | * to insert the new entry. |
| 464 | * If it's going to end up at the end then oldsfep will point there. |
| 465 | */ |
| 466 | for (offset = XFS_DIR2_DATA_FIRST_OFFSET, |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 467 | oldsfep = xfs_dir2_sf_firstentry(oldsfp), |
| 468 | add_datasize = xfs_dir2_data_entsize(args->namelen), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | eof = (char *)oldsfep == &buf[old_isize]; |
| 470 | !eof; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 471 | offset = new_offset + xfs_dir2_data_entsize(oldsfep->namelen), |
| 472 | oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | eof = (char *)oldsfep == &buf[old_isize]) { |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 474 | new_offset = xfs_dir2_sf_get_offset(oldsfep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | if (offset + add_datasize <= new_offset) |
| 476 | break; |
| 477 | } |
| 478 | /* |
| 479 | * Get rid of the old directory, then allocate space for |
| 480 | * the new one. We do this so xfs_idata_realloc won't copy |
| 481 | * the data. |
| 482 | */ |
| 483 | xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK); |
| 484 | xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK); |
| 485 | /* |
| 486 | * Reset the pointer since the buffer was reallocated. |
| 487 | */ |
| 488 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
| 489 | /* |
| 490 | * Copy the first part of the directory, including the header. |
| 491 | */ |
| 492 | nbytes = (int)((char *)oldsfep - (char *)oldsfp); |
| 493 | memcpy(sfp, oldsfp, nbytes); |
| 494 | sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes); |
| 495 | /* |
| 496 | * Fill in the new entry, and update the header counts. |
| 497 | */ |
| 498 | sfep->namelen = args->namelen; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 499 | xfs_dir2_sf_put_offset(sfep, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | memcpy(sfep->name, args->name, sfep->namelen); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 501 | xfs_dir2_sf_put_inumber(sfp, &args->inumber, |
| 502 | xfs_dir2_sf_inumberp(sfep)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | sfp->hdr.count++; |
| 504 | #if XFS_BIG_INUMS |
| 505 | if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange) |
| 506 | sfp->hdr.i8count++; |
| 507 | #endif |
| 508 | /* |
| 509 | * If there's more left to copy, do that. |
| 510 | */ |
| 511 | if (!eof) { |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 512 | sfep = xfs_dir2_sf_nextentry(sfp, sfep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | memcpy(sfep, oldsfep, old_isize - nbytes); |
| 514 | } |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 515 | kmem_free(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | dp->i_d.di_size = new_isize; |
| 517 | xfs_dir2_sf_check(args); |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * Decide if the new entry will fit at all. |
| 522 | * If it will fit, pick between adding the new entry to the end (easy) |
| 523 | * or somewhere else (hard). |
| 524 | * Return 0 (won't fit), 1 (easy), 2 (hard). |
| 525 | */ |
| 526 | /*ARGSUSED*/ |
| 527 | static int /* pick result */ |
| 528 | xfs_dir2_sf_addname_pick( |
| 529 | xfs_da_args_t *args, /* operation arguments */ |
| 530 | int objchange, /* inode # size changes */ |
| 531 | xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */ |
| 532 | xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */ |
| 533 | { |
| 534 | xfs_inode_t *dp; /* incore directory inode */ |
| 535 | int holefit; /* found hole it will fit in */ |
| 536 | int i; /* entry number */ |
| 537 | xfs_mount_t *mp; /* filesystem mount point */ |
| 538 | xfs_dir2_data_aoff_t offset; /* data block offset */ |
| 539 | xfs_dir2_sf_entry_t *sfep; /* shortform entry */ |
| 540 | xfs_dir2_sf_t *sfp; /* shortform structure */ |
| 541 | int size; /* entry's data size */ |
| 542 | int used; /* data bytes used */ |
| 543 | |
| 544 | dp = args->dp; |
| 545 | mp = dp->i_mount; |
| 546 | |
| 547 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 548 | size = xfs_dir2_data_entsize(args->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | offset = XFS_DIR2_DATA_FIRST_OFFSET; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 550 | sfep = xfs_dir2_sf_firstentry(sfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | holefit = 0; |
| 552 | /* |
| 553 | * Loop over sf entries. |
| 554 | * Keep track of data offset and whether we've seen a place |
| 555 | * to insert the new entry. |
| 556 | */ |
| 557 | for (i = 0; i < sfp->hdr.count; i++) { |
| 558 | if (!holefit) |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 559 | holefit = offset + size <= xfs_dir2_sf_get_offset(sfep); |
| 560 | offset = xfs_dir2_sf_get_offset(sfep) + |
| 561 | xfs_dir2_data_entsize(sfep->namelen); |
| 562 | sfep = xfs_dir2_sf_nextentry(sfp, sfep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
| 564 | /* |
| 565 | * Calculate data bytes used excluding the new entry, if this |
| 566 | * was a data block (block form directory). |
| 567 | */ |
| 568 | used = offset + |
| 569 | (sfp->hdr.count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) + |
| 570 | (uint)sizeof(xfs_dir2_block_tail_t); |
| 571 | /* |
| 572 | * If it won't fit in a block form then we can't insert it, |
| 573 | * we'll go back, convert to block, then try the insert and convert |
| 574 | * to leaf. |
| 575 | */ |
| 576 | if (used + (holefit ? 0 : size) > mp->m_dirblksize) |
| 577 | return 0; |
| 578 | /* |
| 579 | * If changing the inode number size, do it the hard way. |
| 580 | */ |
| 581 | #if XFS_BIG_INUMS |
| 582 | if (objchange) { |
| 583 | return 2; |
| 584 | } |
| 585 | #else |
| 586 | ASSERT(objchange == 0); |
| 587 | #endif |
| 588 | /* |
| 589 | * If it won't fit at the end then do it the hard way (use the hole). |
| 590 | */ |
| 591 | if (used + size > mp->m_dirblksize) |
| 592 | return 2; |
| 593 | /* |
| 594 | * Do it the easy way. |
| 595 | */ |
| 596 | *sfepp = sfep; |
| 597 | *offsetp = offset; |
| 598 | return 1; |
| 599 | } |
| 600 | |
| 601 | #ifdef DEBUG |
| 602 | /* |
| 603 | * Check consistency of shortform directory, assert if bad. |
| 604 | */ |
| 605 | static void |
| 606 | xfs_dir2_sf_check( |
| 607 | xfs_da_args_t *args) /* operation arguments */ |
| 608 | { |
| 609 | xfs_inode_t *dp; /* incore directory inode */ |
| 610 | int i; /* entry number */ |
| 611 | int i8count; /* number of big inode#s */ |
| 612 | xfs_ino_t ino; /* entry inode number */ |
| 613 | int offset; /* data offset */ |
| 614 | xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */ |
| 615 | xfs_dir2_sf_t *sfp; /* shortform structure */ |
| 616 | |
| 617 | dp = args->dp; |
| 618 | |
| 619 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
| 620 | offset = XFS_DIR2_DATA_FIRST_OFFSET; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 621 | ino = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | i8count = ino > XFS_DIR2_MAX_SHORT_INUM; |
| 623 | |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 624 | for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | i < sfp->hdr.count; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 626 | i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) { |
| 627 | ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset); |
| 628 | ino = xfs_dir2_sf_get_inumber(sfp, xfs_dir2_sf_inumberp(sfep)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | i8count += ino > XFS_DIR2_MAX_SHORT_INUM; |
| 630 | offset = |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 631 | xfs_dir2_sf_get_offset(sfep) + |
| 632 | xfs_dir2_data_entsize(sfep->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
| 634 | ASSERT(i8count == sfp->hdr.i8count); |
| 635 | ASSERT(XFS_BIG_INUMS || i8count == 0); |
| 636 | ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size); |
| 637 | ASSERT(offset + |
| 638 | (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) + |
| 639 | (uint)sizeof(xfs_dir2_block_tail_t) <= |
| 640 | dp->i_mount->m_dirblksize); |
| 641 | } |
| 642 | #endif /* DEBUG */ |
| 643 | |
| 644 | /* |
| 645 | * Create a new (shortform) directory. |
| 646 | */ |
| 647 | int /* error, always 0 */ |
| 648 | xfs_dir2_sf_create( |
| 649 | xfs_da_args_t *args, /* operation arguments */ |
| 650 | xfs_ino_t pino) /* parent inode number */ |
| 651 | { |
| 652 | xfs_inode_t *dp; /* incore directory inode */ |
| 653 | int i8count; /* parent inode is an 8-byte number */ |
| 654 | xfs_dir2_sf_t *sfp; /* shortform structure */ |
| 655 | int size; /* directory size */ |
| 656 | |
| 657 | xfs_dir2_trace_args_i("sf_create", args, pino); |
| 658 | dp = args->dp; |
| 659 | |
| 660 | ASSERT(dp != NULL); |
| 661 | ASSERT(dp->i_d.di_size == 0); |
| 662 | /* |
| 663 | * If it's currently a zero-length extent file, |
| 664 | * convert it to local format. |
| 665 | */ |
| 666 | if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) { |
| 667 | dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */ |
| 668 | dp->i_d.di_format = XFS_DINODE_FMT_LOCAL; |
| 669 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE); |
| 670 | dp->i_df.if_flags |= XFS_IFINLINE; |
| 671 | } |
| 672 | ASSERT(dp->i_df.if_flags & XFS_IFINLINE); |
| 673 | ASSERT(dp->i_df.if_bytes == 0); |
| 674 | i8count = pino > XFS_DIR2_MAX_SHORT_INUM; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 675 | size = xfs_dir2_sf_hdr_size(i8count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | /* |
| 677 | * Make a buffer for the data. |
| 678 | */ |
| 679 | xfs_idata_realloc(dp, size, XFS_DATA_FORK); |
| 680 | /* |
| 681 | * Fill in the header, |
| 682 | */ |
| 683 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
| 684 | sfp->hdr.i8count = i8count; |
| 685 | /* |
| 686 | * Now can put in the inode number, since i8count is set. |
| 687 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 688 | xfs_dir2_sf_put_inumber(sfp, &pino, &sfp->hdr.parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | sfp->hdr.count = 0; |
| 690 | dp->i_d.di_size = size; |
| 691 | xfs_dir2_sf_check(args); |
| 692 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA); |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | int /* error */ |
| 697 | xfs_dir2_sf_getdents( |
| 698 | xfs_inode_t *dp, /* incore directory inode */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 699 | void *dirent, |
| 700 | xfs_off_t *offset, |
| 701 | filldir_t filldir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | int i; /* shortform entry number */ |
| 704 | xfs_mount_t *mp; /* filesystem mount point */ |
| 705 | xfs_dir2_dataptr_t off; /* current entry's offset */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */ |
| 707 | xfs_dir2_sf_t *sfp; /* shortform structure */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 708 | xfs_dir2_dataptr_t dot_offset; |
| 709 | xfs_dir2_dataptr_t dotdot_offset; |
| 710 | xfs_ino_t ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
| 712 | mp = dp->i_mount; |
| 713 | |
| 714 | ASSERT(dp->i_df.if_flags & XFS_IFINLINE); |
| 715 | /* |
| 716 | * Give up if the directory is way too short. |
| 717 | */ |
| 718 | if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) { |
| 719 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 720 | return XFS_ERROR(EIO); |
| 721 | } |
| 722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | ASSERT(dp->i_df.if_bytes == dp->i_d.di_size); |
| 724 | ASSERT(dp->i_df.if_u1.if_data != NULL); |
| 725 | |
| 726 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
| 727 | |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 728 | 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] | 729 | |
| 730 | /* |
| 731 | * If the block number in the offset is out of range, we're done. |
| 732 | */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 733 | if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
| 736 | /* |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 737 | * Precalculate offsets for . and .. as we will always need them. |
| 738 | * |
| 739 | * XXX(hch): the second argument is sometimes 0 and sometimes |
| 740 | * mp->m_dirdatablk. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 742 | dot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk, |
| 743 | XFS_DIR2_DATA_DOT_OFFSET); |
| 744 | dotdot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk, |
| 745 | XFS_DIR2_DATA_DOTDOT_OFFSET); |
| 746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | /* |
| 748 | * Put . entry unless we're starting past it. |
| 749 | */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 750 | if (*offset <= dot_offset) { |
| 751 | ino = dp->i_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | #if XFS_BIG_INUMS |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 753 | ino += mp->m_inoadd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | #endif |
Lachlan McIlroy | 041388b | 2007-12-18 16:19:34 +1100 | [diff] [blame] | 755 | if (filldir(dirent, ".", 1, dot_offset, ino, DT_DIR)) { |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 756 | *offset = dot_offset; |
| 757 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | * Put .. entry unless we're starting past it. |
| 763 | */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 764 | if (*offset <= dotdot_offset) { |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 765 | ino = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | #if XFS_BIG_INUMS |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 767 | ino += mp->m_inoadd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | #endif |
Lachlan McIlroy | 041388b | 2007-12-18 16:19:34 +1100 | [diff] [blame] | 769 | if (filldir(dirent, "..", 2, dotdot_offset, ino, DT_DIR)) { |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 770 | *offset = dotdot_offset; |
| 771 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * Loop while there are more entries and put'ing works. |
| 777 | */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 778 | sfep = xfs_dir2_sf_firstentry(sfp); |
| 779 | for (i = 0; i < sfp->hdr.count; i++) { |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 780 | off = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk, |
| 781 | xfs_dir2_sf_get_offset(sfep)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 783 | if (*offset > off) { |
| 784 | sfep = xfs_dir2_sf_nextentry(sfp, sfep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | } |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 787 | |
| 788 | ino = xfs_dir2_sf_get_inumber(sfp, xfs_dir2_sf_inumberp(sfep)); |
| 789 | #if XFS_BIG_INUMS |
| 790 | ino += mp->m_inoadd; |
| 791 | #endif |
| 792 | |
| 793 | if (filldir(dirent, sfep->name, sfep->namelen, |
Lachlan McIlroy | 041388b | 2007-12-18 16:19:34 +1100 | [diff] [blame] | 794 | off, ino, DT_UNKNOWN)) { |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 795 | *offset = off; |
| 796 | return 0; |
| 797 | } |
| 798 | sfep = xfs_dir2_sf_nextentry(sfp, sfep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 801 | *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | /* |
| 806 | * Lookup an entry in a shortform directory. |
| 807 | * Returns EEXIST if found, ENOENT if not found. |
| 808 | */ |
| 809 | int /* error */ |
| 810 | xfs_dir2_sf_lookup( |
| 811 | xfs_da_args_t *args) /* operation arguments */ |
| 812 | { |
| 813 | xfs_inode_t *dp; /* incore directory inode */ |
| 814 | int i; /* entry index */ |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 815 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */ |
| 817 | xfs_dir2_sf_t *sfp; /* shortform structure */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 818 | enum xfs_dacmp cmp; /* comparison result */ |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 819 | xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
| 821 | xfs_dir2_trace_args("sf_lookup", args); |
| 822 | xfs_dir2_sf_check(args); |
| 823 | dp = args->dp; |
| 824 | |
| 825 | ASSERT(dp->i_df.if_flags & XFS_IFINLINE); |
| 826 | /* |
| 827 | * Bail out if the directory is way too short. |
| 828 | */ |
| 829 | if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) { |
| 830 | ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount)); |
| 831 | return XFS_ERROR(EIO); |
| 832 | } |
| 833 | ASSERT(dp->i_df.if_bytes == dp->i_d.di_size); |
| 834 | ASSERT(dp->i_df.if_u1.if_data != NULL); |
| 835 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 836 | 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] | 837 | /* |
| 838 | * Special case for . |
| 839 | */ |
| 840 | if (args->namelen == 1 && args->name[0] == '.') { |
| 841 | args->inumber = dp->i_ino; |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 842 | args->cmpresult = XFS_CMP_EXACT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | return XFS_ERROR(EEXIST); |
| 844 | } |
| 845 | /* |
| 846 | * Special case for .. |
| 847 | */ |
| 848 | if (args->namelen == 2 && |
| 849 | args->name[0] == '.' && args->name[1] == '.') { |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 850 | args->inumber = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 851 | args->cmpresult = XFS_CMP_EXACT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | return XFS_ERROR(EEXIST); |
| 853 | } |
| 854 | /* |
| 855 | * Loop over all the entries trying to match ours. |
| 856 | */ |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 857 | ci_sfep = NULL; |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 858 | for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->hdr.count; |
| 859 | i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) { |
| 860 | /* |
| 861 | * Compare name and if it's an exact match, return the inode |
| 862 | * number. If it's the first case-insensitive match, store the |
| 863 | * inode number and continue looking for an exact match. |
| 864 | */ |
| 865 | cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name, |
| 866 | sfep->namelen); |
| 867 | if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { |
| 868 | args->cmpresult = cmp; |
| 869 | args->inumber = xfs_dir2_sf_get_inumber(sfp, |
| 870 | xfs_dir2_sf_inumberp(sfep)); |
| 871 | if (cmp == XFS_CMP_EXACT) |
| 872 | return XFS_ERROR(EEXIST); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 873 | ci_sfep = sfep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | } |
| 875 | } |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 876 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 877 | /* |
| 878 | * Here, we can only be doing a lookup (not a rename or replace). |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 879 | * If a case-insensitive match was not found, return ENOENT. |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 880 | */ |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 881 | if (!ci_sfep) |
| 882 | return XFS_ERROR(ENOENT); |
| 883 | /* otherwise process the CI match as required by the caller */ |
| 884 | error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen); |
| 885 | return XFS_ERROR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /* |
| 889 | * Remove an entry from a shortform directory. |
| 890 | */ |
| 891 | int /* error */ |
| 892 | xfs_dir2_sf_removename( |
| 893 | xfs_da_args_t *args) |
| 894 | { |
| 895 | int byteoff; /* offset of removed entry */ |
| 896 | xfs_inode_t *dp; /* incore directory inode */ |
| 897 | int entsize; /* this entry's size */ |
| 898 | int i; /* shortform entry index */ |
| 899 | int newsize; /* new inode size */ |
| 900 | int oldsize; /* old inode size */ |
| 901 | xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */ |
| 902 | xfs_dir2_sf_t *sfp; /* shortform structure */ |
| 903 | |
| 904 | xfs_dir2_trace_args("sf_removename", args); |
| 905 | dp = args->dp; |
| 906 | |
| 907 | ASSERT(dp->i_df.if_flags & XFS_IFINLINE); |
| 908 | oldsize = (int)dp->i_d.di_size; |
| 909 | /* |
| 910 | * Bail out if the directory is way too short. |
| 911 | */ |
| 912 | if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) { |
| 913 | ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount)); |
| 914 | return XFS_ERROR(EIO); |
| 915 | } |
| 916 | ASSERT(dp->i_df.if_bytes == oldsize); |
| 917 | ASSERT(dp->i_df.if_u1.if_data != NULL); |
| 918 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 919 | ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | /* |
| 921 | * Loop over the old directory entries. |
| 922 | * Find the one we're deleting. |
| 923 | */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 924 | for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->hdr.count; |
| 925 | i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) { |
| 926 | if (xfs_da_compname(args, sfep->name, sfep->namelen) == |
| 927 | XFS_CMP_EXACT) { |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 928 | ASSERT(xfs_dir2_sf_get_inumber(sfp, |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 929 | xfs_dir2_sf_inumberp(sfep)) == |
| 930 | args->inumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | break; |
| 932 | } |
| 933 | } |
| 934 | /* |
| 935 | * Didn't find it. |
| 936 | */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 937 | if (i == sfp->hdr.count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | return XFS_ERROR(ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | /* |
| 940 | * Calculate sizes. |
| 941 | */ |
| 942 | byteoff = (int)((char *)sfep - (char *)sfp); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 943 | entsize = xfs_dir2_sf_entsize_byname(sfp, args->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | newsize = oldsize - entsize; |
| 945 | /* |
| 946 | * Copy the part if any after the removed entry, sliding it down. |
| 947 | */ |
| 948 | if (byteoff + entsize < oldsize) |
| 949 | memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize, |
| 950 | oldsize - (byteoff + entsize)); |
| 951 | /* |
| 952 | * Fix up the header and file size. |
| 953 | */ |
| 954 | sfp->hdr.count--; |
| 955 | dp->i_d.di_size = newsize; |
| 956 | /* |
| 957 | * Reallocate, making it smaller. |
| 958 | */ |
| 959 | xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK); |
| 960 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
| 961 | #if XFS_BIG_INUMS |
| 962 | /* |
| 963 | * Are we changing inode number size? |
| 964 | */ |
| 965 | if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) { |
| 966 | if (sfp->hdr.i8count == 1) |
| 967 | xfs_dir2_sf_toino4(args); |
| 968 | else |
| 969 | sfp->hdr.i8count--; |
| 970 | } |
| 971 | #endif |
| 972 | xfs_dir2_sf_check(args); |
| 973 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA); |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | /* |
| 978 | * Replace the inode number of an entry in a shortform directory. |
| 979 | */ |
| 980 | int /* error */ |
| 981 | xfs_dir2_sf_replace( |
| 982 | xfs_da_args_t *args) /* operation arguments */ |
| 983 | { |
| 984 | xfs_inode_t *dp; /* incore directory inode */ |
| 985 | int i; /* entry index */ |
| 986 | #if XFS_BIG_INUMS || defined(DEBUG) |
| 987 | xfs_ino_t ino=0; /* entry old inode number */ |
| 988 | #endif |
| 989 | #if XFS_BIG_INUMS |
| 990 | int i8elevated; /* sf_toino8 set i8count=1 */ |
| 991 | #endif |
| 992 | xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */ |
| 993 | xfs_dir2_sf_t *sfp; /* shortform structure */ |
| 994 | |
| 995 | xfs_dir2_trace_args("sf_replace", args); |
| 996 | dp = args->dp; |
| 997 | |
| 998 | ASSERT(dp->i_df.if_flags & XFS_IFINLINE); |
| 999 | /* |
| 1000 | * Bail out if the shortform directory is way too small. |
| 1001 | */ |
| 1002 | if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) { |
| 1003 | ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount)); |
| 1004 | return XFS_ERROR(EIO); |
| 1005 | } |
| 1006 | ASSERT(dp->i_df.if_bytes == dp->i_d.di_size); |
| 1007 | ASSERT(dp->i_df.if_u1.if_data != NULL); |
| 1008 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1009 | 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] | 1010 | #if XFS_BIG_INUMS |
| 1011 | /* |
| 1012 | * New inode number is large, and need to convert to 8-byte inodes. |
| 1013 | */ |
| 1014 | if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) { |
| 1015 | int error; /* error return value */ |
| 1016 | int newsize; /* new inode size */ |
| 1017 | |
| 1018 | newsize = |
| 1019 | dp->i_df.if_bytes + |
| 1020 | (sfp->hdr.count + 1) * |
| 1021 | ((uint)sizeof(xfs_dir2_ino8_t) - |
| 1022 | (uint)sizeof(xfs_dir2_ino4_t)); |
| 1023 | /* |
| 1024 | * Won't fit as shortform, convert to block then do replace. |
| 1025 | */ |
| 1026 | if (newsize > XFS_IFORK_DSIZE(dp)) { |
| 1027 | error = xfs_dir2_sf_to_block(args); |
| 1028 | if (error) { |
| 1029 | return error; |
| 1030 | } |
| 1031 | return xfs_dir2_block_replace(args); |
| 1032 | } |
| 1033 | /* |
| 1034 | * Still fits, convert to 8-byte now. |
| 1035 | */ |
| 1036 | xfs_dir2_sf_toino8(args); |
| 1037 | i8elevated = 1; |
| 1038 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
| 1039 | } else |
| 1040 | i8elevated = 0; |
| 1041 | #endif |
| 1042 | ASSERT(args->namelen != 1 || args->name[0] != '.'); |
| 1043 | /* |
| 1044 | * Replace ..'s entry. |
| 1045 | */ |
| 1046 | if (args->namelen == 2 && |
| 1047 | args->name[0] == '.' && args->name[1] == '.') { |
| 1048 | #if XFS_BIG_INUMS || defined(DEBUG) |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1049 | ino = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | ASSERT(args->inumber != ino); |
| 1051 | #endif |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1052 | xfs_dir2_sf_put_inumber(sfp, &args->inumber, &sfp->hdr.parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | } |
| 1054 | /* |
| 1055 | * Normal entry, look for the name. |
| 1056 | */ |
| 1057 | else { |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1058 | for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1059 | i < sfp->hdr.count; |
| 1060 | i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) { |
| 1061 | if (xfs_da_compname(args, sfep->name, sfep->namelen) == |
| 1062 | XFS_CMP_EXACT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | #if XFS_BIG_INUMS || defined(DEBUG) |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1064 | ino = xfs_dir2_sf_get_inumber(sfp, |
| 1065 | xfs_dir2_sf_inumberp(sfep)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | ASSERT(args->inumber != ino); |
| 1067 | #endif |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1068 | xfs_dir2_sf_put_inumber(sfp, &args->inumber, |
| 1069 | xfs_dir2_sf_inumberp(sfep)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | break; |
| 1071 | } |
| 1072 | } |
| 1073 | /* |
| 1074 | * Didn't find it. |
| 1075 | */ |
| 1076 | if (i == sfp->hdr.count) { |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 1077 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | #if XFS_BIG_INUMS |
| 1079 | if (i8elevated) |
| 1080 | xfs_dir2_sf_toino4(args); |
| 1081 | #endif |
| 1082 | return XFS_ERROR(ENOENT); |
| 1083 | } |
| 1084 | } |
| 1085 | #if XFS_BIG_INUMS |
| 1086 | /* |
| 1087 | * See if the old number was large, the new number is small. |
| 1088 | */ |
| 1089 | if (ino > XFS_DIR2_MAX_SHORT_INUM && |
| 1090 | args->inumber <= XFS_DIR2_MAX_SHORT_INUM) { |
| 1091 | /* |
| 1092 | * And the old count was one, so need to convert to small. |
| 1093 | */ |
| 1094 | if (sfp->hdr.i8count == 1) |
| 1095 | xfs_dir2_sf_toino4(args); |
| 1096 | else |
| 1097 | sfp->hdr.i8count--; |
| 1098 | } |
| 1099 | /* |
| 1100 | * See if the old number was small, the new number is large. |
| 1101 | */ |
| 1102 | if (ino <= XFS_DIR2_MAX_SHORT_INUM && |
| 1103 | args->inumber > XFS_DIR2_MAX_SHORT_INUM) { |
| 1104 | /* |
| 1105 | * add to the i8count unless we just converted to 8-byte |
| 1106 | * inodes (which does an implied i8count = 1) |
| 1107 | */ |
| 1108 | ASSERT(sfp->hdr.i8count != 0); |
| 1109 | if (!i8elevated) |
| 1110 | sfp->hdr.i8count++; |
| 1111 | } |
| 1112 | #endif |
| 1113 | xfs_dir2_sf_check(args); |
| 1114 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA); |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | #if XFS_BIG_INUMS |
| 1119 | /* |
| 1120 | * Convert from 8-byte inode numbers to 4-byte inode numbers. |
| 1121 | * The last 8-byte inode number is gone, but the count is still 1. |
| 1122 | */ |
| 1123 | static void |
| 1124 | xfs_dir2_sf_toino4( |
| 1125 | xfs_da_args_t *args) /* operation arguments */ |
| 1126 | { |
| 1127 | char *buf; /* old dir's buffer */ |
| 1128 | xfs_inode_t *dp; /* incore directory inode */ |
| 1129 | int i; /* entry index */ |
| 1130 | xfs_ino_t ino; /* entry inode number */ |
| 1131 | int newsize; /* new inode size */ |
| 1132 | xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */ |
| 1133 | xfs_dir2_sf_t *oldsfp; /* old sf directory */ |
| 1134 | int oldsize; /* old inode size */ |
| 1135 | xfs_dir2_sf_entry_t *sfep; /* new sf entry */ |
| 1136 | xfs_dir2_sf_t *sfp; /* new sf directory */ |
| 1137 | |
| 1138 | xfs_dir2_trace_args("sf_toino4", args); |
| 1139 | dp = args->dp; |
| 1140 | |
| 1141 | /* |
| 1142 | * Copy the old directory to the buffer. |
| 1143 | * Then nuke it from the inode, and add the new buffer to the inode. |
| 1144 | * Don't want xfs_idata_realloc copying the data here. |
| 1145 | */ |
| 1146 | oldsize = dp->i_df.if_bytes; |
| 1147 | buf = kmem_alloc(oldsize, KM_SLEEP); |
| 1148 | oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
| 1149 | ASSERT(oldsfp->hdr.i8count == 1); |
| 1150 | memcpy(buf, oldsfp, oldsize); |
| 1151 | /* |
| 1152 | * Compute the new inode size. |
| 1153 | */ |
| 1154 | newsize = |
| 1155 | oldsize - |
| 1156 | (oldsfp->hdr.count + 1) * |
| 1157 | ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t)); |
| 1158 | xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK); |
| 1159 | xfs_idata_realloc(dp, newsize, XFS_DATA_FORK); |
| 1160 | /* |
| 1161 | * Reset our pointers, the data has moved. |
| 1162 | */ |
| 1163 | oldsfp = (xfs_dir2_sf_t *)buf; |
| 1164 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
| 1165 | /* |
| 1166 | * Fill in the new header. |
| 1167 | */ |
| 1168 | sfp->hdr.count = oldsfp->hdr.count; |
| 1169 | sfp->hdr.i8count = 0; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1170 | ino = xfs_dir2_sf_get_inumber(oldsfp, &oldsfp->hdr.parent); |
| 1171 | xfs_dir2_sf_put_inumber(sfp, &ino, &sfp->hdr.parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | /* |
| 1173 | * Copy the entries field by field. |
| 1174 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1175 | for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp), |
| 1176 | oldsfep = xfs_dir2_sf_firstentry(oldsfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | i < sfp->hdr.count; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1178 | i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep), |
| 1179 | oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | sfep->namelen = oldsfep->namelen; |
| 1181 | sfep->offset = oldsfep->offset; |
| 1182 | memcpy(sfep->name, oldsfep->name, sfep->namelen); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1183 | ino = xfs_dir2_sf_get_inumber(oldsfp, |
| 1184 | xfs_dir2_sf_inumberp(oldsfep)); |
| 1185 | xfs_dir2_sf_put_inumber(sfp, &ino, xfs_dir2_sf_inumberp(sfep)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | } |
| 1187 | /* |
| 1188 | * Clean up the inode. |
| 1189 | */ |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1190 | kmem_free(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | dp->i_d.di_size = newsize; |
| 1192 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA); |
| 1193 | } |
| 1194 | |
| 1195 | /* |
| 1196 | * Convert from 4-byte inode numbers to 8-byte inode numbers. |
| 1197 | * The new 8-byte inode number is not there yet, we leave with the |
| 1198 | * count 1 but no corresponding entry. |
| 1199 | */ |
| 1200 | static void |
| 1201 | xfs_dir2_sf_toino8( |
| 1202 | xfs_da_args_t *args) /* operation arguments */ |
| 1203 | { |
| 1204 | char *buf; /* old dir's buffer */ |
| 1205 | xfs_inode_t *dp; /* incore directory inode */ |
| 1206 | int i; /* entry index */ |
| 1207 | xfs_ino_t ino; /* entry inode number */ |
| 1208 | int newsize; /* new inode size */ |
| 1209 | xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */ |
| 1210 | xfs_dir2_sf_t *oldsfp; /* old sf directory */ |
| 1211 | int oldsize; /* old inode size */ |
| 1212 | xfs_dir2_sf_entry_t *sfep; /* new sf entry */ |
| 1213 | xfs_dir2_sf_t *sfp; /* new sf directory */ |
| 1214 | |
| 1215 | xfs_dir2_trace_args("sf_toino8", args); |
| 1216 | dp = args->dp; |
| 1217 | |
| 1218 | /* |
| 1219 | * Copy the old directory to the buffer. |
| 1220 | * Then nuke it from the inode, and add the new buffer to the inode. |
| 1221 | * Don't want xfs_idata_realloc copying the data here. |
| 1222 | */ |
| 1223 | oldsize = dp->i_df.if_bytes; |
| 1224 | buf = kmem_alloc(oldsize, KM_SLEEP); |
| 1225 | oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
| 1226 | ASSERT(oldsfp->hdr.i8count == 0); |
| 1227 | memcpy(buf, oldsfp, oldsize); |
| 1228 | /* |
| 1229 | * Compute the new inode size. |
| 1230 | */ |
| 1231 | newsize = |
| 1232 | oldsize + |
| 1233 | (oldsfp->hdr.count + 1) * |
| 1234 | ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t)); |
| 1235 | xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK); |
| 1236 | xfs_idata_realloc(dp, newsize, XFS_DATA_FORK); |
| 1237 | /* |
| 1238 | * Reset our pointers, the data has moved. |
| 1239 | */ |
| 1240 | oldsfp = (xfs_dir2_sf_t *)buf; |
| 1241 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; |
| 1242 | /* |
| 1243 | * Fill in the new header. |
| 1244 | */ |
| 1245 | sfp->hdr.count = oldsfp->hdr.count; |
| 1246 | sfp->hdr.i8count = 1; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1247 | ino = xfs_dir2_sf_get_inumber(oldsfp, &oldsfp->hdr.parent); |
| 1248 | xfs_dir2_sf_put_inumber(sfp, &ino, &sfp->hdr.parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | /* |
| 1250 | * Copy the entries field by field. |
| 1251 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1252 | for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp), |
| 1253 | oldsfep = xfs_dir2_sf_firstentry(oldsfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | i < sfp->hdr.count; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1255 | i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep), |
| 1256 | oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | sfep->namelen = oldsfep->namelen; |
| 1258 | sfep->offset = oldsfep->offset; |
| 1259 | memcpy(sfep->name, oldsfep->name, sfep->namelen); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1260 | ino = xfs_dir2_sf_get_inumber(oldsfp, |
| 1261 | xfs_dir2_sf_inumberp(oldsfep)); |
| 1262 | xfs_dir2_sf_put_inumber(sfp, &ino, xfs_dir2_sf_inumberp(sfep)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | } |
| 1264 | /* |
| 1265 | * Clean up the inode. |
| 1266 | */ |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1267 | kmem_free(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | dp->i_d.di_size = newsize; |
| 1269 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA); |
| 1270 | } |
| 1271 | #endif /* XFS_BIG_INUMS */ |