FILE
xfs_attr_item.c
File preview with syntax highlighting.
linux-6.19.14/fs/xfs/xfs_attr_item.c
1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Copyright (C) 2022 Oracle. All Rights Reserved. 4 * Author: Allison Henderson <allison.henderson@oracle.com> 5 */ 6 7#include "xfs.h" 8#include "xfs_fs.h" 9#include "xfs_format.h" 10#include "xfs_trans_resv.h" 11#include "xfs_shared.h" 12#include "xfs_mount.h" 13#include "xfs_defer.h" 14#include "xfs_log_format.h" 15#include "xfs_trans.h" 16#include "xfs_bmap_btree.h" 17#include "xfs_trans_priv.h" 18#include "xfs_log.h" 19#include "xfs_inode.h" 20#include "xfs_da_format.h" 21#include "xfs_da_btree.h" 22#include "xfs_attr.h" 23#include "xfs_attr_item.h" 24#include "xfs_trace.h" 25#include "xfs_trans_space.h" 26#include "xfs_errortag.h" 27#include "xfs_error.h" 28#include "xfs_log_priv.h" 29#include "xfs_log_recover.h" 30#include "xfs_parent.h" 31 32struct kmem_cache *xfs_attri_cache; 33struct kmem_cache *xfs_attrd_cache; 34 35static const struct xfs_item_ops xfs_attri_item_ops; 36static const struct xfs_item_ops xfs_attrd_item_ops; 37 38static inline struct xfs_attri_log_item *ATTRI_ITEM(struct xfs_log_item *lip) 39{ 40 return container_of(lip, struct xfs_attri_log_item, attri_item); 41} 42 43/* 44 * Shared xattr name/value buffers for logged extended attribute operations 45 * 46 * When logging updates to extended attributes, we can create quite a few 47 * attribute log intent items for a single xattr update. To avoid cycling the 48 * memory allocator and memcpy overhead, the name (and value, for setxattr) 49 * are kept in a refcounted object that is shared across all related log items 50 * and the upper-level deferred work state structure. The shared buffer has 51 * a control structure, followed by the name, and then the value. 52 */ 53 54static inline struct xfs_attri_log_nameval * 55xfs_attri_log_nameval_get( 56 struct xfs_attri_log_nameval *nv) 57{ 58 if (!refcount_inc_not_zero(&nv->refcount)) 59 return NULL; 60 return nv; 61} 62 63static inline void 64xfs_attri_log_nameval_put( 65 struct xfs_attri_log_nameval *nv) 66{ 67 if (!nv) 68 return; 69 if (refcount_dec_and_test(&nv->refcount)) 70 kvfree(nv); 71} 72 73static inline struct xfs_attri_log_nameval * 74xfs_attri_log_nameval_alloc( 75 const void *name, 76 unsigned int name_len, 77 const void *new_name, 78 unsigned int new_name_len, 79 const void *value, 80 unsigned int value_len, 81 const void *new_value, 82 unsigned int new_value_len) 83{ 84 struct xfs_attri_log_nameval *nv; 85 86 /* 87 * This could be over 64kB in length, so we have to use kvmalloc() for 88 * this. But kvmalloc() utterly sucks, so we use our own version. 89 */ 90 nv = xlog_kvmalloc(sizeof(struct xfs_attri_log_nameval) + 91 name_len + new_name_len + value_len + 92 new_value_len); 93 94 nv->name.iov_base = nv + 1; 95 nv->name.iov_len = name_len; 96 memcpy(nv->name.iov_base, name, name_len); 97 98 if (new_name_len) { 99 nv->new_name.iov_base = nv->name.iov_base + name_len; 100 nv->new_name.iov_len = new_name_len; 101 memcpy(nv->new_name.iov_base, new_name, new_name_len); 102 } else { 103 nv->new_name.iov_base = NULL; 104 nv->new_name.iov_len = 0; 105 } 106 107 if (value_len) { 108 nv->value.iov_base = nv->name.iov_base + name_len + new_name_len; 109 nv->value.iov_len = value_len; 110 memcpy(nv->value.iov_base, value, value_len); 111 } else { 112 nv->value.iov_base = NULL; 113 nv->value.iov_len = 0; 114 } 115 116 if (new_value_len) { 117 nv->new_value.iov_base = nv->name.iov_base + name_len + 118 new_name_len + value_len; 119 nv->new_value.iov_len = new_value_len; 120 memcpy(nv->new_value.iov_base, new_value, new_value_len); 121 } else { 122 nv->new_value.iov_base = NULL; 123 nv->new_value.iov_len = 0; 124 } 125 126 refcount_set(&nv->refcount, 1); 127 return nv; 128} 129 130STATIC void 131xfs_attri_item_free( 132 struct xfs_attri_log_item *attrip) 133{ 134 kvfree(attrip->attri_item.li_lv_shadow); 135 xfs_attri_log_nameval_put(attrip->attri_nameval); 136 kmem_cache_free(xfs_attri_cache, attrip); 137} 138 139/* 140 * Freeing the attrip requires that we remove it from the AIL if it has already 141 * been placed there. However, the ATTRI may not yet have been placed in the 142 * AIL when called by xfs_attri_release() from ATTRD processing due to the 143 * ordering of committed vs unpin operations in bulk insert operations. Hence 144 * the reference count to ensure only the last caller frees the ATTRI. 145 */ 146STATIC void 147xfs_attri_release( 148 struct xfs_attri_log_item *attrip) 149{ 150 ASSERT(atomic_read(&attrip->attri_refcount) > 0); 151 if (!atomic_dec_and_test(&attrip->attri_refcount)) 152 return; 153 154 xfs_trans_ail_delete(&attrip->attri_item, 0); 155 xfs_attri_item_free(attrip); 156} 157 158STATIC void 159xfs_attri_item_size( 160 struct xfs_log_item *lip, 161 int *nvecs, 162 int *nbytes) 163{ 164 struct xfs_attri_log_item *attrip = ATTRI_ITEM(lip); 165 struct xfs_attri_log_nameval *nv = attrip->attri_nameval; 166 167 *nvecs += 2; 168 *nbytes += sizeof(struct xfs_attri_log_format) + 169 xlog_calc_iovec_len(nv->name.iov_len); 170 171 if (nv->new_name.iov_len) { 172 *nvecs += 1; 173 *nbytes += xlog_calc_iovec_len(nv->new_name.iov_len); 174 } 175 176 if (nv->value.iov_len) { 177 *nvecs += 1; 178 *nbytes += xlog_calc_iovec_len(nv->value.iov_len); 179 } 180 181 if (nv->new_value.iov_len) { 182 *nvecs += 1; 183 *nbytes += xlog_calc_iovec_len(nv->new_value.iov_len); 184 } 185} 186 187/* 188 * This is called to fill in the log iovecs for the given attri log 189 * item. We use 1 iovec for the attri_format_item, 1 for the name, and 190 * another for the value if it is present 191 */ 192STATIC void 193xfs_attri_item_format( 194 struct xfs_log_item *lip, 195 struct xfs_log_vec *lv) 196{ 197 struct xfs_attri_log_item *attrip = ATTRI_ITEM(lip); 198 struct xfs_log_iovec *vecp = NULL; 199 struct xfs_attri_log_nameval *nv = attrip->attri_nameval; 200 201 attrip->attri_format.alfi_type = XFS_LI_ATTRI; 202 attrip->attri_format.alfi_size = 1; 203 204 /* 205 * This size accounting must be done before copying the attrip into the 206 * iovec. If we do it after, the wrong size will be recorded to the log 207 * and we trip across assertion checks for bad region sizes later during 208 * the log recovery. 209 */ 210 211 ASSERT(nv->name.iov_len > 0); 212 attrip->attri_format.alfi_size++; 213 214 if (nv->new_name.iov_len > 0) 215 attrip->attri_format.alfi_size++; 216 217 if (nv->value.iov_len > 0) 218 attrip->attri_format.alfi_size++; 219 220 if (nv->new_value.iov_len > 0) 221 attrip->attri_format.alfi_size++; 222 223 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_ATTRI_FORMAT, 224 &attrip->attri_format, 225 sizeof(struct xfs_attri_log_format)); 226 227 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_ATTR_NAME, nv->name.iov_base, 228 nv->name.iov_len); 229 230 if (nv->new_name.iov_len > 0) 231 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_ATTR_NEWNAME, 232 nv->new_name.iov_base, nv->new_name.iov_len); 233 234 if (nv->value.iov_len > 0) 235 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_ATTR_VALUE, 236 nv->value.iov_base, nv->value.iov_len); 237 238 if (nv->new_value.iov_len > 0) 239 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_ATTR_NEWVALUE, 240 nv->new_value.iov_base, nv->new_value.iov_len); 241} 242 243/* 244 * The unpin operation is the last place an ATTRI is manipulated in the log. It 245 * is either inserted in the AIL or aborted in the event of a log I/O error. In 246 * either case, the ATTRI transaction has been successfully committed to make 247 * it this far. Therefore, we expect whoever committed the ATTRI to either 248 * construct and commit the ATTRD or drop the ATTRD's reference in the event of 249 * error. Simply drop the log's ATTRI reference now that the log is done with 250 * it. 251 */ 252STATIC void 253xfs_attri_item_unpin( 254 struct xfs_log_item *lip, 255 int remove) 256{ 257 xfs_attri_release(ATTRI_ITEM(lip)); 258} 259 260 261STATIC void 262xfs_attri_item_release( 263 struct xfs_log_item *lip) 264{ 265 xfs_attri_release(ATTRI_ITEM(lip)); 266} 267 268/* 269 * Allocate and initialize an attri item. Caller may allocate an additional 270 * trailing buffer for name and value 271 */ 272STATIC struct xfs_attri_log_item * 273xfs_attri_init( 274 struct xfs_mount *mp, 275 struct xfs_attri_log_nameval *nv) 276{ 277 struct xfs_attri_log_item *attrip; 278 279 attrip = kmem_cache_zalloc(xfs_attri_cache, GFP_KERNEL | __GFP_NOFAIL); 280 281 /* 282 * Grab an extra reference to the name/value buffer for this log item. 283 * The caller retains its own reference! 284 */ 285 attrip->attri_nameval = xfs_attri_log_nameval_get(nv); 286 ASSERT(attrip->attri_nameval); 287 288 xfs_log_item_init(mp, &attrip->attri_item, XFS_LI_ATTRI, 289 &xfs_attri_item_ops); 290 attrip->attri_format.alfi_id = (uintptr_t)(void *)attrip; 291 atomic_set(&attrip->attri_refcount, 2); 292 293 return attrip; 294} 295 296static inline struct xfs_attrd_log_item *ATTRD_ITEM(struct xfs_log_item *lip) 297{ 298 return container_of(lip, struct xfs_attrd_log_item, attrd_item); 299} 300 301STATIC void 302xfs_attrd_item_free(struct xfs_attrd_log_item *attrdp) 303{ 304 kvfree(attrdp->attrd_item.li_lv_shadow); 305 kmem_cache_free(xfs_attrd_cache, attrdp); 306} 307 308STATIC void 309xfs_attrd_item_size( 310 struct xfs_log_item *lip, 311 int *nvecs, 312 int *nbytes) 313{ 314 *nvecs += 1; 315 *nbytes += sizeof(struct xfs_attrd_log_format); 316} 317 318/* 319 * This is called to fill in the log iovecs for the given attrd log item. We use 320 * only 1 iovec for the attrd_format, and we point that at the attr_log_format 321 * structure embedded in the attrd item. 322 */ 323STATIC void 324xfs_attrd_item_format( 325 struct xfs_log_item *lip, 326 struct xfs_log_vec *lv) 327{ 328 struct xfs_attrd_log_item *attrdp = ATTRD_ITEM(lip); 329 struct xfs_log_iovec *vecp = NULL; 330 331 attrdp->attrd_format.alfd_type = XFS_LI_ATTRD; 332 attrdp->attrd_format.alfd_size = 1; 333 334 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_ATTRD_FORMAT, 335 &attrdp->attrd_format, 336 sizeof(struct xfs_attrd_log_format)); 337} 338 339/* 340 * The ATTRD is either committed or aborted if the transaction is canceled. If 341 * the transaction is canceled, drop our reference to the ATTRI and free the 342 * ATTRD. 343 */ 344STATIC void 345xfs_attrd_item_release( 346 struct xfs_log_item *lip) 347{ 348 struct xfs_attrd_log_item *attrdp = ATTRD_ITEM(lip); 349 350 xfs_attri_release(attrdp->attrd_attrip); 351 xfs_attrd_item_free(attrdp); 352} 353 354static struct xfs_log_item * 355xfs_attrd_item_intent( 356 struct xfs_log_item *lip) 357{ 358 return &ATTRD_ITEM(lip)->attrd_attrip->attri_item; 359} 360 361static inline unsigned int 362xfs_attr_log_item_op(const struct xfs_attri_log_format *attrp) 363{ 364 return attrp->alfi_op_flags & XFS_ATTRI_OP_FLAGS_TYPE_MASK; 365} 366 367/* Log an attr to the intent item. */ 368STATIC void 369xfs_attr_log_item( 370 struct xfs_trans *tp, 371 struct xfs_attri_log_item *attrip, 372 const struct xfs_attr_intent *attr) 373{ 374 struct xfs_attri_log_format *attrp; 375 struct xfs_attri_log_nameval *nv = attr->xattri_nameval; 376 struct xfs_da_args *args = attr->xattri_da_args; 377 378 /* 379 * At this point the xfs_attr_intent has been constructed, and we've 380 * created the log intent. Fill in the attri log item and log format 381 * structure with fields from this xfs_attr_intent 382 */ 383 attrp = &attrip->attri_format; 384 attrp->alfi_ino = args->dp->i_ino; 385 ASSERT(!(attr->xattri_op_flags & ~XFS_ATTRI_OP_FLAGS_TYPE_MASK)); 386 attrp->alfi_op_flags = attr->xattri_op_flags; 387 attrp->alfi_value_len = nv->value.iov_len; 388 389 switch (xfs_attr_log_item_op(attrp)) { 390 case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE: 391 ASSERT(nv->value.iov_len == nv->new_value.iov_len); 392 393 attrp->alfi_igen = VFS_I(args->dp)->i_generation; 394 attrp->alfi_old_name_len = nv->name.iov_len; 395 attrp->alfi_new_name_len = nv->new_name.iov_len; 396 break; 397 case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE: 398 case XFS_ATTRI_OP_FLAGS_PPTR_SET: 399 attrp->alfi_igen = VFS_I(args->dp)->i_generation; 400 fallthrough; 401 default: 402 attrp->alfi_name_len = nv->name.iov_len; 403 break; 404 } 405 406 ASSERT(!(args->attr_filter & ~XFS_ATTRI_FILTER_MASK)); 407 attrp->alfi_attr_filter = args->attr_filter; 408} 409 410/* Get an ATTRI. */ 411static struct xfs_log_item * 412xfs_attr_create_intent( 413 struct xfs_trans *tp, 414 struct list_head *items, 415 unsigned int count, 416 bool sort) 417{ 418 struct xfs_mount *mp = tp->t_mountp; 419 struct xfs_attri_log_item *attrip; 420 struct xfs_attr_intent *attr; 421 struct xfs_da_args *args; 422 423 ASSERT(count == 1); 424 425 /* 426 * Each attr item only performs one attribute operation at a time, so 427 * this is a list of one 428 */ 429 attr = list_first_entry_or_null(items, struct xfs_attr_intent, 430 xattri_list); 431 args = attr->xattri_da_args; 432 433 if (!(args->op_flags & XFS_DA_OP_LOGGED)) 434 return NULL; 435 436 /* 437 * Create a buffer to store the attribute name and value. This buffer 438 * will be shared between the higher level deferred xattr work state 439 * and the lower level xattr log items. 440 */ 441 if (!attr->xattri_nameval) { 442 /* 443 * Transfer our reference to the name/value buffer to the 444 * deferred work state structure. 445 */ 446 attr->xattri_nameval = xfs_attri_log_nameval_alloc( 447 args->name, args->namelen, 448 args->new_name, args->new_namelen, 449 args->value, args->valuelen, 450 args->new_value, args->new_valuelen); 451 } 452 453 attrip = xfs_attri_init(mp, attr->xattri_nameval); 454 xfs_attr_log_item(tp, attrip, attr); 455 456 return &attrip->attri_item; 457} 458 459static inline void 460xfs_attr_free_item( 461 struct xfs_attr_intent *attr) 462{ 463 if (attr->xattri_da_state) 464 xfs_da_state_free(attr->xattri_da_state); 465 xfs_attri_log_nameval_put(attr->xattri_nameval); 466 if (attr->xattri_da_args->op_flags & XFS_DA_OP_RECOVERY) 467 kfree(attr); 468 else 469 kmem_cache_free(xfs_attr_intent_cache, attr); 470} 471 472static inline struct xfs_attr_intent *attri_entry(const struct list_head *e) 473{ 474 return list_entry(e, struct xfs_attr_intent, xattri_list); 475} 476 477/* Process an attr. */ 478STATIC int 479xfs_attr_finish_item( 480 struct xfs_trans *tp, 481 struct xfs_log_item *done, 482 struct list_head *item, 483 struct xfs_btree_cur **state) 484{ 485 struct xfs_attr_intent *attr = attri_entry(item); 486 struct xfs_da_args *args; 487 int error; 488 489 args = attr->xattri_da_args; 490 491 /* Reset trans after EAGAIN cycle since the transaction is new */ 492 args->trans = tp; 493 494 if (XFS_TEST_ERROR(args->dp->i_mount, XFS_ERRTAG_LARP)) { 495 error = -EIO; 496 goto out; 497 } 498 499 /* If an attr removal is trivially complete, we're done. */ 500 if (attr->xattri_op_flags == XFS_ATTRI_OP_FLAGS_REMOVE && 501 !xfs_inode_hasattr(args->dp)) { 502 error = 0; 503 goto out; 504 } 505 506 error = xfs_attr_set_iter(attr); 507 if (!error && attr->xattri_dela_state != XFS_DAS_DONE) 508 return -EAGAIN; 509 510out: 511 xfs_attr_free_item(attr); 512 return error; 513} 514 515/* Abort all pending ATTRs. */ 516STATIC void 517xfs_attr_abort_intent( 518 struct xfs_log_item *intent) 519{ 520 xfs_attri_release(ATTRI_ITEM(intent)); 521} 522 523/* Cancel an attr */ 524STATIC void 525xfs_attr_cancel_item( 526 struct list_head *item) 527{ 528 struct xfs_attr_intent *attr = attri_entry(item); 529 530 xfs_attr_free_item(attr); 531} 532 533STATIC bool 534xfs_attri_item_match( 535 struct xfs_log_item *lip, 536 uint64_t intent_id) 537{ 538 return ATTRI_ITEM(lip)->attri_format.alfi_id == intent_id; 539} 540 541static inline bool 542xfs_attri_validate_namelen(unsigned int namelen) 543{ 544 return namelen > 0 && namelen <= XATTR_NAME_MAX; 545} 546 547/* Is this recovered ATTRI format ok? */ 548static inline bool 549xfs_attri_validate( 550 struct xfs_mount *mp, 551 struct xfs_attri_log_format *attrp) 552{ 553 unsigned int op = xfs_attr_log_item_op(attrp); 554 555 if (attrp->alfi_op_flags & ~XFS_ATTRI_OP_FLAGS_TYPE_MASK) 556 return false; 557 558 if (attrp->alfi_attr_filter & ~XFS_ATTRI_FILTER_MASK) 559 return false; 560 561 if (!xfs_attr_check_namespace(attrp->alfi_attr_filter & 562 XFS_ATTR_NSP_ONDISK_MASK)) 563 return false; 564 565 switch (op) { 566 case XFS_ATTRI_OP_FLAGS_PPTR_SET: 567 case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE: 568 if (!xfs_has_parent(mp)) 569 return false; 570 if (attrp->alfi_value_len != sizeof(struct xfs_parent_rec)) 571 return false; 572 if (!xfs_attri_validate_namelen(attrp->alfi_name_len)) 573 return false; 574 if (!(attrp->alfi_attr_filter & XFS_ATTR_PARENT)) 575 return false; 576 break; 577 case XFS_ATTRI_OP_FLAGS_SET: 578 case XFS_ATTRI_OP_FLAGS_REPLACE: 579 if (!xfs_is_using_logged_xattrs(mp)) 580 return false; 581 if (attrp->alfi_value_len > XATTR_SIZE_MAX) 582 return false; 583 if (!xfs_attri_validate_namelen(attrp->alfi_name_len)) 584 return false; 585 break; 586 case XFS_ATTRI_OP_FLAGS_REMOVE: 587 if (!xfs_is_using_logged_xattrs(mp)) 588 return false; 589 if (attrp->alfi_value_len != 0) 590 return false; 591 if (!xfs_attri_validate_namelen(attrp->alfi_name_len)) 592 return false; 593 break; 594 case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE: 595 if (!xfs_has_parent(mp)) 596 return false; 597 if (!xfs_attri_validate_namelen(attrp->alfi_old_name_len)) 598 return false; 599 if (!xfs_attri_validate_namelen(attrp->alfi_new_name_len)) 600 return false; 601 if (attrp->alfi_value_len != sizeof(struct xfs_parent_rec)) 602 return false; 603 if (!(attrp->alfi_attr_filter & XFS_ATTR_PARENT)) 604 return false; 605 break; 606 default: 607 return false; 608 } 609 610 return xfs_verify_ino(mp, attrp->alfi_ino); 611} 612 613static int 614xfs_attri_iread_extents( 615 struct xfs_inode *ip) 616{ 617 struct xfs_trans *tp; 618 int error; 619 620 tp = xfs_trans_alloc_empty(ip->i_mount); 621 xfs_ilock(ip, XFS_ILOCK_EXCL); 622 error = xfs_iread_extents(tp, ip, XFS_ATTR_FORK); 623 xfs_iunlock(ip, XFS_ILOCK_EXCL); 624 xfs_trans_cancel(tp); 625 626 return error; 627} 628 629static inline struct xfs_attr_intent * 630xfs_attri_recover_work( 631 struct xfs_mount *mp, 632 struct xfs_defer_pending *dfp, 633 struct xfs_attri_log_format *attrp, 634 struct xfs_inode **ipp, 635 struct xfs_attri_log_nameval *nv) 636{ 637 struct xfs_attr_intent *attr; 638 struct xfs_da_args *args; 639 struct xfs_inode *ip; 640 int local; 641 int error; 642 643 /* 644 * Parent pointer attr items record the generation but regular logged 645 * xattrs do not; select the right iget function. 646 */ 647 switch (xfs_attr_log_item_op(attrp)) { 648 case XFS_ATTRI_OP_FLAGS_PPTR_SET: 649 case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE: 650 case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE: 651 error = xlog_recover_iget_handle(mp, attrp->alfi_ino, 652 attrp->alfi_igen, &ip); 653 break; 654 default: 655 error = xlog_recover_iget(mp, attrp->alfi_ino, &ip); 656 break; 657 } 658 if (error) { 659 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, attrp, 660 sizeof(*attrp)); 661 return ERR_PTR(-EFSCORRUPTED); 662 } 663 664 if (xfs_inode_has_attr_fork(ip)) { 665 error = xfs_attri_iread_extents(ip); 666 if (error) { 667 xfs_irele(ip); 668 return ERR_PTR(error); 669 } 670 } 671 672 attr = kzalloc(sizeof(struct xfs_attr_intent) + 673 sizeof(struct xfs_da_args), GFP_KERNEL | __GFP_NOFAIL); 674 args = (struct xfs_da_args *)(attr + 1); 675 676 attr->xattri_da_args = args; 677 attr->xattri_op_flags = xfs_attr_log_item_op(attrp); 678 679 /* 680 * We're reconstructing the deferred work state structure from the 681 * recovered log item. Grab a reference to the name/value buffer and 682 * attach it to the new work state. 683 */ 684 attr->xattri_nameval = xfs_attri_log_nameval_get(nv); 685 ASSERT(attr->xattri_nameval); 686 687 args->dp = ip; 688 args->geo = mp->m_attr_geo; 689 args->whichfork = XFS_ATTR_FORK; 690 args->name = nv->name.iov_base; 691 args->namelen = nv->name.iov_len; 692 args->new_name = nv->new_name.iov_base; 693 args->new_namelen = nv->new_name.iov_len; 694 args->value = nv->value.iov_base; 695 args->valuelen = nv->value.iov_len; 696 args->new_value = nv->new_value.iov_base; 697 args->new_valuelen = nv->new_value.iov_len; 698 args->attr_filter = attrp->alfi_attr_filter & XFS_ATTRI_FILTER_MASK; 699 args->op_flags = XFS_DA_OP_RECOVERY | XFS_DA_OP_OKNOENT | 700 XFS_DA_OP_LOGGED; 701 args->owner = args->dp->i_ino; 702 xfs_attr_sethash(args); 703 704 switch (xfs_attr_intent_op(attr)) { 705 case XFS_ATTRI_OP_FLAGS_PPTR_SET: 706 case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE: 707 case XFS_ATTRI_OP_FLAGS_SET: 708 case XFS_ATTRI_OP_FLAGS_REPLACE: 709 args->total = xfs_attr_calc_size(args, &local); 710 if (xfs_inode_hasattr(args->dp)) 711 attr->xattri_dela_state = xfs_attr_init_replace_state(args); 712 else 713 attr->xattri_dela_state = xfs_attr_init_add_state(args); 714 break; 715 case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE: 716 case XFS_ATTRI_OP_FLAGS_REMOVE: 717 attr->xattri_dela_state = xfs_attr_init_remove_state(args); 718 break; 719 } 720 721 xfs_defer_add_item(dfp, &attr->xattri_list); 722 *ipp = ip; 723 return attr; 724} 725 726/* 727 * Process an attr intent item that was recovered from the log. We need to 728 * delete the attr that it describes. 729 */ 730STATIC int 731xfs_attr_recover_work( 732 struct xfs_defer_pending *dfp, 733 struct list_head *capture_list) 734{ 735 struct xfs_log_item *lip = dfp->dfp_intent; 736 struct xfs_attri_log_item *attrip = ATTRI_ITEM(lip); 737 struct xfs_attr_intent *attr; 738 struct xfs_mount *mp = lip->li_log->l_mp; 739 struct xfs_inode *ip = NULL; 740 struct xfs_da_args *args; 741 struct xfs_trans *tp; 742 struct xfs_trans_res resv; 743 struct xfs_attri_log_format *attrp; 744 struct xfs_attri_log_nameval *nv = attrip->attri_nameval; 745 int error; 746 unsigned int total = 0; 747 748 /* 749 * First check the validity of the attr described by the ATTRI. If any 750 * are bad, then assume that all are bad and just toss the ATTRI. 751 */ 752 attrp = &attrip->attri_format; 753 if (!xfs_attri_validate(mp, attrp) || 754 !xfs_attr_namecheck(attrp->alfi_attr_filter, nv->name.iov_base, 755 nv->name.iov_len)) 756 return -EFSCORRUPTED; 757 758 attr = xfs_attri_recover_work(mp, dfp, attrp, &ip, nv); 759 if (IS_ERR(attr)) 760 return PTR_ERR(attr); 761 args = attr->xattri_da_args; 762 763 switch (xfs_attr_intent_op(attr)) { 764 case XFS_ATTRI_OP_FLAGS_PPTR_SET: 765 case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE: 766 case XFS_ATTRI_OP_FLAGS_SET: 767 case XFS_ATTRI_OP_FLAGS_REPLACE: 768 resv = xfs_attr_set_resv(args); 769 total = args->total; 770 break; 771 case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE: 772 case XFS_ATTRI_OP_FLAGS_REMOVE: 773 resv = M_RES(mp)->tr_attrrm; 774 total = XFS_ATTRRM_SPACE_RES(mp); 775 break; 776 } 777 resv = xlog_recover_resv(&resv); 778 error = xfs_trans_alloc(mp, &resv, total, 0, XFS_TRANS_RESERVE, &tp); 779 if (error) 780 return error; 781 args->trans = tp; 782 783 xfs_ilock(ip, XFS_ILOCK_EXCL); 784 xfs_trans_ijoin(tp, ip, 0); 785 786 error = xlog_recover_finish_intent(tp, dfp); 787 if (error == -EFSCORRUPTED) 788 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 789 &attrip->attri_format, 790 sizeof(attrip->attri_format)); 791 if (error) 792 goto out_cancel; 793 794 error = xfs_defer_ops_capture_and_commit(tp, capture_list); 795out_unlock: 796 xfs_iunlock(ip, XFS_ILOCK_EXCL); 797 xfs_irele(ip); 798 return error; 799out_cancel: 800 xfs_trans_cancel(tp); 801 goto out_unlock; 802} 803 804/* Re-log an intent item to push the log tail forward. */ 805static struct xfs_log_item * 806xfs_attr_relog_intent( 807 struct xfs_trans *tp, 808 struct xfs_log_item *intent, 809 struct xfs_log_item *done_item) 810{ 811 struct xfs_attri_log_item *old_attrip; 812 struct xfs_attri_log_item *new_attrip; 813 struct xfs_attri_log_format *new_attrp; 814 struct xfs_attri_log_format *old_attrp; 815 816 old_attrip = ATTRI_ITEM(intent); 817 old_attrp = &old_attrip->attri_format; 818 819 /* 820 * Create a new log item that shares the same name/value buffer as the 821 * old log item. 822 */ 823 new_attrip = xfs_attri_init(tp->t_mountp, old_attrip->attri_nameval); 824 new_attrp = &new_attrip->attri_format; 825 826 new_attrp->alfi_ino = old_attrp->alfi_ino; 827 new_attrp->alfi_igen = old_attrp->alfi_igen; 828 new_attrp->alfi_op_flags = old_attrp->alfi_op_flags; 829 new_attrp->alfi_value_len = old_attrp->alfi_value_len; 830 831 switch (xfs_attr_log_item_op(old_attrp)) { 832 case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE: 833 new_attrp->alfi_new_name_len = old_attrp->alfi_new_name_len; 834 new_attrp->alfi_old_name_len = old_attrp->alfi_old_name_len; 835 break; 836 default: 837 new_attrp->alfi_name_len = old_attrp->alfi_name_len; 838 break; 839 } 840 841 new_attrp->alfi_attr_filter = old_attrp->alfi_attr_filter; 842 843 return &new_attrip->attri_item; 844} 845 846/* Get an ATTRD so we can process all the attrs. */ 847static struct xfs_log_item * 848xfs_attr_create_done( 849 struct xfs_trans *tp, 850 struct xfs_log_item *intent, 851 unsigned int count) 852{ 853 struct xfs_attri_log_item *attrip; 854 struct xfs_attrd_log_item *attrdp; 855 856 attrip = ATTRI_ITEM(intent); 857 858 attrdp = kmem_cache_zalloc(xfs_attrd_cache, GFP_KERNEL | __GFP_NOFAIL); 859 860 xfs_log_item_init(tp->t_mountp, &attrdp->attrd_item, XFS_LI_ATTRD, 861 &xfs_attrd_item_ops); 862 attrdp->attrd_attrip = attrip; 863 attrdp->attrd_format.alfd_alf_id = attrip->attri_format.alfi_id; 864 865 return &attrdp->attrd_item; 866} 867 868void 869xfs_attr_defer_add( 870 struct xfs_da_args *args, 871 enum xfs_attr_defer_op op) 872{ 873 struct xfs_attr_intent *new; 874 unsigned int log_op = 0; 875 bool is_pptr = args->attr_filter & XFS_ATTR_PARENT; 876 877 if (is_pptr) { 878 ASSERT(xfs_has_parent(args->dp->i_mount)); 879 ASSERT((args->attr_filter & ~XFS_ATTR_PARENT) == 0); 880 ASSERT(args->op_flags & XFS_DA_OP_LOGGED); 881 ASSERT(args->valuelen == sizeof(struct xfs_parent_rec)); 882 } 883 884 new = kmem_cache_zalloc(xfs_attr_intent_cache, 885 GFP_NOFS | __GFP_NOFAIL); 886 new->xattri_da_args = args; 887 888 /* Compute log operation from the higher level op and namespace. */ 889 switch (op) { 890 case XFS_ATTR_DEFER_SET: 891 if (is_pptr) 892 log_op = XFS_ATTRI_OP_FLAGS_PPTR_SET; 893 else 894 log_op = XFS_ATTRI_OP_FLAGS_SET; 895 break; 896 case XFS_ATTR_DEFER_REPLACE: 897 if (is_pptr) 898 log_op = XFS_ATTRI_OP_FLAGS_PPTR_REPLACE; 899 else 900 log_op = XFS_ATTRI_OP_FLAGS_REPLACE; 901 break; 902 case XFS_ATTR_DEFER_REMOVE: 903 if (is_pptr) 904 log_op = XFS_ATTRI_OP_FLAGS_PPTR_REMOVE; 905 else 906 log_op = XFS_ATTRI_OP_FLAGS_REMOVE; 907 break; 908 default: 909 ASSERT(0); 910 break; 911 } 912 new->xattri_op_flags = log_op; 913 914 /* Set up initial attr operation state. */ 915 switch (log_op) { 916 case XFS_ATTRI_OP_FLAGS_PPTR_SET: 917 case XFS_ATTRI_OP_FLAGS_SET: 918 new->xattri_dela_state = xfs_attr_init_add_state(args); 919 break; 920 case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE: 921 ASSERT(args->new_valuelen == args->valuelen); 922 new->xattri_dela_state = xfs_attr_init_replace_state(args); 923 break; 924 case XFS_ATTRI_OP_FLAGS_REPLACE: 925 new->xattri_dela_state = xfs_attr_init_replace_state(args); 926 break; 927 case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE: 928 case XFS_ATTRI_OP_FLAGS_REMOVE: 929 new->xattri_dela_state = xfs_attr_init_remove_state(args); 930 break; 931 } 932 933 xfs_defer_add(args->trans, &new->xattri_list, &xfs_attr_defer_type); 934 trace_xfs_attr_defer_add(new->xattri_dela_state, args->dp); 935} 936 937const struct xfs_defer_op_type xfs_attr_defer_type = { 938 .name = "attr", 939 .max_items = 1, 940 .create_intent = xfs_attr_create_intent, 941 .abort_intent = xfs_attr_abort_intent, 942 .create_done = xfs_attr_create_done, 943 .finish_item = xfs_attr_finish_item, 944 .cancel_item = xfs_attr_cancel_item, 945 .recover_work = xfs_attr_recover_work, 946 .relog_intent = xfs_attr_relog_intent, 947}; 948 949static inline void * 950xfs_attri_validate_name_iovec( 951 struct xfs_mount *mp, 952 struct xfs_attri_log_format *attri_formatp, 953 const struct kvec *iovec, 954 unsigned int name_len) 955{ 956 if (iovec->iov_len != xlog_calc_iovec_len(name_len)) { 957 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 958 attri_formatp, sizeof(*attri_formatp)); 959 return NULL; 960 } 961 962 if (!xfs_attr_namecheck(attri_formatp->alfi_attr_filter, iovec->iov_base, 963 name_len)) { 964 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 965 attri_formatp, sizeof(*attri_formatp)); 966 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 967 iovec->iov_base, iovec->iov_len); 968 return NULL; 969 } 970 971 return iovec->iov_base; 972} 973 974static inline void * 975xfs_attri_validate_value_iovec( 976 struct xfs_mount *mp, 977 struct xfs_attri_log_format *attri_formatp, 978 const struct kvec *iovec, 979 unsigned int value_len) 980{ 981 if (iovec->iov_len != xlog_calc_iovec_len(value_len)) { 982 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 983 attri_formatp, sizeof(*attri_formatp)); 984 return NULL; 985 } 986 987 if ((attri_formatp->alfi_attr_filter & XFS_ATTR_PARENT) && 988 !xfs_parent_valuecheck(mp, iovec->iov_base, value_len)) { 989 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 990 attri_formatp, sizeof(*attri_formatp)); 991 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 992 iovec->iov_base, iovec->iov_len); 993 return NULL; 994 } 995 996 return iovec->iov_base; 997} 998 999STATIC int 1000xlog_recover_attri_commit_pass2( 1001 struct xlog *log, 1002 struct list_head *buffer_list, 1003 struct xlog_recover_item *item, 1004 xfs_lsn_t lsn) 1005{ 1006 struct xfs_mount *mp = log->l_mp; 1007 struct xfs_attri_log_item *attrip; 1008 struct xfs_attri_log_format *attri_formatp; 1009 struct xfs_attri_log_nameval *nv; 1010 const void *attr_name; 1011 const void *attr_value = NULL; 1012 const void *attr_new_name = NULL; 1013 const void *attr_new_value = NULL; 1014 size_t len; 1015 unsigned int name_len = 0; 1016 unsigned int value_len = 0; 1017 unsigned int new_name_len = 0; 1018 unsigned int new_value_len = 0; 1019 unsigned int op, i = 0; 1020 1021 /* Validate xfs_attri_log_format before the large memory allocation */ 1022 len = sizeof(struct xfs_attri_log_format); 1023 if (item->ri_buf[i].iov_len != len) { 1024 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 1025 item->ri_buf[0].iov_base, item->ri_buf[0].iov_len); 1026 return -EFSCORRUPTED; 1027 } 1028 1029 attri_formatp = item->ri_buf[i].iov_base; 1030 if (!xfs_attri_validate(mp, attri_formatp)) { 1031 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 1032 attri_formatp, len); 1033 return -EFSCORRUPTED; 1034 } 1035 1036 /* Check the number of log iovecs makes sense for the op code. */ 1037 op = xfs_attr_log_item_op(attri_formatp); 1038 switch (op) { 1039 case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE: 1040 case XFS_ATTRI_OP_FLAGS_PPTR_SET: 1041 /* Log item, attr name, attr value */ 1042 if (item->ri_total != 3) { 1043 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 1044 attri_formatp, len); 1045 return -EFSCORRUPTED; 1046 } 1047 name_len = attri_formatp->alfi_name_len; 1048 value_len = attri_formatp->alfi_value_len; 1049 break; 1050 case XFS_ATTRI_OP_FLAGS_SET: 1051 case XFS_ATTRI_OP_FLAGS_REPLACE: 1052 /* Log item, attr name, optional attr value */ 1053 if (item->ri_total != 2 + !!attri_formatp->alfi_value_len) { 1054 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 1055 attri_formatp, len); 1056 return -EFSCORRUPTED; 1057 } 1058 name_len = attri_formatp->alfi_name_len; 1059 value_len = attri_formatp->alfi_value_len; 1060 break; 1061 case XFS_ATTRI_OP_FLAGS_REMOVE: 1062 /* Log item, attr name */ 1063 if (item->ri_total != 2) { 1064 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 1065 attri_formatp, len); 1066 return -EFSCORRUPTED; 1067 } 1068 name_len = attri_formatp->alfi_name_len; 1069 break; 1070 case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE: 1071 /* 1072 * Log item, attr name, new attr name, attr value, new attr 1073 * value 1074 */ 1075 if (item->ri_total != 5) { 1076 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 1077 attri_formatp, len); 1078 return -EFSCORRUPTED; 1079 } 1080 name_len = attri_formatp->alfi_old_name_len; 1081 new_name_len = attri_formatp->alfi_new_name_len; 1082 new_value_len = value_len = attri_formatp->alfi_value_len; 1083 break; 1084 default: 1085 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 1086 attri_formatp, len); 1087 return -EFSCORRUPTED; 1088 } 1089 i++; 1090 1091 /* Validate the attr name */ 1092 attr_name = xfs_attri_validate_name_iovec(mp, attri_formatp, 1093 &item->ri_buf[i], name_len); 1094 if (!attr_name) 1095 return -EFSCORRUPTED; 1096 i++; 1097 1098 /* Validate the new attr name */ 1099 if (new_name_len > 0) { 1100 attr_new_name = xfs_attri_validate_name_iovec(mp, 1101 attri_formatp, &item->ri_buf[i], 1102 new_name_len); 1103 if (!attr_new_name) 1104 return -EFSCORRUPTED; 1105 i++; 1106 } 1107 1108 /* Validate the attr value, if present */ 1109 if (value_len != 0) { 1110 attr_value = xfs_attri_validate_value_iovec(mp, attri_formatp, 1111 &item->ri_buf[i], value_len); 1112 if (!attr_value) 1113 return -EFSCORRUPTED; 1114 i++; 1115 } 1116 1117 /* Validate the new attr value, if present */ 1118 if (new_value_len != 0) { 1119 attr_new_value = xfs_attri_validate_value_iovec(mp, 1120 attri_formatp, &item->ri_buf[i], 1121 new_value_len); 1122 if (!attr_new_value) 1123 return -EFSCORRUPTED; 1124 i++; 1125 } 1126 1127 /* 1128 * Make sure we got the correct number of buffers for the operation 1129 * that we just loaded. 1130 */ 1131 if (i != item->ri_total) { 1132 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 1133 attri_formatp, len); 1134 return -EFSCORRUPTED; 1135 } 1136 1137 switch (op) { 1138 case XFS_ATTRI_OP_FLAGS_REMOVE: 1139 /* Regular remove operations operate only on names. */ 1140 if (attr_value != NULL || value_len != 0) { 1141 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 1142 attri_formatp, len); 1143 return -EFSCORRUPTED; 1144 } 1145 fallthrough; 1146 case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE: 1147 case XFS_ATTRI_OP_FLAGS_PPTR_SET: 1148 case XFS_ATTRI_OP_FLAGS_SET: 1149 case XFS_ATTRI_OP_FLAGS_REPLACE: 1150 /* 1151 * Regular xattr set/remove/replace operations require a name 1152 * and do not take a newname. Values are optional for set and 1153 * replace. 1154 * 1155 * Name-value set/remove operations must have a name, do not 1156 * take a newname, and can take a value. 1157 */ 1158 if (attr_name == NULL || name_len == 0) { 1159 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 1160 attri_formatp, len); 1161 return -EFSCORRUPTED; 1162 } 1163 break; 1164 case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE: 1165 /* 1166 * Name-value replace operations require the caller to 1167 * specify the old and new names and values explicitly. 1168 * Values are optional. 1169 */ 1170 if (attr_name == NULL || name_len == 0) { 1171 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 1172 attri_formatp, len); 1173 return -EFSCORRUPTED; 1174 } 1175 if (attr_new_name == NULL || new_name_len == 0) { 1176 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 1177 attri_formatp, len); 1178 return -EFSCORRUPTED; 1179 } 1180 break; 1181 } 1182 1183 /* 1184 * Memory alloc failure will cause replay to abort. We attach the 1185 * name/value buffer to the recovered incore log item and drop our 1186 * reference. 1187 */ 1188 nv = xfs_attri_log_nameval_alloc(attr_name, name_len, 1189 attr_new_name, new_name_len, 1190 attr_value, value_len, 1191 attr_new_value, new_value_len); 1192 1193 attrip = xfs_attri_init(mp, nv); 1194 memcpy(&attrip->attri_format, attri_formatp, len); 1195 1196 xlog_recover_intent_item(log, &attrip->attri_item, lsn, 1197 &xfs_attr_defer_type); 1198 xfs_attri_log_nameval_put(nv); 1199 return 0; 1200} 1201 1202/* 1203 * This routine is called when an ATTRD format structure is found in a committed 1204 * transaction in the log. Its purpose is to cancel the corresponding ATTRI if 1205 * it was still in the log. To do this it searches the AIL for the ATTRI with 1206 * an id equal to that in the ATTRD format structure. If we find it we drop 1207 * the ATTRD reference, which removes the ATTRI from the AIL and frees it. 1208 */ 1209STATIC int 1210xlog_recover_attrd_commit_pass2( 1211 struct xlog *log, 1212 struct list_head *buffer_list, 1213 struct xlog_recover_item *item, 1214 xfs_lsn_t lsn) 1215{ 1216 struct xfs_attrd_log_format *attrd_formatp; 1217 1218 attrd_formatp = item->ri_buf[0].iov_base; 1219 if (item->ri_buf[0].iov_len != sizeof(struct xfs_attrd_log_format)) { 1220 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp, 1221 item->ri_buf[0].iov_base, item->ri_buf[0].iov_len); 1222 return -EFSCORRUPTED; 1223 } 1224 1225 xlog_recover_release_intent(log, XFS_LI_ATTRI, 1226 attrd_formatp->alfd_alf_id); 1227 return 0; 1228} 1229 1230static const struct xfs_item_ops xfs_attri_item_ops = { 1231 .flags = XFS_ITEM_INTENT, 1232 .iop_size = xfs_attri_item_size, 1233 .iop_format = xfs_attri_item_format, 1234 .iop_unpin = xfs_attri_item_unpin, 1235 .iop_release = xfs_attri_item_release, 1236 .iop_match = xfs_attri_item_match, 1237}; 1238 1239const struct xlog_recover_item_ops xlog_attri_item_ops = { 1240 .item_type = XFS_LI_ATTRI, 1241 .commit_pass2 = xlog_recover_attri_commit_pass2, 1242}; 1243 1244static const struct xfs_item_ops xfs_attrd_item_ops = { 1245 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED | 1246 XFS_ITEM_INTENT_DONE, 1247 .iop_size = xfs_attrd_item_size, 1248 .iop_format = xfs_attrd_item_format, 1249 .iop_release = xfs_attrd_item_release, 1250 .iop_intent = xfs_attrd_item_intent, 1251}; 1252 1253const struct xlog_recover_item_ops xlog_attrd_item_ops = { 1254 .item_type = XFS_LI_ATTRD, 1255 .commit_pass2 = xlog_recover_attrd_commit_pass2, 1256};
~/
Read-only file preview.