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