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