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