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