LCOV - code coverage report
Current view: top level - fs/xfs - xfs_attr_item.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-acha @ Mon Jul 31 20:08:06 PDT 2023 Lines: 428 482 88.8 %
Date: 2023-07-31 20:08:07 Functions: 30 30 100.0 %

          Line data    Source code
       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             : 
      32             : struct kmem_cache               *xfs_attri_cache;
      33             : struct kmem_cache               *xfs_attrd_cache;
      34             : 
      35             : static const struct xfs_item_ops xfs_attri_item_ops;
      36             : static const struct xfs_item_ops xfs_attrd_item_ops;
      37             : static struct xfs_attrd_log_item *xfs_trans_get_attrd(struct xfs_trans *tp,
      38             :                                         struct xfs_attri_log_item *attrip);
      39             : 
      40             : static inline struct xfs_attri_log_item *ATTRI_ITEM(struct xfs_log_item *lip)
      41             : {
      42             :         return container_of(lip, struct xfs_attri_log_item, attri_item);
      43             : }
      44             : 
      45             : /*
      46             :  * Shared xattr name/value buffers for logged extended attribute operations
      47             :  *
      48             :  * When logging updates to extended attributes, we can create quite a few
      49             :  * attribute log intent items for a single xattr update.  To avoid cycling the
      50             :  * memory allocator and memcpy overhead, the name (and value, for setxattr)
      51             :  * are kept in a refcounted object that is shared across all related log items
      52             :  * and the upper-level deferred work state structure.  The shared buffer has
      53             :  * a control structure, followed by the name, and then the value.
      54             :  */
      55             : 
      56             : static inline struct xfs_attri_log_nameval *
      57   182109766 : xfs_attri_log_nameval_get(
      58             :         struct xfs_attri_log_nameval    *nv)
      59             : {
      60   182109766 :         if (!refcount_inc_not_zero(&nv->refcount))
      61           0 :                 return NULL;
      62             :         return nv;
      63             : }
      64             : 
      65             : static inline void
      66   458490337 : xfs_attri_log_nameval_put(
      67             :         struct xfs_attri_log_nameval    *nv)
      68             : {
      69   458490337 :         if (!nv)
      70             :                 return;
      71   326643299 :         if (refcount_dec_and_test(&nv->refcount))
      72   144548187 :                 kvfree(nv);
      73             : }
      74             : 
      75             : static inline struct xfs_attri_log_nameval *
      76   144524745 : xfs_attri_log_nameval_alloc(
      77             :         const void                      *name,
      78             :         unsigned int                    name_len,
      79             :         const void                      *new_name,
      80             :         unsigned int                    new_name_len,
      81             :         const void                      *value,
      82             :         unsigned int                    value_len,
      83             :         const void                      *new_value,
      84             :         unsigned int                    new_value_len)
      85             : {
      86   144524745 :         struct xfs_attri_log_nameval    *nv;
      87             : 
      88             :         /*
      89             :          * This could be over 64kB in length, so we have to use kvmalloc() for
      90             :          * this. But kvmalloc() utterly sucks, so we use our own version.
      91             :          */
      92   144524745 :         nv = xlog_kvmalloc(sizeof(struct xfs_attri_log_nameval) +
      93   144524745 :                                         name_len + new_name_len + value_len +
      94             :                                         new_value_len);
      95             : 
      96   144526389 :         nv->name.i_addr = nv + 1;
      97   144526389 :         nv->name.i_len = name_len;
      98   144526389 :         nv->name.i_type = XLOG_REG_TYPE_ATTR_NAME;
      99   289052778 :         memcpy(nv->name.i_addr, name, name_len);
     100             : 
     101   144526389 :         if (new_name_len) {
     102    37417647 :                 nv->new_name.i_addr = nv->name.i_addr + name_len;
     103    37417647 :                 nv->new_name.i_len = new_name_len;
     104    74835294 :                 memcpy(nv->new_name.i_addr, new_name, new_name_len);
     105             :         } else {
     106   107108742 :                 nv->new_name.i_addr = NULL;
     107   107108742 :                 nv->new_name.i_len = 0;
     108             :         }
     109   144526389 :         nv->new_name.i_type = XLOG_REG_TYPE_ATTR_NEWNAME;
     110             : 
     111   144526389 :         if (value_len) {
     112   144524490 :                 nv->value.i_addr = nv->name.i_addr + name_len + new_name_len;
     113   144524490 :                 nv->value.i_len = value_len;
     114   289048980 :                 memcpy(nv->value.i_addr, value, value_len);
     115             :         } else {
     116        2142 :                 nv->value.i_addr = NULL;
     117        2142 :                 nv->value.i_len = 0;
     118             :         }
     119   144526389 :         nv->value.i_type = XLOG_REG_TYPE_ATTR_VALUE;
     120             : 
     121   144526389 :         if (new_value_len) {
     122    37417625 :                 nv->new_value.i_addr = nv->name.i_addr + name_len +
     123    37417625 :                                                 new_name_len + value_len;
     124    37417625 :                 nv->new_value.i_len = new_value_len;
     125    74835250 :                 memcpy(nv->new_value.i_addr, new_value, new_value_len);
     126             :         } else {
     127   107108764 :                 nv->new_value.i_addr = NULL;
     128   107108764 :                 nv->new_value.i_len = 0;
     129             :         }
     130   144526389 :         nv->new_value.i_type = XLOG_REG_TYPE_ATTR_NEWVALUE;
     131             : 
     132   144526389 :         refcount_set(&nv->refcount, 1);
     133   144526389 :         return nv;
     134             : }
     135             : 
     136             : STATIC void
     137   182153476 : xfs_attri_item_free(
     138             :         struct xfs_attri_log_item       *attrip)
     139             : {
     140   182153476 :         kmem_free(attrip->attri_item.li_lv_shadow);
     141   182153367 :         xfs_attri_log_nameval_put(attrip->attri_nameval);
     142   182149324 :         kmem_cache_free(xfs_attri_cache, attrip);
     143   182152836 : }
     144             : 
     145             : /*
     146             :  * Freeing the attrip requires that we remove it from the AIL if it has already
     147             :  * been placed there. However, the ATTRI may not yet have been placed in the
     148             :  * AIL when called by xfs_attri_release() from ATTRD processing due to the
     149             :  * ordering of committed vs unpin operations in bulk insert operations. Hence
     150             :  * the reference count to ensure only the last caller frees the ATTRI.
     151             :  */
     152             : STATIC void
     153   364189762 : xfs_attri_release(
     154             :         struct xfs_attri_log_item       *attrip)
     155             : {
     156   364189762 :         ASSERT(atomic_read(&attrip->attri_refcount) > 0);
     157   728434865 :         if (!atomic_dec_and_test(&attrip->attri_refcount))
     158             :                 return;
     159             : 
     160   182148754 :         xfs_trans_ail_delete(&attrip->attri_item, 0);
     161   182153427 :         xfs_attri_item_free(attrip);
     162             : }
     163             : 
     164             : STATIC void
     165   182139911 : xfs_attri_item_size(
     166             :         struct xfs_log_item             *lip,
     167             :         int                             *nvecs,
     168             :         int                             *nbytes)
     169             : {
     170   182139911 :         struct xfs_attri_log_item       *attrip = ATTRI_ITEM(lip);
     171   182139911 :         struct xfs_attri_log_nameval    *nv = attrip->attri_nameval;
     172             : 
     173   182139911 :         *nvecs += 2;
     174   182139911 :         *nbytes += sizeof(struct xfs_attri_log_format) +
     175   182139911 :                         xlog_calc_iovec_len(nv->name.i_len);
     176             : 
     177   182139911 :         if (nv->new_name.i_len) {
     178    74972383 :                 *nvecs += 1;
     179    74972383 :                 *nbytes += xlog_calc_iovec_len(nv->new_name.i_len);
     180             :         }
     181             : 
     182   182139911 :         if (nv->value.i_len) {
     183   182141030 :                 *nvecs += 1;
     184   182141030 :                 *nbytes += xlog_calc_iovec_len(nv->value.i_len);
     185             :         }
     186             : 
     187   182139911 :         if (nv->new_value.i_len) {
     188    74972374 :                 *nvecs += 1;
     189    74972374 :                 *nbytes += xlog_calc_iovec_len(nv->new_value.i_len);
     190             :         }
     191   182139911 : }
     192             : 
     193             : /*
     194             :  * This is called to fill in the log iovecs for the given attri log
     195             :  * item. We use  1 iovec for the attri_format_item, 1 for the name, and
     196             :  * another for the value if it is present
     197             :  */
     198             : STATIC void
     199   182143701 : xfs_attri_item_format(
     200             :         struct xfs_log_item             *lip,
     201             :         struct xfs_log_vec              *lv)
     202             : {
     203   182143701 :         struct xfs_attri_log_item       *attrip = ATTRI_ITEM(lip);
     204   182143701 :         struct xfs_log_iovec            *vecp = NULL;
     205   182143701 :         struct xfs_attri_log_nameval    *nv = attrip->attri_nameval;
     206             : 
     207   182143701 :         attrip->attri_format.alfi_type = XFS_LI_ATTRI;
     208   182143701 :         attrip->attri_format.alfi_size = 1;
     209             : 
     210             :         /*
     211             :          * This size accounting must be done before copying the attrip into the
     212             :          * iovec.  If we do it after, the wrong size will be recorded to the log
     213             :          * and we trip across assertion checks for bad region sizes later during
     214             :          * the log recovery.
     215             :          */
     216             : 
     217   182143701 :         ASSERT(nv->name.i_len > 0);
     218   182143701 :         attrip->attri_format.alfi_size++;
     219             : 
     220   182143701 :         if (nv->new_name.i_len > 0)
     221    74972405 :                 attrip->attri_format.alfi_size++;
     222             : 
     223   182143701 :         if (nv->value.i_len > 0)
     224   182145345 :                 attrip->attri_format.alfi_size++;
     225             : 
     226   182143701 :         if (nv->new_value.i_len > 0)
     227    74972409 :                 attrip->attri_format.alfi_size++;
     228             : 
     229   182143701 :         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_ATTRI_FORMAT,
     230   182143701 :                         &attrip->attri_format,
     231             :                         sizeof(struct xfs_attri_log_format));
     232   182141797 :         xlog_copy_from_iovec(lv, &vecp, &nv->name);
     233             : 
     234   182146494 :         if (nv->new_name.i_len > 0)
     235    74972421 :                 xlog_copy_from_iovec(lv, &vecp, &nv->new_name);
     236             : 
     237   182146496 :         if (nv->value.i_len > 0)
     238   182145632 :                 xlog_copy_from_iovec(lv, &vecp, &nv->value);
     239             : 
     240   182145586 :         if (nv->new_value.i_len > 0)
     241    74972419 :                 xlog_copy_from_iovec(lv, &vecp, &nv->new_value);
     242   182145585 : }
     243             : 
     244             : /*
     245             :  * The unpin operation is the last place an ATTRI is manipulated in the log. It
     246             :  * is either inserted in the AIL or aborted in the event of a log I/O error. In
     247             :  * either case, the ATTRI transaction has been successfully committed to make
     248             :  * it this far. Therefore, we expect whoever committed the ATTRI to either
     249             :  * construct and commit the ATTRD or drop the ATTRD's reference in the event of
     250             :  * error. Simply drop the log's ATTRI reference now that the log is done with
     251             :  * it.
     252             :  */
     253             : STATIC void
     254   182141776 : xfs_attri_item_unpin(
     255             :         struct xfs_log_item     *lip,
     256             :         int                     remove)
     257             : {
     258   182141776 :         xfs_attri_release(ATTRI_ITEM(lip));
     259   182151030 : }
     260             : 
     261             : 
     262             : STATIC void
     263        1466 : xfs_attri_item_release(
     264             :         struct xfs_log_item     *lip)
     265             : {
     266        1466 :         xfs_attri_release(ATTRI_ITEM(lip));
     267        1466 : }
     268             : 
     269             : /*
     270             :  * Allocate and initialize an attri item.  Caller may allocate an additional
     271             :  * trailing buffer for name and value
     272             :  */
     273             : STATIC struct xfs_attri_log_item *
     274   182124224 : xfs_attri_init(
     275             :         struct xfs_mount                *mp,
     276             :         struct xfs_attri_log_nameval    *nv)
     277             : {
     278   182124224 :         struct xfs_attri_log_item       *attrip;
     279             : 
     280   182124224 :         attrip = kmem_cache_zalloc(xfs_attri_cache, GFP_NOFS | __GFP_NOFAIL);
     281             : 
     282             :         /*
     283             :          * Grab an extra reference to the name/value buffer for this log item.
     284             :          * The caller retains its own reference!
     285             :          */
     286   182104109 :         attrip->attri_nameval = xfs_attri_log_nameval_get(nv);
     287   182115646 :         ASSERT(attrip->attri_nameval);
     288             : 
     289   182115646 :         xfs_log_item_init(mp, &attrip->attri_item, XFS_LI_ATTRI,
     290             :                           &xfs_attri_item_ops);
     291   182121229 :         attrip->attri_format.alfi_id = (uintptr_t)(void *)attrip;
     292   182121229 :         atomic_set(&attrip->attri_refcount, 2);
     293             : 
     294   182121229 :         return attrip;
     295             : }
     296             : 
     297             : static inline struct xfs_attrd_log_item *ATTRD_ITEM(struct xfs_log_item *lip)
     298             : {
     299             :         return container_of(lip, struct xfs_attrd_log_item, attrd_item);
     300             : }
     301             : 
     302             : STATIC void
     303   182148253 : xfs_attrd_item_free(struct xfs_attrd_log_item *attrdp)
     304             : {
     305   182148253 :         kmem_free(attrdp->attrd_item.li_lv_shadow);
     306   182148467 :         kmem_cache_free(xfs_attrd_cache, attrdp);
     307   182148873 : }
     308             : 
     309             : STATIC void
     310   182143742 : xfs_attrd_item_size(
     311             :         struct xfs_log_item             *lip,
     312             :         int                             *nvecs,
     313             :         int                             *nbytes)
     314             : {
     315   182143742 :         *nvecs += 1;
     316   182143742 :         *nbytes += sizeof(struct xfs_attrd_log_format);
     317   182143742 : }
     318             : 
     319             : /*
     320             :  * This is called to fill in the log iovecs for the given attrd log item. We use
     321             :  * only 1 iovec for the attrd_format, and we point that at the attr_log_format
     322             :  * structure embedded in the attrd item.
     323             :  */
     324             : STATIC void
     325       36744 : xfs_attrd_item_format(
     326             :         struct xfs_log_item     *lip,
     327             :         struct xfs_log_vec      *lv)
     328             : {
     329       36744 :         struct xfs_attrd_log_item       *attrdp = ATTRD_ITEM(lip);
     330       36744 :         struct xfs_log_iovec            *vecp = NULL;
     331             : 
     332       36744 :         attrdp->attrd_format.alfd_type = XFS_LI_ATTRD;
     333       36744 :         attrdp->attrd_format.alfd_size = 1;
     334             : 
     335       36744 :         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_ATTRD_FORMAT,
     336       36744 :                         &attrdp->attrd_format,
     337             :                         sizeof(struct xfs_attrd_log_format));
     338       36743 : }
     339             : 
     340             : /*
     341             :  * The ATTRD is either committed or aborted if the transaction is canceled. If
     342             :  * the transaction is canceled, drop our reference to the ATTRI and free the
     343             :  * ATTRD.
     344             :  */
     345             : STATIC void
     346   182142710 : xfs_attrd_item_release(
     347             :         struct xfs_log_item             *lip)
     348             : {
     349   182142710 :         struct xfs_attrd_log_item       *attrdp = ATTRD_ITEM(lip);
     350             : 
     351   182142710 :         xfs_attri_release(attrdp->attrd_attrip);
     352   182147690 :         xfs_attrd_item_free(attrdp);
     353   182148523 : }
     354             : 
     355             : static struct xfs_log_item *
     356   182145695 : xfs_attrd_item_intent(
     357             :         struct xfs_log_item     *lip)
     358             : {
     359   182145695 :         return &ATTRD_ITEM(lip)->attrd_attrip->attri_item;
     360             : }
     361             : 
     362             : /*
     363             :  * Performs one step of an attribute update intent and marks the attrd item
     364             :  * dirty..  An attr operation may be a set or a remove.  Note that the
     365             :  * transaction is marked dirty regardless of whether the operation succeeds or
     366             :  * fails to support the ATTRI/ATTRD lifecycle rules.
     367             :  */
     368             : STATIC int
     369   353474821 : xfs_xattri_finish_update(
     370             :         struct xfs_attr_intent          *attr,
     371             :         struct xfs_attrd_log_item       *attrdp)
     372             : {
     373   353474821 :         struct xfs_da_args              *args = attr->xattri_da_args;
     374   353474821 :         int                             error;
     375             : 
     376   353474821 :         if (XFS_TEST_ERROR(false, args->dp->i_mount, XFS_ERRTAG_LARP)) {
     377          56 :                 error = -EIO;
     378          56 :                 goto out;
     379             :         }
     380             : 
     381   353498836 :         error = xfs_attr_set_iter(attr);
     382   353511800 :         if (!error && attr->xattri_dela_state != XFS_DAS_DONE)
     383    77111620 :                 error = -EAGAIN;
     384   276400180 : out:
     385             :         /*
     386             :          * Mark the transaction dirty, even on error. This ensures the
     387             :          * transaction is aborted, which:
     388             :          *
     389             :          * 1.) releases the ATTRI and frees the ATTRD
     390             :          * 2.) shuts down the filesystem
     391             :          */
     392   353511856 :         args->trans->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
     393             : 
     394             :         /*
     395             :          * attr intent/done items are null when logged attributes are disabled
     396             :          */
     397   353511856 :         if (attrdp)
     398   182175654 :                 set_bit(XFS_LI_DIRTY, &attrdp->attrd_item.li_flags);
     399             : 
     400   353479868 :         return error;
     401             : }
     402             : 
     403             : static inline unsigned int
     404             : xfs_attr_log_item_op(const struct xfs_attri_log_format *attrp)
     405             : {
     406   182139156 :         return attrp->alfi_op_flags & XFS_ATTRI_OP_FLAGS_TYPE_MASK;
     407             : }
     408             : 
     409             : /* Log an attr to the intent item. */
     410             : STATIC void
     411   182126132 : xfs_attr_log_item(
     412             :         struct xfs_trans                *tp,
     413             :         struct xfs_attri_log_item       *attrip,
     414             :         const struct xfs_attr_intent    *attr)
     415             : {
     416   182126132 :         struct xfs_attri_log_format     *attrp;
     417             : 
     418   182126132 :         tp->t_flags |= XFS_TRANS_DIRTY;
     419   182126132 :         set_bit(XFS_LI_DIRTY, &attrip->attri_item.li_flags);
     420             : 
     421             :         /*
     422             :          * At this point the xfs_attr_intent has been constructed, and we've
     423             :          * created the log intent. Fill in the attri log item and log format
     424             :          * structure with fields from this xfs_attr_intent
     425             :          */
     426   182135161 :         attrp = &attrip->attri_format;
     427   182135161 :         attrp->alfi_ino = attr->xattri_da_args->dp->i_ino;
     428   182135161 :         ASSERT(!(attr->xattri_op_flags & ~XFS_ATTRI_OP_FLAGS_TYPE_MASK));
     429   182135161 :         attrp->alfi_op_flags = attr->xattri_op_flags;
     430   182135161 :         attrp->alfi_value_len = attr->xattri_nameval->value.i_len;
     431             : 
     432   182135161 :         if (xfs_attr_log_item_op(attrp) == XFS_ATTRI_OP_FLAGS_NVREPLACE) {
     433    74972287 :                 attrp->alfi_old_name_len = attr->xattri_nameval->name.i_len;
     434    74972287 :                 attrp->alfi_new_name_len = attr->xattri_nameval->new_name.i_len;
     435    74972287 :                 attrp->alfi_new_value_len = attr->xattri_nameval->new_value.i_len;
     436             :         } else {
     437   107162874 :                 attrp->alfi_name_len = attr->xattri_nameval->name.i_len;
     438             :         }
     439             : 
     440   182135161 :         ASSERT(!(attr->xattri_da_args->attr_filter & ~XFS_ATTRI_FILTER_MASK));
     441   182135161 :         attrp->alfi_attr_filter = attr->xattri_da_args->attr_filter;
     442   182135161 : }
     443             : 
     444             : /* Get an ATTRI. */
     445             : static struct xfs_log_item *
     446   353472592 : xfs_attr_create_intent(
     447             :         struct xfs_trans                *tp,
     448             :         struct list_head                *items,
     449             :         unsigned int                    count,
     450             :         bool                            sort)
     451             : {
     452   353472592 :         struct xfs_mount                *mp = tp->t_mountp;
     453   353472592 :         struct xfs_attri_log_item       *attrip;
     454   353472592 :         struct xfs_attr_intent          *attr;
     455   353472592 :         struct xfs_da_args              *args;
     456             : 
     457   353472592 :         ASSERT(count == 1);
     458             : 
     459             :         /*
     460             :          * Each attr item only performs one attribute operation at a time, so
     461             :          * this is a list of one
     462             :          */
     463   353472592 :         attr = list_first_entry_or_null(items, struct xfs_attr_intent,
     464             :                         xattri_list);
     465   353472592 :         args = attr->xattri_da_args;
     466             : 
     467   353472592 :         if (!(args->op_flags & XFS_DA_OP_LOGGED))
     468             :                 return NULL;
     469             : 
     470             :         /*
     471             :          * Create a buffer to store the attribute name and value.  This buffer
     472             :          * will be shared between the higher level deferred xattr work state
     473             :          * and the lower level xattr log items.
     474             :          */
     475   182123138 :         if (!attr->xattri_nameval) {
     476             :                 /*
     477             :                  * Transfer our reference to the name/value buffer to the
     478             :                  * deferred work state structure.
     479             :                  */
     480   144512099 :                 attr->xattri_nameval = xfs_attri_log_nameval_alloc(
     481   144524198 :                                 args->name, args->namelen,
     482   144524198 :                                 args->new_name, args->new_namelen,
     483   144524198 :                                 args->value, args->valuelen,
     484   144524198 :                                 args->new_value, args->new_valuelen);
     485             :         }
     486             : 
     487   182111039 :         attrip = xfs_attri_init(mp, attr->xattri_nameval);
     488   182114324 :         xfs_trans_add_item(tp, &attrip->attri_item);
     489   182134273 :         xfs_attr_log_item(tp, attrip, attr);
     490             : 
     491   182134273 :         return &attrip->attri_item;
     492             : }
     493             : 
     494             : static inline void
     495   276381587 : xfs_attr_free_item(
     496             :         struct xfs_attr_intent          *attr)
     497             : {
     498   276381587 :         if (attr->xattri_da_state)
     499      588290 :                 xfs_da_state_free(attr->xattri_da_state);
     500   276381588 :         xfs_attri_log_nameval_put(attr->xattri_nameval);
     501   276387977 :         if (attr->xattri_da_args->op_flags & XFS_DA_OP_RECOVERY)
     502          92 :                 kmem_free(attr);
     503             :         else
     504   276387885 :                 kmem_cache_free(xfs_attr_intent_cache, attr);
     505   276414615 : }
     506             : 
     507             : /* Process an attr. */
     508             : STATIC int
     509   353455484 : xfs_attr_finish_item(
     510             :         struct xfs_trans                *tp,
     511             :         struct xfs_log_item             *done,
     512             :         struct list_head                *item,
     513             :         struct xfs_btree_cur            **state)
     514             : {
     515   353455484 :         struct xfs_attr_intent          *attr;
     516   353455484 :         struct xfs_attrd_log_item       *done_item = NULL;
     517   353455484 :         int                             error;
     518             : 
     519   353455484 :         attr = container_of(item, struct xfs_attr_intent, xattri_list);
     520   353455484 :         if (done)
     521   182140800 :                 done_item = ATTRD_ITEM(done);
     522             : 
     523             :         /*
     524             :          * Always reset trans after EAGAIN cycle
     525             :          * since the transaction is new
     526             :          */
     527   353455484 :         attr->xattri_da_args->trans = tp;
     528             : 
     529   353455484 :         error = xfs_xattri_finish_update(attr, done_item);
     530   353492317 :         if (error != -EAGAIN)
     531   276397240 :                 xfs_attr_free_item(attr);
     532             : 
     533   353505000 :         return error;
     534             : }
     535             : 
     536             : /* Abort all pending ATTRs. */
     537             : STATIC void
     538          45 : xfs_attr_abort_intent(
     539             :         struct xfs_log_item             *intent)
     540             : {
     541          45 :         xfs_attri_release(ATTRI_ITEM(intent));
     542          45 : }
     543             : 
     544             : /* Cancel an attr */
     545             : STATIC void
     546         217 : xfs_attr_cancel_item(
     547             :         struct list_head                *item)
     548             : {
     549         217 :         struct xfs_attr_intent          *attr;
     550             : 
     551         217 :         attr = container_of(item, struct xfs_attr_intent, xattri_list);
     552         217 :         xfs_attr_free_item(attr);
     553         217 : }
     554             : 
     555             : STATIC bool
     556        1568 : xfs_attri_item_match(
     557             :         struct xfs_log_item     *lip,
     558             :         uint64_t                intent_id)
     559             : {
     560        1568 :         return ATTRI_ITEM(lip)->attri_format.alfi_id == intent_id;
     561             : }
     562             : 
     563             : /* Is this recovered ATTRI format ok? */
     564             : static inline bool
     565        1637 : xfs_attri_validate(
     566             :         struct xfs_mount                *mp,
     567             :         struct xfs_attri_log_format     *attrp)
     568             : {
     569        1637 :         unsigned int                    op = xfs_attr_log_item_op(attrp);
     570             : 
     571        1637 :         if (attrp->alfi_op_flags & ~XFS_ATTRI_OP_FLAGS_TYPE_MASK)
     572             :                 return false;
     573             : 
     574        1637 :         if (attrp->alfi_attr_filter & ~XFS_ATTRI_FILTER_MASK)
     575             :                 return false;
     576             : 
     577             :         /* alfi_op_flags should be either a set or remove */
     578        1637 :         switch (op) {
     579          48 :         case XFS_ATTRI_OP_FLAGS_REMOVE:
     580          48 :                 if (attrp->alfi_value_len != 0)
     581             :                         return false;
     582          48 :                 if (attrp->alfi_name_len == 0 ||
     583             :                     attrp->alfi_name_len > XATTR_NAME_MAX)
     584             :                         return false;
     585          48 :                 if (attrp->alfi_new_value_len != 0)
     586             :                         return false;
     587             :                 break;
     588         866 :         case XFS_ATTRI_OP_FLAGS_SET:
     589             :         case XFS_ATTRI_OP_FLAGS_REPLACE:
     590             :         case XFS_ATTRI_OP_FLAGS_NVREMOVE:
     591             :         case XFS_ATTRI_OP_FLAGS_NVSET:
     592         866 :                 if (attrp->alfi_name_len == 0 ||
     593             :                     attrp->alfi_name_len > XATTR_NAME_MAX)
     594             :                         return false;
     595         866 :                 if (attrp->alfi_value_len > XATTR_SIZE_MAX)
     596             :                         return false;
     597         866 :                 if (attrp->alfi_new_value_len != 0)
     598             :                         return false;
     599             :                 break;
     600         723 :         case XFS_ATTRI_OP_FLAGS_NVREPLACE:
     601         723 :                 if (attrp->alfi_old_name_len == 0 ||
     602             :                     attrp->alfi_old_name_len > XATTR_NAME_MAX)
     603             :                         return false;
     604         723 :                 if (attrp->alfi_new_name_len == 0 ||
     605             :                     attrp->alfi_new_name_len > XATTR_NAME_MAX)
     606             :                         return false;
     607         723 :                 if (attrp->alfi_value_len > XATTR_SIZE_MAX)
     608             :                         return false;
     609         723 :                 if (attrp->alfi_new_value_len > XATTR_SIZE_MAX)
     610             :                         return false;
     611             :                 break;
     612             :         default:
     613             :                 return false;
     614             :         }
     615             : 
     616        1637 :         return xfs_verify_ino(mp, attrp->alfi_ino);
     617             : }
     618             : 
     619             : /*
     620             :  * Process an attr intent item that was recovered from the log.  We need to
     621             :  * delete the attr that it describes.
     622             :  */
     623             : STATIC int
     624          92 : xfs_attri_item_recover(
     625             :         struct xfs_log_item             *lip,
     626             :         struct list_head                *capture_list)
     627             : {
     628          92 :         struct xfs_attri_log_item       *attrip = ATTRI_ITEM(lip);
     629          92 :         struct xfs_attr_intent          *attr;
     630          92 :         struct xfs_mount                *mp = lip->li_log->l_mp;
     631          92 :         struct xfs_inode                *ip;
     632          92 :         struct xfs_da_args              *args;
     633          92 :         struct xfs_trans                *tp;
     634          92 :         struct xfs_trans_res            tres;
     635          92 :         struct xfs_attri_log_format     *attrp;
     636          92 :         struct xfs_attri_log_nameval    *nv = attrip->attri_nameval;
     637          92 :         int                             error;
     638          92 :         int                             total;
     639          92 :         int                             local;
     640          92 :         struct xfs_attrd_log_item       *done_item = NULL;
     641             : 
     642             :         /*
     643             :          * First check the validity of the attr described by the ATTRI.  If any
     644             :          * are bad, then assume that all are bad and just toss the ATTRI.
     645             :          */
     646          92 :         attrp = &attrip->attri_format;
     647         184 :         if (!xfs_attri_validate(mp, attrp) ||
     648          92 :             !xfs_attr_namecheck(mp, nv->name.i_addr, nv->name.i_len,
     649             :                                 attrp->alfi_attr_filter))
     650           0 :                 return -EFSCORRUPTED;
     651             : 
     652          92 :         error = xlog_recover_iget(mp,  attrp->alfi_ino, &ip);
     653          92 :         if (error)
     654             :                 return error;
     655             : 
     656          92 :         attr = kmem_zalloc(sizeof(struct xfs_attr_intent) +
     657             :                            sizeof(struct xfs_da_args), KM_NOFS);
     658          92 :         args = (struct xfs_da_args *)(attr + 1);
     659             : 
     660          92 :         attr->xattri_da_args = args;
     661          92 :         attr->xattri_op_flags = xfs_attr_log_item_op(attrp);
     662             : 
     663             :         /*
     664             :          * We're reconstructing the deferred work state structure from the
     665             :          * recovered log item.  Grab a reference to the name/value buffer and
     666             :          * attach it to the new work state.
     667             :          */
     668          92 :         attr->xattri_nameval = xfs_attri_log_nameval_get(nv);
     669          92 :         ASSERT(attr->xattri_nameval);
     670             : 
     671          92 :         args->dp = ip;
     672          92 :         args->geo = mp->m_attr_geo;
     673          92 :         args->whichfork = XFS_ATTR_FORK;
     674          92 :         args->name = nv->name.i_addr;
     675          92 :         args->namelen = nv->name.i_len;
     676          92 :         args->new_name = nv->new_name.i_addr;
     677          92 :         args->new_namelen = nv->new_name.i_len;
     678          92 :         args->hashval = xfs_da_hashname(args->name, args->namelen);
     679          92 :         args->value = nv->value.i_addr;
     680          92 :         args->valuelen = nv->value.i_len;
     681          92 :         args->new_value = nv->new_value.i_addr;
     682          92 :         args->new_valuelen = nv->new_value.i_len;
     683          92 :         args->attr_filter = attrp->alfi_attr_filter & XFS_ATTRI_FILTER_MASK;
     684          92 :         args->op_flags = XFS_DA_OP_RECOVERY | XFS_DA_OP_OKNOENT |
     685             :                          XFS_DA_OP_LOGGED;
     686          92 :         args->owner = ip->i_ino;
     687             : 
     688         184 :         ASSERT(xfs_sb_version_haslogxattrs(&mp->m_sb));
     689             : 
     690          92 :         switch (xfs_attr_intent_op(attr)) {
     691          23 :         case XFS_ATTRI_OP_FLAGS_NVREPLACE:
     692             :         case XFS_ATTRI_OP_FLAGS_NVSET:
     693          23 :                 args->op_flags |= XFS_DA_OP_NVLOOKUP;
     694          57 :                 fallthrough;
     695          57 :         case XFS_ATTRI_OP_FLAGS_SET:
     696             :         case XFS_ATTRI_OP_FLAGS_REPLACE:
     697          57 :                 args->total = xfs_attr_calc_size(args, &local);
     698          57 :                 if (xfs_inode_hasattr(args->dp))
     699          42 :                         attr->xattri_dela_state = xfs_attr_init_replace_state(args);
     700             :                 else
     701          15 :                         attr->xattri_dela_state = xfs_attr_init_add_state(args);
     702             :                 break;
     703          11 :         case XFS_ATTRI_OP_FLAGS_NVREMOVE:
     704          11 :                 args->op_flags |= XFS_DA_OP_NVLOOKUP;
     705          35 :                 fallthrough;
     706          35 :         case XFS_ATTRI_OP_FLAGS_REMOVE:
     707          35 :                 if (!xfs_inode_hasattr(args->dp))
     708           0 :                         goto out;
     709          35 :                 attr->xattri_dela_state = xfs_attr_init_remove_state(args);
     710          35 :                 break;
     711           0 :         default:
     712           0 :                 ASSERT(0);
     713           0 :                 error = -EFSCORRUPTED;
     714           0 :                 goto out;
     715             :         }
     716             : 
     717          92 :         xfs_init_attr_trans(args, &tres, &total);
     718          92 :         error = xfs_trans_alloc(mp, &tres, total, 0, XFS_TRANS_RESERVE, &tp);
     719          92 :         if (error)
     720           0 :                 goto out;
     721             : 
     722          92 :         args->trans = tp;
     723          92 :         done_item = xfs_trans_get_attrd(tp, attrip);
     724             : 
     725          92 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
     726          92 :         xfs_trans_ijoin(tp, ip, 0);
     727             : 
     728          92 :         error = xfs_xattri_finish_update(attr, done_item);
     729          92 :         if (error == -EAGAIN) {
     730             :                 /*
     731             :                  * There's more work to do, so add the intent item to this
     732             :                  * transaction so that we can continue it later.
     733             :                  */
     734          60 :                 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_ATTR, &attr->xattri_list);
     735          60 :                 error = xfs_defer_ops_capture_and_commit(tp, capture_list);
     736          60 :                 if (error)
     737           0 :                         goto out_unlock;
     738             : 
     739          60 :                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
     740          60 :                 xfs_irele(ip);
     741          60 :                 return 0;
     742             :         }
     743          32 :         if (error) {
     744           0 :                 xfs_trans_cancel(tp);
     745           0 :                 goto out_unlock;
     746             :         }
     747             : 
     748          32 :         error = xfs_defer_ops_capture_and_commit(tp, capture_list);
     749          32 : out_unlock:
     750          32 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     751          32 :         xfs_irele(ip);
     752          32 : out:
     753          32 :         xfs_attr_free_item(attr);
     754          32 :         return error;
     755             : }
     756             : 
     757             : /* Re-log an intent item to push the log tail forward. */
     758             : static struct xfs_log_item *
     759         721 : xfs_attri_item_relog(
     760             :         struct xfs_log_item             *intent,
     761             :         struct xfs_trans                *tp)
     762             : {
     763         721 :         struct xfs_attrd_log_item       *attrdp;
     764         721 :         struct xfs_attri_log_item       *old_attrip;
     765         721 :         struct xfs_attri_log_item       *new_attrip;
     766         721 :         struct xfs_attri_log_format     *new_attrp;
     767         721 :         struct xfs_attri_log_format     *old_attrp;
     768             : 
     769         721 :         old_attrip = ATTRI_ITEM(intent);
     770         721 :         old_attrp = &old_attrip->attri_format;
     771             : 
     772         721 :         tp->t_flags |= XFS_TRANS_DIRTY;
     773         721 :         attrdp = xfs_trans_get_attrd(tp, old_attrip);
     774         721 :         set_bit(XFS_LI_DIRTY, &attrdp->attrd_item.li_flags);
     775             : 
     776             :         /*
     777             :          * Create a new log item that shares the same name/value buffer as the
     778             :          * old log item.
     779             :          */
     780         721 :         new_attrip = xfs_attri_init(tp->t_mountp, old_attrip->attri_nameval);
     781         721 :         new_attrp = &new_attrip->attri_format;
     782             : 
     783         721 :         new_attrp->alfi_ino = old_attrp->alfi_ino;
     784         721 :         new_attrp->alfi_op_flags = old_attrp->alfi_op_flags;
     785         721 :         new_attrp->alfi_value_len = old_attrp->alfi_value_len;
     786             : 
     787         721 :         if (xfs_attr_log_item_op(old_attrp) == XFS_ATTRI_OP_FLAGS_NVREPLACE) {
     788          32 :                 new_attrp->alfi_new_name_len = old_attrp->alfi_new_name_len;
     789          32 :                 new_attrp->alfi_old_name_len = old_attrp->alfi_old_name_len;
     790          32 :                 new_attrp->alfi_new_value_len = old_attrp->alfi_new_value_len;
     791             :         } else {
     792         689 :                 new_attrp->alfi_name_len = old_attrp->alfi_name_len;
     793             :         }
     794             : 
     795         721 :         new_attrp->alfi_attr_filter = old_attrp->alfi_attr_filter;
     796             : 
     797         721 :         xfs_trans_add_item(tp, &new_attrip->attri_item);
     798         721 :         set_bit(XFS_LI_DIRTY, &new_attrip->attri_item.li_flags);
     799             : 
     800         721 :         return &new_attrip->attri_item;
     801             : }
     802             : 
     803             : STATIC int
     804        1545 : xlog_recover_attri_commit_pass2(
     805             :         struct xlog                     *log,
     806             :         struct list_head                *buffer_list,
     807             :         struct xlog_recover_item        *item,
     808             :         xfs_lsn_t                       lsn)
     809             : {
     810        1545 :         struct xfs_mount                *mp = log->l_mp;
     811        1545 :         struct xfs_attri_log_item       *attrip;
     812        1545 :         struct xfs_attri_log_format     *attri_formatp;
     813        1545 :         struct xfs_attri_log_nameval    *nv;
     814        1545 :         const void                      *attr_name;
     815        1545 :         const void                      *attr_value = NULL;
     816        1545 :         const void                      *attr_new_name = NULL;
     817        1545 :         const void                      *attr_new_value = NULL;
     818        1545 :         size_t                          len;
     819        1545 :         unsigned int                    name_len = 0;
     820        1545 :         unsigned int                    value_len = 0;
     821        1545 :         unsigned int                    new_name_len = 0;
     822        1545 :         unsigned int                    new_value_len = 0;
     823        1545 :         unsigned int                    op, i = 0;
     824             : 
     825             :         /* Validate xfs_attri_log_format before the large memory allocation */
     826        1545 :         len = sizeof(struct xfs_attri_log_format);
     827        1545 :         if (item->ri_buf[i].i_len != len) {
     828           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     829             :                                 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
     830           0 :                 return -EFSCORRUPTED;
     831             :         }
     832             : 
     833        1545 :         attri_formatp = item->ri_buf[i].i_addr;
     834        1545 :         if (!xfs_attri_validate(mp, attri_formatp)) {
     835           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     836             :                                 attri_formatp, len);
     837           0 :                 return -EFSCORRUPTED;
     838             :         }
     839             : 
     840             :         /* Check the number of log iovecs makes sense for the op code. */
     841        1545 :         op = xfs_attr_log_item_op(attri_formatp);
     842        1545 :         switch (op) {
     843         782 :         case XFS_ATTRI_OP_FLAGS_NVSET:
     844             :         case XFS_ATTRI_OP_FLAGS_NVREMOVE:
     845             :                 /* Log item, attr name, optional attr value */
     846         782 :                 if (item->ri_total != 3 && item->ri_total != 2) {
     847           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     848             :                                              attri_formatp, len);
     849           0 :                         return -EFSCORRUPTED;
     850             :                 }
     851         782 :                 name_len = attri_formatp->alfi_name_len;
     852         782 :                 value_len = attri_formatp->alfi_value_len;
     853         782 :                 break;
     854          34 :         case XFS_ATTRI_OP_FLAGS_SET:
     855             :         case XFS_ATTRI_OP_FLAGS_REPLACE:
     856             :                 /* Log item, attr name, attr value */
     857          34 :                 if (item->ri_total != 3) {
     858           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     859             :                                              attri_formatp, len);
     860           0 :                         return -EFSCORRUPTED;
     861             :                 }
     862          34 :                 name_len = attri_formatp->alfi_name_len;
     863          34 :                 value_len = attri_formatp->alfi_value_len;
     864          34 :                 break;
     865          24 :         case XFS_ATTRI_OP_FLAGS_REMOVE:
     866             :                 /* Log item, attr name */
     867          24 :                 if (item->ri_total != 2) {
     868           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     869             :                                              attri_formatp, len);
     870           0 :                         return -EFSCORRUPTED;
     871             :                 }
     872          24 :                 name_len = attri_formatp->alfi_name_len;
     873          24 :                 break;
     874         705 :         case XFS_ATTRI_OP_FLAGS_NVREPLACE:
     875             :                 /*
     876             :                  * Log item, attr name, new attr name, optional attr value,
     877             :                  * optional new attr value
     878             :                  */
     879         705 :                 if (item->ri_total < 3 || item->ri_total > 5) {
     880           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     881             :                                              attri_formatp, len);
     882           0 :                         return -EFSCORRUPTED;
     883             :                 }
     884         705 :                 name_len = attri_formatp->alfi_old_name_len;
     885         705 :                 new_name_len = attri_formatp->alfi_new_name_len;
     886         705 :                 value_len = attri_formatp->alfi_value_len;
     887         705 :                 new_value_len = attri_formatp->alfi_new_value_len;
     888         705 :                 break;
     889           0 :         default:
     890           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     891             :                                      attri_formatp, len);
     892           0 :                 return -EFSCORRUPTED;
     893             :         }
     894        1545 :         i++;
     895             : 
     896             :         /* Validate the attr name */
     897        1545 :         if (item->ri_buf[i].i_len != xlog_calc_iovec_len(name_len)) {
     898           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     899             :                                 attri_formatp, len);
     900           0 :                 return -EFSCORRUPTED;
     901             :         }
     902             : 
     903        1545 :         attr_name = item->ri_buf[i].i_addr;
     904        1545 :         if (!xfs_attr_namecheck(mp, attr_name, name_len,
     905             :                                 attri_formatp->alfi_attr_filter)) {
     906           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     907             :                                 attri_formatp, len);
     908           0 :                 return -EFSCORRUPTED;
     909             :         }
     910        1545 :         i++;
     911             : 
     912             :         /* Validate the new attr name */
     913        1545 :         if (new_name_len > 0) {
     914         705 :                 if (item->ri_buf[i].i_len != xlog_calc_iovec_len(new_name_len)) {
     915           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     916             :                                         item->ri_buf[i].i_addr,
     917             :                                         item->ri_buf[i].i_len);
     918           0 :                         return -EFSCORRUPTED;
     919             :                 }
     920             : 
     921         705 :                 attr_new_name = item->ri_buf[i].i_addr;
     922         705 :                 if (!xfs_attr_namecheck(mp, attr_new_name, new_name_len,
     923             :                                         attri_formatp->alfi_attr_filter)) {
     924           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     925             :                                         item->ri_buf[i].i_addr,
     926             :                                         item->ri_buf[i].i_len);
     927           0 :                         return -EFSCORRUPTED;
     928             :                 }
     929             :                 i++;
     930             :         }
     931             : 
     932             :         /* Validate the attr value, if present */
     933        1545 :         if (value_len != 0) {
     934        1521 :                 if (item->ri_buf[i].i_len != xlog_calc_iovec_len(value_len)) {
     935           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     936             :                                         attri_formatp, len);
     937           0 :                         return -EFSCORRUPTED;
     938             :                 }
     939             : 
     940        1521 :                 attr_value = item->ri_buf[i].i_addr;
     941        3008 :                 if ((attri_formatp->alfi_attr_filter & XFS_ATTR_PARENT) &&
     942        1487 :                     !xfs_parent_valuecheck(mp, attr_value, value_len)) {
     943           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     944             :                                         item->ri_buf[i].i_addr,
     945             :                                         item->ri_buf[i].i_len);
     946           0 :                         return -EFSCORRUPTED;
     947             :                 }
     948        1521 :                 i++;
     949             :         }
     950             : 
     951             :         /* Validate the new attr value, if present */
     952        1545 :         if (new_value_len != 0) {
     953         705 :                 if (item->ri_buf[i].i_len != xlog_calc_iovec_len(new_value_len)) {
     954           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     955             :                                         attri_formatp, len);
     956           0 :                         return -EFSCORRUPTED;
     957             :                 }
     958             : 
     959         705 :                 attr_new_value = item->ri_buf[i].i_addr;
     960        1410 :                 if ((attri_formatp->alfi_attr_filter & XFS_ATTR_PARENT) &&
     961         705 :                     !xfs_parent_valuecheck(mp, attr_new_value, new_value_len)) {
     962           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     963             :                                         item->ri_buf[i].i_addr,
     964             :                                         item->ri_buf[i].i_len);
     965           0 :                         return -EFSCORRUPTED;
     966             :                 }
     967         705 :                 i++;
     968             :         }
     969             : 
     970             :         /*
     971             :          * Make sure we got the correct number of buffers for the operation
     972             :          * that we just loaded.
     973             :          */
     974        1545 :         if (i != item->ri_total) {
     975           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     976             :                                 attri_formatp, len);
     977           0 :                 return -EFSCORRUPTED;
     978             :         }
     979             : 
     980        1545 :         switch (op) {
     981          24 :         case XFS_ATTRI_OP_FLAGS_REMOVE:
     982             :                 /* Regular remove operations operate only on names. */
     983          24 :                 if (attr_value != NULL || value_len != 0) {
     984           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     985             :                                              attri_formatp, len);
     986           0 :                         return -EFSCORRUPTED;
     987             :                 }
     988         840 :                 fallthrough;
     989             :         case XFS_ATTRI_OP_FLAGS_NVSET:
     990             :         case XFS_ATTRI_OP_FLAGS_NVREMOVE:
     991             :         case XFS_ATTRI_OP_FLAGS_SET:
     992             :         case XFS_ATTRI_OP_FLAGS_REPLACE:
     993             :                 /*
     994             :                  * Regular xattr set/remove/replace operations require a name
     995             :                  * and do not take a newname.  Values are optional for set and
     996             :                  * replace.
     997             :                  *
     998             :                  * Name-value set/remove operations must have a name, do not
     999             :                  * take a newname, and can take a value.
    1000             :                  */
    1001         840 :                 if (attr_name == NULL || name_len == 0) {
    1002           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
    1003             :                                              attri_formatp, len);
    1004           0 :                         return -EFSCORRUPTED;
    1005             :                 }
    1006             :                 break;
    1007         705 :         case XFS_ATTRI_OP_FLAGS_NVREPLACE:
    1008             :                 /*
    1009             :                  * Name-value replace operations require the caller to
    1010             :                  * specify the old and new names and values explicitly.
    1011             :                  * Values are optional.
    1012             :                  */
    1013         705 :                 if (attr_name == NULL || name_len == 0) {
    1014           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
    1015             :                                              attri_formatp, len);
    1016           0 :                         return -EFSCORRUPTED;
    1017             :                 }
    1018         705 :                 if (attr_new_name == NULL || new_name_len == 0) {
    1019           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
    1020             :                                              attri_formatp, len);
    1021           0 :                         return -EFSCORRUPTED;
    1022             :                 }
    1023             :                 break;
    1024             :         }
    1025             : 
    1026             :         /*
    1027             :          * Memory alloc failure will cause replay to abort.  We attach the
    1028             :          * name/value buffer to the recovered incore log item and drop our
    1029             :          * reference.
    1030             :          */
    1031        1545 :         nv = xfs_attri_log_nameval_alloc(attr_name, name_len,
    1032             :                         attr_new_name, new_name_len,
    1033             :                         attr_value, value_len,
    1034             :                         attr_new_value, new_value_len);
    1035             : 
    1036        1545 :         attrip = xfs_attri_init(mp, nv);
    1037        3090 :         memcpy(&attrip->attri_format, attri_formatp, len);
    1038             : 
    1039             :         /*
    1040             :          * The ATTRI has two references. One for the ATTRD and one for ATTRI to
    1041             :          * ensure it makes it into the AIL. Insert the ATTRI into the AIL
    1042             :          * directly and drop the ATTRI reference. Note that
    1043             :          * xfs_trans_ail_update() drops the AIL lock.
    1044             :          */
    1045        1545 :         xfs_trans_ail_insert(log->l_ailp, &attrip->attri_item, lsn);
    1046        1545 :         xfs_attri_release(attrip);
    1047        1545 :         xfs_attri_log_nameval_put(nv);
    1048        1545 :         return 0;
    1049             : }
    1050             : 
    1051             : /*
    1052             :  * This routine is called to allocate an "attr free done" log item.
    1053             :  */
    1054             : static struct xfs_attrd_log_item *
    1055   182144386 : xfs_trans_get_attrd(struct xfs_trans            *tp,
    1056             :                   struct xfs_attri_log_item     *attrip)
    1057             : {
    1058   182144386 :         struct xfs_attrd_log_item               *attrdp;
    1059             : 
    1060   182144386 :         ASSERT(tp != NULL);
    1061             : 
    1062   182144386 :         attrdp = kmem_cache_zalloc(xfs_attrd_cache, GFP_NOFS | __GFP_NOFAIL);
    1063             : 
    1064   182147391 :         xfs_log_item_init(tp->t_mountp, &attrdp->attrd_item, XFS_LI_ATTRD,
    1065             :                           &xfs_attrd_item_ops);
    1066   182145607 :         attrdp->attrd_attrip = attrip;
    1067   182145607 :         attrdp->attrd_format.alfd_alf_id = attrip->attri_format.alfi_id;
    1068             : 
    1069   182145607 :         xfs_trans_add_item(tp, &attrdp->attrd_item);
    1070   182146781 :         return attrdp;
    1071             : }
    1072             : 
    1073             : /* Get an ATTRD so we can process all the attrs. */
    1074             : static struct xfs_log_item *
    1075   353463239 : xfs_attr_create_done(
    1076             :         struct xfs_trans                *tp,
    1077             :         struct xfs_log_item             *intent,
    1078             :         unsigned int                    count)
    1079             : {
    1080   353463239 :         if (!intent)
    1081             :                 return NULL;
    1082             : 
    1083   182140723 :         return &xfs_trans_get_attrd(tp, ATTRI_ITEM(intent))->attrd_item;
    1084             : }
    1085             : 
    1086             : const struct xfs_defer_op_type xfs_attr_defer_type = {
    1087             :         .max_items      = 1,
    1088             :         .create_intent  = xfs_attr_create_intent,
    1089             :         .abort_intent   = xfs_attr_abort_intent,
    1090             :         .create_done    = xfs_attr_create_done,
    1091             :         .finish_item    = xfs_attr_finish_item,
    1092             :         .cancel_item    = xfs_attr_cancel_item,
    1093             : };
    1094             : 
    1095             : /*
    1096             :  * This routine is called when an ATTRD format structure is found in a committed
    1097             :  * transaction in the log. Its purpose is to cancel the corresponding ATTRI if
    1098             :  * it was still in the log. To do this it searches the AIL for the ATTRI with
    1099             :  * an id equal to that in the ATTRD format structure. If we find it we drop
    1100             :  * the ATTRD reference, which removes the ATTRI from the AIL and frees it.
    1101             :  */
    1102             : STATIC int
    1103        1461 : xlog_recover_attrd_commit_pass2(
    1104             :         struct xlog                     *log,
    1105             :         struct list_head                *buffer_list,
    1106             :         struct xlog_recover_item        *item,
    1107             :         xfs_lsn_t                       lsn)
    1108             : {
    1109        1461 :         struct xfs_attrd_log_format     *attrd_formatp;
    1110             : 
    1111        1461 :         attrd_formatp = item->ri_buf[0].i_addr;
    1112        1461 :         if (item->ri_buf[0].i_len != sizeof(struct xfs_attrd_log_format)) {
    1113           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
    1114             :                                 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
    1115           0 :                 return -EFSCORRUPTED;
    1116             :         }
    1117             : 
    1118        1461 :         xlog_recover_release_intent(log, XFS_LI_ATTRI,
    1119             :                                     attrd_formatp->alfd_alf_id);
    1120        1461 :         return 0;
    1121             : }
    1122             : 
    1123             : static const struct xfs_item_ops xfs_attri_item_ops = {
    1124             :         .flags          = XFS_ITEM_INTENT,
    1125             :         .iop_size       = xfs_attri_item_size,
    1126             :         .iop_format     = xfs_attri_item_format,
    1127             :         .iop_unpin      = xfs_attri_item_unpin,
    1128             :         .iop_release    = xfs_attri_item_release,
    1129             :         .iop_recover    = xfs_attri_item_recover,
    1130             :         .iop_match      = xfs_attri_item_match,
    1131             :         .iop_relog      = xfs_attri_item_relog,
    1132             : };
    1133             : 
    1134             : const struct xlog_recover_item_ops xlog_attri_item_ops = {
    1135             :         .item_type      = XFS_LI_ATTRI,
    1136             :         .commit_pass2   = xlog_recover_attri_commit_pass2,
    1137             : };
    1138             : 
    1139             : static const struct xfs_item_ops xfs_attrd_item_ops = {
    1140             :         .flags          = XFS_ITEM_RELEASE_WHEN_COMMITTED |
    1141             :                           XFS_ITEM_INTENT_DONE,
    1142             :         .iop_size       = xfs_attrd_item_size,
    1143             :         .iop_format     = xfs_attrd_item_format,
    1144             :         .iop_release    = xfs_attrd_item_release,
    1145             :         .iop_intent     = xfs_attrd_item_intent,
    1146             : };
    1147             : 
    1148             : const struct xlog_recover_item_ops xlog_attrd_item_ops = {
    1149             :         .item_type      = XFS_LI_ATTRD,
    1150             :         .commit_pass2   = xlog_recover_attrd_commit_pass2,
    1151             : };

Generated by: LCOV version 1.14