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