]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs: parent pointer attribute creation
authorAllison Henderson <allison.henderson@oracle.com>
Mon, 29 Jul 2024 23:22:47 +0000 (16:22 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Tue, 30 Jul 2024 00:01:02 +0000 (17:01 -0700)
Source kernel commit: b7c62d90c12c6cc86f10b8a62cefe0029374b6ff

Add parent pointer attribute during xfs_create, and subroutines to
initialize attributes.  Note that the xfs_attr_intent object contains a
pointer to the caller's xfs_da_args object, so the latter must persist
until transaction commit.

Signed-off-by: Dave Chinner <dchinner@redhat.com>
Signed-off-by: Allison Henderson <allison.henderson@oracle.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
[djwong: shorten names, adjust to new format, set init_xattrs for parent
pointers]
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
include/libxfs.h
include/xfs_inode.h
libxfs/Makefile
libxfs/init.c
libxfs/libxfs_api_defs.h
libxfs/xfs_parent.c
libxfs/xfs_parent.h
libxfs/xfs_trans_space.c [new file with mode: 0644]
libxfs/xfs_trans_space.h
repair/phase6.c

index fb8efb696220a8431e24281fb02e23c1b4f2fcfe..e760a46d89503b92230f6106a9512670bf531902 100644 (file)
@@ -90,6 +90,7 @@ struct iomap;
 #include "libxfs/xfile.h"
 #include "libxfs/buf_mem.h"
 #include "xfs_btree_mem.h"
+#include "xfs_parent.h"
 
 #ifndef ARRAY_SIZE
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
index 825708383641ee0f36bd92a1980914cfd1440b7b..c6e4f84bdc16a92ac2e451aceb2b0143d2bcb56d 100644 (file)
@@ -268,6 +268,12 @@ static inline struct inode *VFS_I(struct xfs_inode *ip)
        return &ip->i_vnode;
 }
 
+/* convert from const xfs inode to const vfs inode */
+static inline const struct inode *VFS_IC(const struct xfs_inode *ip)
+{
+       return &ip->i_vnode;
+}
+
 /* We only have i_size in the xfs inode in userspace */
 static inline loff_t i_size_read(struct inode *inode)
 {
index 2a5cead9aa19432ed647583335f2cbf75312bb75..9fb53d9cc32cd4961a4895be4c5e5d679a6d02fb 100644 (file)
@@ -113,6 +113,7 @@ CFILES = buf_mem.c \
        xfs_symlink_remote.c \
        xfs_trans_inode.c \
        xfs_trans_resv.c \
+       xfs_trans_space.c \
        xfs_types.c
 
 #
index de91bbf3ca266ac8b3479e8b046bee5f0c478946..95de1e6d1d4fb911622aa918cf5f13e86833df5e 100644 (file)
@@ -214,6 +214,8 @@ init_caches(void)
                        "xfs_extfree_item");
        xfs_trans_cache = kmem_cache_init(
                        sizeof(struct xfs_trans), "xfs_trans");
+       xfs_parent_args_cache = kmem_cache_init(
+                       sizeof(struct xfs_parent_args), "xfs_parent_args");
 }
 
 static int
@@ -231,6 +233,7 @@ destroy_caches(void)
        xfs_btree_destroy_cur_caches();
        leaked += kmem_cache_destroy(xfs_extfree_item_cache);
        leaked += kmem_cache_destroy(xfs_trans_cache);
+       leaked += kmem_cache_destroy(xfs_parent_args_cache);
 
        return leaked;
 }
index 16f6513f671e4ef6f86d948a1d4448eddefbbf6c..9b44b7709fdbb0d37b4319e22909d82915b43438 100644 (file)
@@ -98,6 +98,7 @@
 #define xfs_calc_dquots_per_chunk      libxfs_calc_dquots_per_chunk
 #define xfs_cntbt_init_cursor          libxfs_cntbt_init_cursor
 #define xfs_compute_rextslog           libxfs_compute_rextslog
