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