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