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