+#define xfs_create_space_res           libxfs_create_space_res
 #define xfs_da3_node_hdr_from_disk     libxfs_da3_node_hdr_from_disk
 #define xfs_da_get_buf                 libxfs_da_get_buf
 #define xfs_da_hashname                        libxfs_da_hashname
 #define xfs_log_get_max_trans_res      libxfs_log_get_max_trans_res
 #define xfs_log_sb                     libxfs_log_sb
 #define xfs_mode_to_ftype              libxfs_mode_to_ftype
+#define xfs_mkdir_space_res            libxfs_mkdir_space_res
 #define xfs_perag_get                  libxfs_perag_get
 #define xfs_perag_hold                 libxfs_perag_hold
 #define xfs_perag_put                  libxfs_perag_put
index 7447acc2c6873696e89fb0388be648e70cadaa29..b0516c3f68dcebcbbffceff416f3bd8e68f52292 100644 (file)
 #include "xfs_defer.h"
 #include "xfs_parent.h"
 #include "xfs_trans_space.h"
+#include "defer_item.h"
+#include "xfs_health.h"
+
+struct kmem_cache              *xfs_parent_args_cache;
 
 /*
  * Parent pointer attribute handling.
@@ -134,3 +138,67 @@ xfs_parent_hashattr(
 
        return xfs_parent_hashval(mp, name, namelen, be64_to_cpu(rec->p_ino));
 }
+
+/*
+ * Initialize the parent pointer arguments structure.  Caller must have zeroed
+ * the contents of @args.  @tp is only required for updates.
+ */
+static void
+xfs_parent_da_args_init(
+       struct xfs_da_args      *args,
+       struct xfs_trans        *tp,
+       struct xfs_parent_rec   *rec,
+       struct xfs_inode        *child,
+       xfs_ino_t               owner,
+       const struct xfs_name   *parent_name)
+{
+       args->geo = child->i_mount->m_attr_geo;
+       args->whichfork = XFS_ATTR_FORK;
+       args->attr_filter = XFS_ATTR_PARENT;
+       args->op_flags = XFS_DA_OP_LOGGED | XFS_DA_OP_OKNOENT;
+       args->trans = tp;
+       args->dp = child;
+       args->owner = owner;
+       args->name = parent_name->name;
+       args->namelen = parent_name->len;
+       args->value = rec;
+       args->valuelen = sizeof(struct xfs_parent_rec);
+       xfs_attr_sethash(args);
+}
+
+/* Make sure the incore state is ready for a parent pointer query/update. */
+static inline int
+xfs_parent_iread_extents(
+       struct xfs_trans        *tp,
+       struct xfs_inode        *child)
+{
+       /* Parent pointers require that the attr fork must exist. */
+       if (XFS_IS_CORRUPT(child->i_mount, !xfs_inode_has_attr_fork(child))) {
+               xfs_inode_mark_sick(child, XFS_SICK_INO_PARENT);
+               return -EFSCORRUPTED;
+       }
+
+       return xfs_iread_extents(tp, child, XFS_ATTR_FORK);
+}
+
+/* Add a parent pointer to reflect a dirent addition. */
+int
+xfs_parent_addname(
+       struct xfs_trans        *tp,
+       struct xfs_parent_args  *ppargs,
+       struct xfs_inode        *dp,
+       const struct xfs_name   *parent_name,
+       struct xfs_inode        *child)
+{
+       int                     error;
+
+       error = xfs_parent_iread_extents(tp, child);
+       if (error)
+               return error;
+
+       xfs_inode_to_parent_rec(&ppargs->rec, dp);
+       xfs_parent_da_args_init(&ppargs->args, tp, &ppargs->rec, child,
+                       child->i_ino, parent_name);
+       xfs_attr_defer_add(&ppargs->args, XFS_ATTR_DEFER_SET);
+       return 0;
+}
index 6a4028871b72a118dad5b9b87501219d8653e5af..6de24e3ef318ca5614732ea913d3b7a2d6b1b73c 100644 (file)
@@ -17,4 +17,69 @@ xfs_dahash_t xfs_parent_hashval(struct xfs_mount *mp, const uint8_t *name,
 xfs_dahash_t xfs_parent_hashattr(struct xfs_mount *mp, const uint8_t *name,
                int namelen, const void *value, int valuelen);
 
+/* Initializes a xfs_parent_rec to be stored as an attribute name. */
+static inline void
+xfs_parent_rec_init(
+       struct xfs_parent_rec   *rec,
+       xfs_ino_t               ino,
+       uint32_t                gen)
+{
+       rec->p_ino = cpu_to_be64(ino);
+       rec->p_gen = cpu_to_be32(gen);
+}
+
+/* Initializes a xfs_parent_rec to be stored as an attribute name. */
+static inline void
+xfs_inode_to_parent_rec(
+       struct xfs_parent_rec   *rec,
+       const struct xfs_inode  *dp)
+{
+       xfs_parent_rec_init(rec, dp->i_ino, VFS_IC(dp)->i_generation);
+}
+
+extern struct kmem_cache       *xfs_parent_args_cache;
+
+/*
+ * Parent pointer information needed to pass around the deferred xattr update
+ * machinery.
+ */
+struct xfs_parent_args {
+       struct xfs_parent_rec   rec;
+       struct xfs_da_args      args;
+};
+
+/*
+ * Start a parent pointer update by allocating the context object we need to
+ * perform a parent pointer update.
+ */
+static inline int
+xfs_parent_start(
+       struct xfs_mount        *mp,
+       struct xfs_parent_args  **ppargsp)
+{
+       if (!xfs_has_parent(mp)) {
+               *ppargsp = NULL;
+               return 0;
+       }
+
+       *ppargsp = kmem_cache_zalloc(xfs_parent_args_cache, GFP_KERNEL);
+       if (!*ppargsp)
+               return -ENOMEM;
+       return 0;
+}
+
+/* Finish a parent pointer update by freeing the context object. */
+static inline void
+xfs_parent_finish(
+       struct xfs_mount        *mp,
+       struct xfs_parent_args  *ppargs)
+{
+       if (ppargs)
+               kmem_cache_free(xfs_parent_args_cache, ppargs);
+}
+
+int xfs_parent_addname(struct xfs_trans *tp, struct xfs_parent_args *ppargs,
+               struct xfs_inode *dp, const struct xfs_name *parent_name,
+               struct xfs_inode *child);
+
 #endif /* __XFS_PARENT_H__ */
diff --git a/libxfs/xfs_trans_space.c b/libxfs/xfs_trans_space.c
new file mode 100644 (file)
index 0000000..3408e70
--- /dev/null
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2000,2005 Silicon Graphics, Inc.
+ * All Rights Reserved.
+ */
+#include "libxfs_priv.h"
+#include "xfs_fs.h"
+#include "xfs_shared.h"
+#include "xfs_format.h"
+#include "xfs_da_format.h"
+#include "xfs_log_format.h"
+#include "xfs_trans_resv.h"
+#include "xfs_mount.h"
+#include "xfs_da_btree.h"
+#include "xfs_bmap_btree.h"
+#include "xfs_trans_space.h"
+
+/* Calculate the disk space required to add a parent pointer. */
+unsigned int
+xfs_parent_calc_space_res(
+       struct xfs_mount        *mp,
+       unsigned int            namelen)
+{
+       /*
+        * Parent pointers are always the first attr in an attr tree, and never
+        * larger than a block
+        */
+       return XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK) +
+              XFS_NEXTENTADD_SPACE_RES(mp, namelen, XFS_ATTR_FORK);
+}
+
+unsigned int
+xfs_create_space_res(
+       struct xfs_mount        *mp,
+       unsigned int            namelen)
+{
+       unsigned int            ret;
+
+       ret = XFS_IALLOC_SPACE_RES(mp) + XFS_DIRENTER_SPACE_RES(mp, namelen);
+       if (xfs_has_parent(mp))
+               ret += xfs_parent_calc_space_res(mp, namelen);
+
+       return ret;
+}
+
+unsigned int
+xfs_mkdir_space_res(
+       struct xfs_mount        *mp,
+       unsigned int            namelen)
+{
+       return xfs_create_space_res(mp, namelen);
+}
index 9640fc232c1473e7e11ec00b7d596e11ee0af2fd..6cda87153b38cf4580eaf3a4a076f6623b1a2613 100644 (file)
@@ -80,8 +80,6 @@
 /* This macro is not used - see inline code in xfs_attr_set */
 #define        XFS_ATTRSET_SPACE_RES(mp, v)    \
        (XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK) + XFS_B_TO_FSB(mp, v))
