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