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