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