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