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