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. |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 3 | * Copyright (c) 2013 Red Hat, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * published by the Free Software Foundation. |
| 9 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 10 | * This program is distributed in the hope that it would be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 20 | #include "xfs_fs.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 21 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_mount.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 25 | #include "xfs_da_format.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 26 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_inode.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 28 | #include "xfs_trans.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_inode_item.h" |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 30 | #include "xfs_bmap.h" |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 31 | #include "xfs_buf_item.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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_error.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 35 | #include "xfs_trace.h" |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 36 | #include "xfs_cksum.h" |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 37 | #include "xfs_log.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Local function prototypes. |
| 41 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 42 | static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, struct xfs_buf *bp, |
| 43 | int first, int last); |
| 44 | static void xfs_dir2_block_log_tail(xfs_trans_t *tp, struct xfs_buf *bp); |
| 45 | static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, struct xfs_buf **bpp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | int *entno); |
| 47 | static int xfs_dir2_block_sort(const void *a, const void *b); |
| 48 | |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 49 | static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot; |
| 50 | |
| 51 | /* |
| 52 | * One-time startup routine called from xfs_init(). |
| 53 | */ |
| 54 | void |
| 55 | xfs_dir_startup(void) |
| 56 | { |
Dave Chinner | 4a24cb7 | 2010-01-20 10:48:05 +1100 | [diff] [blame] | 57 | xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1); |
| 58 | xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2); |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 59 | } |
| 60 | |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 61 | static xfs_failaddr_t |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 62 | xfs_dir3_block_verify( |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 63 | struct xfs_buf *bp) |
| 64 | { |
| 65 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 66 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 67 | |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 68 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 69 | if (hdr3->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 70 | return __this_address; |
Eric Sandeen | ce748ea | 2015-07-29 11:53:31 +1000 | [diff] [blame] | 71 | if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid)) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 72 | return __this_address; |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 73 | if (be64_to_cpu(hdr3->blkno) != bp->b_bn) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 74 | return __this_address; |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 75 | if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn))) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 76 | return __this_address; |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 77 | } else { |
| 78 | if (hdr3->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 79 | return __this_address; |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 80 | } |
Darrick J. Wong | 9101d37 | 2018-01-08 10:51:01 -0800 | [diff] [blame] | 81 | return __xfs_dir3_data_check(NULL, bp); |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 82 | } |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 83 | |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 84 | static void |
| 85 | xfs_dir3_block_read_verify( |
| 86 | struct xfs_buf *bp) |
| 87 | { |
| 88 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 89 | xfs_failaddr_t fa; |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 90 | |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 91 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
| 92 | !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF)) |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 93 | xfs_verifier_error(bp, -EFSBADCRC, __this_address); |
| 94 | else { |
| 95 | fa = xfs_dir3_block_verify(bp); |
| 96 | if (fa) |
| 97 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
| 98 | } |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 99 | } |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 100 | |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 101 | static void |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 102 | xfs_dir3_block_write_verify( |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 103 | struct xfs_buf *bp) |
| 104 | { |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 105 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Carlos Maiolino | fb1755a | 2018-01-24 13:38:48 -0800 | [diff] [blame^] | 106 | struct xfs_buf_log_item *bip = bp->b_log_item; |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 107 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 108 | xfs_failaddr_t fa; |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 109 | |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 110 | fa = xfs_dir3_block_verify(bp); |
| 111 | if (fa) { |
| 112 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | |
| 116 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 117 | return; |
| 118 | |
| 119 | if (bip) |
| 120 | hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn); |
| 121 | |
Eric Sandeen | f1dbcd7 | 2014-02-27 15:18:23 +1100 | [diff] [blame] | 122 | xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF); |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 123 | } |
| 124 | |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 125 | const struct xfs_buf_ops xfs_dir3_block_buf_ops = { |
Eric Sandeen | 233135b | 2016-01-04 16:10:19 +1100 | [diff] [blame] | 126 | .name = "xfs_dir3_block", |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 127 | .verify_read = xfs_dir3_block_read_verify, |
| 128 | .verify_write = xfs_dir3_block_write_verify, |
Darrick J. Wong | b557259 | 2018-01-08 10:51:08 -0800 | [diff] [blame] | 129 | .verify_struct = xfs_dir3_block_verify, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 130 | }; |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 131 | |
Dave Chinner | 4a8af27 | 2013-08-12 20:49:36 +1000 | [diff] [blame] | 132 | int |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 133 | xfs_dir3_block_read( |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 134 | struct xfs_trans *tp, |
| 135 | struct xfs_inode *dp, |
| 136 | struct xfs_buf **bpp) |
| 137 | { |
| 138 | struct xfs_mount *mp = dp->i_mount; |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 139 | int err; |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 140 | |
Dave Chinner | 7dda6e8 | 2014-06-06 15:11:18 +1000 | [diff] [blame] | 141 | err = xfs_da_read_buf(tp, dp, mp->m_dir_geo->datablk, -1, bpp, |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 142 | XFS_DATA_FORK, &xfs_dir3_block_buf_ops); |
Darrick J. Wong | cd87d86 | 2017-07-07 18:55:17 -0700 | [diff] [blame] | 143 | if (!err && tp && *bpp) |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 144 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_BLOCK_BUF); |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 145 | return err; |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static void |
| 149 | xfs_dir3_block_init( |
| 150 | struct xfs_mount *mp, |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 151 | struct xfs_trans *tp, |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 152 | struct xfs_buf *bp, |
| 153 | struct xfs_inode *dp) |
| 154 | { |
| 155 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
| 156 | |
| 157 | bp->b_ops = &xfs_dir3_block_buf_ops; |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 158 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_BLOCK_BUF); |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 159 | |
| 160 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 161 | memset(hdr3, 0, sizeof(*hdr3)); |
| 162 | hdr3->magic = cpu_to_be32(XFS_DIR3_BLOCK_MAGIC); |
| 163 | hdr3->blkno = cpu_to_be64(bp->b_bn); |
| 164 | hdr3->owner = cpu_to_be64(dp->i_ino); |
Eric Sandeen | ce748ea | 2015-07-29 11:53:31 +1000 | [diff] [blame] | 165 | uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid); |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 166 | return; |
| 167 | |
| 168 | } |
| 169 | hdr3->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC); |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static void |
| 173 | xfs_dir2_block_need_space( |
Dave Chinner | 2ca9877 | 2013-10-29 22:11:49 +1100 | [diff] [blame] | 174 | struct xfs_inode *dp, |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 175 | struct xfs_dir2_data_hdr *hdr, |
| 176 | struct xfs_dir2_block_tail *btp, |
| 177 | struct xfs_dir2_leaf_entry *blp, |
| 178 | __be16 **tagpp, |
| 179 | struct xfs_dir2_data_unused **dupp, |
| 180 | struct xfs_dir2_data_unused **enddupp, |
| 181 | int *compact, |
| 182 | int len) |
| 183 | { |
| 184 | struct xfs_dir2_data_free *bf; |
| 185 | __be16 *tagp = NULL; |
| 186 | struct xfs_dir2_data_unused *dup = NULL; |
| 187 | struct xfs_dir2_data_unused *enddup = NULL; |
| 188 | |
| 189 | *compact = 0; |
Dave Chinner | 2ca9877 | 2013-10-29 22:11:49 +1100 | [diff] [blame] | 190 | bf = dp->d_ops->data_bestfree_p(hdr); |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | * If there are stale entries we'll use one for the leaf. |
| 194 | */ |
| 195 | if (btp->stale) { |
| 196 | if (be16_to_cpu(bf[0].length) >= len) { |
| 197 | /* |
| 198 | * The biggest entry enough to avoid compaction. |
| 199 | */ |
| 200 | dup = (xfs_dir2_data_unused_t *) |
| 201 | ((char *)hdr + be16_to_cpu(bf[0].offset)); |
| 202 | goto out; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Will need to compact to make this work. |
| 207 | * Tag just before the first leaf entry. |
| 208 | */ |
| 209 | *compact = 1; |
| 210 | tagp = (__be16 *)blp - 1; |
| 211 | |
| 212 | /* Data object just before the first leaf entry. */ |
| 213 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); |
| 214 | |
| 215 | /* |
| 216 | * If it's not free then the data will go where the |
| 217 | * leaf data starts now, if it works at all. |
| 218 | */ |
| 219 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
| 220 | if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) * |
| 221 | (uint)sizeof(*blp) < len) |
| 222 | dup = NULL; |
| 223 | } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len) |
| 224 | dup = NULL; |
| 225 | else |
| 226 | dup = (xfs_dir2_data_unused_t *)blp; |
| 227 | goto out; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * no stale entries, so just use free space. |
| 232 | * Tag just before the first leaf entry. |
| 233 | */ |
| 234 | tagp = (__be16 *)blp - 1; |
| 235 | |
| 236 | /* Data object just before the first leaf entry. */ |
| 237 | enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); |
| 238 | |
| 239 | /* |
| 240 | * If it's not free then can't do this add without cleaning up: |
| 241 | * the space before the first leaf entry needs to be free so it |
| 242 | * can be expanded to hold the pointer to the new entry. |
| 243 | */ |
| 244 | if (be16_to_cpu(enddup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
| 245 | /* |
| 246 | * Check out the biggest freespace and see if it's the same one. |
| 247 | */ |
| 248 | dup = (xfs_dir2_data_unused_t *) |
| 249 | ((char *)hdr + be16_to_cpu(bf[0].offset)); |
| 250 | if (dup != enddup) { |
| 251 | /* |
| 252 | * Not the same free entry, just check its length. |
| 253 | */ |
| 254 | if (be16_to_cpu(dup->length) < len) |
| 255 | dup = NULL; |
| 256 | goto out; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * It is the biggest freespace, can it hold the leaf too? |
| 261 | */ |
| 262 | if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) { |
| 263 | /* |
| 264 | * Yes, use the second-largest entry instead if it works. |
| 265 | */ |
| 266 | if (be16_to_cpu(bf[1].length) >= len) |
| 267 | dup = (xfs_dir2_data_unused_t *) |
| 268 | ((char *)hdr + be16_to_cpu(bf[1].offset)); |
| 269 | else |
| 270 | dup = NULL; |
| 271 | } |
| 272 | } |
| 273 | out: |
| 274 | *tagpp = tagp; |
| 275 | *dupp = dup; |
| 276 | *enddupp = enddup; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * compact the leaf entries. |
| 281 | * Leave the highest-numbered stale entry stale. |
| 282 | * XXX should be the one closest to mid but mid is not yet computed. |
| 283 | */ |
| 284 | static void |
| 285 | xfs_dir2_block_compact( |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 286 | struct xfs_da_args *args, |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 287 | struct xfs_buf *bp, |
| 288 | struct xfs_dir2_data_hdr *hdr, |
| 289 | struct xfs_dir2_block_tail *btp, |
| 290 | struct xfs_dir2_leaf_entry *blp, |
| 291 | int *needlog, |
| 292 | int *lfloghigh, |
| 293 | int *lfloglow) |
| 294 | { |
| 295 | int fromidx; /* source leaf index */ |
| 296 | int toidx; /* target leaf index */ |
| 297 | int needscan = 0; |
| 298 | int highstale; /* high stale index */ |
| 299 | |
| 300 | fromidx = toidx = be32_to_cpu(btp->count) - 1; |
| 301 | highstale = *lfloghigh = -1; |
| 302 | for (; fromidx >= 0; fromidx--) { |
| 303 | if (blp[fromidx].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) { |
| 304 | if (highstale == -1) |
| 305 | highstale = toidx; |
| 306 | else { |
| 307 | if (*lfloghigh == -1) |
| 308 | *lfloghigh = toidx; |
| 309 | continue; |
| 310 | } |
| 311 | } |
| 312 | if (fromidx < toidx) |
| 313 | blp[toidx] = blp[fromidx]; |
| 314 | toidx--; |
| 315 | } |
| 316 | *lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1); |
| 317 | *lfloghigh -= be32_to_cpu(btp->stale) - 1; |
| 318 | be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1)); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 319 | xfs_dir2_data_make_free(args, bp, |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 320 | (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr), |
| 321 | (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)), |
| 322 | needlog, &needscan); |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 323 | btp->stale = cpu_to_be32(1); |
| 324 | /* |
| 325 | * If we now need to rebuild the bestfree map, do so. |
| 326 | * This needs to happen before the next call to use_free. |
| 327 | */ |
| 328 | if (needscan) |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 329 | xfs_dir2_data_freescan(args->dp, hdr, needlog); |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 330 | } |
| 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | /* |
| 333 | * Add an entry to a block directory. |
| 334 | */ |
| 335 | int /* error */ |
| 336 | xfs_dir2_block_addname( |
| 337 | xfs_da_args_t *args) /* directory op arguments */ |
| 338 | { |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 339 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 341 | struct xfs_buf *bp; /* buffer for block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 343 | int compact; /* need to compact leaf ents */ |
| 344 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 345 | xfs_inode_t *dp; /* directory inode */ |
| 346 | xfs_dir2_data_unused_t *dup; /* block unused entry */ |
| 347 | int error; /* error return value */ |
| 348 | xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */ |
| 349 | xfs_dahash_t hash; /* hash value of found entry */ |
| 350 | int high; /* high index for binary srch */ |
| 351 | int highstale; /* high stale index */ |
| 352 | int lfloghigh=0; /* last final leaf to log */ |
| 353 | int lfloglow=0; /* first final leaf to log */ |
| 354 | int len; /* length of the new entry */ |
| 355 | int low; /* low index for binary srch */ |
| 356 | int lowstale; /* low stale index */ |
| 357 | int mid=0; /* midpoint for binary srch */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | int needlog; /* need to log header */ |
| 359 | int needscan; /* need to rescan freespace */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 360 | __be16 *tagp; /* pointer to tag value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | xfs_trans_t *tp; /* transaction structure */ |
| 362 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 363 | trace_xfs_dir2_block_addname(args); |
| 364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | dp = args->dp; |
| 366 | tp = args->trans; |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 367 | |
| 368 | /* Read the (one and only) directory block into bp. */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 369 | error = xfs_dir3_block_read(tp, dp, &bp); |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 370 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | return error; |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 372 | |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 373 | len = dp->d_ops->data_entsize(args->namelen); |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | /* |
| 376 | * Set up pointers to parts of the block. |
| 377 | */ |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 378 | hdr = bp->b_addr; |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 379 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 380 | blp = xfs_dir2_block_leaf_p(btp); |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | /* |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 383 | * Find out if we can reuse stale entries or whether we need extra |
| 384 | * space for entry and new leaf. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | */ |
Dave Chinner | 2ca9877 | 2013-10-29 22:11:49 +1100 | [diff] [blame] | 386 | xfs_dir2_block_need_space(dp, hdr, btp, blp, &tagp, &dup, |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 387 | &enddup, &compact, len); |
| 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | /* |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 390 | * Done everything we need for a space check now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | */ |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 392 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 393 | xfs_trans_brelse(tp, bp); |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 394 | if (!dup) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 395 | return -ENOSPC; |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 396 | return 0; |
| 397 | } |
| 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | /* |
| 400 | * If we don't have space for the new entry & leaf ... |
| 401 | */ |
| 402 | if (!dup) { |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 403 | /* Don't have a space reservation: return no-space. */ |
| 404 | if (args->total == 0) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 405 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | /* |
| 407 | * Convert to the next larger format. |
| 408 | * Then add the new entry in that format. |
| 409 | */ |
| 410 | error = xfs_dir2_block_to_leaf(args, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | if (error) |
| 412 | return error; |
| 413 | return xfs_dir2_leaf_addname(args); |
| 414 | } |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | needlog = needscan = 0; |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | /* |
| 419 | * If need to compact the leaf entries, do it now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | */ |
Eric Sandeen | 37f1356 | 2013-01-10 10:41:48 -0600 | [diff] [blame] | 421 | if (compact) { |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 422 | xfs_dir2_block_compact(args, bp, hdr, btp, blp, &needlog, |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 423 | &lfloghigh, &lfloglow); |
Eric Sandeen | 37f1356 | 2013-01-10 10:41:48 -0600 | [diff] [blame] | 424 | /* recalculate blp post-compaction */ |
| 425 | blp = xfs_dir2_block_leaf_p(btp); |
| 426 | } else if (btp->stale) { |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 427 | /* |
| 428 | * Set leaf logging boundaries to impossible state. |
| 429 | * For the no-stale case they're set explicitly. |
| 430 | */ |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 431 | lfloglow = be32_to_cpu(btp->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | lfloghigh = -1; |
| 433 | } |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | /* |
| 436 | * Find the slot that's first lower than our hash value, -1 if none. |
| 437 | */ |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 438 | for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | mid = (low + high) >> 1; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 440 | if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | break; |
| 442 | if (hash < args->hashval) |
| 443 | low = mid + 1; |
| 444 | else |
| 445 | high = mid - 1; |
| 446 | } |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 447 | while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | mid--; |
| 449 | } |
| 450 | /* |
| 451 | * No stale entries, will use enddup space to hold new leaf. |
| 452 | */ |
| 453 | if (!btp->stale) { |
| 454 | /* |
| 455 | * Mark the space needed for the new leaf entry, now in use. |
| 456 | */ |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 457 | xfs_dir2_data_use_free(args, bp, enddup, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | (xfs_dir2_data_aoff_t) |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 459 | ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | sizeof(*blp)), |
| 461 | (xfs_dir2_data_aoff_t)sizeof(*blp), |
| 462 | &needlog, &needscan); |
| 463 | /* |
| 464 | * Update the tail (entry count). |
| 465 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 466 | be32_add_cpu(&btp->count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | /* |
| 468 | * If we now need to rebuild the bestfree map, do so. |
| 469 | * This needs to happen before the next call to use_free. |
| 470 | */ |
| 471 | if (needscan) { |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 472 | xfs_dir2_data_freescan(dp, hdr, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | needscan = 0; |
| 474 | } |
| 475 | /* |
| 476 | * Adjust pointer to the first leaf entry, we're about to move |
| 477 | * the table up one to open up space for the new leaf entry. |
| 478 | * Then adjust our index to match. |
| 479 | */ |
| 480 | blp--; |
| 481 | mid++; |
| 482 | if (mid) |
| 483 | memmove(blp, &blp[1], mid * sizeof(*blp)); |
| 484 | lfloglow = 0; |
| 485 | lfloghigh = mid; |
| 486 | } |
| 487 | /* |
| 488 | * Use a stale leaf for our new entry. |
| 489 | */ |
| 490 | else { |
| 491 | for (lowstale = mid; |
| 492 | lowstale >= 0 && |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 493 | blp[lowstale].address != |
| 494 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | lowstale--) |
| 496 | continue; |
| 497 | for (highstale = mid + 1; |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 498 | highstale < be32_to_cpu(btp->count) && |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 499 | blp[highstale].address != |
| 500 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | (lowstale < 0 || mid - lowstale > highstale - mid); |
| 502 | highstale++) |
| 503 | continue; |
| 504 | /* |
| 505 | * Move entries toward the low-numbered stale entry. |
| 506 | */ |
| 507 | if (lowstale >= 0 && |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 508 | (highstale == be32_to_cpu(btp->count) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | mid - lowstale <= highstale - mid)) { |
| 510 | if (mid - lowstale) |
| 511 | memmove(&blp[lowstale], &blp[lowstale + 1], |
| 512 | (mid - lowstale) * sizeof(*blp)); |
| 513 | lfloglow = MIN(lowstale, lfloglow); |
| 514 | lfloghigh = MAX(mid, lfloghigh); |
| 515 | } |
| 516 | /* |
| 517 | * Move entries toward the high-numbered stale entry. |
| 518 | */ |
| 519 | else { |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 520 | ASSERT(highstale < be32_to_cpu(btp->count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | mid++; |
| 522 | if (highstale - mid) |
| 523 | memmove(&blp[mid + 1], &blp[mid], |
| 524 | (highstale - mid) * sizeof(*blp)); |
| 525 | lfloglow = MIN(mid, lfloglow); |
| 526 | lfloghigh = MAX(highstale, lfloghigh); |
| 527 | } |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 528 | be32_add_cpu(&btp->stale, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
| 530 | /* |
| 531 | * Point to the new data entry. |
| 532 | */ |
| 533 | dep = (xfs_dir2_data_entry_t *)dup; |
| 534 | /* |
| 535 | * Fill in the leaf entry. |
| 536 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 537 | blp[mid].hashval = cpu_to_be32(args->hashval); |
Eric Sandeen | 2599405 | 2014-04-14 19:02:30 +1000 | [diff] [blame] | 538 | blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr( |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 539 | (char *)dep - (char *)hdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh); |
| 541 | /* |
| 542 | * Mark space for the data entry used. |
| 543 | */ |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 544 | xfs_dir2_data_use_free(args, bp, dup, |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 545 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | (xfs_dir2_data_aoff_t)len, &needlog, &needscan); |
| 547 | /* |
| 548 | * Create the new data entry. |
| 549 | */ |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 550 | dep->inumber = cpu_to_be64(args->inumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | dep->namelen = args->namelen; |
| 552 | memcpy(dep->name, args->name, args->namelen); |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 553 | dp->d_ops->data_put_ftype(dep, args->filetype); |
| 554 | tagp = dp->d_ops->data_entry_tag_p(dep); |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 555 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | /* |
| 557 | * Clean up the bestfree array and log the header, tail, and entry. |
| 558 | */ |
| 559 | if (needscan) |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 560 | xfs_dir2_data_freescan(dp, hdr, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | if (needlog) |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 562 | xfs_dir2_data_log_header(args, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | xfs_dir2_block_log_tail(tp, bp); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 564 | xfs_dir2_data_log_entry(args, bp, dep); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 565 | xfs_dir3_data_check(dp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | * Log leaf entries from the block. |
| 571 | */ |
| 572 | static void |
| 573 | xfs_dir2_block_log_leaf( |
| 574 | xfs_trans_t *tp, /* transaction structure */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 575 | struct xfs_buf *bp, /* block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | int first, /* index of first logged leaf */ |
| 577 | int last) /* index of last logged leaf */ |
| 578 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 579 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 580 | xfs_dir2_leaf_entry_t *blp; |
| 581 | xfs_dir2_block_tail_t *btp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 583 | btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 584 | blp = xfs_dir2_block_leaf_p(btp); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 585 | xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr), |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 586 | (uint)((char *)&blp[last + 1] - (char *)hdr - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Log the block tail. |
| 591 | */ |
| 592 | static void |
| 593 | xfs_dir2_block_log_tail( |
| 594 | xfs_trans_t *tp, /* transaction structure */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 595 | struct xfs_buf *bp) /* block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 597 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 598 | xfs_dir2_block_tail_t *btp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 600 | btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 601 | xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr), |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 602 | (uint)((char *)(btp + 1) - (char *)hdr - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* |
| 606 | * Look up an entry in the block. This is the external routine, |
| 607 | * xfs_dir2_block_lookup_int does the real work. |
| 608 | */ |
| 609 | int /* error */ |
| 610 | xfs_dir2_block_lookup( |
| 611 | xfs_da_args_t *args) /* dir lookup arguments */ |
| 612 | { |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 613 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 615 | struct xfs_buf *bp; /* block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 617 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 618 | xfs_inode_t *dp; /* incore inode */ |
| 619 | int ent; /* entry index */ |
| 620 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 622 | trace_xfs_dir2_block_lookup(args); |
| 623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | /* |
| 625 | * Get the buffer, look up the entry. |
| 626 | * If not found (ENOENT) then return, have no buffer. |
| 627 | */ |
| 628 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) |
| 629 | return error; |
| 630 | dp = args->dp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 631 | hdr = bp->b_addr; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 632 | xfs_dir3_data_check(dp, bp); |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 633 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 634 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | /* |
| 636 | * Get the offset from the leaf entry, to point to the data. |
| 637 | */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 638 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 639 | xfs_dir2_dataptr_to_off(args->geo, |
| 640 | be32_to_cpu(blp[ent].address))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | /* |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 642 | * Fill in inode number, CI name if appropriate, release the block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | */ |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 644 | args->inumber = be64_to_cpu(dep->inumber); |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 645 | args->filetype = dp->d_ops->data_get_ftype(dep); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 646 | error = xfs_dir_cilookup_result(args, dep->name, dep->namelen); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 647 | xfs_trans_brelse(args->trans, bp); |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 648 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Internal block lookup routine. |
| 653 | */ |
| 654 | static int /* error */ |
| 655 | xfs_dir2_block_lookup_int( |
| 656 | xfs_da_args_t *args, /* dir lookup arguments */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 657 | struct xfs_buf **bpp, /* returned block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | int *entno) /* returned entry number */ |
| 659 | { |
| 660 | xfs_dir2_dataptr_t addr; /* data entry address */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 661 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 663 | struct xfs_buf *bp; /* block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 665 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 666 | xfs_inode_t *dp; /* incore inode */ |
| 667 | int error; /* error return value */ |
| 668 | xfs_dahash_t hash; /* found hash value */ |
| 669 | int high; /* binary search high index */ |
| 670 | int low; /* binary search low index */ |
| 671 | int mid; /* binary search current idx */ |
| 672 | xfs_mount_t *mp; /* filesystem mount point */ |
| 673 | xfs_trans_t *tp; /* transaction pointer */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 674 | enum xfs_dacmp cmp; /* comparison result */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
| 676 | dp = args->dp; |
| 677 | tp = args->trans; |
| 678 | mp = dp->i_mount; |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 679 | |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 680 | error = xfs_dir3_block_read(tp, dp, &bp); |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 681 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | return error; |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 683 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 684 | hdr = bp->b_addr; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 685 | xfs_dir3_data_check(dp, bp); |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 686 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 687 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | /* |
| 689 | * Loop doing a binary search for our hash value. |
| 690 | * Find our entry, ENOENT if it's not there. |
| 691 | */ |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 692 | for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | ASSERT(low <= high); |
| 694 | mid = (low + high) >> 1; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 695 | if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | break; |
| 697 | if (hash < args->hashval) |
| 698 | low = mid + 1; |
| 699 | else |
| 700 | high = mid - 1; |
| 701 | if (low > high) { |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 702 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 703 | xfs_trans_brelse(tp, bp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 704 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | /* |
| 708 | * Back up to the first one with the right hash value. |
| 709 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 710 | while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | mid--; |
| 712 | } |
| 713 | /* |
| 714 | * Now loop forward through all the entries with the |
| 715 | * right hash value looking for our name. |
| 716 | */ |
| 717 | do { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 718 | if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | continue; |
| 720 | /* |
| 721 | * Get pointer to the entry from the leaf. |
| 722 | */ |
| 723 | dep = (xfs_dir2_data_entry_t *) |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 724 | ((char *)hdr + xfs_dir2_dataptr_to_off(args->geo, addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | /* |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 726 | * Compare name and if it's an exact match, return the index |
| 727 | * and buffer. If it's the first case-insensitive match, store |
| 728 | * the index and buffer and continue looking for an exact match. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 730 | cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen); |
| 731 | if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { |
| 732 | args->cmpresult = cmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | *bpp = bp; |
| 734 | *entno = mid; |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 735 | if (cmp == XFS_CMP_EXACT) |
| 736 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 738 | } while (++mid < be32_to_cpu(btp->count) && |
| 739 | be32_to_cpu(blp[mid].hashval) == hash); |
| 740 | |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 741 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 742 | /* |
| 743 | * Here, we can only be doing a lookup (not a rename or replace). |
| 744 | * If a case-insensitive match was found earlier, return success. |
| 745 | */ |
| 746 | if (args->cmpresult == XFS_CMP_CASE) |
| 747 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | /* |
| 749 | * No match, release the buffer and return ENOENT. |
| 750 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 751 | xfs_trans_brelse(tp, bp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 752 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /* |
| 756 | * Remove an entry from a block format directory. |
| 757 | * If that makes the block small enough to fit in shortform, transform it. |
| 758 | */ |
| 759 | int /* error */ |
| 760 | xfs_dir2_block_removename( |
| 761 | xfs_da_args_t *args) /* directory operation args */ |
| 762 | { |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 763 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 765 | struct xfs_buf *bp; /* block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 767 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 768 | xfs_inode_t *dp; /* incore inode */ |
| 769 | int ent; /* block leaf entry index */ |
| 770 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | int needlog; /* need to log block header */ |
| 772 | int needscan; /* need to fixup bestfree */ |
| 773 | xfs_dir2_sf_hdr_t sfh; /* shortform header */ |
| 774 | int size; /* shortform size */ |
| 775 | xfs_trans_t *tp; /* transaction pointer */ |
| 776 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 777 | trace_xfs_dir2_block_removename(args); |
| 778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | /* |
| 780 | * Look up the entry in the block. Gets the buffer and entry index. |
| 781 | * It will always be there, the vnodeops level does a lookup first. |
| 782 | */ |
| 783 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) { |
| 784 | return error; |
| 785 | } |
| 786 | dp = args->dp; |
| 787 | tp = args->trans; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 788 | hdr = bp->b_addr; |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 789 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 790 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | /* |
| 792 | * Point to the data entry using the leaf entry. |
| 793 | */ |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 794 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + |
| 795 | xfs_dir2_dataptr_to_off(args->geo, |
| 796 | be32_to_cpu(blp[ent].address))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | /* |
| 798 | * Mark the data entry's space free. |
| 799 | */ |
| 800 | needlog = needscan = 0; |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 801 | xfs_dir2_data_make_free(args, bp, |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 802 | (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr), |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 803 | dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | /* |
| 805 | * Fix up the block tail. |
| 806 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 807 | be32_add_cpu(&btp->stale, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | xfs_dir2_block_log_tail(tp, bp); |
| 809 | /* |
| 810 | * Remove the leaf entry by marking it stale. |
| 811 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 812 | blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | xfs_dir2_block_log_leaf(tp, bp, ent, ent); |
| 814 | /* |
| 815 | * Fix up bestfree, log the header if necessary. |
| 816 | */ |
| 817 | if (needscan) |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 818 | xfs_dir2_data_freescan(dp, hdr, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | if (needlog) |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 820 | xfs_dir2_data_log_header(args, bp); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 821 | xfs_dir3_data_check(dp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | /* |
| 823 | * See if the size as a shortform is good enough. |
| 824 | */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 825 | size = xfs_dir2_block_sfsize(dp, hdr, &sfh); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 826 | if (size > XFS_IFORK_DSIZE(dp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | return 0; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | /* |
| 830 | * If it works, do the conversion. |
| 831 | */ |
| 832 | return xfs_dir2_block_to_sf(args, bp, size, &sfh); |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | * Replace an entry in a V2 block directory. |
| 837 | * Change the inode number to the new value. |
| 838 | */ |
| 839 | int /* error */ |
| 840 | xfs_dir2_block_replace( |
| 841 | xfs_da_args_t *args) /* directory operation args */ |
| 842 | { |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 843 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 845 | struct xfs_buf *bp; /* block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 847 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 848 | xfs_inode_t *dp; /* incore inode */ |
| 849 | int ent; /* leaf entry index */ |
| 850 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 852 | trace_xfs_dir2_block_replace(args); |
| 853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | /* |
| 855 | * Lookup the entry in the directory. Get buffer and entry index. |
| 856 | * This will always succeed since the caller has already done a lookup. |
| 857 | */ |
| 858 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) { |
| 859 | return error; |
| 860 | } |
| 861 | dp = args->dp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 862 | hdr = bp->b_addr; |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 863 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 864 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | /* |
| 866 | * Point to the data entry we need to change. |
| 867 | */ |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 868 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + |
| 869 | xfs_dir2_dataptr_to_off(args->geo, |
| 870 | be32_to_cpu(blp[ent].address))); |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 871 | ASSERT(be64_to_cpu(dep->inumber) != args->inumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | /* |
| 873 | * Change the inode number to the new value. |
| 874 | */ |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 875 | dep->inumber = cpu_to_be64(args->inumber); |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 876 | dp->d_ops->data_put_ftype(dep, args->filetype); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 877 | xfs_dir2_data_log_entry(args, bp, dep); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 878 | xfs_dir3_data_check(dp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * Qsort comparison routine for the block leaf entries. |
| 884 | */ |
| 885 | static int /* sort order */ |
| 886 | xfs_dir2_block_sort( |
| 887 | const void *a, /* first leaf entry */ |
| 888 | const void *b) /* second leaf entry */ |
| 889 | { |
| 890 | const xfs_dir2_leaf_entry_t *la; /* first leaf entry */ |
| 891 | const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */ |
| 892 | |
| 893 | la = a; |
| 894 | lb = b; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 895 | return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 : |
| 896 | (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | /* |
| 900 | * Convert a V2 leaf directory to a V2 block directory if possible. |
| 901 | */ |
| 902 | int /* error */ |
| 903 | xfs_dir2_leaf_to_block( |
| 904 | xfs_da_args_t *args, /* operation arguments */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 905 | struct xfs_buf *lbp, /* leaf buffer */ |
| 906 | struct xfs_buf *dbp) /* data buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 908 | __be16 *bestsp; /* leaf bests table */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 909 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 911 | xfs_inode_t *dp; /* incore directory inode */ |
| 912 | xfs_dir2_data_unused_t *dup; /* unused data entry */ |
| 913 | int error; /* error return value */ |
| 914 | int from; /* leaf from index */ |
| 915 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 916 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 917 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
| 918 | xfs_mount_t *mp; /* file system mount point */ |
| 919 | int needlog; /* need to log data header */ |
| 920 | int needscan; /* need to scan for bestfree */ |
| 921 | xfs_dir2_sf_hdr_t sfh; /* shortform header */ |
| 922 | int size; /* bytes used */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 923 | __be16 *tagp; /* end of entry (tag) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | int to; /* block/leaf to index */ |
| 925 | xfs_trans_t *tp; /* transaction pointer */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 926 | struct xfs_dir2_leaf_entry *ents; |
| 927 | struct xfs_dir3_icleaf_hdr leafhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 929 | trace_xfs_dir2_leaf_to_block(args); |
| 930 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | dp = args->dp; |
| 932 | tp = args->trans; |
| 933 | mp = dp->i_mount; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 934 | leaf = lbp->b_addr; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 935 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 936 | ents = dp->d_ops->leaf_ents_p(leaf); |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 937 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 938 | |
| 939 | ASSERT(leafhdr.magic == XFS_DIR2_LEAF1_MAGIC || |
| 940 | leafhdr.magic == XFS_DIR3_LEAF1_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | /* |
| 942 | * If there are data blocks other than the first one, take this |
| 943 | * opportunity to remove trailing empty data blocks that may have |
| 944 | * been left behind during no-space-reservation operations. |
| 945 | * These will show up in the leaf bests table. |
| 946 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 947 | while (dp->i_d.di_size > args->geo->blksize) { |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 948 | int hdrsz; |
| 949 | |
Dave Chinner | 1c9a5b2 | 2013-10-30 09:15:02 +1100 | [diff] [blame] | 950 | hdrsz = dp->d_ops->data_entry_offset; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 951 | bestsp = xfs_dir2_leaf_bests_p(ltp); |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 952 | if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) == |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 953 | args->geo->blksize - hdrsz) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | if ((error = |
| 955 | xfs_dir2_leaf_trim_data(args, lbp, |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 956 | (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1)))) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 957 | return error; |
| 958 | } else |
| 959 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | } |
| 961 | /* |
| 962 | * Read the data block if we don't already have it, give up if it fails. |
| 963 | */ |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 964 | if (!dbp) { |
Dave Chinner | 7dda6e8 | 2014-06-06 15:11:18 +1000 | [diff] [blame] | 965 | error = xfs_dir3_data_read(tp, dp, args->geo->datablk, -1, &dbp); |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 966 | if (error) |
| 967 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | } |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 969 | hdr = dbp->b_addr; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 970 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
| 971 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC)); |
| 972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | /* |
| 974 | * Size of the "leaf" area in the block. |
| 975 | */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 976 | size = (uint)sizeof(xfs_dir2_block_tail_t) + |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 977 | (uint)sizeof(*lep) * (leafhdr.count - leafhdr.stale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | /* |
| 979 | * Look at the last data entry. |
| 980 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 981 | tagp = (__be16 *)((char *)hdr + args->geo->blksize) - 1; |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 982 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | /* |
| 984 | * If it's not free or is too short we can't do it. |
| 985 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 986 | if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG || |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 987 | be16_to_cpu(dup->length) < size) |
| 988 | return 0; |
| 989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | /* |
| 991 | * Start converting it to block form. |
| 992 | */ |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 993 | xfs_dir3_block_init(mp, tp, dbp, dp); |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | needlog = 1; |
| 996 | needscan = 0; |
| 997 | /* |
| 998 | * Use up the space at the end of the block (blp/btp). |
| 999 | */ |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1000 | xfs_dir2_data_use_free(args, dbp, dup, args->geo->blksize - size, size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | &needlog, &needscan); |
| 1002 | /* |
| 1003 | * Initialize the block tail. |
| 1004 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1005 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1006 | btp->count = cpu_to_be32(leafhdr.count - leafhdr.stale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | btp->stale = 0; |
| 1008 | xfs_dir2_block_log_tail(tp, dbp); |
| 1009 | /* |
| 1010 | * Initialize the block leaf area. We compact out stale entries. |
| 1011 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1012 | lep = xfs_dir2_block_leaf_p(btp); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1013 | for (from = to = 0; from < leafhdr.count; from++) { |
| 1014 | if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | continue; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1016 | lep[to++] = ents[from]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | } |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 1018 | ASSERT(to == be32_to_cpu(btp->count)); |
| 1019 | xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | /* |
| 1021 | * Scan the bestfree if we need it and log the data block header. |
| 1022 | */ |
| 1023 | if (needscan) |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 1024 | xfs_dir2_data_freescan(dp, hdr, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | if (needlog) |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1026 | xfs_dir2_data_log_header(args, dbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | /* |
| 1028 | * Pitch the old leaf block. |
| 1029 | */ |
Dave Chinner | 7dda6e8 | 2014-06-06 15:11:18 +1000 | [diff] [blame] | 1030 | error = xfs_da_shrink_inode(args, args->geo->leafblk, lbp); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1031 | if (error) |
| 1032 | return error; |
| 1033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | /* |
| 1035 | * Now see if the resulting block can be shrunken to shortform. |
| 1036 | */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1037 | size = xfs_dir2_block_sfsize(dp, hdr, &sfh); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1038 | if (size > XFS_IFORK_DSIZE(dp)) |
| 1039 | return 0; |
| 1040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | return xfs_dir2_block_to_sf(args, dbp, size, &sfh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | /* |
| 1045 | * Convert the shortform directory to block form. |
| 1046 | */ |
| 1047 | int /* error */ |
| 1048 | xfs_dir2_sf_to_block( |
| 1049 | xfs_da_args_t *args) /* operation arguments */ |
| 1050 | { |
| 1051 | xfs_dir2_db_t blkno; /* dir-relative block # (0) */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1052 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1054 | struct xfs_buf *bp; /* block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | xfs_dir2_block_tail_t *btp; /* block tail pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | xfs_dir2_data_entry_t *dep; /* data entry pointer */ |
| 1057 | xfs_inode_t *dp; /* incore directory inode */ |
| 1058 | int dummy; /* trash */ |
| 1059 | xfs_dir2_data_unused_t *dup; /* unused entry pointer */ |
| 1060 | int endoffset; /* end of data objects */ |
| 1061 | int error; /* error return value */ |
| 1062 | int i; /* index */ |
| 1063 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1064 | int needlog; /* need to log block header */ |
| 1065 | int needscan; /* need to scan block freespc */ |
| 1066 | int newoffset; /* offset from current entry */ |
| 1067 | int offset; /* target block offset */ |
| 1068 | xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */ |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1069 | xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */ |
| 1070 | xfs_dir2_sf_hdr_t *sfp; /* shortform header */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 1071 | __be16 *tagp; /* end of data entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | xfs_trans_t *tp; /* transaction pointer */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1073 | struct xfs_name name; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1074 | struct xfs_ifork *ifp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1076 | trace_xfs_dir2_sf_to_block(args); |
| 1077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | dp = args->dp; |
| 1079 | tp = args->trans; |
| 1080 | mp = dp->i_mount; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1081 | ifp = XFS_IFORK_PTR(dp, XFS_DATA_FORK); |
| 1082 | ASSERT(ifp->if_flags & XFS_IFINLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | /* |
| 1084 | * Bomb out if the shortform directory is way too short. |
| 1085 | */ |
| 1086 | if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) { |
| 1087 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1088 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | } |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1090 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1091 | oldsfp = (xfs_dir2_sf_hdr_t *)ifp->if_u1.if_data; |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1092 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1093 | ASSERT(ifp->if_bytes == dp->i_d.di_size); |
| 1094 | ASSERT(ifp->if_u1.if_data != NULL); |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1095 | ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count)); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1096 | ASSERT(dp->i_d.di_nextents == 0); |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | /* |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1099 | * Copy the directory into a temporary buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | * Then pitch the incore inode data so we can make extents. |
| 1101 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1102 | sfp = kmem_alloc(ifp->if_bytes, KM_SLEEP); |
| 1103 | memcpy(sfp, oldsfp, ifp->if_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1105 | xfs_idata_realloc(dp, -ifp->if_bytes, XFS_DATA_FORK); |
| 1106 | xfs_bmap_local_to_extents_empty(dp, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | dp->i_d.di_size = 0; |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | /* |
| 1110 | * Add block 0 to the inode. |
| 1111 | */ |
| 1112 | error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno); |
| 1113 | if (error) { |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1114 | kmem_free(sfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | return error; |
| 1116 | } |
| 1117 | /* |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1118 | * Initialize the data block, then convert it to block format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1120 | error = xfs_dir3_data_init(args, blkno, &bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | if (error) { |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1122 | kmem_free(sfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | return error; |
| 1124 | } |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 1125 | xfs_dir3_block_init(mp, tp, bp, dp); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1126 | hdr = bp->b_addr; |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | /* |
| 1129 | * Compute size of block "tail" area. |
| 1130 | */ |
| 1131 | i = (uint)sizeof(*btp) + |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1132 | (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | /* |
| 1134 | * The whole thing is initialized to free by the init routine. |
| 1135 | * Say we're using the leaf and tail area. |
| 1136 | */ |
Dave Chinner | 2ca9877 | 2013-10-29 22:11:49 +1100 | [diff] [blame] | 1137 | dup = dp->d_ops->data_unused_p(hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | needlog = needscan = 0; |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1139 | xfs_dir2_data_use_free(args, bp, dup, args->geo->blksize - i, |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1140 | i, &needlog, &needscan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | ASSERT(needscan == 0); |
| 1142 | /* |
| 1143 | * Fill in the tail. |
| 1144 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1145 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1146 | btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | btp->stale = 0; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1148 | blp = xfs_dir2_block_leaf_p(btp); |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1149 | endoffset = (uint)((char *)blp - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | /* |
| 1151 | * Remove the freespace, we'll manage it. |
| 1152 | */ |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1153 | xfs_dir2_data_use_free(args, bp, dup, |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1154 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1155 | be16_to_cpu(dup->length), &needlog, &needscan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | /* |
| 1157 | * Create entry for . |
| 1158 | */ |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 1159 | dep = dp->d_ops->data_dot_entry_p(hdr); |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 1160 | dep->inumber = cpu_to_be64(dp->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | dep->namelen = 1; |
| 1162 | dep->name[0] = '.'; |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 1163 | dp->d_ops->data_put_ftype(dep, XFS_DIR3_FT_DIR); |
| 1164 | tagp = dp->d_ops->data_entry_tag_p(dep); |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1165 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1166 | xfs_dir2_data_log_entry(args, bp, dep); |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1167 | blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot); |
Eric Sandeen | 2599405 | 2014-04-14 19:02:30 +1000 | [diff] [blame] | 1168 | blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr( |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1169 | (char *)dep - (char *)hdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | /* |
| 1171 | * Create entry for .. |
| 1172 | */ |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 1173 | dep = dp->d_ops->data_dotdot_entry_p(hdr); |
Dave Chinner | 4740175 | 2013-10-29 22:11:47 +1100 | [diff] [blame] | 1174 | dep->inumber = cpu_to_be64(dp->d_ops->sf_get_parent_ino(sfp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | dep->namelen = 2; |
| 1176 | dep->name[0] = dep->name[1] = '.'; |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 1177 | dp->d_ops->data_put_ftype(dep, XFS_DIR3_FT_DIR); |
| 1178 | tagp = dp->d_ops->data_entry_tag_p(dep); |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1179 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1180 | xfs_dir2_data_log_entry(args, bp, dep); |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1181 | blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot); |
Eric Sandeen | 2599405 | 2014-04-14 19:02:30 +1000 | [diff] [blame] | 1182 | blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr( |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1183 | (char *)dep - (char *)hdr)); |
Dave Chinner | 1c9a5b2 | 2013-10-30 09:15:02 +1100 | [diff] [blame] | 1184 | offset = dp->d_ops->data_first_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | /* |
| 1186 | * Loop over existing entries, stuff them in. |
| 1187 | */ |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1188 | i = 0; |
| 1189 | if (!sfp->count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | sfep = NULL; |
| 1191 | else |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1192 | sfep = xfs_dir2_sf_firstentry(sfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | /* |
| 1194 | * Need to preserve the existing offset values in the sf directory. |
| 1195 | * Insert holes (unused entries) where necessary. |
| 1196 | */ |
| 1197 | while (offset < endoffset) { |
| 1198 | /* |
| 1199 | * sfep is null when we reach the end of the list. |
| 1200 | */ |
| 1201 | if (sfep == NULL) |
| 1202 | newoffset = endoffset; |
| 1203 | else |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1204 | newoffset = xfs_dir2_sf_get_offset(sfep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | /* |
| 1206 | * There should be a hole here, make one. |
| 1207 | */ |
| 1208 | if (offset < newoffset) { |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1209 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1210 | dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 1211 | dup->length = cpu_to_be16(newoffset - offset); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1212 | *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16( |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1213 | ((char *)dup - (char *)hdr)); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1214 | xfs_dir2_data_log_unused(args, bp, dup); |
Dave Chinner | 2ca9877 | 2013-10-29 22:11:49 +1100 | [diff] [blame] | 1215 | xfs_dir2_data_freeinsert(hdr, |
| 1216 | dp->d_ops->data_bestfree_p(hdr), |
| 1217 | dup, &dummy); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1218 | offset += be16_to_cpu(dup->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | continue; |
| 1220 | } |
| 1221 | /* |
| 1222 | * Copy a real entry. |
| 1223 | */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1224 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset); |
Dave Chinner | 4740175 | 2013-10-29 22:11:47 +1100 | [diff] [blame] | 1225 | dep->inumber = cpu_to_be64(dp->d_ops->sf_get_ino(sfp, sfep)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | dep->namelen = sfep->namelen; |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 1227 | dp->d_ops->data_put_ftype(dep, dp->d_ops->sf_get_ftype(sfep)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | memcpy(dep->name, sfep->name, dep->namelen); |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 1229 | tagp = dp->d_ops->data_entry_tag_p(dep); |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1230 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1231 | xfs_dir2_data_log_entry(args, bp, dep); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1232 | name.name = sfep->name; |
| 1233 | name.len = sfep->namelen; |
| 1234 | blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops-> |
| 1235 | hashname(&name)); |
Eric Sandeen | 2599405 | 2014-04-14 19:02:30 +1000 | [diff] [blame] | 1236 | blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr( |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1237 | (char *)dep - (char *)hdr)); |
| 1238 | offset = (int)((char *)(tagp + 1) - (char *)hdr); |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1239 | if (++i == sfp->count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | sfep = NULL; |
| 1241 | else |
Dave Chinner | 32c5483 | 2013-10-29 22:11:46 +1100 | [diff] [blame] | 1242 | sfep = dp->d_ops->sf_nextentry(sfp, sfep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | } |
| 1244 | /* Done with the temporary buffer */ |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1245 | kmem_free(sfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | /* |
| 1247 | * Sort the leaf entries by hash value. |
| 1248 | */ |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 1249 | xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | /* |
| 1251 | * Log the leaf entry area and tail. |
| 1252 | * Already logged the header in data_init, ignore needlog. |
| 1253 | */ |
| 1254 | ASSERT(needscan == 0); |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 1255 | xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | xfs_dir2_block_log_tail(tp, bp); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1257 | xfs_dir3_data_check(dp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | return 0; |
| 1259 | } |