-#define        XFS_CREATE_SPACE_RES(mp,nl)     \
-       (XFS_IALLOC_SPACE_RES(mp) + XFS_DIRENTER_SPACE_RES(mp,nl))
 #define        XFS_DIOSTRAT_SPACE_RES(mp, v)   \
        (XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK) + (v))
 #define        XFS_GROWFS_SPACE_RES(mp)        \
@@ -90,8 +88,6 @@
        ((b) + XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK))
 #define        XFS_LINK_SPACE_RES(mp,nl)       \
        XFS_DIRENTER_SPACE_RES(mp,nl)
-#define        XFS_MKDIR_SPACE_RES(mp,nl)      \
-       (XFS_IALLOC_SPACE_RES(mp) + XFS_DIRENTER_SPACE_RES(mp,nl))
 #define        XFS_QM_DQALLOC_SPACE_RES(mp)    \
        (XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK) + \
         XFS_DQUOT_CLUSTER_SIZE_FSB)
 #define XFS_IFREE_SPACE_RES(mp)                \
        (xfs_has_finobt(mp) ? M_IGEO(mp)->inobt_maxlevels : 0)
 
+unsigned int xfs_parent_calc_space_res(struct xfs_mount *mp,
+               unsigned int namelen);
+
+unsigned int xfs_create_space_res(struct xfs_mount *mp, unsigned int namelen);
+unsigned int xfs_mkdir_space_res(struct xfs_mount *mp, unsigned int namelen);
 
 #endif /* __XFS_TRANS_SPACE_H__ */
