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