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-2005 Silicon Graphics, Inc. |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [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 | 632b89e | 2013-10-29 22:11:58 +1100 | [diff] [blame] | 21 | #include "xfs_shared.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 22 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 23 | #include "xfs_log_format.h" |
| 24 | #include "xfs_trans_resv.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 25 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_sb.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_mount.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 28 | #include "xfs_da_format.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_inode.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 31 | #include "xfs_trans.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_inode_item.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 33 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_bmap.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 35 | #include "xfs_attr_sf.h" |
| 36 | #include "xfs_attr_remote.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "xfs_attr.h" |
| 38 | #include "xfs_attr_leaf.h" |
| 39 | #include "xfs_error.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 40 | #include "xfs_trace.h" |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 41 | #include "xfs_buf_item.h" |
| 42 | #include "xfs_cksum.h" |
Dave Chinner | 4bceb18 | 2013-10-29 22:11:51 +1100 | [diff] [blame] | 43 | #include "xfs_dir2.h" |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 44 | #include "xfs_log.h" |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * xfs_attr_leaf.c |
| 49 | * |
| 50 | * Routines to implement leaf blocks of attributes as Btrees of hashed names. |
| 51 | */ |
| 52 | |
| 53 | /*======================================================================== |
| 54 | * Function prototypes for the kernel. |
| 55 | *========================================================================*/ |
| 56 | |
| 57 | /* |
| 58 | * Routines used for growing the Btree. |
| 59 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 60 | STATIC int xfs_attr3_leaf_create(struct xfs_da_args *args, |
| 61 | xfs_dablk_t which_block, struct xfs_buf **bpp); |
| 62 | STATIC int xfs_attr3_leaf_add_work(struct xfs_buf *leaf_buffer, |
| 63 | struct xfs_attr3_icleaf_hdr *ichdr, |
| 64 | struct xfs_da_args *args, int freemap_index); |
| 65 | STATIC void xfs_attr3_leaf_compact(struct xfs_da_args *args, |
| 66 | struct xfs_attr3_icleaf_hdr *ichdr, |
| 67 | struct xfs_buf *leaf_buffer); |
| 68 | STATIC void xfs_attr3_leaf_rebalance(xfs_da_state_t *state, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | xfs_da_state_blk_t *blk1, |
| 70 | xfs_da_state_blk_t *blk2); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 71 | STATIC int xfs_attr3_leaf_figure_balance(xfs_da_state_t *state, |
| 72 | xfs_da_state_blk_t *leaf_blk_1, |
| 73 | struct xfs_attr3_icleaf_hdr *ichdr1, |
| 74 | xfs_da_state_blk_t *leaf_blk_2, |
| 75 | struct xfs_attr3_icleaf_hdr *ichdr2, |
| 76 | int *number_entries_in_blk1, |
| 77 | int *number_usedbytes_in_blk1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * Utility routines. |
| 81 | */ |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 82 | STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args, |
| 83 | struct xfs_attr_leafblock *src_leaf, |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 84 | struct xfs_attr3_icleaf_hdr *src_ichdr, int src_start, |
| 85 | struct xfs_attr_leafblock *dst_leaf, |
| 86 | struct xfs_attr3_icleaf_hdr *dst_ichdr, int dst_start, |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 87 | int move_count); |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 88 | STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index); |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 89 | |
Brian Foster | e87021a | 2015-04-13 11:27:10 +1000 | [diff] [blame] | 90 | /* |
| 91 | * attr3 block 'firstused' conversion helpers. |
| 92 | * |
| 93 | * firstused refers to the offset of the first used byte of the nameval region |
| 94 | * of an attr leaf block. The region starts at the tail of the block and expands |
| 95 | * backwards towards the middle. As such, firstused is initialized to the block |
| 96 | * size for an empty leaf block and is reduced from there. |
| 97 | * |
| 98 | * The attr3 block size is pegged to the fsb size and the maximum fsb is 64k. |
| 99 | * The in-core firstused field is 32-bit and thus supports the maximum fsb size. |
| 100 | * The on-disk field is only 16-bit, however, and overflows at 64k. Since this |
| 101 | * only occurs at exactly 64k, we use zero as a magic on-disk value to represent |
| 102 | * the attr block size. The following helpers manage the conversion between the |
| 103 | * in-core and on-disk formats. |
| 104 | */ |
| 105 | |
| 106 | static void |
| 107 | xfs_attr3_leaf_firstused_from_disk( |
| 108 | struct xfs_da_geometry *geo, |
| 109 | struct xfs_attr3_icleaf_hdr *to, |
| 110 | struct xfs_attr_leafblock *from) |
| 111 | { |
| 112 | struct xfs_attr3_leaf_hdr *hdr3; |
| 113 | |
| 114 | if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) { |
| 115 | hdr3 = (struct xfs_attr3_leaf_hdr *) from; |
| 116 | to->firstused = be16_to_cpu(hdr3->firstused); |
| 117 | } else { |
| 118 | to->firstused = be16_to_cpu(from->hdr.firstused); |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Convert from the magic fsb size value to actual blocksize. This |
| 123 | * should only occur for empty blocks when the block size overflows |
| 124 | * 16-bits. |
| 125 | */ |
| 126 | if (to->firstused == XFS_ATTR3_LEAF_NULLOFF) { |
| 127 | ASSERT(!to->count && !to->usedbytes); |
| 128 | ASSERT(geo->blksize > USHRT_MAX); |
| 129 | to->firstused = geo->blksize; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static void |
| 134 | xfs_attr3_leaf_firstused_to_disk( |
| 135 | struct xfs_da_geometry *geo, |
| 136 | struct xfs_attr_leafblock *to, |
| 137 | struct xfs_attr3_icleaf_hdr *from) |
| 138 | { |
| 139 | struct xfs_attr3_leaf_hdr *hdr3; |
| 140 | uint32_t firstused; |
| 141 | |
| 142 | /* magic value should only be seen on disk */ |
| 143 | ASSERT(from->firstused != XFS_ATTR3_LEAF_NULLOFF); |
| 144 | |
| 145 | /* |
| 146 | * Scale down the 32-bit in-core firstused value to the 16-bit on-disk |
| 147 | * value. This only overflows at the max supported value of 64k. Use the |
| 148 | * magic on-disk value to represent block size in this case. |
| 149 | */ |
| 150 | firstused = from->firstused; |
| 151 | if (firstused > USHRT_MAX) { |
| 152 | ASSERT(from->firstused == geo->blksize); |
| 153 | firstused = XFS_ATTR3_LEAF_NULLOFF; |
| 154 | } |
| 155 | |
| 156 | if (from->magic == XFS_ATTR3_LEAF_MAGIC) { |
| 157 | hdr3 = (struct xfs_attr3_leaf_hdr *) to; |
| 158 | hdr3->firstused = cpu_to_be16(firstused); |
| 159 | } else { |
| 160 | to->hdr.firstused = cpu_to_be16(firstused); |
| 161 | } |
| 162 | } |
| 163 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 164 | void |
| 165 | xfs_attr3_leaf_hdr_from_disk( |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 166 | struct xfs_da_geometry *geo, |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 167 | struct xfs_attr3_icleaf_hdr *to, |
| 168 | struct xfs_attr_leafblock *from) |
| 169 | { |
| 170 | int i; |
| 171 | |
| 172 | ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) || |
| 173 | from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)); |
| 174 | |
| 175 | if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) { |
| 176 | struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)from; |
| 177 | |
| 178 | to->forw = be32_to_cpu(hdr3->info.hdr.forw); |
| 179 | to->back = be32_to_cpu(hdr3->info.hdr.back); |
| 180 | to->magic = be16_to_cpu(hdr3->info.hdr.magic); |
| 181 | to->count = be16_to_cpu(hdr3->count); |
| 182 | to->usedbytes = be16_to_cpu(hdr3->usedbytes); |
Brian Foster | e87021a | 2015-04-13 11:27:10 +1000 | [diff] [blame] | 183 | xfs_attr3_leaf_firstused_from_disk(geo, to, from); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 184 | to->holes = hdr3->holes; |
| 185 | |
| 186 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { |
| 187 | to->freemap[i].base = be16_to_cpu(hdr3->freemap[i].base); |
| 188 | to->freemap[i].size = be16_to_cpu(hdr3->freemap[i].size); |
| 189 | } |
| 190 | return; |
| 191 | } |
| 192 | to->forw = be32_to_cpu(from->hdr.info.forw); |
| 193 | to->back = be32_to_cpu(from->hdr.info.back); |
| 194 | to->magic = be16_to_cpu(from->hdr.info.magic); |
| 195 | to->count = be16_to_cpu(from->hdr.count); |
| 196 | to->usedbytes = be16_to_cpu(from->hdr.usedbytes); |
Brian Foster | e87021a | 2015-04-13 11:27:10 +1000 | [diff] [blame] | 197 | xfs_attr3_leaf_firstused_from_disk(geo, to, from); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 198 | to->holes = from->hdr.holes; |
| 199 | |
| 200 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { |
| 201 | to->freemap[i].base = be16_to_cpu(from->hdr.freemap[i].base); |
| 202 | to->freemap[i].size = be16_to_cpu(from->hdr.freemap[i].size); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void |
| 207 | xfs_attr3_leaf_hdr_to_disk( |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 208 | struct xfs_da_geometry *geo, |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 209 | struct xfs_attr_leafblock *to, |
| 210 | struct xfs_attr3_icleaf_hdr *from) |
| 211 | { |
Brian Foster | e87021a | 2015-04-13 11:27:10 +1000 | [diff] [blame] | 212 | int i; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 213 | |
| 214 | ASSERT(from->magic == XFS_ATTR_LEAF_MAGIC || |
| 215 | from->magic == XFS_ATTR3_LEAF_MAGIC); |
| 216 | |
| 217 | if (from->magic == XFS_ATTR3_LEAF_MAGIC) { |
| 218 | struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)to; |
| 219 | |
| 220 | hdr3->info.hdr.forw = cpu_to_be32(from->forw); |
| 221 | hdr3->info.hdr.back = cpu_to_be32(from->back); |
| 222 | hdr3->info.hdr.magic = cpu_to_be16(from->magic); |
| 223 | hdr3->count = cpu_to_be16(from->count); |
| 224 | hdr3->usedbytes = cpu_to_be16(from->usedbytes); |
Brian Foster | e87021a | 2015-04-13 11:27:10 +1000 | [diff] [blame] | 225 | xfs_attr3_leaf_firstused_to_disk(geo, to, from); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 226 | hdr3->holes = from->holes; |
| 227 | hdr3->pad1 = 0; |
| 228 | |
| 229 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { |
| 230 | hdr3->freemap[i].base = cpu_to_be16(from->freemap[i].base); |
| 231 | hdr3->freemap[i].size = cpu_to_be16(from->freemap[i].size); |
| 232 | } |
| 233 | return; |
| 234 | } |
| 235 | to->hdr.info.forw = cpu_to_be32(from->forw); |
| 236 | to->hdr.info.back = cpu_to_be32(from->back); |
| 237 | to->hdr.info.magic = cpu_to_be16(from->magic); |
| 238 | to->hdr.count = cpu_to_be16(from->count); |
| 239 | to->hdr.usedbytes = cpu_to_be16(from->usedbytes); |
Brian Foster | e87021a | 2015-04-13 11:27:10 +1000 | [diff] [blame] | 240 | xfs_attr3_leaf_firstused_to_disk(geo, to, from); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 241 | to->hdr.holes = from->holes; |
| 242 | to->hdr.pad1 = 0; |
| 243 | |
| 244 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { |
| 245 | to->hdr.freemap[i].base = cpu_to_be16(from->freemap[i].base); |
| 246 | to->hdr.freemap[i].size = cpu_to_be16(from->freemap[i].size); |
| 247 | } |
| 248 | } |
| 249 | |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 250 | static xfs_failaddr_t |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 251 | xfs_attr3_leaf_verify( |
Darrick J. Wong | 79a69bf | 2018-01-16 18:54:12 -0800 | [diff] [blame] | 252 | struct xfs_buf *bp) |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 253 | { |
Darrick J. Wong | 79a69bf | 2018-01-16 18:54:12 -0800 | [diff] [blame] | 254 | struct xfs_attr3_icleaf_hdr ichdr; |
| 255 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 256 | struct xfs_attr_leafblock *leaf = bp->b_addr; |
| 257 | struct xfs_perag *pag = bp->b_pag; |
| 258 | struct xfs_attr_leaf_entry *entries; |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 259 | |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 260 | xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 261 | |
| 262 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 263 | struct xfs_da3_node_hdr *hdr3 = bp->b_addr; |
| 264 | |
| 265 | if (ichdr.magic != XFS_ATTR3_LEAF_MAGIC) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 266 | return __this_address; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 267 | |
Eric Sandeen | ce748ea | 2015-07-29 11:53:31 +1000 | [diff] [blame] | 268 | if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid)) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 269 | return __this_address; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 270 | if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 271 | return __this_address; |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 272 | if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->info.lsn))) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 273 | return __this_address; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 274 | } else { |
| 275 | if (ichdr.magic != XFS_ATTR_LEAF_MAGIC) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 276 | return __this_address; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 277 | } |
Eric Sandeen | 2e1d233 | 2016-12-09 16:49:47 +1100 | [diff] [blame] | 278 | /* |
| 279 | * In recovery there is a transient state where count == 0 is valid |
| 280 | * because we may have transitioned an empty shortform attr to a leaf |
| 281 | * if the attr didn't fit in shortform. |
| 282 | */ |
| 283 | if (pag && pag->pagf_init && ichdr.count == 0) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 284 | return __this_address; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 285 | |
Darrick J. Wong | 79a69bf | 2018-01-16 18:54:12 -0800 | [diff] [blame] | 286 | /* |
| 287 | * firstused is the block offset of the first name info structure. |
| 288 | * Make sure it doesn't go off the block or crash into the header. |
| 289 | */ |
| 290 | if (ichdr.firstused > mp->m_attr_geo->blksize) |
| 291 | return __this_address; |
| 292 | if (ichdr.firstused < xfs_attr3_leaf_hdr_size(leaf)) |
| 293 | return __this_address; |
| 294 | |
| 295 | /* Make sure the entries array doesn't crash into the name info. */ |
| 296 | entries = xfs_attr3_leaf_entryp(bp->b_addr); |
| 297 | if ((char *)&entries[ichdr.count] > |
| 298 | (char *)bp->b_addr + ichdr.firstused) |
| 299 | return __this_address; |
| 300 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 301 | /* XXX: need to range check rest of attr header values */ |
| 302 | /* XXX: hash order check? */ |
| 303 | |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 304 | return NULL; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static void |
| 308 | xfs_attr3_leaf_write_verify( |
| 309 | struct xfs_buf *bp) |
| 310 | { |
| 311 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Carlos Maiolino | fb1755a | 2018-01-24 13:38:48 -0800 | [diff] [blame^] | 312 | struct xfs_buf_log_item *bip = bp->b_log_item; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 313 | struct xfs_attr3_leaf_hdr *hdr3 = bp->b_addr; |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 314 | xfs_failaddr_t fa; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 315 | |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 316 | fa = xfs_attr3_leaf_verify(bp); |
| 317 | if (fa) { |
| 318 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 319 | return; |
| 320 | } |
| 321 | |
| 322 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 323 | return; |
| 324 | |
| 325 | if (bip) |
| 326 | hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn); |
| 327 | |
Eric Sandeen | f1dbcd7 | 2014-02-27 15:18:23 +1100 | [diff] [blame] | 328 | xfs_buf_update_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* |
| 332 | * leaf/node format detection on trees is sketchy, so a node read can be done on |
| 333 | * leaf level blocks when detection identifies the tree as a node format tree |
| 334 | * incorrectly. In this case, we need to swap the verifier to match the correct |
| 335 | * format of the block being read. |
| 336 | */ |
| 337 | static void |
| 338 | xfs_attr3_leaf_read_verify( |
| 339 | struct xfs_buf *bp) |
| 340 | { |
| 341 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 342 | xfs_failaddr_t fa; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 343 | |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 344 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
| 345 | !xfs_buf_verify_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF)) |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 346 | xfs_verifier_error(bp, -EFSBADCRC, __this_address); |
| 347 | else { |
| 348 | fa = xfs_attr3_leaf_verify(bp); |
| 349 | if (fa) |
| 350 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
| 351 | } |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 352 | } |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 353 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 354 | const struct xfs_buf_ops xfs_attr3_leaf_buf_ops = { |
Eric Sandeen | 233135b | 2016-01-04 16:10:19 +1100 | [diff] [blame] | 355 | .name = "xfs_attr3_leaf", |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 356 | .verify_read = xfs_attr3_leaf_read_verify, |
| 357 | .verify_write = xfs_attr3_leaf_write_verify, |
Darrick J. Wong | b557259 | 2018-01-08 10:51:08 -0800 | [diff] [blame] | 358 | .verify_struct = xfs_attr3_leaf_verify, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 359 | }; |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 360 | |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 361 | int |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 362 | xfs_attr3_leaf_read( |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 363 | struct xfs_trans *tp, |
| 364 | struct xfs_inode *dp, |
| 365 | xfs_dablk_t bno, |
| 366 | xfs_daddr_t mappedbno, |
| 367 | struct xfs_buf **bpp) |
| 368 | { |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 369 | int err; |
| 370 | |
| 371 | err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp, |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 372 | XFS_ATTR_FORK, &xfs_attr3_leaf_buf_ops); |
Darrick J. Wong | cd87d86 | 2017-07-07 18:55:17 -0700 | [diff] [blame] | 373 | if (!err && tp && *bpp) |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 374 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_ATTR_LEAF_BUF); |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 375 | return err; |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 376 | } |
| 377 | |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 378 | /*======================================================================== |
| 379 | * Namespace helper routines |
| 380 | *========================================================================*/ |
| 381 | |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 382 | /* |
| 383 | * If namespace bits don't match return 0. |
| 384 | * If all match then return 1. |
| 385 | */ |
Christoph Hellwig | b8f82a4 | 2009-11-14 16:17:22 +0000 | [diff] [blame] | 386 | STATIC int |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 387 | xfs_attr_namesp_match(int arg_flags, int ondisk_flags) |
| 388 | { |
| 389 | return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags); |
| 390 | } |
| 391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
| 393 | /*======================================================================== |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 394 | * External routines when attribute fork size < XFS_LITINO(mp). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | *========================================================================*/ |
| 396 | |
| 397 | /* |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 398 | * Query whether the requested number of additional bytes of extended |
| 399 | * attribute space will be able to fit inline. |
Christoph Hellwig | 4c393a6 | 2011-11-19 17:44:30 +0000 | [diff] [blame] | 400 | * |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 401 | * Returns zero if not, else the di_forkoff fork offset to be used in the |
| 402 | * literal area for attribute data once the new bytes have been added. |
| 403 | * |
| 404 | * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value; |
| 405 | * special case for dev/uuid inodes, they have fixed size data forks. |
| 406 | */ |
| 407 | int |
| 408 | xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes) |
| 409 | { |
| 410 | int offset; |
| 411 | int minforkoff; /* lower limit on valid forkoff locations */ |
| 412 | int maxforkoff; /* upper limit on valid forkoff locations */ |
Christoph Hellwig | 4c393a6 | 2011-11-19 17:44:30 +0000 | [diff] [blame] | 413 | int dsize; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 414 | xfs_mount_t *mp = dp->i_mount; |
| 415 | |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 416 | /* rounded down */ |
| 417 | offset = (XFS_LITINO(mp, dp->i_d.di_version) - bytes) >> 3; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 418 | |
Christoph Hellwig | 42b67dc | 2017-10-19 11:07:09 -0700 | [diff] [blame] | 419 | if (dp->i_d.di_format == XFS_DINODE_FMT_DEV) { |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 420 | minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 421 | return (offset >= minforkoff) ? minforkoff : 0; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 422 | } |
| 423 | |
Christoph Hellwig | 4c393a6 | 2011-11-19 17:44:30 +0000 | [diff] [blame] | 424 | /* |
| 425 | * If the requested numbers of bytes is smaller or equal to the |
| 426 | * current attribute fork size we can always proceed. |
| 427 | * |
| 428 | * Note that if_bytes in the data fork might actually be larger than |
| 429 | * the current data fork size is due to delalloc extents. In that |
| 430 | * case either the extent count will go down when they are converted |
| 431 | * to real extents, or the delalloc conversion will take care of the |
| 432 | * literal area rebalancing. |
| 433 | */ |
| 434 | if (bytes <= XFS_IFORK_ASIZE(dp)) |
| 435 | return dp->i_d.di_forkoff; |
| 436 | |
| 437 | /* |
| 438 | * For attr2 we can try to move the forkoff if there is space in the |
| 439 | * literal area, but for the old format we are done if there is no |
| 440 | * space in the fixed attribute fork. |
| 441 | */ |
| 442 | if (!(mp->m_flags & XFS_MOUNT_ATTR2)) |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 443 | return 0; |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 444 | |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 445 | dsize = dp->i_df.if_bytes; |
Christoph Hellwig | 4c393a6 | 2011-11-19 17:44:30 +0000 | [diff] [blame] | 446 | |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 447 | switch (dp->i_d.di_format) { |
| 448 | case XFS_DINODE_FMT_EXTENTS: |
Christoph Hellwig | 4c393a6 | 2011-11-19 17:44:30 +0000 | [diff] [blame] | 449 | /* |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 450 | * If there is no attr fork and the data fork is extents, |
Christoph Hellwig | 4c393a6 | 2011-11-19 17:44:30 +0000 | [diff] [blame] | 451 | * determine if creating the default attr fork will result |
| 452 | * in the extents form migrating to btree. If so, the |
| 453 | * minimum offset only needs to be the space required for |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 454 | * the btree root. |
Christoph Hellwig | 4c393a6 | 2011-11-19 17:44:30 +0000 | [diff] [blame] | 455 | */ |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 456 | if (!dp->i_d.di_forkoff && dp->i_df.if_bytes > |
| 457 | xfs_default_attroffset(dp)) |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 458 | dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
| 459 | break; |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 460 | case XFS_DINODE_FMT_BTREE: |
| 461 | /* |
Christoph Hellwig | 4c393a6 | 2011-11-19 17:44:30 +0000 | [diff] [blame] | 462 | * If we have a data btree then keep forkoff if we have one, |
| 463 | * otherwise we are adding a new attr, so then we set |
| 464 | * minforkoff to where the btree root can finish so we have |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 465 | * plenty of room for attrs |
| 466 | */ |
| 467 | if (dp->i_d.di_forkoff) { |
Christoph Hellwig | 4c393a6 | 2011-11-19 17:44:30 +0000 | [diff] [blame] | 468 | if (offset < dp->i_d.di_forkoff) |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 469 | return 0; |
Christoph Hellwig | 4c393a6 | 2011-11-19 17:44:30 +0000 | [diff] [blame] | 470 | return dp->i_d.di_forkoff; |
| 471 | } |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 472 | dsize = XFS_BMAP_BROOT_SPACE(mp, dp->i_df.if_broot); |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 473 | break; |
| 474 | } |
Christoph Hellwig | 4c393a6 | 2011-11-19 17:44:30 +0000 | [diff] [blame] | 475 | |
| 476 | /* |
| 477 | * A data fork btree root must have space for at least |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 478 | * MINDBTPTRS key/ptr pairs if the data fork is small or empty. |
| 479 | */ |
| 480 | minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS)); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 481 | minforkoff = roundup(minforkoff, 8) >> 3; |
| 482 | |
| 483 | /* attr fork btree root can have at least this many key/ptr pairs */ |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 484 | maxforkoff = XFS_LITINO(mp, dp->i_d.di_version) - |
| 485 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 486 | maxforkoff = maxforkoff >> 3; /* rounded down */ |
| 487 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 488 | if (offset >= maxforkoff) |
| 489 | return maxforkoff; |
Christoph Hellwig | 4c393a6 | 2011-11-19 17:44:30 +0000 | [diff] [blame] | 490 | if (offset >= minforkoff) |
| 491 | return offset; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Switch on the ATTR2 superblock bit (implies also FEATURES2) |
| 497 | */ |
| 498 | STATIC void |
| 499 | xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp) |
| 500 | { |
Nathan Scott | 13059ff | 2006-01-11 15:32:01 +1100 | [diff] [blame] | 501 | if ((mp->m_flags & XFS_MOUNT_ATTR2) && |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 502 | !(xfs_sb_version_hasattr2(&mp->m_sb))) { |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 503 | spin_lock(&mp->m_sb_lock); |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 504 | if (!xfs_sb_version_hasattr2(&mp->m_sb)) { |
| 505 | xfs_sb_version_addattr2(&mp->m_sb); |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 506 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 507 | xfs_log_sb(tp); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 508 | } else |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 509 | spin_unlock(&mp->m_sb_lock); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
| 513 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | * Create the initial contents of a shortform attribute list. |
| 515 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 516 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | xfs_attr_shortform_create(xfs_da_args_t *args) |
| 518 | { |
| 519 | xfs_attr_sf_hdr_t *hdr; |
| 520 | xfs_inode_t *dp; |
| 521 | xfs_ifork_t *ifp; |
| 522 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 523 | trace_xfs_attr_sf_create(args); |
| 524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | dp = args->dp; |
| 526 | ASSERT(dp != NULL); |
| 527 | ifp = dp->i_afp; |
| 528 | ASSERT(ifp != NULL); |
| 529 | ASSERT(ifp->if_bytes == 0); |
| 530 | if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) { |
| 531 | ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */ |
| 532 | dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL; |
| 533 | ifp->if_flags |= XFS_IFINLINE; |
| 534 | } else { |
| 535 | ASSERT(ifp->if_flags & XFS_IFINLINE); |
| 536 | } |
| 537 | xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK); |
| 538 | hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data; |
| 539 | hdr->count = 0; |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 540 | hdr->totsize = cpu_to_be16(sizeof(*hdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /* |
| 545 | * Add a name/value pair to the shortform attribute list. |
| 546 | * Overflow from the inode has already been checked for. |
| 547 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 548 | void |
| 549 | xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | { |
| 551 | xfs_attr_shortform_t *sf; |
| 552 | xfs_attr_sf_entry_t *sfe; |
| 553 | int i, offset, size; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 554 | xfs_mount_t *mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | xfs_inode_t *dp; |
| 556 | xfs_ifork_t *ifp; |
| 557 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 558 | trace_xfs_attr_sf_add(args); |
| 559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | dp = args->dp; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 561 | mp = dp->i_mount; |
| 562 | dp->i_d.di_forkoff = forkoff; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | ifp = dp->i_afp; |
| 565 | ASSERT(ifp->if_flags & XFS_IFINLINE); |
| 566 | sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data; |
| 567 | sfe = &sf->list[0]; |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 568 | for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) { |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 569 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | if (sfe->namelen != args->namelen) |
| 571 | continue; |
| 572 | if (memcmp(args->name, sfe->nameval, args->namelen) != 0) |
| 573 | continue; |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 574 | if (!xfs_attr_namesp_match(args->flags, sfe->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | continue; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 576 | ASSERT(0); |
| 577 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | offset = (char *)sfe - (char *)sf; |
| 581 | size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen); |
| 582 | xfs_idata_realloc(dp, size, XFS_ATTR_FORK); |
| 583 | sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data; |
| 584 | sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset); |
| 585 | |
| 586 | sfe->namelen = args->namelen; |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 587 | sfe->valuelen = args->valuelen; |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 588 | sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | memcpy(sfe->nameval, args->name, args->namelen); |
| 590 | memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen); |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 591 | sf->hdr.count++; |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 592 | be16_add_cpu(&sf->hdr.totsize, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA); |
| 594 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 595 | xfs_sbversion_add_attr2(mp, args->trans); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /* |
Christoph Hellwig | e1486de | 2009-02-04 09:36:00 +0100 | [diff] [blame] | 599 | * After the last attribute is removed revert to original inode format, |
| 600 | * making all literal area available to the data fork once more. |
| 601 | */ |
Dave Chinner | 6dfe5a0 | 2015-05-29 07:40:08 +1000 | [diff] [blame] | 602 | void |
| 603 | xfs_attr_fork_remove( |
Christoph Hellwig | e1486de | 2009-02-04 09:36:00 +0100 | [diff] [blame] | 604 | struct xfs_inode *ip, |
| 605 | struct xfs_trans *tp) |
| 606 | { |
| 607 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); |
| 608 | ip->i_d.di_forkoff = 0; |
| 609 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 610 | |
| 611 | ASSERT(ip->i_d.di_anextents == 0); |
| 612 | ASSERT(ip->i_afp == NULL); |
| 613 | |
Christoph Hellwig | e1486de | 2009-02-04 09:36:00 +0100 | [diff] [blame] | 614 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 615 | } |
| 616 | |
| 617 | /* |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 618 | * Remove an attribute from the shortform attribute list structure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | */ |
| 620 | int |
| 621 | xfs_attr_shortform_remove(xfs_da_args_t *args) |
| 622 | { |
| 623 | xfs_attr_shortform_t *sf; |
| 624 | xfs_attr_sf_entry_t *sfe; |
| 625 | int base, size=0, end, totsize, i; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 626 | xfs_mount_t *mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | xfs_inode_t *dp; |
| 628 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 629 | trace_xfs_attr_sf_remove(args); |
| 630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | dp = args->dp; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 632 | mp = dp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | base = sizeof(xfs_attr_sf_hdr_t); |
| 634 | sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data; |
| 635 | sfe = &sf->list[0]; |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 636 | end = sf->hdr.count; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 637 | for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | base += size, i++) { |
| 639 | size = XFS_ATTR_SF_ENTSIZE(sfe); |
| 640 | if (sfe->namelen != args->namelen) |
| 641 | continue; |
| 642 | if (memcmp(sfe->nameval, args->name, args->namelen) != 0) |
| 643 | continue; |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 644 | if (!xfs_attr_namesp_match(args->flags, sfe->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | continue; |
| 646 | break; |
| 647 | } |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 648 | if (i == end) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 649 | return -ENOATTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 651 | /* |
| 652 | * Fix up the attribute fork data, covering the hole |
| 653 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | end = base + size; |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 655 | totsize = be16_to_cpu(sf->hdr.totsize); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 656 | if (end != totsize) |
| 657 | memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end); |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 658 | sf->hdr.count--; |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 659 | be16_add_cpu(&sf->hdr.totsize, -size); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 660 | |
| 661 | /* |
| 662 | * Fix up the start offset of the attribute fork |
| 663 | */ |
| 664 | totsize -= size; |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 665 | if (totsize == sizeof(xfs_attr_sf_hdr_t) && |
Christoph Hellwig | e1486de | 2009-02-04 09:36:00 +0100 | [diff] [blame] | 666 | (mp->m_flags & XFS_MOUNT_ATTR2) && |
| 667 | (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) && |
| 668 | !(args->op_flags & XFS_DA_OP_ADDNAME)) { |
Dave Chinner | 6dfe5a0 | 2015-05-29 07:40:08 +1000 | [diff] [blame] | 669 | xfs_attr_fork_remove(dp, args->trans); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 670 | } else { |
| 671 | xfs_idata_realloc(dp, -size, XFS_ATTR_FORK); |
| 672 | dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize); |
| 673 | ASSERT(dp->i_d.di_forkoff); |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 674 | ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) || |
| 675 | (args->op_flags & XFS_DA_OP_ADDNAME) || |
| 676 | !(mp->m_flags & XFS_MOUNT_ATTR2) || |
| 677 | dp->i_d.di_format == XFS_DINODE_FMT_BTREE); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 678 | xfs_trans_log_inode(args->trans, dp, |
| 679 | XFS_ILOG_CORE | XFS_ILOG_ADATA); |
| 680 | } |
| 681 | |
| 682 | xfs_sbversion_add_attr2(mp, args->trans); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 684 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /* |
| 688 | * Look up a name in a shortform attribute list structure. |
| 689 | */ |
| 690 | /*ARGSUSED*/ |
| 691 | int |
| 692 | xfs_attr_shortform_lookup(xfs_da_args_t *args) |
| 693 | { |
| 694 | xfs_attr_shortform_t *sf; |
| 695 | xfs_attr_sf_entry_t *sfe; |
| 696 | int i; |
| 697 | xfs_ifork_t *ifp; |
| 698 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 699 | trace_xfs_attr_sf_lookup(args); |
| 700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | ifp = args->dp->i_afp; |
| 702 | ASSERT(ifp->if_flags & XFS_IFINLINE); |
| 703 | sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data; |
| 704 | sfe = &sf->list[0]; |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 705 | for (i = 0; i < sf->hdr.count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) { |
| 707 | if (sfe->namelen != args->namelen) |
| 708 | continue; |
| 709 | if (memcmp(args->name, sfe->nameval, args->namelen) != 0) |
| 710 | continue; |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 711 | if (!xfs_attr_namesp_match(args->flags, sfe->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | continue; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 713 | return -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 715 | return -ENOATTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /* |
| 719 | * Look up a name in a shortform attribute list structure. |
| 720 | */ |
| 721 | /*ARGSUSED*/ |
| 722 | int |
| 723 | xfs_attr_shortform_getvalue(xfs_da_args_t *args) |
| 724 | { |
| 725 | xfs_attr_shortform_t *sf; |
| 726 | xfs_attr_sf_entry_t *sfe; |
| 727 | int i; |
| 728 | |
Eric Sandeen | 914ed44 | 2012-03-30 11:24:11 -0500 | [diff] [blame] | 729 | ASSERT(args->dp->i_afp->if_flags == XFS_IFINLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data; |
| 731 | sfe = &sf->list[0]; |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 732 | for (i = 0; i < sf->hdr.count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) { |
| 734 | if (sfe->namelen != args->namelen) |
| 735 | continue; |
| 736 | if (memcmp(args->name, sfe->nameval, args->namelen) != 0) |
| 737 | continue; |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 738 | if (!xfs_attr_namesp_match(args->flags, sfe->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | continue; |
| 740 | if (args->flags & ATTR_KERNOVAL) { |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 741 | args->valuelen = sfe->valuelen; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 742 | return -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 744 | if (args->valuelen < sfe->valuelen) { |
| 745 | args->valuelen = sfe->valuelen; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 746 | return -ERANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | } |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 748 | args->valuelen = sfe->valuelen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | memcpy(args->value, &sfe->nameval[args->namelen], |
| 750 | args->valuelen); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 751 | return -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | } |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 753 | return -ENOATTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /* |
Darrick J. Wong | 6e643cd | 2017-12-07 19:07:02 -0800 | [diff] [blame] | 757 | * Convert from using the shortform to the leaf. On success, return the |
| 758 | * buffer so that we can keep it locked until we're totally done with it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | */ |
| 760 | int |
Darrick J. Wong | 6e643cd | 2017-12-07 19:07:02 -0800 | [diff] [blame] | 761 | xfs_attr_shortform_to_leaf( |
| 762 | struct xfs_da_args *args, |
| 763 | struct xfs_buf **leaf_bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | { |
| 765 | xfs_inode_t *dp; |
| 766 | xfs_attr_shortform_t *sf; |
| 767 | xfs_attr_sf_entry_t *sfe; |
| 768 | xfs_da_args_t nargs; |
| 769 | char *tmpbuffer; |
| 770 | int error, i, size; |
| 771 | xfs_dablk_t blkno; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 772 | struct xfs_buf *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | xfs_ifork_t *ifp; |
| 774 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 775 | trace_xfs_attr_sf_to_leaf(args); |
| 776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | dp = args->dp; |
| 778 | ifp = dp->i_afp; |
| 779 | sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data; |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 780 | size = be16_to_cpu(sf->hdr.totsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | tmpbuffer = kmem_alloc(size, KM_SLEEP); |
| 782 | ASSERT(tmpbuffer != NULL); |
| 783 | memcpy(tmpbuffer, ifp->if_u1.if_data, size); |
| 784 | sf = (xfs_attr_shortform_t *)tmpbuffer; |
| 785 | |
| 786 | xfs_idata_realloc(dp, -size, XFS_ATTR_FORK); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 787 | xfs_bmap_local_to_extents_empty(dp, XFS_ATTR_FORK); |
| 788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | bp = NULL; |
| 790 | error = xfs_da_grow_inode(args, &blkno); |
| 791 | if (error) { |
| 792 | /* |
| 793 | * If we hit an IO error middle of the transaction inside |
| 794 | * grow_inode(), we may have inconsistent data. Bail out. |
| 795 | */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 796 | if (error == -EIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | goto out; |
| 798 | xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */ |
| 799 | memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */ |
| 800 | goto out; |
| 801 | } |
| 802 | |
| 803 | ASSERT(blkno == 0); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 804 | error = xfs_attr3_leaf_create(args, blkno, &bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | if (error) { |
| 806 | error = xfs_da_shrink_inode(args, 0, bp); |
| 807 | bp = NULL; |
| 808 | if (error) |
| 809 | goto out; |
| 810 | xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */ |
| 811 | memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */ |
| 812 | goto out; |
| 813 | } |
| 814 | |
| 815 | memset((char *)&nargs, 0, sizeof(nargs)); |
| 816 | nargs.dp = dp; |
Dave Chinner | 0650b55 | 2014-06-06 15:01:58 +1000 | [diff] [blame] | 817 | nargs.geo = args->geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | nargs.firstblock = args->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 819 | nargs.dfops = args->dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | nargs.total = args->total; |
| 821 | nargs.whichfork = XFS_ATTR_FORK; |
| 822 | nargs.trans = args->trans; |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 823 | nargs.op_flags = XFS_DA_OP_OKNOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
| 825 | sfe = &sf->list[0]; |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 826 | for (i = 0; i < sf->hdr.count; i++) { |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 827 | nargs.name = sfe->nameval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | nargs.namelen = sfe->namelen; |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 829 | nargs.value = &sfe->nameval[nargs.namelen]; |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 830 | nargs.valuelen = sfe->valuelen; |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 831 | nargs.hashval = xfs_da_hashname(sfe->nameval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | sfe->namelen); |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 833 | nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 834 | error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 835 | ASSERT(error == -ENOATTR); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 836 | error = xfs_attr3_leaf_add(bp, &nargs); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 837 | ASSERT(error != -ENOSPC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | if (error) |
| 839 | goto out; |
| 840 | sfe = XFS_ATTR_SF_NEXTENTRY(sfe); |
| 841 | } |
| 842 | error = 0; |
Darrick J. Wong | 6e643cd | 2017-12-07 19:07:02 -0800 | [diff] [blame] | 843 | *leaf_bp = bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | out: |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 845 | kmem_free(tmpbuffer); |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 846 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | /* |
| 850 | * Check a leaf attribute block to see if all the entries would fit into |
| 851 | * a shortform attribute list. |
| 852 | */ |
| 853 | int |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 854 | xfs_attr_shortform_allfit( |
Dave Chinner | b38958d | 2013-05-20 09:51:14 +1000 | [diff] [blame] | 855 | struct xfs_buf *bp, |
| 856 | struct xfs_inode *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | { |
Dave Chinner | b38958d | 2013-05-20 09:51:14 +1000 | [diff] [blame] | 858 | struct xfs_attr_leafblock *leaf; |
| 859 | struct xfs_attr_leaf_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | xfs_attr_leaf_name_local_t *name_loc; |
Dave Chinner | b38958d | 2013-05-20 09:51:14 +1000 | [diff] [blame] | 861 | struct xfs_attr3_icleaf_hdr leafhdr; |
| 862 | int bytes; |
| 863 | int i; |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 864 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 866 | leaf = bp->b_addr; |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 867 | xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf); |
Dave Chinner | b38958d | 2013-05-20 09:51:14 +1000 | [diff] [blame] | 868 | entry = xfs_attr3_leaf_entryp(leaf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | bytes = sizeof(struct xfs_attr_sf_hdr); |
Dave Chinner | b38958d | 2013-05-20 09:51:14 +1000 | [diff] [blame] | 871 | for (i = 0; i < leafhdr.count; entry++, i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | if (entry->flags & XFS_ATTR_INCOMPLETE) |
| 873 | continue; /* don't copy partial entries */ |
| 874 | if (!(entry->flags & XFS_ATTR_LOCAL)) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 875 | return 0; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 876 | name_loc = xfs_attr3_leaf_name_local(leaf, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 878 | return 0; |
Nathan Scott | 053b575 | 2006-03-17 17:29:09 +1100 | [diff] [blame] | 879 | if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 880 | return 0; |
Dave Chinner | b38958d | 2013-05-20 09:51:14 +1000 | [diff] [blame] | 881 | bytes += sizeof(struct xfs_attr_sf_entry) - 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | + name_loc->namelen |
Nathan Scott | 053b575 | 2006-03-17 17:29:09 +1100 | [diff] [blame] | 883 | + be16_to_cpu(name_loc->valuelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | } |
Nathan Scott | 13059ff | 2006-01-11 15:32:01 +1100 | [diff] [blame] | 885 | if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) && |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 886 | (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) && |
Nathan Scott | e0144ca | 2005-11-25 16:42:22 +1100 | [diff] [blame] | 887 | (bytes == sizeof(struct xfs_attr_sf_hdr))) |
Dave Chinner | b38958d | 2013-05-20 09:51:14 +1000 | [diff] [blame] | 888 | return -1; |
| 889 | return xfs_attr_shortform_bytesfit(dp, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Darrick J. Wong | 1e1bbd8 | 2018-01-08 10:51:05 -0800 | [diff] [blame] | 892 | /* Verify the consistency of an inline attribute fork. */ |
| 893 | xfs_failaddr_t |
| 894 | xfs_attr_shortform_verify( |
| 895 | struct xfs_inode *ip) |
| 896 | { |
| 897 | struct xfs_attr_shortform *sfp; |
| 898 | struct xfs_attr_sf_entry *sfep; |
| 899 | struct xfs_attr_sf_entry *next_sfep; |
| 900 | char *endp; |
| 901 | struct xfs_ifork *ifp; |
| 902 | int i; |
| 903 | int size; |
| 904 | |
| 905 | ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL); |
| 906 | ifp = XFS_IFORK_PTR(ip, XFS_ATTR_FORK); |
| 907 | sfp = (struct xfs_attr_shortform *)ifp->if_u1.if_data; |
| 908 | size = ifp->if_bytes; |
| 909 | |
| 910 | /* |
| 911 | * Give up if the attribute is way too short. |
| 912 | */ |
| 913 | if (size < sizeof(struct xfs_attr_sf_hdr)) |
| 914 | return __this_address; |
| 915 | |
| 916 | endp = (char *)sfp + size; |
| 917 | |
| 918 | /* Check all reported entries */ |
| 919 | sfep = &sfp->list[0]; |
| 920 | for (i = 0; i < sfp->hdr.count; i++) { |
| 921 | /* |
| 922 | * struct xfs_attr_sf_entry has a variable length. |
| 923 | * Check the fixed-offset parts of the structure are |
| 924 | * within the data buffer. |
| 925 | */ |
| 926 | if (((char *)sfep + sizeof(*sfep)) >= endp) |
| 927 | return __this_address; |
| 928 | |
| 929 | /* Don't allow names with known bad length. */ |
| 930 | if (sfep->namelen == 0) |
| 931 | return __this_address; |
| 932 | |
| 933 | /* |
| 934 | * Check that the variable-length part of the structure is |
| 935 | * within the data buffer. The next entry starts after the |
| 936 | * name component, so nextentry is an acceptable test. |
| 937 | */ |
| 938 | next_sfep = XFS_ATTR_SF_NEXTENTRY(sfep); |
| 939 | if ((char *)next_sfep > endp) |
| 940 | return __this_address; |
| 941 | |
| 942 | /* |
| 943 | * Check for unknown flags. Short form doesn't support |
| 944 | * the incomplete or local bits, so we can use the namespace |
| 945 | * mask here. |
| 946 | */ |
| 947 | if (sfep->flags & ~XFS_ATTR_NSP_ONDISK_MASK) |
| 948 | return __this_address; |
| 949 | |
| 950 | /* |
| 951 | * Check for invalid namespace combinations. We only allow |
| 952 | * one namespace flag per xattr, so we can just count the |
| 953 | * bits (i.e. hweight) here. |
| 954 | */ |
| 955 | if (hweight8(sfep->flags & XFS_ATTR_NSP_ONDISK_MASK) > 1) |
| 956 | return __this_address; |
| 957 | |
| 958 | sfep = next_sfep; |
| 959 | } |
| 960 | if ((void *)sfep != (void *)endp) |
| 961 | return __this_address; |
| 962 | |
| 963 | return NULL; |
| 964 | } |
| 965 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | /* |
| 967 | * Convert a leaf attribute list to shortform attribute list |
| 968 | */ |
| 969 | int |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 970 | xfs_attr3_leaf_to_shortform( |
| 971 | struct xfs_buf *bp, |
| 972 | struct xfs_da_args *args, |
| 973 | int forkoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 975 | struct xfs_attr_leafblock *leaf; |
| 976 | struct xfs_attr3_icleaf_hdr ichdr; |
| 977 | struct xfs_attr_leaf_entry *entry; |
| 978 | struct xfs_attr_leaf_name_local *name_loc; |
| 979 | struct xfs_da_args nargs; |
| 980 | struct xfs_inode *dp = args->dp; |
| 981 | char *tmpbuffer; |
| 982 | int error; |
| 983 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 985 | trace_xfs_attr_leaf_to_sf(args); |
| 986 | |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 987 | tmpbuffer = kmem_alloc(args->geo->blksize, KM_SLEEP); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 988 | if (!tmpbuffer) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 989 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 991 | memcpy(tmpbuffer, bp->b_addr, args->geo->blksize); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | leaf = (xfs_attr_leafblock_t *)tmpbuffer; |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 994 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 995 | entry = xfs_attr3_leaf_entryp(leaf); |
| 996 | |
| 997 | /* XXX (dgc): buffer is about to be marked stale - why zero it? */ |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 998 | memset(bp->b_addr, 0, args->geo->blksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | |
| 1000 | /* |
| 1001 | * Clean out the prior contents of the attribute list. |
| 1002 | */ |
| 1003 | error = xfs_da_shrink_inode(args, 0, bp); |
| 1004 | if (error) |
| 1005 | goto out; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 1006 | |
| 1007 | if (forkoff == -1) { |
Nathan Scott | 13059ff | 2006-01-11 15:32:01 +1100 | [diff] [blame] | 1008 | ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2); |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 1009 | ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE); |
Dave Chinner | 6dfe5a0 | 2015-05-29 07:40:08 +1000 | [diff] [blame] | 1010 | xfs_attr_fork_remove(dp, args->trans); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | goto out; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | xfs_attr_shortform_create(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
| 1016 | /* |
| 1017 | * Copy the attributes |
| 1018 | */ |
| 1019 | memset((char *)&nargs, 0, sizeof(nargs)); |
Dave Chinner | 0650b55 | 2014-06-06 15:01:58 +1000 | [diff] [blame] | 1020 | nargs.geo = args->geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | nargs.dp = dp; |
| 1022 | nargs.firstblock = args->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1023 | nargs.dfops = args->dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | nargs.total = args->total; |
| 1025 | nargs.whichfork = XFS_ATTR_FORK; |
| 1026 | nargs.trans = args->trans; |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 1027 | nargs.op_flags = XFS_DA_OP_OKNOENT; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1028 | |
| 1029 | for (i = 0; i < ichdr.count; entry++, i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | if (entry->flags & XFS_ATTR_INCOMPLETE) |
| 1031 | continue; /* don't copy partial entries */ |
| 1032 | if (!entry->nameidx) |
| 1033 | continue; |
| 1034 | ASSERT(entry->flags & XFS_ATTR_LOCAL); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1035 | name_loc = xfs_attr3_leaf_name_local(leaf, i); |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 1036 | nargs.name = name_loc->nameval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | nargs.namelen = name_loc->namelen; |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 1038 | nargs.value = &name_loc->nameval[nargs.namelen]; |
Nathan Scott | 053b575 | 2006-03-17 17:29:09 +1100 | [diff] [blame] | 1039 | nargs.valuelen = be16_to_cpu(name_loc->valuelen); |
Nathan Scott | 6b19f2d | 2006-03-17 17:29:02 +1100 | [diff] [blame] | 1040 | nargs.hashval = be32_to_cpu(entry->hashval); |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 1041 | nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 1042 | xfs_attr_shortform_add(&nargs, forkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | } |
| 1044 | error = 0; |
| 1045 | |
| 1046 | out: |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1047 | kmem_free(tmpbuffer); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1048 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /* |
| 1052 | * Convert from using a single leaf to a root node and a leaf. |
| 1053 | */ |
| 1054 | int |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1055 | xfs_attr3_leaf_to_node( |
| 1056 | struct xfs_da_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1058 | struct xfs_attr_leafblock *leaf; |
| 1059 | struct xfs_attr3_icleaf_hdr icleafhdr; |
| 1060 | struct xfs_attr_leaf_entry *entries; |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1061 | struct xfs_da_node_entry *btree; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1062 | struct xfs_da3_icnode_hdr icnodehdr; |
| 1063 | struct xfs_da_intnode *node; |
| 1064 | struct xfs_inode *dp = args->dp; |
| 1065 | struct xfs_mount *mp = dp->i_mount; |
| 1066 | struct xfs_buf *bp1 = NULL; |
| 1067 | struct xfs_buf *bp2 = NULL; |
| 1068 | xfs_dablk_t blkno; |
| 1069 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1071 | trace_xfs_attr_leaf_to_node(args); |
| 1072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | error = xfs_da_grow_inode(args, &blkno); |
| 1074 | if (error) |
| 1075 | goto out; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1076 | error = xfs_attr3_leaf_read(args->trans, dp, 0, -1, &bp1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | if (error) |
| 1078 | goto out; |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 1079 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1080 | error = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp2, XFS_ATTR_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | if (error) |
| 1082 | goto out; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1083 | |
| 1084 | /* copy leaf to new buffer, update identifiers */ |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 1085 | xfs_trans_buf_set_type(args->trans, bp2, XFS_BLFT_ATTR_LEAF_BUF); |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 1086 | bp2->b_ops = bp1->b_ops; |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1087 | memcpy(bp2->b_addr, bp1->b_addr, args->geo->blksize); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1088 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 1089 | struct xfs_da3_blkinfo *hdr3 = bp2->b_addr; |
| 1090 | hdr3->blkno = cpu_to_be64(bp2->b_bn); |
| 1091 | } |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1092 | xfs_trans_log_buf(args->trans, bp2, 0, args->geo->blksize - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | |
| 1094 | /* |
| 1095 | * Set up the new root node. |
| 1096 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1097 | error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | if (error) |
| 1099 | goto out; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1100 | node = bp1->b_addr; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1101 | dp->d_ops->node_hdr_from_disk(&icnodehdr, node); |
Dave Chinner | 4bceb18 | 2013-10-29 22:11:51 +1100 | [diff] [blame] | 1102 | btree = dp->d_ops->node_tree_p(node); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1103 | |
| 1104 | leaf = bp2->b_addr; |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 1105 | xfs_attr3_leaf_hdr_from_disk(args->geo, &icleafhdr, leaf); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1106 | entries = xfs_attr3_leaf_entryp(leaf); |
| 1107 | |
| 1108 | /* both on-disk, don't endian-flip twice */ |
| 1109 | btree[0].hashval = entries[icleafhdr.count - 1].hashval; |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1110 | btree[0].before = cpu_to_be32(blkno); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1111 | icnodehdr.count = 1; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1112 | dp->d_ops->node_hdr_to_disk(node, &icnodehdr); |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1113 | xfs_trans_log_buf(args->trans, bp1, 0, args->geo->blksize - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | error = 0; |
| 1115 | out: |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1116 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | /*======================================================================== |
| 1120 | * Routines used for growing the Btree. |
| 1121 | *========================================================================*/ |
| 1122 | |
| 1123 | /* |
| 1124 | * Create the initial contents of a leaf attribute list |
| 1125 | * or a leaf in a node attribute list. |
| 1126 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 1127 | STATIC int |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1128 | xfs_attr3_leaf_create( |
| 1129 | struct xfs_da_args *args, |
| 1130 | xfs_dablk_t blkno, |
| 1131 | struct xfs_buf **bpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1133 | struct xfs_attr_leafblock *leaf; |
| 1134 | struct xfs_attr3_icleaf_hdr ichdr; |
| 1135 | struct xfs_inode *dp = args->dp; |
| 1136 | struct xfs_mount *mp = dp->i_mount; |
| 1137 | struct xfs_buf *bp; |
| 1138 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1140 | trace_xfs_attr_leaf_create(args); |
| 1141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp, |
| 1143 | XFS_ATTR_FORK); |
| 1144 | if (error) |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1145 | return error; |
| 1146 | bp->b_ops = &xfs_attr3_leaf_buf_ops; |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 1147 | xfs_trans_buf_set_type(args->trans, bp, XFS_BLFT_ATTR_LEAF_BUF); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1148 | leaf = bp->b_addr; |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1149 | memset(leaf, 0, args->geo->blksize); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1150 | |
| 1151 | memset(&ichdr, 0, sizeof(ichdr)); |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1152 | ichdr.firstused = args->geo->blksize; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1153 | |
| 1154 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 1155 | struct xfs_da3_blkinfo *hdr3 = bp->b_addr; |
| 1156 | |
| 1157 | ichdr.magic = XFS_ATTR3_LEAF_MAGIC; |
| 1158 | |
| 1159 | hdr3->blkno = cpu_to_be64(bp->b_bn); |
| 1160 | hdr3->owner = cpu_to_be64(dp->i_ino); |
Eric Sandeen | ce748ea | 2015-07-29 11:53:31 +1000 | [diff] [blame] | 1161 | uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1162 | |
| 1163 | ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr); |
| 1164 | } else { |
| 1165 | ichdr.magic = XFS_ATTR_LEAF_MAGIC; |
| 1166 | ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1168 | ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 1170 | xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr); |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1171 | xfs_trans_log_buf(args->trans, bp, 0, args->geo->blksize - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | |
| 1173 | *bpp = bp; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1174 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * Split the leaf node, rebalance, then add the new entry. |
| 1179 | */ |
| 1180 | int |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1181 | xfs_attr3_leaf_split( |
| 1182 | struct xfs_da_state *state, |
| 1183 | struct xfs_da_state_blk *oldblk, |
| 1184 | struct xfs_da_state_blk *newblk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | { |
| 1186 | xfs_dablk_t blkno; |
| 1187 | int error; |
| 1188 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1189 | trace_xfs_attr_leaf_split(state->args); |
| 1190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | /* |
| 1192 | * Allocate space for a new leaf node. |
| 1193 | */ |
| 1194 | ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC); |
| 1195 | error = xfs_da_grow_inode(state->args, &blkno); |
| 1196 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1197 | return error; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1198 | error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1200 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | newblk->blkno = blkno; |
| 1202 | newblk->magic = XFS_ATTR_LEAF_MAGIC; |
| 1203 | |
| 1204 | /* |
| 1205 | * Rebalance the entries across the two leaves. |
| 1206 | * NOTE: rebalance() currently depends on the 2nd block being empty. |
| 1207 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1208 | xfs_attr3_leaf_rebalance(state, oldblk, newblk); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1209 | error = xfs_da3_blk_link(state, oldblk, newblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1211 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | |
| 1213 | /* |
| 1214 | * Save info on "old" attribute for "atomic rename" ops, leaf_add() |
| 1215 | * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the |
| 1216 | * "new" attrs info. Will need the "old" info to remove it later. |
| 1217 | * |
| 1218 | * Insert the "new" entry in the correct block. |
| 1219 | */ |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1220 | if (state->inleaf) { |
| 1221 | trace_xfs_attr_leaf_add_old(state->args); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1222 | error = xfs_attr3_leaf_add(oldblk->bp, state->args); |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1223 | } else { |
| 1224 | trace_xfs_attr_leaf_add_new(state->args); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1225 | error = xfs_attr3_leaf_add(newblk->bp, state->args); |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | |
| 1228 | /* |
| 1229 | * Update last hashval in each block since we added the name. |
| 1230 | */ |
| 1231 | oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL); |
| 1232 | newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL); |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1233 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | /* |
| 1237 | * Add a name to the leaf attribute list structure. |
| 1238 | */ |
| 1239 | int |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1240 | xfs_attr3_leaf_add( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1241 | struct xfs_buf *bp, |
| 1242 | struct xfs_da_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1244 | struct xfs_attr_leafblock *leaf; |
| 1245 | struct xfs_attr3_icleaf_hdr ichdr; |
| 1246 | int tablesize; |
| 1247 | int entsize; |
| 1248 | int sum; |
| 1249 | int tmp; |
| 1250 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1252 | trace_xfs_attr_leaf_add(args); |
| 1253 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1254 | leaf = bp->b_addr; |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 1255 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1256 | ASSERT(args->index >= 0 && args->index <= ichdr.count); |
Dave Chinner | c59f0ad | 2014-06-06 15:21:27 +1000 | [diff] [blame] | 1257 | entsize = xfs_attr_leaf_newentsize(args, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
| 1259 | /* |
| 1260 | * Search through freemap for first-fit on new name length. |
| 1261 | * (may need to figure in size of entry struct too) |
| 1262 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1263 | tablesize = (ichdr.count + 1) * sizeof(xfs_attr_leaf_entry_t) |
| 1264 | + xfs_attr3_leaf_hdr_size(leaf); |
| 1265 | for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) { |
| 1266 | if (tablesize > ichdr.firstused) { |
| 1267 | sum += ichdr.freemap[i].size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | continue; |
| 1269 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1270 | if (!ichdr.freemap[i].size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | continue; /* no space in this map */ |
| 1272 | tmp = entsize; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1273 | if (ichdr.freemap[i].base < ichdr.firstused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | tmp += sizeof(xfs_attr_leaf_entry_t); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1275 | if (ichdr.freemap[i].size >= tmp) { |
| 1276 | tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, i); |
| 1277 | goto out_log_hdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1279 | sum += ichdr.freemap[i].size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | /* |
| 1283 | * If there are no holes in the address space of the block, |
| 1284 | * and we don't have enough freespace, then compaction will do us |
| 1285 | * no good and we should just give up. |
| 1286 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1287 | if (!ichdr.holes && sum < entsize) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1288 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | |
| 1290 | /* |
| 1291 | * Compact the entries to coalesce free space. |
| 1292 | * This may change the hdr->count via dropping INCOMPLETE entries. |
| 1293 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1294 | xfs_attr3_leaf_compact(args, &ichdr, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | |
| 1296 | /* |
| 1297 | * After compaction, the block is guaranteed to have only one |
| 1298 | * free region, in freemap[0]. If it is not big enough, give up. |
| 1299 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1300 | if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1301 | tmp = -ENOSPC; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1302 | goto out_log_hdr; |
| 1303 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1305 | tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, 0); |
| 1306 | |
| 1307 | out_log_hdr: |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 1308 | xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1309 | xfs_trans_log_buf(args->trans, bp, |
| 1310 | XFS_DA_LOGRANGE(leaf, &leaf->hdr, |
| 1311 | xfs_attr3_leaf_hdr_size(leaf))); |
| 1312 | return tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | /* |
| 1316 | * Add a name to a leaf attribute list structure. |
| 1317 | */ |
| 1318 | STATIC int |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1319 | xfs_attr3_leaf_add_work( |
| 1320 | struct xfs_buf *bp, |
| 1321 | struct xfs_attr3_icleaf_hdr *ichdr, |
| 1322 | struct xfs_da_args *args, |
| 1323 | int mapindex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1325 | struct xfs_attr_leafblock *leaf; |
| 1326 | struct xfs_attr_leaf_entry *entry; |
| 1327 | struct xfs_attr_leaf_name_local *name_loc; |
| 1328 | struct xfs_attr_leaf_name_remote *name_rmt; |
| 1329 | struct xfs_mount *mp; |
| 1330 | int tmp; |
| 1331 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1333 | trace_xfs_attr_leaf_add_work(args); |
| 1334 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1335 | leaf = bp->b_addr; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1336 | ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE); |
| 1337 | ASSERT(args->index >= 0 && args->index <= ichdr->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | |
| 1339 | /* |
| 1340 | * Force open some space in the entry array and fill it in. |
| 1341 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1342 | entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; |
| 1343 | if (args->index < ichdr->count) { |
| 1344 | tmp = ichdr->count - args->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | tmp *= sizeof(xfs_attr_leaf_entry_t); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1346 | memmove(entry + 1, entry, tmp); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1347 | xfs_trans_log_buf(args->trans, bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry))); |
| 1349 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1350 | ichdr->count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | |
| 1352 | /* |
| 1353 | * Allocate space for the new string (at the end of the run). |
| 1354 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | mp = args->trans->t_mountp; |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1356 | ASSERT(ichdr->freemap[mapindex].base < args->geo->blksize); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1357 | ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0); |
| 1358 | ASSERT(ichdr->freemap[mapindex].size >= |
Dave Chinner | c59f0ad | 2014-06-06 15:21:27 +1000 | [diff] [blame] | 1359 | xfs_attr_leaf_newentsize(args, NULL)); |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1360 | ASSERT(ichdr->freemap[mapindex].size < args->geo->blksize); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1361 | ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0); |
| 1362 | |
Dave Chinner | c59f0ad | 2014-06-06 15:21:27 +1000 | [diff] [blame] | 1363 | ichdr->freemap[mapindex].size -= xfs_attr_leaf_newentsize(args, &tmp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1364 | |
| 1365 | entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base + |
| 1366 | ichdr->freemap[mapindex].size); |
Nathan Scott | 6b19f2d | 2006-03-17 17:29:02 +1100 | [diff] [blame] | 1367 | entry->hashval = cpu_to_be32(args->hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | entry->flags = tmp ? XFS_ATTR_LOCAL : 0; |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 1369 | entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags); |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 1370 | if (args->op_flags & XFS_DA_OP_RENAME) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | entry->flags |= XFS_ATTR_INCOMPLETE; |
| 1372 | if ((args->blkno2 == args->blkno) && |
| 1373 | (args->index2 <= args->index)) { |
| 1374 | args->index2++; |
| 1375 | } |
| 1376 | } |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1377 | xfs_trans_log_buf(args->trans, bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry))); |
Nathan Scott | 6b19f2d | 2006-03-17 17:29:02 +1100 | [diff] [blame] | 1379 | ASSERT((args->index == 0) || |
| 1380 | (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval))); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1381 | ASSERT((args->index == ichdr->count - 1) || |
Nathan Scott | 6b19f2d | 2006-03-17 17:29:02 +1100 | [diff] [blame] | 1382 | (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | |
| 1384 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | * For "remote" attribute values, simply note that we need to |
| 1386 | * allocate space for the "remote" value. We can't actually |
| 1387 | * allocate the extents in this transaction, and we can't decide |
| 1388 | * which blocks they should be as we might allocate more blocks |
| 1389 | * as part of this transaction (a split operation for example). |
| 1390 | */ |
| 1391 | if (entry->flags & XFS_ATTR_LOCAL) { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1392 | name_loc = xfs_attr3_leaf_name_local(leaf, args->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | name_loc->namelen = args->namelen; |
Nathan Scott | 053b575 | 2006-03-17 17:29:09 +1100 | [diff] [blame] | 1394 | name_loc->valuelen = cpu_to_be16(args->valuelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | memcpy((char *)name_loc->nameval, args->name, args->namelen); |
| 1396 | memcpy((char *)&name_loc->nameval[args->namelen], args->value, |
Nathan Scott | 053b575 | 2006-03-17 17:29:09 +1100 | [diff] [blame] | 1397 | be16_to_cpu(name_loc->valuelen)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | } else { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1399 | name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | name_rmt->namelen = args->namelen; |
| 1401 | memcpy((char *)name_rmt->name, args->name, args->namelen); |
| 1402 | entry->flags |= XFS_ATTR_INCOMPLETE; |
| 1403 | /* just in case */ |
| 1404 | name_rmt->valuelen = 0; |
| 1405 | name_rmt->valueblk = 0; |
| 1406 | args->rmtblkno = 1; |
Dave Chinner | ad1858d | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 1407 | args->rmtblkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen); |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 1408 | args->rmtvaluelen = args->valuelen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | } |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1410 | xfs_trans_log_buf(args->trans, bp, |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1411 | XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | xfs_attr_leaf_entsize(leaf, args->index))); |
| 1413 | |
| 1414 | /* |
| 1415 | * Update the control info for this leaf node |
| 1416 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1417 | if (be16_to_cpu(entry->nameidx) < ichdr->firstused) |
| 1418 | ichdr->firstused = be16_to_cpu(entry->nameidx); |
| 1419 | |
| 1420 | ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t) |
| 1421 | + xfs_attr3_leaf_hdr_size(leaf)); |
| 1422 | tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t) |
| 1423 | + xfs_attr3_leaf_hdr_size(leaf); |
| 1424 | |
| 1425 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { |
| 1426 | if (ichdr->freemap[i].base == tmp) { |
| 1427 | ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t); |
| 1428 | ichdr->freemap[i].size -= sizeof(xfs_attr_leaf_entry_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | } |
| 1430 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1431 | ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index); |
| 1432 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | /* |
| 1436 | * Garbage collect a leaf attribute list block by copying it to a new buffer. |
| 1437 | */ |
| 1438 | STATIC void |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1439 | xfs_attr3_leaf_compact( |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1440 | struct xfs_da_args *args, |
Dave Chinner | d4c712b | 2013-05-21 18:02:06 +1000 | [diff] [blame] | 1441 | struct xfs_attr3_icleaf_hdr *ichdr_dst, |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1442 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | { |
Dave Chinner | d4c712b | 2013-05-21 18:02:06 +1000 | [diff] [blame] | 1444 | struct xfs_attr_leafblock *leaf_src; |
| 1445 | struct xfs_attr_leafblock *leaf_dst; |
| 1446 | struct xfs_attr3_icleaf_hdr ichdr_src; |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1447 | struct xfs_trans *trans = args->trans; |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1448 | char *tmpbuffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1450 | trace_xfs_attr_leaf_compact(args); |
| 1451 | |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1452 | tmpbuffer = kmem_alloc(args->geo->blksize, KM_SLEEP); |
| 1453 | memcpy(tmpbuffer, bp->b_addr, args->geo->blksize); |
| 1454 | memset(bp->b_addr, 0, args->geo->blksize); |
Dave Chinner | d4c712b | 2013-05-21 18:02:06 +1000 | [diff] [blame] | 1455 | leaf_src = (xfs_attr_leafblock_t *)tmpbuffer; |
| 1456 | leaf_dst = bp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | |
| 1458 | /* |
Dave Chinner | d4c712b | 2013-05-21 18:02:06 +1000 | [diff] [blame] | 1459 | * Copy the on-disk header back into the destination buffer to ensure |
| 1460 | * all the information in the header that is not part of the incore |
| 1461 | * header structure is preserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | */ |
Dave Chinner | d4c712b | 2013-05-21 18:02:06 +1000 | [diff] [blame] | 1463 | memcpy(bp->b_addr, tmpbuffer, xfs_attr3_leaf_hdr_size(leaf_src)); |
| 1464 | |
| 1465 | /* Initialise the incore headers */ |
| 1466 | ichdr_src = *ichdr_dst; /* struct copy */ |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1467 | ichdr_dst->firstused = args->geo->blksize; |
Dave Chinner | d4c712b | 2013-05-21 18:02:06 +1000 | [diff] [blame] | 1468 | ichdr_dst->usedbytes = 0; |
| 1469 | ichdr_dst->count = 0; |
| 1470 | ichdr_dst->holes = 0; |
| 1471 | ichdr_dst->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_src); |
| 1472 | ichdr_dst->freemap[0].size = ichdr_dst->firstused - |
| 1473 | ichdr_dst->freemap[0].base; |
| 1474 | |
Dave Chinner | d4c712b | 2013-05-21 18:02:06 +1000 | [diff] [blame] | 1475 | /* write the header back to initialise the underlying buffer */ |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 1476 | xfs_attr3_leaf_hdr_to_disk(args->geo, leaf_dst, ichdr_dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | |
| 1478 | /* |
| 1479 | * Copy all entry's in the same (sorted) order, |
| 1480 | * but allocate name/value pairs packed and in sequence. |
| 1481 | */ |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1482 | xfs_attr3_leaf_moveents(args, leaf_src, &ichdr_src, 0, |
| 1483 | leaf_dst, ichdr_dst, 0, ichdr_src.count); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1484 | /* |
| 1485 | * this logs the entire buffer, but the caller must write the header |
| 1486 | * back to the buffer when it is finished modifying it. |
| 1487 | */ |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1488 | xfs_trans_log_buf(trans, bp, 0, args->geo->blksize - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1490 | kmem_free(tmpbuffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | /* |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1494 | * Compare two leaf blocks "order". |
| 1495 | * Return 0 unless leaf2 should go before leaf1. |
| 1496 | */ |
| 1497 | static int |
| 1498 | xfs_attr3_leaf_order( |
| 1499 | struct xfs_buf *leaf1_bp, |
| 1500 | struct xfs_attr3_icleaf_hdr *leaf1hdr, |
| 1501 | struct xfs_buf *leaf2_bp, |
| 1502 | struct xfs_attr3_icleaf_hdr *leaf2hdr) |
| 1503 | { |
| 1504 | struct xfs_attr_leaf_entry *entries1; |
| 1505 | struct xfs_attr_leaf_entry *entries2; |
| 1506 | |
| 1507 | entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr); |
| 1508 | entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr); |
| 1509 | if (leaf1hdr->count > 0 && leaf2hdr->count > 0 && |
| 1510 | ((be32_to_cpu(entries2[0].hashval) < |
| 1511 | be32_to_cpu(entries1[0].hashval)) || |
| 1512 | (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) < |
| 1513 | be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) { |
| 1514 | return 1; |
| 1515 | } |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
| 1519 | int |
| 1520 | xfs_attr_leaf_order( |
| 1521 | struct xfs_buf *leaf1_bp, |
| 1522 | struct xfs_buf *leaf2_bp) |
| 1523 | { |
| 1524 | struct xfs_attr3_icleaf_hdr ichdr1; |
| 1525 | struct xfs_attr3_icleaf_hdr ichdr2; |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 1526 | struct xfs_mount *mp = leaf1_bp->b_target->bt_mount; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1527 | |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 1528 | xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr1, leaf1_bp->b_addr); |
| 1529 | xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr2, leaf2_bp->b_addr); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1530 | return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2); |
| 1531 | } |
| 1532 | |
| 1533 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | * Redistribute the attribute list entries between two leaf nodes, |
| 1535 | * taking into account the size of the new entry. |
| 1536 | * |
| 1537 | * NOTE: if new block is empty, then it will get the upper half of the |
| 1538 | * old block. At present, all (one) callers pass in an empty second block. |
| 1539 | * |
| 1540 | * This code adjusts the args->index/blkno and args->index2/blkno2 fields |
| 1541 | * to match what it is doing in splitting the attribute leaf block. Those |
| 1542 | * values are used in "atomic rename" operations on attributes. Note that |
| 1543 | * the "new" and "old" values can end up in different blocks. |
| 1544 | */ |
| 1545 | STATIC void |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1546 | xfs_attr3_leaf_rebalance( |
| 1547 | struct xfs_da_state *state, |
| 1548 | struct xfs_da_state_blk *blk1, |
| 1549 | struct xfs_da_state_blk *blk2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1551 | struct xfs_da_args *args; |
| 1552 | struct xfs_attr_leafblock *leaf1; |
| 1553 | struct xfs_attr_leafblock *leaf2; |
| 1554 | struct xfs_attr3_icleaf_hdr ichdr1; |
| 1555 | struct xfs_attr3_icleaf_hdr ichdr2; |
| 1556 | struct xfs_attr_leaf_entry *entries1; |
| 1557 | struct xfs_attr_leaf_entry *entries2; |
| 1558 | int count; |
| 1559 | int totallen; |
| 1560 | int max; |
| 1561 | int space; |
| 1562 | int swap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | |
| 1564 | /* |
| 1565 | * Set up environment. |
| 1566 | */ |
| 1567 | ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC); |
| 1568 | ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1569 | leaf1 = blk1->bp->b_addr; |
| 1570 | leaf2 = blk2->bp->b_addr; |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 1571 | xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr1, leaf1); |
| 1572 | xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, leaf2); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1573 | ASSERT(ichdr2.count == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | args = state->args; |
| 1575 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1576 | trace_xfs_attr_leaf_rebalance(args); |
| 1577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | /* |
| 1579 | * Check ordering of blocks, reverse if it makes things simpler. |
| 1580 | * |
| 1581 | * NOTE: Given that all (current) callers pass in an empty |
| 1582 | * second block, this code should never set "swap". |
| 1583 | */ |
| 1584 | swap = 0; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1585 | if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) { |
| 1586 | struct xfs_da_state_blk *tmp_blk; |
| 1587 | struct xfs_attr3_icleaf_hdr tmp_ichdr; |
| 1588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | tmp_blk = blk1; |
| 1590 | blk1 = blk2; |
| 1591 | blk2 = tmp_blk; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1592 | |
| 1593 | /* struct copies to swap them rather than reconverting */ |
| 1594 | tmp_ichdr = ichdr1; |
| 1595 | ichdr1 = ichdr2; |
| 1596 | ichdr2 = tmp_ichdr; |
| 1597 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1598 | leaf1 = blk1->bp->b_addr; |
| 1599 | leaf2 = blk2->bp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | swap = 1; |
| 1601 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | |
| 1603 | /* |
| 1604 | * Examine entries until we reduce the absolute difference in |
| 1605 | * byte usage between the two blocks to a minimum. Then get |
| 1606 | * the direction to copy and the number of elements to move. |
| 1607 | * |
| 1608 | * "inleaf" is true if the new entry should be inserted into blk1. |
| 1609 | * If "swap" is also true, then reverse the sense of "inleaf". |
| 1610 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1611 | state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1, |
| 1612 | blk2, &ichdr2, |
| 1613 | &count, &totallen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | if (swap) |
| 1615 | state->inleaf = !state->inleaf; |
| 1616 | |
| 1617 | /* |
| 1618 | * Move any entries required from leaf to leaf: |
| 1619 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1620 | if (count < ichdr1.count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | /* |
| 1622 | * Figure the total bytes to be added to the destination leaf. |
| 1623 | */ |
| 1624 | /* number entries being moved */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1625 | count = ichdr1.count - count; |
| 1626 | space = ichdr1.usedbytes - totallen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | space += count * sizeof(xfs_attr_leaf_entry_t); |
| 1628 | |
| 1629 | /* |
| 1630 | * leaf2 is the destination, compact it if it looks tight. |
| 1631 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1632 | max = ichdr2.firstused - xfs_attr3_leaf_hdr_size(leaf1); |
| 1633 | max -= ichdr2.count * sizeof(xfs_attr_leaf_entry_t); |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1634 | if (space > max) |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1635 | xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | |
| 1637 | /* |
| 1638 | * Move high entries from leaf1 to low end of leaf2. |
| 1639 | */ |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1640 | xfs_attr3_leaf_moveents(args, leaf1, &ichdr1, |
| 1641 | ichdr1.count - count, leaf2, &ichdr2, 0, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1643 | } else if (count > ichdr1.count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | /* |
| 1645 | * I assert that since all callers pass in an empty |
| 1646 | * second buffer, this code should never execute. |
| 1647 | */ |
Dave Chinner | 07428d7 | 2012-11-12 22:09:44 +1100 | [diff] [blame] | 1648 | ASSERT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | |
| 1650 | /* |
| 1651 | * Figure the total bytes to be added to the destination leaf. |
| 1652 | */ |
| 1653 | /* number entries being moved */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1654 | count -= ichdr1.count; |
| 1655 | space = totallen - ichdr1.usedbytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | space += count * sizeof(xfs_attr_leaf_entry_t); |
| 1657 | |
| 1658 | /* |
| 1659 | * leaf1 is the destination, compact it if it looks tight. |
| 1660 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1661 | max = ichdr1.firstused - xfs_attr3_leaf_hdr_size(leaf1); |
| 1662 | max -= ichdr1.count * sizeof(xfs_attr_leaf_entry_t); |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1663 | if (space > max) |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1664 | xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | |
| 1666 | /* |
| 1667 | * Move low entries from leaf2 to high end of leaf1. |
| 1668 | */ |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1669 | xfs_attr3_leaf_moveents(args, leaf2, &ichdr2, 0, leaf1, &ichdr1, |
| 1670 | ichdr1.count, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | } |
| 1672 | |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 1673 | xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf1, &ichdr1); |
| 1674 | xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf2, &ichdr2); |
Dave Chinner | b2a21e7 | 2014-06-06 15:22:04 +1000 | [diff] [blame] | 1675 | xfs_trans_log_buf(args->trans, blk1->bp, 0, args->geo->blksize - 1); |
| 1676 | xfs_trans_log_buf(args->trans, blk2->bp, 0, args->geo->blksize - 1); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | /* |
| 1679 | * Copy out last hashval in each block for B-tree code. |
| 1680 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1681 | entries1 = xfs_attr3_leaf_entryp(leaf1); |
| 1682 | entries2 = xfs_attr3_leaf_entryp(leaf2); |
| 1683 | blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval); |
| 1684 | blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | |
| 1686 | /* |
| 1687 | * Adjust the expected index for insertion. |
| 1688 | * NOTE: this code depends on the (current) situation that the |
| 1689 | * second block was originally empty. |
| 1690 | * |
| 1691 | * If the insertion point moved to the 2nd block, we must adjust |
| 1692 | * the index. We must also track the entry just following the |
| 1693 | * new entry for use in an "atomic rename" operation, that entry |
| 1694 | * is always the "old" entry and the "new" entry is what we are |
| 1695 | * inserting. The index/blkno fields refer to the "old" entry, |
| 1696 | * while the index2/blkno2 fields refer to the "new" entry. |
| 1697 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1698 | if (blk1->index > ichdr1.count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | ASSERT(state->inleaf == 0); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1700 | blk2->index = blk1->index - ichdr1.count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | args->index = args->index2 = blk2->index; |
| 1702 | args->blkno = args->blkno2 = blk2->blkno; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1703 | } else if (blk1->index == ichdr1.count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | if (state->inleaf) { |
| 1705 | args->index = blk1->index; |
| 1706 | args->blkno = blk1->blkno; |
| 1707 | args->index2 = 0; |
| 1708 | args->blkno2 = blk2->blkno; |
| 1709 | } else { |
Dave Chinner | 07428d7 | 2012-11-12 22:09:44 +1100 | [diff] [blame] | 1710 | /* |
| 1711 | * On a double leaf split, the original attr location |
| 1712 | * is already stored in blkno2/index2, so don't |
| 1713 | * overwrite it overwise we corrupt the tree. |
| 1714 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1715 | blk2->index = blk1->index - ichdr1.count; |
Dave Chinner | 07428d7 | 2012-11-12 22:09:44 +1100 | [diff] [blame] | 1716 | args->index = blk2->index; |
| 1717 | args->blkno = blk2->blkno; |
| 1718 | if (!state->extravalid) { |
| 1719 | /* |
| 1720 | * set the new attr location to match the old |
| 1721 | * one and let the higher level split code |
| 1722 | * decide where in the leaf to place it. |
| 1723 | */ |
| 1724 | args->index2 = blk2->index; |
| 1725 | args->blkno2 = blk2->blkno; |
| 1726 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | } |
| 1728 | } else { |
| 1729 | ASSERT(state->inleaf == 1); |
| 1730 | args->index = args->index2 = blk1->index; |
| 1731 | args->blkno = args->blkno2 = blk1->blkno; |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | /* |
| 1736 | * Examine entries until we reduce the absolute difference in |
| 1737 | * byte usage between the two blocks to a minimum. |
| 1738 | * GROT: Is this really necessary? With other than a 512 byte blocksize, |
| 1739 | * GROT: there will always be enough room in either block for a new entry. |
| 1740 | * GROT: Do a double-split for this case? |
| 1741 | */ |
| 1742 | STATIC int |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1743 | xfs_attr3_leaf_figure_balance( |
| 1744 | struct xfs_da_state *state, |
| 1745 | struct xfs_da_state_blk *blk1, |
| 1746 | struct xfs_attr3_icleaf_hdr *ichdr1, |
| 1747 | struct xfs_da_state_blk *blk2, |
| 1748 | struct xfs_attr3_icleaf_hdr *ichdr2, |
| 1749 | int *countarg, |
| 1750 | int *usedbytesarg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1752 | struct xfs_attr_leafblock *leaf1 = blk1->bp->b_addr; |
| 1753 | struct xfs_attr_leafblock *leaf2 = blk2->bp->b_addr; |
| 1754 | struct xfs_attr_leaf_entry *entry; |
| 1755 | int count; |
| 1756 | int max; |
| 1757 | int index; |
| 1758 | int totallen = 0; |
| 1759 | int half; |
| 1760 | int lastdelta; |
| 1761 | int foundit = 0; |
| 1762 | int tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | |
| 1764 | /* |
| 1765 | * Examine entries until we reduce the absolute difference in |
| 1766 | * byte usage between the two blocks to a minimum. |
| 1767 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1768 | max = ichdr1->count + ichdr2->count; |
| 1769 | half = (max + 1) * sizeof(*entry); |
| 1770 | half += ichdr1->usedbytes + ichdr2->usedbytes + |
Dave Chinner | c59f0ad | 2014-06-06 15:21:27 +1000 | [diff] [blame] | 1771 | xfs_attr_leaf_newentsize(state->args, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | half /= 2; |
Dave Chinner | b2a21e7 | 2014-06-06 15:22:04 +1000 | [diff] [blame] | 1773 | lastdelta = state->args->geo->blksize; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1774 | entry = xfs_attr3_leaf_entryp(leaf1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | for (count = index = 0; count < max; entry++, index++, count++) { |
| 1776 | |
| 1777 | #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A)) |
| 1778 | /* |
| 1779 | * The new entry is in the first block, account for it. |
| 1780 | */ |
| 1781 | if (count == blk1->index) { |
| 1782 | tmp = totallen + sizeof(*entry) + |
Dave Chinner | c59f0ad | 2014-06-06 15:21:27 +1000 | [diff] [blame] | 1783 | xfs_attr_leaf_newentsize(state->args, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | if (XFS_ATTR_ABS(half - tmp) > lastdelta) |
| 1785 | break; |
| 1786 | lastdelta = XFS_ATTR_ABS(half - tmp); |
| 1787 | totallen = tmp; |
| 1788 | foundit = 1; |
| 1789 | } |
| 1790 | |
| 1791 | /* |
| 1792 | * Wrap around into the second block if necessary. |
| 1793 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1794 | if (count == ichdr1->count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | leaf1 = leaf2; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1796 | entry = xfs_attr3_leaf_entryp(leaf1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | index = 0; |
| 1798 | } |
| 1799 | |
| 1800 | /* |
| 1801 | * Figure out if next leaf entry would be too much. |
| 1802 | */ |
| 1803 | tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1, |
| 1804 | index); |
| 1805 | if (XFS_ATTR_ABS(half - tmp) > lastdelta) |
| 1806 | break; |
| 1807 | lastdelta = XFS_ATTR_ABS(half - tmp); |
| 1808 | totallen = tmp; |
| 1809 | #undef XFS_ATTR_ABS |
| 1810 | } |
| 1811 | |
| 1812 | /* |
| 1813 | * Calculate the number of usedbytes that will end up in lower block. |
| 1814 | * If new entry not in lower block, fix up the count. |
| 1815 | */ |
| 1816 | totallen -= count * sizeof(*entry); |
| 1817 | if (foundit) { |
| 1818 | totallen -= sizeof(*entry) + |
Dave Chinner | c59f0ad | 2014-06-06 15:21:27 +1000 | [diff] [blame] | 1819 | xfs_attr_leaf_newentsize(state->args, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | *countarg = count; |
| 1823 | *usedbytesarg = totallen; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1824 | return foundit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | /*======================================================================== |
| 1828 | * Routines used for shrinking the Btree. |
| 1829 | *========================================================================*/ |
| 1830 | |
| 1831 | /* |
| 1832 | * Check a leaf block and its neighbors to see if the block should be |
| 1833 | * collapsed into one or the other neighbor. Always keep the block |
| 1834 | * with the smaller block number. |
| 1835 | * If the current block is over 50% full, don't try to join it, return 0. |
| 1836 | * If the block is empty, fill in the state structure and return 2. |
| 1837 | * If it can be collapsed, fill in the state structure and return 1. |
| 1838 | * If nothing can be done, return 0. |
| 1839 | * |
| 1840 | * GROT: allow for INCOMPLETE entries in calculation. |
| 1841 | */ |
| 1842 | int |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1843 | xfs_attr3_leaf_toosmall( |
| 1844 | struct xfs_da_state *state, |
| 1845 | int *action) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1847 | struct xfs_attr_leafblock *leaf; |
| 1848 | struct xfs_da_state_blk *blk; |
| 1849 | struct xfs_attr3_icleaf_hdr ichdr; |
| 1850 | struct xfs_buf *bp; |
| 1851 | xfs_dablk_t blkno; |
| 1852 | int bytes; |
| 1853 | int forward; |
| 1854 | int error; |
| 1855 | int retval; |
| 1856 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1858 | trace_xfs_attr_leaf_toosmall(state->args); |
| 1859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | /* |
| 1861 | * Check for the degenerate case of the block being over 50% full. |
| 1862 | * If so, it's not worth even looking to see if we might be able |
| 1863 | * to coalesce with a sibling. |
| 1864 | */ |
| 1865 | blk = &state->path.blk[ state->path.active-1 ]; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1866 | leaf = blk->bp->b_addr; |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 1867 | xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr, leaf); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1868 | bytes = xfs_attr3_leaf_hdr_size(leaf) + |
| 1869 | ichdr.count * sizeof(xfs_attr_leaf_entry_t) + |
| 1870 | ichdr.usedbytes; |
Dave Chinner | b2a21e7 | 2014-06-06 15:22:04 +1000 | [diff] [blame] | 1871 | if (bytes > (state->args->geo->blksize >> 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | *action = 0; /* blk over 50%, don't try to join */ |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1873 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | /* |
| 1877 | * Check for the degenerate case of the block being empty. |
| 1878 | * If the block is empty, we'll simply delete it, no need to |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1879 | * coalesce it with a sibling block. We choose (arbitrarily) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | * to merge with the forward block unless it is NULL. |
| 1881 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1882 | if (ichdr.count == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | /* |
| 1884 | * Make altpath point to the block we want to keep and |
| 1885 | * path point to the block we want to drop (this one). |
| 1886 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1887 | forward = (ichdr.forw != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | memcpy(&state->altpath, &state->path, sizeof(state->path)); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1889 | error = xfs_da3_path_shift(state, &state->altpath, forward, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | 0, &retval); |
| 1891 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1892 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | if (retval) { |
| 1894 | *action = 0; |
| 1895 | } else { |
| 1896 | *action = 2; |
| 1897 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1898 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | /* |
| 1902 | * Examine each sibling block to see if we can coalesce with |
| 1903 | * at least 25% free space to spare. We need to figure out |
| 1904 | * whether to merge with the forward or the backward block. |
| 1905 | * We prefer coalescing with the lower numbered sibling so as |
| 1906 | * to shrink an attribute list over time. |
| 1907 | */ |
| 1908 | /* start with smaller blk num */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1909 | forward = ichdr.forw < ichdr.back; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | for (i = 0; i < 2; forward = !forward, i++) { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1911 | struct xfs_attr3_icleaf_hdr ichdr2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | if (forward) |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1913 | blkno = ichdr.forw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | else |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1915 | blkno = ichdr.back; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | if (blkno == 0) |
| 1917 | continue; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1918 | error = xfs_attr3_leaf_read(state->args->trans, state->args->dp, |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 1919 | blkno, -1, &bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1921 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 1923 | xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, bp->b_addr); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1924 | |
Dave Chinner | b2a21e7 | 2014-06-06 15:22:04 +1000 | [diff] [blame] | 1925 | bytes = state->args->geo->blksize - |
| 1926 | (state->args->geo->blksize >> 2) - |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1927 | ichdr.usedbytes - ichdr2.usedbytes - |
| 1928 | ((ichdr.count + ichdr2.count) * |
| 1929 | sizeof(xfs_attr_leaf_entry_t)) - |
| 1930 | xfs_attr3_leaf_hdr_size(leaf); |
| 1931 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1932 | xfs_trans_brelse(state->args->trans, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | if (bytes >= 0) |
| 1934 | break; /* fits with at least 25% to spare */ |
| 1935 | } |
| 1936 | if (i >= 2) { |
| 1937 | *action = 0; |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1938 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | /* |
| 1942 | * Make altpath point to the block we want to keep (the lower |
| 1943 | * numbered block) and path point to the block we want to drop. |
| 1944 | */ |
| 1945 | memcpy(&state->altpath, &state->path, sizeof(state->path)); |
| 1946 | if (blkno < blk->blkno) { |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1947 | error = xfs_da3_path_shift(state, &state->altpath, forward, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | 0, &retval); |
| 1949 | } else { |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1950 | error = xfs_da3_path_shift(state, &state->path, forward, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | 0, &retval); |
| 1952 | } |
| 1953 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1954 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | if (retval) { |
| 1956 | *action = 0; |
| 1957 | } else { |
| 1958 | *action = 1; |
| 1959 | } |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1960 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | /* |
| 1964 | * Remove a name from the leaf attribute list structure. |
| 1965 | * |
| 1966 | * Return 1 if leaf is less than 37% full, 0 if >= 37% full. |
| 1967 | * If two leaves are 37% full, when combined they will leave 25% free. |
| 1968 | */ |
| 1969 | int |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1970 | xfs_attr3_leaf_remove( |
| 1971 | struct xfs_buf *bp, |
| 1972 | struct xfs_da_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1974 | struct xfs_attr_leafblock *leaf; |
| 1975 | struct xfs_attr3_icleaf_hdr ichdr; |
| 1976 | struct xfs_attr_leaf_entry *entry; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1977 | int before; |
| 1978 | int after; |
| 1979 | int smallest; |
| 1980 | int entsize; |
| 1981 | int tablesize; |
| 1982 | int tmp; |
| 1983 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1985 | trace_xfs_attr_leaf_remove(args); |
| 1986 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1987 | leaf = bp->b_addr; |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 1988 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1989 | |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1990 | ASSERT(ichdr.count > 0 && ichdr.count < args->geo->blksize / 8); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1991 | ASSERT(args->index >= 0 && args->index < ichdr.count); |
| 1992 | ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) + |
| 1993 | xfs_attr3_leaf_hdr_size(leaf)); |
| 1994 | |
| 1995 | entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; |
| 1996 | |
| 1997 | ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused); |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1998 | ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | |
| 2000 | /* |
| 2001 | * Scan through free region table: |
| 2002 | * check for adjacency of free'd entry with an existing one, |
| 2003 | * find smallest free region in case we need to replace it, |
| 2004 | * adjust any map that borders the entry table, |
| 2005 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2006 | tablesize = ichdr.count * sizeof(xfs_attr_leaf_entry_t) |
| 2007 | + xfs_attr3_leaf_hdr_size(leaf); |
| 2008 | tmp = ichdr.freemap[0].size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | before = after = -1; |
| 2010 | smallest = XFS_ATTR_LEAF_MAPSIZE - 1; |
| 2011 | entsize = xfs_attr_leaf_entsize(leaf, args->index); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2012 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2013 | ASSERT(ichdr.freemap[i].base < args->geo->blksize); |
| 2014 | ASSERT(ichdr.freemap[i].size < args->geo->blksize); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2015 | if (ichdr.freemap[i].base == tablesize) { |
| 2016 | ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t); |
| 2017 | ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | } |
| 2019 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2020 | if (ichdr.freemap[i].base + ichdr.freemap[i].size == |
| 2021 | be16_to_cpu(entry->nameidx)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | before = i; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2023 | } else if (ichdr.freemap[i].base == |
| 2024 | (be16_to_cpu(entry->nameidx) + entsize)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | after = i; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2026 | } else if (ichdr.freemap[i].size < tmp) { |
| 2027 | tmp = ichdr.freemap[i].size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | smallest = i; |
| 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | /* |
| 2033 | * Coalesce adjacent freemap regions, |
| 2034 | * or replace the smallest region. |
| 2035 | */ |
| 2036 | if ((before >= 0) || (after >= 0)) { |
| 2037 | if ((before >= 0) && (after >= 0)) { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2038 | ichdr.freemap[before].size += entsize; |
| 2039 | ichdr.freemap[before].size += ichdr.freemap[after].size; |
| 2040 | ichdr.freemap[after].base = 0; |
| 2041 | ichdr.freemap[after].size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | } else if (before >= 0) { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2043 | ichdr.freemap[before].size += entsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | } else { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2045 | ichdr.freemap[after].base = be16_to_cpu(entry->nameidx); |
| 2046 | ichdr.freemap[after].size += entsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | } |
| 2048 | } else { |
| 2049 | /* |
| 2050 | * Replace smallest region (if it is smaller than free'd entry) |
| 2051 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2052 | if (ichdr.freemap[smallest].size < entsize) { |
| 2053 | ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx); |
| 2054 | ichdr.freemap[smallest].size = entsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | } |
| 2056 | } |
| 2057 | |
| 2058 | /* |
| 2059 | * Did we remove the first entry? |
| 2060 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2061 | if (be16_to_cpu(entry->nameidx) == ichdr.firstused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | smallest = 1; |
| 2063 | else |
| 2064 | smallest = 0; |
| 2065 | |
| 2066 | /* |
| 2067 | * Compress the remaining entries and zero out the removed stuff. |
| 2068 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2069 | memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize); |
| 2070 | ichdr.usedbytes -= entsize; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2071 | xfs_trans_log_buf(args->trans, bp, |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2072 | XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | entsize)); |
| 2074 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2075 | tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t); |
| 2076 | memmove(entry, entry + 1, tmp); |
| 2077 | ichdr.count--; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2078 | xfs_trans_log_buf(args->trans, bp, |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2079 | XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t))); |
| 2080 | |
| 2081 | entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count]; |
| 2082 | memset(entry, 0, sizeof(xfs_attr_leaf_entry_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | |
| 2084 | /* |
| 2085 | * If we removed the first entry, re-find the first used byte |
| 2086 | * in the name area. Note that if the entry was the "firstused", |
| 2087 | * then we don't have a "hole" in our block resulting from |
| 2088 | * removing the name. |
| 2089 | */ |
| 2090 | if (smallest) { |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2091 | tmp = args->geo->blksize; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2092 | entry = xfs_attr3_leaf_entryp(leaf); |
| 2093 | for (i = ichdr.count - 1; i >= 0; entry++, i--) { |
| 2094 | ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused); |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2095 | ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize); |
Nathan Scott | 6b19f2d | 2006-03-17 17:29:02 +1100 | [diff] [blame] | 2096 | |
| 2097 | if (be16_to_cpu(entry->nameidx) < tmp) |
| 2098 | tmp = be16_to_cpu(entry->nameidx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2100 | ichdr.firstused = tmp; |
Brian Foster | 66db810 | 2015-04-13 11:27:59 +1000 | [diff] [blame] | 2101 | ASSERT(ichdr.firstused != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | } else { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2103 | ichdr.holes = 1; /* mark as needing compaction */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | } |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 2105 | xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2106 | xfs_trans_log_buf(args->trans, bp, |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2107 | XFS_DA_LOGRANGE(leaf, &leaf->hdr, |
| 2108 | xfs_attr3_leaf_hdr_size(leaf))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | |
| 2110 | /* |
| 2111 | * Check if leaf is less than 50% full, caller may want to |
| 2112 | * "join" the leaf with a sibling if so. |
| 2113 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2114 | tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) + |
| 2115 | ichdr.count * sizeof(xfs_attr_leaf_entry_t); |
| 2116 | |
Dave Chinner | ed358c0 | 2014-06-06 15:18:10 +1000 | [diff] [blame] | 2117 | return tmp < args->geo->magicpct; /* leaf is < 37% full */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | } |
| 2119 | |
| 2120 | /* |
| 2121 | * Move all the attribute list entries from drop_leaf into save_leaf. |
| 2122 | */ |
| 2123 | void |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2124 | xfs_attr3_leaf_unbalance( |
| 2125 | struct xfs_da_state *state, |
| 2126 | struct xfs_da_state_blk *drop_blk, |
| 2127 | struct xfs_da_state_blk *save_blk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2129 | struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr; |
| 2130 | struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr; |
| 2131 | struct xfs_attr3_icleaf_hdr drophdr; |
| 2132 | struct xfs_attr3_icleaf_hdr savehdr; |
| 2133 | struct xfs_attr_leaf_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 2135 | trace_xfs_attr_leaf_unbalance(state->args); |
| 2136 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2137 | drop_leaf = drop_blk->bp->b_addr; |
| 2138 | save_leaf = save_blk->bp->b_addr; |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 2139 | xfs_attr3_leaf_hdr_from_disk(state->args->geo, &drophdr, drop_leaf); |
| 2140 | xfs_attr3_leaf_hdr_from_disk(state->args->geo, &savehdr, save_leaf); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2141 | entry = xfs_attr3_leaf_entryp(drop_leaf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | |
| 2143 | /* |
| 2144 | * Save last hashval from dying block for later Btree fixup. |
| 2145 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2146 | drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | |
| 2148 | /* |
| 2149 | * Check if we need a temp buffer, or can we do it in place. |
| 2150 | * Note that we don't check "leaf" for holes because we will |
| 2151 | * always be dropping it, toosmall() decided that for us already. |
| 2152 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2153 | if (savehdr.holes == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | /* |
| 2155 | * dest leaf has no holes, so we add there. May need |
| 2156 | * to make some room in the entry array. |
| 2157 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2158 | if (xfs_attr3_leaf_order(save_blk->bp, &savehdr, |
| 2159 | drop_blk->bp, &drophdr)) { |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2160 | xfs_attr3_leaf_moveents(state->args, |
| 2161 | drop_leaf, &drophdr, 0, |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2162 | save_leaf, &savehdr, 0, |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2163 | drophdr.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | } else { |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2165 | xfs_attr3_leaf_moveents(state->args, |
| 2166 | drop_leaf, &drophdr, 0, |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2167 | save_leaf, &savehdr, |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2168 | savehdr.count, drophdr.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | } |
| 2170 | } else { |
| 2171 | /* |
| 2172 | * Destination has holes, so we make a temporary copy |
| 2173 | * of the leaf and add them both to that. |
| 2174 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2175 | struct xfs_attr_leafblock *tmp_leaf; |
| 2176 | struct xfs_attr3_icleaf_hdr tmphdr; |
| 2177 | |
Dave Chinner | b2a21e7 | 2014-06-06 15:22:04 +1000 | [diff] [blame] | 2178 | tmp_leaf = kmem_zalloc(state->args->geo->blksize, KM_SLEEP); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2179 | |
Dave Chinner | 8517de2 | 2013-05-21 18:02:05 +1000 | [diff] [blame] | 2180 | /* |
| 2181 | * Copy the header into the temp leaf so that all the stuff |
| 2182 | * not in the incore header is present and gets copied back in |
| 2183 | * once we've moved all the entries. |
| 2184 | */ |
| 2185 | memcpy(tmp_leaf, save_leaf, xfs_attr3_leaf_hdr_size(save_leaf)); |
| 2186 | |
| 2187 | memset(&tmphdr, 0, sizeof(tmphdr)); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2188 | tmphdr.magic = savehdr.magic; |
| 2189 | tmphdr.forw = savehdr.forw; |
| 2190 | tmphdr.back = savehdr.back; |
Dave Chinner | b2a21e7 | 2014-06-06 15:22:04 +1000 | [diff] [blame] | 2191 | tmphdr.firstused = state->args->geo->blksize; |
Dave Chinner | 8517de2 | 2013-05-21 18:02:05 +1000 | [diff] [blame] | 2192 | |
| 2193 | /* write the header to the temp buffer to initialise it */ |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 2194 | xfs_attr3_leaf_hdr_to_disk(state->args->geo, tmp_leaf, &tmphdr); |
Dave Chinner | 8517de2 | 2013-05-21 18:02:05 +1000 | [diff] [blame] | 2195 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2196 | if (xfs_attr3_leaf_order(save_blk->bp, &savehdr, |
| 2197 | drop_blk->bp, &drophdr)) { |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2198 | xfs_attr3_leaf_moveents(state->args, |
| 2199 | drop_leaf, &drophdr, 0, |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2200 | tmp_leaf, &tmphdr, 0, |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2201 | drophdr.count); |
| 2202 | xfs_attr3_leaf_moveents(state->args, |
| 2203 | save_leaf, &savehdr, 0, |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2204 | tmp_leaf, &tmphdr, tmphdr.count, |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2205 | savehdr.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | } else { |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2207 | xfs_attr3_leaf_moveents(state->args, |
| 2208 | save_leaf, &savehdr, 0, |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2209 | tmp_leaf, &tmphdr, 0, |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2210 | savehdr.count); |
| 2211 | xfs_attr3_leaf_moveents(state->args, |
| 2212 | drop_leaf, &drophdr, 0, |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2213 | tmp_leaf, &tmphdr, tmphdr.count, |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2214 | drophdr.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | } |
Dave Chinner | b2a21e7 | 2014-06-06 15:22:04 +1000 | [diff] [blame] | 2216 | memcpy(save_leaf, tmp_leaf, state->args->geo->blksize); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2217 | savehdr = tmphdr; /* struct copy */ |
| 2218 | kmem_free(tmp_leaf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | } |
| 2220 | |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 2221 | xfs_attr3_leaf_hdr_to_disk(state->args->geo, save_leaf, &savehdr); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2222 | xfs_trans_log_buf(state->args->trans, save_blk->bp, 0, |
Dave Chinner | b2a21e7 | 2014-06-06 15:22:04 +1000 | [diff] [blame] | 2223 | state->args->geo->blksize - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | |
| 2225 | /* |
| 2226 | * Copy out last hashval in each block for B-tree code. |
| 2227 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2228 | entry = xfs_attr3_leaf_entryp(save_leaf); |
| 2229 | save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | /*======================================================================== |
| 2233 | * Routines used for finding things in the Btree. |
| 2234 | *========================================================================*/ |
| 2235 | |
| 2236 | /* |
| 2237 | * Look up a name in a leaf attribute list structure. |
| 2238 | * This is the internal routine, it uses the caller's buffer. |
| 2239 | * |
| 2240 | * Note that duplicate keys are allowed, but only check within the |
| 2241 | * current leaf node. The Btree code must check in adjacent leaf nodes. |
| 2242 | * |
| 2243 | * Return in args->index the index into the entry[] array of either |
| 2244 | * the found entry, or where the entry should have been (insert before |
| 2245 | * that entry). |
| 2246 | * |
| 2247 | * Don't change the args->value unless we find the attribute. |
| 2248 | */ |
| 2249 | int |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2250 | xfs_attr3_leaf_lookup_int( |
| 2251 | struct xfs_buf *bp, |
| 2252 | struct xfs_da_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2254 | struct xfs_attr_leafblock *leaf; |
| 2255 | struct xfs_attr3_icleaf_hdr ichdr; |
| 2256 | struct xfs_attr_leaf_entry *entry; |
| 2257 | struct xfs_attr_leaf_entry *entries; |
| 2258 | struct xfs_attr_leaf_name_local *name_loc; |
| 2259 | struct xfs_attr_leaf_name_remote *name_rmt; |
| 2260 | xfs_dahash_t hashval; |
| 2261 | int probe; |
| 2262 | int span; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 2264 | trace_xfs_attr_leaf_lookup(args); |
| 2265 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2266 | leaf = bp->b_addr; |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 2267 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2268 | entries = xfs_attr3_leaf_entryp(leaf); |
Darrick J. Wong | 8ba92d4 | 2018-01-08 10:51:07 -0800 | [diff] [blame] | 2269 | if (ichdr.count >= args->geo->blksize / 8) |
| 2270 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | |
| 2272 | /* |
| 2273 | * Binary search. (note: small blocks will skip this loop) |
| 2274 | */ |
| 2275 | hashval = args->hashval; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2276 | probe = span = ichdr.count / 2; |
| 2277 | for (entry = &entries[probe]; span > 4; entry = &entries[probe]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | span /= 2; |
Nathan Scott | 6b19f2d | 2006-03-17 17:29:02 +1100 | [diff] [blame] | 2279 | if (be32_to_cpu(entry->hashval) < hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | probe += span; |
Nathan Scott | 6b19f2d | 2006-03-17 17:29:02 +1100 | [diff] [blame] | 2281 | else if (be32_to_cpu(entry->hashval) > hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | probe -= span; |
| 2283 | else |
| 2284 | break; |
| 2285 | } |
Darrick J. Wong | 8ba92d4 | 2018-01-08 10:51:07 -0800 | [diff] [blame] | 2286 | if (!(probe >= 0 && (!ichdr.count || probe < ichdr.count))) |
| 2287 | return -EFSCORRUPTED; |
| 2288 | if (!(span <= 4 || be32_to_cpu(entry->hashval) == hashval)) |
| 2289 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | |
| 2291 | /* |
| 2292 | * Since we may have duplicate hashval's, find the first matching |
| 2293 | * hashval in the leaf. |
| 2294 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2295 | while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | entry--; |
| 2297 | probe--; |
| 2298 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2299 | while (probe < ichdr.count && |
| 2300 | be32_to_cpu(entry->hashval) < hashval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | entry++; |
| 2302 | probe++; |
| 2303 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2304 | if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | args->index = probe; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2306 | return -ENOATTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | /* |
| 2310 | * Duplicate keys may be present, so search all of them for a match. |
| 2311 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2312 | for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | entry++, probe++) { |
| 2314 | /* |
| 2315 | * GROT: Add code to remove incomplete entries. |
| 2316 | */ |
| 2317 | /* |
| 2318 | * If we are looking for INCOMPLETE entries, show only those. |
| 2319 | * If we are looking for complete entries, show only those. |
| 2320 | */ |
| 2321 | if ((args->flags & XFS_ATTR_INCOMPLETE) != |
| 2322 | (entry->flags & XFS_ATTR_INCOMPLETE)) { |
| 2323 | continue; |
| 2324 | } |
| 2325 | if (entry->flags & XFS_ATTR_LOCAL) { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2326 | name_loc = xfs_attr3_leaf_name_local(leaf, probe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | if (name_loc->namelen != args->namelen) |
| 2328 | continue; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2329 | if (memcmp(args->name, name_loc->nameval, |
| 2330 | args->namelen) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | continue; |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 2332 | if (!xfs_attr_namesp_match(args->flags, entry->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | continue; |
| 2334 | args->index = probe; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2335 | return -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | } else { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2337 | name_rmt = xfs_attr3_leaf_name_remote(leaf, probe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | if (name_rmt->namelen != args->namelen) |
| 2339 | continue; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2340 | if (memcmp(args->name, name_rmt->name, |
| 2341 | args->namelen) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | continue; |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 2343 | if (!xfs_attr_namesp_match(args->flags, entry->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | continue; |
| 2345 | args->index = probe; |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 2346 | args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen); |
Nathan Scott | c0f054e | 2006-03-17 17:29:18 +1100 | [diff] [blame] | 2347 | args->rmtblkno = be32_to_cpu(name_rmt->valueblk); |
Dave Chinner | ad1858d | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 2348 | args->rmtblkcnt = xfs_attr3_rmt_blocks( |
| 2349 | args->dp->i_mount, |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 2350 | args->rmtvaluelen); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2351 | return -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | } |
| 2353 | } |
| 2354 | args->index = probe; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2355 | return -ENOATTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | /* |
| 2359 | * Get the value associated with an attribute name from a leaf attribute |
| 2360 | * list structure. |
| 2361 | */ |
| 2362 | int |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2363 | xfs_attr3_leaf_getvalue( |
| 2364 | struct xfs_buf *bp, |
| 2365 | struct xfs_da_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2367 | struct xfs_attr_leafblock *leaf; |
| 2368 | struct xfs_attr3_icleaf_hdr ichdr; |
| 2369 | struct xfs_attr_leaf_entry *entry; |
| 2370 | struct xfs_attr_leaf_name_local *name_loc; |
| 2371 | struct xfs_attr_leaf_name_remote *name_rmt; |
| 2372 | int valuelen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2374 | leaf = bp->b_addr; |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 2375 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2376 | ASSERT(ichdr.count < args->geo->blksize / 8); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2377 | ASSERT(args->index < ichdr.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2379 | entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | if (entry->flags & XFS_ATTR_LOCAL) { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2381 | name_loc = xfs_attr3_leaf_name_local(leaf, args->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | ASSERT(name_loc->namelen == args->namelen); |
| 2383 | ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0); |
Nathan Scott | 053b575 | 2006-03-17 17:29:09 +1100 | [diff] [blame] | 2384 | valuelen = be16_to_cpu(name_loc->valuelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | if (args->flags & ATTR_KERNOVAL) { |
| 2386 | args->valuelen = valuelen; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2387 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | } |
| 2389 | if (args->valuelen < valuelen) { |
| 2390 | args->valuelen = valuelen; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2391 | return -ERANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | } |
| 2393 | args->valuelen = valuelen; |
| 2394 | memcpy(args->value, &name_loc->nameval[args->namelen], valuelen); |
| 2395 | } else { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2396 | name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | ASSERT(name_rmt->namelen == args->namelen); |
| 2398 | ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0); |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 2399 | args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen); |
Nathan Scott | c0f054e | 2006-03-17 17:29:18 +1100 | [diff] [blame] | 2400 | args->rmtblkno = be32_to_cpu(name_rmt->valueblk); |
Dave Chinner | ad1858d | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 2401 | args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount, |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 2402 | args->rmtvaluelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | if (args->flags & ATTR_KERNOVAL) { |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 2404 | args->valuelen = args->rmtvaluelen; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2405 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | } |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 2407 | if (args->valuelen < args->rmtvaluelen) { |
| 2408 | args->valuelen = args->rmtvaluelen; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2409 | return -ERANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | } |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 2411 | args->valuelen = args->rmtvaluelen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2413 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | } |
| 2415 | |
| 2416 | /*======================================================================== |
| 2417 | * Utility routines. |
| 2418 | *========================================================================*/ |
| 2419 | |
| 2420 | /* |
| 2421 | * Move the indicated entries from one leaf to another. |
| 2422 | * NOTE: this routine modifies both source and destination leaves. |
| 2423 | */ |
| 2424 | /*ARGSUSED*/ |
| 2425 | STATIC void |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2426 | xfs_attr3_leaf_moveents( |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2427 | struct xfs_da_args *args, |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2428 | struct xfs_attr_leafblock *leaf_s, |
| 2429 | struct xfs_attr3_icleaf_hdr *ichdr_s, |
| 2430 | int start_s, |
| 2431 | struct xfs_attr_leafblock *leaf_d, |
| 2432 | struct xfs_attr3_icleaf_hdr *ichdr_d, |
| 2433 | int start_d, |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2434 | int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2436 | struct xfs_attr_leaf_entry *entry_s; |
| 2437 | struct xfs_attr_leaf_entry *entry_d; |
| 2438 | int desti; |
| 2439 | int tmp; |
| 2440 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | |
| 2442 | /* |
| 2443 | * Check for nothing to do. |
| 2444 | */ |
| 2445 | if (count == 0) |
| 2446 | return; |
| 2447 | |
| 2448 | /* |
| 2449 | * Set up environment. |
| 2450 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2451 | ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC || |
| 2452 | ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC); |
| 2453 | ASSERT(ichdr_s->magic == ichdr_d->magic); |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2454 | ASSERT(ichdr_s->count > 0 && ichdr_s->count < args->geo->blksize / 8); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2455 | ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s)) |
| 2456 | + xfs_attr3_leaf_hdr_size(leaf_s)); |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2457 | ASSERT(ichdr_d->count < args->geo->blksize / 8); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2458 | ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d)) |
| 2459 | + xfs_attr3_leaf_hdr_size(leaf_d)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2461 | ASSERT(start_s < ichdr_s->count); |
| 2462 | ASSERT(start_d <= ichdr_d->count); |
| 2463 | ASSERT(count <= ichdr_s->count); |
| 2464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | |
| 2466 | /* |
| 2467 | * Move the entries in the destination leaf up to make a hole? |
| 2468 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2469 | if (start_d < ichdr_d->count) { |
| 2470 | tmp = ichdr_d->count - start_d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | tmp *= sizeof(xfs_attr_leaf_entry_t); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2472 | entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d]; |
| 2473 | entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count]; |
| 2474 | memmove(entry_d, entry_s, tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | /* |
| 2478 | * Copy all entry's in the same (sorted) order, |
| 2479 | * but allocate attribute info packed and in sequence. |
| 2480 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2481 | entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s]; |
| 2482 | entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | desti = start_d; |
| 2484 | for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2485 | ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i); |
| 2487 | #ifdef GROT |
| 2488 | /* |
| 2489 | * Code to drop INCOMPLETE entries. Difficult to use as we |
| 2490 | * may also need to change the insertion index. Code turned |
| 2491 | * off for 6.2, should be revisited later. |
| 2492 | */ |
| 2493 | if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2494 | memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp); |
| 2495 | ichdr_s->usedbytes -= tmp; |
| 2496 | ichdr_s->count -= 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | entry_d--; /* to compensate for ++ in loop hdr */ |
| 2498 | desti--; |
| 2499 | if ((start_s + i) < offset) |
| 2500 | result++; /* insertion index adjustment */ |
| 2501 | } else { |
| 2502 | #endif /* GROT */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2503 | ichdr_d->firstused -= tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | /* both on-disk, don't endian flip twice */ |
| 2505 | entry_d->hashval = entry_s->hashval; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2506 | entry_d->nameidx = cpu_to_be16(ichdr_d->firstused); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | entry_d->flags = entry_s->flags; |
Nathan Scott | 6b19f2d | 2006-03-17 17:29:02 +1100 | [diff] [blame] | 2508 | ASSERT(be16_to_cpu(entry_d->nameidx) + tmp |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2509 | <= args->geo->blksize); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2510 | memmove(xfs_attr3_leaf_name(leaf_d, desti), |
| 2511 | xfs_attr3_leaf_name(leaf_s, start_s + i), tmp); |
Nathan Scott | 6b19f2d | 2006-03-17 17:29:02 +1100 | [diff] [blame] | 2512 | ASSERT(be16_to_cpu(entry_s->nameidx) + tmp |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2513 | <= args->geo->blksize); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2514 | memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp); |
| 2515 | ichdr_s->usedbytes -= tmp; |
| 2516 | ichdr_d->usedbytes += tmp; |
| 2517 | ichdr_s->count -= 1; |
| 2518 | ichdr_d->count += 1; |
| 2519 | tmp = ichdr_d->count * sizeof(xfs_attr_leaf_entry_t) |
| 2520 | + xfs_attr3_leaf_hdr_size(leaf_d); |
| 2521 | ASSERT(ichdr_d->firstused >= tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | #ifdef GROT |
| 2523 | } |
| 2524 | #endif /* GROT */ |
| 2525 | } |
| 2526 | |
| 2527 | /* |
| 2528 | * Zero out the entries we just copied. |
| 2529 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2530 | if (start_s == ichdr_s->count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | tmp = count * sizeof(xfs_attr_leaf_entry_t); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2532 | entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | ASSERT(((char *)entry_s + tmp) <= |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2534 | ((char *)leaf_s + args->geo->blksize)); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2535 | memset(entry_s, 0, tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | } else { |
| 2537 | /* |
| 2538 | * Move the remaining entries down to fill the hole, |
| 2539 | * then zero the entries at the top. |
| 2540 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2541 | tmp = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t); |
| 2542 | entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count]; |
| 2543 | entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s]; |
| 2544 | memmove(entry_d, entry_s, tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | |
| 2546 | tmp = count * sizeof(xfs_attr_leaf_entry_t); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2547 | entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | ASSERT(((char *)entry_s + tmp) <= |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 2549 | ((char *)leaf_s + args->geo->blksize)); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2550 | memset(entry_s, 0, tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | /* |
| 2554 | * Fill in the freemap information |
| 2555 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2556 | ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_d); |
| 2557 | ichdr_d->freemap[0].base += ichdr_d->count * sizeof(xfs_attr_leaf_entry_t); |
| 2558 | ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base; |
| 2559 | ichdr_d->freemap[1].base = 0; |
| 2560 | ichdr_d->freemap[2].base = 0; |
| 2561 | ichdr_d->freemap[1].size = 0; |
| 2562 | ichdr_d->freemap[2].size = 0; |
| 2563 | ichdr_s->holes = 1; /* leaf may not be compact */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | } |
| 2565 | |
| 2566 | /* |
| 2567 | * Pick up the last hashvalue from a leaf block. |
| 2568 | */ |
| 2569 | xfs_dahash_t |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2570 | xfs_attr_leaf_lasthash( |
| 2571 | struct xfs_buf *bp, |
| 2572 | int *count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2574 | struct xfs_attr3_icleaf_hdr ichdr; |
| 2575 | struct xfs_attr_leaf_entry *entries; |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 2576 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 2578 | xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, bp->b_addr); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2579 | entries = xfs_attr3_leaf_entryp(bp->b_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | if (count) |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2581 | *count = ichdr.count; |
| 2582 | if (!ichdr.count) |
| 2583 | return 0; |
| 2584 | return be32_to_cpu(entries[ichdr.count - 1].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | /* |
| 2588 | * Calculate the number of bytes used to store the indicated attribute |
| 2589 | * (whether local or remote only calculate bytes in this block). |
| 2590 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 2591 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index) |
| 2593 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2594 | struct xfs_attr_leaf_entry *entries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | xfs_attr_leaf_name_local_t *name_loc; |
| 2596 | xfs_attr_leaf_name_remote_t *name_rmt; |
| 2597 | int size; |
| 2598 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2599 | entries = xfs_attr3_leaf_entryp(leaf); |
| 2600 | if (entries[index].flags & XFS_ATTR_LOCAL) { |
| 2601 | name_loc = xfs_attr3_leaf_name_local(leaf, index); |
Eric Sandeen | c9fb86a | 2009-01-01 16:40:11 -0600 | [diff] [blame] | 2602 | size = xfs_attr_leaf_entsize_local(name_loc->namelen, |
Nathan Scott | 053b575 | 2006-03-17 17:29:09 +1100 | [diff] [blame] | 2603 | be16_to_cpu(name_loc->valuelen)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2604 | } else { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2605 | name_rmt = xfs_attr3_leaf_name_remote(leaf, index); |
Eric Sandeen | c9fb86a | 2009-01-01 16:40:11 -0600 | [diff] [blame] | 2606 | size = xfs_attr_leaf_entsize_remote(name_rmt->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2608 | return size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | /* |
| 2612 | * Calculate the number of bytes that would be required to store the new |
| 2613 | * attribute (whether local or remote only calculate bytes in this block). |
| 2614 | * This routine decides as a side effect whether the attribute will be |
| 2615 | * a "local" or a "remote" attribute. |
| 2616 | */ |
| 2617 | int |
Dave Chinner | c59f0ad | 2014-06-06 15:21:27 +1000 | [diff] [blame] | 2618 | xfs_attr_leaf_newentsize( |
| 2619 | struct xfs_da_args *args, |
| 2620 | int *local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | { |
Dave Chinner | c59f0ad | 2014-06-06 15:21:27 +1000 | [diff] [blame] | 2622 | int size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | |
Dave Chinner | c59f0ad | 2014-06-06 15:21:27 +1000 | [diff] [blame] | 2624 | size = xfs_attr_leaf_entsize_local(args->namelen, args->valuelen); |
| 2625 | if (size < xfs_attr_leaf_entsize_local_max(args->geo->blksize)) { |
| 2626 | if (local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | *local = 1; |
Dave Chinner | c59f0ad | 2014-06-06 15:21:27 +1000 | [diff] [blame] | 2628 | return size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | } |
Dave Chinner | c59f0ad | 2014-06-06 15:21:27 +1000 | [diff] [blame] | 2630 | if (local) |
| 2631 | *local = 0; |
| 2632 | return xfs_attr_leaf_entsize_remote(args->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | |
| 2636 | /*======================================================================== |
| 2637 | * Manage the INCOMPLETE flag in a leaf entry |
| 2638 | *========================================================================*/ |
| 2639 | |
| 2640 | /* |
| 2641 | * Clear the INCOMPLETE flag on an entry in a leaf block. |
| 2642 | */ |
| 2643 | int |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2644 | xfs_attr3_leaf_clearflag( |
| 2645 | struct xfs_da_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2647 | struct xfs_attr_leafblock *leaf; |
| 2648 | struct xfs_attr_leaf_entry *entry; |
| 2649 | struct xfs_attr_leaf_name_remote *name_rmt; |
| 2650 | struct xfs_buf *bp; |
| 2651 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | #ifdef DEBUG |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2653 | struct xfs_attr3_icleaf_hdr ichdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | xfs_attr_leaf_name_local_t *name_loc; |
| 2655 | int namelen; |
| 2656 | char *name; |
| 2657 | #endif /* DEBUG */ |
| 2658 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 2659 | trace_xfs_attr_leaf_clearflag(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | /* |
| 2661 | * Set up the operation. |
| 2662 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2663 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp); |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 2664 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 2665 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2667 | leaf = bp->b_addr; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2668 | entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | ASSERT(entry->flags & XFS_ATTR_INCOMPLETE); |
| 2670 | |
| 2671 | #ifdef DEBUG |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 2672 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2673 | ASSERT(args->index < ichdr.count); |
| 2674 | ASSERT(args->index >= 0); |
| 2675 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | if (entry->flags & XFS_ATTR_LOCAL) { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2677 | name_loc = xfs_attr3_leaf_name_local(leaf, args->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | namelen = name_loc->namelen; |
| 2679 | name = (char *)name_loc->nameval; |
| 2680 | } else { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2681 | name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | namelen = name_rmt->namelen; |
| 2683 | name = (char *)name_rmt->name; |
| 2684 | } |
Nathan Scott | 6b19f2d | 2006-03-17 17:29:02 +1100 | [diff] [blame] | 2685 | ASSERT(be32_to_cpu(entry->hashval) == args->hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | ASSERT(namelen == args->namelen); |
| 2687 | ASSERT(memcmp(name, args->name, namelen) == 0); |
| 2688 | #endif /* DEBUG */ |
| 2689 | |
| 2690 | entry->flags &= ~XFS_ATTR_INCOMPLETE; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2691 | xfs_trans_log_buf(args->trans, bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry))); |
| 2693 | |
| 2694 | if (args->rmtblkno) { |
| 2695 | ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2696 | name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); |
Nathan Scott | c0f054e | 2006-03-17 17:29:18 +1100 | [diff] [blame] | 2697 | name_rmt->valueblk = cpu_to_be32(args->rmtblkno); |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 2698 | name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2699 | xfs_trans_log_buf(args->trans, bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt))); |
| 2701 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | |
| 2703 | /* |
| 2704 | * Commit the flag value change and start the next trans in series. |
| 2705 | */ |
Christoph Hellwig | 411350d | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 2706 | return xfs_trans_roll_inode(&args->trans, args->dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | /* |
| 2710 | * Set the INCOMPLETE flag on an entry in a leaf block. |
| 2711 | */ |
| 2712 | int |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2713 | xfs_attr3_leaf_setflag( |
| 2714 | struct xfs_da_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2716 | struct xfs_attr_leafblock *leaf; |
| 2717 | struct xfs_attr_leaf_entry *entry; |
| 2718 | struct xfs_attr_leaf_name_remote *name_rmt; |
| 2719 | struct xfs_buf *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2720 | int error; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2721 | #ifdef DEBUG |
| 2722 | struct xfs_attr3_icleaf_hdr ichdr; |
| 2723 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 2725 | trace_xfs_attr_leaf_setflag(args); |
| 2726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | /* |
| 2728 | * Set up the operation. |
| 2729 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2730 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp); |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 2731 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 2732 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2734 | leaf = bp->b_addr; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2735 | #ifdef DEBUG |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 2736 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2737 | ASSERT(args->index < ichdr.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | ASSERT(args->index >= 0); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2739 | #endif |
| 2740 | entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2741 | |
| 2742 | ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0); |
| 2743 | entry->flags |= XFS_ATTR_INCOMPLETE; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2744 | xfs_trans_log_buf(args->trans, bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry))); |
| 2746 | if ((entry->flags & XFS_ATTR_LOCAL) == 0) { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2747 | name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | name_rmt->valueblk = 0; |
| 2749 | name_rmt->valuelen = 0; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2750 | xfs_trans_log_buf(args->trans, bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt))); |
| 2752 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | |
| 2754 | /* |
| 2755 | * Commit the flag value change and start the next trans in series. |
| 2756 | */ |
Christoph Hellwig | 411350d | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 2757 | return xfs_trans_roll_inode(&args->trans, args->dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | /* |
| 2761 | * In a single transaction, clear the INCOMPLETE flag on the leaf entry |
| 2762 | * given by args->blkno/index and set the INCOMPLETE flag on the leaf |
| 2763 | * entry given by args->blkno2/index2. |
| 2764 | * |
| 2765 | * Note that they could be in different blocks, or in the same block. |
| 2766 | */ |
| 2767 | int |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2768 | xfs_attr3_leaf_flipflags( |
| 2769 | struct xfs_da_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2771 | struct xfs_attr_leafblock *leaf1; |
| 2772 | struct xfs_attr_leafblock *leaf2; |
| 2773 | struct xfs_attr_leaf_entry *entry1; |
| 2774 | struct xfs_attr_leaf_entry *entry2; |
| 2775 | struct xfs_attr_leaf_name_remote *name_rmt; |
| 2776 | struct xfs_buf *bp1; |
| 2777 | struct xfs_buf *bp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | int error; |
| 2779 | #ifdef DEBUG |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2780 | struct xfs_attr3_icleaf_hdr ichdr1; |
| 2781 | struct xfs_attr3_icleaf_hdr ichdr2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | xfs_attr_leaf_name_local_t *name_loc; |
| 2783 | int namelen1, namelen2; |
| 2784 | char *name1, *name2; |
| 2785 | #endif /* DEBUG */ |
| 2786 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 2787 | trace_xfs_attr_leaf_flipflags(args); |
| 2788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | /* |
| 2790 | * Read the block containing the "old" attr |
| 2791 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2792 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1); |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 2793 | if (error) |
| 2794 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | |
| 2796 | /* |
| 2797 | * Read the block containing the "new" attr, if it is different |
| 2798 | */ |
| 2799 | if (args->blkno2 != args->blkno) { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2800 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno2, |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 2801 | -1, &bp2); |
| 2802 | if (error) |
| 2803 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | } else { |
| 2805 | bp2 = bp1; |
| 2806 | } |
| 2807 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2808 | leaf1 = bp1->b_addr; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2809 | entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2811 | leaf2 = bp2->b_addr; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2812 | entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | |
| 2814 | #ifdef DEBUG |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 2815 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr1, leaf1); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2816 | ASSERT(args->index < ichdr1.count); |
| 2817 | ASSERT(args->index >= 0); |
| 2818 | |
Brian Foster | 2f66124 | 2015-04-13 11:26:02 +1000 | [diff] [blame] | 2819 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr2, leaf2); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2820 | ASSERT(args->index2 < ichdr2.count); |
| 2821 | ASSERT(args->index2 >= 0); |
| 2822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | if (entry1->flags & XFS_ATTR_LOCAL) { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2824 | name_loc = xfs_attr3_leaf_name_local(leaf1, args->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | namelen1 = name_loc->namelen; |
| 2826 | name1 = (char *)name_loc->nameval; |
| 2827 | } else { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2828 | name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | namelen1 = name_rmt->namelen; |
| 2830 | name1 = (char *)name_rmt->name; |
| 2831 | } |
| 2832 | if (entry2->flags & XFS_ATTR_LOCAL) { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2833 | name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | namelen2 = name_loc->namelen; |
| 2835 | name2 = (char *)name_loc->nameval; |
| 2836 | } else { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2837 | name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2838 | namelen2 = name_rmt->namelen; |
| 2839 | name2 = (char *)name_rmt->name; |
| 2840 | } |
Nathan Scott | 6b19f2d | 2006-03-17 17:29:02 +1100 | [diff] [blame] | 2841 | ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | ASSERT(namelen1 == namelen2); |
| 2843 | ASSERT(memcmp(name1, name2, namelen1) == 0); |
| 2844 | #endif /* DEBUG */ |
| 2845 | |
| 2846 | ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE); |
| 2847 | ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0); |
| 2848 | |
| 2849 | entry1->flags &= ~XFS_ATTR_INCOMPLETE; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2850 | xfs_trans_log_buf(args->trans, bp1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1))); |
| 2852 | if (args->rmtblkno) { |
| 2853 | ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2854 | name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index); |
Nathan Scott | c0f054e | 2006-03-17 17:29:18 +1100 | [diff] [blame] | 2855 | name_rmt->valueblk = cpu_to_be32(args->rmtblkno); |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 2856 | name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2857 | xfs_trans_log_buf(args->trans, bp1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt))); |
| 2859 | } |
| 2860 | |
| 2861 | entry2->flags |= XFS_ATTR_INCOMPLETE; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2862 | xfs_trans_log_buf(args->trans, bp2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2))); |
| 2864 | if ((entry2->flags & XFS_ATTR_LOCAL) == 0) { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2865 | name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | name_rmt->valueblk = 0; |
| 2867 | name_rmt->valuelen = 0; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2868 | xfs_trans_log_buf(args->trans, bp2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt))); |
| 2870 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | |
| 2872 | /* |
| 2873 | * Commit the flag value change and start the next trans in series. |
| 2874 | */ |
Christoph Hellwig | 411350d | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 2875 | error = xfs_trans_roll_inode(&args->trans, args->dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 2877 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2878 | } |