| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
| 5 | */ |
| 6 | #include "xfs.h" |
| 7 | #include "xfs_fs.h" |
| 8 | #include "xfs_shared.h" |
| 9 | #include "xfs_format.h" |
| 10 | #include "xfs_log_format.h" |
| 11 | #include "xfs_trans_resv.h" |
| 12 | #include "xfs_mount.h" |
| 13 | #include "xfs_defer.h" |
| 14 | #include "xfs_da_format.h" |
| 15 | #include "xfs_da_btree.h" |
| 16 | #include "xfs_attr_sf.h" |
| 17 | #include "xfs_inode.h" |
| 18 | #include "xfs_trans.h" |
| 19 | #include "xfs_bmap.h" |
| 20 | #include "xfs_bmap_btree.h" |
| 21 | #include "xfs_attr.h" |
| 22 | #include "xfs_attr_leaf.h" |
| 23 | #include "xfs_attr_remote.h" |
| 24 | #include "xfs_quota.h" |
| 25 | #include "xfs_trans_space.h" |
| 26 | #include "xfs_trace.h" |
| 27 | #include "xfs_attr_item.h" |
| 28 | #include "xfs_xattr.h" |
| 29 | #include "xfs_parent.h" |
| 30 | |
| 31 | struct kmem_cache *xfs_attr_intent_cache; |
| 32 | |
| 33 | /* |
| 34 | * xfs_attr.c |
| 35 | * |
| 36 | * Provide the external interfaces to manage attribute lists. |
| 37 | */ |
| 38 | |
| 39 | /*======================================================================== |
| 40 | * Function prototypes for the kernel. |
| 41 | *========================================================================*/ |
| 42 | |
| 43 | /* |
| 44 | * Internal routines when attribute list fits inside the inode. |
| 45 | */ |
| 46 | STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args); |
| 47 | |
| 48 | /* |
| 49 | * Internal routines when attribute list is one block. |
| 50 | */ |
| 51 | STATIC int xfs_attr_leaf_get(xfs_da_args_t *args); |
| 52 | STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args); |
| 53 | STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp); |
| 54 | |
| 55 | /* |
| 56 | * Internal routines when attribute list is more than one block. |
| 57 | */ |
| 58 | STATIC int xfs_attr_node_get(xfs_da_args_t *args); |
| 59 | STATIC void xfs_attr_restore_rmt_blk(struct xfs_da_args *args); |
| 60 | static int xfs_attr_node_try_addname(struct xfs_attr_intent *attr); |
| 61 | STATIC int xfs_attr_node_addname_find_attr(struct xfs_attr_intent *attr); |
| 62 | STATIC int xfs_attr_node_remove_attr(struct xfs_attr_intent *attr); |
| 63 | STATIC int xfs_attr_node_lookup(struct xfs_da_args *args, |
| 64 | struct xfs_da_state *state); |
| 65 | |
| 66 | int |
| 67 | xfs_inode_hasattr( |
| 68 | struct xfs_inode *ip) |
| 69 | { |
| 70 | if (!xfs_inode_has_attr_fork(ip)) |
| 71 | return 0; |
| 72 | if (ip->i_af.if_format == XFS_DINODE_FMT_EXTENTS && |
| 73 | ip->i_af.if_nextents == 0) |
| 74 | return 0; |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Returns true if the there is exactly only block in the attr fork, in which |
| 80 | * case the attribute fork consists of a single leaf block entry. |
| 81 | */ |
| 82 | bool |
| 83 | xfs_attr_is_leaf( |
| 84 | struct xfs_inode *ip) |
| 85 | { |
| 86 | struct xfs_ifork *ifp = &ip->i_af; |
| 87 | struct xfs_iext_cursor icur; |
| 88 | struct xfs_bmbt_irec imap; |
| 89 | |
| 90 | ASSERT(!xfs_need_iread_extents(ifp)); |
| 91 | |
| 92 | if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS) |
| 93 | return false; |
| 94 | |
| 95 | xfs_iext_first(ifp, &icur); |
| 96 | xfs_iext_get_extent(ifp, &icur, &imap); |
| 97 | return imap.br_startoff == 0 && imap.br_blockcount == 1; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * XXX (dchinner): name path state saving and refilling is an optimisation to |
| 102 | * avoid needing to look up name entries after rolling transactions removing |
| 103 | * remote xattr blocks between the name entry lookup and name entry removal. |
| 104 | * This optimisation got sidelined when combining the set and remove state |
| 105 | * machines, but the code has been left in place because it is worthwhile to |
| 106 | * restore the optimisation once the combined state machine paths have settled. |
| 107 | * |
| 108 | * This comment is a public service announcement to remind Future Dave that he |
| 109 | * still needs to restore this code to working order. |
| 110 | */ |
| 111 | #if 0 |
| 112 | /* |
| 113 | * Fill in the disk block numbers in the state structure for the buffers |
| 114 | * that are attached to the state structure. |
| 115 | * This is done so that we can quickly reattach ourselves to those buffers |
| 116 | * after some set of transaction commits have released these buffers. |
| 117 | */ |
| 118 | static int |
| 119 | xfs_attr_fillstate(xfs_da_state_t *state) |
| 120 | { |
| 121 | xfs_da_state_path_t *path; |
| 122 | xfs_da_state_blk_t *blk; |
| 123 | int level; |
| 124 | |
| 125 | trace_xfs_attr_fillstate(state->args); |
| 126 | |
| 127 | /* |
| 128 | * Roll down the "path" in the state structure, storing the on-disk |
| 129 | * block number for those buffers in the "path". |
| 130 | */ |
| 131 | path = &state->path; |
| 132 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 133 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 134 | if (blk->bp) { |
| 135 | blk->disk_blkno = xfs_buf_daddr(blk->bp); |
| 136 | blk->bp = NULL; |
| 137 | } else { |
| 138 | blk->disk_blkno = 0; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Roll down the "altpath" in the state structure, storing the on-disk |
| 144 | * block number for those buffers in the "altpath". |
| 145 | */ |
| 146 | path = &state->altpath; |
| 147 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 148 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 149 | if (blk->bp) { |
| 150 | blk->disk_blkno = xfs_buf_daddr(blk->bp); |
| 151 | blk->bp = NULL; |
| 152 | } else { |
| 153 | blk->disk_blkno = 0; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Reattach the buffers to the state structure based on the disk block |
| 162 | * numbers stored in the state structure. |
| 163 | * This is done after some set of transaction commits have released those |
| 164 | * buffers from our grip. |
| 165 | */ |
| 166 | static int |
| 167 | xfs_attr_refillstate(xfs_da_state_t *state) |
| 168 | { |
| 169 | xfs_da_state_path_t *path; |
| 170 | xfs_da_state_blk_t *blk; |
| 171 | int level, error; |
| 172 | |
| 173 | trace_xfs_attr_refillstate(state->args); |
| 174 | |
| 175 | /* |
| 176 | * Roll down the "path" in the state structure, storing the on-disk |
| 177 | * block number for those buffers in the "path". |
| 178 | */ |
| 179 | path = &state->path; |
| 180 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 181 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 182 | if (blk->disk_blkno) { |
| 183 | error = xfs_da3_node_read_mapped(state->args->trans, |
| 184 | state->args->dp, blk->disk_blkno, |
| 185 | &blk->bp, XFS_ATTR_FORK); |
| 186 | if (error) |
| 187 | return error; |
| 188 | } else { |
| 189 | blk->bp = NULL; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Roll down the "altpath" in the state structure, storing the on-disk |
| 195 | * block number for those buffers in the "altpath". |
| 196 | */ |
| 197 | path = &state->altpath; |
| 198 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 199 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 200 | if (blk->disk_blkno) { |
| 201 | error = xfs_da3_node_read_mapped(state->args->trans, |
| 202 | state->args->dp, blk->disk_blkno, |
| 203 | &blk->bp, XFS_ATTR_FORK); |
| 204 | if (error) |
| 205 | return error; |
| 206 | } else { |
| 207 | blk->bp = NULL; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | #else |
| 214 | static int xfs_attr_fillstate(xfs_da_state_t *state) { return 0; } |
| 215 | #endif |
| 216 | |
| 217 | /*======================================================================== |
| 218 | * Overall external interface routines. |
| 219 | *========================================================================*/ |
| 220 | |
| 221 | /* |
| 222 | * Retrieve an extended attribute and its value. Must have ilock. |
| 223 | * Returns 0 on successful retrieval, otherwise an error. |
| 224 | */ |
| 225 | int |
| 226 | xfs_attr_get_ilocked( |
| 227 | struct xfs_da_args *args) |
| 228 | { |
| 229 | int error; |
| 230 | |
| 231 | xfs_assert_ilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL); |
| 232 | |
| 233 | if (!xfs_inode_hasattr(args->dp)) |
| 234 | return -ENOATTR; |
| 235 | |
| 236 | /* |
| 237 | * The incore attr fork iext tree must be loaded for xfs_attr_is_leaf |
| 238 | * to work correctly. |
| 239 | */ |
| 240 | error = xfs_iread_extents(args->trans, args->dp, XFS_ATTR_FORK); |
| 241 | if (error) |
| 242 | return error; |
| 243 | |
| 244 | if (args->dp->i_af.if_format == XFS_DINODE_FMT_LOCAL) |
| 245 | return xfs_attr_shortform_getvalue(args); |
| 246 | if (xfs_attr_is_leaf(args->dp)) |
| 247 | return xfs_attr_leaf_get(args); |
| 248 | return xfs_attr_node_get(args); |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Retrieve an extended attribute by name, and its value if requested. |
| 253 | * |
| 254 | * If args->valuelen is zero, then the caller does not want the value, just an |
| 255 | * indication whether the attribute exists and the size of the value if it |
| 256 | * exists. The size is returned in args.valuelen. |
| 257 | * |
| 258 | * If args->value is NULL but args->valuelen is non-zero, allocate the buffer |
| 259 | * for the value after existence of the attribute has been determined. The |
| 260 | * caller always has to free args->value if it is set, no matter if this |
| 261 | * function was successful or not. |
| 262 | * |
| 263 | * If the attribute is found, but exceeds the size limit set by the caller in |
| 264 | * args->valuelen, return -ERANGE with the size of the attribute that was found |
| 265 | * in args->valuelen. |
| 266 | */ |
| 267 | int |
| 268 | xfs_attr_get( |
| 269 | struct xfs_da_args *args) |
| 270 | { |
| 271 | uint lock_mode; |
| 272 | int error; |
| 273 | |
| 274 | XFS_STATS_INC(args->dp->i_mount, xs_attr_get); |
| 275 | |
| 276 | if (xfs_is_shutdown(args->dp->i_mount)) |
| 277 | return -EIO; |
| 278 | |
| 279 | if (!args->owner) |
| 280 | args->owner = args->dp->i_ino; |
| 281 | args->geo = args->dp->i_mount->m_attr_geo; |
| 282 | args->whichfork = XFS_ATTR_FORK; |
| 283 | xfs_attr_sethash(args); |
| 284 | |
| 285 | /* Entirely possible to look up a name which doesn't exist */ |
| 286 | args->op_flags = XFS_DA_OP_OKNOENT; |
| 287 | |
| 288 | lock_mode = xfs_ilock_attr_map_shared(args->dp); |
| 289 | error = xfs_attr_get_ilocked(args); |
| 290 | xfs_iunlock(args->dp, lock_mode); |
| 291 | |
| 292 | return error; |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * Calculate how many blocks we need for the new attribute, |
| 297 | */ |
| 298 | int |
| 299 | xfs_attr_calc_size( |
| 300 | struct xfs_da_args *args, |
| 301 | int *local) |
| 302 | { |
| 303 | struct xfs_mount *mp = args->dp->i_mount; |
| 304 | int size; |
| 305 | int nblks; |
| 306 | |
| 307 | /* |
| 308 | * Determine space new attribute will use, and if it would be |
| 309 | * "local" or "remote" (note: local != inline). |
| 310 | */ |
| 311 | size = xfs_attr_leaf_newentsize(args, local); |
| 312 | nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK); |
| 313 | if (*local) { |
| 314 | if (size > (args->geo->blksize / 2)) { |
| 315 | /* Double split possible */ |
| 316 | nblks *= 2; |
| 317 | } |
| 318 | } else { |
| 319 | /* |
| 320 | * Out of line attribute, cannot double split, but |
| 321 | * make room for the attribute value itself. |
| 322 | */ |
| 323 | uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen); |
| 324 | nblks += dblocks; |
| 325 | nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK); |
| 326 | } |
| 327 | |
| 328 | return nblks; |
| 329 | } |
| 330 | |
| 331 | /* Initialize transaction reservation for an xattr set/replace/upsert */ |
| 332 | inline struct xfs_trans_res |
| 333 | xfs_attr_set_resv( |
| 334 | const struct xfs_da_args *args) |
| 335 | { |
| 336 | struct xfs_mount *mp = args->dp->i_mount; |
| 337 | struct xfs_trans_res ret = { |
| 338 | .tr_logres = M_RES(mp)->tr_attrsetm.tr_logres + |
| 339 | M_RES(mp)->tr_attrsetrt.tr_logres * args->total, |
| 340 | .tr_logcount = XFS_ATTRSET_LOG_COUNT, |
| 341 | .tr_logflags = XFS_TRANS_PERM_LOG_RES, |
| 342 | }; |
| 343 | |
| 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Add an attr to a shortform fork. If there is no space, |
| 349 | * xfs_attr_shortform_addname() will convert to leaf format and return -ENOSPC. |
| 350 | * to use. |
| 351 | */ |
| 352 | STATIC int |
| 353 | xfs_attr_try_sf_addname( |
| 354 | struct xfs_inode *dp, |
| 355 | struct xfs_da_args *args) |
| 356 | { |
| 357 | |
| 358 | int error; |
| 359 | |
| 360 | /* |
| 361 | * Build initial attribute list (if required). |
| 362 | */ |
| 363 | if (dp->i_af.if_format == XFS_DINODE_FMT_EXTENTS) |
| 364 | xfs_attr_shortform_create(args); |
| 365 | |
| 366 | error = xfs_attr_shortform_addname(args); |
| 367 | if (error == -ENOSPC) |
| 368 | return error; |
| 369 | |
| 370 | /* |
| 371 | * Commit the shortform mods, and we're done. |
| 372 | * NOTE: this is also the error path (EEXIST, etc). |
| 373 | */ |
| 374 | if (!error) |
| 375 | xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG); |
| 376 | |
| 377 | if (xfs_has_wsync(dp->i_mount)) |
| 378 | xfs_trans_set_sync(args->trans); |
| 379 | |
| 380 | return error; |
| 381 | } |
| 382 | |
| 383 | static int |
| 384 | xfs_attr_sf_addname( |
| 385 | struct xfs_attr_intent *attr) |
| 386 | { |
| 387 | struct xfs_da_args *args = attr->xattri_da_args; |
| 388 | struct xfs_inode *dp = args->dp; |
| 389 | int error = 0; |
| 390 | |
| 391 | error = xfs_attr_try_sf_addname(dp, args); |
| 392 | if (error != -ENOSPC) { |
| 393 | ASSERT(!error || error == -EEXIST); |
| 394 | attr->xattri_dela_state = XFS_DAS_DONE; |
| 395 | goto out; |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * It won't fit in the shortform, transform to a leaf block. GROT: |
| 400 | * another possible req'mt for a double-split btree op. |
| 401 | */ |
| 402 | error = xfs_attr_shortform_to_leaf(args); |
| 403 | if (error) |
| 404 | return error; |
| 405 | |
| 406 | attr->xattri_dela_state = XFS_DAS_LEAF_ADD; |
| 407 | out: |
| 408 | trace_xfs_attr_sf_addname_return(attr->xattri_dela_state, args->dp); |
| 409 | return error; |
| 410 | } |
| 411 | |
| 412 | /* Compute the hash value for a user/root/secure extended attribute */ |
| 413 | xfs_dahash_t |
| 414 | xfs_attr_hashname( |
| 415 | const uint8_t *name, |
| 416 | int namelen) |
| 417 | { |
| 418 | return xfs_da_hashname(name, namelen); |
| 419 | } |
| 420 | |
| 421 | /* Compute the hash value for any extended attribute from any namespace. */ |
| 422 | xfs_dahash_t |
| 423 | xfs_attr_hashval( |
| 424 | struct xfs_mount *mp, |
| 425 | unsigned int attr_flags, |
| 426 | const uint8_t *name, |
| 427 | int namelen, |
| 428 | const void *value, |
| 429 | int valuelen) |
| 430 | { |
| 431 | ASSERT(xfs_attr_check_namespace(attr_flags)); |
| 432 | |
| 433 | if (attr_flags & XFS_ATTR_PARENT) |
| 434 | return xfs_parent_hashattr(mp, name, namelen, value, valuelen); |
| 435 | |
| 436 | return xfs_attr_hashname(name, namelen); |
| 437 | } |
| 438 | |
| 439 | /* Save the current remote block info and clear the current pointers. */ |
| 440 | static void |
| 441 | xfs_attr_save_rmt_blk( |
| 442 | struct xfs_da_args *args) |
| 443 | { |
| 444 | args->blkno2 = args->blkno; |
| 445 | args->index2 = args->index; |
| 446 | args->rmtblkno2 = args->rmtblkno; |
| 447 | args->rmtblkcnt2 = args->rmtblkcnt; |
| 448 | args->rmtvaluelen2 = args->rmtvaluelen; |
| 449 | args->rmtblkno = 0; |
| 450 | args->rmtblkcnt = 0; |
| 451 | args->rmtvaluelen = 0; |
| 452 | } |
| 453 | |
| 454 | /* Set stored info about a remote block */ |
| 455 | static void |
| 456 | xfs_attr_restore_rmt_blk( |
| 457 | struct xfs_da_args *args) |
| 458 | { |
| 459 | args->blkno = args->blkno2; |
| 460 | args->index = args->index2; |
| 461 | args->rmtblkno = args->rmtblkno2; |
| 462 | args->rmtblkcnt = args->rmtblkcnt2; |
| 463 | args->rmtvaluelen = args->rmtvaluelen2; |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * PPTR_REPLACE operations require the caller to set the old and new names and |
| 468 | * values explicitly. Update the canonical fields to the new name and value |
| 469 | * here now that the removal phase has finished. |
| 470 | */ |
| 471 | static void |
| 472 | xfs_attr_update_pptr_replace_args( |
| 473 | struct xfs_da_args *args) |
| 474 | { |
| 475 | ASSERT(args->new_namelen > 0); |
| 476 | args->name = args->new_name; |
| 477 | args->namelen = args->new_namelen; |
| 478 | args->value = args->new_value; |
| 479 | args->valuelen = args->new_valuelen; |
| 480 | xfs_attr_sethash(args); |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * Handle the state change on completion of a multi-state attr operation. |
| 485 | * |
| 486 | * If the XFS_DA_OP_REPLACE flag is set, this means the operation was the first |
| 487 | * modification in a attr replace operation and we still have to do the second |
| 488 | * state, indicated by @replace_state. |
| 489 | * |
| 490 | * We consume the XFS_DA_OP_REPLACE flag so that when we are called again on |
| 491 | * completion of the second half of the attr replace operation we correctly |
| 492 | * signal that it is done. |
| 493 | */ |
| 494 | static enum xfs_delattr_state |
| 495 | xfs_attr_complete_op( |
| 496 | struct xfs_attr_intent *attr, |
| 497 | enum xfs_delattr_state replace_state) |
| 498 | { |
| 499 | struct xfs_da_args *args = attr->xattri_da_args; |
| 500 | |
| 501 | if (!(args->op_flags & XFS_DA_OP_REPLACE)) |
| 502 | replace_state = XFS_DAS_DONE; |
| 503 | else if (xfs_attr_intent_op(attr) == XFS_ATTRI_OP_FLAGS_PPTR_REPLACE) |
| 504 | xfs_attr_update_pptr_replace_args(args); |
| 505 | |
| 506 | args->op_flags &= ~XFS_DA_OP_REPLACE; |
| 507 | args->attr_filter &= ~XFS_ATTR_INCOMPLETE; |
| 508 | return replace_state; |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * Try to add an attribute to an inode in leaf form. |
| 513 | */ |
| 514 | static int |
| 515 | xfs_attr_leaf_addname( |
| 516 | struct xfs_attr_intent *attr) |
| 517 | { |
| 518 | struct xfs_da_args *args = attr->xattri_da_args; |
| 519 | struct xfs_buf *bp; |
| 520 | int error; |
| 521 | |
| 522 | ASSERT(xfs_attr_is_leaf(args->dp)); |
| 523 | |
| 524 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 0, &bp); |
| 525 | if (error) |
| 526 | return error; |
| 527 | |
| 528 | /* |
| 529 | * Look up the xattr name to set the insertion point for the new xattr. |
| 530 | */ |
| 531 | error = xfs_attr3_leaf_lookup_int(leaf: bp, args); |
| 532 | switch (error) { |
| 533 | case -ENOATTR: |
| 534 | if (args->op_flags & XFS_DA_OP_REPLACE) |
| 535 | goto out_brelse; |
| 536 | break; |
| 537 | case -EEXIST: |
| 538 | if (!(args->op_flags & XFS_DA_OP_REPLACE)) |
| 539 | goto out_brelse; |
| 540 | |
| 541 | trace_xfs_attr_leaf_replace(args); |
| 542 | /* |
| 543 | * Save the existing remote attr state so that the current |
| 544 | * values reflect the state of the new attribute we are about to |
| 545 | * add, not the attribute we just found and will remove later. |
| 546 | */ |
| 547 | xfs_attr_save_rmt_blk(args); |
| 548 | break; |
| 549 | case 0: |
| 550 | break; |
| 551 | default: |
| 552 | goto out_brelse; |
| 553 | } |
| 554 | |
| 555 | /* |
| 556 | * We need to commit and roll if we need to allocate remote xattr blocks |
| 557 | * or perform more xattr manipulations. Otherwise there is nothing more |
| 558 | * to do and we can return success. |
| 559 | */ |
| 560 | if (!xfs_attr3_leaf_add(bp, args)) { |
| 561 | error = xfs_attr3_leaf_to_node(args); |
| 562 | if (error) |
| 563 | return error; |
| 564 | |
| 565 | attr->xattri_dela_state = XFS_DAS_NODE_ADD; |
| 566 | } else if (args->rmtblkno) { |
| 567 | attr->xattri_dela_state = XFS_DAS_LEAF_SET_RMT; |
| 568 | } else { |
| 569 | attr->xattri_dela_state = |
| 570 | xfs_attr_complete_op(attr, replace_state: XFS_DAS_LEAF_REPLACE); |
| 571 | } |
| 572 | |
| 573 | trace_xfs_attr_leaf_addname_return(attr->xattri_dela_state, args->dp); |
| 574 | return 0; |
| 575 | |
| 576 | out_brelse: |
| 577 | xfs_trans_brelse(args->trans, bp); |
| 578 | return error; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Add an entry to a node format attr tree. |
| 583 | * |
| 584 | * Note that we might still have a leaf here - xfs_attr_is_leaf() cannot tell |
| 585 | * the difference between leaf + remote attr blocks and a node format tree, |
| 586 | * so we may still end up having to convert from leaf to node format here. |
| 587 | */ |
| 588 | static int |
| 589 | xfs_attr_node_addname( |
| 590 | struct xfs_attr_intent *attr) |
| 591 | { |
| 592 | struct xfs_da_args *args = attr->xattri_da_args; |
| 593 | int error; |
| 594 | |
| 595 | error = xfs_attr_node_addname_find_attr(attr); |
| 596 | if (error) |
| 597 | return error; |
| 598 | |
| 599 | error = xfs_attr_node_try_addname(attr); |
| 600 | if (error == 1) { |
| 601 | error = xfs_attr3_leaf_to_node(args); |
| 602 | if (error) |
| 603 | return error; |
| 604 | /* |
| 605 | * No state change, we really are in node form now |
| 606 | * but we need the transaction rolled to continue. |
| 607 | */ |
| 608 | goto out; |
| 609 | } |
| 610 | if (error) |
| 611 | return error; |
| 612 | |
| 613 | if (args->rmtblkno) |
| 614 | attr->xattri_dela_state = XFS_DAS_NODE_SET_RMT; |
| 615 | else |
| 616 | attr->xattri_dela_state = xfs_attr_complete_op(attr, |
| 617 | replace_state: XFS_DAS_NODE_REPLACE); |
| 618 | out: |
| 619 | trace_xfs_attr_node_addname_return(attr->xattri_dela_state, args->dp); |
| 620 | return error; |
| 621 | } |
| 622 | |
| 623 | static int |
| 624 | xfs_attr_rmtval_alloc( |
| 625 | struct xfs_attr_intent *attr) |
| 626 | { |
| 627 | struct xfs_da_args *args = attr->xattri_da_args; |
| 628 | int error = 0; |
| 629 | |
| 630 | /* |
| 631 | * If there was an out-of-line value, allocate the blocks we |
| 632 | * identified for its storage and copy the value. This is done |
| 633 | * after we create the attribute so that we don't overflow the |
| 634 | * maximum size of a transaction and/or hit a deadlock. |
| 635 | */ |
| 636 | if (attr->xattri_blkcnt > 0) { |
| 637 | error = xfs_attr_rmtval_set_blk(attr); |
| 638 | if (error) |
| 639 | return error; |
| 640 | /* Roll the transaction only if there is more to allocate. */ |
| 641 | if (attr->xattri_blkcnt > 0) |
| 642 | goto out; |
| 643 | } |
| 644 | |
| 645 | error = xfs_attr_rmtval_set_value(args); |
| 646 | if (error) |
| 647 | return error; |
| 648 | |
| 649 | attr->xattri_dela_state = xfs_attr_complete_op(attr, |
| 650 | replace_state: ++attr->xattri_dela_state); |
| 651 | /* |
| 652 | * If we are not doing a rename, we've finished the operation but still |
| 653 | * have to clear the incomplete flag protecting the new attr from |
| 654 | * exposing partially initialised state if we crash during creation. |
| 655 | */ |
| 656 | if (attr->xattri_dela_state == XFS_DAS_DONE) |
| 657 | error = xfs_attr3_leaf_clearflag(args); |
| 658 | out: |
| 659 | trace_xfs_attr_rmtval_alloc(attr->xattri_dela_state, args->dp); |
| 660 | return error; |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers |
| 665 | * for later deletion of the entry. |
| 666 | */ |
| 667 | static int |
| 668 | xfs_attr_leaf_mark_incomplete( |
| 669 | struct xfs_da_args *args, |
| 670 | struct xfs_da_state *state) |
| 671 | { |
| 672 | int error; |
| 673 | |
| 674 | /* |
| 675 | * Fill in disk block numbers in the state structure |
| 676 | * so that we can get the buffers back after we commit |
| 677 | * several transactions in the following calls. |
| 678 | */ |
| 679 | error = xfs_attr_fillstate(state); |
| 680 | if (error) |
| 681 | return error; |
| 682 | |
| 683 | /* |
| 684 | * Mark the attribute as INCOMPLETE |
| 685 | */ |
| 686 | return xfs_attr3_leaf_setflag(args); |
| 687 | } |
| 688 | |
| 689 | /* Ensure the da state of an xattr deferred work item is ready to go. */ |
| 690 | static inline void |
| 691 | xfs_attr_item_init_da_state( |
| 692 | struct xfs_attr_intent *attr) |
| 693 | { |
| 694 | struct xfs_da_args *args = attr->xattri_da_args; |
| 695 | |
| 696 | if (!attr->xattri_da_state) |
| 697 | attr->xattri_da_state = xfs_da_state_alloc(args); |
| 698 | else |
| 699 | xfs_da_state_reset(state: attr->xattri_da_state, args); |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Initial setup for xfs_attr_node_removename. Make sure the attr is there and |
| 704 | * the blocks are valid. Attr keys with remote blocks will be marked |
| 705 | * incomplete. |
| 706 | */ |
| 707 | static |
| 708 | int xfs_attr_node_removename_setup( |
| 709 | struct xfs_attr_intent *attr) |
| 710 | { |
| 711 | struct xfs_da_args *args = attr->xattri_da_args; |
| 712 | struct xfs_da_state *state; |
| 713 | int error; |
| 714 | |
| 715 | xfs_attr_item_init_da_state(attr); |
| 716 | error = xfs_attr_node_lookup(args, attr->xattri_da_state); |
| 717 | if (error != -EEXIST) |
| 718 | goto out; |
| 719 | error = 0; |
| 720 | |
| 721 | state = attr->xattri_da_state; |
| 722 | ASSERT(state->path.blk[state->path.active - 1].bp != NULL); |
| 723 | ASSERT(state->path.blk[state->path.active - 1].magic == |
| 724 | XFS_ATTR_LEAF_MAGIC); |
| 725 | |
| 726 | error = xfs_attr_leaf_mark_incomplete(args, state); |
| 727 | if (error) |
| 728 | goto out; |
| 729 | if (args->rmtblkno > 0) |
| 730 | error = xfs_attr_rmtval_invalidate(args); |
| 731 | out: |
| 732 | if (error) { |
| 733 | xfs_da_state_free(state: attr->xattri_da_state); |
| 734 | attr->xattri_da_state = NULL; |
| 735 | } |
| 736 | |
| 737 | return error; |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * Remove the original attr we have just replaced. This is dependent on the |
| 742 | * original lookup and insert placing the old attr in args->blkno/args->index |
| 743 | * and the new attr in args->blkno2/args->index2. |
| 744 | */ |
| 745 | static int |
| 746 | xfs_attr_leaf_remove_attr( |
| 747 | struct xfs_attr_intent *attr) |
| 748 | { |
| 749 | struct xfs_da_args *args = attr->xattri_da_args; |
| 750 | struct xfs_inode *dp = args->dp; |
| 751 | struct xfs_buf *bp = NULL; |
| 752 | int forkoff; |
| 753 | int error; |
| 754 | |
| 755 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, |
| 756 | args->blkno, &bp); |
| 757 | if (error) |
| 758 | return error; |
| 759 | |
| 760 | xfs_attr3_leaf_remove(leaf_buffer: bp, args); |
| 761 | |
| 762 | forkoff = xfs_attr_shortform_allfit(bp, dp); |
| 763 | if (forkoff) |
| 764 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
| 765 | /* bp is gone due to xfs_da_shrink_inode */ |
| 766 | |
| 767 | return error; |
| 768 | } |
| 769 | |
| 770 | /* |
| 771 | * Shrink an attribute from leaf to shortform. Used by the node format remove |
| 772 | * path when the node format collapses to a single block and so we have to check |
| 773 | * if it can be collapsed further. |
| 774 | */ |
| 775 | static int |
| 776 | xfs_attr_leaf_shrink( |
| 777 | struct xfs_da_args *args) |
| 778 | { |
| 779 | struct xfs_inode *dp = args->dp; |
| 780 | struct xfs_buf *bp; |
| 781 | int forkoff; |
| 782 | int error; |
| 783 | |
| 784 | if (!xfs_attr_is_leaf(dp)) |
| 785 | return 0; |
| 786 | |
| 787 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 0, &bp); |
| 788 | if (error) |
| 789 | return error; |
| 790 | |
| 791 | forkoff = xfs_attr_shortform_allfit(bp, dp); |
| 792 | if (forkoff) { |
| 793 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
| 794 | /* bp is gone due to xfs_da_shrink_inode */ |
| 795 | } else { |
| 796 | xfs_trans_brelse(args->trans, bp); |
| 797 | } |
| 798 | |
| 799 | return error; |
| 800 | } |
| 801 | |
| 802 | /* |
| 803 | * Run the attribute operation specified in @attr. |
| 804 | * |
| 805 | * This routine is meant to function as a delayed operation and will set the |
| 806 | * state to XFS_DAS_DONE when the operation is complete. Calling functions will |
| 807 | * need to handle this, and recall the function until either an error or |
| 808 | * XFS_DAS_DONE is detected. |
| 809 | */ |
| 810 | int |
| 811 | xfs_attr_set_iter( |
| 812 | struct xfs_attr_intent *attr) |
| 813 | { |
| 814 | struct xfs_da_args *args = attr->xattri_da_args; |
| 815 | int error = 0; |
| 816 | |
| 817 | /* State machine switch */ |
| 818 | next_state: |
| 819 | switch (attr->xattri_dela_state) { |
| 820 | case XFS_DAS_UNINIT: |
| 821 | ASSERT(0); |
| 822 | return -EFSCORRUPTED; |
| 823 | case XFS_DAS_SF_ADD: |
| 824 | return xfs_attr_sf_addname(attr); |
| 825 | case XFS_DAS_LEAF_ADD: |
| 826 | return xfs_attr_leaf_addname(attr); |
| 827 | case XFS_DAS_NODE_ADD: |
| 828 | return xfs_attr_node_addname(attr); |
| 829 | |
| 830 | case XFS_DAS_SF_REMOVE: |
| 831 | error = xfs_attr_sf_removename(args); |
| 832 | attr->xattri_dela_state = xfs_attr_complete_op(attr, |
| 833 | replace_state: xfs_attr_init_add_state(args)); |
| 834 | break; |
| 835 | case XFS_DAS_LEAF_REMOVE: |
| 836 | error = xfs_attr_leaf_removename(args); |
| 837 | attr->xattri_dela_state = xfs_attr_complete_op(attr, |
| 838 | replace_state: xfs_attr_init_add_state(args)); |
| 839 | break; |
| 840 | case XFS_DAS_NODE_REMOVE: |
| 841 | error = xfs_attr_node_removename_setup(attr); |
| 842 | if (error == -ENOATTR && |
| 843 | (args->op_flags & XFS_DA_OP_RECOVERY)) { |
| 844 | attr->xattri_dela_state = xfs_attr_complete_op(attr, |
| 845 | replace_state: xfs_attr_init_add_state(args)); |
| 846 | error = 0; |
| 847 | break; |
| 848 | } |
| 849 | if (error) |
| 850 | return error; |
| 851 | attr->xattri_dela_state = XFS_DAS_NODE_REMOVE_RMT; |
| 852 | if (args->rmtblkno == 0) |
| 853 | attr->xattri_dela_state++; |
| 854 | break; |
| 855 | |
| 856 | case XFS_DAS_LEAF_SET_RMT: |
| 857 | case XFS_DAS_NODE_SET_RMT: |
| 858 | error = xfs_attr_rmtval_find_space(attr); |
| 859 | if (error) |
| 860 | return error; |
| 861 | attr->xattri_dela_state++; |
| 862 | fallthrough; |
| 863 | |
| 864 | case XFS_DAS_LEAF_ALLOC_RMT: |
| 865 | case XFS_DAS_NODE_ALLOC_RMT: |
| 866 | error = xfs_attr_rmtval_alloc(attr); |
| 867 | if (error) |
| 868 | return error; |
| 869 | if (attr->xattri_dela_state == XFS_DAS_DONE) |
| 870 | break; |
| 871 | goto next_state; |
| 872 | |
| 873 | case XFS_DAS_LEAF_REPLACE: |
| 874 | case XFS_DAS_NODE_REPLACE: |
| 875 | /* |
| 876 | * We must "flip" the incomplete flags on the "new" and "old" |
| 877 | * attribute/value pairs so that one disappears and one appears |
| 878 | * atomically. |
| 879 | */ |
| 880 | error = xfs_attr3_leaf_flipflags(args); |
| 881 | if (error) |
| 882 | return error; |
| 883 | /* |
| 884 | * We must commit the flag value change now to make it atomic |
| 885 | * and then we can start the next trans in series at REMOVE_OLD. |
| 886 | */ |
| 887 | attr->xattri_dela_state++; |
| 888 | break; |
| 889 | |
| 890 | case XFS_DAS_LEAF_REMOVE_OLD: |
| 891 | case XFS_DAS_NODE_REMOVE_OLD: |
| 892 | /* |
| 893 | * If we have a remote attr, start the process of removing it |
| 894 | * by invalidating any cached buffers. |
| 895 | * |
| 896 | * If we don't have a remote attr, we skip the remote block |
| 897 | * removal state altogether with a second state increment. |
| 898 | */ |
| 899 | xfs_attr_restore_rmt_blk(args); |
| 900 | if (args->rmtblkno) { |
| 901 | error = xfs_attr_rmtval_invalidate(args); |
| 902 | if (error) |
| 903 | return error; |
| 904 | } else { |
| 905 | attr->xattri_dela_state++; |
| 906 | } |
| 907 | |
| 908 | attr->xattri_dela_state++; |
| 909 | goto next_state; |
| 910 | |
| 911 | case XFS_DAS_LEAF_REMOVE_RMT: |
| 912 | case XFS_DAS_NODE_REMOVE_RMT: |
| 913 | error = xfs_attr_rmtval_remove(attr); |
| 914 | if (error == -EAGAIN) { |
| 915 | error = 0; |
| 916 | break; |
| 917 | } |
| 918 | if (error) |
| 919 | return error; |
| 920 | |
| 921 | /* |
| 922 | * We've finished removing the remote attr blocks, so commit the |
| 923 | * transaction and move on to removing the attr name from the |
| 924 | * leaf/node block. Removing the attr might require a full |
| 925 | * transaction reservation for btree block freeing, so we |
| 926 | * can't do that in the same transaction where we removed the |
| 927 | * remote attr blocks. |
| 928 | */ |
| 929 | attr->xattri_dela_state++; |
| 930 | break; |
| 931 | |
| 932 | case XFS_DAS_LEAF_REMOVE_ATTR: |
| 933 | error = xfs_attr_leaf_remove_attr(attr); |
| 934 | attr->xattri_dela_state = xfs_attr_complete_op(attr, |
| 935 | replace_state: xfs_attr_init_add_state(args)); |
| 936 | break; |
| 937 | |
| 938 | case XFS_DAS_NODE_REMOVE_ATTR: |
| 939 | error = xfs_attr_node_remove_attr(attr); |
| 940 | if (!error) |
| 941 | error = xfs_attr_leaf_shrink(args); |
| 942 | attr->xattri_dela_state = xfs_attr_complete_op(attr, |
| 943 | replace_state: xfs_attr_init_add_state(args)); |
| 944 | break; |
| 945 | default: |
| 946 | ASSERT(0); |
| 947 | break; |
| 948 | } |
| 949 | |
| 950 | trace_xfs_attr_set_iter_return(attr->xattri_dela_state, args->dp); |
| 951 | return error; |
| 952 | } |
| 953 | |
| 954 | |
| 955 | /* |
| 956 | * Return EEXIST if attr is found, or ENOATTR if not |
| 957 | */ |
| 958 | static int |
| 959 | xfs_attr_lookup( |
| 960 | struct xfs_da_args *args) |
| 961 | { |
| 962 | struct xfs_inode *dp = args->dp; |
| 963 | struct xfs_buf *bp = NULL; |
| 964 | struct xfs_da_state *state; |
| 965 | int error; |
| 966 | |
| 967 | if (!xfs_inode_hasattr(dp)) |
| 968 | return -ENOATTR; |
| 969 | |
| 970 | if (dp->i_af.if_format == XFS_DINODE_FMT_LOCAL) { |
| 971 | if (xfs_attr_sf_findname(args)) |
| 972 | return -EEXIST; |
| 973 | return -ENOATTR; |
| 974 | } |
| 975 | |
| 976 | /* Prerequisite for xfs_attr_is_leaf */ |
| 977 | error = xfs_iread_extents(args->trans, args->dp, XFS_ATTR_FORK); |
| 978 | if (error) |
| 979 | return error; |
| 980 | |
| 981 | if (xfs_attr_is_leaf(dp)) { |
| 982 | error = xfs_attr_leaf_hasname(args, &bp); |
| 983 | |
| 984 | if (bp) |
| 985 | xfs_trans_brelse(args->trans, bp); |
| 986 | |
| 987 | return error; |
| 988 | } |
| 989 | |
| 990 | state = xfs_da_state_alloc(args); |
| 991 | error = xfs_attr_node_lookup(args, state); |
| 992 | xfs_da_state_free(state); |
| 993 | return error; |
| 994 | } |
| 995 | |
| 996 | int |
| 997 | xfs_attr_add_fork( |
| 998 | struct xfs_inode *ip, /* incore inode pointer */ |
| 999 | int size, /* space new attribute needs */ |
| 1000 | int rsvd) /* xact may use reserved blks */ |
| 1001 | { |
| 1002 | struct xfs_mount *mp = ip->i_mount; |
| 1003 | struct xfs_trans *tp; /* transaction pointer */ |
| 1004 | unsigned int blks; /* space reservation */ |
| 1005 | int error; /* error return value */ |
| 1006 | |
| 1007 | if (!xfs_is_metadir_inode(ip)) |
| 1008 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
| 1009 | |
| 1010 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
| 1011 | |
| 1012 | error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_addafork, blks, 0, |
| 1013 | rsvd, &tp); |
| 1014 | if (error) |
| 1015 | return error; |
| 1016 | |
| 1017 | if (xfs_inode_has_attr_fork(ip)) |
| 1018 | goto trans_cancel; |
| 1019 | |
| 1020 | error = xfs_bmap_add_attrfork(tp, ip, size, rsvd); |
| 1021 | if (error) |
| 1022 | goto trans_cancel; |
| 1023 | |
| 1024 | error = xfs_trans_commit(tp); |
| 1025 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1026 | return error; |
| 1027 | |
| 1028 | trans_cancel: |
| 1029 | xfs_trans_cancel(tp); |
| 1030 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1031 | return error; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * Make a change to the xattr structure. |
| 1036 | * |
| 1037 | * The caller must have initialized @args, attached dquots, and must not hold |
| 1038 | * any ILOCKs. Reserved data blocks may be used if @rsvd is set. |
| 1039 | * |
| 1040 | * Returns -EEXIST for XFS_ATTRUPDATE_CREATE if the name already exists. |
| 1041 | * Returns -ENOATTR for XFS_ATTRUPDATE_REMOVE if the name does not exist. |
| 1042 | * Returns 0 on success, or a negative errno if something else went wrong. |
| 1043 | */ |
| 1044 | int |
| 1045 | xfs_attr_set( |
| 1046 | struct xfs_da_args *args, |
| 1047 | enum xfs_attr_update op, |
| 1048 | bool rsvd) |
| 1049 | { |
| 1050 | struct xfs_inode *dp = args->dp; |
| 1051 | struct xfs_mount *mp = dp->i_mount; |
| 1052 | struct xfs_trans_res tres; |
| 1053 | int error, local; |
| 1054 | int rmt_blks = 0; |
| 1055 | unsigned int total = 0; |
| 1056 | |
| 1057 | ASSERT(!args->trans); |
| 1058 | |
| 1059 | switch (op) { |
| 1060 | case XFS_ATTRUPDATE_UPSERT: |
| 1061 | case XFS_ATTRUPDATE_CREATE: |
| 1062 | case XFS_ATTRUPDATE_REPLACE: |
| 1063 | XFS_STATS_INC(mp, xs_attr_set); |
| 1064 | args->total = xfs_attr_calc_size(args, local: &local); |
| 1065 | |
| 1066 | /* |
| 1067 | * If the inode doesn't have an attribute fork, add one. |
| 1068 | * (inode must not be locked when we call this routine) |
| 1069 | */ |
| 1070 | if (xfs_inode_has_attr_fork(dp) == 0) { |
| 1071 | int sf_size = sizeof(struct xfs_attr_sf_hdr) + |
| 1072 | xfs_attr_sf_entsize_byname(args->namelen, |
| 1073 | args->valuelen); |
| 1074 | |
| 1075 | error = xfs_attr_add_fork(ip: dp, size: sf_size, rsvd: rsvd); |
| 1076 | if (error) |
| 1077 | return error; |
| 1078 | } |
| 1079 | |
| 1080 | if (!local) |
| 1081 | rmt_blks = xfs_attr3_rmt_blocks(mp, attrlen: args->valuelen); |
| 1082 | |
| 1083 | tres = xfs_attr_set_resv(args); |
| 1084 | total = args->total; |
| 1085 | break; |
| 1086 | case XFS_ATTRUPDATE_REMOVE: |
| 1087 | XFS_STATS_INC(mp, xs_attr_remove); |
| 1088 | rmt_blks = xfs_attr3_max_rmt_blocks(mp); |
| 1089 | tres = M_RES(mp)->tr_attrrm; |
| 1090 | total = XFS_ATTRRM_SPACE_RES(mp); |
| 1091 | break; |
| 1092 | } |
| 1093 | |
| 1094 | /* |
| 1095 | * Root fork attributes can use reserved data blocks for this |
| 1096 | * operation if necessary |
| 1097 | */ |
| 1098 | error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans); |
| 1099 | if (error) |
| 1100 | return error; |
| 1101 | |
| 1102 | if (op != XFS_ATTRUPDATE_REMOVE || xfs_inode_hasattr(ip: dp)) { |
| 1103 | error = xfs_iext_count_extend(args->trans, dp, XFS_ATTR_FORK, |
| 1104 | XFS_IEXT_ATTR_MANIP_CNT(rmt_blks)); |
| 1105 | if (error) |
| 1106 | goto out_trans_cancel; |
| 1107 | } |
| 1108 | |
| 1109 | error = xfs_attr_lookup(args); |
| 1110 | switch (error) { |
| 1111 | case -EEXIST: |
| 1112 | if (op == XFS_ATTRUPDATE_REMOVE) { |
| 1113 | /* if no value, we are performing a remove operation */ |
| 1114 | xfs_attr_defer_add(args, XFS_ATTR_DEFER_REMOVE); |
| 1115 | break; |
| 1116 | } |
| 1117 | |
| 1118 | /* Pure create fails if the attr already exists */ |
| 1119 | if (op == XFS_ATTRUPDATE_CREATE) |
| 1120 | goto out_trans_cancel; |
| 1121 | xfs_attr_defer_add(args, XFS_ATTR_DEFER_REPLACE); |
| 1122 | break; |
| 1123 | case -ENOATTR: |
| 1124 | /* Can't remove what isn't there. */ |
| 1125 | if (op == XFS_ATTRUPDATE_REMOVE) |
| 1126 | goto out_trans_cancel; |
| 1127 | |
| 1128 | /* Pure replace fails if no existing attr to replace. */ |
| 1129 | if (op == XFS_ATTRUPDATE_REPLACE) |
| 1130 | goto out_trans_cancel; |
| 1131 | xfs_attr_defer_add(args, XFS_ATTR_DEFER_SET); |
| 1132 | break; |
| 1133 | default: |
| 1134 | goto out_trans_cancel; |
| 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * If this is a synchronous mount, make sure that the |
| 1139 | * transaction goes to disk before returning to the user. |
| 1140 | */ |
| 1141 | if (xfs_has_wsync(mp)) |
| 1142 | xfs_trans_set_sync(args->trans); |
| 1143 | |
| 1144 | xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG); |
| 1145 | |
| 1146 | /* |
| 1147 | * Commit the last in the sequence of transactions. |
| 1148 | */ |
| 1149 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE); |
| 1150 | error = xfs_trans_commit(args->trans); |
| 1151 | out_unlock: |
| 1152 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 1153 | args->trans = NULL; |
| 1154 | return error; |
| 1155 | |
| 1156 | out_trans_cancel: |
| 1157 | if (args->trans) |
| 1158 | xfs_trans_cancel(args->trans); |
| 1159 | goto out_unlock; |
| 1160 | } |
| 1161 | |
| 1162 | /*======================================================================== |
| 1163 | * External routines when attribute list is inside the inode |
| 1164 | *========================================================================*/ |
| 1165 | |
| 1166 | int xfs_attr_sf_totsize(struct xfs_inode *dp) |
| 1167 | { |
| 1168 | struct xfs_attr_sf_hdr *sf = dp->i_af.if_data; |
| 1169 | |
| 1170 | return be16_to_cpu(sf->totsize); |
| 1171 | } |
| 1172 | |
| 1173 | /* |
| 1174 | * Add a name to the shortform attribute list structure |
| 1175 | * This is the external routine. |
| 1176 | */ |
| 1177 | static int |
| 1178 | xfs_attr_shortform_addname( |
| 1179 | struct xfs_da_args *args) |
| 1180 | { |
| 1181 | int newsize, forkoff; |
| 1182 | |
| 1183 | trace_xfs_attr_sf_addname(args); |
| 1184 | |
| 1185 | if (xfs_attr_sf_findname(args)) { |
| 1186 | int error; |
| 1187 | |
| 1188 | ASSERT(args->op_flags & XFS_DA_OP_REPLACE); |
| 1189 | |
| 1190 | error = xfs_attr_sf_removename(args); |
| 1191 | if (error) |
| 1192 | return error; |
| 1193 | |
| 1194 | /* |
| 1195 | * Since we have removed the old attr, clear XFS_DA_OP_REPLACE |
| 1196 | * so that the new attr doesn't fit in shortform format, the |
| 1197 | * leaf format add routine won't trip over the attr not being |
| 1198 | * around. |
| 1199 | */ |
| 1200 | args->op_flags &= ~XFS_DA_OP_REPLACE; |
| 1201 | } else { |
| 1202 | ASSERT(!(args->op_flags & XFS_DA_OP_REPLACE)); |
| 1203 | } |
| 1204 | |
| 1205 | if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX || |
| 1206 | args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX) |
| 1207 | return -ENOSPC; |
| 1208 | |
| 1209 | newsize = xfs_attr_sf_totsize(dp: args->dp); |
| 1210 | newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen); |
| 1211 | |
| 1212 | forkoff = xfs_attr_shortform_bytesfit(dp: args->dp, bytes: newsize); |
| 1213 | if (!forkoff) |
| 1214 | return -ENOSPC; |
| 1215 | |
| 1216 | xfs_attr_shortform_add(args, forkoff); |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | |
| 1221 | /*======================================================================== |
| 1222 | * External routines when attribute list is one block |
| 1223 | *========================================================================*/ |
| 1224 | |
| 1225 | /* |
| 1226 | * Return EEXIST if attr is found, or ENOATTR if not |
| 1227 | */ |
| 1228 | STATIC int |
| 1229 | xfs_attr_leaf_hasname( |
| 1230 | struct xfs_da_args *args, |
| 1231 | struct xfs_buf **bp) |
| 1232 | { |
| 1233 | int error = 0; |
| 1234 | |
| 1235 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 0, bp); |
| 1236 | if (error) |
| 1237 | return error; |
| 1238 | |
| 1239 | error = xfs_attr3_leaf_lookup_int(leaf: *bp, args); |
| 1240 | if (error != -ENOATTR && error != -EEXIST) |
| 1241 | xfs_trans_brelse(args->trans, *bp); |
| 1242 | |
| 1243 | return error; |
| 1244 | } |
| 1245 | |
| 1246 | /* |
| 1247 | * Remove a name from the leaf attribute list structure |
| 1248 | * |
| 1249 | * This leaf block cannot have a "remote" value, we only call this routine |
| 1250 | * if bmap_one_block() says there is only one block (ie: no remote blks). |
| 1251 | */ |
| 1252 | STATIC int |
| 1253 | xfs_attr_leaf_removename( |
| 1254 | struct xfs_da_args *args) |
| 1255 | { |
| 1256 | struct xfs_inode *dp; |
| 1257 | struct xfs_buf *bp; |
| 1258 | int error, forkoff; |
| 1259 | |
| 1260 | trace_xfs_attr_leaf_removename(args); |
| 1261 | |
| 1262 | /* |
| 1263 | * Remove the attribute. |
| 1264 | */ |
| 1265 | dp = args->dp; |
| 1266 | |
| 1267 | error = xfs_attr_leaf_hasname(args, &bp); |
| 1268 | if (error == -ENOATTR) { |
| 1269 | xfs_trans_brelse(args->trans, bp); |
| 1270 | if (args->op_flags & XFS_DA_OP_RECOVERY) |
| 1271 | return 0; |
| 1272 | return error; |
| 1273 | } else if (error != -EEXIST) |
| 1274 | return error; |
| 1275 | |
| 1276 | xfs_attr3_leaf_remove(leaf_buffer: bp, args); |
| 1277 | |
| 1278 | /* |
| 1279 | * If the result is small enough, shrink it all into the inode. |
| 1280 | */ |
| 1281 | forkoff = xfs_attr_shortform_allfit(bp, dp); |
| 1282 | if (forkoff) |
| 1283 | return xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
| 1284 | /* bp is gone due to xfs_da_shrink_inode */ |
| 1285 | |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
| 1289 | /* |
| 1290 | * Look up a name in a leaf attribute list structure. |
| 1291 | * |
| 1292 | * This leaf block cannot have a "remote" value, we only call this routine |
| 1293 | * if bmap_one_block() says there is only one block (ie: no remote blks). |
| 1294 | * |
| 1295 | * Returns 0 on successful retrieval, otherwise an error. |
| 1296 | */ |
| 1297 | STATIC int |
| 1298 | xfs_attr_leaf_get(xfs_da_args_t *args) |
| 1299 | { |
| 1300 | struct xfs_buf *bp; |
| 1301 | int error; |
| 1302 | |
| 1303 | trace_xfs_attr_leaf_get(args); |
| 1304 | |
| 1305 | error = xfs_attr_leaf_hasname(args, &bp); |
| 1306 | |
| 1307 | if (error == -ENOATTR) { |
| 1308 | xfs_trans_brelse(args->trans, bp); |
| 1309 | return error; |
| 1310 | } else if (error != -EEXIST) |
| 1311 | return error; |
| 1312 | |
| 1313 | |
| 1314 | error = xfs_attr3_leaf_getvalue(bp, args); |
| 1315 | xfs_trans_brelse(args->trans, bp); |
| 1316 | return error; |
| 1317 | } |
| 1318 | |
| 1319 | /* Return EEXIST if attr is found, or ENOATTR if not. */ |
| 1320 | STATIC int |
| 1321 | xfs_attr_node_lookup( |
| 1322 | struct xfs_da_args *args, |
| 1323 | struct xfs_da_state *state) |
| 1324 | { |
| 1325 | int retval, error; |
| 1326 | |
| 1327 | /* |
| 1328 | * Search to see if name exists, and get back a pointer to it. |
| 1329 | */ |
| 1330 | error = xfs_da3_node_lookup_int(state, result: &retval); |
| 1331 | if (error) |
| 1332 | return error; |
| 1333 | |
| 1334 | return retval; |
| 1335 | } |
| 1336 | |
| 1337 | /*======================================================================== |
| 1338 | * External routines when attribute list size > geo->blksize |
| 1339 | *========================================================================*/ |
| 1340 | |
| 1341 | STATIC int |
| 1342 | xfs_attr_node_addname_find_attr( |
| 1343 | struct xfs_attr_intent *attr) |
| 1344 | { |
| 1345 | struct xfs_da_args *args = attr->xattri_da_args; |
| 1346 | int error; |
| 1347 | |
| 1348 | /* |
| 1349 | * Search to see if name already exists, and get back a pointer |
| 1350 | * to where it should go. |
| 1351 | */ |
| 1352 | xfs_attr_item_init_da_state(attr); |
| 1353 | error = xfs_attr_node_lookup(args, attr->xattri_da_state); |
| 1354 | switch (error) { |
| 1355 | case -ENOATTR: |
| 1356 | if (args->op_flags & XFS_DA_OP_REPLACE) |
| 1357 | goto error; |
| 1358 | break; |
| 1359 | case -EEXIST: |
| 1360 | if (!(args->op_flags & XFS_DA_OP_REPLACE)) |
| 1361 | goto error; |
| 1362 | |
| 1363 | |
| 1364 | trace_xfs_attr_node_replace(args); |
| 1365 | /* |
| 1366 | * Save the existing remote attr state so that the current |
| 1367 | * values reflect the state of the new attribute we are about to |
| 1368 | * add, not the attribute we just found and will remove later. |
| 1369 | */ |
| 1370 | xfs_attr_save_rmt_blk(args); |
| 1371 | break; |
| 1372 | case 0: |
| 1373 | break; |
| 1374 | default: |
| 1375 | goto error; |
| 1376 | } |
| 1377 | |
| 1378 | return 0; |
| 1379 | error: |
| 1380 | if (attr->xattri_da_state) { |
| 1381 | xfs_da_state_free(state: attr->xattri_da_state); |
| 1382 | attr->xattri_da_state = NULL; |
| 1383 | } |
| 1384 | return error; |
| 1385 | } |
| 1386 | |
| 1387 | /* |
| 1388 | * Add a name to a Btree-format attribute list. |
| 1389 | * |
| 1390 | * This will involve walking down the Btree, and may involve splitting leaf |
| 1391 | * nodes and even splitting intermediate nodes up to and including the root |
| 1392 | * node (a special case of an intermediate node). |
| 1393 | * |
| 1394 | * If the tree was still in single leaf format and needs to converted to |
| 1395 | * real node format return 1 and let the caller handle that. |
| 1396 | */ |
| 1397 | static int |
| 1398 | xfs_attr_node_try_addname( |
| 1399 | struct xfs_attr_intent *attr) |
| 1400 | { |
| 1401 | struct xfs_da_state *state = attr->xattri_da_state; |
| 1402 | struct xfs_da_state_blk *blk; |
| 1403 | int error = 0; |
| 1404 | |
| 1405 | trace_xfs_attr_node_addname(state->args); |
| 1406 | |
| 1407 | blk = &state->path.blk[state->path.active-1]; |
| 1408 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
| 1409 | |
| 1410 | if (!xfs_attr3_leaf_add(blk->bp, state->args)) { |
| 1411 | if (state->path.active == 1) { |
| 1412 | /* |
| 1413 | * Its really a single leaf node, but it had |
| 1414 | * out-of-line values so it looked like it *might* |
| 1415 | * have been a b-tree. Let the caller deal with this. |
| 1416 | */ |
| 1417 | error = 1; |
| 1418 | goto out; |
| 1419 | } |
| 1420 | |
| 1421 | /* |
| 1422 | * Split as many Btree elements as required. |
| 1423 | * This code tracks the new and old attr's location |
| 1424 | * in the index/blkno/rmtblkno/rmtblkcnt fields and |
| 1425 | * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields. |
| 1426 | */ |
| 1427 | error = xfs_da3_split(state); |
| 1428 | if (error) |
| 1429 | goto out; |
| 1430 | } else { |
| 1431 | /* |
| 1432 | * Addition succeeded, update Btree hashvals. |
| 1433 | */ |
| 1434 | xfs_da3_fixhashpath(state, path_to_to_fix: &state->path); |
| 1435 | } |
| 1436 | |
| 1437 | out: |
| 1438 | xfs_da_state_free(state); |
| 1439 | attr->xattri_da_state = NULL; |
| 1440 | return error; |
| 1441 | } |
| 1442 | |
| 1443 | static int |
| 1444 | xfs_attr_node_removename( |
| 1445 | struct xfs_da_args *args, |
| 1446 | struct xfs_da_state *state) |
| 1447 | { |
| 1448 | struct xfs_da_state_blk *blk; |
| 1449 | int retval; |
| 1450 | |
| 1451 | /* |
| 1452 | * Remove the name and update the hashvals in the tree. |
| 1453 | */ |
| 1454 | blk = &state->path.blk[state->path.active-1]; |
| 1455 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
| 1456 | retval = xfs_attr3_leaf_remove(leaf_buffer: blk->bp, args); |
| 1457 | xfs_da3_fixhashpath(state, path_to_to_fix: &state->path); |
| 1458 | |
| 1459 | return retval; |
| 1460 | } |
| 1461 | |
| 1462 | static int |
| 1463 | xfs_attr_node_remove_attr( |
| 1464 | struct xfs_attr_intent *attr) |
| 1465 | { |
| 1466 | struct xfs_da_args *args = attr->xattri_da_args; |
| 1467 | struct xfs_da_state *state = xfs_da_state_alloc(args); |
| 1468 | int retval = 0; |
| 1469 | int error = 0; |
| 1470 | |
| 1471 | /* |
| 1472 | * The attr we are removing has already been marked incomplete, so |
| 1473 | * we need to set the filter appropriately to re-find the "old" |
| 1474 | * attribute entry after any split ops. |
| 1475 | */ |
| 1476 | args->attr_filter |= XFS_ATTR_INCOMPLETE; |
| 1477 | error = xfs_da3_node_lookup_int(state, result: &retval); |
| 1478 | if (error) |
| 1479 | goto out; |
| 1480 | |
| 1481 | error = xfs_attr_node_removename(args, state); |
| 1482 | |
| 1483 | /* |
| 1484 | * Check to see if the tree needs to be collapsed. |
| 1485 | */ |
| 1486 | if (retval && (state->path.active > 1)) { |
| 1487 | error = xfs_da3_join(state); |
| 1488 | if (error) |
| 1489 | goto out; |
| 1490 | } |
| 1491 | retval = error = 0; |
| 1492 | |
| 1493 | out: |
| 1494 | xfs_da_state_free(state); |
| 1495 | if (error) |
| 1496 | return error; |
| 1497 | return retval; |
| 1498 | } |
| 1499 | |
| 1500 | /* |
| 1501 | * Retrieve the attribute data from a node attribute list. |
| 1502 | * |
| 1503 | * This routine gets called for any attribute fork that has more than one |
| 1504 | * block, ie: both true Btree attr lists and for single-leaf-blocks with |
| 1505 | * "remote" values taking up more blocks. |
| 1506 | * |
| 1507 | * Returns 0 on successful retrieval, otherwise an error. |
| 1508 | */ |
| 1509 | STATIC int |
| 1510 | xfs_attr_node_get( |
| 1511 | struct xfs_da_args *args) |
| 1512 | { |
| 1513 | struct xfs_da_state *state; |
| 1514 | struct xfs_da_state_blk *blk; |
| 1515 | int i; |
| 1516 | int error; |
| 1517 | |
| 1518 | trace_xfs_attr_node_get(args); |
| 1519 | |
| 1520 | /* |
| 1521 | * Search to see if name exists, and get back a pointer to it. |
| 1522 | */ |
| 1523 | state = xfs_da_state_alloc(args); |
| 1524 | error = xfs_attr_node_lookup(args, state); |
| 1525 | if (error != -EEXIST) |
| 1526 | goto out_release; |
| 1527 | |
| 1528 | /* |
| 1529 | * Get the value, local or "remote" |
| 1530 | */ |
| 1531 | blk = &state->path.blk[state->path.active - 1]; |
| 1532 | error = xfs_attr3_leaf_getvalue(bp: blk->bp, args); |
| 1533 | |
| 1534 | /* |
| 1535 | * If not in a transaction, we have to release all the buffers. |
| 1536 | */ |
| 1537 | out_release: |
| 1538 | for (i = 0; i < state->path.active; i++) { |
| 1539 | xfs_trans_brelse(args->trans, state->path.blk[i].bp); |
| 1540 | state->path.blk[i].bp = NULL; |
| 1541 | } |
| 1542 | |
| 1543 | xfs_da_state_free(state); |
| 1544 | return error; |
| 1545 | } |
| 1546 | |
| 1547 | /* Enforce that there is at most one namespace bit per attr. */ |
| 1548 | inline bool xfs_attr_check_namespace(unsigned int attr_flags) |
| 1549 | { |
| 1550 | return hweight32(attr_flags & XFS_ATTR_NSP_ONDISK_MASK) < 2; |
| 1551 | } |
| 1552 | |
| 1553 | /* Returns true if the attribute entry name is valid. */ |
| 1554 | bool |
| 1555 | xfs_attr_namecheck( |
| 1556 | unsigned int attr_flags, |
| 1557 | const void *name, |
| 1558 | size_t length) |
| 1559 | { |
| 1560 | /* Only one namespace bit allowed. */ |
| 1561 | if (!xfs_attr_check_namespace(attr_flags)) |
| 1562 | return false; |
| 1563 | |
| 1564 | /* |
| 1565 | * MAXNAMELEN includes the trailing null, but (name/length) leave it |
| 1566 | * out, so use >= for the length check. |
| 1567 | */ |
| 1568 | if (length >= MAXNAMELEN) |
| 1569 | return false; |
| 1570 | |
| 1571 | /* Parent pointers have their own validation. */ |
| 1572 | if (attr_flags & XFS_ATTR_PARENT) |
| 1573 | return xfs_parent_namecheck(attr_flags, name, length); |
| 1574 | |
| 1575 | /* There shouldn't be any nulls here */ |
| 1576 | return !memchr(name, 0, length); |
| 1577 | } |
| 1578 | |
| 1579 | int __init |
| 1580 | xfs_attr_intent_init_cache(void) |
| 1581 | { |
| 1582 | xfs_attr_intent_cache = kmem_cache_create("xfs_attr_intent" , |
| 1583 | sizeof(struct xfs_attr_intent), |
| 1584 | 0, 0, NULL); |
| 1585 | |
| 1586 | return xfs_attr_intent_cache != NULL ? 0 : -ENOMEM; |
| 1587 | } |
| 1588 | |
| 1589 | void |
| 1590 | xfs_attr_intent_destroy_cache(void) |
| 1591 | { |
| 1592 | kmem_cache_destroy(xfs_attr_intent_cache); |
| 1593 | xfs_attr_intent_cache = NULL; |
| 1594 | } |
| 1595 | |