]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs: create libxfs helper to link an existing inode into a directory
authorDarrick J. Wong <djwong@kernel.org>
Wed, 2 Oct 2024 01:14:36 +0000 (18:14 -0700)
committerAndrey Albershteyn <aalbersh@redhat.com>
Fri, 4 Oct 2024 10:42:07 +0000 (12:42 +0200)
Source kernel commit: c1f0bad4232fd309b2fe849153fcf473e775b1f7

Create a new libxfs function to link an existing inode into a directory.
The upcoming metadata directory feature will need this to create a
metadata directory tree.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
libxfs/xfs_dir2.c
libxfs/xfs_dir2.h

index e98b2802408a98c1cf22f2924634f653547cc217..802b9a1b3dbd2755fa6d20d759f0b03e9f52f8fb 100644 (file)
@@ -21,6 +21,7 @@
 #include "xfs_bmap_btree.h"
 #include "xfs_trans_space.h"
 #include "xfs_parent.h"
+#include "xfs_ag.h"
 
 const struct xfs_name xfs_name_dotdot = {
        .name   = (const unsigned char *)"..",
@@ -586,9 +587,9 @@ xfs_dir_replace(
  */
 int
 xfs_dir_canenter(
-       xfs_trans_t     *tp,
-       xfs_inode_t     *dp,
-       struct xfs_name *name)          /* name of entry to add */
+       struct xfs_trans        *tp,
+       struct xfs_inode        *dp,
+       const struct xfs_name   *name)          /* name of entry to add */
 {
        return xfs_dir_createname(tp, dp, name, 0, 0);
 }
@@ -808,3 +809,67 @@ xfs_dir_create_child(
 
        return 0;
 }
+
+/*
+ * Given a directory @dp, an existing non-directory inode @ip, and a @name,
+ * link @ip into @dp under the given @name.  Both inodes must have the ILOCK
+ * held.
+ */
+int
+xfs_dir_add_child(
+       struct xfs_trans        *tp,
+       unsigned int            resblks,
+       struct xfs_dir_update   *du)
+{
+       struct xfs_inode        *dp = du->dp;
+       const struct xfs_name   *name = du->name;
+       struct xfs_inode        *ip = du->ip;
+       struct xfs_mount        *mp = tp->t_mountp;
+       int                     error;
+
+       xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
+       xfs_assert_ilocked(dp, XFS_ILOCK_EXCL);
+       ASSERT(!S_ISDIR(VFS_I(ip)->i_mode));
+
+       if (!resblks) {
+               error = xfs_dir_canenter(tp, dp, name);
+               if (error)
+                       return error;
+       }
+
+       /*
+        * Handle initial link state of O_TMPFILE inode
+        */
+       if (VFS_I(ip)->i_nlink == 0) {
+               struct xfs_perag        *pag;
+
+               pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
+               error = xfs_iunlink_remove(tp, pag, ip);
+               xfs_perag_put(pag);
+               if (error)
+                       return error;
+       }
+
+       error = xfs_dir_createname(tp, dp, name, ip->i_ino, resblks);
+       if (error)
+               return error;
+
+       xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
+       xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
+
+       xfs_bumplink(tp, ip);
+
+       /*
+        * If we have parent pointers, we now need to add the parent record to
+        * the attribute fork of the inode. If this is the initial parent
+        * attribute, we need to create it correctly, otherwise we can just add
+        * the parent to the inode.
+        */
+       if (du->ppargs) {
+               error = xfs_parent_addname(tp, du->ppargs, dp, name, ip);
+               if (error)
+                       return error;
+       }
+
+       return 0;
+}
index a1ba6fd0a725fe20e3169bd60888d3acf12d1e3e..4f9711509571a1ab73e3e370e2439b7aeb24dc66 100644 (file)
@@ -74,7 +74,7 @@ extern int xfs_dir_replace(struct xfs_trans *tp, struct xfs_inode *dp,
                                const struct xfs_name *name, xfs_ino_t inum,
                                xfs_extlen_t tot);
 extern int xfs_dir_canenter(struct xfs_trans *tp, struct xfs_inode *dp,
-                               struct xfs_name *name);
+                               const struct xfs_name *name);
 
 int xfs_dir_lookup_args(struct xfs_da_args *args);
 int xfs_dir_createname_args(struct xfs_da_args *args);
@@ -320,5 +320,7 @@ struct xfs_dir_update {
 
 int xfs_dir_create_child(struct xfs_trans *tp, unsigned int resblks,
                struct xfs_dir_update *du);
+int xfs_dir_add_child(struct xfs_trans *tp, unsigned int resblks,
+               struct xfs_dir_update *du);
 
 #endif /* __XFS_DIR2_H__ */