]> www.infradead.org Git - users/hch/xfsprogs.git/commitdiff
xfs: parent pointer attribute creation
authorAllison Henderson <allison.henderson@oracle.com>
Tue, 9 Jan 2024 17:41:36 +0000 (09:41 -0800)
committerDarrick J. Wong <djwong@kernel.org>
Wed, 10 Apr 2024 00:21:29 +0000 (17:21 -0700)
Add parent pointer attribute during xfs_create, and subroutines to
initialize attributes

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>
include/libxfs.h
include/xfs_inode.h
libxfs/Makefile
libxfs/init.c
libxfs/libxfs_api_defs.h
libxfs/libxfs_priv.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 1636dda2f83d0c00f93e447ba85cc39e4941da22..86e4bd9bc1955bd609f1d03615214204981f2cbd 100644 (file)
@@ -91,6 +91,7 @@ struct iomap;
 #include "libxfs/buf_mem.h"
 #include "xfs_btree_mem.h"
 #include "xfs_ag_resv.h"
+#include "xfs_parent.h"
 
 #ifndef ARRAY_SIZE
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
index 987f86fcd3d527180e2d2453110e8afaaf026951..45339b42621a619b8ea061c27ccea93d1f0992f0 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 d0478960278a395d29620b63f2a5465ef2c3aaca..7bfdc15f61de54c774494f888e71222b90a163ff 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 71cd43d97672b682c9dad1a5014b4a4dababfcba..465d2e81add3f8ed7be81b2b3ed05463d3c126de 100644 (file)
 #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 5d4d46013eb080e0525696af96cd6653624bd7e2..ee8d4d4ad8ec13914ff4aef5008ee8e869fc898c 100644 (file)
@@ -568,8 +568,9 @@ struct xfs_item_ops;
 bool xfs_log_check_lsn(struct xfs_mount *, xfs_lsn_t);
 void xfs_log_item_init(struct xfs_mount *mp, struct xfs_log_item *lip, int type,
                const struct xfs_item_ops *ops);
-#define xfs_attr_use_log_assist(mp)    (0)
-#define xlog_drop_incompat_feat(log)   do { } while (0)
+#define xfs_attr_grab_log_assist(mp)   (0)
+#define xfs_attr_rele_log_assist(mp)   ((void) 0)
+#define xlog_drop_incompat_feat(log,w) do { } while (0)
 #define xfs_log_in_recovery(mp)                (false)
 
 /* xfs_icache.c */
index a18c13954748e50680bd5e0aff12cfc2c30c4c57..c467b8e27b6c46e6d0e20ca24ee54787bae571c6 100644 (file)
 #include "xfs_da_format.h"
 #include "xfs_format.h"
 #include "xfs_trans_space.h"
+#include "xfs_health.h"
+#include "defer_item.h"
+
+struct kmem_cache              *xfs_parent_args_cache;
 
 /*
  * Parent pointer attribute handling.
@@ -140,3 +144,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_parent(&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 7833e95474d3af0ae6e75f17c2cc5de2293c927f..91c36e79257a6fb0567bf9dc8fb51d87d8162105 100644 (file)
@@ -924,7 +924,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);
@@ -1335,7 +1335,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)
@@ -2946,7 +2946,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)
@@ -3001,7 +3001,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)