index e6103f76898809c1f09c15fc82917c67097859c7..c4260fb9187e30558d33c30c18ea2e92be7901e2 100644 (file)
@@ -927,7 +927,7 @@ mk_orphanage(xfs_mount_t *mp)
        /*
         * could not be found, create it
         */
-       nres = XFS_MKDIR_SPACE_RES(mp, xname.len);
+       nres = libxfs_mkdir_space_res(mp, xname.len);
        i = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_mkdir, nres, 0, 0, &tp);
        if (i)
                res_failed(i);
@@ -1343,7 +1343,7 @@ longform_dir2_rebuild(
                                                p->name.name[1] == '.'))))
                        continue;
 
-               nres = XFS_CREATE_SPACE_RES(mp, p->name.len);
+               nres = libxfs_create_space_res(mp, p->name.len);
                error = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_create,
                                            nres, 0, 0, &tp);
                if (error)
@@ -2955,7 +2955,7 @@ _("error %d fixing shortform directory %llu\n"),
 
                do_warn(_("recreating root directory .. entry\n"));
 
-               nres = XFS_MKDIR_SPACE_RES(mp, 2);
+               nres = libxfs_mkdir_space_res(mp, 2);
                error = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_mkdir,
                                            nres, 0, 0, &tp);
                if (error)
@@ -3010,7 +3010,7 @@ _("error %d fixing shortform directory %llu\n"),
                        do_warn(
        _("creating missing \".\" entry in dir ino %" PRIu64 "\n"), ino);
 
-                       nres = XFS_MKDIR_SPACE_RES(mp, 1);
+                       nres = libxfs_mkdir_space_res(mp, 1);
                        error = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_mkdir,
                                                    nres, 0, 0, &tp);
                        if (error)