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