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